Raw UDP socket failure

Scott Bonar 1 Reputation point
2022-10-22T20:06:30.113+00:00

On Win10 I am trying to open and use a raw UDP socket. I understand that there are restrictions and that TCP is not supported but UDP should be.
I can create the socket successfully but when I try to set the IP_HDRINCL option, since my code provides this, I am getting an error and it is one that I do not understand.

The call to WSGetLastError() returns a 0 (????)
The errno is set to 2 - "No such file or directory"

Here is the code snippet

// Configure RAW UDP socket for outgoing  
SOCKET fd = INVALID_SOCKET;  
int err = 0;  
int optVal = 1;  
std::shared_ptr<NetworkLibraryInitializer> netLibInstance = NetworkLibraryInitializer::Instance();  
if (netLibInstance == nullptr)  
{  
LOG4CP_ERROR(logger, "Failed to initialize WS");  
return false;  
}  
  
// RAW UDP Socket  
fd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);  
if (fd == INVALID_SOCKET)  
{  
LOG4CP_ERROR(logger, "Failed to create raw UDP interface! [" << errno << "]");  
return false;  
}  
  
// Let the Kernel know we are building the IP Header ourselves  
err = setsockopt(fd, IPPROTO_IP, IP_HDRINCL, (char *)&optVal, sizeof(optVal));  
LOG4CP_DEBUG(logger, "fd : " << fd << " err : " << err);  
if (err == SOCKET_ERROR)  
{  
LOG4CP_ERROR(logger, "setsockopt(IP_HDRINCL) error: " << WSAGetLastError() << " errno: " << errno);  
closesocket(fd);  
  
return false;  
}  

Any suggestions or thoughts would be appreciative.

Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-10-24T05:10:41.377+00:00

    Hi ScottBonar,

    Hello, as far as I know, the server cannot use something similar to "INADDR_ANY" because this means any address on the host, but if you don't know the IP address of the server, it is impossible to find the server. So here you should set "INADDR_ANY" to a real IP address, "INADDR_ANY" can only be used for the sender (that is, if you need to send something, you can use my arbitrary IP, and the received IP must be a We can find the IP of its location).
    In addition, the address specified in the API call is not valid for that function or when the specified port is 0, there is also such an error.
    Hope my answer can help you!

    Best Regards,
    Xu Gu


  2. Limitless Technology 44,751 Reputation points
    2022-10-30T08:16:42.67+00:00

    Hi. Thank you for your question and reaching out.

    Address requested cannot be assigned.
    The context of the requested address makes it invalid. This typically happens when a local machine tries to bind to an address that is invalid. While the remote address or port is invalid for a remote machine, this can also happen when using the connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo commands (for example, address or port 0).

    You may also check this thread about this kind of issue.

    -----------------------------------------------------------------------------------------------------------------------------------

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.