Commit | Line | Data |
---|---|---|
3d6143ec DC |
1 | /* |
2 | * idnlookup - ICMP Domain Name lookup utility for Linux | |
3 | * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
51a62941 DC |
19 | #include <stdlib.h> |
20 | #include <unistd.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <errno.h> | |
3d6143ec | 24 | #include <netdb.h> |
51a62941 DC |
25 | #include <sys/socket.h> |
26 | #include <netinet/in.h> | |
3d6143ec | 27 | #include <netinet/ip.h> |
c9d75321 | 28 | #include <netinet/ip6.h> |
51a62941 DC |
29 | #include <arpa/inet.h> |
30 | #include <sys/types.h> | |
3d6143ec DC |
31 | #include <sys/poll.h> |
32 | #include <sys/time.h> | |
51a62941 DC |
33 | |
34 | struct icmphdr { | |
35 | u_int8_t type; | |
36 | u_int8_t code; | |
37 | u_int16_t checksum; | |
3d6143ec DC |
38 | }; |
39 | ||
40 | struct reqhdr { | |
41 | u_int8_t type; | |
42 | u_int8_t code; | |
43 | u_int16_t checksum; | |
44 | u_int16_t id; | |
45 | u_int16_t seq; | |
46 | }; | |
47 | ||
48 | struct rephdr { | |
49 | u_int8_t type; | |
50 | u_int8_t code; | |
51 | u_int16_t checksum; | |
51a62941 DC |
52 | u_int16_t id; |
53 | u_int16_t seq; | |
3d6143ec DC |
54 | int32_t ttl; |
55 | ||
51a62941 DC |
56 | }; |
57 | ||
58 | #define ICMP_NAMEREQ 37 | |
59 | #define ICMP_NAMEREP 38 | |
60 | ||
3d6143ec DC |
61 | unsigned char buf[65536]; |
62 | ||
63 | /* DN decompression not yet implemented, since I don't know where to | |
64 | * begin counting the offset from -- the beginning of the ICMP | |
65 | * payload, or from the beginning of the DN data buffer? */ | |
c9d75321 | 66 | void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst) |
3d6143ec DC |
67 | { |
68 | unsigned char *p; | |
69 | ||
70 | p = dnbuf; | |
71 | while(p - dnbuf < size) { | |
72 | while(*p != 0) { | |
73 | if(*p & 0xc0) { | |
74 | fprintf(stderr, "domain name decompression not implemented, aborting\n"); | |
75 | exit(1); | |
76 | } | |
bfd59ca0 | 77 | fprintf(f, "%.*s", (int)*p, p + 1); |
3d6143ec | 78 | p += 1 + (int)*p; |
bfd59ca0 DC |
79 | if(*p != 0) |
80 | fprintf(f, "."); | |
3d6143ec DC |
81 | } |
82 | p++; | |
bfd59ca0 | 83 | fprintf(f, "\n"); |
c9d75321 DC |
84 | if(onlyfirst) |
85 | break; | |
3d6143ec DC |
86 | } |
87 | } | |
88 | ||
89 | void cksum(void *hdr, size_t len) | |
90 | { | |
91 | struct icmphdr *ih; | |
92 | u_int8_t *cb; | |
93 | int i; | |
9cc7607e | 94 | int b1, b2; |
3d6143ec DC |
95 | |
96 | ih = (struct icmphdr *)hdr; | |
97 | cb = (u_int8_t *)hdr; | |
98 | ih->checksum = 0; | |
99 | b1 = b2 = 0; | |
100 | for(i = 0; i < (len & ~1); i += 2) { | |
101 | b1 += cb[i]; | |
102 | b2 += cb[i + 1]; | |
103 | } | |
104 | if(i & 1) | |
105 | b1 += cb[len - 1]; | |
9cc7607e DC |
106 | while(1) { |
107 | if(b1 >= 256) { | |
108 | b2 += b1 >> 8; | |
109 | b1 &= 0xff; | |
110 | continue; | |
111 | } | |
112 | if(b2 >= 256) { | |
113 | b1 += b2 >> 8; | |
114 | b2 &= 0xff; | |
115 | continue; | |
116 | } | |
117 | break; | |
118 | } | |
3d6143ec | 119 | cb = (u_int8_t *)&ih->checksum; |
9cc7607e DC |
120 | cb[0] = ~(u_int8_t)b1; |
121 | cb[1] = ~(u_int8_t)b2; | |
3d6143ec DC |
122 | } |
123 | ||
124 | void usage(void) | |
125 | { | |
c9d75321 | 126 | fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n"); |
3d6143ec DC |
127 | } |
128 | ||
51a62941 DC |
129 | int main(int argc, char **argv) |
130 | { | |
131 | int ret; | |
3d6143ec DC |
132 | int s, c; |
133 | int id; | |
134 | int namelen; | |
135 | struct reqhdr req; | |
136 | struct rephdr rep; | |
137 | struct iphdr iphdr; | |
c9d75321 | 138 | size_t hdrlen; |
3d6143ec DC |
139 | struct addrinfo *ai, *cai, aihint; |
140 | struct pollfd pfd; | |
141 | struct timeval tvb, tvc; | |
142 | struct sockaddr_storage name; | |
c9d75321 DC |
143 | int timeout, dispttl, onlyfirst; |
144 | int elapsed, timedout, found; | |
51a62941 | 145 | |
3d6143ec | 146 | timeout = 3000; |
e172b003 | 147 | dispttl = 0; |
c9d75321 DC |
148 | onlyfirst = 1; |
149 | while((c = getopt(argc, argv, "haTt:")) != -1) { | |
3d6143ec DC |
150 | switch(c) { |
151 | case 't': | |
152 | timeout = atoi(optarg); | |
153 | break; | |
c9d75321 DC |
154 | case 'a': |
155 | onlyfirst = 0; | |
156 | break; | |
e172b003 DC |
157 | case 'T': |
158 | dispttl = 1; | |
159 | break; | |
3d6143ec DC |
160 | case 'h': |
161 | case '?': | |
162 | case ':': | |
163 | default: | |
164 | usage(); | |
165 | exit((c == 'h')?0:1); | |
166 | } | |
51a62941 DC |
167 | } |
168 | ||
3d6143ec DC |
169 | if(argc - optind < 1) { |
170 | usage(); | |
171 | exit(1); | |
172 | } | |
51a62941 | 173 | |
3d6143ec | 174 | memset(&aihint, 0, sizeof(aihint)); |
3d6143ec DC |
175 | aihint.ai_socktype = SOCK_RAW; |
176 | aihint.ai_protocol = IPPROTO_ICMP; | |
177 | ret = getaddrinfo(argv[optind], NULL, &aihint, &ai); | |
51a62941 | 178 | |
3d6143ec DC |
179 | for(cai = ai; cai != NULL; cai = cai->ai_next) { |
180 | if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) { | |
181 | perror("could not create raw socket"); | |
182 | exit(1); | |
183 | } | |
184 | ||
185 | id = random() % 65536; | |
186 | memset(&req, 0, sizeof(req)); | |
187 | req.type = ICMP_NAMEREQ; | |
188 | req.id = htons(id); | |
189 | cksum(&req, sizeof(req)); | |
190 | ||
191 | ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen); | |
192 | if(ret < 0) { | |
193 | perror("sendto"); | |
194 | exit(1); | |
195 | } else if(ret != sizeof(req)) { | |
196 | fprintf(stderr, "socket would not send entire packet\n"); | |
197 | exit(1); | |
198 | } | |
199 | ||
c9d75321 DC |
200 | timedout = 0; |
201 | found = 0; | |
3d6143ec DC |
202 | gettimeofday(&tvb, NULL); |
203 | while(1) { | |
204 | pfd.fd = s; | |
205 | pfd.events = POLLIN; | |
206 | gettimeofday(&tvc, NULL); | |
207 | elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000); | |
899ad19e | 208 | if(elapsed >= timeout) { |
c9d75321 DC |
209 | timedout = 1; |
210 | break; | |
3d6143ec DC |
211 | } |
212 | ret = poll(&pfd, 1, timeout - elapsed); | |
213 | if(ret < 0) { | |
214 | perror("idnlookup: reading data"); | |
215 | exit(1); | |
216 | } | |
217 | ||
218 | if(pfd.revents & POLLIN) { | |
219 | namelen = sizeof(name); | |
220 | ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen); | |
221 | if(ret < 0) { | |
222 | perror("idnlookup: receiving data"); | |
223 | exit(1); | |
224 | } | |
225 | ||
226 | if(name.ss_family != cai->ai_addr->sa_family) | |
227 | continue; | |
228 | if(name.ss_family == AF_INET) { | |
229 | if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr))) | |
230 | continue; | |
c9d75321 DC |
231 | if(ret < sizeof(iphdr) + sizeof(rep)) |
232 | continue; | |
233 | hdrlen = sizeof(iphdr); | |
234 | memcpy(&iphdr, buf, sizeof(iphdr)); | |
235 | if(iphdr.protocol != IPPROTO_ICMP) | |
236 | continue; | |
3d6143ec DC |
237 | } else if(name.ss_family == AF_INET6) { |
238 | if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr))) | |
239 | continue; | |
c9d75321 DC |
240 | if(ret < sizeof(rep)) |
241 | continue; | |
242 | hdrlen = 0; | |
3d6143ec DC |
243 | } else { |
244 | continue; | |
245 | } | |
246 | ||
c9d75321 | 247 | memcpy(&rep, buf + hdrlen, sizeof(rep)); |
3d6143ec DC |
248 | if(rep.type != ICMP_NAMEREP) |
249 | continue; | |
250 | if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0))) | |
251 | continue; | |
252 | ||
c9d75321 | 253 | found = 1; |
3d6143ec DC |
254 | break; |
255 | } | |
256 | } | |
257 | ||
3d6143ec | 258 | close(s); |
c9d75321 DC |
259 | |
260 | if(found) { | |
261 | if(dispttl) | |
262 | printf("%i\n", ntohl(rep.ttl)); | |
263 | printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst); | |
264 | break; | |
265 | } | |
266 | } | |
267 | ||
268 | if(timedout) { | |
269 | fprintf(stderr, "idnlookup: timeout\n"); | |
270 | exit(1); | |
51a62941 | 271 | } |
3d6143ec | 272 | |
51a62941 DC |
273 | return(0); |
274 | } |