1727c4dedc20c26e0b00114961bdbb22d434f49d
[icmp-dn.git] / src / idnlookup.c
1 /*
2  *  idnlookup - ICMP Domain Name lookup utility for Linux
3  *  Should be installed SUID root, even though I don't know if it's secure yet. :-)
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 */
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <netinet/ip.h>
29 #include <netinet/ip6.h>
30 #include <arpa/inet.h>
31 #include <sys/types.h>
32 #include <sys/poll.h>
33 #include <sys/time.h>
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 struct icmphdr {
39     u_int8_t type;
40     u_int8_t code;
41     u_int16_t checksum;
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;
56     u_int16_t id;
57     u_int16_t seq;
58     int32_t ttl;
59     
60 };
61
62 #define ICMP_NAMEREQ 37
63 #define ICMP_NAMEREP 38
64
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? */
70 void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst)
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             }
81             fprintf(f, "%.*s", (int)*p, p + 1);
82             p += 1 + (int)*p;
83             if(*p != 0)
84                 fprintf(f, ".");
85         }
86         p++;
87         fprintf(f, "\n");
88         if(onlyfirst)
89             break;
90     }
91 }
92
93 void cksum(void *hdr, size_t len)
94 {
95     struct icmphdr *ih;
96     u_int8_t *cb;
97     int i;
98     int b1, b2;
99     
100     ih = (struct icmphdr *)hdr;
101     cb = (u_int8_t *)hdr;
102     ih->checksum = 0;
103     b1 = b2 = 0;
104     for(i = 0; i < (len & ~1); i += 2) {
105         b1 += cb[i];
106         b2 += cb[i + 1];
107     }
108     if(i & 1)
109         b1 += cb[len - 1];
110     while(1) {
111         if(b1 >= 256) {
112             b2 += b1 >> 8;
113             b1 &= 0xff;
114             continue;
115         }
116         if(b2 >= 256) {
117             b1 += b2 >> 8;
118             b2 &= 0xff;
119             continue;
120         }
121         break;
122     }
123     cb = (u_int8_t *)&ih->checksum;
124     cb[0] = ~(u_int8_t)b1;
125     cb[1] = ~(u_int8_t)b2;
126 }
127
128 void usage(void)
129 {
130     fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
131 }
132
133 int main(int argc, char **argv)
134 {
135     int ret;
136     int s, c;
137     int id;
138     int namelen;
139     struct reqhdr req;
140     struct rephdr rep;
141     struct iphdr iphdr;
142     size_t hdrlen;
143     struct addrinfo *ai, *cai, aihint;
144     struct pollfd pfd;
145     struct timeval tvb, tvc;
146     struct sockaddr_storage name;
147     int timeout, dispttl, onlyfirst;
148     int elapsed, timedout, found;
149     
150     timeout = 3000;
151     dispttl = 0;
152     onlyfirst = 1;
153     while((c = getopt(argc, argv, "haTt:")) != -1) {
154         switch(c) {
155         case 't':
156             timeout = atoi(optarg);
157             break;
158         case 'a':
159             onlyfirst = 0;
160             break;
161         case 'T':
162             dispttl = 1;
163             break;
164         case 'h':
165         case '?':
166         case ':':
167         default:
168             usage();
169             exit((c == 'h')?0:1);
170         }
171     }
172     
173     if(argc - optind < 1) {
174         usage();
175         exit(1);
176     }
177     
178     memset(&aihint, 0, sizeof(aihint));
179     aihint.ai_socktype = SOCK_RAW;
180     aihint.ai_protocol = IPPROTO_ICMP;
181     ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
182     
183     for(cai = ai; cai != NULL; cai = cai->ai_next) {
184         if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) {
185             perror("could not create raw socket");
186             exit(1);
187         }
188         
189         id = random() % 65536;
190         memset(&req, 0, sizeof(req));
191         req.type = ICMP_NAMEREQ;
192         req.id = htons(id);
193         cksum(&req, sizeof(req));
194         
195         ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen);
196         if(ret < 0) {
197             perror("sendto");
198             exit(1);
199         } else if(ret != sizeof(req)) {
200             fprintf(stderr, "socket would not send entire packet\n");
201             exit(1);
202         }
203         
204         timedout = 0;
205         found = 0;
206         gettimeofday(&tvb, NULL);
207         while(1) {
208             pfd.fd = s;
209             pfd.events = POLLIN;
210             gettimeofday(&tvc, NULL);
211             elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000);
212             if(elapsed >= timeout) {
213                 timedout = 1;
214                 break;
215             }
216             ret = poll(&pfd, 1, timeout - elapsed);
217             if(ret < 0) {
218                 perror("idnlookup: reading data");
219                 exit(1);
220             }
221             
222             if(pfd.revents & POLLIN) {
223                 namelen = sizeof(name);
224                 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
225                 if(ret < 0) {
226                     perror("idnlookup: receiving data");
227                     exit(1);
228                 }
229                 
230                 if(name.ss_family != cai->ai_addr->sa_family)
231                     continue;
232                 if(name.ss_family == AF_INET) {
233                     if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr)))
234                         continue;
235                     if(ret < sizeof(iphdr) + sizeof(rep))
236                         continue;
237                     hdrlen = sizeof(iphdr);
238                     memcpy(&iphdr, buf, sizeof(iphdr));
239                     if(iphdr.protocol != IPPROTO_ICMP)
240                         continue;
241                 } else if(name.ss_family == AF_INET6) {
242                     if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr)))
243                         continue;
244                     if(ret < sizeof(rep))
245                         continue;
246                     hdrlen = 0;
247                 } else {
248                     continue;
249                 }
250                 
251                 memcpy(&rep, buf + hdrlen, sizeof(rep));
252                 if(rep.type != ICMP_NAMEREP)
253                     continue;
254                 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
255                     continue;
256                 
257                 found = 1;
258                 break;
259             }
260         }
261         
262         close(s);
263         
264         if(found) {
265             if(dispttl)
266                 printf("%i\n", ntohl(rep.ttl));
267             printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst);
268             break;
269         }
270     }
271     
272     if(timedout) {
273         fprintf(stderr, "idnlookup: timeout\n");
274         exit(1);
275     }
276     
277     return(0);
278 }
279
280 /*
281  * Local Variables:
282  * compile-command: "gcc -Wall -g -o idnlookup idnlookup.c"
283  * End:
284  */