开发者

Retrieving the protocol of a socket in winsock

开发者 https://www.devze.com 2023-02-08 06:06 出处:网络
I am working in networking reliability simulation, I need to simulate packet dropping based on a quality of service percentage. Currently I have a DLL that hooks into 开发者_Go百科send, sendto, recv a

I am working in networking reliability simulation, I need to simulate packet dropping based on a quality of service percentage. Currently I have a DLL that hooks into 开发者_Go百科send, sendto, recv and recvfrom. My hooks then 'drop' packets based on the quality of service.

I just need to apply the hook to UDP packets, and not disturb TCP (TCP is used for remote debugging).

Is there a way that I can query WinSock for the protocol that a socket is bound to?

int WSAAPI HookedSend(SOCKET s, const char FAR * buf, int len, int flags)
{
  //if(s is UDP)
  //Drop according to QOS

  else
    //Send TCP packets undisturbed
    return send(s, buf, len, flags);
}


I think you could get the socket type by using getsockopt:

int optVal;
int optLen = sizeof(int);

getsockopt(socket, 
          SOL_SOCKET, 
          SO_TYPE, 
          (char*)&optVal, 
          &optLen);

if(optVal = SOCK_STREAM)
     printf("This is a TCP socket.\n");
else if(optVal = SOCK_DGRAM)
     printf("This is a UTP socket.\n");
else
     printf("Error");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号