inet_pton(3) Library Functions Manual inet_pton(3) inet_pton - IPv4 IPv6 (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.ddd.ddd.ddd" 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 IPv6 IPv4. x:x:x:x:x:x:d.d.d.d xs 16 ( 96 ) ds 32 . ::FFFF:204.152.189.116. RFC 2373 IPv6. inet_pton() 1 ( ). 0 src . af -1 errno EAFNOSUPPORT. attributes(7). +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ | | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |inet_pton() | | (locale) (MT-Safe) | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ inet_aton(3) inet_addr(3) inet_pton() IPv6. inet_pton() IPv4 inet_aton(3) inet_addr(3) ( ). IPv6 IPv4 getaddrinfo(3). POSIX.1-2008. POSIX.1-2001. AF_INET6 IPv4. IPv6 IPv4 src . 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 #define streq(...) (strcmp(__VA_ARGS__) == 0) 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 = streq(argv[1], "i4") ? AF_INET : streq(argv[1], "i6") ? 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) 3 . . : . 6.18 22 2026 inet_pton(3)