Name

getnameinfo - address-to-name translation in protocol-independent manner

Synopsis

#include <sys/types.h> #include <sys/socket.h> #include <netdb.h>
  int
.Fo getnameinfo const struct sockaddr *sa socklen_t salen char *host size_t hostlen char *serv size_t servlen int flags
.Fc

Return values

getnameinfo returns zero on success or one of the error codes listed in gai_strerror if an error occurs.

Detailed description

The getnameinfo function is used to convert a sockaddr structure to a pair of host name and service strings. It is a replacement for and provides more flexibility than the gethostbyaddr and getservbyport functions and is the converse of the getaddrinfo function.

The sockaddr structure sa should point to sockaddr_in (for IPv4) that is salen bytes long.

The host and service names associated with sa are stored in host and serv which have length parameters hostlen and servlen. The maximum value for hostlen is NI_MAXHOST and the maximum value for servlen is NI_MAXSERV, as defined by <netdb.h.> If a length parameter is zero, no string will be stored. Otherwise, enough space must be provided to store the host name or service string plus a byte for the NUL terminator.

The flags argument is formed by OR ’ing the following values:

NI_NOFQDN A fully qualified domain name is not required for local hosts. The local part of the fully qualified domain name is returned instead.
NI_NUMERICHOST Return the address in numeric form, as if calling inet_ntop, instead of a host name.
NI_NAMEREQD A name is required. If the host name cannot be found in DNS and this flag is set, a non-zero error code is returned. If the host name is not found and the flag is not set, the address is returned in numeric form.
NI_NUMERICSERV The service name is returned as a digit string representing the port number.
NI_DGRAM Specifies that the service being looked up is a datagram service, and causes getservbyport to be called with a second argument of "udp" instead of its default of "tcp." This is required for the few ports (512-514) that have different services for UDP and TCP.


Examples

#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
int main()
{
     struct addrinfo *result;
     char hostname[80];
     int error;
     if (error = getaddrinfo("www.yahoo.com",NULL, NULL, &result))
     {
            fprintf(stderr, "error using getaddrinfo: %s\n", gai_strerror(error));
     }
     if (result)
     {
            if (error = getnameinfo(result->ai_addr, sizeof(struct sockaddr), hostname, sizeof(hostname), NULL,0,0))
            {
                   printf( "error using getnameinfo: %s\n", gai_strerror(error));
            }
    }
return 0;
}

         


See also

gai_strerror, getaddrinfo, inet_ntop
Basic Socket Interface Extensions for IPv6
Protocol Independence Using the Sockets API
The getnameinfo function is defined by the -p1003.1g-2000 draft specification and documented in RFC 2553, "Basic Socket Interface Extensions for IPv6."

Caveats

getnameinfo can return both numeric and FQDN forms of the address specified in sa. There is no return value that indicates whether the string returned in host is a result of binary to numeric-text translation (like inet_ntop or is the result of a DNS reverse lookup. Because of this, malicious parties could set up a PTR record as follows:
1.0.0.127.in-addr.arpa. IN PTR  10.1.1.1

         

and trick the caller of getnameinfo into believing that sa is 10.1.1.1 when it is actually 127.0.0.1.

To prevent such attacks, the use of NI_NAMEREQD is recommended when the result of getnameinfo is used for access control purposes:

struct sockaddr *sa;
socklen_t salen;
char addr[NI_MAXHOST];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, salen, addr, sizeof(addr),
    NULL, 0, NI_NAMEREQD);
if (error == 0) {
        memset(&hints, 0, sizeof(hints));
        hints.ai_socktype = SOCK_DGRAM; /*dummy*/
        hints.ai_flags = AI_NUMERICHOST;
        if (getaddrinfo(addr, "0", &hints, &res) == 0) {
                /* malicious PTR record */
                freeaddrinfo(res);
                printf("bogus PTR record\n");
                return -1;
        }
        /* addr is FQDN as a result of PTR lookup */
} else {
        /* addr is numeric string */
        error = getnameinfo(sa, salen, addr, sizeof(addr),
            NULL, 0, NI_NUMERICHOST);
}

         


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top