The order of the IP addresses in the gethostbyname function

Since I did not have an answer to my previous question, I will rephrase it.

What order of IP addresses (if IP addresses are bound to the same interface) is used when running gethostbyname () using the computer name (NetBIOS name)?

I have this code:

#include <iostream>
#include <winsock.h>
#pragma comment(lib, "Ws2_32.lib")

int main()
{
    char hostname[255];
    struct hostent *he;
    struct in_addr **addr_list;

    WSAData data;
    WSAStartup(MAKEWORD(2, 2), &data);

    gethostname(hostname, 255);
    std::cout << "Host name: " << hostname << std::endl;

    if ((he = gethostbyname(hostname)) == NULL) {
        std::cout << "gethostbyname error" << std::endl;
    } else {
        std::cout << "IP addresses: "  << std::endl;
        addr_list = (struct in_addr **)he->h_addr_list;
        for(int i = 0; addr_list[i] != NULL; i++) {
            std::cout << inet_ntoa(*addr_list[i]) << std::endl;
        }
    }
    std::cin.get();
}

And this gives me different results in Windows Server 2012 and Windows Server 2008 / Windows 7. On my home PC with Windows 7, an increasing order is used:

Host name: SplattWin
IP addresses:
192.168.1.140
192.168.3.1
192.168.3.2
192.168.3.3
192.168.3.4

However, on a Windows 2012 server, it gives me IP addresses in descending order:

Host name: WinServ
IP addresses:
1.1.1.4
1.1.1.3
1.1.1.2
1.1.1.1

Is there a way to reorder? I tried the skipassource flag when I added these IP addresses, but in this case it does not work.

, gethostname(), gethostbyname(), IP- ( ). , , IP- .

+4
1

IP- Windows .. , Windows. IP- . :

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

#pragma comment(lib, "Ws2_32.lib")

bool SortInAddr(const in_addr &a, const in_addr &b)
{
    return (a.S_un.S_addr < b.S_un.S_addr);
} 

int main()
{
    WSAData data;
    WSAStartup(MAKEWORD(2, 2), &data);

    char hostname[256] = {0};
    if (gethostname(hostname, 255) == SOCKET_ERROR)
    {
        std::cout << "gethostname error: " << WSAGetLastError() << std::endl;
    }
    else
    {
        std::cout << "Host name: " << hostname << std::endl;

        struct hostent *he = gethostbyname(hostname);
        if (he == NULL)
        {
            std::cout << "gethostbyname error: " << WSAGetLastError() << std::endl;
        }
        else if (he->h_length != sizeof(in_addr))
        {
            std::cout << "gethostbyname did not return IPv4 addresses" << std::endl;
        }
        else
        {
            std::vector<in_addr> addrs;

            struct in_addr **addr_list = (struct in_addr **)(he->h_addr_list);
            for(int i = 0; addr_list[i] != NULL; ++i)
            {
                addrs.push_back(*(addr_list[i]));
            }

            if (addrs.size() > 1)
            {
                std::sort(addrs.begin(), addrs.end(), SortInAddr);
            }

            std::cout << "IPv4 addresses: " << std::endl;
            for(std::vector<in_addr>::iterator iter = addrs.begin();
                iter != addrs.end();
                ++iter)
            {
                std::cout << inet_ntoa(addrs[i]) << std::endl;
            }
        }
    }

    WSACleanup();
    std::cin.get();
}

gethostbyname() ( getaddrinfo()) . . GetAdaptersInfo() GetAdaptersAddresses(). .

+2

All Articles