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.
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.