1INET(3) Linux Programmer's Manual INET(3)
2
3
4
6 inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr,
7 inet_lnaof, inet_netof - Internet address manipulation routines
8
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 in_addr_t inet_network(const char *cp);
18
19 char *inet_ntoa(struct in_addr in);
20
21 struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host);
22
23 in_addr_t inet_lnaof(struct in_addr in);
24 in_addr_t inet_netof(struct in_addr in);
25
26 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
27
28 inet_aton(), inet_ntoa():
29 Since glibc 2.19:
30 _DEFAULT_SOURCE
31 In glibc up to and including 2.19:
32 _BSD_SOURCE || _BSD_SOURCE
33
35 inet_aton() converts the Internet host address cp from the IPv4 num‐
36 bers-and-dots notation into binary form (in network byte order) and
37 stores it in the structure that inp points to. inet_aton() returns
38 nonzero if the address is valid, zero if not. The address supplied in
39 cp can have one of the following forms:
40
41 a.b.c.d Each of the four numeric parts specifies a byte of the ad‐
42 dress; the bytes are assigned in left-to-right order to pro‐
43 duce the binary address.
44
45 a.b.c Parts a and b specify the first two bytes of the binary ad‐
46 dress. Part c is interpreted as a 16-bit value that defines
47 the rightmost two bytes of the binary address. This notation
48 is suitable for specifying (outmoded) Class B network ad‐
49 dresses.
50
51 a.b Part a specifies the first byte of the binary address. Part
52 b is interpreted as a 24-bit value that defines the rightmost
53 three bytes of the binary address. This notation is suitable
54 for specifying (outmoded) Class A network addresses.
55
56 a The value a is interpreted as a 32-bit value that is stored
57 directly into the binary address without any byte rearrange‐
58 ment.
59
60 In all of the above forms, components of the dotted address can be
61 specified in decimal, octal (with a leading 0), or hexadecimal, with a
62 leading 0X). Addresses in any of these forms are collectively termed
63 IPV4 numbers-and-dots notation. The form that uses exactly four deci‐
64 mal numbers is referred to as IPv4 dotted-decimal notation (or some‐
65 times: IPv4 dotted-quad notation).
66
67 inet_aton() returns 1 if the supplied string was successfully inter‐
68 preted, or 0 if the string is invalid (errno is not set on error).
69
70 The inet_addr() function converts the Internet host address cp from
71 IPv4 numbers-and-dots notation into binary data in network byte order.
72 If the input is invalid, INADDR_NONE (usually -1) is returned. Use of
73 this function is problematic because -1 is a valid address
74 (255.255.255.255). Avoid its use in favor of inet_aton(),
75 inet_pton(3), or getaddrinfo(3), which provide a cleaner way to indi‐
76 cate error return.
77
78 The inet_network() function converts cp, a string in IPv4 numbers-and-
79 dots notation, into a number in host byte order suitable for use as an
80 Internet network address. On success, the converted address is re‐
81 turned. If the input is invalid, -1 is returned.
82
83 The inet_ntoa() function converts the Internet host address in, given
84 in network byte order, to a string in IPv4 dotted-decimal notation.
85 The string is returned in a statically allocated buffer, which subse‐
86 quent calls will overwrite.
87
88 The inet_lnaof() function returns the local network address part of the
89 Internet address in. The returned value is in host byte order.
90
91 The inet_netof() function returns the network number part of the Inter‐
92 net address in. The returned value is in host byte order.
93
94 The inet_makeaddr() function is the converse of inet_netof() and
95 inet_lnaof(). It returns an Internet host address in network byte or‐
96 der, created by combining the network number net with the local address
97 host, both in host byte order.
98
99 The structure in_addr as used in inet_ntoa(), inet_makeaddr(),
100 inet_lnaof(), and inet_netof() is defined in <netinet/in.h> as:
101
102 typedef uint32_t in_addr_t;
103
104 struct in_addr {
105 in_addr_t s_addr;
106 };
107
109 For an explanation of the terms used in this section, see at‐
110 tributes(7).
111
112 ┌─────────────────────────────────────┬───────────────┬────────────────┐
113 │Interface │ Attribute │ Value │
114 ├─────────────────────────────────────┼───────────────┼────────────────┤
115 │inet_aton(), inet_addr(), │ Thread safety │ MT-Safe locale │
116 │inet_network(), inet_ntoa() │ │ │
117 ├─────────────────────────────────────┼───────────────┼────────────────┤
118 │inet_makeaddr(), inet_lnaof(), │ Thread safety │ MT-Safe │
119 │inet_netof() │ │ │
120 └─────────────────────────────────────┴───────────────┴────────────────┘
121
123 inet_addr(), inet_ntoa(): POSIX.1-2001, POSIX.1-2008, 4.3BSD.
124
125 inet_aton() is not specified in POSIX.1, but is available on most sys‐
126 tems.
127
129 On x86 architectures, the host byte order is Least Significant Byte
130 first (little endian), whereas the network byte order, as used on the
131 Internet, is Most Significant Byte first (big endian).
132
133 inet_lnaof(), inet_netof(), and inet_makeaddr() are legacy functions
134 that assume they are dealing with classful network addresses. Classful
135 networking divides IPv4 network addresses into host and network compo‐
136 nents at byte boundaries, as follows:
137
138 Class A This address type is indicated by the value 0 in the most
139 significant bit of the (network byte ordered) address. The
140 network address is contained in the most significant byte,
141 and the host address occupies the remaining three bytes.
142
143 Class B This address type is indicated by the binary value 10 in the
144 most significant two bits of the address. The network ad‐
145 dress is contained in the two most significant bytes, and the
146 host address occupies the remaining two bytes.
147
148 Class C This address type is indicated by the binary value 110 in the
149 most significant three bits of the address. The network ad‐
150 dress is contained in the three most significant bytes, and
151 the host address occupies the remaining byte.
152
153 Classful network addresses are now obsolete, having been superseded by
154 Classless Inter-Domain Routing (CIDR), which divides addresses into
155 network and host components at arbitrary bit (rather than byte) bound‐
156 aries.
157
159 An example of the use of inet_aton() and inet_ntoa() is shown below.
160 Here are some example runs:
161
162 $ ./a.out 226.000.000.037 # Last byte is in octal
163 226.0.0.31
164 $ ./a.out 0x7f.1 # First byte is in hex
165 127.0.0.1
166
167 Program source
168
169 #define _BSD_SOURCE
170 #include <arpa/inet.h>
171 #include <stdio.h>
172 #include <stdlib.h>
173
174 int
175 main(int argc, char *argv[])
176 {
177 struct in_addr addr;
178
179 if (argc != 2) {
180 fprintf(stderr, "%s <dotted-address>\n", argv[0]);
181 exit(EXIT_FAILURE);
182 }
183
184 if (inet_aton(argv[1], &addr) == 0) {
185 fprintf(stderr, "Invalid address\n");
186 exit(EXIT_FAILURE);
187 }
188
189 printf("%s\n", inet_ntoa(addr));
190 exit(EXIT_SUCCESS);
191 }
192
194 byteorder(3), getaddrinfo(3), gethostbyname(3), getnameinfo(3), getne‐
195 tent(3), inet_net_pton(3), inet_ntop(3), inet_pton(3), hosts(5), net‐
196 works(5)
197
199 This page is part of release 5.13 of the Linux man-pages project. A
200 description of the project, information about reporting bugs, and the
201 latest version of this page, can be found at
202 https://www.kernel.org/doc/man-pages/.
203
204
205
206GNU 2021-03-22 INET(3)