How to get the client IP address before accepting the connection in C++

On Windows only, you can use the conditional callback feature of WinSock2's WSAAccept() function to access client information before accepting a connection, and to even reject the connection before it is accepted.


This can't be done in terms of the standard socket API. On all platforms I know, the system actually accepts the connection (i.e. responds with SYN+ACK TCP datagram) before the application has a chance to monitor the pending request.


For optimum performance, this would be solved by filtering in the network stack, but the details of doing that will depend on the operating system (this is not part of the socket interface and your application may generally not even have the rights to configure your network stack this way.)

The other opportunity is after the accept, by which time the connection is already accepted (CONNECT ACK) on TCP level.

I don't think you can do it in the middle phase where you would prefer that. That however would not be very different from doing it after accept anyway.

Tags:

C++

Sockets

Ip