#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
+#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/poll.h>
/* 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)
+void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst)
{
unsigned char *p;
}
p++;
fprintf(f, "\n");
+ if(onlyfirst)
+ break;
}
}
void usage(void)
{
- fprintf(stderr, "usage: idnlookup [-h] [-t timeout] host\n");
+ fprintf(stderr, "usage: idnlookup [-hTa] [-t timeout] host\n");
}
int main(int argc, char **argv)
struct reqhdr req;
struct rephdr rep;
struct iphdr iphdr;
+ size_t hdrlen;
struct addrinfo *ai, *cai, aihint;
struct pollfd pfd;
struct timeval tvb, tvc;
struct sockaddr_storage name;
- int timeout, dispttl;
- int elapsed;
+ int timeout, dispttl, onlyfirst;
+ int elapsed, timedout, found;
timeout = 3000;
dispttl = 0;
- while((c = getopt(argc, argv, "hTt:")) != -1) {
+ onlyfirst = 1;
+ while((c = getopt(argc, argv, "haTt:")) != -1) {
switch(c) {
case 't':
timeout = atoi(optarg);
break;
+ case 'a':
+ onlyfirst = 0;
+ break;
case 'T':
dispttl = 1;
break;
exit(1);
}
+ timedout = 0;
+ found = 0;
gettimeofday(&tvb, NULL);
while(1) {
pfd.fd = s;
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);
+ timedout = 1;
+ break;
}
ret = poll(&pfd, 1, timeout - elapsed);
if(ret < 0) {
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;
+ if(ret < sizeof(iphdr) + sizeof(rep))
+ continue;
+ hdrlen = sizeof(iphdr);
+ memcpy(&iphdr, buf, sizeof(iphdr));
+ if(iphdr.protocol != IPPROTO_ICMP)
+ 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;
+ if(ret < sizeof(rep))
+ continue;
+ hdrlen = 0;
} 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;
+ memcpy(&rep, buf + hdrlen, sizeof(rep));
if(rep.type != ICMP_NAMEREP)
continue;
if((ntohs(rep.id) != id) || (ntohs(rep.seq != 0)))
continue;
+ found = 1;
break;
}
}
- if(dispttl)
- printf("%i\n", ntohl(rep.ttl));
- printdn(stdout, buf + sizeof(iphdr) + sizeof(rep), ret - sizeof(iphdr) - sizeof(rep));
-
close(s);
+
+ if(found) {
+ if(dispttl)
+ printf("%i\n", ntohl(rep.ttl));
+ printdn(stdout, buf + hdrlen + sizeof(rep), ret - hdrlen - sizeof(rep), onlyfirst);
+ break;
+ }
+ }
+
+ if(timedout) {
+ fprintf(stderr, "idnlookup: timeout\n");
+ exit(1);
}
return(0);