The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Интерактивная система просмотра системных руководств (man-ов)

 [Cписок руководств | Печать]

getnameinfo (3)
  • getnameinfo (3) ( Solaris man: Библиотечные вызовы )
  • >> getnameinfo (3) ( FreeBSD man: Библиотечные вызовы )
  • getnameinfo (3) ( Русские man: Библиотечные вызовы )
  • getnameinfo (3) ( Linux man: Библиотечные вызовы )
  • getnameinfo (3) ( POSIX man: Библиотечные вызовы )

  • BSD mandoc


     - socket address structure to hostname and service name


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


    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(3) and getservbyport(3) functions and is the converse of the getaddrinfo(3) function.

    If a link-layer address is passed to getnameinfo (,);
    its ASCII representation will be stored in Fa host . The string pointed to by Fa serv will be set to the empty string if non-NULL; Fa flags will always be ignored. This is intended as a replacement for the legacy link_ntoa3 function.

    The sockaddr structure Fa sa should point to either a sockaddr_in sockaddr_in6 or sockaddr_dl structure (for IPv4, IPv6 or link-layer respectively) that is Fa salen bytes long.

    The host and service names associated with Fa sa are stored in Fa host and Fa serv which have length parameters Fa hostlen and Fa servlen . The maximum value for Fa hostlen is NI_MAXHOST and the maximum value for Fa servlen is NI_MAXSERV as defined by Aq Pa 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 Fa flags argument is formed by OR 'ing the following values:

    A fully qualified domain name is not required for local hosts. The local part of the fully qualified domain name is returned instead.
    Return the address in numeric form, as if calling inet_ntop3, instead of a host name.
    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.
    The service name is returned as a digit string representing the port number.
    Specifies that the service being looked up is a datagram service, and causes getservbyport(3) 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

    This implementation allows numeric IPv6 address notation with scope identifier, as documented in chapter 11 of draft-ietf-ipv6-scoping-arch-02.txt. IPv6 link-local address will appear as a string like ``fe80::1%ne0 '' Refer to getaddrinfo(3) for more information.  


    getnameinfo ();
    returns zero on success or one of the error codes listed in gai_strerror3 if an error occurs.  


    The following code tries to get a numeric host name, and service name, for a given socket address. Observe that there is no hardcoded reference to a particular address family.
    struct sockaddr *sa;    /* input */
    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
    if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
        sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
            errx(1, "could not get numeric hostname");
    printf("host=%s, serv=%s\n", hbuf, sbuf);

    The following version checks if the socket address has a reverse address mapping:

    struct sockaddr *sa;    /* input */
    char hbuf[NI_MAXHOST];
    if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
        NI_NAMEREQD)) {
            errx(1, "could not resolve hostname");
    printf("host=%s\n", hbuf);


    gai_strerror3, getaddrinfo(3), gethostbyaddr(3), getservbyport(3), inet_ntop3, link_ntoa3, resolver(3), hosts(5), resolv.conf5, services(5), hostname(7), named(8)
    R. Gilligan S. Thomson J. Bound W. Stevens Basic Socket Interface Extensions for IPv6 RFC 2553 March 1999
    S. Deering B. Haberman T. Jinmei E. Nordmark B. Zill "IPv6 Scoped Address Architecture" internet draft draft-ietf-ipv6-scoping-arch-02.txt work in progress material
    Craig Metz Protocol Independence Using the Sockets API "Proceedings of the freenix track: 2000 USENIX annual technical conference" June 2000


    The getnameinfo ();
    function is defined by the St -p1003.1g-2000 draft specification and documented in RFC 2553 ``Basic Socket Interface Extensions for IPv6''  


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

    and trick the caller of getnameinfo ();
    into believing that Fa sa is when it is actually

    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 */
                    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);




    Поиск по тексту MAN-ов: 

    Inferno Solutions
    Hosting by

    Закладки на сайте
    Проследить за страницей
    Created 1996-2024 by Maxim Chirkov
    Добавить, Поддержать, Вебмастеру