This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug nss/25222] New: getaddrinfo("localhost") returns 2x

            Bug ID: 25222
           Summary: getaddrinfo("localhost") returns 2x
           Product: glibc
           Version: 2.31
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: nss
          Assignee: unassigned at sourceware dot org
          Reporter: tim.ruehsen at gmx dot de
  Target Milestone: ---

Created attachment 12090
[getaddrinfo] ignore ::1 in /etc/hosts if asked for AF_INET localhost

Given that /etc/hosts contains ::1 and for localhost, getaddrinfo()
returns two identical entries, even if the hints are set to AF_INET.

Example /etc/hosts  localhost localhost4
::1        localhost localhost6

The code for this was introduced in 2007 by commit 11bf311edc7.

There is an explicit code branch executed if AF_INET has been requested and a
IPv6 address has been found (makes sense with V4MAPPED). But if IPv4 mapping is
not used and the IP is ::1, the code simply adds an entry with This
creates the second entry mentioned above.

Removing the code (attached patch), the glibc test suite results do not change

C example code
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>

int main(void)
        struct addrinfo hints = {
                .ai_family = AF_INET,
                .ai_socktype = SOCK_STREAM,
                .ai_flags = AI_ADDRCONFIG
        const char *host = "localhost";
        struct addrinfo *addrinfo;
        int rc;

        if ((rc = getaddrinfo(host, NULL, &hints, &addrinfo)) == 0) {
                for (struct addrinfo *ai = addrinfo; ai; ai = ai->ai_next) {
                        char adr[NI_MAXHOST];
                        int rc;

                        if ((rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, adr,
sizeof(adr), NULL, 0, NI_NUMERICHOST)) == 0)
                                printf("%s\n", adr);
                                printf("??? (%s)\n", gai_strerror(rc));
        } else
                printf("rc = %d %s, errno %d\n", rc, gai_strerror(rc), errno);

        return 0;

You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]