views:

190

answers:

3

There doesn't seem to be any method of Socket, or ListenSocket that will allow me to conditionally accept connections.

When I recieve a SYN, I want to be able decide if I want this connection depending on the source, if I send a SYN/ACK back (accept connection) or a RST (a forceful reject).

Is there any want to achieve this? Unfortunately, I can't just immediate close the connection after the accept, it needs to not be opened at all. I would also like to avoid having to work with it as a RAW socket.

+1  A: 

Unfortunately this is not possible. There is no way to conditionally accept using a TCP socket connection. You can only filter a connection once it's been established.

But what exactly are you trying to filter on? At the point you get the SYN packet all you know is the IP address of the source and the port they are trying to connect to. It seems like it would be much better to filter based on this data using the firewall. I realize this isn't controlled via your app but it's an alternative to consider.

JaredPar
It is a repeater service for a custom protocol over TCP. And I need it to begin rejecting local connections if the remote connection cannot be established. Also, there are some firewall features built-in as well.
Nick Whaley
A: 

It seems it is not possible without going into RAW mode. Once the socket goes into Winsock's listen mode, it will accept anything and everything, even BEFORE Socket.Accept is called.

You must use RAW mode and parse the packets with your own TCP stack if you want this functionality.

Nick Whaley
A: 

Scratch that too. Windows XP SP2 and later no longer support sending TCP packets over RAW sockets. See here for more details.

Nick Whaley