4

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?

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Takkun
  • 6,131
  • 16
  • 52
  • 69
  • 1
    Why do you ask? What kind of server are you thinking of? I would believe that `connect`-ing cost more than `send`-ing. Read about the C10K problem http://en.wikipedia.org/wiki/C10k_problem – Basile Starynkevitch Nov 01 '12 at 19:45
  • 1
    Its easier to do in firewalls - such as iptables on linux. – Julian Nov 01 '12 at 19:45

3 Answers3

1

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

Tayyab
  • 9,951
  • 1
  • 16
  • 19
1

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

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770