2 * idnlookup - ICMP Domain Name lookup utility for Linux
3 * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
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>
57 #define ICMP_NAMEREQ 37
58 #define ICMP_NAMEREP 38
60 unsigned char buf[65536];
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)
70 while(p - dnbuf < size) {
73 fprintf(stderr, "domain name decompression not implemented, aborting\n");
76 printf("%.*s.", (int)*p, p + 1);
84 void cksum(void *hdr, size_t len)
91 ih = (struct icmphdr *)hdr;
95 for(i = 0; i < (len & ~1); i += 2) {
114 cb = (u_int8_t *)&ih->checksum;
115 cb[0] = ~(u_int8_t)b1;
116 cb[1] = ~(u_int8_t)b2;
121 fprintf(stderr, "usage: idnlookup [-h] [-t timeout] host\n");
124 int main(int argc, char **argv)
133 struct addrinfo *ai, *cai, aihint;
135 struct timeval tvb, tvc;
136 struct sockaddr_storage name;
137 int timeout, elapsed;
140 while((c = getopt(argc, argv, "ht:")) != -1) {
143 timeout = atoi(optarg);
150 exit((c == 'h')?0:1);
154 if(argc - optind < 1) {
159 memset(&aihint, 0, sizeof(aihint));
160 aihint.ai_family = PF_INET; /* Only IPv4 for now. */
161 aihint.ai_socktype = SOCK_RAW;
162 aihint.ai_protocol = IPPROTO_ICMP;
163 ret = getaddrinfo(argv[optind], NULL, &aihint, &ai);
165 for(cai = ai; cai != NULL; cai = cai->ai_next) {
166 if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) {
167 perror("could not create raw socket");
171 id = random() % 65536;
172 memset(&req, 0, sizeof(req));
173 req.type = ICMP_NAMEREQ;
175 cksum(&req, sizeof(req));
177 ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen);
181 } else if(ret != sizeof(req)) {
182 fprintf(stderr, "socket would not send entire packet\n");
186 gettimeofday(&tvb, NULL);
190 gettimeofday(&tvc, NULL);
191 elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000);
192 if(elapsed > timeout) {
193 fprintf(stderr, "idnlookup: timeout\n");
196 ret = poll(&pfd, 1, timeout - elapsed);
198 perror("idnlookup: reading data");
202 if(pfd.revents & POLLIN) {
203 namelen = sizeof(name);
204 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
206 perror("idnlookup: receiving data");
210 if(name.ss_family != cai->ai_addr->sa_family)
212 if(name.ss_family == AF_INET) {
213 if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr)))
215 } else if(name.ss_family == AF_INET6) {
216 if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr)))
222 if(ret < sizeof(iphdr) + sizeof(rep))
224 memcpy(&iphdr, buf, sizeof(iphdr));
225 memcpy(&rep, buf + sizeof(iphdr), sizeof(rep));
226 if(iphdr.protocol != IPPROTO_ICMP)
228 if(rep.type != ICMP_NAMEREP)
230 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
237 printdn(stdout, buf + sizeof(iphdr) + sizeof(rep), ret - sizeof(iphdr) - sizeof(rep));