Output the proper number of names, the proper number of times.
[icmp-dn.git] / idnlookup.c
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 */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/ip.h>
28 #include <netinet/ip6.h>
29 #include <arpa/inet.h>
30 #include <sys/types.h>
31 #include <sys/poll.h>
32 #include <sys/time.h>
33
34 struct icmphdr {
35     u_int8_t type;
36     u_int8_t code;
37     u_int16_t checksum;
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;
52     u_int16_t id;
53     u_int16_t seq;
54     int32_t ttl;
55     
56 };
57
58 #define ICMP_NAMEREQ 37
59 #define ICMP_NAMEREP 38
60
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? */
66 void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst)
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             }
77             fprintf(f, "%.*s", (int)*p, p + 1);
78             p += 1 + (int)*p;
79             if(*p != 0)
80                 fprintf(f, ".");
81         }
82         p++;
83         fprintf(f, "\n");
84         if(onlyfirst)
85             break;
86     }
87 }
88
89 void cksum(void *hdr, size_t len)
90 {
91     struct icmphdr *ih;
92     u_int8_t *cb;
93     int i;
94     int b1, b2;
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];
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     }
119     cb = (u_int8_t *)&ih->checksum;
120     cb[0] = ~(u_int8_t)b1;
121     cb[1] = ~(u_int8_t)b2;
122 }
123
124 void usage(void)
125 {
126     fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
127 }
128
129 int main(int argc, char **argv)
130 {
131     int ret;
132     int s, c;
133     int id;
134     int namelen;
135     struct reqhdr req;
136     struct rephdr rep;
137     struct iphdr iphdr;
138     size_t hdrlen;
139     struct addrinfo *ai, *cai, aihint;
140     struct pollfd pfd;
141     struct timeval tvb, tvc;
142     struct sockaddr_storage name;
143     int timeout, dispttl, onlyfirst;
144     int elapsed, timedout, found;
145     
146     timeout = 3000;
147     dispttl = 0;
148     onlyfirst = 1;
149     while((c = getopt(argc, argv, "haTt:")) != -1) {
150         switch(c) {
151         case 't':
152             timeout = atoi(optarg);
153             break;
154         case 'a':
155             onlyfirst = 0;
156             break;
157         case 'T':
158             dispttl = 1;
159             break;
160         case 'h':
161         case '?':
162         case ':':
163         default:
164             usage();
165             exit((c == 'h')?0:1);
166         }
167     }
168     
169     if(argc - optind < 1) {
170         usage();
171         exit(1);
172     }
173     
174     memset(&aihint, 0, sizeof(aihint));
175     aihint.ai_socktype = SOCK_RAW;
176     aihint.ai_protocol = IPPROTO_ICMP;
177     ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
178     
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         
200         timedout = 0;
201         found = 0;
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);
208             if(elapsed >= timeout) {
209                 timedout = 1;
210                 break;
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;
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;
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;
240                     if(ret < sizeof(rep))
241                         continue;
242                     hdrlen = 0;
243                 } else {
244                     continue;
245                 }
246                 
247                 memcpy(&rep, buf + hdrlen, sizeof(rep));
248                 if(rep.type != ICMP_NAMEREP)
249                     continue;
250                 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
251                     continue;
252                 
253                 found = 1;
254                 break;
255             }
256         }
257         
258         close(s);
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);
271     }
272     
273     return(0);
274 }