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