FreeBSD/usr.bin/systat/netstat.c
/*
 * Construct an Internet address representation.
 * If the nflag has been supplied, give
 * numeric value, otherwise try for symbolic name.
 */
Construct an Internet address representation.
If the nflag has been supplied, give
numeric value, otherwise try for symbolic name.
Construct an Internet address representation . If the nflag has been supplied give numeric value otherwise try for symbolic name .
static char *
inetname(struct sockaddr *sa)
{
	char *cp = 0;
	static char line[NI_MAXHOST];
	struct hostent *hp;
	struct netent *np;
	struct in_addr in;
#ifdef INET6
	if (sa->sa_family == AF_INET6) {
		if (memcmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
		    &in6addr_any, sizeof(in6addr_any)) == 0)
			strcpy(line, "*");
		else
			getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
			    nflag ? NI_NUMERICHOST : 0);
		return (line);
	}
#endif
	in = ((struct sockaddr_in *)sa)->sin_addr;
	if (!nflag && in.s_addr != INADDR_ANY) {
		int net = inet_netof(in);
		int lna = inet_lnaof(in);
		if (lna == INADDR_ANY) {
			np = getnetbyaddr(net, AF_INET);
			if (np)
				cp = np->n_name;
		}
		if (cp == NULL) {
			hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
			if (hp)
				cp = hp->h_name;
		}
	}
	if (in.s_addr == INADDR_ANY)
		strcpy(line, "*");
	else if (cp)
		snprintf(line, sizeof(line), "%s", cp);
	else {
		in.s_addr = ntohl(in.s_addr);
#define C(x)	((x) & 0xff)
		snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
			C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
	}
	return (line);
}
static char * inetname ( struct sockaddr * sa ) { char * cp = 0 ; static char line [ NI_MAXHOST ] ; struct hostent * hp ; struct netent * np ; struct in_addr in ; #ifdef INET6 if ( sa -> sa_family == AF_INET6 ) { if ( memcmp ( & ( ( struct sockaddr_in6 * ) sa ) -> sin6_addr , & in6addr_any , sizeof ( in6addr_any ) ) == 0 ) strcpy ( line , " " ) ; else getnameinfo ( sa , sa -> sa_len , line , sizeof ( line ) , NULL , 0 , nflag ? NI_NUMERICHOST : 0 ) ; return ( line ) ; } #endif in = ( ( struct sockaddr_in * ) sa ) -> sin_addr ; if ( ! nflag && in . s_addr != INADDR_ANY ) { int net = inet_netof ( in ) ; int lna = inet_lnaof ( in ) ; if ( lna == INADDR_ANY ) { np = getnetbyaddr ( net , AF_INET ) ; if ( np ) cp = np -> n_name ; } if ( cp == NULL ) { hp = gethostbyaddr ( ( char * ) & in , sizeof ( in ) , AF_INET ) ; if ( hp ) cp = hp -> h_name ; } } if ( in . s_addr == INADDR_ANY ) strcpy ( line , " " ) ; else if ( cp ) snprintf ( line , sizeof ( line ) , " " , cp ) ; else { in . s_addr = ntohl ( in . s_addr ) ; #define C ( x ) 	((x) & 0xff) snprintf ( line , sizeof ( line ) , " " , C ( in . s_addr >> 24 ) , C ( in . s_addr >> 16 ) , C ( in . s_addr >> 8 ) , C ( in . s_addr ) ) ; } return ( line ) ; }
Construct an Internet address representation.
Construct an Internet address representation .