| 1 | /* |
| 2 | * idnlookup - ICMP Domain Name lookup utility for Linux |
| 3 | * Copyright (C) 2005 Fredrik Tolf <fredrik@dolda2000.com> |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <errno.h> |
| 24 | #include <netdb.h> |
| 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> |
| 30 | #include <sys/poll.h> |
| 31 | #include <sys/time.h> |
| 32 | |
| 33 | struct icmphdr { |
| 34 | u_int8_t type; |
| 35 | u_int8_t code; |
| 36 | u_int16_t checksum; |
| 37 | }; |
| 38 | |
| 39 | struct reqhdr { |
| 40 | u_int8_t type; |
| 41 | u_int8_t code; |
| 42 | u_int16_t checksum; |
| 43 | u_int16_t id; |
| 44 | u_int16_t seq; |
| 45 | }; |
| 46 | |
| 47 | struct rephdr { |
| 48 | u_int8_t type; |
| 49 | u_int8_t code; |
| 50 | u_int16_t checksum; |
| 51 | u_int16_t id; |
| 52 | u_int16_t seq; |
| 53 | int32_t ttl; |
| 54 | |
| 55 | }; |
| 56 | |
| 57 | #define ICMP_NAMEREQ 37 |
| 58 | #define ICMP_NAMEREP 38 |
| 59 | |
| 60 | unsigned char buf[65536]; |
| 61 | |
| 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) |
| 66 | { |
| 67 | unsigned char *p; |
| 68 | |
| 69 | p = dnbuf; |
| 70 | while(p - dnbuf < size) { |
| 71 | while(*p != 0) { |
| 72 | if(*p & 0xc0) { |
| 73 | fprintf(stderr, "domain name decompression not implemented, aborting\n"); |
| 74 | exit(1); |
| 75 | } |
| 76 | fprintf(f, "%.*s", (int)*p, p + 1); |
| 77 | p += 1 + (int)*p; |
| 78 | if(*p != 0) |
| 79 | fprintf(f, "."); |
| 80 | } |
| 81 | p++; |
| 82 | fprintf(f, "\n"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void cksum(void *hdr, size_t len) |
| 87 | { |
| 88 | struct icmphdr *ih; |
| 89 | u_int8_t *cb; |
| 90 | int i; |
| 91 | int b1, b2; |
| 92 | |
| 93 | ih = (struct icmphdr *)hdr; |
| 94 | cb = (u_int8_t *)hdr; |
| 95 | ih->checksum = 0; |
| 96 | b1 = b2 = 0; |
| 97 | for(i = 0; i < (len & ~1); i += 2) { |
| 98 | b1 += cb[i]; |
| 99 | b2 += cb[i + 1]; |
| 100 | } |
| 101 | if(i & 1) |
| 102 | b1 += cb[len - 1]; |
| 103 | while(1) { |
| 104 | if(b1 >= 256) { |
| 105 | b2 += b1 >> 8; |
| 106 | b1 &= 0xff; |
| 107 | continue; |
| 108 | } |
| 109 | if(b2 >= 256) { |
| 110 | b1 += b2 >> 8; |
| 111 | b2 &= 0xff; |
| 112 | continue; |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | cb = (u_int8_t *)&ih->checksum; |
| 117 | cb[0] = ~(u_int8_t)b1; |
| 118 | cb[1] = ~(u_int8_t)b2; |
| 119 | } |
| 120 | |
| 121 | void usage(void) |
| 122 | { |
| 123 | fprintf(stderr, "usage: idnlookup [-h] [-t timeout] host\n"); |
| 124 | } |
| 125 | |
| 126 | int main(int argc, char **argv) |
| 127 | { |
| 128 | int ret; |
| 129 | int s, c; |
| 130 | int id; |
| 131 | int namelen; |
| 132 | struct reqhdr req; |
| 133 | struct rephdr rep; |
| 134 | struct iphdr iphdr; |
| 135 | struct addrinfo *ai, *cai, aihint; |
| 136 | struct pollfd pfd; |
| 137 | struct timeval tvb, tvc; |
| 138 | struct sockaddr_storage name; |
| 139 | int timeout, dispttl; |
| 140 | int elapsed; |
| 141 | |
| 142 | timeout = 3000; |
| 143 | dispttl = 0; |
| 144 | while((c = getopt(argc, argv, "hTt:")) != -1) { |
| 145 | switch(c) { |
| 146 | case 't': |
| 147 | timeout = atoi(optarg); |
| 148 | break; |
| 149 | case 'T': |
| 150 | dispttl = 1; |
| 151 | break; |
| 152 | case 'h': |
| 153 | case '?': |
| 154 | case ':': |
| 155 | default: |
| 156 | usage(); |
| 157 | exit((c == 'h')?0:1); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if(argc - optind < 1) { |
| 162 | usage(); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
| 166 | memset(&aihint, 0, sizeof(aihint)); |
| 167 | aihint.ai_socktype = SOCK_RAW; |
| 168 | aihint.ai_protocol = IPPROTO_ICMP; |
| 169 | ret = getaddrinfo(argv[optind], NULL, &aihint, &ai); |
| 170 | |
| 171 | for(cai = ai; cai != NULL; cai = cai->ai_next) { |
| 172 | if((s = socket(cai->ai_family, SOCK_RAW, IPPROTO_ICMP)) < 0) { |
| 173 | perror("could not create raw socket"); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | id = random() % 65536; |
| 178 | memset(&req, 0, sizeof(req)); |
| 179 | req.type = ICMP_NAMEREQ; |
| 180 | req.id = htons(id); |
| 181 | cksum(&req, sizeof(req)); |
| 182 | |
| 183 | ret = sendto(s, &req, sizeof(req), 0, cai->ai_addr, cai->ai_addrlen); |
| 184 | if(ret < 0) { |
| 185 | perror("sendto"); |
| 186 | exit(1); |
| 187 | } else if(ret != sizeof(req)) { |
| 188 | fprintf(stderr, "socket would not send entire packet\n"); |
| 189 | exit(1); |
| 190 | } |
| 191 | |
| 192 | gettimeofday(&tvb, NULL); |
| 193 | while(1) { |
| 194 | pfd.fd = s; |
| 195 | pfd.events = POLLIN; |
| 196 | gettimeofday(&tvc, NULL); |
| 197 | elapsed = ((tvc.tv_sec - tvb.tv_sec) * 1000) + ((tvc.tv_usec - tvb.tv_usec) / 1000); |
| 198 | if(elapsed >= timeout) { |
| 199 | fprintf(stderr, "idnlookup: timeout\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | ret = poll(&pfd, 1, timeout - elapsed); |
| 203 | if(ret < 0) { |
| 204 | perror("idnlookup: reading data"); |
| 205 | exit(1); |
| 206 | } |
| 207 | |
| 208 | if(pfd.revents & POLLIN) { |
| 209 | namelen = sizeof(name); |
| 210 | ret = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *)&name, &namelen); |
| 211 | if(ret < 0) { |
| 212 | perror("idnlookup: receiving data"); |
| 213 | exit(1); |
| 214 | } |
| 215 | |
| 216 | if(name.ss_family != cai->ai_addr->sa_family) |
| 217 | continue; |
| 218 | if(name.ss_family == AF_INET) { |
| 219 | if(memcmp(&(((struct sockaddr_in *)&name)->sin_addr), &(((struct sockaddr_in *)cai->ai_addr)->sin_addr), sizeof(struct in_addr))) |
| 220 | continue; |
| 221 | } else if(name.ss_family == AF_INET6) { |
| 222 | if(memcmp(&(((struct sockaddr_in6 *)&name)->sin6_addr), &(((struct sockaddr_in6 *)cai->ai_addr)->sin6_addr), sizeof(struct in6_addr))) |
| 223 | continue; |
| 224 | } else { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | if(ret < sizeof(iphdr) + sizeof(rep)) |
| 229 | continue; |
| 230 | memcpy(&iphdr, buf, sizeof(iphdr)); |
| 231 | memcpy(&rep, buf + sizeof(iphdr), sizeof(rep)); |
| 232 | if(iphdr.protocol != IPPROTO_ICMP) |
| 233 | continue; |
| 234 | if(rep.type != ICMP_NAMEREP) |
| 235 | continue; |
| 236 | if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0))) |
| 237 | continue; |
| 238 | |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if(dispttl) |
| 244 | printf("%i\n", ntohl(rep.ttl)); |
| 245 | printdn(stdout, buf + sizeof(iphdr) + sizeof(rep), ret - sizeof(iphdr) - sizeof(rep)); |
| 246 | |
| 247 | close(s); |
| 248 | } |
| 249 | |
| 250 | return(0); |
| 251 | } |