inet_pton(3) Library Functions Manual inet_pton(3) inet_pton - IPv4 IPv6 LIBRARY Standard C library (libc, -lc) #include int inet_pton(int af, const char *restrict src, void *restrict dst); src af, dst. af AF_INET AF_INET6. dst . : AF_INET src , IPv4 <>, ddd -- 0 255, . struct in_addr dst, sizeof(struct in_addr) (4) (32 ). AF_INET6 src , IPv6. struct in6_addr dst, sizeof(struct in6_addr) (16) (128 ). IPv6: o -- x:x:x:x:x:x:x:x. , 16- (. ., x 4 ). o ::. ::. , 0:0:0:0:0:0:0:1 ::1. , , ::. o -- IPv4- IPv6. x:x:x:x:x:x:d.d.d.d, x , 16- (. ., 96 ), d - , 32 . : ::FFFF:204.152.189.116. IPv6 RFC 2373. inet_pton() 1 ( ). 0, src , . af , -1 errno EAFNOSUPPORT. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |inet_pton() | | MT-Safe locale | +----------------------------+----------------------------------------------------------+--------------------------+ inet_aton(3) inet_addr(3), inet_pton() IPv6. , inet_pton() IPv4 - , inet_aton(3) inet_addr(3) -- (numbers-and-dots notation) ( , , ). , IPv6, IPv4 --, getaddrinfo(3). POSIX.1-2008. POSIX.1-2001. AF_INET6 IPv4. src IPv4- IPv6. inet_pton() inet_ntop(3). : $ ./a.out i6 0:0:0:0:0:0:0:0 :: $ ./a.out i6 1:0:0:0:0:0:0:8 1::8 $ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116 ::ffff:204.152.189.116 #include #include #include #include int main(int argc, char *argv[]) { unsigned char buf[sizeof(struct in6_addr)]; int domain, s; char str[INET6_ADDRSTRLEN]; if (argc != 3) { fprintf(stderr, "Usage: %s {i4|i6|} string\n", argv[0]); exit(EXIT_FAILURE); } domain = (strcmp(argv[1], "i4") == 0) ? AF_INET : (strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]); s = inet_pton(domain, argv[2], buf); if (s <= 0) { if (s == 0) fprintf(stderr, "Not in presentation format"); else perror("inet_pton"); exit(EXIT_FAILURE); } if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) { perror("inet_ntop"); exit(EXIT_FAILURE); } printf("%s\n", str); exit(EXIT_SUCCESS); } . getaddrinfo(3), inet(3), inet_ntop(3) Azamat Hackimov , Dmitriy S. Seregin , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . inet_pton(3)