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?
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.
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