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>
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.
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.
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
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>
62 #define ICMP_NAMEREQ 37
63 #define ICMP_NAMEREP 38
65 unsigned char buf[65536];
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)
75 while(p - dnbuf < size) {
78 fprintf(stderr, "domain name decompression not implemented, aborting\n");
81 if((int)*p + (p - dnbuf) >= size) {
82 fprintf(stderr, "malformed domain name\n");
85 fprintf(f, "%.*s", (int)*p, p + 1);
97 void cksum(void *hdr, size_t len)
104 ih = (struct icmphdr *)hdr;
105 cb = (u_int8_t *)hdr;
108 for(i = 0; i < (len & ~1); i += 2) {
127 cb = (u_int8_t *)&ih->checksum;
128 cb[0] = ~(u_int8_t)b1;
129 cb[1] = ~(u_int8_t)b2;
134 fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
137 int main(int argc, char **argv)
147 struct addrinfo *ai, *cai, aihint;
149 struct timeval tvb, tvc;
150 struct sockaddr_storage name;
151 int timeout, dispttl, onlyfirst;
152 int elapsed, timedout, found;
157 while((c = getopt(argc, argv, "haTt:")) != -1) {
160 timeout = atoi(optarg);
173 exit((c == 'h')?0:1);
177 if(argc - optind < 1) {
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);
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");
193 id = random() % 65536;
194 memset(&req, 0, sizeof(req));
195 req.type = ICMP_NAMEREQ;
197 cksum(&req, sizeof(req));
199 ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen);
203 } else if(ret != sizeof(req)) {
204 fprintf(stderr, "socket would not send entire packet\n");
210 gettimeofday(&tvb, NULL);
214 gettimeofday(&tvc, NULL);
215 elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000);
216 if(elapsed >= timeout) {
220 ret = poll(&pfd, 1, timeout - elapsed);
222 perror("idnlookup: reading data");
226 if(pfd.revents & POLLIN) {
227 namelen = sizeof(name);
228 ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen);
230 perror("idnlookup: receiving data");
234 if(name.ss_family != cai->ai_addr->sa_family)
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)))
239 if(ret < sizeof(iphdr) + sizeof(rep))
241 hdrlen = sizeof(iphdr);
242 memcpy(&iphdr, buf, sizeof(iphdr));
243 if(iphdr.protocol != IPPROTO_ICMP)
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)))
248 if(ret < sizeof(rep))
255 memcpy(&rep, buf + hdrlen, sizeof(rep));
256 if(rep.type != ICMP_NAMEREP)
258 if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
270 printf("%i\n", ntohl(rep.ttl));
271 printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst);
277 fprintf(stderr, "idnlookup: timeout\n");
286 * compile-command: "make CFLAGS='-Wall -g'"