-1

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();
}
darekg11
  • 175
  • 1
  • 3
  • 13

1 Answers1

6

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I need local IP adress (the one which is assigned by router) so i just can try to connect to for example google.com and then use getsockname on that socket? Thank You very much for answers. – darekg11 Jul 10 '14 at 11:34
  • Yes, that will get you the router-assigned local IP address. – Remy Lebeau Jul 10 '14 at 14:50
  • I tried to do it, I connected to google IP adress on 80 port, connection was successful but getsocketname return 127.0.0.1 on that socket. – darekg11 Jul 11 '14 at 08:47
  • It is physically impossible for 127.0.0.1 to connect to an outside server. You must have done something wrong when retrieving the IP. – Remy Lebeau Jul 11 '14 at 15:28
  • I added an example to my answer. – Remy Lebeau Jul 11 '14 at 15:43
  • I really don'y know whats wrong with my code because I did the same as You. I included code in my first post because it's too long for comment - if You could check it out. – darekg11 Jul 12 '14 at 08:19
  • The only thing I see wrong is that you are not zeroing out the `sockaddr_in` structs, and not checking the return value of `getsockname()` for `SOCKET_ERROR` before printing out its output. But that would not account for 127.0.0.1 being reported. Most likely, you have a local antivirus/firewall installed that is interferring with outbound HTTP connections to port 80 (Norton and Symantec are known for doing that). Try a different port, or disable the antivirus/firewall, and see if the result changes. – Remy Lebeau Jul 12 '14 at 16:12
  • Also, you can use `gethostbyname()` or `getaddrinfo()` to resolve a hostname to IP so you don't have to hard-code it, as most Internet IPs do change over time. Or use a well-known static IP, like Google's DNS servers at 8.8.8.8/8.8.4.4 on port 53. – Remy Lebeau Jul 12 '14 at 16:17
  • Thank You! Using Google's DNS on 53 port fixed it. Thanks again – darekg11 Jul 12 '14 at 16:23