Click to See Complete Forum and Search --> : winsock hook


paragk11
June 4th, 2004, 04:38 AM
Hi,
I am using an application which is similar to chat application. I want to log all the text on chat window.I have hooked send() Recv() and closesocket() with other functions.
The problem now I am facing is if user types some text and immediately closes the window ,I can not get the buffer in send () as it's closesocket gets called .
If I close the hook engine then the message goes to other end properly .With hook engine on ,If user types and waits or does not close the window then mesage gets to other end and also gets logged .
Is there any way to make sure that all the request (data) on connected socket is received in send() before calling it's closesocket().
OR how can I wait in hooked send() or closesocket() to make sure that all the send request is processed before closing the connected socket?? OR
before calling the original send() and closesocket()?

MikeAThon
June 4th, 2004, 02:35 PM
Call shutdown() before closesocket(), with correct setting of the "how" flag. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/shutdown_2.asp
from MSDN site
To assure that all data is sent and received on a connected socket before it is closed, an application should use shutdown to close connection before calling closesocket. For example, to initiate a graceful disconnect:
1. Call WSAAsyncSelect to register for FD_CLOSE notification.
2. Call shutdown with how=SD_SEND.
3. When FD_CLOSE received, call recv until zero returned, or SOCKET_ERROR.
4. Call closesocket.

Mike

paragk11
June 7th, 2004, 01:06 AM
ya..I have tried that by calling shutdown before calling original closesocket() in my HookCloseSocket()..but it does not work.

j0nas
June 7th, 2004, 09:11 AM
I don't exacty understand the semantics in your problem--are you sure closesocket() is called before the last send()? I cannot understand how that would work. What may happen, is that closesocket is called before the whole buffer has been sent. If this is the case, you can record actual bytes pending in send(), something like this:

HookSend() {
n = send(s, buf, len, 0);
pendingSend = (n != len); // record pending send
if (pendingClose && !pendingSend)
closesocket(s);
}

HookClosesocket() {
if (pendingSend)
pendingClose = TRUE;
else
closesocket(s);
}