1

I am developing an application in Rust using UDP sockets. The problem I encounter is that sometimes, the send_to function fails with an error saying Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }

I create the UDP socket using -

let socket = UdpSocket::bind(("0.0.0.0", 0)).expect("unable to create socket")

Using 0 as port so its automatically assigned.

I found a similar issue shown here with C++ without any explanation of what the error means or how to resolve it. In another post here it is mentioned that the kernel drops some of the packets. The solution there alters the system configuration.

So, what does this error mean and what could be a solution to deal with this? Even in the docs for sent_to no such error is mentioned.

As mentioned in the C++ post it is very rare and is hard to debug.

EDIT:

The I am using send_to as

socket.send_to(&sequence_data, address).expect("Couldn't send sequence")

Here,

  • address is a SocketAddr with value 127.0.0.1:<port>. The port is decided at runtime.
  • sequence_data is a Vec<u8>
  • socket is a UdpSocket
Anish Sharma
  • 314
  • 3
  • 18
  • If you are using Linux, from `man 2 send`: `EACCESS` _(For UDP sockets) An attempt was made to send to a network/broadcast address as though it was a unicast address_. That is maybe you are using a broadcast address as destination? – rodrigo Feb 09 '22 at 09:57
  • And in [Windows](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-sendto): `WSAEACCES`: _The requested address is a broadcast address, but the appropriate flag was not set._ – rodrigo Feb 09 '22 at 10:02
  • 1
    Your question is about an error from `sendto()` but you haven't provided any `sendto()` code. – user207421 Feb 10 '22 at 02:45
  • @user207421 I have added the code that causes the error. – Anish Sharma Feb 10 '22 at 10:14
  • @rodrigo I am using `127.0.0.1` as the address when testing. Isn't that unicast? I have encountered this error even during testing with other IPs. – Anish Sharma Feb 10 '22 at 10:16
  • Also, now when I modified the code such that I retry even when I encounter this error (basically a loop that sends till its successful), the error is encountered only once and the subsequent try is successful. – Anish Sharma Feb 10 '22 at 10:23
  • Ah, but maybe (from `man 7 udp`): "You may get an error for an earlier packet that was sent on the same socket." so the error may be in the address used for the previous `sendto()` call? – rodrigo Feb 10 '22 at 10:44
  • I don't think that applies because this is the first `sendto()` call I make after initializing the socket. Although it is possible that the destination address, the socket that is supposed to receive might not have been initialized yet. But I think for UDP it doesn't matter if the destination receives the packet or not. – Anish Sharma Feb 11 '22 at 03:40
  • It doesn't matter if the destination receives the packet or not but it matters if the destination _refuses_ the packet, which will happen if it receives the packet and doesn't have any open socket listening on the corresponding port. – Jmb Feb 11 '22 at 07:21
  • @Jmb: But if the peer is not listening to that port, it would returns a "port unreachable" ICMP error, that would be converted to `ECONNRESET`, not to `EACCESS`, IIRC. – rodrigo Feb 11 '22 at 17:24
  • I delayed the initialization of the receiving socket. Even then, the `sendto` function is actually successful and returns the number of bytes sent. Infact, the error doesn't occur anymore when the receiving socket doesn't exist. So, is it possible that the error might be thrown when the OS has initialized the port but the application is yet to complete initialization of the socket? So, even though the port is available, there is nothing receiving the data. – Anish Sharma Feb 12 '22 at 03:54

0 Answers0