-1

sendto() give me "invalid argument" because len decrease by 2 bytes from 20. Why calling recvfrom() modifies len?

s=socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));    
struct sockaddr_ll sll;
len=sizeof(sll);
bzero((char *)&sll, sizeof(sll));
sll.sll_ifindex=3;

rf=recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &sll, &len);

st=sendto(s, buf, 6+6+2+20+8+8, 0, (struct sockaddr *) &sll, len);
Enrico R.
  • 21
  • 5

1 Answers1

1

To quote the man page

Syntax:

 ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
      int flags, struct sockaddr *restrict address, socklen_t *restrict address_len);

and,

[...] If the address argument is not a null pointer and the protocol provides the source address of messages, the source address of the received message shall be stored in the sockaddr structure pointed to by the address argument, and the length of this address shall be stored in the object pointed to by the address_len argument.

So, it's status-by-design, recvfrom() will modify the value pointed by the last argument, provided

  • the address argument is not a null pointer
  • the protocol provides the source address of messages
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261