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