Unix Network Programming Episode 78

'getaddrinfo' Function

The gethostbyname and gethostbyaddr functions only support IPv4. The API for resolving IPv6 addresses went through several iterations, as will be described in Section 11.20(See 8.9.20); the final result is the getaddrinfo function.

The POSIX definition of this function comes from an earlier proposal by Keith Sklower for a function named getconninfo. This function was the result of discussions with Eric Allman, William Durst, Michael Karels, and Steven Wise, and from an early implementation written by Eric Allman. The observation that specifying a hostname and a service name would suffice for connecting to a service independent of protocol details was made by Marshall Rose in a proposal to X/Open.

复制代码
#include <netdb.h>
int getaddrinfo (const char *hostname, const char *service, const struct addrinfo *hints, struct addrinfo **result) ;

This function returns through the result pointer a pointer to a linked list of addrinfo structures, which is defined by including <netdb.h>.

复制代码
struct addrinfo {
	int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
	int ai_family; /* AF_xxx */
	int ai_socktype; /* SOCK_xxx */
	int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
	socklen_t ai_addrlen; /* length of ai_addr */
	char *ai_canonname; /* ptr to canonical name for host */
	struct sockaddr *ai_addr; /* ptr to socket address structure */
	struct addrinfo *ai_next; /* ptr to next structure in linked list */
};

The members of the hints structure that can be set by the caller are:

  • ai_flags (zero or more AI_XXX values OR'ed together)
  • ai_family (an AF_xxx value)
  • ai_socktype (a SOCK_xxx value)
  • ai_protocol

If the function returns success (0), the variable pointed to by the result argument is filled in with a pointer to a linked list of addrinfo structures, linked through the ai_next pointer. There are two ways that multiple structures can be returned:

1.If there are multiple addresses associated with the hostname, one structure is returned for each address that is usable with the requested address family (the ai_family hint, if specified).

2.If the service is provided for multiple socket types, one structure can be returned for each socket type, depending on the ai_socktype hint. (Note that most getaddrinfo implementations consider a port number string to be implemented only by the socket type requested in ai_socktype; if ai_socktype is not specified, an error is returned instead.)

For example, if no hints are provided and if the domain service is looked up for a host with two IP addresses, four addrinfo structures are returned:

  • One for the first IP address and a socket type of SOCK_STREAM
  • One for the first IP address and a socket type of SOCK_DGRAM
  • One for the second IP address and a socket type of SOCK_STREAM
  • One for the second IP address and a socket type of SOCK_DGRAM

If we were to enumerate all 64 possible inputs to getaddrinfo (there are six input variables), many would be invalid and some would make little sense. Instead, we will look at the common cases.

  • Specify the hostname and service. This is normal for a TCP or UDP client. On return, a TCP client loops through all returned IP addresses, calling socket and connect for each one, until the connection succeeds or until all addresses have been tried. We will show an example of this with our tcp_connect function in Figure 11.10(See 8.9.12).
  • For a UDP client, the socket address structure filled in by getaddrinfo would be used in a call to sendto or connect. If the client can tell that the first address doesn't appear to work (either by receiving an error on a connected UDP socket or by experiencing a timeout on an unconnected socket), additional addresses can be tried.
  • If the client knows it handles only one type of socket (e.g., Telnet and FTP clients handle only TCP; TFTP clients handle only UDP), then the ai_socktype member of the hints structure should be specified as either SOCK_STREAM or SOCK_DGRAM.
  • A typical server specifies the service but not the hostname, and specifies the AI_PASSIVE flag in the hints structure. The socket address structures returned should contain an IP address of INADDR_ANY (for IPv4) or IN6ADDR_ANY_INIT (for IPv6). A TCP server then calls socket, bind, and listen. If the server wants to malloc another socket address structure to obtain the client's address from accept, the returned ai_addrlen value specifies this size.
  • A UDP server would call socket, bind, and then recvfrom. If the server wants to malloc another socket address structure to obtain the client's address from recvfrom, the returned ai_addrlen value specifies this size.
  • As with the typical client code, if the server knows it only handles one type of socket, the ai_socktype member of the hints structure should be set to either SOCK_STREAM or SOCK_DGRAM. This avoids having multiple structures returned, possibly with the wrong ai_socktype value.
  • The TCP servers that we have shown so far create one listening socket, and the UDP servers create one datagram socket. That is what we assume in the previous item. An alternate server design is for the server to handle multiple sockets using select or poll. In this scenario, the server would go through the entire list of structures returned by getaddrinfo, create one socket per structure, and use select or poll.
相关推荐
ai产品老杨3 分钟前
深度解析:基于异构计算的工业级AI视频中台架构,如何实现GB28181/RTSP跨平台部署与源码交付?
人工智能·架构·音视频
郑寿昌3 分钟前
虚幻引擎UE5 Lumen兼容PBR材质全解析
服务器·网络·材质
Rubin智造社4 分钟前
04月25日AI每日参考:谷歌豪掷400亿押注Anthropic,DeepSeek V4横空出世
大数据·人工智能·物联网·comfyui·deepseek v4·谷歌anthropic投资·meta亚马逊芯片
geneculture4 分钟前
本真信息观:基于序位守恒的融智学理论框架——人类认知第二次大飞跃的基础
人工智能·算法·机器学习·数据挖掘·融智学的重要应用·哲学与科学统一性·融智时代(杂志)
ch3nyuyu12 分钟前
IO缓冲区
linux·服务器
俊哥V12 分钟前
每日 AI 研究简报 · 2026-04-25
人工智能·ai
szxinmai主板定制专家16 分钟前
基于RK3588超小体积,轻巧,长续航的无人机AI模块,支持视频跟踪
arm开发·人工智能·嵌入式硬件·fpga开发·无人机
我是无敌小恐龙20 分钟前
Java SE 零基础入门 Day05 类与对象核心详解(封装+构造方法+内存+变量)
java·开发语言·人工智能·python·机器学习·计算机视觉·数据挖掘
wheeldown24 分钟前
2026年4月横评三款主流远控软件实况实测:UU远程,Todesk,向日葵,综合性能 UU 远程表现最佳
linux·运维·服务器
~央千澈~25 分钟前
《2026鸿蒙NEXT纯血开发与AI辅助》第五章:选择成熟方案,创建第一个鸿蒙应用并成功运行-卓伊凡
人工智能·华为·harmonyos·harmony·harmony os