Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use getaddrinfo()?

Im trying to make a simple program that takes in a string like www.google.com and returns the ip address...

What i have so far:

char* hostname = new char[www.size()+1];
std::copy(www.begin(), www.end(), hostname);
hostname[www.size()] = '\0';

struct addrinfo new_addr, *res;

getaddrinfo(www.c_str(), SERVICE.c_str(), &new_addr, &res);



cout << new_addr.ai_addr;

What are the 3rd of 4th parameters supposed to do? Does the getaddrinfo function modify the new_addr structure or what? I dont really understand the msdn documentation. After the hostname is resolved I want to connect a socket to it.

like image 680
Kelvin Avatar asked Oct 21 '22 13:10

Kelvin


2 Answers

What if i leave the third parameter nullified?

Heres the code i developed so far.

    char* hostname = new char[www.size()+1];
copy(www.begin(), www.end(), hostname);
hostname[www.size()] = '\0';

struct addrinfo *res;
struct in_addr addr;

getaddrinfo(hostname, NULL, 0, &res);

addr.S_un = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.S_un;

server.sin_addr.s_addr = inet_addr(inet_ntoa(addr));
server.sin_port = htons(portno);

freeaddrinfo(res);
delete []hostname;

server.sin is declared elsewhere that i use to fill a socket in another method of my sockets class.

like image 71
Kelvin Avatar answered Oct 25 '22 19:10

Kelvin


The MSDN documentation is very detailed and explains exactly what the various parameters are for. The third parameter lets you specify the type of socket that will be used with the results of the lookup. This allies the results to be optimized as needed. The fourth parameter returns the actual results. The documentation also contains a full example of how to use the function. So what example is unclear about what the documentation says?

Try this:

struct addrinfo hints = {0};
hints.ai_flags = 0;
hints.ai_family = AF_UNSPEC; // IPv4 and IPv6 allowed
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

struct addrinfo *res = NULL;

if (getaddrinfo(www.c_str(), SERVICE.c_str(), &hints, &res) == 0)
{
    TCHAR szIPAddr[64];
    DWORD szIPAddrLen;
    SOCKET skt;

    struct addrinfo *addr = res;
    do
    {
        skt = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (skt == INVALID_SOCKET)
            cout << "Unable to create socket, error " << WSAGetLastError() << endl;
        else
        {
            szIPAddrLen = 64;
            WSAAddressToString(addr->ai_addr, addr->ai_addrlen, NULL, szIPAddr, &szIPAddrLen);

            cout << "Connecting to " << szIPAddr << " ..." << endl;

            if (connect(skt, addr->ai_addr, addr->ai_addrlen) == 0)
            {
                cout << "Connected!" << endl;
                break;
            }

            cout << "Unable to connect, error " << WSAGetLastError() << endl;
            closesocket(skt);
            skt = INVALID_SOCKET;
        }

        addr = addr->ai_next;
    }
    while (addr);

    freeaddrinfo(res);

    if (skt != INVALID_SOCKET)
    {
        // use skt as needed...
        closesocket(skt);
    }
}
like image 36
Remy Lebeau Avatar answered Oct 25 '22 17:10

Remy Lebeau