Fixed the compile-command.
[icmp-dn.git] / src / idnlookup.c
CommitLineData
3d6143ec
DC
1/*
2 * idnlookup - ICMP Domain Name lookup utility for Linux
ecbdf1d6 3 * Should be installed SUID root, even though I don't know if it's secure yet. :-)
3d6143ec
DC
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*/
51a62941
DC
20#include <stdlib.h>
21#include <unistd.h>
22#include <stdio.h>
23#include <string.h>
24#include <errno.h>
3d6143ec 25#include <netdb.h>
51a62941
DC
26#include <sys/socket.h>
27#include <netinet/in.h>
3d6143ec 28#include <netinet/ip.h>
c9d75321 29#include <netinet/ip6.h>
51a62941
DC
30#include <arpa/inet.h>
31#include <sys/types.h>
3d6143ec
DC
32#include <sys/poll.h>
33#include <sys/time.h>
25415ec8 34#ifdef HAVE_CONFIG_H
2269406c 35#include "config.h"
25415ec8 36#endif
51a62941
DC
37
38struct icmphdr {
39 u_int8_t type;
40 u_int8_t code;
41 u_int16_t checksum;
3d6143ec
DC
42};
43
44struct 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
52struct rephdr {
53 u_int8_t type;
54 u_int8_t code;
55 u_int16_t checksum;
51a62941
DC
56 u_int16_t id;
57 u_int16_t seq;
3d6143ec
DC
58 int32_t ttl;
59
51a62941
DC
60};
61
62#define ICMP_NAMEREQ 37
63#define ICMP_NAMEREP 38
64
3d6143ec
DC
65unsigned 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? */
c9d75321 70void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst)
3d6143ec
DC
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 }
bfd59ca0 81 fprintf(f, "%.*s", (int)*p, p + 1);
3d6143ec 82 p += 1 + (int)*p;
bfd59ca0
DC
83 if(*p != 0)
84 fprintf(f, ".");
3d6143ec
DC
85 }
86 p++;
bfd59ca0 87 fprintf(f, "\n");
c9d75321
DC
88 if(onlyfirst)
89 break;
3d6143ec
DC
90 }
91}
92
93void cksum(void *hdr, size_t len)
94{
95 struct icmphdr *ih;
96 u_int8_t *cb;
97 int i;
9cc7607e 98 int b1, b2;
3d6143ec
DC
99
100 ih = (struct icmphdr *)hdr;
101 cb = (u_int8_t *)hdr;
102 ih->checksum = 0;
103 b1 = b2 = 0;
104 for(i = 0; i < (len & ~1); i += 2) {
105 b1 += cb[i];
106 b2 += cb[i + 1];
107 }
108 if(i & 1)
109 b1 += cb[len - 1];
9cc7607e
DC
110 while(1) {
111 if(b1 >= 256) {
112 b2 += b1 >> 8;
113 b1 &= 0xff;
114 continue;
115 }
116 if(b2 >= 256) {
117 b1 += b2 >> 8;
118 b2 &= 0xff;
119 continue;
120 }
121 break;
122 }
3d6143ec 123 cb = (u_int8_t *)&ih->checksum;
9cc7607e
DC
124 cb[0] = ~(u_int8_t)b1;
125 cb[1] = ~(u_int8_t)b2;
3d6143ec
DC
126}
127
128void usage(void)
129{
c9d75321 130 fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
3d6143ec
DC
131}
132
51a62941
DC
133int main(int argc, char **argv)
134{
135 int ret;
3d6143ec
DC
136 int s, c;
137 int id;
138 int namelen;
139 struct reqhdr req;
140 struct rephdr rep;
141 struct iphdr iphdr;
c9d75321 142 size_t hdrlen;
3d6143ec
DC
143 struct addrinfo *ai, *cai, aihint;
144 struct pollfd pfd;
145 struct timeval tvb, tvc;
146 struct sockaddr_storage name;
c9d75321
DC
147 int timeout, dispttl, onlyfirst;
148 int elapsed, timedout, found;
51a62941 149
3d6143ec 150 timeout = 3000;
e172b003 151 dispttl = 0;
c9d75321
DC
152 onlyfirst = 1;
153 while((c = getopt(argc, argv, "haTt:")) != -1) {
3d6143ec
DC
154 switch(c) {
155 case 't':
156 timeout = atoi(optarg);
157 break;
c9d75321
DC
158 case 'a':
159 onlyfirst = 0;
160 break;
e172b003
DC
161 case 'T':
162 dispttl = 1;
163 break;
3d6143ec
DC
164 case 'h':
165 case '?':
166 case ':':
167 default:
168 usage();
169 exit((c == 'h')?0:1);
170 }
51a62941
DC
171 }
172
3d6143ec
DC
173 if(argc - optind < 1) {
174 usage();
175 exit(1);
176 }
51a62941 177
3d6143ec 178 memset(&aihint, 0, sizeof(aihint));
3d6143ec
DC
179 aihint.ai_socktype = SOCK_RAW;
180 aihint.ai_protocol = IPPROTO_ICMP;
181 ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
51a62941 182
3d6143ec
DC
183 for(cai = ai; cai != NULL; cai = cai->ai_next) {
184 if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) {
185 perror("could not create raw socket");
186 exit(1);
187 }
188
189 id = random() % 65536;
190 memset(&req, 0, sizeof(req));
191 req.type = ICMP_NAMEREQ;
192 req.id = htons(id);
193 cksum(&req, sizeof(req));
194
195 ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen);
196 if(ret < 0) {
197 perror("sendto");
198 exit(1);
199 } else if(ret != sizeof(req)) {
200 fprintf(stderr, "socket would not send entire packet\n");
201 exit(1);
202 }
203
c9d75321
DC
204 timedout = 0;
205 found = 0;
3d6143ec
DC
206 gettimeofday(&tvb, NULL);
207 while(1) {
208 pfd.fd = s;
209 pfd.events = POLLIN;
210 gettimeofday(&tvc, NULL);
211 elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000);
899ad19e 212 if(elapsed >= timeout) {
c9d75321
DC
213 timedout = 1;
214 break;
3d6143ec
DC
215 }
216 ret = poll(&pfd, 1, timeout - elapsed);
217 if(ret < 0) {
218 perror("idnlookup: reading data");
219 exit(1);
220 }
221
222 if(pfd.revents & POLLIN) {
223 namelen = sizeof(name);
224 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
225 if(ret < 0) {
226 perror("idnlookup: receiving data");
227 exit(1);
228 }
229
230 if(name.ss_family != cai->ai_addr->sa_family)
231 continue;
232 if(name.ss_family == AF_INET) {
233 if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr)))
234 continue;
c9d75321
DC
235 if(ret < sizeof(iphdr) + sizeof(rep))
236 continue;
237 hdrlen = sizeof(iphdr);
238 memcpy(&iphdr, buf, sizeof(iphdr));
239 if(iphdr.protocol != IPPROTO_ICMP)
240 continue;
3d6143ec
DC
241 } else if(name.ss_family == AF_INET6) {
242 if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr)))
243 continue;
c9d75321
DC
244 if(ret < sizeof(rep))
245 continue;
246 hdrlen = 0;
3d6143ec
DC
247 } else {
248 continue;
249 }
250
c9d75321 251 memcpy(&rep, buf + hdrlen, sizeof(rep));
3d6143ec
DC
252 if(rep.type != ICMP_NAMEREP)
253 continue;
254 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
255 continue;
256
c9d75321 257 found = 1;
3d6143ec
DC
258 break;
259 }
260 }
261
3d6143ec 262 close(s);
c9d75321
DC
263
264 if(found) {
265 if(dispttl)
266 printf("%i\n", ntohl(rep.ttl));
267 printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst);
268 break;
269 }
270 }
271
272 if(timedout) {
273 fprintf(stderr, "idnlookup: timeout\n");
274 exit(1);
51a62941 275 }
3d6143ec 276
51a62941
DC
277 return(0);
278}
ecbdf1d6
DC
279
280/*
281 * Local Variables:
9155cd45 282 * compile-command: "make CFLAGS='-Wall -g'"
ecbdf1d6
DC
283 * End:
284 */