Here is a massively reduced code sample, following my code from my eRCaGuy_hello_world repo here as a pattern: socket__geeksforgeeks_udp_client_GS_edit_GREAT.c:
#define SOCKET_TYPE_UDP_IPV4 AF_INET, SOCK_DGRAM, 0
// Create an IPv4 UDP socket to send Ethernet packets out to a connected device
int socket_fd = socket(SOCKET_TYPE_UDP_IPV4);
// Send a packet via `sendto()`
const char msg_to_send[] = "Hello from client.";
ssize_t num_bytes_sent = sendto(socket_fd, msg_to_send, sizeof(msg_to_send), 0,
(const struct sockaddr *)&addr_server, sizeof(addr_server));
if (num_bytes_sent == -1)
{
printf("Failed to send to server. errno = %i: %s\n", errno, strerror(errno));
goto cleanup;
}
sendto() fails, however, with the num_bytes_sent return code set to -1 and errno set to EPERM. EPERM stands for "permissions error: 'E'rror 'PERM'issions". A list of all possible errno errors can be found here: https://man7.org/linux/man-pages/man3/errno.3.html. It shows:
EPERMOperation not permitted (POSIX.1-2001).
However, none of the 3 reference pages I have for the sendto() function show EPERM as a valid or even possible error condition for calling this function! Here are the 3 reference pages I have for sendto(). See the "ERRORS" or "RETURN VALUE" section of each of them:
- POSIX Programmer's Manual for
sendto(): https://man7.org/linux/man-pages/man3/sendto.3p.html - man7.org "Linux Programmer's Manual" for
sendto(): https://man7.org/linux/man-pages/man2/send.2.html - Linux.die.net manual for
sendto(): https://linux.die.net/man/2/sendto
So, what's happening and how do I fix it? I'd like sendto() to work so I can send out an Ethernet UDP packet to a connected device. On other machines it works fine, but from my embedded Linux board it fails with EPERM.
How to fix
EPERM(permissions error; 'E'rror 'PERM'issions) by disabling the firewallAfter a ton of study, googling, testing on an embedded Linux board, etc, I've determined it's simply because I have a firewall up blocking my outgoing traffic. Do this to disable the firewall, then try again, and the
EPERMerror will go away:One source that helped me conclude this was the problem, for instance, was Dmitry V. Krivenok here: UDP socket && sendto && EPERM (emphasis added):
See also: