Commit bcc7d40b authored by Mike Frysinger's avatar Mike Frysinger

hostname: unify IPv4/IPv6 codepaths

Use inet_ntop everywhere so that we only have one code path between IPv4
and IPv6.  This also fixes some alignment warnings (due to gethostname
returning char* pointers which we then cast up to in_addr structs).

URL: https://bugs.gentoo.org/558436
parent 742e6280
...@@ -131,11 +131,18 @@ static void setdname(char *dname) ...@@ -131,11 +131,18 @@ static void setdname(char *dname)
static void showhname(char *hname, int c) static void showhname(char *hname, int c)
{ {
struct hostent *hp; struct hostent *hp;
register char *p, **alias;
#if HAVE_AFINET6 #if HAVE_AFINET6
struct in6_addr **ip6; char addr[INET6_ADDRSTRLEN + 1];
#else
char addr[INET_ADDRSTRLEN + 1];
#endif #endif
register char *p, **alias; socklen_t len;
struct in_addr **ip; char **addrp;
/* We use -1 so we can guarantee the buffer is NUL terminated. */
len = sizeof(addr) - 1;
addr[len] = '\0';
if (opt_v) if (opt_v)
fprintf(stderr, _("Resolving `%s' ...\n"), hname); fprintf(stderr, _("Resolving `%s' ...\n"), hname);
...@@ -156,27 +163,15 @@ static void showhname(char *hname, int c) ...@@ -156,27 +163,15 @@ static void showhname(char *hname, int c)
while (alias[0]) while (alias[0])
fprintf(stderr, _("Result: h_aliases=`%s'\n"), fprintf(stderr, _("Result: h_aliases=`%s'\n"),
*alias++); *alias++);
#if HAVE_AFINET6
if (hp->h_addrtype == PF_INET6) { for (addrp = hp->h_addr_list; *addrp; ++addrp) {
char addr[INET6_ADDRSTRLEN + 1]; if (inet_ntop(hp->h_addrtype, *addrp, addr, len))
addr[INET6_ADDRSTRLEN] = '\0'; fprintf(stderr, _("Result: h_addr_list=`%s'\n"), addr);
ip6 = (struct in6_addr **) hp->h_addr_list; else if (errno == EAFNOSUPPORT)
while (ip6[0]) { fprintf(stderr, _("%s: protocol family not supported\n"),
if (inet_ntop(PF_INET6, *ip6++, addr, INET6_ADDRSTRLEN)) program_name);
fprintf(stderr, _("Result: h_addr_list=`%s'\n"), addr); else if (errno == ENOSPC)
else if (errno == EAFNOSUPPORT) fprintf(stderr, _("%s: name too long\n"), program_name);
fprintf(stderr, _("%s: protocol family not supported\n"),
program_name);
else if (errno == ENOSPC)
fprintf(stderr, _("%s: name too long\n"), program_name);
}
} else
#endif
{
ip = (struct in_addr **) hp->h_addr_list;
while (ip[0])
fprintf(stderr, _("Result: h_addr_list=`%s'\n"),
inet_ntoa(**ip++));
} }
} }
if (!(p = strchr(hp->h_name, '.')) && (c == 'd')) if (!(p = strchr(hp->h_name, '.')) && (c == 'd'))
...@@ -189,28 +184,14 @@ static void showhname(char *hname, int c) ...@@ -189,28 +184,14 @@ static void showhname(char *hname, int c)
printf("\n"); printf("\n");
break; break;
case 'i': case 'i':
#if HAVE_AFINET6 for (addrp = hp->h_addr_list; *addrp; ++addrp) {
if (hp->h_addrtype == PF_INET6) { if (inet_ntop(hp->h_addrtype, *addrp, addr, len))
char addr[INET6_ADDRSTRLEN + 1]; printf("%s ", addr);
addr[INET6_ADDRSTRLEN] = '\0'; else if (errno == EAFNOSUPPORT)
while (hp->h_addr_list[0]) { fprintf(stderr, _("%s: protocol family not supported\n"),
if (inet_ntop(PF_INET6, (struct in6_addr *)*hp->h_addr_list++, program_name);
addr, INET6_ADDRSTRLEN)) { else if (errno == ENOSPC)
printf("%s ", addr); fprintf(stderr, _("%s: name too long\n"), program_name);
} else if (errno == EAFNOSUPPORT) {
fprintf(stderr, _("\n%s: protocol family not supported\n"),
program_name);
exit(1);
} else if (errno == ENOSPC) {
fprintf(stderr, _("\n%s: name too long\n"), program_name);
exit(1);
}
}
} else
#endif
{
while (hp->h_addr_list[0])
printf("%s ", inet_ntoa(*(struct in_addr *)*hp->h_addr_list++));
} }
printf("\n"); printf("\n");
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment