6

For a project, I need to know whether the network connection is from the local computer or from a remote computer.

How to achieve this?

1 Answer 1

7

This can be achieved by utilizing the getpeername and the getsockname functions.

This snipped does exactly what I need it to:

 bool checkForLocalConnection(SOCKET Sock) {
      sockaddr_in RemAddr, LocAddr;
      int Len = sizeof(RemAddr);
      getpeername(Sock, (sockaddr *)&RemAddr, &Len);
      getsockname(Sock, (sockaddr *)&LocAddr, &Len);
      return (RemAddr.sin_addr.S_un.S_addr == LocAddr.sin_addr.S_un.S_addr);
 }

The endianess of the result is always the same, which is why you don't even have to convert it to native endianess.

Why this works and why it's necessary:

If you connect to localhost or 127.0.0.1, getpeername will always yield the address 127.0.0.1 (converted to an unsigned long, obviously). That means, you could just check for htonl(2130706433); and be done with it (Minding the endianess). However if you enter the actual address...or any of your other local addresses your NIC might have, getpeername will return that address, instead of 127.0.0.1.

getsockname will return the local interface this socket is connected on, which means it will choose the correct interface and tell you its address, which is equal only if you're connected from a local machine.

I hope this will help someone, since I had to search forever to find that little info. It should work for most common cases. (There are some exceptions)

List of exceptions:

Multi-Address network cards. These are on the same machine but either not on the same NIC or bound to a different IP. There isn't that much you can do about that.

Calling localhost on a different IP than 127.0.0.1. getsockname will always return 127.0.0.1, regardless of which 127.x.x.x you're calling. As a 'guard' against that, you can check specifically for the 127 in the first octet of the peer address.

Many thanks for the help with this goes to harper.

9
  • Also keep in mind that your system might be forwarding some ports, which would look local but ain't really. Commented Aug 23, 2014 at 14:58
  • It's common to have 127.0.0.1 but not necessary. See superuser.com/questions/31824/why-is-localhost-ip-127-0-0-1 to find more localhosts. Your comparison should check only for the first byte if it's 127.
    – harper
    Commented Aug 23, 2014 at 15:59
  • @harper That's why I offered this solution. It should work for about all cases, even if 127.0.0.1 isn't the localhost. Commented Aug 23, 2014 at 16:03
  • But peername and sockname doesn't need to have the same sin_address. "127.1.1.1"!="127.0.0.1".
    – harper
    Commented Aug 23, 2014 at 16:09
  • 1
    To handle the multi-homed situation, you can enumerate the available local IPs using OS-specific APIs, like GetAdaptersAddresses() or getifaddrs(). If the IPs reported by getsockname() and getpeername() are both local, then you have a local connection, otherwise you have a remote connection. Commented Aug 23, 2014 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.