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
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(int net, int 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(): _BSD_SOURCE || _SVID_SOURCE
31
33 inet_aton() converts the Internet host address cp from the IPv4 num‐
34 bers-and-dots notation into binary form (in network byte order) and
35 stores it in the structure that inp points to. inet_aton() returns
36 nonzero if the address is valid, zero if not. The address supplied in
37 cp can have one of the following forms:
38
39 a.b.c.d Each of the four numeric parts specifies a byte of the
40 address; the bytes are assigned in left-to-right order to
41 produce the binary address.
42
43 a.b.c Parts a and b specify the first two bytes of the binary
44 address. Part c is interpreted as a 16-bit value that
45 defines the rightmost two bytes of the binary address. This
46 notation is suitable for specifying (outmoded) Class B net‐
47 work addresses.
48
49 a.b Part a specifies the first byte of the binary address. Part
50 b is interpreted as a 24-bit value that defines the rightmost
51 three bytes of the binary address. This notation is suitable
52 for specifying (outmoded) Class C network addresses.
53
54 a The value a is interpreted as a 32-bit value that is stored
55 directly into the binary address without any byte rearrange‐
56 ment.
57
58 In all of the above forms, components of the dotted address can be
59 specified in decimal, octal (with a leading 0), or hexadecimal, with a
60 leading 0X). Addresses in any of these forms are collectively termed
61 IPV4 numbers-and-dots notation. The form that uses exactly four deci‐
62 mal numbers is referred to as IPv4 dotted-decimal notation (or some‐
63 times: IPv4 dotted-quad notation).
64
65 The inet_addr() function converts the Internet host address cp from
66 IPv4 numbers-and-dots notation into binary data in network byte order.
67 If the input is invalid, INADDR_NONE (usually -1) is returned. Use of
68 this function is problematic because -1 is a valid address
69 (255.255.255.255). Avoid its use in favor of inet_aton(),
70 inet_pton(3), or getaddrinfo(3) which provide a cleaner way to indicate
71 error return.
72
73 The inet_network() function converts cp, a string in IPv4 numbers-and-
74 dots notation, into a number in host byte order suitable for use as an
75 Internet network address. On success, the converted address is
76 returned. If the input is invalid, -1 is returned.
77
78 The inet_ntoa() function converts the Internet host address in, given
79 in network byte order, to a string in IPv4 dotted-decimal notation.
80 The string is returned in a statically allocated buffer, which subse‐
81 quent calls will overwrite.
82
83 The inet_lnaof() function returns the local network address part of the
84 Internet address in. The returned value is in host byte order.
85
86 The inet_netof() function returns the network number part of the Inter‐
87 net address in. The returned value is in host byte order.
88
89 The inet_makeaddr() function is the converse of inet_netof() and
90 inet_lnaof(). It returns an Internet host address in network byte
91 order, created by combining the network number net with the local
92 address host, both in host byte order.
93
94 The structure in_addr as used in inet_ntoa(), inet_makeaddr(),
95 inet_lnaof() and inet_netof() is defined in <netinet/in.h> as:
96
97 typedef uint32_t in_addr_t;
98
99 struct in_addr {
100 in_addr_t s_addr;
101 };
102
104 4.3BSD. inet_addr() and inet_ntoa() are specified in POSIX.1-2001.
105 inet_aton() is not specified in POSIX.1-2001, but is available on most
106 systems.
107
109 On the i386 the host byte order is Least Significant Byte first (little
110 endian), whereas the network byte order, as used on the Internet, is
111 Most Significant Byte first (big endian).
112
113 inet_lnaof(), inet_netof(), and inet_makeaddr() are legacy functions
114 that assume they are dealing with classful network addresses. Classful
115 networking divides IPv4 network addresses into host and network compo‐
116 nents at byte boundaries, as follows:
117
118 Class A This address type is indicated by the value 0 in the most
119 significant bit of the (network byte ordered) address. The
120 network address is contained in the most significant byte,
121 and the host address occupies the remaining three bytes.
122
123 Class B This address type is indicated by the binary value 10 in the
124 most significant two bits of the address. The network
125 address is contained in the two most significant bytes, and
126 the host address occupies the remaining two bytes.
127
128 Class C This address type is indicated by the binary value 110 in the
129 most significant three bits of the address. The network
130 address is contained in the three most significant bytes, and
131 the host address occupies the remaining byte.
132
133 Classful network addresses are now obsolete, having been superseded by
134 Classless Inter-Domain Routing (CIDR), which divides addresses into
135 network and host components at arbitrary bit (rather than byte) bound‐
136 aries.
137
139 An example of the use of inet_aton() and inet_ntoa() is shown below.
140 Here are some example runs:
141
142 $ ./a.out 226.000.000.037 # Last byte is in octal
143 226.0.0.31
144 $ ./a.out 0x7f.1 # First byte is in hex
145 127.0.0.1
146
147 Program source
148
149 #define _BSD_SOURCE
150 #include <arpa/inet.h>
151 #include <stdio.h>
152 #include <stdlib.h>
153
154 int
155 main(int argc, char *argv[])
156 {
157 struct in_addr addr;
158
159 if (argc != 2) {
160 fprintf(stderr, "%s <dotted-address>\n", argv[0]);
161 exit(EXIT_FAILURE);
162 }
163
164 if (inet_aton(argv[1], &addr) == 0) {
165 perror("inet_aton");
166 exit(EXIT_FAILURE);
167 }
168
169 printf("%s\n", inet_ntoa(addr));
170 exit(EXIT_SUCCESS);
171 }
172
174 byteorder(3), getaddrinfo(3), gethostbyname(3), getnameinfo(3), getne‐
175 tent(3), inet_ntop(3), inet_pton(3), hosts(5), networks(5)
176
178 This page is part of release 3.25 of the Linux man-pages project. A
179 description of the project, and information about reporting bugs, can
180 be found at http://www.kernel.org/doc/man-pages/.
181
182
183
184GNU 2008-06-19 INET(3)