inet_net_pton(3) Library Functions Manual inet_net_pton(3) inet_net_pton, inet_net_ntop - LIBRARY Resolver library (libresolv, -lresolv) #include int inet_net_pton(int af, const char *pres, void netp[.nsize], size_t nsize); char *inet_net_ntop(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(): Since glibc 2.20: _DEFAULT_SOURCE Before glibc 2.20: _BSD_SOURCE || _SVID_SOURCE , (. . ), (. ., ) . af ; AF_INET. inet_net_pton() The inet_net_pton() function converts pres, a null-terminated string containing an Internet network number in presentation format to network format. The result of the conversion, which is in network byte order, is placed in the buffer pointed to by netp. (The netp argument typically points to an in_addr structure.) The nsize argument specifies the number of bytes available in netp. inet_net_pton() , netp. . : , netp, inet_net_pton(), , ( pres), , . inet_net_ntop() inet_net_ntop() , netp, ; *netp . bits *netp. null , pres. psize , pres. CIDR: , , . On success, inet_net_pton() returns the number of bits in the network number. On error, it returns -1, and errno is set to indicate the error. On success, inet_net_ntop() returns pres. On error, it returns NULL, and errno is set to indicate the error. EAFNOSUPPORT af , AF_INET. EMSGSIZE . ENOENT (inet_net_pton()) pres . None. 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() : 32 inet_net_ntop() : 193.168.1.128/32 : c1a80180 ( , , inet_net_pton() ): $ ./a.out 193.168.1.128/24 inet_net_pton() : 24 inet_net_ntop() : 193.168.1/24 : c1a80180 /* Link with "-lresolv" */ #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0) 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) errExit("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) errExit("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) Azamat Hackimov , Dmitriy S. Seregin , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . inet_net_pton(3)