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