1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH inet_net_pton 3 (date) "Linux man-pages (unreleased)"
7 inet_net_pton, inet_net_ntop \- Internet network number conversion
10 .RI ( libresolv ", " \-lresolv )
13 .B #include <arpa/inet.h>
15 .BI "int inet_net_pton(int " af ", const char *" pres ,
16 .BI " void " netp [. nsize "], size_t " nsize );
17 .BI "char *inet_net_ntop(int " af ,
18 .BI " const void " netp [(. bits " - CHAR_BIT + 1) / CHAR_BIT],"
20 .BI " char " pres [. psize "], size_t " psize );
24 Feature Test Macro Requirements for glibc (see
25 .BR feature_test_macros (7)):
34 _BSD_SOURCE || _SVID_SOURCE
37 These functions convert network numbers between
38 presentation (i.e., printable) format and network (i.e., binary) format.
42 specifies the address family for the conversion;
43 the only supported value is
50 a null-terminated string containing an Internet network number in
51 presentation format to network format.
52 The result of the conversion, which is in network byte order,
53 is placed in the buffer pointed to by
57 argument typically points to an
62 argument specifies the number of bytes available in
67 returns the number of bits in the network number field
68 of the result placed in
70 For a discussion of the input presentation format and the return value,
74 the buffer pointed to by
76 should be zeroed out before calling
78 since the call writes only as many bytes as are required
79 for the network number (or as are explicitly specified by
81 which may be less than the number of bytes in a complete network address.
85 function converts the network number in the buffer pointed to by
87 to presentation format;
89 is interpreted as a value in network byte order.
92 argument specifies the number of bits in the network number in
95 The null-terminated presentation-format string
96 is placed in the buffer pointed to by
100 argument specifies the number of bytes available in
102 The presentation string is in CIDR format:
103 a dotted-decimal number representing the network address,
104 followed by a slash, and the size of the network number in bits.
108 returns the number of bits in the network number.
109 On error, it returns \-1, and
111 is set to indicate the error.
117 On error, it returns NULL, and
119 is set to indicate the error.
124 specified a value other than
128 The size of the output buffer was insufficient.
131 .RB ( inet_net_pton ())
133 was not in correct presentation format.
137 .SS Input presentation format for inet_net_pton()
138 The network number may be specified either
139 as a hexadecimal value
140 or in dotted-decimal notation.
142 Hexadecimal values are indicated by an initial "0x" or "0X".
143 The hexadecimal digits populate the nibbles (half octets) of the
144 network number from left to right in network byte order.
145 .\" If the hexadecimal string is short, the remaining nibbles are zeroed.
147 In dotted-decimal notation, up to four octets are specified,
148 as decimal numbers separated by dots.
149 Thus, any of the following forms are accepted:
160 Each part is a number in the range 0 to 255 that
161 populates one byte of the resulting network number,
162 going from left to right, in network-byte (big endian) order.
163 Where a part is omitted, the resulting byte in the network number is zero.
164 .\" Reading other man pages, some other implementations treat
165 .\" 'c' in a.b.c as a 16-bit number that populates right-most two bytes
166 .\" 'b' in a.b as a 24-bit number that populates right-most three bytes
168 For either hexadecimal or dotted-decimal format,
169 the network number can optionally be followed by a slash
170 and a number in the range 0 to 32,
171 which specifies the size of the network number in bits.
172 .SS Return value of inet_net_pton()
175 is the number of bits in the network number field.
176 If the input presentation string terminates with a slash and
177 an explicit size value, then that size becomes the return value of
178 .BR inet_net_pton ().
179 Otherwise, the return value,
181 is inferred as follows:
183 If the most significant byte of the network number is
184 greater than or equal to 240,
190 if the most significant byte of the network number is
191 greater than or equal to 224,
197 if the most significant byte of the network number is
198 greater than or equal to 192,
204 if the most significant byte of the network number is
205 greater than or equal to 128,
216 value from the above steps is greater than or equal to 8,
217 but the number of octets specified in the network number exceed
221 is set to 8 times the number of octets actually specified.
223 The program below demonstrates the use of
226 .BR inet_net_ntop ().
229 to convert the presentation format network address provided in
230 its first command-line argument to binary form, displays the return value from
231 .BR inet_net_pton ().
234 to convert the binary form back to presentation format,
235 and displays the resulting string.
237 In order to demonstrate that
239 may not write to all bytes of its
241 argument, the program allows an optional second command-line argument,
242 a number used to initialize the buffer before
245 As its final line of output,
246 the program displays all of the bytes of the buffer returned by
248 allowing the user to see which bytes have not been touched by
249 .BR inet_net_pton ().
251 An example run, showing that
253 infers the number of bits in the network number:
257 $ \fB./a.out 193.168\fP
258 inet_net_pton() returned: 24
259 inet_net_ntop() yielded: 193.168.0/24
260 Raw address: c1a80000
266 does not zero out unused bytes in its result buffer:
270 $ \fB./a.out 193.168 0xffffffff\fP
271 inet_net_pton() returned: 24
272 inet_net_ntop() yielded: 193.168.0/24
273 Raw address: c1a800ff
279 will widen the inferred size of the network number,
280 if the supplied number of bytes in the presentation
281 string exceeds the inferred value:
285 $ \fB./a.out 193.168.1.128\fP
286 inet_net_pton() returned: 32
287 inet_net_ntop() yielded: 193.168.1.128/32
288 Raw address: c1a80180
292 Explicitly specifying the size of the network number overrides any
293 inference about its size
294 (but any extra bytes that are explicitly specified will still be used by
295 .BR inet_net_pton ():
296 to populate the result buffer):
300 $ \fB./a.out 193.168.1.128/24\fP
301 inet_net_pton() returned: 24
302 inet_net_ntop() yielded: 193.168.1/24
303 Raw address: c1a80180
307 .\" SRC BEGIN (inet_net_pton.c)
309 /* Link with "\-lresolv" */
311 #include <arpa/inet.h>
315 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
319 main(int argc, char *argv[])
327 "Usage: %s presentation\-form [addr\-init\-value]\en",
332 /* If argv[2] is supplied (a numeric value), use it to initialize
333 the output buffer given to inet_net_pton(), so that we can see
334 that inet_net_pton() initializes only those bytes needed for
335 the network number. If argv[2] is not supplied, then initialize
336 the buffer to zero (as is recommended practice). */
338 addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0;
340 /* Convert presentation network number in argv[1] to binary. */
342 bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr));
344 errExit("inet_net_ntop");
346 printf("inet_net_pton() returned: %d\en", bits);
348 /* Convert binary format back to presentation, using \[aq]bits\[aq]
349 returned by inet_net_pton(). */
351 if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL)
352 errExit("inet_net_ntop");
354 printf("inet_net_ntop() yielded: %s\en", buf);
356 /* Display \[aq]addr\[aq] in raw form (in network byte order), so we can
357 see bytes not displayed by inet_net_ntop(); some of those bytes
358 may not have been touched by inet_net_ntop(), and so will still
359 have any initial value that was specified in argv[2]. */
361 printf("Raw address: %x\en", htonl(addr.s_addr));