Getting IP adress associated to REAL hardware ethernet controller in Windows C

非 Y 不嫁゛ 提交于 2020-01-07 09:44:18

问题


I tried to search for it but I could'nt find any working soultions.

Nowadays Windows can detect more than ONE PROPER HARDWARE installed eternet controlled due to Hamachi, Virtual box etc etc.

Now i need to get the real IP adress of machine. When i tried do it by using gethostbyname it returned IP adress of my Hamachi client.

Now I need to find 100% solid method to receive real ip adress. Any links/tips/articles/methods really appreciate.

EDIT:

So far got this - method sugested by Remy Lebau, but it doesn't work the way I want - always returning 127.0.0.1:

    int main()
{
    WSADATA WSA_info;
    if (WSAStartup(MAKEWORD(2,2),&WSA_info)!=0)
    {
        std::cout << "Error with WSA.\n";
        std::cin.get();
        return -1;
    }
    SOCKADDR_IN adress;
    adress.sin_family = AF_INET;
    adress.sin_port = htons(80);
    adress.sin_addr.S_un.S_addr = inet_addr("213.241.88.40"); //its google's IP adress - to chech it I used ping google.com in cmd (maybe there is somethign wrong with this)
    SOCKET sock;
    if ((sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))== INVALID_SOCKET){
        std::cout << "Error while creating socket." << std::endl;
        closesocket(sock);
        WSACleanup();
        std::cin.get();
        return -1;
    }
    if (connect(sock,(sockaddr*)&adress,sizeof(sockaddr))== SOCKET_ERROR) {
        std::cout << "Couldnt connect." << std::endl;
        closesocket(sock);
        WSACleanup();
        std::cin.get();
        return -1;
    }
    sockaddr_in test;
    int len = sizeof(test);
    getsockname(sock,(struct sockaddr *) &test, &len);
    std::cout << inet_ntoa(test.sin_addr);

    std::cin.get();
}

回答1:


A PC can be connected to multiple networks at the same time (Internet, VM, VPN, etc), each with its own IP address, and you could initialize sockets on any of them at the same time and it would work. And more and more common, PCs are getting their Internet access from home router/ICS connections, not directly from Internet modems.

So what "real IP address" are you looking for? The PC can have multiple IP addresses, and they are all "real" as far as the OS cares.

If you just want to enumerate the available local IP addresses, you can use GetAdaptersInfo() or GetAdaptersAddresses(). DO NOT use gethostbyname() to discover local IP addresses - that is not what it is meant for, and it can report false information!

I think the question you actually want to ask is - how do I find the IP of the Ethernet controller used to access the Internet - am I right?

For that, you will have to connect() an unbound socket to an outside server that is running on the Internet, and if successful then you can use getsockname() to find out which local IP address the OS bound the socket to, for example:

SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

sockaddr_in remoteaddr = {0};
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_addr.s_addr = inet_addr("remote ip address");
remoteaddr.sin_port = htons(80);

if (connect(sock, (sock_addr*)&remoteaddr, sizeof(remoteaddr)) == 0)
{
    sockaddr_in localaddr = {0};
    int len = sizeof(localaddr);

    getsockname(sock, (sock_addr*)&localaddr, &len);
    // use localaddr as needed...
}

closesocket(sock);

But that is only going to tell you the IP address on the local PC. If you are behind a router/ICS, that is not going to be the same IP address that the Internet uses to send packets to your PC. To get that IP address, you can connect to and query an outside iplookup service, such as http://whatismyip.com, and see what IP address it reports back to you.



来源:https://stackoverflow.com/questions/24661022/getting-ip-adress-associated-to-real-hardware-ethernet-controller-in-windows-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!