Index: contrib/traceroute/as.c =================================================================== --- contrib/traceroute/as.c (revision 366529) +++ contrib/traceroute/as.c (working copy) @@ -119,7 +119,7 @@ } unsigned int -as_lookup(void *_asn, char *addr, sa_family_t family) +as_lookup(void *_asn, char *addr, sa_family_t family, int *status) { struct aslookup *asn = _asn; char buf[1024]; @@ -129,8 +129,16 @@ as = 0; rc = dlen = 0; plen = (family == AF_INET6) ? 128 : 32; - (void)fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen); - (void)fflush(asn->as_f); + *status = fprintf(asn->as_f, "!r%s/%d,l\n", addr, plen); + if (*status < 0) { + *status = errno; + return 0; + } + *status = fflush(asn->as_f); + if (*status == EOF) { + *status = errno; + return 0; + } #ifdef AS_DEBUG_FILE if (asn->as_debug) { @@ -139,7 +147,14 @@ } #endif /* AS_DEBUG_FILE */ - while (fgets(buf, sizeof(buf), asn->as_f) != NULL) { + while (1) { + if (fgets(buf, sizeof(buf), asn->as_f) == NULL) { + if(feof(asn->as_f) || ferror(asn->as_f)) { + *status = EIO; + return 0; + } + break; + } buf[sizeof(buf) - 1] = '\0'; #ifdef AS_DEBUG_FILE Index: contrib/traceroute/traceroute.c =================================================================== --- contrib/traceroute/traceroute.c (revision 366529) +++ contrib/traceroute/traceroute.c (working copy) @@ -356,7 +356,7 @@ int nflag; /* print addresses numerically */ int as_path; /* print as numbers for each hop */ char *as_server = NULL; -void *asn; +void *as_lookup_conn = NULL; #ifdef CANT_HACK_IPCKSUM int doipcksum = 0; /* don't calculate ip checksums by default */ #else @@ -992,8 +992,8 @@ } if (as_path) { - asn = as_setup(as_server); - if (asn == NULL) { + as_lookup_conn = as_setup(as_server); + if (as_lookup_conn == NULL) { Fprintf(stderr, "%s: as_setup failed, AS# lookups" " disabled\n", prog); (void)fflush(stderr); @@ -1251,8 +1251,8 @@ (unreachable > 0 && unreachable >= nprobes - 1)) break; } - if (as_path) - as_shutdown(asn); + if (as_lookup_conn) + as_shutdown(as_lookup_conn); exit(0); } @@ -1705,6 +1705,7 @@ { register struct ip *ip; register int hlen; + int asn, status; char addr[INET_ADDRSTRLEN]; ip = (struct ip *) buf; @@ -1713,8 +1714,25 @@ strlcpy(addr, inet_ntoa(from->sin_addr), sizeof(addr)); - if (as_path) - Printf(" [AS%u]", as_lookup(asn, addr, AF_INET)); + while (as_path) { + asn = as_lookup(as_lookup_conn, addr, AF_INET, &status); + if (status) { + as_shutdown(as_lookup_conn); + as_lookup_conn = as_setup(as_server); + if (as_lookup_conn == NULL) { + Fprintf(stderr, "%s: as_setup failed, AS# lookups" + " disabled\n", prog); + (void)fflush(stderr); + as_path = 0; + break; + } + else + continue; + } + if (asn > 0) + Printf(" [AS%u]", asn); + break; + } if (nflag) Printf(" %s", addr); Index: usr.sbin/traceroute6/traceroute6.c =================================================================== --- usr.sbin/traceroute6/traceroute6.c (revision 366529) +++ usr.sbin/traceroute6/traceroute6.c (working copy) @@ -351,7 +351,7 @@ int lflag; /* print both numerical address & hostname */ int as_path; /* print as numbers for each hop */ char *as_server = NULL; -void *asn; +void *as_lookup_conn = NULL; int main(int argc, char *argv[]) @@ -871,8 +871,8 @@ } if (as_path) { - asn = as_setup(as_server); - if (asn == NULL) { + as_lookup_conn = as_setup(as_server); + if (as_lookup_conn == NULL) { fprintf(stderr, "traceroute6: as_setup failed, AS# lookups" " disabled\n"); @@ -880,6 +880,8 @@ as_path = 0; } } + if (as_path) + signal(SIGPIPE, SIG_IGN); /* * Message to users @@ -977,8 +979,8 @@ exit(0); } } - if (as_path) - as_shutdown(asn); + if (as_lookup_conn) + as_shutdown(as_lookup_conn); exit(0); } @@ -1487,13 +1489,32 @@ print(struct msghdr *mhdr, int cc) { struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name; + int asn, status; char hbuf[NI_MAXHOST]; if (getnameinfo((struct sockaddr *)from, from->sin6_len, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) strlcpy(hbuf, "invalid", sizeof(hbuf)); - if (as_path) - printf(" [AS%u]", as_lookup(asn, hbuf, AF_INET6)); + + while (as_path) { + asn = as_lookup(as_lookup_conn, hbuf, AF_INET6, &status); + if (status) { + as_shutdown(as_lookup_conn); + as_lookup_conn = as_setup(as_server); + if (as_lookup_conn == NULL) { + fprintf(stderr, "traceroute6: as_setup failed, AS# lookups" + " disabled\n"); + (void)fflush(stderr); + as_path = 0; + break; + } + else + continue; + } + if (asn > 0) + printf(" [AS%u]", asn); + break; + } if (nflag) printf(" %s", hbuf); else if (lflag)