From 59fa8b4f48818f5a4f634633b48440ed3a466c8a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 11 Jun 2014 22:14:44 -0500 Subject: Cleanup pass on host.c --- toys/pending/host.c | 131 +++++++++++++++++++--------------------------------- 1 file changed, 47 insertions(+), 84 deletions(-) (limited to 'toys') diff --git a/toys/pending/host.c b/toys/pending/host.c index 178a4da7..50de1d34 100644 --- a/toys/pending/host.c +++ b/toys/pending/host.c @@ -67,12 +67,11 @@ static const char rct[16][32] = { void host_main(void) { - int verbose=(toys.optflags & (FLAG_a|FLAG_v)), type; - char *name, *nsname; - int i, j, ret, sec, count, rcode, qlen, alen, pllen = 0; + int verbose=(toys.optflags & (FLAG_a|FLAG_v)), type, + i, j, ret, sec, count, rcode, qlen, alen, pllen = 0; unsigned ttl, pri, v[5]; unsigned char qbuf[280], abuf[512], *p; - char rrname[256], plname[640], ptrbuf[64]; + char *name, *nsname, rrname[256], plname[640], ptrbuf[64]; struct addrinfo *ai, iplit_hints = { .ai_flags = AI_NUMERICHOST }; name = *toys.optargs; @@ -80,16 +79,14 @@ void host_main(void) if (!TT.type_str && (toys.optflags & FLAG_a)) TT.type_str = "255"; if (!getaddrinfo(name, 0, &iplit_hints, &ai)) { - switch (ai->ai_family) { - unsigned char *a; - static const char xdigits[] = "0123456789abcdef"; - case AF_INET: + unsigned char *a; + static const char xdigits[] = "0123456789abcdef"; + + if (ai->ai_family == AF_INET) { a = (void *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr; - snprintf(ptrbuf, sizeof ptrbuf, - "%d.%d.%d.%d.in-addr.arpa", + snprintf(ptrbuf, sizeof(ptrbuf), "%d.%d.%d.%d.in-addr.arpa", a[3], a[2], a[1], a[0]); - break; - case AF_INET6: + } else if (ai->ai_family == AF_INET6) { a = (void *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr; for (j=0, i=15; i>=0; i--) { ptrbuf[j++] = xdigits[a[i]&15]; @@ -98,17 +95,13 @@ void host_main(void) ptrbuf[j++] = '.'; } strcpy(ptrbuf+j, "ip6.arpa"); - break; } name = ptrbuf; if (!TT.type_str) TT.type_str="12"; - } else { - if (!TT.type_str) TT.type_str="1"; - } + } else if (!TT.type_str) TT.type_str="1"; - if (TT.type_str[0]-'0' < 10u) { - type = atoi(TT.type_str); - } else { + if (TT.type_str[0]-'0' < 10u) type = atoi(TT.type_str); + else { type = -1; for (i=0; i < sizeof rrt / sizeof *rrt; i++) { if (rrt[i].name && !strcasecmp(TT.type_str, rrt[i].name)) { @@ -117,76 +110,57 @@ void host_main(void) } } if (!strcasecmp(TT.type_str, "any")) type = 255; - if (type < 0) { - fprintf(stderr, "Invalid query type: %s\n", TT.type_str); - goto out_error; - } + if (type < 0) error_exit("Invalid query type: %s", TT.type_str); } qlen = res_mkquery(0, name, 1, type, 0, 0, 0, qbuf, sizeof qbuf); - if (qlen < 0) { - fprintf(stderr, "Invalid query parameters: %s", name); - goto out_error; - } + if (qlen < 0) error_exit("Invalid query parameters: %s", name); if (nsname) { struct addrinfo ns_hints = { .ai_socktype = SOCK_DGRAM }; - if ((ret = getaddrinfo(nsname, "53", &ns_hints, &ai)) < 0) { - fprintf(stderr, "Error looking up server name: %s\n", - gai_strerror(ret)); - goto out_error; - } + + if ((ret = getaddrinfo(nsname, "53", &ns_hints, &ai)) < 0) + error_exit("Error looking up server name: %s", gai_strerror(ret)); int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (s < 0 || connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { - fprintf(stderr, "Socket error: %s\n", strerror(errno)); - goto out_error; - } - setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, - &(struct timeval){ .tv_sec = 5 }, - sizeof (struct timeval)); + if (s < 0 || connect(s, ai->ai_addr, ai->ai_addrlen) < 0) + perror_exit("Socket error"); + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &(struct timeval){ .tv_sec = 5 }, + sizeof(struct timeval)); printf("Using domain server %s:\n", nsname); send(s, qbuf, qlen, 0); alen = recv(s, abuf, sizeof abuf, 0); - } else { - alen = res_send(qbuf, qlen, abuf, sizeof abuf); - } + } else alen = res_send(qbuf, qlen, abuf, sizeof abuf); - if (alen < 12) { - fprintf(stderr, "Host not found, try again.\n"); - goto out_error; - } + if (alen < 12) error_exit("Host not found."); rcode = abuf[3] & 15; if (verbose) { printf("rcode = %d (%s), ancount = %d\n", rcode, rct[rcode], 256*abuf[6] + abuf[7]); - if (!(abuf[2] & 4)) - printf("The following answer is not authoritative:\n"); + if (!(abuf[2] & 4)) printf("The following answer is not authoritative:\n"); } - if (rcode) { - fprintf(stderr, "Host not found.\n"); - if (!verbose) goto out_error; - } + if (rcode) error_exit("Host not found."); p = abuf + 12; for (sec=0; sec<4; sec++) { count = 256*abuf[4+2*sec] + abuf[5+2*sec]; - if (verbose && count>0 && sec>1) { + if (verbose && count>0 && sec>1) puts(sec==2 ? "For authoritative answers, see:" : "Additional information:"); - } + for (; count--; p += pllen) { - p += dn_expand(abuf, abuf+alen, p, rrname, sizeof rrname); - type = 256*p[0] + p[1]; + p += dn_expand(abuf, abuf+alen, p, rrname, sizeof(rrname)); + type = (p[0]<<8) + p[1]; p += 4; if (!sec) continue; - ttl = 16777216*p[0] + 65536*p[1] + 256*p[2] + p[3]; + ttl = (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]; p += 4; - pllen = 256*p[0] + p[1]; + pllen = (p[0]<<8) + p[1]; p += 2; - switch (type