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> | |
25415ec8 | 34 | #ifdef HAVE_CONFIG_H |
2269406c | 35 | #include "config.h" |
25415ec8 | 36 | #endif |
51a62941 DC |
37 | |
38 | struct icmphdr { | |
39 | u_int8_t type; | |
40 | u_int8_t code; | |
41 | u_int16_t checksum; | |
3d6143ec DC |
42 | }; |
43 | ||
44 | struct reqhdr { | |
45 | u_int8_t type; | |
46 | u_int8_t code; | |
47 | u_int16_t checksum; | |
48 | u_int16_t id; | |
49 | u_int16_t seq; | |
50 | }; | |
51 | ||
52 | struct rephdr { | |
53 | u_int8_t type; | |
54 | u_int8_t code; | |
55 | u_int16_t checksum; | |
51a62941 DC |
56 | u_int16_t id; |
57 | u_int16_t seq; | |
3d6143ec DC |
58 | int32_t ttl; |
59 | ||
51a62941 DC |
60 | }; |
61 | ||
62 | #define ICMP_NAMEREQ 37 | |
63 | #define ICMP_NAMEREP 38 | |
64 | ||
3d6143ec DC |
65 | unsigned char buf[65536]; |
66 | ||
67 | /* DN decompression not yet implemented, since I don't know where to | |
68 | * begin counting the offset from -- the beginning of the ICMP | |
69 | * payload, or from the beginning of the DN data buffer? */ | |
c9d75321 | 70 | void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst) |
3d6143ec DC |
71 | { |
72 | unsigned char *p; | |
73 | ||
74 | p = dnbuf; | |
75 | while(p - dnbuf < size) { | |
76 | while(*p != 0) { | |
77 | if(*p & 0xc0) { | |
78 | fprintf(stderr, "domain name decompression not implemented, aborting\n"); | |
79 | exit(1); | |
80 | } | |
9533e44b | 81 | if((int)*p + (p - dnbuf) >= size) { |
82 | fprintf(stderr, "malformed domain name\n"); | |
83 | return; | |
84 | } | |
bfd59ca0 | 85 | fprintf(f, "%.*s", (int)*p, p + 1); |
3d6143ec | 86 | p += 1 + (int)*p; |
bfd59ca0 DC |
87 | if(*p != 0) |
88 | fprintf(f, "."); | |
3d6143ec DC |
89 | } |
90 | p++; | |
bfd59ca0 | 91 | fprintf(f, "\n"); |
c9d75321 DC |
92 | if(onlyfirst) |
93 | break; | |
3d6143ec DC |
94 | } |
95 | } | |
96 | ||
97 | void cksum(void *hdr, size_t len) | |
98 | { | |
99 | struct icmphdr *ih; | |
100 | u_int8_t *cb; | |
101 | int i; | |
9cc7607e | 102 | int b1, b2; |
3d6143ec DC |
103 | |
104 | ih = (struct icmphdr *)hdr; | |
105 | cb = (u_int8_t *)hdr; | |
106 | ih->checksum = 0; | |
107 | b1 = b2 = 0; | |
108 | for(i = 0; i < (len & ~1); i += 2) { | |
109 | b1 += cb[i]; | |
110 | b2 += cb[i + 1]; | |
111 | } | |
112 | if(i & 1) | |
113 | b1 += cb[len - 1]; | |
9cc7607e DC |
114 | while(1) { |
115 | if(b1 >= 256) { | |
116 | b2 += b1 >> 8; | |
117 | b1 &= 0xff; | |
118 | continue; | |
119 | } | |
120 | if(b2 >= 256) { | |
121 | b1 += b2 >> 8; | |
122 | b2 &= 0xff; | |
123 | continue; | |
124 | } | |
125 | break; | |
126 | } | |
3d6143ec | 127 | cb = (u_int8_t *)&ih->checksum; |
9cc7607e DC |
128 | cb[0] = ~(u_int8_t)b1; |
129 | cb[1] = ~(u_int8_t)b2; | |
3d6143ec DC |
130 | } |
131 | ||
132 | void usage(void) | |
133 | { | |
c9d75321 | 134 | fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n"); |
3d6143ec DC |
135 | } |
136 | ||
51a62941 DC |
137 | int main(int argc, char **argv) |
138 | { | |
139 | int ret; | |
3d6143ec DC |
140 | int s, c; |
141 | int id; | |
142 | int namelen; | |
143 | struct reqhdr req; | |
144 | struct rephdr rep; | |
145 | struct iphdr iphdr; | |
c9d75321 | 146 | size_t hdrlen; |
3d6143ec DC |
147 | struct addrinfo *ai, *cai, aihint; |
148 | struct pollfd pfd; | |
149 | struct timeval tvb, tvc; | |
150 | struct sockaddr_storage name; | |
c9d75321 DC |
151 | int timeout, dispttl, onlyfirst; |
152 | int elapsed, timedout, found; | |
51a62941 | 153 | |
3d6143ec | 154 | timeout = 3000; |
e172b003 | 155 | dispttl = 0; |
c9d75321 DC |
156 | onlyfirst = 1; |
157 | while((c = getopt(argc, argv, "haTt:")) != -1) { | |
3d6143ec DC |
158 | switch(c) { |
159 | case 't': | |
160 | timeout = atoi(optarg); | |
161 | break; | |
c9d75321 DC |
162 | case 'a': |
163 | onlyfirst = 0; | |
164 | break; | |
e172b003 DC |
165 | case 'T': |
166 | dispttl = 1; | |
167 | break; | |
3d6143ec DC |
168 | case 'h': |
169 | case '?': | |
170 | case ':': | |
171 | default: | |
172 | usage(); | |
173 | exit((c == 'h')?0:1); | |
174 | } | |
51a62941 DC |
175 | } |
176 | ||
3d6143ec DC |
177 | if(argc - optind < 1) { |
178 | usage(); | |
179 | exit(1); | |
180 | } | |
51a62941 | 181 | |
3d6143ec | 182 | memset(&aihint, 0, sizeof(aihint)); |
3d6143ec DC |
183 | aihint.ai_socktype = SOCK_RAW; |
184 | aihint.ai_protocol = IPPROTO_ICMP; | |
185 | ret = getaddrinfo(argv[optind], NULL, &aihint, &ai); | |
51a62941 | 186 | |
3d6143ec DC |
187 | for(cai = ai; cai != NULL; cai = cai->ai_next) { |
188 | if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) { | |
189 | perror("could not create raw socket"); | |
190 | exit(1); | |
191 | } | |
192 | ||
193 | id = random() % 65536; | |
194 | memset(&req, 0, sizeof(req)); | |
195 | req.type = ICMP_NAMEREQ; | |
196 | req.id = htons(id); | |
197 | cksum(&req, sizeof(req)); | |
198 | ||
199 | ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen); | |
200 | if(ret < 0) { | |
201 | perror("sendto"); | |
202 | exit(1); | |
203 | } else if(ret != sizeof(req)) { | |
204 | fprintf(stderr, "socket would not send entire packet\n"); | |
205 | exit(1); | |
206 | } | |
207 | ||
c9d75321 DC |
208 | timedout = 0; |
209 | found = 0; | |
3d6143ec DC |
210 | gettimeofday(&tvb, NULL); |
211 | while(1) { | |
212 | pfd.fd = s; | |
213 | pfd.events = POLLIN; | |
214 | gettimeofday(&tvc, NULL); | |
215 | elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000); | |
899ad19e | 216 | if(elapsed >= timeout) { |
c9d75321 DC |
217 | timedout = 1; |
218 | break; | |
3d6143ec DC |
219 | } |
220 | ret = poll(&pfd, 1, timeout - elapsed); | |
221 | if(ret < 0) { | |
222 | perror("idnlookup: reading data"); | |
223 | exit(1); | |
224 | } | |
225 | ||
226 | if(pfd.revents & POLLIN) { | |
227 | namelen = sizeof(name); | |
228 | ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen); | |
229 | if(ret < 0) { | |
230 | perror("idnlookup: receiving data"); | |
231 | exit(1); | |
232 | } | |
233 | ||
234 | if(name.ss_family != cai->ai_addr->sa_family) | |
235 | continue; | |
236 | if(name.ss_family == AF_INET) { | |
237 | if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr))) | |
238 | continue; | |
c9d75321 DC |
239 | if(ret < sizeof(iphdr) + sizeof(rep)) |
240 | continue; | |
241 | hdrlen = sizeof(iphdr); | |
242 | memcpy(&iphdr, buf, sizeof(iphdr)); | |
243 | if(iphdr.protocol != IPPROTO_ICMP) | |
244 | continue; | |
3d6143ec DC |
245 | } else if(name.ss_family == AF_INET6) { |
246 | if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr))) | |
247 | continue; | |
c9d75321 DC |
248 | if(ret < sizeof(rep)) |
249 | continue; | |
250 | hdrlen = 0; | |
3d6143ec DC |
251 | } else { |
252 | continue; | |
253 | } | |
254 | ||
c9d75321 | 255 | memcpy(&rep, buf + hdrlen, sizeof(rep)); |
3d6143ec DC |
256 | if(rep.type != ICMP_NAMEREP) |
257 | continue; | |
258 | if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0))) | |
259 | continue; | |
260 | ||
c9d75321 | 261 | found = 1; |
3d6143ec DC |
262 | break; |
263 | } | |
264 | } | |
265 | ||
3d6143ec | 266 | close(s); |
c9d75321 DC |
267 | |
268 | if(found) { | |
269 | if(dispttl) | |
270 | printf("%i\n", ntohl(rep.ttl)); | |
271 | printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst); | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | if(timedout) { | |
277 | fprintf(stderr, "idnlookup: timeout\n"); | |
278 | exit(1); | |
51a62941 | 279 | } |
3d6143ec | 280 | |
51a62941 DC |
281 | return(0); |
282 | } | |
ecbdf1d6 DC |
283 | |
284 | /* | |
285 | * Local Variables: | |
9155cd45 | 286 | * compile-command: "make CFLAGS='-Wall -g'" |
ecbdf1d6 DC |
287 | * End: |
288 | */ |