recvfrom() 是否修改 len?

Does recvfrom() modify len?

sendto() 给我 "invalid argument" 因为 len 从 20 减少了 2 个字节。 为什么调用 recvfrom() 会修改 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);

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);

并且

[...] 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.

所以,它是设计状态,recvfrom() 将修改最后一个参数指向的值,前提是

  • address 参数不是空指针
  • 协议提供消息的源地址