1INET(3)                    Linux Programmer's Manual                   INET(3)
2
3
4

NAME

6       inet_aton,    inet_addr,    inet_network,   inet_ntoa,   inet_makeaddr,
7       inet_lnaof, inet_netof - Internet address manipulation routines
8

SYNOPSIS

10       #include <sys/socket.h>
11       #include <netinet/in.h>
12       #include <arpa/inet.h>
13
14       int inet_aton(const char *cp, struct in_addr *inp);
15
16       in_addr_t inet_addr(const char *cp);
17
18       in_addr_t inet_network(const char *cp);
19
20       char *inet_ntoa(struct in_addr in);
21
22       struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host);
23
24       in_addr_t inet_lnaof(struct in_addr in);
25
26       in_addr_t inet_netof(struct in_addr in);
27
28   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
29
30       inet_aton(), inet_ntoa():
31           Since glibc 2.19:
32               _DEFAULT_SOURCE
33           In glibc up to and including 2.19:
34               _BSD_SOURCE || _BSD_SOURCE
35

DESCRIPTION

37       inet_aton() converts the Internet host address cp from  the  IPv4  num‐
38       bers-and-dots  notation  into  binary  form (in network byte order) and
39       stores it in the structure that inp  points  to.   inet_aton()  returns
40       nonzero  if the address is valid, zero if not.  The address supplied in
41       cp can have one of the following forms:
42
43       a.b.c.d   Each of the four  numeric  parts  specifies  a  byte  of  the
44                 address;  the  bytes  are  assigned in left-to-right order to
45                 produce the binary address.
46
47       a.b.c     Parts a and b specify the  first  two  bytes  of  the  binary
48                 address.   Part  c  is  interpreted  as  a  16-bit value that
49                 defines the rightmost two bytes of the binary address.   This
50                 notation  is  suitable for specifying (outmoded) Class B net‐
51                 work addresses.
52
53       a.b       Part a specifies the first byte of the binary address.   Part
54                 b is interpreted as a 24-bit value that defines the rightmost
55                 three bytes of the binary address.  This notation is suitable
56                 for specifying (outmoded) Class A network addresses.
57
58       a         The  value  a is interpreted as a 32-bit value that is stored
59                 directly into the binary address without any byte  rearrange‐
60                 ment.
61
62       In  all  of  the  above  forms, components of the dotted address can be
63       specified in decimal, octal (with a leading 0), or hexadecimal, with  a
64       leading  0X).   Addresses in any of these forms are collectively termed
65       IPV4 numbers-and-dots notation.  The form that uses exactly four  deci‐
66       mal  numbers  is  referred to as IPv4 dotted-decimal notation (or some‐
67       times: IPv4 dotted-quad notation).
68
69       inet_aton() returns 1 if the supplied string  was  successfully  inter‐
70       preted, or 0 if the string is invalid (errno is not set on error).
71
72       The  inet_addr()  function  converts  the Internet host address cp from
73       IPv4 numbers-and-dots notation into binary data in network byte  order.
74       If  the input is invalid, INADDR_NONE (usually -1) is returned.  Use of
75       this  function  is  problematic  because  -1   is   a   valid   address
76       (255.255.255.255).    Avoid   its   use   in   favor   of  inet_aton(),
77       inet_pton(3), or getaddrinfo(3), which provide a cleaner way  to  indi‐
78       cate error return.
79
80       The  inet_network() function converts cp, a string in IPv4 numbers-and-
81       dots notation, into a number in host byte order suitable for use as  an
82       Internet  network  address.   On  success,  the  converted  address  is
83       returned.  If the input is invalid, -1 is returned.
84
85       The inet_ntoa() function converts the Internet host address  in,  given
86       in  network  byte  order,  to a string in IPv4 dotted-decimal notation.
87       The string is returned in a statically allocated buffer,  which  subse‐
88       quent calls will overwrite.
89
90       The inet_lnaof() function returns the local network address part of the
91       Internet address in.  The returned value is in host byte order.
92
93       The inet_netof() function returns the network number part of the Inter‐
94       net address in.  The returned value is in host byte order.
95
96       The  inet_makeaddr()  function  is  the  converse  of  inet_netof() and
97       inet_lnaof().  It returns an Internet  host  address  in  network  byte
98       order,  created  by  combining  the  network  number net with the local
99       address host, both in host byte order.
100
101       The  structure  in_addr  as  used  in   inet_ntoa(),   inet_makeaddr(),
102       inet_lnaof() and inet_netof() is defined in <netinet/in.h> as:
103
104           typedef uint32_t in_addr_t;
105
106           struct in_addr {
107               in_addr_t s_addr;
108           };
109

ATTRIBUTES

111       For   an   explanation   of   the  terms  used  in  this  section,  see
112       attributes(7).
113
114       ┌───────────────────────────────┬───────────────┬────────────────┐
115Interface                      Attribute     Value          
116       ├───────────────────────────────┼───────────────┼────────────────┤
117inet_aton(), inet_addr(),      │ Thread safety │ MT-Safe locale │
118inet_network(), inet_ntoa()    │               │                │
119       ├───────────────────────────────┼───────────────┼────────────────┤
120inet_makeaddr(), inet_lnaof(), │ Thread safety │ MT-Safe        │
121inet_netof()                   │               │                │
122       └───────────────────────────────┴───────────────┴────────────────┘

CONFORMING TO

124       inet_addr(), inet_ntoa(): POSIX.1-2001, POSIX.1-2008, 4.3BSD.
125
126       inet_aton() is not specified in POSIX.1, but is available on most  sys‐
127       tems.
128

NOTES

130       On  x86  architectures,  the  host byte order is Least Significant Byte
131       first (little endian), whereas the network byte order, as used  on  the
132       Internet, is Most Significant Byte first (big endian).
133
134       inet_lnaof(),  inet_netof(),  and  inet_makeaddr() are legacy functions
135       that assume they are dealing with classful network addresses.  Classful
136       networking  divides IPv4 network addresses into host and network compo‐
137       nents at byte boundaries, as follows:
138
139       Class A   This address type is indicated by the value  0  in  the  most
140                 significant  bit  of the (network byte ordered) address.  The
141                 network address is contained in the  most  significant  byte,
142                 and the host address occupies the remaining three bytes.
143
144       Class B   This  address type is indicated by the binary value 10 in the
145                 most significant  two  bits  of  the  address.   The  network
146                 address  is  contained in the two most significant bytes, and
147                 the host address occupies the remaining two bytes.
148
149       Class C   This address type is indicated by the binary value 110 in the
150                 most  significant  three  bits  of  the address.  The network
151                 address is contained in the three most significant bytes, and
152                 the host address occupies the remaining byte.
153
154       Classful  network addresses are now obsolete, having been superseded by
155       Classless Inter-Domain Routing (CIDR),  which  divides  addresses  into
156       network  and host components at arbitrary bit (rather than byte) bound‐
157       aries.
158

EXAMPLE

160       An example of the use of inet_aton() and inet_ntoa()  is  shown  below.
161       Here are some example runs:
162
163           $ ./a.out 226.000.000.037      # Last byte is in octal
164           226.0.0.31
165           $ ./a.out 0x7f.1               # First byte is in hex
166           127.0.0.1
167
168   Program source
169
170       #define _BSD_SOURCE
171       #include <arpa/inet.h>
172       #include <stdio.h>
173       #include <stdlib.h>
174
175       int
176       main(int argc, char *argv[])
177       {
178           struct in_addr addr;
179
180           if (argc != 2) {
181               fprintf(stderr, "%s <dotted-address>\n", argv[0]);
182               exit(EXIT_FAILURE);
183           }
184
185           if (inet_aton(argv[1], &addr) == 0) {
186               fprintf(stderr, "Invalid address\n");
187               exit(EXIT_FAILURE);
188           }
189
190           printf("%s\n", inet_ntoa(addr));
191           exit(EXIT_SUCCESS);
192       }
193

SEE ALSO

195       byteorder(3),  getaddrinfo(3), gethostbyname(3), getnameinfo(3), getne‐
196       tent(3), inet_net_pton(3), inet_ntop(3), inet_pton(3),  hosts(5),  net‐
197       works(5)
198

COLOPHON

200       This  page  is  part of release 4.16 of the Linux man-pages project.  A
201       description of the project, information about reporting bugs,  and  the
202       latest     version     of     this    page,    can    be    found    at
203       https://www.kernel.org/doc/man-pages/.
204
205
206
207GNU                               2017-09-15                           INET(3)
Impressum