Server socket caller info

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Server socket caller info

Postby denville » Sat Dec 01, 2018 9:15 am

Hello friends and a very happy Christmas to you all.

When a (TCP IPv4) server socket is opened, is there a reliable, generic way to know whether the caller is on a local intranet (which may be a router or two away) or has connected via the Internet ?
denville
BCBJ Veteran
BCBJ Veteran
 
Posts: 57
Joined: Sat Mar 06, 2010 4:40 am

Re: Server socket caller info

Postby rlebeau » Mon Dec 03, 2018 1:09 pm

denville wrote:When a (TCP IPv4) server socket is opened, is there a reliable, generic way to know whether the caller is on a local intranet (which may be a router or two away) or has connected via the Internet ?


If the client is not on the same local subnet as the server, then no. Once you cross subnet boundaries, there is no direct way to know how many subnets away the client actually is (though, a TRACERT may show you how many network segments the query has to pass through to reach the client).

To know if the client is on the same subnet as the server, obtain the client's remote IP address, and the local server IP that the client connected to (both can be gotten from the accepted socket), then obtain the local server IP's subnet mask (can be gotten from the OS, not from the accepted socket), and mask both IPs with it. If the resulting values match, the client is on the server's local subnet. If not, then the client is on another subnet, or a completely different network.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1560
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Server socket caller info

Postby denville » Wed Dec 05, 2018 10:28 am

Thanks Remy
denville
BCBJ Veteran
BCBJ Veteran
 
Posts: 57
Joined: Sat Mar 06, 2010 4:40 am


Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 15 guests

cron