The Linux Programming Interface

(nextflipdebug5) #1
Sockets: Internet Domains 1231

The inet_aton() function returns 1 if the conversion was successful, or 0 if str was
invalid.
The numeric components of the string given to inet_aton() need not be decimal.
They can be octal (specified by a leading 0) or hexadecimal (specified by a leading
0x or 0X). Furthermore, inet_aton() supports shorthand forms that allow an address
to be specified using fewer than four numeric components. (See the inet(3) manual
page for details.) The term numbers-and-dots notation is used for the more general
address strings that employ these features.
SUSv3 doesn’t specify inet_aton(). Nevertheless, this function is available on
most implementations. On Linux, we must define one of the feature test macros
_BSD_SOURCE, _SVID_SOURCE, or _GNU_SOURCE in order to obtain the declaration of inet_aton()
from <arpa/inet.h>.
The inet_ntoa() (“network to ASCII”) function performs the converse of
inet_aton().

Given an in_addr structure (a 32-bit IPv4 address in network byte order), inet_ntoa()
returns a pointer to a (statically allocated) string containing the address in dotted-
decimal notation.
Because the string returned by inet_ntoa() is statically allocated, it is overwritten
by successive calls.

59.13.2 The gethostbyname() and gethostbyaddr() Functions


The gethostbyname() and gethostbyaddr() functions allow conversion between hostnames
and IP addresses. These functions are nowadays made obsolete by getaddrinfo() and
getnameinfo().

#include <arpa/inet.h>

int inet_aton(const char *str, struct in_addr *addr);
Returns 1 (true) if str is a valid dotted-decimal address, or 0 (false) on error

#include <arpa/inet.h>

char *inet_ntoa(struct in_addr addr);
Returns pointer to (statically allocated)
dotted-decimal string version of addr

#include <netdb.h>

extern int h_errno;

struct hostent *gethostbyname(const char *name);
struct hostent *gethostbyaddr(const char *addr, socklen_t len, int type);
Both return pointer to (statically allocated) hostent structure
on success, or NULL on error
Free download pdf