Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP sendto() error: invalid argument

Tags:

c

udp

We are trying to apply a UDP based protocol and having some problem with sendto() function.

when we try to response to the write-request with ack we get "invalid argument" from the sendto() function

this is our code:

int                sock;                  // Socket 
sockaddr_in_t      echoServAddr;          // Local address 
sockaddr_in_t      echoClntAddr;          // Client address 
unsigned int       cliAddrLen;            // Length of incoming message
data_packet_t      echoBuffer;
wrq_packet_t       wrqBuffer;
unsigned short     echoServPort;          // Server port 
int                recvMsgSize;           // Size of received message 
ack_packet_t      Ack;
struct timeval     timeout;
fd_set             fds;



/* Create socket for sending/receiving datagrams */
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) perror("TTFTPERROR: socket() failed"); 

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
echoServAddr.sin_port = htons(echoServPort);

/*Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) perror("TTFTPERROR: bind() failed");

FD_ZERO(&fds);
FD_SET(sock, &fds);
timeout.tv_sec = WAIT_FOR_PACKET_TIMEOUT;
timeout.tv_usec = 0;

while (1) {
    recvMsgSize = recvfrom(sock, &wrqBuffer, FULL_PACKET_SIZE, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen);
    if (recvMsgSize > 0) break; // we got something!
}

Ack = CreateAckPacket(0); // send ack 0
if (sendto(sock, &Ack, sizeof(Ack), 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) == -1){
    perror("TTFTPERROR: sendto() failed to send ack 0");
    exit(-1);
}

Could you help us understand what is wrong?

like image 345
user3350919 Avatar asked Jun 19 '15 14:06

user3350919


2 Answers

The likely error is a combination of the sendto line and recvfrom a few lines above.

What you are doing is, you reuse the sockaddr that recvfrom gave you to send a reply (ACK in this case). That is absolutely legitimate and correct, how else could you send a reply to someone on a connectionless protocol (you have no other way of knowing whom to send the answer to, nor with what protocol version!).

But: While you correctly provide &cliAddrLen to recvfrom so it can return the actual address length (which may vary, think of IPv4 vs IPv6 addresses) that was written into the buffer echoClntAddr, you later call sendto with sizeof(echoClntAddr) as the size of the address field.
Since echoClntAddr is most likely a sockaddr_storage (it should be, anyway), its size will be larger than any valid protocol's address size. Thus, it's an invalid argument. Or rather, the address is.

You should instead call sendto with cliAddrLen (and initialize cliAddrLen to sizeof(echoClntAddr) prior to calling recvfrom).

like image 156
Damon Avatar answered Oct 05 '22 08:10

Damon


Also be very careful to you pass VALID and EXISTING IP address to the bind()'s parameter: echoServAddr.sin_addr.s_addr

If you don't have network interface exposed with that exactly IP address the bind won't fail on some platforms (ESP32 for example) BUT the sendto() WILL definitely fail. I've spend 2 days debugging that and I'm mad!

like image 39
valioiv Avatar answered Oct 05 '22 06:10

valioiv