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.
13 #include <netinet/in.h>
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
);
51 whine("Cannot get address info!\n");
55 for (ai
= ahead
; ai
!= NULL
&& fd
< 0; ai
= ai
->ai_next
) {
56 fd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
60 ret
= connect(fd
, ai
->ai_addr
, ai
->ai_addrlen
);
62 whine("Cannot connect to remote, try %d: %s!\n",
63 try++, strerror(errno
));
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 */
84 int aslookup(const char *lhost
, struct asrecord
*rec
)
90 bug_on(strlen(lhost
) + 8 >= len
);
92 fd
= socket(ai_family
, ai_socktype
, ai_protocol
);
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) {
109 enum parser_state state
= STATE_AS
;
113 for (i
= 0, ptr
= skips(buff
); i
< len
; ++i
) {
114 if (buff
[i
] == '|' && state
== STATE_AS
) {
118 while (*ptr2
== ' ' && ptr2
> ptr
) {
123 strlcpy(rec
->number
, ptr
, strlen(ptr
) + 1);
125 ptr
= skips(&buff
[i
] + 1);
127 } else if (buff
[i
] == '|' && state
== STATE_IP
) {
131 while (*ptr2
== ' ' && ptr2
> ptr
) {
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
) {
145 while (*ptr2
== ' ' && ptr2
> ptr
) {
150 strlcpy(rec
->prefix
, ptr
, strlen(ptr
) + 1);
152 ptr
= skips(&buff
[i
] + 1);
154 } else if (buff
[i
] == '|' && state
== STATE_CC
) {
158 while (*ptr2
== ' ' && ptr2
> ptr
) {
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
) {
171 while (*ptr2
== ' ' && ptr2
> ptr
) {
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
) {
184 while (*ptr2
== ' ' && ptr2
> ptr
) {
189 strlcpy(rec
->since
, ptr
, strlen(ptr
) + 1);
191 ptr
= skips(&buff
[i
] + 1);
193 } else if (buff
[i
] == '\n' && state
== STATE_AS_NAME
) {
197 while (*ptr2
== ' ' && ptr2
> ptr
) {
202 strlcpy(rec
->name
, ptr
, strlen(ptr
) + 1);
208 memset(buff
, 0, len
);