From: fredrik@DOLDA2000.COM Date: Thu, 28 Apr 2005 15:09:38 +0000 (+0000) Subject: First seemingly complete version (apart from DC decomp). X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=3d6143ece9dc7829a59674c2d1148830a480d7ce;p=icmp-dn.git First seemingly complete version (apart from DC decomp). git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/icmp-dn@211 959494ce-11ee-0310-bf91-de5d638817bd --- diff --git a/idnlookup.c b/idnlookup.c index e2632df..bd45b75 100644 --- a/idnlookup.c +++ b/idnlookup.c @@ -1,48 +1,230 @@ +/* + * idnlookup - ICMP Domain Name lookup utility for Linux + * Copyright (C) 2005 Fredrik Tolf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ #include #include #include #include #include +#include #include #include +#include #include #include +#include +#include struct icmphdr { u_int8_t type; u_int8_t code; u_int16_t checksum; +}; + +struct reqhdr { + u_int8_t type; + u_int8_t code; + u_int16_t checksum; + u_int16_t id; + u_int16_t seq; +}; + +struct rephdr { + u_int8_t type; + u_int8_t code; + u_int16_t checksum; u_int16_t id; u_int16_t seq; + int32_t ttl; + }; #define ICMP_NAMEREQ 37 #define ICMP_NAMEREP 38 +unsigned char buf[65536]; + +/* DN decompression not yet implemented, since I don't know where to + * begin counting the offset from -- the beginning of the ICMP + * payload, or from the beginning of the DN data buffer? */ +void printdn(FILE *f, unsigned char *dnbuf, size_t size) +{ + unsigned char *p; + + p = dnbuf; + while(p - dnbuf < size) { + while(*p != 0) { + if(*p & 0xc0) { + fprintf(stderr, "domain name decompression not implemented, aborting\n"); + exit(1); + } + printf("%.*s.", (int)*p, p + 1); + p += 1 + (int)*p; + } + p++; + printf("\n"); + } +} + +void cksum(void *hdr, size_t len) +{ + struct icmphdr *ih; + u_int8_t *cb; + int i; + u_int8_t b1, b2; + + ih = (struct icmphdr *)hdr; + cb = (u_int8_t *)hdr; + ih->checksum = 0; + b1 = b2 = 0; + for(i = 0; i < (len & ~1); i += 2) { + b1 += cb[i]; + b2 += cb[i + 1]; + } + if(i & 1) + b1 += cb[len - 1]; + cb = (u_int8_t *)&ih->checksum; + cb[0] = ~b1; + cb[1] = ~b2; +} + +void usage(void) +{ + fprintf(stderr, "usage: idnlookup [-h] [-t timeout] host\n"); +} + int main(int argc, char **argv) { int ret; - int s; - struct sockaddr_in host; - struct icmphdr data; + int s, c; + int id; + int namelen; + struct reqhdr req; + struct rephdr rep; + struct iphdr iphdr; + struct addrinfo *ai, *cai, aihint; + struct pollfd pfd; + struct timeval tvb, tvc; + struct sockaddr_storage name; + int timeout, elapsed; - if((s = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { - perror("could not create raw socket"); - exit(1); + timeout = 3000; + while((c = getopt(argc, argv, "ht:")) != -1) { + switch(c) { + case 't': + timeout = atoi(optarg); + break; + case 'h': + case '?': + case ':': + default: + usage(); + exit((c == 'h')?0:1); + } } - host.sin_family = AF_INET; - inet_aton("192.168.1.254", &host.sin_addr); + if(argc - optind < 1) { + usage(); + exit(1); + } - memset(&data, 0, sizeof(data)); - data.type = ICMP_NAMEREQ; + memset(&aihint, 0, sizeof(aihint)); + aihint.ai_family = PF_INET; /* Only IPv4 for now. */ + aihint.ai_socktype = SOCK_RAW; + aihint.ai_protocol = IPPROTO_ICMP; + ret = getaddrinfo(argv[optind], NULL, &aihint, &ai); - ret = sendto(s, &data, sizeof(data), 0, (struct sockaddr *)&host, sizeof(host)); - if(ret < 0) { - perror("sendto"); - } else { - printf("%i\n", ret); + for(cai = ai; cai != NULL; cai = cai->ai_next) { + if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) { + perror("could not create raw socket"); + exit(1); + } + + id = random() % 65536; + memset(&req, 0, sizeof(req)); + req.type = ICMP_NAMEREQ; + req.id = htons(id); + cksum(&req, sizeof(req)); + + ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen); + if(ret < 0) { + perror("sendto"); + exit(1); + } else if(ret != sizeof(req)) { + fprintf(stderr, "socket would not send entire packet\n"); + exit(1); + } + + gettimeofday(&tvb, NULL); + while(1) { + pfd.fd = s; + pfd.events = POLLIN; + gettimeofday(&tvc, NULL); + elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000); + if(elapsed > timeout) { + fprintf(stderr, "idnlookup: timeout\n"); + exit(1); + } + ret = poll(&pfd, 1, timeout - elapsed); + if(ret < 0) { + perror("idnlookup: reading data"); + exit(1); + } + + if(pfd.revents & POLLIN) { + namelen = sizeof(name); + ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen); + if(ret < 0) { + perror("idnlookup: receiving data"); + exit(1); + } + + if(name.ss_family != cai->ai_addr->sa_family) + continue; + if(name.ss_family == AF_INET) { + if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr))) + continue; + } else if(name.ss_family == AF_INET6) { + if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr))) + continue; + } else { + continue; + } + + if(ret < sizeof(iphdr) + sizeof(rep)) + continue; + memcpy(&iphdr, buf, sizeof(iphdr)); + memcpy(&rep, buf + sizeof(iphdr), sizeof(rep)); + if(iphdr.protocol != IPPROTO_ICMP) + continue; + if(rep.type != ICMP_NAMEREP) + continue; + if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0))) + continue; + + break; + } + } + + printdn(stdout, buf + sizeof(iphdr) + sizeof(rep), ret - sizeof(iphdr) - sizeof(rep)); + + close(s); } - close(s); + return(0); }