Takkun
Takkun

Reputation: 6361

Limit Incoming TCP Connections based on IP

Do sockets in C offer any kind of way to limit the number of incoming connections to the socket based on the IP?

For example, to prevent one client IP from spamming connections, is there a way to limit the number of times an IP can connect to a socket?

Or does something like this have to be custom made?

Upvotes: 4

Views: 352

Answers (3)

fkl
fkl

Reputation: 5535

I feel the real intention you are talking about is throttling i.e. for a particular client / connection, allowing only a fixed number of packets in a given time. This sounds like a more realistic usage scenario than allowing / disallowing more connections.

Most of modern languages provide some kind of support such as java or c# but not c.

However, here is an elegant approach to implementing it. I myself have used it in production code.

implementing throttling

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 595837

There is nothing in the standard socket API for that, no. Using standard APIs, the only thing the server code can do is accept() a client connection, check its IP, and then close the connection if needed.

In the case of Microsoft's WinSock API, the WSAAccept() function does have a callback that is called before a connection is accepted from the server's queue. The callback function can decide to either accept the connection, reject the connection, or leave it in the queue.

Upvotes: 1

Tayyab
Tayyab

Reputation: 10631

There is nothing like that in sockets. You need custom solution and it will be better to consider doing it in your firewall.

Upvotes: 1

Related Questions