0
votes

I'm trying to create a server that is able to determine whether each accepted connection is local or remote.

Server does this:

  1. call socket() to create TCP srvsock
  2. bind() srvsock to INADDR_ANY|server_port
  3. listen() on svrsock
  4. accept() connection on svrsock

Local client does this:

  1. call socket() to create clisock
  2. connect() clisock to 127.0.0.1|server_port

Remote client does this:

  1. call socket() to create clisock
  2. connect() clisock to server_public_ip|server_port

When accept() returns, how can the server determine whether the client is local or remote?

1

1 Answers

0
votes

The server can use getpeername() to get the address of a connected client. Or use the address that accept() outputs. Either way, if that address is among the addresses returned by getifaddrs() (or equivalent function, depending on your platform) then the client is local; otherwise, it is remote.