Link to home
Create AccountLog in
Avatar of adisal
adisal

asked on

Want to limit bandwidth in windows using Winsock SPI

I am trying to make a tool that can limit / throttle / shape / cap the bandwidth allocated to each process / application in Windows XP.
Got some lead on LSP here :
http://www.microsoft.com/msj/0599/LayeredService/LayeredService.aspx
But still  i am not clear about how i can limit bandwidth as for each process.
I read about functions WSPSend & WSPRecv etc in SPI, how can i use these functions for my job?
ASKER CERTIFIED SOLUTION
Avatar of migoEX
migoEX

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of adisal
adisal

ASKER

I was able to hook API call WSPSend .
I inserted sleep in between calls.
 but it feels like Internet Explorer hangs for a while in between calls.
May be i inserted sleep in wrong place , trying to figure it out now.
Any suggestions?
I assume that it uses non-blocking mode: asynchronous API or doing "select" before the calls.

(1) Select
(1.a) WSPSelect function - you can call the original function, but add  a sleep here before returning from the call
(1.b) WSPAsyncSleep - it's more complicated. Basically, the application gives you Event object and it will later so one of "WaitFor" functions on it. You can replace the event with your own event, but you have to make sure you get called when the event is fired. One option is to add hooks (with "detours") on all versions of "WaitFor" (note: there are "single object", "multiple objects", "msg" etc), and add your handle to the list of application's handles (of cause, only if the "hooked" handle is present). When you Event is being signeled - it's a place to "sleep", but you don't want to sleep - or the appliction will block :) Here you can set up a timer and signel the original event when the timer expires. See, for example, http://msdn.microsoft.com/en-us/library/ms686898(VS.85).aspx
The other option would be doing the waiting in separate thread, and once ready + appropriate time has passed - notify the original event.

(2) Async (overlapped) functions
(2.a) Through "lpCompletionRoutine" parameter (say, of WSPSend). This case is pretty easy - just override the callback with your own function + register timer callback + notify original event
(2.b) Through"hEvent" member of "lpOverlapped" parameter - in this case you should act same way as i (1.b)

Good luck :)

I would suggest to start with printing a debug info, to understand which type of non-blocking API is used by IE. Of cause, if you want to make a generic solution, you'll need to support all synchronization types.