1230 Chapter 59
freeaddrinfo(result);
return (rp == NULL)? -1 : sfd;
}
int
inetListen(const char *service, int backlog, socklen_t *addrlen)
{
return inetPassiveSocket(service, SOCK_STREAM, addrlen, TRUE, backlog);
}
int
inetBind(const char *service, int type, socklen_t *addrlen)
{
return inetPassiveSocket(service, type, addrlen, FALSE, 0);
}
char *
inetAddressStr(const struct sockaddr *addr, socklen_t addrlen,
char *addrStr, int addrStrLen)
{
char host[NI_MAXHOST], service[NI_MAXSERV];
if (getnameinfo(addr, addrlen, host, NI_MAXHOST,
service, NI_MAXSERV, NI_NUMERICSERV) == 0)
snprintf(addrStr, addrStrLen, "(%s, %s)", host, service);
else
snprintf(addrStr, addrStrLen, "(?UNKNOWN?)");
addrStr[addrStrLen - 1] = '\0'; /* Ensure result is null-terminated */
return addrStr;
}
––––––––––––––––––––––––––––––––––––––––––––––––––––sockets/inet_sockets.c
59.13 Obsolete APIs for Host and Service Conversions
In the following sections, we describe the older, now obsolete functions for con-
verting host names and service names to and from binary and presentation for-
mats. Although new programs should perform these conversions using the modern
functions described earlier in this chapter, a knowledge of the obsolete functions is
useful because we may encounter them in older code.
59.13.1 The inet_aton() and inet_ntoa() Functions
The inet_aton() and inet_ntoa() functions convert IPv4 addresses between dotted-
decimal notation and binary form (in network byte order). These functions are
nowadays made obsolete by inet_pton() and inet_ntop().
The inet_aton() (“ASCII to network”) function converts the dotted-decimal string
pointed to by str into an IPv4 address in network byte order, which is returned in the
in_addr structure pointed to by addr.