1inet(3SOCKET) Sockets Library Functions inet(3SOCKET)
2
3
4
6 inet, inet6, inet_ntop, inet_pton, inet_aton, inet_addr, inet_network,
7 inet_makeaddr, inet_lnaof, inet_netof, inet_ntoa - Internet address
8 manipulation
9
11 cc [ flag... ] file... -lsocket -lnsl [ library... ]
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 const char *inet_ntop(int af, const void *addr, char *cp,
18 size_t size);
19
20
21 int inet_pton(int af, const char *cp, void *addr);
22
23
24 int inet_aton(const char *cp, struct in_addr *addr);
25
26
27 in_addr_t inet_addr(const char *cp);
28
29
30 in_addr_t inet_network(const char *cp);
31
32
33 struct in_addr inet_makeaddr(const int net, const int lna);
34
35
36 int inet_lnaof(const struct in_addr in);
37
38
39 int inet_netof(const struct in_addr in);
40
41
42 char *inet_ntoa(const struct in_addr in);
43
44
46 The inet_ntop() and inet_pton() functions can manipulate both IPv4 and
47 IPv6 addresses. The inet_aton(), inet_addr(), inet_network(),
48 inet_makeaddr(), inet_lnaof(), inet_netof(), and inet_ntoa() functions
49 can only manipulate IPv4 addresses.
50
51
52 The inet_ntop() function converts a numeric address into a string suit‐
53 able for presentation. The af argument specifies the family of the
54 address which can be AF_INET or AF_INET6. The addr argument points to a
55 buffer that holds an IPv4 address if the af argument is AF_INET. The
56 addr argument points to a buffer that holds an IPv6 address if the af
57 argument is AF_INET6. The address must be in network byte order. The cp
58 argument points to a buffer where the function stores the resulting
59 string. The application must specify a non-NULL cp argument. The size
60 argument specifies the size of this buffer. For IPv6 addresses, the
61 buffer must be at least 46-octets. For IPv4 addresses, the buffer must
62 be at least 16-octets. To allow applications to easily declare buffers
63 of the proper size to store IPv4 and IPv6 addresses in string form, the
64 following two constants are defined in <netinet/in.h>:
65
66 #define INET_ADDRSTRLEN 16
67 #define INET6_ADDRSTRLEN 46
68
69
70
71 The inet_pton() function converts the standard text presentation form
72 of a function to the numeric binary form. The af argument specifies the
73 family of the address. Currently, the AF_INET and AF_INET6 address fam‐
74 ilies are supported. The cp argument points to the string being passed
75 in. The addr argument points to a buffer where the function stores the
76 numeric address. The calling application must ensure that the buffer
77 referred to by addr is large enough to hold the numeric address, at
78 least 4 bytes for AF_INET or 16 bytes for AF_INET6.
79
80
81 The inet_aton(), inet_addr(), and inet_network() functions interpret
82 character strings that represent numbers expressed in the IPv4 standard
83 '.' notation, returning numbers suitable for use as IPv4 addresses and
84 IPv4 network numbers, respectively. The inet_makeaddr() function uses
85 an IPv4 network number and a local network address to construct an IPv4
86 address. The inet_netof() and inet_lnaof() functions break apart IPv4
87 host addresses, then return the network number and local network
88 address, respectively.
89
90
91 The inet_ntoa() function returns a pointer to a string in the base 256
92 notation d.d.d.d. See the following section on IPv4 addresses.
93
94
95 Internet addresses are returned in network order, bytes ordered from
96 left to right. Network numbers and local address parts are returned as
97 machine format integer values.
98
99 IPv6 Addresses
100 There are three conventional forms for representing IPv6 addresses as
101 strings:
102
103 1. The preferred form is x:x:x:x:x:x:x:x, where the 'x's are
104 the hexadecimal values of the eight 16-bit pieces of the
105 address. For example:
106
107 1080:0:0:0:8:800:200C:417A
108
109 It is not necessary to write the leading zeros in an indi‐
110 vidual field. There must be at least one numeral in every
111 field, except when the special syntax described in the fol‐
112 lowing is used.
113
114 2. It is common for addresses to contain long strings of zero
115 bits in some methods used to allocate certain IPv6 address
116 styles. A special syntax is available to compress the zeros.
117 The use of "::" indicates multiple groups of 16 bits of
118 zeros. The :: may only appear once in an address. The :: can
119 also be used to compress the leading and trailing zeros in
120 an address. For example:
121
122 1080::8:800:200C:417A
123
124
125 3. The alternative form x:x:x:x:x:x:d.d.d.d is sometimes more
126 convenient when dealing with a mixed environment of IPv4 and
127 IPv6 nodes. The x's in this form represent the hexadecimal
128 values of the six high-order 16-bit pieces of the address.
129 The d's represent the decimal values of the four low-order
130 8-bit pieces of the standard IPv4 address. For example:
131
132 ::FFFF:129.144.52.38
133 ::129.144.52.38
134
135 The ::FFFF:d.d.d.d and ::d.d.d.d pieces are the general
136 forms of an IPv4-mapped IPv6 address and an IPv4-compatible
137 IPv6 address.
138
139 The IPv4 portion must be in the d.d.d.d form. The following
140 forms are invalid:
141
142 ::FFFF:d.d.d
143 ::FFFF:d.d
144 ::d.d.d
145 ::d.d
146
147 The ::FFFF:d form is a valid but unconventional representa‐
148 tion of the IPv4-compatible IPv6 address ::255.255.0.d.
149
150 The ::d form corresponds to the general IPv6 address
151 0:0:0:0:0:0:0:d.
152
153 IPv4 Addresses
154 Values specified using `.' notation take one of the following forms:
155
156 d.d.d.d
157 d.d.d
158 d.d
159 d
160
161
162
163 When four parts are specified, each part is interpreted as a byte of
164 data and assigned from left to right to the four bytes of an IPv4
165 address.
166
167
168 When a three-part address is specified, the last part is interpreted as
169 a 16-bit quantity and placed in the right most two bytes of the network
170 address. The three part address format is convenient for specifying
171 Class B network addresses such as 128.net.host.
172
173
174 When a two-part address is supplied, the last part is interpreted as a
175 24-bit quantity and placed in the right most three bytes of the network
176 address. The two part address format is convenient for specifying Class
177 A network addresses such as net.host.
178
179
180 When only one part is given, the value is stored directly in the net‐
181 work address without any byte rearrangement.
182
183
184 With the exception of inet_pton(), numbers supplied as parts in '.'
185 notation may be decimal, octal, or hexadecimal, as specified in C lan‐
186 guage. For example, a leading 0x or 0X implies hexadecimal. A leading 0
187 implies octal. Otherwise, the number is interpreted as decimal.
188
189
190 For IPv4 addresses, inet_pton() accepts only a string in standard IPv4
191 dot notation:
192
193 d.d.d.d
194
195
196
197 Each number has one to three digits with a decimal value between 0 and
198 255.
199
200
201 The inet_addr() function has been obsoleted by inet_aton().
202
204 The inet_aton() function returns nonzero if the address is valid, 0 if
205 the address is invalid.
206
207
208 The inet_ntop() function returns a pointer to the buffer that contains
209 a string if the conversion succeeds. Otherwise, NULL is returned. Upon
210 failure, errno is set to EAFNOSUPPORT if the af argument is invalid or
211 ENOSPC if the size of the result buffer is inadequate.
212
213
214 The inet_pton() function returns 1 if the conversion succeeds, 0 if the
215 input is not a valid IPv4 dotted-decimal string or a valid IPv6
216 address string. The function returns -1 with errno set to EAFNOSUPPORT
217 if the af argument is unknown.
218
219
220 The value INADDR_NONE, which is equivalent to (in_addr_t)(-1), is
221 returned by inet_addr() and inet_network() for malformed requests.
222
223
224 The functions inet_netof() and inet_lnaof() break apart IPv4 host
225 addresses, returning the network number and local network address part,
226 respectively.
227
228
229 The function inet_ntoa() returns a pointer to a string in the base 256
230 notation d.d.d.d, described in the section on IPv4 addresses.
231
233 See attributes(5) for descriptions of the following attributes:
234
235
236
237
238 ┌─────────────────────────────┬─────────────────────────────┐
239 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
240 ├─────────────────────────────┼─────────────────────────────┤
241 │Interface Stability │See below. │
242 ├─────────────────────────────┼─────────────────────────────┤
243 │MT-Level │Safe │
244 └─────────────────────────────┴─────────────────────────────┘
245
246
247 The inet_ntop(), inet_pton(), inet_aton(), inet_addr(), and inet_net‐
248 work() functions are Committed. The inet_lnaof(), inet_makeaddr(),
249 inet_netof(), and inet_network() functions are Committed (Obsolete).
250
252 gethostbyname(3NSL), getipnodebyname(3SOCKET), getnetbyname(3SOCKET),
253 inet.h(3HEAD), hosts(4), networks(4), attributes(5)
254
256 The return value from inet_ntoa() points to a buffer which is overwrit‐
257 ten on each call. This buffer is implemented as thread-specific data in
258 multithreaded applications.
259
260
261 IPv4-mapped addresses are not recommended.
262
264 The problem of host byte ordering versus network byte ordering is con‐
265 fusing. A simple way to specify Class C network addresses in a manner
266 similar to that for Class B and Class A is needed.
267
268
269
270SunOS 5.11 28 Nov 2007 inet(3SOCKET)