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