Home > Blockchain >  Measuring packet per second for connected clients IP addresses
Measuring packet per second for connected clients IP addresses

Time:07-03

How can we determine the packet rate of clients connected to our server in case of multi client server using Winsock. The idea I came up with is keeping a frequency map for IP addresses of all the clients and storing the packets count for some arbitrary amount k seconds. Now after k seconds we traverse the map and see what IP addresses have more than 100*k packets, now we block these IP addresses. After every k seconds we empty the map and start again. PSEUDO CODE: (k = 10)

map<string,int> map;

void calculate() {
  for(auto &ip : map){
     if(ip.second>10000) blacklist(ip.first);
  }
  map.clear();
  Sleep(10000);
  calculate();
}

int s = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
// bind(), listen()

calculate();

while(1) {
  if(recv(s,buff,len)>0) map[client.ip]  ;
  
}

CodePudding user response:

Per comments:

If someone is sending too fast, I'd like to block him permanently rather than receiving his messages less frequently. Something like this is what I'm trying to achieve

If this was UDP, I'd 100% be onboard with what you are trying to do and give the code I have. But this is TCP and your assumptions are flawed.

Let's say the sender invokes this:

send(sock, buffer, 1000, 0);

And then on the other side, you invoke this:

recv(sock, buffer, 1000, 0)

Did you know that recv may do any of the following:

  • It may return any value less than or equal to 1000. It could return 1 and expect you to invoke it another 999 times to consume the entire message. One of the biggest confusions with TCP socket is assuming that each send call mirrors a recv call in a 1:1 fashion. Lots of buggy apps have shipped that way.

  • More probably, you'll get 1 or 2 recv calls because of IP fragmentation and/or TCP segmentation. How fast you invoke recv also But this is never guaranteed or expected to be consistent. What you observe with local testing on your on LAN will not resemble actual internet behavior.

  • How many recv calls you get has nothing to do with how many actual IP packets or TCP segments, because "the packets" will get coalesced anyway by the TCP stack on the recv side.

  • Similarly, how many bytes you pass to send doesn't influence the packet count. TCP, including any number of routers and gateways in between, may split up this 1000 byte stream into additional fragments and segments.

I'm going to offer two suggestions:

  1. Detect flood attacks by counting application protocol messages and/or the size of these application protocol messages - but not individual recv calls. That is, as you recv data, you'll accumulate this data stream into logical protocol messages based on a fixed size of bytes or a delimiter based message structure and pass it up to a higher part of your application for processing. Do the incremental count then.

  2. Instead of trying to thwart flood attacks at the message level, it's probably simpler to just throttle clients to a fixed rate of data. That is, each time you recv data, count how many bytes it returns and use with a timer to measure an incoming bytes/second rate. If the remote side exceeds your limit, insert sleep statements in between recv calls. This will implicitly make TCP slow the other side down from sending too fast.

  • Related