trafgen: add smoke test for drnd packets
[netsniff-ng.git] / src / aslookup.c
blob0f5da16375392d7c578100ef9779ccdc808a3988
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <netdb.h>
13 #include <netinet/in.h>
15 #include "die.h"
16 #include "xutils.h"
17 #include "aslookup.h"
18 #include "xmalloc.h"
19 #include "built_in.h"
21 enum parser_state {
22 STATE_AS,
23 STATE_IP,
24 STATE_BGP_PREFIX,
25 STATE_CC,
26 STATE_REGISTRY,
27 STATE_ALLOCATED,
28 STATE_AS_NAME,
31 static int ai_family = 0, ai_socktype = 0, ai_protocol = 0;
32 static struct sockaddr_storage ai_ss;
34 int aslookup_prepare(const char *server, const char *port)
36 int ret, fd = -1, try = 1;
37 struct addrinfo hints, *ahead, *ai;
39 bug_on(!server || !port);
41 memset(&ai_ss, 0, sizeof(ai_ss));
42 memset(&hints, 0, sizeof(hints));
44 hints.ai_family = PF_UNSPEC;
45 hints.ai_socktype = SOCK_STREAM;
46 hints.ai_protocol = IPPROTO_TCP;
47 hints.ai_flags = AI_NUMERICSERV;
49 ret = getaddrinfo(server, port, &hints, &ahead);
50 if (ret != 0) {
51 whine("Cannot get address info!\n");
52 return -EIO;
55 for (ai = ahead; ai != NULL && fd < 0; ai = ai->ai_next) {
56 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
57 if (fd < 0)
58 continue;
60 ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
61 if (ret < 0) {
62 whine("Cannot connect to remote, try %d: %s!\n",
63 try++, strerror(errno));
64 close(fd);
65 fd = -1;
66 continue;
69 ai_family = ai->ai_family;
70 ai_socktype = ai->ai_socktype;
71 ai_protocol = ai->ai_protocol;
72 memcpy(&ai_ss, ai->ai_addr, ai->ai_addrlen);
74 /* we know details for later connections, close for now */
75 close(fd);
76 break;
79 freeaddrinfo(ahead);
81 return 0;
84 int aslookup(const char *lhost, struct asrecord *rec)
86 char *buff;
87 int ret, fd;
88 size_t len = 1024;
90 bug_on(strlen(lhost) + 8 >= len);
92 fd = socket(ai_family, ai_socktype, ai_protocol);
93 if (unlikely(fd < 0))
94 panic("Cannot create socket!\n");
96 ret = connect(fd, (struct sockaddr *) &ai_ss, sizeof(ai_ss));
97 if (unlikely(ret < 0))
98 panic("Cannot connect to AS server!\n");
100 buff = xzmalloc(len);
101 slprintf(buff, len, "-v -f %s\r\n", lhost);
102 ret = write(fd, buff, strlen(buff));
103 if (unlikely(ret < 0))
104 panic("Cannot write to AS server!\n");
106 memset(buff, 0, len);
107 while ((ret = read(fd, buff, len)) > 0) {
108 int i;
109 enum parser_state state = STATE_AS;
110 char *ptr, *ptr2;
112 buff[len - 1] = 0;
113 for (i = 0, ptr = skips(buff); i < len; ++i) {
114 if (buff[i] == '|' && state == STATE_AS) {
115 buff[i] = 0;
116 ptr2 = &buff[i] - 1;
118 while (*ptr2 == ' ' && ptr2 > ptr) {
119 *ptr2 = 0;
120 ptr2--;
123 strlcpy(rec->number, ptr, strlen(ptr) + 1);
125 ptr = skips(&buff[i] + 1);
126 state = STATE_IP;
127 } else if (buff[i] == '|' && state == STATE_IP) {
128 buff[i] = 0;
129 ptr2 = &buff[i] - 1;
131 while (*ptr2 == ' ' && ptr2 > ptr) {
132 *ptr2 = 0;
133 ptr2--;
136 strlcpy(rec->ip, ptr, strlen(ptr) + 1);
138 ptr = skips(&buff[i] + 1);
139 state = STATE_BGP_PREFIX;
140 } else if (buff[i] == '|' &&
141 state == STATE_BGP_PREFIX) {
142 buff[i] = 0;
143 ptr2 = &buff[i] - 1;
145 while (*ptr2 == ' ' && ptr2 > ptr) {
146 *ptr2 = 0;
147 ptr2--;
150 strlcpy(rec->prefix, ptr, strlen(ptr) + 1);
152 ptr = skips(&buff[i] + 1);
153 state = STATE_CC;
154 } else if (buff[i] == '|' && state == STATE_CC) {
155 buff[i] = 0;
156 ptr2 = &buff[i] - 1;
158 while (*ptr2 == ' ' && ptr2 > ptr) {
159 *ptr2 = 0;
160 ptr2--;
163 strlcpy(rec->country, ptr, strlen(ptr) + 1);
165 ptr = skips(&buff[i] + 1);
166 state = STATE_REGISTRY;
167 } else if (buff[i] == '|' && state == STATE_REGISTRY) {
168 buff[i] = 0;
169 ptr2 = &buff[i] - 1;
171 while (*ptr2 == ' ' && ptr2 > ptr) {
172 *ptr2 = 0;
173 ptr2--;
176 strlcpy(rec->registry, ptr, strlen(ptr) + 1);
178 ptr = skips(&buff[i] + 1);
179 state = STATE_ALLOCATED;
180 } else if (buff[i] == '|' && state == STATE_ALLOCATED) {
181 buff[i] = 0;
182 ptr2 = &buff[i] - 1;
184 while (*ptr2 == ' ' && ptr2 > ptr) {
185 *ptr2 = 0;
186 ptr2--;
189 strlcpy(rec->since, ptr, strlen(ptr) + 1);
191 ptr = skips(&buff[i] + 1);
192 state = 6;
193 } else if (buff[i] == '\n' && state == STATE_AS_NAME) {
194 buff[i] = 0;
195 ptr2 = &buff[i] - 1;
197 while (*ptr2 == ' ' && ptr2 > ptr) {
198 *ptr2 = 0;
199 ptr2--;
202 strlcpy(rec->name, ptr, strlen(ptr) + 1);
204 goto out;
208 memset(buff, 0, len);
210 out:
211 close(fd);
212 xfree(buff);
214 return 0;