I need to write a small socket server proxy application that accepts connections from local applications on ALL interfaces (socket should bind to 0.0.0.0).
I'm not aware of way to achieve this (I have additional requirements that prevent binding to the 127.0.0.1).
A first attempt at this does the following:
bind(0.0.0.0) ... s = accept() ... // reject remote connection if (s.src_addr is not in local_interfaces) close() ... //proceed normally with local conenction
This implementation has a side effect on remote applications - they see an accept/close combination. The system is required to behave such that remote application perceive that "nothing is there": -->SYN <--RST/ACK
To implement this behavior I used winsock API combination of SO_CONDITIONAL_ACCEPT and a WSAAccept callback (LPCONDITIONPROC lpfnCondition),to accept / reject a connection based on its origin interface (i.e. is it one of the local addresses or not).
This results in the desired functional behavior: local apps work; remote apps get the required WSAECONNREFUSED error.
This comes with a certain prices: SO_CONDITIONAL_ACCEPT has certain side effects (see MSDN), but more importantly, we need a LINUX implementation and may later have to port this to other UNIXs.
My first priority is a LINUX solution. Note that I'm willing to go beyond the socket API if that's what it takes.