#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton(const char *cp, struct in_addr *inp); in_addr_t inet_addr(const char *cp); in_addr_t inet_network(const char *cp); char *inet_ntoa(struct in_addr in); struct in_addr inet_makeaddr(int net, int host); in_addr_t inet_lnaof(struct in_addr in); in_addr_t inet_netof(struct in_addr in);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
inet_aton(), inet_ntoa(): _BSD_SOURCE || _SVID_SOURCE
In all of the above forms, components of the dotted address can be specified in decimal, octal (with a leading 0), or hexadecimal, with a leading 0X). Addresses in any of these forms are collectively termed IPV4 numbers-and-dots notation. The form that uses exactly four decimal numbers is referred to as IPv4 dotted-decimal notation (or sometimes: IPv4 dotted-quad notation).
The inet_addr() function converts the Internet host address cp from IPv4 numbers-and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned. Use of this function is problematic because -1 is a valid address (255.255.255.255). Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3) which provide a cleaner way to indicate error return.
The inet_network() function converts cp, a string in IPv4 numbers-and-dots notation, into a number in host byte order suitable for use as an Internet network address. On success, the converted address is returned. If the input is invalid, -1 is returned.
The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
The inet_lnaof() function returns the local network address part of the Internet address in. The returned value is in host byte order.
The inet_netof() function returns the network number part of the Internet address in. The returned value is in host byte order.
The inet_makeaddr() function is the converse of inet_netof() and inet_lnaof(). It returns an Internet host address in network byte order, created by combining the network number net with the local address host, both in host byte order.
The structure in_addr as used in inet_ntoa(), inet_makeaddr(), inet_lnaof() and inet_netof() is defined in <netinet/in.h> as:
typedef uint32_t in_addr_t; struct in_addr { in_addr_t s_addr; };
inet_lnaof(), inet_netof(), and inet_makeaddr() are legacy functions that assume they are dealing with classful network addresses. Classful networking divides IPv4 network addresses into host and network components at byte boundaries, as follows:
Classful network addresses are now obsolete, having been superseded by Classless Inter-Domain Routing (CIDR), which divides addresses into network and host components at arbitrary bit (rather than byte) boundaries.
$ ./a.out 226.000.000.037 # Last byte is in octal 226.0.0.31 $ ./a.out 0x7f.1 # First byte is in hex 127.0.0.1
#define _BSD_SOURCE #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct in_addr addr; if (argc != 2) { fprintf(stderr, "%s <dotted-address>\n", argv[0]); exit(EXIT_FAILURE); } if (inet_aton(argv[1], &addr) == 0) { perror("inet_aton"); exit(EXIT_FAILURE); } printf("%s\n", inet_ntoa(addr)); exit(EXIT_SUCCESS); }