Reputation: 29
I am using a tcp socket on linux to listen for incoming tcp connections. the socket is blocking type.
I would like to find out what client is trying to connect to my socket before calling accept(). More specifically, I want to know the source IP and port.
Now, I have done some reading, and found people saying that this is not possible because the source IP address is not known (yet). I don't understand this. if a client notified the server that it wants to connect then it must have sent a SYN packet, which contains the information I want, right?
(source) Obtaining the source IP and port of an INADDR_ANY client socket before the TCP three-way handshake?
Perhaps they were referring to the client side.
As a server, I can select() or poll() the file descriptor to know how many clients want to connect, why can't I get their ip addresses and ports before accept()ing the connection?
I am sorry, my knowledge of networking is not so rich.
Thank you.
Upvotes: 2
Views: 1895
Reputation: 29
Thanks everyone for your answers. I figured out a workaround to my problem. so I figured I'd post it as a solution to my own question.
It is possible to use iptables and netfilter_queue under linux and filter out the SYN packets. One can then read the needed information from the packet and re-insert it into the stack without "accept"ing it!
I have tried this and it works. :)
Thanks, everyone, for your help.
Upvotes: 0
Reputation: 3541
It is the accept
that returns a new socket id for the new connection that got established. If accept
fails there is no connections anyways.
And with new socket on successful accept
you can use getpeername
to determine the client details.
select
or poll
will only notify activity on socket and not give any more details. You need to depend on accept
to check if connection is successful or not
Upvotes: 2