tags:

views:

253

answers:

4

How, in C, can I detect whether a program is connecting to itself.

For example, I've set up a listener on port 1234, then I set up another socket to connect to an arbitrary address on port 1234. I want to detect whether I'm connecting to my own program. Is there any way?

Thanks, Dave

A: 

You could send a sequence of magic packets upon connection, which is calculated in a deterministic way. The trick is how to do this in a way that sender and receiver will always calculate the same packet contents if they are from the same instance of the program. A little more information on what your program is would be helpful here, but most likely you can do some sort of hash on a bunch of program state and come up with something fairly unique to that instance of the program.

Matt J
I'm writing a simple transparent HTTP proxy server, but want to prevent clients from crashing or maxing out the proxy if they try to connect to the server's address.
@Matt J: why not to send the PID ?
yves Baumes
How? As an HTTP header? Then the proxy won't be all that transparent...
@yves: The PID would uniquely identify the instance within a single machine, but the "random address" part makes me think this will be running on multiple machines, where you could have a PID collision, especially with frequent reboots ;-)
Matt J
A: 

I assume you mean not just the same program, but the same instance of it running on the same machine.

Do you care about the case where you're connecting back to yourself via the network (perhaps you have two network cards, or a port-forwarding router, or some unusual routing out on the internet somewhere)?

If not, you could check whether the arbitrary address resolves to loopback (127.0.0.1), or any of the other IP addresses you know are you. I'm not a networking expert, so I may have missed some possibilities.

If you do care about that "indirect loopback" case, do some handshaking including a randomly-generated number which the two endpoints share via memory. I don't know whether there are security concerns in your situation: if so bear in mind that this is almost certainly subject to MITM unless you also secure the connection.

Steve Jessop
+1  A: 

Linux provides tools that I think can solve this problem. If the connection is to the same machine, you can run

fuser -n tcp <port-number>

and get back a list of processes listening to that port. You can then look in /proc and found out if there is a process with a pid not your own which is running the same binary you are. A bit of chewing gum and baling wire will help keep the whole contraption together.

I don't think you can easily ask questions about a process on another machine.

Norman Ramsey
you could ask the other proccess to ask for you and send back a response.
hhafez
+1  A: 

One of the parameters to the accept() function is a pointer to a struct sockaddr.

When you call accept() on the server side it will fill in the address of the remote machine connecting to your server socket.

If that address matches the address of any of the interfaces on that machine then that indicates that the client is on the same machine as the server.

Andrew Edgecombe