FreeBSD/usr.bin/netstat/inet6.c
/*
 * Construct an Internet address representation.
 * If the numeric_addr has been supplied, give
 * numeric value, otherwise try for symbolic name.
 */
Construct an Internet address representation.
If the numeric_addr has been supplied, give
numeric value, otherwise try for symbolic name.
Construct an Internet address representation . If the numeric_addr has been supplied give numeric value otherwise try for symbolic name .
char *
inet6name(struct in6_addr *in6p)
{
	struct sockaddr_in6 sin6;
	char hbuf[NI_MAXHOST], *cp;
	static char line[50];
	static char domain[MAXHOSTNAMELEN];
	static int first = 1;
	int flags, error;
	if (IN6_IS_ADDR_UNSPECIFIED(in6p)) {
		strcpy(line, "*");
		return (line);
	}
	if (first && !numeric_addr) {
		first = 0;
		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
		    (cp = strchr(domain, '.')))
			(void) strcpy(domain, cp + 1);
		else
			domain[0] = 0;
	}
	memset(&sin6, 0, sizeof(sin6));
	memcpy(&sin6.sin6_addr, in6p, sizeof(*in6p));
	sin6.sin6_family = AF_INET6;
	in6_fillscopeid(&sin6);
	flags = (numeric_addr) ? NI_NUMERICHOST : 0;
	error = getnameinfo((struct sockaddr *)&sin6, sizeof(sin6), hbuf,
	    sizeof(hbuf), NULL, 0, flags);
	if (error == 0) {
		if ((flags & NI_NUMERICHOST) == 0 &&
		    (cp = strchr(hbuf, '.')) &&
		    !strcmp(cp + 1, domain))
			*cp = 0;
		strcpy(line, hbuf);
	} else {
		sprintf(line, "%s",
			inet_ntop(AF_INET6, (void *)&sin6.sin6_addr, ntop_buf,
				sizeof(ntop_buf)));
	}
	return (line);
}
char * inet6name ( struct in6_addr * in6p ) { struct sockaddr_in6 sin6 ; char hbuf [ NI_MAXHOST ] , * cp ; static char line [ 50 ] ; static char domain [ MAXHOSTNAMELEN ] ; static int first = 1 ; int flags , error ; if ( IN6_IS_ADDR_UNSPECIFIED ( in6p ) ) { strcpy ( line , " " ) ; return ( line ) ; } if ( first && ! numeric_addr ) { first = 0 ; if ( gethostname ( domain , MAXHOSTNAMELEN ) == 0 && ( cp = strchr ( domain , ' ' ) ) ) ( void ) strcpy ( domain , cp + 1 ) ; else domain [ 0 ] = 0 ; } memset ( & sin6 , 0 , sizeof ( sin6 ) ) ; memcpy ( & sin6 . sin6_addr , in6p , sizeof ( * in6p ) ) ; sin6 . sin6_family = AF_INET6 ; in6_fillscopeid ( & sin6 ) ; flags = ( numeric_addr ) ? NI_NUMERICHOST : 0 ; error = getnameinfo ( ( struct sockaddr * ) & sin6 , sizeof ( sin6 ) , hbuf , sizeof ( hbuf ) , NULL , 0 , flags ) ; if ( error == 0 ) { if ( ( flags & NI_NUMERICHOST ) == 0 && ( cp = strchr ( hbuf , ' ' ) ) && ! strcmp ( cp + 1 , domain ) ) * cp = 0 ; strcpy ( line , hbuf ) ; } else { sprintf ( line , " " , inet_ntop ( AF_INET6 , ( void * ) & sin6 . sin6_addr , ntop_buf , sizeof ( ntop_buf ) ) ) ; } return ( line ) ; }
Construct an Internet address representation.
Construct an Internet address representation .
/* XXX: in6p.s6_addr[2] can contain scopeid. */ /* XXX: this should not happen. */