getaddrinfo(3) Library Functions Manual getaddrinfo(3) getaddrinfo, freeaddrinfo, gai_strerror - LIBRARY Standard C library (libc, -lc) #include #include #include int getaddrinfo(const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res); void freeaddrinfo(struct addrinfo *res); const char *gai_strerror(int errcode); glibc (. feature_test_macros(7)): getaddrinfo(), freeaddrinfo(), gai_strerror(): Since glibc 2.22: _POSIX_C_SOURCE >= 200112L glibc 2.21 and earlier: _POSIX_C_SOURCE node service, , getaddrinfo() addrinfo, -, bind(2) connect(2). getaddrinfo() , gethostbyname(3) getservbyname(3) , , getaddrinfo() IPv4 IPv6. addrinfo, getaddrinfo(), : struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; socklen_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }; hints addrinfo, , , res. hints NULL, addrinfo. ai_family, ai_socktype ai_protocol , , getaddrinfo(): ai_family . AF_INET AF_INET6. AF_UNSPEC , getaddrinfo() (, IPv4, IPv6), node service. ai_socktype , , SOCK_STREAM SOCK_DGRAM. 0, , getaddrinfo() . ai_protocol . 0, , getaddrinfo() . ai_flags , . . , hints, 0 null, . hints NULL ai_socktype ai_protocol 0; ai_family AF_UNSPEC; ai_flags (AI_V4MAPPED | AI_ADDRCONFIG) ( POSIX ai_flags; ). node ( IPv4 - , inet_aton(3); IPv6 , inet_pton(3)), , . hints.ai_flags AI_NUMERICHOST, node . AI_NUMERICHOST . hints.ai_flags AI_PASSIVE node NULL, bind(2) , accept(2). << >> (INADDR_ANY IPv4, IN6ADDR_ANY_INIT IPv6). (, ), . node NULL, AI_PASSIVE . AI_PASSIVE hints.ai_flags, connect(2), sendto(2), sendmsg(2). node NULL, (loopback) (INADDR_LOOPBACK IPv4, IN6ADDR_LOOPBACK_INIT IPv6); , , . service . -- ( services(5)), . , , . service NULL, . hints.ai_flags AI_NUMERICSERV service NULL, service . , , . node, service ( ) NULL. getaddrinfo() addrinfo, , node service, , hints, res. ai_next. , addrinfo: , (, AF_INET AF_INET6); (, -- SOCK_STREAM, -- SOCK_DGRAM). , , . , getaddrinfo(), RFC 3484; /etc/gai.conf (, glibc 2.5). hints.ai_flags AI_CANONNAME, ai_canonname addrinfo . addrinfo : o ai_family, ai_socktype ai_protocol (. ., , socket(2)). , ai_family AF_INET AF_INET6; ai_socktype SOCK_DGRAM SOCK_STREAM; ai_protocol . o ai_addr, ( ) ai_addrlen. hints.ai_flags AI_ADDRCONFIG, IPv4, , res, , , , IPv4, IPv6 , , , IPv6. (loopback) . , IPv4-, getaddrinfo() IPv6, connect(2) bind(2). hints.ai_flags AI_V4MAPPED hints.ai_family AF_INET6, IPv6, , res, IPv6 IPv4. hints.ai_flags AI_V4MAPPED, AI_ALL, , res, IPv6 IPv6 IPv4. AI_ALL , AI_V4MAPPED. freeaddrinfo() , res. getaddrinfo() glibc 2.3.4, getaddrinfo() (IDN, . RFC 3490, Internationalizing Domain Names in Applications (IDNA)). : AI_IDN , , , , node, IDN-. . -ASCII, IDN. ( ), -ASCII, ASCII Compatible Encoding (ACE) , AI_CANONIDN AI_CANONNAME getaddrinfo() addrinfo. , . ACE, xn-- . , AI_CANONNAME AI_CANONIDN. . AI_IDN_ALLOW_UNASSIGNED AI_IDN_USE_STD3_ASCII_RULES IDNA_ALLOW_UNASSIGNED ( ) IDNA_USE_STD3_ASCII_RULES ( STD3) IDNA. getaddrinfo() 0, : EAI_ADDRFAMILY . EAI_AGAIN . . EAI_BADFLAGS hints.ai_flags , hints.ai_flags AI_CANONNAME, name - NULL. EAI_FAIL . EAI_FAMILY . EAI_MEMORY . EAI_NODATA , . EAI_NONAME node service ; node, service NULL; hints.ai_flags AI_NUMERICSERV, service . EAI_SERVICE . . , , service <> (, ) hints.ai_protocol IPPROTO_UDP, hints.ai_socktype SOCK_DGRAM. , service NULL, hints.ai_socktype SOCK_RAW ( , ). EAI_SOCKTYPE . , hints.ai_socktype hints.ai_protocol (, SOCK_DGRAM IPPROTO_TCP ). EAI_SYSTEM Other system error; errno is set to indicate the error. gai_strerror() , . /etc/gai.conf attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getaddrinfo() | | MT-Safe env locale | +----------------------------+----------------------------------------------------------+--------------------------+ |freeaddrinfo(), | | MT-Safe | |gai_strerror() | | | +----------------------------+----------------------------------------------------------+--------------------------+ According to POSIX.1, specifying hints as NULL should cause ai_flags to be assumed as 0. The GNU C library instead assumes a value of (AI_V4MAPPED | AI_ADDRCONFIG) for this case, since this value is considered an improvement on the specification. POSIX.1-2008. getaddrinfo() RFC 2553. POSIX.1-2001. AI_ADDRCONFIG AI_ALL AI_V4MAPPED glibc 2.3.3. AI_NUMERICSERV glibc 2.3.4. getaddrinfo() address%scope-id IPv6 scope-ID. getaddrinfo(), gai_strerror(), freeaddrinfo() getnameinfo(3). - UDP-. #include #include #include #include #include #include #include #define BUF_SIZE 500 int main(int argc, char *argv[]) { int sfd, s; char buf[BUF_SIZE]; ssize_t nread; socklen_t peer_addrlen; struct addrinfo hints; struct addrinfo *result, *rp; struct sockaddr_storage peer_addr; if (argc != 2) { fprintf(stderr, "Usage: %s port\n", argv[0]); exit(EXIT_FAILURE); } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ hints.ai_protocol = 0; /* Any protocol */ hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; s = getaddrinfo(NULL, argv[1], &hints, &result); if (s != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); exit(EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully bind(2). If socket(2) (or bind(2)) fails, we (close the socket and) try the next address. */ for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) break; /* Success */ close(sfd); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { /* No address succeeded */ fprintf(stderr, "Could not bind\n"); exit(EXIT_FAILURE); } /* Read datagrams and echo them back to sender. */ for (;;) { char host[NI_MAXHOST], service[NI_MAXSERV]; peer_addrlen = sizeof(peer_addr); nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addrlen); if (nread == -1) continue; /* Ignore failed request */ s = getnameinfo((struct sockaddr *) &peer_addr, peer_addrlen, host, NI_MAXHOST, service, NI_MAXSERV, NI_NUMERICSERV); if (s == 0) printf("Received %zd bytes from %s:%s\n", nread, host, service); else fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s)); if (sendto(sfd, buf, nread, 0, (struct sockaddr *) &peer_addr, peer_addrlen) != nread) { fprintf(stderr, "Error sending response\n"); } } } #include #include #include #include #include #include #include #define BUF_SIZE 500 int main(int argc, char *argv[]) { int sfd, s; char buf[BUF_SIZE]; size_t len; ssize_t nread; struct addrinfo hints; struct addrinfo *result, *rp; if (argc < 3) { fprintf(stderr, "Usage: %s host port msg...\n", argv[0]); exit(EXIT_FAILURE); } /* Obtain address(es) matching host/port. */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0; hints.ai_protocol = 0; /* Any protocol */ s = getaddrinfo(argv[1], argv[2], &hints, &result); if (s != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); exit(EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully connect(2). If socket(2) (or connect(2)) fails, we (close the socket and) try the next address. */ for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) break; /* Success */ close(sfd); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { /* No address succeeded */ fprintf(stderr, "Could not connect\n"); exit(EXIT_FAILURE); } /* Send remaining command-line arguments as separate datagrams, and read responses from server. */ for (size_t j = 3; j < argc; j++) { len = strlen(argv[j]) + 1; /* +1 for terminating null byte */ if (len > BUF_SIZE) { fprintf(stderr, "Ignoring long message in argument %zu\n", j); continue; } if (write(sfd, argv[j], len) != len) { fprintf(stderr, "partial/failed write\n"); exit(EXIT_FAILURE); } nread = read(sfd, buf, BUF_SIZE); if (nread == -1) { perror("read"); exit(EXIT_FAILURE); } printf("Received %zd bytes: %s\n", nread, buf); } exit(EXIT_SUCCESS); } . getaddrinfo_a(3), gethostbyname(3), getnameinfo(3), inet(3), gai.conf(5), hostname(7), ip(7) Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . getaddrinfo(3)