inet_net_pton(3) Library Functions Manual inet_net_pton(3) inet_net_pton, inet_net_ntop - (libresolv -lresolv) #include int inet_net_pton(size_t nsize; int af, const char *pres, void netp[nsize], size_t nsize); char *inet_net_ntop(int bits, size_t psize; int af, const void netp[(bits - CHAR_BIT + 1) / CHAR_BIT], int bits, char pres[psize], size_t psize); glibc ( feature_test_macros(7)): inet_net_pton(), inet_net_ntop(): glibc 2.20: _DEFAULT_SOURCE glibc 2.20: _BSD_SOURCE || _SVID_SOURCE ( ) ( ). af AF_INET. inet_net_pton() inet_net_pton() pres . netp. ( netp in_addr.) nsize netp. inet_net_pton() netp. . : netp inet_net_pton() ( pres) . inet_net_ntop() inet_net_ntop() netp *netp . bits *netp. pres. psize pres. CIDR: . inet_net_pton() . -1 errno . inet_net_ntop() pres. NULL errno . EAFNOSUPPORT af AF_INET. EMSGSIZE . ENOENT (inet_net_pton()) pres . . inet_net_pton() . "0x" "0X". ( ) . . : a.b.c.d a.b.c a.b a 0 255 ( ). . 0 32 . inet_net_pton() inet_net_pton() . inet_net_pton(). bits : o 240 bits 32. o 224 bits 4. o 192 bits 24. o 128 bits 16. o bits 8. bits 8 bits/8 bits 8 . inet_net_pton() inet_net_ntop(). inet_net_pton() inet_net_pton(). inet_net_ntop() . inet_net_pton() netp inet_net_pton(). inet_net_pton() inet_net_pton(). inet_net_pton() : $ ./a.out 193.168; inet_net_pton(): 24 inet_net_ntop(): 193.168.0/24 : c1a80000 inet_net_pton() : $ ./a.out 193.168 0xffffffff; inet_net_pton(): 24 inet_net_ntop(): 193.168.0/24 : c1a800ff inet_net_pton() : $ ./a.out 193.168.1.128; inet_net_pton() returned: 32 inet_net_ntop() yielded: 193.168.1.128/32 Raw address: c1a80180 ( inet_net_pton(): ): $ ./a.out 193.168.1.128/24; inet_net_pton() returned: 24 inet_net_ntop() yielded: 193.168.1/24 Raw address: c1a80180 /* Link with "-lresolv" */ #include #include #include #include int main(int argc, char *argv[]) { char buf[100]; struct in_addr addr; int bits; if (argc < 2) { fprintf(stderr, "Usage: %s presentation-form [addr-init-value]\n", argv[0]); exit(EXIT_FAILURE); } /* If argv[2] is supplied (a numeric value), use it to initialize the output buffer given to inet_net_pton(), so that we can see that inet_net_pton() initializes only those bytes needed for the network number. If argv[2] is not supplied, then initialize the buffer to zero (as is recommended practice). */ addr.s_addr = (argc > 2) ? strtod(argv[2], NULL) : 0; /* Convert presentation network number in argv[1] to binary. */ bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr)); if (bits == -1) err(EXIT_FAILURE, "inet_net_ntop"); printf("inet_net_pton() returned: %d\n", bits); /* Convert binary format back to presentation, using 'bits' returned by inet_net_pton(). */ if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL) err(EXIT_FAILURE, "inet_net_ntop"); printf("inet_net_ntop() yielded: %s\n", buf); /* Display 'addr' in raw form (in network byte order), so we can see bytes not displayed by inet_net_ntop(); some of those bytes may not have been touched by inet_net_ntop(), and so will still have any initial value that was specified in argv[2]. */ printf("Raw address: %x\n", htonl(addr.s_addr)); exit(EXIT_SUCCESS); } inet(3), networks(5) 3 . . : . 6.18 8 2026 inet_net_pton(3)