Add potential buffer overrun check.
[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             if((int)*p + (p - dnbuf) >= size) {
82                 fprintf(stderr, "malformed domain name\n");
83                 return;
84             }
85             fprintf(f, "%.*s", (int)*p, p + 1);
86             p += 1 + (int)*p;
87             if(*p != 0)
88                 fprintf(f, ".");
89         }
90         p++;
91         fprintf(f, "\n");
92         if(onlyfirst)
93             break;
94     }
95 }
96
97 void cksum(void *hdr, size_t len)
98 {
99     struct icmphdr *ih;
100     u_int8_t *cb;
101     int i;
102     int b1, b2;
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];
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     }
127     cb = (u_int8_t *)&ih->checksum;
128     cb[0] = ~(u_int8_t)b1;
129     cb[1] = ~(u_int8_t)b2;
130 }
131
132 void usage(void)
133 {
134     fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
135 }
136
137 int main(int argc, char **argv)
138 {
139     int ret;
140     int s, c;
141     int id;
142     int namelen;
143     struct reqhdr req;
144     struct rephdr rep;
145     struct iphdr iphdr;
146     size_t hdrlen;
147     struct addrinfo *ai, *cai, aihint;
148     struct pollfd pfd;
149     struct timeval tvb, tvc;
150     struct sockaddr_storage name;
151     int timeout, dispttl, onlyfirst;
152     int elapsed, timedout, found;
153     
154     timeout = 3000;
155     dispttl = 0;
156     onlyfirst = 1;
157     while((c = getopt(argc, argv, "haTt:")) != -1) {
158         switch(c) {
159         case 't':
160             timeout = atoi(optarg);
161             break;
162         case 'a':
163             onlyfirst = 0;
164             break;
165         case 'T':
166             dispttl = 1;
167             break;
168         case 'h':
169         case '?':
170         case ':':
171         default:
172             usage();
173             exit((c == 'h')?0:1);
174         }
175     }
176     
177     if(argc - optind < 1) {
178         usage();
179         exit(1);
180     }
181     
182     memset(&aihint, 0, sizeof(aihint));
183     aihint.ai_socktype = SOCK_RAW;
184     aihint.ai_protocol = IPPROTO_ICMP;
185     ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
186     
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         
208         timedout = 0;
209         found = 0;
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);
216             if(elapsed >= timeout) {
217                 timedout = 1;
218                 break;
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;
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;
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;
248                     if(ret < sizeof(rep))
249                         continue;
250                     hdrlen = 0;
251                 } else {
252                     continue;
253                 }
254                 
255                 memcpy(&rep, buf + hdrlen, sizeof(rep));
256                 if(rep.type != ICMP_NAMEREP)
257                     continue;
258                 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
259                     continue;
260                 
261                 found = 1;
262                 break;
263             }
264         }
265         
266         close(s);
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);
279     }
280     
281     return(0);
282 }
283
284 /*
285  * Local Variables:
286  * compile-command: "make CFLAGS='-Wall -g'"
287  * End:
288  */