[C Language] The function getaddrinfo() that supports IPv6 address query

Introduction: In C language hardware development, use the function getaddrinfo() that supports IPv6 address query instead of gethostbyname()
The function of the address query function is to return detailed host information through the host name or domain name. Among them, the most commonly used function is to obtain the host's IP address and other information through the host name.
To access the hardware of the Alibaba Cloud IoT platform, the IoT access endpoint must first be analyzed, and each product access domain name is different. The rules are:

getaddrinfo() function
The new API of getaddrinfo() introduced in IPv6 is protocol-independent and can be used for both IPv4 and IPv6. The getaddrinfo() function can handle both name-to-address and service-to-port conversions, and returns an addrinfo structure (list) pointer instead of an address list.

It is strongly recommended that you use the getaddrinfo() function to replace the outdated IPv4-only gethostbyname()

function prototype
Get IP address by hostname or servicename
head File:,

int getaddrinfo(const char *restrict host,
const char *restrict service,
const struct addrinfo *restrict hints,
struct addrinfo **restrict result);

Parameter Description:

host: a host name or address string (dotted decimal string for IPv4 or hexadecimal string for IPv6)
service: The service name can be a decimal port number or a defined service name, such as ftp, http, etc.
Hints: It can be a null pointer or a pointer to an addrinfo structure in which the caller fills in hints about the type of information expected to be returned. For example: the specified service can support both TCP and UDP, so the caller can set the ai_socktype member in the hints structure to SOCK_DGRAM so that only the information applicable to the datagram socket is returned.
result: This function returns a pointer to the addrinfo structure linked list through the result pointer parameter.
return value:

If successful, return 0; if error, return a non-zero error code
addrinfo structure:

struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
socklen_t ai_addrlen; //address length of ai_addr
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next; //point to the next node of the linked list
Use reference:

struct addrinfo *ai, *aip;
struct addrinfo hint;
struct sockaddr_in *sinp;
const char *addr;
int err;
char buf[1024];

hint.ai_flags = AI_CANONNAME;
hint.ai_family = 0;
hint.ai_socktype = 0;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL;

if((err = getaddrinfo("aliyun.com", NULL, &hint, &ai)) != 0)
printf("ERROR: getaddrinfo error: %s ", gai_strerror(err));

for(aip = ai; aip != NULL; aip = aip->ai_next)
printf(" ");
printf("Canonical Name: %s ", aip->ai_canonname);
if(aip->ai_family == AF_INET)
sinp = (struct sockaddr_in *)aip->ai_addr;
addr = inet_ntop(AF_INET, &sinp->sin_addr, buf, sizeof buf);
printf("IP Address: %s ", addr);
printf("Port: %d ", ntohs(sinp->sin_port));
printf(" ");

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us