2 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH INET_NET_PTON 3 2014-04-14 "Linux" "Linux Programmer's Manual"
28 inet_net_pton, inet_net_ntop \- Internet network number conversion
31 .B #include <arpa/inet.h>
33 .BI "int inet_net_pton(int " af ", const char *" pres ,
34 .BI " void *" netp ", size_t " nsize );
36 .BI "char *inet_net_ntop(int " af ", const void *" netp ", int " bits ,
37 .BI " char *" pres ", size_t " psize );
44 Feature Test Macro Requirements for glibc (see
45 .BR feature_test_macros (7)):
58 _BSD_SOURCE || _SVID_SOURCE
63 These functions convert network numbers between
64 presentation (i.e., printable) format and network (i.e., binary) format.
68 specifies the address family for the conversion;
69 the only supported value is
76 a null-terminated string containing an Internet network number in
77 presentation format to network format.
78 The result of the conversion, which is in network byte order,
79 is placed in the buffer pointed to by
83 argument typically points to an
88 argument specifies the number of bytes available in
93 returns the number of bits in the network number field
94 of the result placed in
96 For a discussion of the input presentation format and the return value,
100 the buffer pointed to by
102 should be zeroed out before calling
103 .BR inet_net_pton (),
104 since the call writes only as many bytes as are required
105 for the network number (or as are explicitly specified by
107 which may be less than the number of bytes in a complete network address.
111 function converts the network number in the buffer pointed to by
113 to presentation format;
115 is interpreted as a value in network byte order.
118 argument specifies the number of bits in the network number in
121 The null-terminated presentation-format string
122 is placed in the buffer pointed to by
126 argument specifies the number of bytes available in
128 The presentation string is in CIDR format:
129 a dotted-decimal number representing the network address,
130 followed by a slash, and the size of the network number in bits.
134 returns the number of bits in the network number.
135 On error, it returns \-1, and
137 is set to indicate the cause of the error.
143 On error, it returns NULL, and
145 is set to indicate the cause of the error.
150 specified a value other than
154 The size of the output buffer was insufficient.
157 .RB ( inet_net_pton ())
159 was not in correct presentation format.
165 functions are nonstandard, but widely available.
167 .SS Input presentation format for inet_net_pton()
168 The network number may be specified either
169 as a hexadecimal value
170 or in dotted-decimal notation.
172 Hexadecimal values are indicated by an initial "0x" or "0X".
173 The hexadecimal digits populate the nibbles (half octets) of the
174 network number from left to right in network byte order.
175 .\" If the hexadecimal string is short, the remaining nibbles are zeroed.
177 In dotted-decimal notation, up to four octets are specified,
178 as decimal numbers separated by dots.
179 Thus, any of the following forms are accepted:
186 Each part is a number in the range 0 to 255 that
187 populates one byte of the resulting network number,
188 going from left to right, in network-byte (big endian) order.
189 Where a part is omitted, the resulting byte in the network number is zero.
190 .\" Reading other man pages, some other implementations treat
191 .\" 'c' in a.b.c as a 16-bit number that populates right-most two bytes
192 .\" 'b' in a.b as a 24-bit number that populates right-most three bytes
194 For either hexadecimal or dotted-decimal format,
195 the network number can optionally be followed by a slash
196 and a number in the range 0 to 32,
197 which specifies the size of the network number in bits.
198 .SS Return value of inet_net_pton()
201 is the number of bits in the network number field.
202 If the input presentation string terminates with a slash and
203 an explicit size value, then that size becomes the return value of
204 .BR inet_net_pton ().
205 Otherwise, the return value,
207 is inferred as follows:
209 If the most significant byte of the network number is
210 greater than or equal to 240,
216 if the most significant byte of the network number is
217 greater than or equal to 224,
223 if the most significant byte of the network number is
224 greater than or equal to 192,
230 if the most significant byte of the network number is
231 greater than or equal to 128,
242 value from the above steps is greater than or equal to 8,
243 but the number of octets specified in the network number exceed
247 is set to 8 times the number of octets actually specified.
249 The program below demonstrates the use of
252 .BR inet_net_ntop ().
255 to convert the presentation format network address provided in
256 its first command-line argument to binary form, displays the return value from
257 .BR inet_net_pton ().
260 to convert the binary form back to presentation format,
261 and displays the resulting string.
263 In order to demonstrate that
265 may not write to all bytes of its
267 argument, the program allows an optional second command-line argument,
268 a number used to initialize the buffer before
271 As its final line of output,
272 the program displays all of the bytes of the buffer returned by
274 allowing the user to see which bytes have not been touched by
275 .BR inet_net_pton ().
277 An example run, showing that
279 infers the number of bits in the network number:
283 $ \fB./a.out 193.168\fP
284 inet_net_pton() returned: 24
285 inet_net_ntop() yielded: 193.168.0/24
286 Raw address: c1a80000
292 does not zero out unused bytes in its result buffer:
296 $ \fB./a.out 193.168 0xffffffff\fP
297 inet_net_pton() returned: 24
298 inet_net_ntop() yielded: 193.168.0/24
299 Raw address: c1a800ff
305 will widen the inferred size of the network number,
306 if the supplied number of bytes in the presentation
307 string exceeds the inferred value:
311 $ \fB./a.out 193.168.1.128\fP
312 inet_net_pton() returned: 32
313 inet_net_ntop() yielded: 193.168.1.128/32
314 Raw address: c1a80180
318 Explicitly specifying the size of the network number overrides any
319 inference about its size
320 (but any extra bytes that are explicitly specified will still be used by
321 .BR inet_net_pton ():
322 to populate the result buffer):
326 $ \fB./a.out 193.168.1.128/24\fP
327 inet_net_pton() returned: 24
328 inet_net_ntop() yielded: 193.168.1/24
329 Raw address: c1a80180
334 /* Link with -lresolv */
336 #include <arpa/inet.h>
340 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
344 main(int argc, char *argv[])
352 "Usage: %s presentation\-form [addr\-init\-value]\\n",
357 /* If argv[2] is supplied (a numeric value), use it to initialize
358 the output buffer given to inet_net_pton(), so that we can see
359 that inet_net_pton() initializes only those bytes needed for
360 the network number. If argv[2] is not supplied, then initialize
361 the buffer to zero (as is recommended practice). */
363 addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0;
365 /* Convert presentation network number in argv[1] to binary */
367 bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr));
369 errExit("inet_net_ntop");
371 printf("inet_net_pton() returned: %d\\n", bits);
373 /* Convert binary format back to presentation, using \(aqbits\(aq
374 returned by inet_net_pton() */
376 if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL)
377 errExit("inet_net_ntop");
379 printf("inet_net_ntop() yielded: %s\\n", buf);
381 /* Display \(aqaddr\(aq in raw form (in network byte order), so we can
382 see bytes not displayed by inet_net_ntop(); some of those bytes
383 may not have been touched by inet_net_ntop(), and so will still
384 have any initial value that was specified in argv[2]. */
386 printf("Raw address: %x\\n", htonl(addr.s_addr));
395 This page is part of release 3.67 of the Linux
398 A description of the project,
399 information about reporting bugs,
400 and the latest version of this page,
402 \%http://www.kernel.org/doc/man\-pages/.