Get destination port in WSPSend
I am having a problem in getting the destination port number in the WSPSend function in the sample LSP provided with Microsoft platform SDK.
Here is the code that I am using. As shown below, the if statement is not entered. I verified this using a debug function.
I am trying to recognize the outgoing HTTP packets inside this function using the destination port 80.
int WSPAPI
WSPSend(
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesSent,
DWORD dwFlags,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
LPWSATHREADID lpThreadId,
LPINT lpErrno
)
{
INT ret = SOCKET_ERROR;
SOCK_INFO *SocketContext = NULL;
LPWSAOVERLAPPEDPLUS ProviderOverlapped = NULL;
*lpErrno = NO_ERROR;
//
开发者_JAVA百科 // Find our provider socket corresponding to this one
//
SocketContext = FindAndRefSocketContext(s, lpErrno);
if ( NULL == SocketContext )
{
dbgprint( "WSPSend: FindAndRefSocketContext failed!" );
goto cleanup;
}
// My code starts here!!!
SOCKET app = SocketContext->LayeredSocket;
struct sockaddr FAR name;
int FAR namelen;
getpeername(app, &name, &namelen);
struct sockaddr_in sin;
sin =* (const struct sockaddr_in *) (&name);
if(sin.sin_port == htons(80))
{
// This code is not executed after sending HTTP packets!!
}
}
Any idea?
Has getpeername worked? Your code needs to check the return code before using the results.
If no error occurs, getpeername returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
That aside, you need to specify namelen
to be the size of your output structure before making this call - that is my bet for what is wrong here, since namelen
is not initialized. It's important to read the WinSock documentation carefully - Windows is riddled with these API usage rules, and you could waste a lot of time if you don't follow them.
On call, the namelen parameter contains the size, in bytes, of the name buffer. On return, the namelen parameter contains the actual size, in bytes, of the name parameter returned.
精彩评论