Client in C++, usa gethostbyname o getaddrinfo

Client in C++, usa gethostbyname o getaddrinfo

Il gethostbyname() e gethostbyaddr() le funzioni sono deprecate sulla maggior parte delle piattaforme e non implementano il supporto per IPv6. IPv4 ha raggiunto i suoi limiti, il mondo è passato a IPv6 da un po' di tempo ormai. Usa getaddrinfo() e getnameinfo() invece, rispettivamente.

Per rispondere alle tue domande:

R. getaddrinfo() e getnameinfo() può essere utilizzato sia per client che per server, proprio come gethostbyname() e gethostbyaddr() può essere. Sono solo funzioni di risoluzione di host/indirizzi, come vengono utilizzati i valori risolti spetta all'app chiamante decidere.

B. codice cliente utilizzando getaddrinfo() sarebbe simile a questo:

int OpenConnection(const char *hostname, int port)
{
    int sd, err;
    struct addrinfo hints = {}, *addrs;
    char port_str[16] = {};

    hints.ai_family = AF_INET; // Since your original code was using sockaddr_in and
                               // PF_INET, I'm using AF_INET here to match.  Use
                               // AF_UNSPEC instead if you want to allow getaddrinfo()
                               // to find both IPv4 and IPv6 addresses for the hostname.
                               // Just make sure the rest of your code is equally family-
                               // agnostic when dealing with the IP addresses associated
                               // with this connection. For instance, make sure any uses
                               // of sockaddr_in are changed to sockaddr_storage,
                               // and pay attention to its ss_family field, etc...
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    sprintf(port_str, "%d", port);

    err = getaddrinfo(hostname, port_str, &hints, &addrs);
    if (err != 0)
    {
        fprintf(stderr, "%s: %s\n", hostname, gai_strerror(err));
        abort();
    }

    for(struct addrinfo *addr = addrs; addr != NULL; addr = addr->ai_next)
    {
        sd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (sd == -1)
        {
            err = errno;
            break; // if using AF_UNSPEC above instead of AF_INET/6 specifically,
                   // replace this 'break' with 'continue' instead, as the 'ai_family'
                   // may be different on the next iteration...
        }

        if (connect(sd, addr->ai_addr, addr->ai_addrlen) == 0)
            break;

        err = errno;

        close(sd);
        sd = -1;
    }

    freeaddrinfo(addrs);

    if (sd == -1)
    {
        fprintf(stderr, "%s: %s\n", hostname, strerror(err));
        abort();
    }

    return sd;
}

Ho sempre usato gethostbyname() da "per sempre". Ha sempre funzionato, continua a funzionare ed è "più semplice".

getaddrinfo() è la funzione più recente:

Capisco che getaddrinfo() è più robusto, più efficiente e più sicuro:non dovresti comunque usare gethostbyname()

APPENDICE:

In risposta alle tue domande specifiche:

A] getaddrinfo() è preferito a gethostbyname() per cercare l'indirizzo IP di un hostname; "client" o "server".

B] D:Come posso modificare la struttura degli hint ei parametri della funzione?

A:I "suggerimenti" sembrano OK, ma probabilmente modificherei la porta su NULL.

Ecco un esempio completo:

https://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];

    if (argc != 2) {
       fprintf(stderr, "Usage: %s hostname\n", argv[0]);
       return 1;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;

    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
        return 2;
    }

    for(p = res;p != NULL; p = p->ai_next) {
        void *addr;
        if (p->ai_family == AF_INET) {
            return 1;  
        } else {
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
            addr = &(ipv6->sin6_addr);

            /* convert the IP to a string and print it: */
            inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);             
            printf("Hostname: %s\n", argv[1]);
            printf("IP Address: %s\n", ipstr);
        }
    }

    freeaddrinfo(res); // free the linked list     
    return 0;
}