Getaddirnfo is a thread-safe function. Moreover, it uses DNS protocol to connect with the name servers configured with the IP address. Getaddrinfo() function is known to call as the call combinatory because this function alone calls more than 100 system calls. This function is also said to be a blocking call as there exists no way to specify a time for returning the value easily, so it eventually blocks the call.
constant character *servname,
constant structure addrinfo *hints,
structure addrinfo **res);
Servname: It is the server name. A non-NULL “servname” can be a server name or a port number in the decimal notation.
Hints: It is a pointer that points towards the “addrinfo” structure because it provides hints with the sockets you are supporting.
Res: It is the location’s address where the function stores a pointer with a linked list of more than one “addrinfo” structure.
General Description regarding Getaddrinfo
The “servername” and the “nodename” arguments are the pointers to the null-terminated strings or the null pointers. Both or one of them should be considered as a non-null string pointer. The valid name format depends on the family of the protocol. When no errors detection occurs, all families are specified then all successful results will be returned.
Now talking about the null-termination of “nodesname” and the “servername.” If the “servername” is null, all the calls are returned with the network addresses for the specific “nodename.” If the server name is not null, it is considered a null-terminated character string that requests the service. It is either a descriptive or a numeric representation suitable for families.
On the other hand, they are talking about the hints argument. It shows the structure containing the input value that directs the operations and provides the options by keeping the information returned in some limit to a respective socket type and protocol. If a value is zero for socket type, the caller can accept any socket type. Similarly, if the returned value is zero for the protocol, the caller will accept any protocol like the socket.
The “addrinfo” structure of hints argument accepts different types of sockets
- If it accepts any protocol family, then the family is ai_family.
- If it accepts any socket type, it uses the family of ai_socktype.
- If it accepts any protocol, then it uses the ai_protocol.
- To accept all the arguments with the ai_flags settled to zero, it uses the hints family.
Each time the function returns a value, it contains the main three arguments parameter: ai_family, ai_socktype, and ai_protocol. After calling the function, we get these arguments. In each addrinfo structure, a filled-in socket structure is pointed by ai_addr, where the length of the socket address is identified and specified by ai_addrlen member.
In the case of failure of the function, getaddrinfo() returns a non-zero error code. There are many error codes like EAI_FAIL, EAI_FAMILY, etc.
Implementation of getaddrinfo() function
We have used the Linux operating system. Write the codes in the text editor and then execute the source code file on the Ubuntu terminal.
This example uses the getaddrinfo() function to resolve the issue of domain name www.sample.com into the address list. After that, we call getnameinfo() to return the name to the address. The function will create an original hostname unless multiple names are allotted to the specific address. We have printed the domain name more than twice in an example. For each time, the same result is obtained.
This example will not use the structures. Directly the main program is entertained by the function. In the main program, after initializing libraries, we have used two functional variables for the resultant values. If an error is not equal to zero, it means an error has occurred, then notify the “errno.”
After that, we will take the hostname through the host information, and address length is also taken. If an error occurs again, an error is identified; on the other hand, the hostname is printed.
Compile the result with the compiler’s help and execute it on the terminal. The compiler used here is a GCC compiler. ‘file1.c’ is the name of a file. You can see that the host number is displayed three times.
All the libraries regarding sockets will be used here. Inside the function, we will describe the structure having all the arguments information with the data types. Hints will describe all the sockets, family, and “soctype.” After this, we applied a check like the first example; if the error is non-zero, it will be rectified. And if the resultant of the “getaddeinfo” is other than 0. So the hostname is targeted and will be displayed.
We have used a while loop with a switch statement to consider each case, but the statement is terminated when the desired one is reached. “Sockaddr” will check each IP by using AF_INET for IP4 and AF_INET6 for IPv6. The pointer will point to the address used here. Inet_ntop() function is used here that is mainly used to convert the IP address of the numeric and binary string into a text string of the address that is very easily readable. Then close the function.
Inside the main program, a do-while loop is used, as this part involves the user interaction. So unless the correct domain is entered, it keeps displaying the message. The buffer is allotted with the length to enter the number. A “strlen” function is used to measure the length. If the length is short, the error is sent, and if it is greater than 0, the input is saved in the buffer.
Execute and compile the code then you will see that the system first asks for the domain name; if the name is invalid, it displays the invalid argument message. If it is unavailable, it asks again to re-enter the name; this process continues until you enter the right domain name.
The article “C: getaddrinfo function usage’” shows the use of this function along with the arguments it has that have their functionality in each aspect of address taking. The getaddrinfo deals mainly with the domain name currently available. This article showed the example and work of getaddrinfo in the Linux operating system.