handora
handora

Reputation: 659

Can gethostbyname() return an IPv6 address?

I'm reading the UNIX NETWORK PROGRAMMING, and come up with a question about the exercise 11.4 to support the code with gethostbyname for IPv6. The answer in the book is as below:

#include    "unp.h"

int
main(int argc, char **argv)
{
    int                 sockfd, n;
    char                recvline[MAXLINE + 1];
    struct sockaddr_in  servaddr;
    struct sockaddr_in6 servaddr6;
    struct sockaddr     *sa;
    socklen_t           salen;
    struct in_addr      **pptr;
    struct hostent      *hp;
    struct servent      *sp;

    if (argc != 3)
        err_quit("usage: daytimetcpcli3 <hostname> <service>");

    if ( (hp = gethostbyname(argv[1])) == NULL)
        err_quit("hostname error for %s: %s", argv[1], hstrerror(h_errno));

    if ( (sp = getservbyname(argv[2], "tcp")) == NULL)
        err_quit("getservbyname error for %s", argv[2]);

    pptr = (struct in_addr **) hp->h_addr_list;
    for ( ; *pptr != NULL; pptr++) {
        sockfd = Socket(hp->h_addrtype, SOCK_STREAM, 0);

        if (hp->h_addrtype == AF_INET) {
            sa = (SA *) &servaddr;
            salen = sizeof(servaddr);
        } else if (hp->h_addrtype == AF_INET6) {
            sa = (SA *) &servaddr6;
            salen = sizeof(servaddr6);
        } else
            err_quit("unknown addrtype %d", hp->h_addrtype);

        bzero(sa, salen);
        sa->sa_family = hp->h_addrtype;
        sock_set_port(sa, salen, sp->s_port);
        sock_set_addr(sa, salen, *pptr);

        printf("trying %s\n", Sock_ntop(sa, salen));

        if (connect(sockfd, sa, salen) == 0)
            break;      /* success */
        err_ret("connect error");
        close(sockfd);
    }
    if (*pptr == NULL)
        err_quit("unable to connect");

    while ( (n = Read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = 0;    /* null terminate */
        Fputs(recvline, stdout);
    }
    exit(0);
}

With information on book and other resource, i found that the gethostbyname can't return IPv6. So my question is whether gethostbyname can return information about IPV6, or just the code is wrong?

I think this question is not about to replace the gethostbyname, i want to know the correctness of this code.

Upvotes: 3

Views: 4050

Answers (2)

Jeff Silverman
Jeff Silverman

Reputation: 724

You want the getaddrinfo function.

Upvotes: 0

alk
alk

Reputation: 70981

gethostbyname() is not part of (the current) POSIX (IEEE Std 1003.1-2008, 2016 Edition) any more.

It explicitly mentions:

The obsolescent h_errno external integer, and the obsolescent gethostbyaddr() and gethostbyname() functions are removed, along with the HOST_NOT_FOUND, NO_DATA, NO_RECOVERY, and TRY_AGAIN macros.


The last POSIX version supporting gethostbyname() was IEEE Std 1003.1, 2004 Edition, which states:

struct hostent *gethostbyname(const char *name);

[...]

The gethostbyname() function shall return an entry containing addresses of address family AF_INET for the host with name name.

It does not mention AF_INET6 at all.


So from the POSIX perspective your question about

the correctness of this code.

can be answered as:

  • Yes, it use to be correct, relying on an implementation defined/specific extension (namely support for AF_INET6).
  • No, as of now it is not correct any more, as gethostbyname() had been removed from POSIX.

Upvotes: 3

Related Questions