Incorporate make system changes by Oron Peled.
[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 #include "config.h"
35
36 struct icmphdr {
37     u_int8_t type;
38     u_int8_t code;
39     u_int16_t checksum;
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;
54     u_int16_t id;
55     u_int16_t seq;
56     int32_t ttl;
57     
58 };
59
60 #define ICMP_NAMEREQ 37
61 #define ICMP_NAMEREP 38
62
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? */
68 void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst)
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             }
79             fprintf(f, "%.*s", (int)*p, p + 1);
80             p += 1 + (int)*p;
81             if(*p != 0)
82                 fprintf(f, ".");
83         }
84         p++;
85         fprintf(f, "\n");
86         if(onlyfirst)
87             break;
88     }
89 }
90
91 void cksum(void *hdr, size_t len)
92 {
93     struct icmphdr *ih;
94     u_int8_t *cb;
95     int i;
96     int b1, b2;
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];
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     }
121     cb = (u_int8_t *)&ih->checksum;
122     cb[0] = ~(u_int8_t)b1;
123     cb[1] = ~(u_int8_t)b2;
124 }
125
126 void usage(void)
127 {
128     fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
129 }
130
131 int main(int argc, char **argv)
132 {
133     int ret;
134     int s, c;
135     int id;
136     int namelen;
137     struct reqhdr req;
138     struct rephdr rep;
139     struct iphdr iphdr;
140     size_t hdrlen;
141     struct addrinfo *ai, *cai, aihint;
142     struct pollfd pfd;
143     struct timeval tvb, tvc;
144     struct sockaddr_storage name;
145     int timeout, dispttl, onlyfirst;
146     int elapsed, timedout, found;
147     
148     timeout = 3000;
149     dispttl = 0;
150     onlyfirst = 1;
151     while((c = getopt(argc, argv, "haTt:")) != -1) {
152         switch(c) {
153         case 't':
154             timeout = atoi(optarg);
155             break;
156         case 'a':
157             onlyfirst = 0;
158             break;
159         case 'T':
160             dispttl = 1;
161             break;
162         case 'h':
163         case '?':
164         case ':':
165         default:
166             usage();
167             exit((c == 'h')?0:1);
168         }
169     }
170     
171     if(argc - optind < 1) {
172         usage();
173         exit(1);
174     }
175     
176     memset(&aihint, 0, sizeof(aihint));
177     aihint.ai_socktype = SOCK_RAW;
178     aihint.ai_protocol = IPPROTO_ICMP;
179     ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
180     
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         
202         timedout = 0;
203         found = 0;
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);
210             if(elapsed >= timeout) {
211                 timedout = 1;
212                 break;
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;
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;
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;
242                     if(ret < sizeof(rep))
243                         continue;
244                     hdrlen = 0;
245                 } else {
246                     continue;
247                 }
248                 
249                 memcpy(&rep, buf + hdrlen, sizeof(rep));
250                 if(rep.type != ICMP_NAMEREP)
251                     continue;
252                 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
253                     continue;
254                 
255                 found = 1;
256                 break;
257             }
258         }
259         
260         close(s);
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);
273     }
274     
275     return(0);
276 }
277
278 /*
279  * Local Variables:
280  * compile-command: "gcc -Wall -g -o idnlookup idnlookup.c"
281  * End:
282  */