Sign in to follow this  
jamesd128

Determining if connection coming from my network

Recommended Posts

I'm writing this in C#. I have a host listening on a TCP socket. When someone joins I want to be able to determine if they are joining me from a computer that is on my network. How do I do this?

Share this post


Link to post
Share on other sites
Ok well this works for my computer, can someone verify that this is the right way to use the subnet mask to tell if I'm getting contacted by a computer on my network?


IPAddress clientIPAddr = IPAddress.Parse(socket.RemoteEndPoint.ToString().Split(new char[1] { ':' })[0]);
IPAddress hostIPAddr = IPAddress.Parse(socket.LocalEndPoint.ToString().Split(new char[1] { ':' })[0]);

bool onSameNetwork = false;
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces()) {
if (f.OperationalStatus == OperationalStatus.Up) {
IPInterfaceProperties ipInterface = f.GetIPProperties();
foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses) {
if (unicastAddress.IPv4Mask != null && unicastAddress.IPv4Mask.ToString() != "0.0.0.0")
if((clientIPAddr.Address & hostIPAddr.Address) == (clientIPAddr.Address & clientIPAddr.Address))
onSameNetwork = true;
}
}
}

Share this post


Link to post
Share on other sites
Nevermind that didn't work as intended, here's a new version:


IPAddress clientIPAddr = IPAddress.Parse(socket.RemoteEndPoint.ToString().Split(new char[1] { ':' })[0]);
IPAddress hostLocalIPAddr = IPAddress.Parse(socket.LocalEndPoint.ToString().Split(new char[1] { ':' })[0]);

bool onSameNetwork = true;
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces()) {
if (f.OperationalStatus == OperationalStatus.Up) {
IPInterfaceProperties ipInterface = f.GetIPProperties();
foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses) {
if (unicastAddress.IPv4Mask != null && unicastAddress.IPv4Mask.ToString() != "0.0.0.0") {

String[] parts = unicastAddress.IPv4Mask.ToString().Split(new char[1] { '.' });
int[] subnetMask = new int[4] { Int32.Parse(parts[0]), Int32.Parse(parts[1]), Int32.Parse(parts[2]), Int32.Parse(parts[3]) };
parts = clientIPAddr.ToString().Split(new char[1] { '.' });
int[] clientAddr = new int[4] { Int32.Parse(parts[0]), Int32.Parse(parts[1]), Int32.Parse(parts[2]), Int32.Parse(parts[3]) };
parts = hostLocalIPAddr.ToString().Split(new char[1] { '.' });
int[] hostAddr = new int[4] { Int32.Parse(parts[0]), Int32.Parse(parts[1]), Int32.Parse(parts[2]), Int32.Parse(parts[3]) };

for (int i = 0; i < 4; i++) {
if ((clientAddr[i] & subnetMask[i]) != (hostAddr[i] & subnetMask[i]))
onSameNetwork = false;
}

}
}
}
}

Share this post


Link to post
Share on other sites
I can't think of many reasons why you would want to know this, and parsing IP address strings is going to leave you in a sad spot if you ever want to support IPv6.

So, why do you need to know if someone is on the same network as you?

Share this post


Link to post
Share on other sites
Quote:
Original post by jamesd128
When someone joins I want to be able to determine if they are joining me from a computer that is on my network.
Why does it mater if they are on the same subnet?

If it is a matter of providing priority/higher-quality, then measuring end-to-end latency and available bandwidth might be a better metric. If it is a security issue, you should be configuring the firewall to block unwanted clients instead.

Particularly if you are dealing with a matchmaking server outside the current network, then depending on the NAT setup, it may be very involved to robustly determine that you are both on the same subnet.

Share this post


Link to post
Share on other sites
Actually the only reason I'm doing this is to avoid NAT issues. The TCP/IP option of my game uses fully connected sockets. If the host and another player are behind a NAT, the host needs to be able to tell the third player how to connect to the other person behind the NAT. To do this I require all players who are joining my game from within my network to provide their external IP (by querying whatismyip). I can then pass that external IP to the player who is joining from outside the network. The points is I need to know if they are from my network to see if I should require them to provide an external IP.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this