C:getaddrinfo
lastmod 2020-03-31created 2019-09-093 edits
Example
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int main(int argc,char *argv[])
{
int status;
struct addrinfo hints;
struct addrinfo *servinfo; // 결과를 저장할 변수
memset(&hints, 0, sizeof(hints)); // hints 구조체의 모든 값을 0으로 초기화
hints.ai_family = AF_UNSPEC; // IPv4와 IPv6 상관하지 않고 결과를 모두 받겠다
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
status = getaddrinfo("www.google.com", "80", &hints, &servinfo);
return 0;
}
Result
The getaddrinfo function dynamically allocates the memory for all the structures that result points to. The caller of the getaddrinfo function uses those structures and is responsible for freeing the memory. To free the memory of the addrinfo list, you must call the freeaddrinfo function.
FILES
-
/etc/hosts -
/etc/host.conf -
/etc/resolv.conf -
/etc/gai.conf
Return value
This function returns 0 on success. On failure it returns a nonzero value, one of the error values below.
- EAI_ADDRFAMILY
- The specified network host has no addresses in the requested address family. For example, if you ask a network host that has only IPv4 addresses to "give me an IPv6 address," this error is returned.
- EAI_AGAIN
- The nameserver returned a temporary failure indication. It means to try again after some time.
- EAI_BADFLAGS
- hints.ai_flags contains invalid flags. Or hints.ai_flags included AI_CANONNAME and the name was NULL.
- EAI_FAIL
- The nameserver returned a permanent failure indication. Unlike EAI_AGAIN, it means that retrying will fail too.
- EAI_FAMILY
- The requested address family is not supported.
- EAI_MEMORY
- Not enough memory to perform getaddrinfo. This is pretty much a hopeless situation. Wait, hoping the kernel's OOM killer does its job, retry after some time, and if you still get the same error, just reboot and be done with it.
- EAI_NODATA
- The specified network host exists... but has no network addresses defined.
- EAI_NONAME
- The node or service is not known. Or both node and service are NULL. Or AI_NUMERICSERV was specified in hints.ai_flags, but service was not a numeric string representing a port number. In any case, it means the input parameters were wrong. Did you pass the parameters correctly? Check your own code again.
- EAI_SERVICE
- The requested service is not available for the requested socket type. It may be available through another socket type. For example, this error can occur if service is "shell" (a service available only on stream sockets) and hints.ai_protocol is IPPROTO_UDP or hints.ai_socktype is SOCK_DGRAM. Or the error can occur if service is not NULL and hints.ai_socktype is SOCK_RAW (a socket type that does not support the concept of services).
- EAI_SOCKTYPE
- The requested socket type is not supported. For example, when hints.ai_socktype and hints.ai_protocol are inconsistent. This error can occur if you pass as the input parameter a hint with SOCK_DGRAM in ai_socktype and IPPROTO_TCP in ai_protocol.
- EAI_SYSTEM
- Some other system error not covered by any of the above, a troublesome case. You need to check errno for details.
To interpret the return value:
This function converts the error code returned by getaddrinfo( ) into a human-readable string. Useful for debugging and log output, so keep it in mind.
Troubleshooting
EAI_SERVICE
The requested service is not available for the requested socket type. Service names are things like https, smtp, and ftp. The code that looks this up can be found here. If you check where the servname variable is used, you can see that partway through it looks it up using getservbyname.
This function checks the contents of the /etc/services file. If this file does not exist, you need to create it. On Debian, you can add it with the command below.