Slove
Slove

Reputation: 19

Displaying IPv6 addresses

I am writting a program to display local IP address of the machine. I am getting able to display IPv4 address, while getting unable to display IPv6 address. Below is the program that i am using to display IPv4 address:

#include <iostream.h>
#include <winsock.h>

int doit(int, char **)
{
    char ac[80];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        cerr << "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
    cout << "Host name is " << ac << "." << endl;

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Yow! Bad host lookup." << endl;
        return 1;
    }

    for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
    }

    return 0;
}

int main(int argc, char *argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }

    int retval = doit(argc, argv);

    WSACleanup();

    return retval;
}

Upvotes: 1

Views: 648

Answers (2)

Erik Alap&#228;&#228;
Erik Alap&#228;&#228;

Reputation: 2703

As previous answer stated, use getaddrinfo(). Also, since you are using C++, make sure you use RAII for the cleanup, so cleanup will be done even if exceptions are thrown. Here is an example:

if ( (n = getaddrinfo(host.c_str(), service.c_str(), &hints, &res)) != 0) {
    ostringstream ss;

    ss << "getaddrinfo error for " << host << ", " << service
       << ", " << gai_strerror(n);

    throw std::runtime_error(ss.str());
}

// Make sure freeaddrinfo is called even if exceptions are thrown.                                                                                                                                                                       
// Note that we do not need to check res for NULL, unique_ptr handles that                                                                                                                                                               
// when deallocating.                                                                                                                                                                                                                    
auto cleanup = [](addrinfo* ai) { freeaddrinfo(ai); };
unique_ptr<addrinfo, decltype(cleanup)> aip(res, cleanup);

Upvotes: 1

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

gethostbyname is obsolete and on many systems ignores IPv6 entries.

Use modern getaddrinfo function and check ai_family member for AI_INET or AI_INET6 in order to identify address type.

Upvotes: 1

Related Questions