Home > Software design >  how to determine if a socket handle is ipv4 or ipv6?
how to determine if a socket handle is ipv4 or ipv6?

Time:01-09

Given a socket handle, is there any way to determine if it is AF_INET or AF_INET6, e.g. first parameter to create socket(https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket)? I am programming on Windows.

CodePudding user response:

getsockopt function:

int getsockopt(
  [in]      SOCKET s,
  [in]      int    level,
  [in]      int    optname,
  [out]     char   *optval,
  [in, out] int    *optlen
);

If optname parameter is SO_PROTOCOL_INFO, buffer should contain WSAPROTOCOL_INFO structure. It's iAddressFamily member will be AF_INET or AF_INET6.

getsockopt function (winsock.h)

WSAPROTOCOL_INFOA structure (winsock2.h)

  •  Tags:  
  • Related