From c9d7532139d71ed4955b5da519643a64bd590089 Mon Sep 17 00:00:00 2001 From: "fredrik@DOLDA2000.COM" Date: Fri, 29 Apr 2005 17:47:06 +0000 Subject: [PATCH] Output the proper number of names, the proper number of times. git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/icmp-dn@228 959494ce-11ee-0310-bf91-de5d638817bd --- idnlookup.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/idnlookup.c b/idnlookup.c index 2c49f87..39c95d7 100644 --- a/idnlookup.c +++ b/idnlookup.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -62,7 +63,7 @@ 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) +void printdn(FILE *f, unsigned char *dnbuf, size_t size, int onlyfirst) { unsigned char *p; @@ -80,6 +81,8 @@ void printdn(FILE *f, unsigned char *dnbuf, size_t size) } p++; fprintf(f, "\n"); + if(onlyfirst) + break; } } @@ -120,7 +123,7 @@ void cksum(void *hdr, size_t len) 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) @@ -132,20 +135,25 @@ 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; @@ -189,6 +197,8 @@ int main(int argc, char **argv) exit(1); } + timedout = 0; + found = 0; gettimeofday(&tvb, NULL); while(1) { pfd.fd = s; @@ -196,8 +206,8 @@ int main(int argc, char **argv) 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) { @@ -218,33 +228,46 @@ int main(int argc, char **argv) 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); -- 2.11.0