0
votes

Steps to reproduce:

  1. Create IPv6 socket AF_INET6/SOCK_DGRAM/UDP
  2. Bind the socket to an IPv6 address
  3. Use sendto(...) winsocket API but specify an IPv4 socket AF_INET family address address to send to

Winsock2 API will return WSAEFAULT with WSAGetLastError.

1
A better approach would be to specify AF_INET6 for the family argument, and use a sockaddr_in6, but in that struct place an IPv4-mapped IPv6 address (as described here: tcpipguide.com/free/t_IPv6IPv4AddressEmbedding-2.htm ). That gives you a IPv6 socket that nevertheless is communicating via IPv4, and works well.Jeremy Friesner

1 Answers

2
votes

If you attempt to send a UDP packet to an IPv4 address using an IPv6 socket, sendto() will report the WSAEFAULT error. This behavior is described by MSDN:

sendto function

Return value

If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

...

WSAEFAULT
The buf or to parameters are not part of the user address space, or the tolen parameter is too small.

Technically that's true because an IPv4 address is smaller than an IPv6 address. It might be a bit misleading to think sizeof was wrong when it's actually the wrong address family used and hence the sizeof represents the wrong size to use.

See also windows error codes here:

Windows Sockets Error Codes