Main index | Section 3 | Options |
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
The type and usage of the return value, struct hostent is described in gethostbyname(3).
For getipnodebyname(), the name argument can be either a node name or a numeric address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). The af argument specifies the address family, either AF_INET or AF_INET6. The flags argument specifies the types of addresses that are searched for, and the types of addresses that are returned. We note that a special flags value of AI_DEFAULT (defined below) should handle most applications. That is, porting simple applications to use IPv6 replaces the call
hptr = gethostbyname(name);
with
hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num);
Applications desiring finer control over the types of addresses searched for and returned, can specify other combinations of the flags argument.
A flags of 0 implies a strict interpretation of the af argument:
Other constants can be logically-ORed into the flags argument, to modify the behavior of the function.
For example, if the node has no IPv6 source addresses configured, and af equals AF_INET6, and the node name being looked up has both AAAA and A records, then: (a) if only AI_ADDRCONFIG is specified, the function returns a NULL pointer; (b) if AI_ADDRCONFIG | AI_V4MAPPED is specified, the A records are returned as IPv4-mapped IPv6 addresses;
The special flags value of AI_DEFAULT is defined as
#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
We noted that the getipnodebyname() function must allow the name argument to be either a node name or a literal address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). This saves applications from having to call inet_pton(3) to handle literal address strings. When the name argument is a literal address string, the flags argument is always ignored.
There are four scenarios based on the type of literal address string and the value of the af argument. The two simple cases are when name is a dotted-decimal IPv4 address and af equals AF_INET, or when name is an IPv6 hex address and af equals AF_INET6. The members of the returned hostent structure are: h_name points to a copy of the name argument, h_aliases is a NULL pointer, h_addrtype is a copy of the af argument, h_length is either 4 (for AF_INET) or 16 (for AF_INET6), h_addr_list[0] is a pointer to the 4-byte or 16-byte binary address, and h_addr_list[1] is a NULL pointer.
When name is a dotted-decimal IPv4 address and af equals AF_INET6, and AI_V4MAPPED is specified, an IPv4-mapped IPv6 address is returned: h_name points to an IPv6 hex address containing the IPv4-mapped IPv6 address, h_aliases is a NULL pointer, h_addrtype is AF_INET6, h_length is 16, h_addr_list[0] is a pointer to the 16-byte binary address, and h_addr_list[1] is a NULL pointer.
It is an error when name is an IPv6 hex address and af equals AF_INET. The function's return value is a NULL pointer and the value pointed to by error_num equals HOST_NOT_FOUND.
The getipnodebyaddr() function takes almost the same argument as gethostbyaddr(3), but adds a pointer to return an error number. Additionally it takes care of IPv4-mapped IPv6 addresses, and IPv4-compatible IPv6 addresses.
The getipnodebyname() and getipnodebyaddr() functions dynamically allocate the structure to be returned to the caller. The freehostent() function reclaims memory region allocated and returned by getipnodebyname() or getipnodebyaddr().
/etc/hosts
/etc/nsswitch.conf /etc/resolv.conf | |
RFC2553, Basic Socket Interface Extensions for IPv6, March 1999.
, , , ,The text was shamelessly copied from RFC2553.
GETIPNODEBYNAME (3) | June 27, 2022 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | There are two major products of Berkeley, CA -- LSD and UNIX. We don't believe this to be strictly by coincidence. | ” |
— Jeremy S. Anderson |