A pointer to an ANSI string used to hold the host name. On success, the host name is returned as a Fully Qualified Domain Name (FQDN) by default. If the host parameter is NULL, this indicates the caller does not want to receive a host name string.
A pointer to an ANSI string to hold the service name. On success, an ANSI string that represents the service name associated with the port number is returned. If the serv parameter is NULL, this indicates the caller does not want to receive a service name string.
Nonzero error codes returned by thegetnameinfo function also map to the set of errors outlined by Internet Engineering Task Force (IETF) recommendations. The following table lists these error codes and their WSA equivalents. It is recommended that the WSA error codes be used, as they offer familiar and comprehensive error information for Winsock programmers.
Use thegai_strerror function to print error messages based on the EAI codes returned by thegetnameinfo function. Thegai_strerror function is provided for compliance with IETF recommendations, but it is not thread safe. Therefore, use of traditional Windows Sockets functions such asWSAGetLastError is recommended.
The getnameinfo function is the ANSI version of a function that provides protocol-independent name resolution. The getnameinfo function is used to translate the contents of a socket address structure to a node name and/or a service name.
For IPv6 and IPv4 protocols, Name resolution can be by the Domain Name System (DNS), a local hosts file, or by other naming mechanisms. This function can be used to determine the host name for an IPv4 or IPv6 address, a reverse DNS lookup, or determine the service name for a port number. The getnameinfo function can also be used to convert an IP address or a port number in a sockaddr structure to an ANSI string. This function can also be used to determine the IP address for a host name.
To simplify determining buffer requirements for the host and serv parameters, the following values for maximum host name length and maximum service name are defined in the Ws2tcpip.h header file.
Setting the NI_NUMERICSERV flag returns the port number of the service instead of its name. Also, if a host name is not found for an IP address (127.0.0.2, for example), the hostname is returned as the IP address.
On Windows Vista and later, if NI_NUMERICSERV is not specified in the flags parameter and the port number contained in sockaddr structure pointed to by the sa parameter does not resolve to a well known service, the getnameinfo function returns the numeric form of theservice address (the port number) as a numeric string. When NI_NUMERICSERV is specified, the port number is returned as a numeric string. This behavior is specified in section 6.2 of RFC 3493. For more information, see www.ietf.org/rfc3493.txt
The IPv6 protocol is supported on Windows 2000 when the IPv6 Technology Preview for Windows 2000 is installed. Otherwise getnameinfo support on versions of Windows earlier than Windows XP is limited to handling IPv4 name resolution.
The newer functions can do more: they handle IPv6 and can translate strings like 'http' to 80 (port). In the future they can also determine if e.g. TCP should be used for the service in question or SCTP. The interface is ready.
Edit #2 I've noticed you are trying to use the return value of getnameinfo as hostname. But that is not correct, the hostname is saved within the provided host pointer. The return value indicates whether the operation was sufficient. Also have a look at the man page.
Ie, when getnameinfo was trying to look up the hostname corresponding to an IP address or the service name corresponding to a port number something failed in an unexpected manner.
(By default netstat shows names for both addresses and ports, also see the -n option)
In my C program, I am using getaddrinfo to get IP address from a hostname, and getnameinfo to get hostname from IP address. These two functions are complementary. I read in man getaddrinfo that I have to use freeaddrinfo() function to free up the memory that was allocated for the dynamically allocated linked list res. Thus my code looks like this:
getaddrinfo and getnameinfo are inverse functions of each other. They are network protocol agnostic, and support both IPv4 and IPv6. It is the recommended interface for name resolution in building protocol independent applications and for transitioning legacy IPv4 code to the IPv6 Internet.
Internally, the functions may use a variety of resolution methods not limited to the Domain Name System (DNS). The Name Service Switch is commonly used on Unix-like systems and affects most implementation of this pair as it did with their BSD-socket era predecessors.[2]
getaddrinfo() converts human-readable text strings representing hostnames or IP addresses into a dynamically allocated linked list of struct addrinfo structures.The function prototype for this function is specified as follows:
The function getnameinfo() converts the internal binary representation of an IP address in the form of a pointer to a struct sockaddr into text strings consisting of the hostname or, if the address cannot be resolved into a name, a textual IP address representation, as well as the service port name or number. The function prototype is specified as follows:
The following example uses getaddrinfo() to resolve the domain name www.example.com into its list of addresses and then calls getnameinfo() on each result to return the canonical name for the address. In general, this produces the original hostname, unless the particular address has multiple names, in which case the canonical name is returned. In this example, the domain name is printed three times, once for each of the three results obtained.
The address format required by a particular socket object is automaticallyselected based on the address family specified when the socket object wascreated. Socket addresses are represented as follows:
A pair (host, port) is used for the AF_INET address family,where host is a string representing either a hostname in internet domainnotation like 'daring.cwi.nl' or an IPv4 address like '100.50.200.5',and port is an integer.
For IPv4 addresses, two special forms are accepted instead of a hostaddress: '' represents INADDR_ANY, which is used to bind to allinterfaces, and the string '' representsINADDR_BROADCAST. This behavior is not compatible with IPv6,therefore, you may want to avoid these if you intend to support IPv6 with yourPython programs.
For AF_INET6 address family, a four-tuple (host, port, flowinfo,scope_id) is used, where flowinfo and scope_id represent the sin6_flowinfoand sin6_scope_id members in struct sockaddr_in6 in C. Forsocket module methods, flowinfo and scope_id can be omitted just forbackward compatibility. Note, however, omission of scope_id can cause problemsin manipulating scoped IPv6 addresses.
Linux-only support for TIPC is available using the AF_TIPCaddress family. TIPC is an open, non-IP based networked protocol designedfor use in clustered computer environments. Addresses are represented by atuple, and the fields depend on the address type. The general tuple form is(addr_type, v1, v2, v3 [, scope]), where:
A tuple (interface, ) is used for the AF_CAN address family,where interface is a string representing a network interface name like'can0'. The network interface name '' can be used to receive packetsfrom all network interfaces of this family.
CAN_J1939 protocol require a tuple (interface, name, pgn, addr)where additional parameters are 64-bit unsigned integer representing theECU name, a 32-bit unsigned integer representing the Parameter Group Number(PGN), and an 8-bit integer representing the address.
A string or a tuple (id, unit) is used for the SYSPROTO_CONTROLprotocol of the PF_SYSTEM family. The string is the name of akernel control using a dynamically assigned ID. The tuple can be used if IDand unit number of the kernel control are known or if a registered ID isused.
BTPROTO_HCI accepts (device_id,) where device_id iseither an integer or a string with the Bluetooth address of theinterface. (This depends on your OS; NetBSD and DragonFlyBSD expecta Bluetooth address while everything else expects an integer.)
AF_QIPCRTR is a Linux-only socket based interface for communicatingwith services running on co-processors in Qualcomm platforms. The addressfamily is represented as a (node, port) tuple where the node and portare non-negative integers.
IPPROTO_UDPLITE is a variant of UDP which allows you to specifywhat portion of a packet is covered with the checksum. It adds two socketoptions that you can change.self.setsockopt(IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, length) willchange what portion of outgoing packets are covered by the checksum andself.setsockopt(IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, length) willfilter out packets which cover too little of their data. In both caseslength should be in range(8, 2**16, 8).
AF_HYPERV is a Windows-only socket based interface for communicatingwith Hyper-V hosts and guests. The address family is represented as a(vm_id, service_id) tuple where the vm_id and service_id areUUID strings.
If you use a hostname in the host portion of IPv4/v6 socket address, theprogram may show a nondeterministic behavior, as Python uses the first addressreturned from the DNS resolution. The socket address will be resolveddifferently into an actual IPv4/v6 address, depending on the results from DNSresolution and/or the host configuration. For deterministic behavior use anumeric address in host portion.
All errors raise exceptions. The normal exceptions for invalid argument typesand out-of-memory conditions can be raised. Errorsrelated to socket or address semantics raise OSError or one of itssubclasses.
A subclass of OSError, this exception is raised foraddress-related errors, i.e. for functions that use h_errno in the POSIXC API, including gethostbyname_ex() and gethostbyaddr().The accompanying value is a pair (h_errno, string) representing anerror returned by a library call. h_errno is a numeric value, whilestring represents the description of h_errno, as returned by thehstrerror() C function.