views:

57

answers:

2

I need to write a rate limiter, that will perform some stuff each time X bytes were transmitted. The straightforward is to check the length of each transmitted packet, but I think it will be to slow for me. Is there a way to use some kind of network event, that will be triggered by transmitted packets/bytes?

+1  A: 

I think you may look at netfilter.

Using its (kernel level) api, you can have your custom code triggered by network events, modify received messages before passing it to application, and so on.

http://www.netfilter.org/

Mr Couch
A: 

It's protocol dependent, actually. But for TCP, you can setsockopt the SO_RCVLOWAT option to define the minimum number of bytes (watermark) to permit the read operation. If you need to enforce the maximum size too, adjust the receive buffer size using SO_RCVBUF.

jweyrich