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