1inet_net_pton(3)           Library Functions Manual           inet_net_pton(3)
2
3
4

NAME

6       inet_net_pton, inet_net_ntop - Internet network number conversion
7

LIBRARY

9       Resolver library (libresolv, -lresolv)
10

SYNOPSIS

12       #include <arpa/inet.h>
13
14       int inet_net_pton(int af, const char *pres,
15                         void netp[.nsize], size_t nsize);
16       char *inet_net_ntop(int af,
17                         const void netp[(.bits - CHAR_BIT + 1) / CHAR_BIT],
18                         int bits,
19                         char pres[.psize], size_t psize);
20
21   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23       inet_net_pton(), inet_net_ntop():
24           Since glibc 2.20:
25               _DEFAULT_SOURCE
26           Before glibc 2.20:
27               _BSD_SOURCE || _SVID_SOURCE
28

DESCRIPTION

30       These  functions  convert  network  numbers between presentation (i.e.,
31       printable) format and network (i.e., binary) format.
32
33       For both functions, af specifies the address family for the conversion;
34       the only supported value is AF_INET.
35
36   inet_net_pton()
37       The  inet_net_pton()  function  converts pres, a null-terminated string
38       containing an Internet network number in presentation format to network
39       format.   The result of the conversion, which is in network byte order,
40       is placed in the buffer pointed to by netp.  (The netp  argument  typi‐
41       cally  points  to  an in_addr structure.)  The nsize argument specifies
42       the number of bytes available in netp.
43
44       On success, inet_net_pton() returns the number of bits in  the  network
45       number field of the result placed in netp.  For a discussion of the in‐
46       put presentation format and the return value, see NOTES.
47
48       Note: the buffer pointed to by netp should be zeroed out before calling
49       inet_net_pton(),  since  the  call writes only as many bytes as are re‐
50       quired for the network number (or as are explicitly specified by pres),
51       which  may  be  less than the number of bytes in a complete network ad‐
52       dress.
53
54   inet_net_ntop()
55       The inet_net_ntop() function converts the network number in the  buffer
56       pointed  to  by  netp to presentation format; *netp is interpreted as a
57       value in network byte order.  The bits argument specifies the number of
58       bits in the network number in *netp.
59
60       The  null-terminated presentation-format string is placed in the buffer
61       pointed to by pres.  The psize argument specifies the number  of  bytes
62       available  in  pres.  The presentation string is in CIDR format: a dot‐
63       ted-decimal number representing the  network  address,  followed  by  a
64       slash, and the size of the network number in bits.
65

RETURN VALUE

67       On  success,  inet_net_pton() returns the number of bits in the network
68       number.  On error, it returns -1, and errno is set to indicate the  er‐
69       ror.
70
71       On  success,  inet_net_ntop() returns pres.  On error, it returns NULL,
72       and errno is set to indicate the error.
73

ERRORS

75       EAFNOSUPPORT
76              af specified a value other than AF_INET.
77
78       EMSGSIZE
79              The size of the output buffer was insufficient.
80
81       ENOENT (inet_net_pton()) pres was not in correct presentation format.
82

STANDARDS

84       None.
85

NOTES

87   Input presentation format for inet_net_pton()
88       The network number may be specified either as a hexadecimal value or in
89       dotted-decimal notation.
90
91       Hexadecimal values are indicated by an initial "0x" or "0X".  The hexa‐
92       decimal digits populate the nibbles (half octets) of the network number
93       from left to right in network byte order.
94
95       In dotted-decimal notation, up to four octets are specified, as decimal
96       numbers separated by dots.  Thus, any of the following  forms  are  ac‐
97       cepted:
98
99           a.b.c.d
100           a.b.c
101           a.b
102           a
103
104       Each  part is a number in the range 0 to 255 that populates one byte of
105       the resulting network number, going from left to right, in network-byte
106       (big endian) order.  Where a part is omitted, the resulting byte in the
107       network number is zero.
108
109       For either hexadecimal or dotted-decimal format, the network number can
110       optionally  be  followed  by a slash and a number in the range 0 to 32,
111       which specifies the size of the network number in bits.
112
113   Return value of inet_net_pton()
114       The return value of inet_net_pton() is the number of bits in  the  net‐
115       work  number field.  If the input presentation string terminates with a
116       slash and an explicit size value, then that  size  becomes  the  return
117       value  of  inet_net_pton().   Otherwise, the return value, bits, is in‐
118       ferred as follows:
119
120       •  If the most significant byte of the network number is  greater  than
121          or equal to 240, then bits is 32.
122
123       •  Otherwise,  if  the  most  significant byte of the network number is
124          greater than or equal to 224, then bits is 4.
125
126       •  Otherwise, if the most significant byte of  the  network  number  is
127          greater than or equal to 192, then bits is 24.
128
129       •  Otherwise,  if  the  most  significant byte of the network number is
130          greater than or equal to 128, then bits is 16.
131
132       •  Otherwise, bits is 8.
133
134       If the resulting bits value from the above steps  is  greater  than  or
135       equal  to  8,  but the number of octets specified in the network number
136       exceed bits/8, then bits is set to 8 times the number of  octets  actu‐
137       ally specified.
138

EXAMPLES

140       The   program   below  demonstrates  the  use  of  inet_net_pton()  and
141       inet_net_ntop().  It uses inet_net_pton() to convert  the  presentation
142       format  network  address provided in its first command-line argument to
143       binary form, displays the return value from inet_net_pton().   It  then
144       uses  inet_net_ntop()  to  convert the binary form back to presentation
145       format, and displays the resulting string.
146
147       In order to demonstrate that inet_net_pton() may not write to all bytes
148       of  its  netp  argument, the program allows an optional second command-
149       line  argument,  a  number  used  to  initialize  the   buffer   before
150       inet_net_pton()  is  called.   As its final line of output, the program
151       displays all of the bytes of the buffer returned by inet_net_pton() al‐
152       lowing   the  user  to  see  which  bytes  have  not  been  touched  by
153       inet_net_pton().
154
155       An example run, showing that inet_net_pton() infers the number of  bits
156       in the network number:
157
158           $ ./a.out 193.168
159           inet_net_pton() returned: 24
160           inet_net_ntop() yielded:  193.168.0/24
161           Raw address:              c1a80000
162
163       Demonstrate  that inet_net_pton() does not zero out unused bytes in its
164       result buffer:
165
166           $ ./a.out 193.168 0xffffffff
167           inet_net_pton() returned: 24
168           inet_net_ntop() yielded:  193.168.0/24
169           Raw address:              c1a800ff
170
171       Demonstrate that inet_net_pton() will widen the inferred  size  of  the
172       network  number,  if  the  supplied number of bytes in the presentation
173       string exceeds the inferred value:
174
175           $ ./a.out 193.168.1.128
176           inet_net_pton() returned: 32
177           inet_net_ntop() yielded:  193.168.1.128/32
178           Raw address:              c1a80180
179
180       Explicitly specifying the size of the network number overrides any  in‐
181       ference  about its size (but any extra bytes that are explicitly speci‐
182       fied will still be used by inet_net_pton(): to populate the result buf‐
183       fer):
184
185           $ ./a.out 193.168.1.128/24
186           inet_net_pton() returned: 24
187           inet_net_ntop() yielded:  193.168.1/24
188           Raw address:              c1a80180
189
190   Program source
191       /* Link with "-lresolv" */
192
193       #include <arpa/inet.h>
194       #include <stdio.h>
195       #include <stdlib.h>
196
197       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
198                               } while (0)
199
200       int
201       main(int argc, char *argv[])
202       {
203           char buf[100];
204           struct in_addr addr;
205           int bits;
206
207           if (argc < 2) {
208               fprintf(stderr,
209                       "Usage: %s presentation-form [addr-init-value]\n",
210                       argv[0]);
211               exit(EXIT_FAILURE);
212           }
213
214           /* If argv[2] is supplied (a numeric value), use it to initialize
215              the output buffer given to inet_net_pton(), so that we can see
216              that inet_net_pton() initializes only those bytes needed for
217              the network number. If argv[2] is not supplied, then initialize
218              the buffer to zero (as is recommended practice). */
219
220           addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0;
221
222           /* Convert presentation network number in argv[1] to binary. */
223
224           bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr));
225           if (bits == -1)
226               errExit("inet_net_ntop");
227
228           printf("inet_net_pton() returned: %d\n", bits);
229
230           /* Convert binary format back to presentation, using 'bits'
231              returned by inet_net_pton(). */
232
233           if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL)
234               errExit("inet_net_ntop");
235
236           printf("inet_net_ntop() yielded:  %s\n", buf);
237
238           /* Display 'addr' in raw form (in network byte order), so we can
239              see bytes not displayed by inet_net_ntop(); some of those bytes
240              may not have been touched by inet_net_ntop(), and so will still
241              have any initial value that was specified in argv[2]. */
242
243           printf("Raw address:              %x\n", htonl(addr.s_addr));
244
245           exit(EXIT_SUCCESS);
246       }
247

SEE ALSO

249       inet(3), networks(5)
250
251
252
253Linux man-pages 6.04              2023-03-30                  inet_net_pton(3)
Impressum