在Linux下,由于sin6_port值,sendto在UDP原始套接字ipv6上返回无效参数

sendto returns with Invalid Argument on UDP Raw Socket ipv6 in Linux due to sin6_port value?

本文关键字:套接字 原始 ipv6 返回 参数 无效 UDP Linux 由于 sin6 port      更新时间:2023-10-16
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
struct ip6_hdr;
struct udphdr;
int main(){
  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_in6 serverAddr6;
  socklen_t addr_size;
  struct hostent *hp;
  struct msghdr m;
  struct in_addr **addr_list;
  /*Create UDP socket*/
 clientSocket = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
  /*Configure settings in address struct*/
  hp = gethostbyname2("2001:0db8:0:f101::2", PF_INET6);
  memset((char *)&serverAddr6, 0, sizeof(serverAddr6));
  memcpy((char *)&serverAddr6.sin6_addr, hp->h_addr, hp->h_length);
  serverAddr6.sin6_family = hp->h_addrtype;
  // serverAddr6.sin6_port   = htons(7891);
  serverAddr6.sin6_port   = 0;//htons(IPPROTO_RAW);
  /*Initialize size variable to be used later on*/
  const size_t IPV6_HEADER_LEN = 40;
  const size_t UDP_HEADER_LEN  = 8;
  addr_size = sizeof serverAddr6;
  size_t data_len = sizeof(serverAddr6) - IPV6_HEADER_LEN;
  /*Populate ip header*/
  struct ip6_hdr *iphdr = (struct ip6_hdr *)buffer;
  iphdr->ip6_flow = htonl ((6 << 28) | (0 << 20) | 0); // IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
  iphdr->ip6_plen = data_len;
  iphdr->ip6_nxt = IPPROTO_UDP;
  iphdr->ip6_hops = 64;
  //supposing src_addr and dst_addr pointers are never NULL
  memcpy(&iphdr->ip6_src, (const in6_addr*)&serverAddr6.sin6_addr, sizeof(in6_addr));
  memcpy(&iphdr->ip6_dst, (const in6_addr*)&serverAddr6.sin6_addr, sizeof(in6_addr));
  /*Populate UDP header*/
  udphdr * udp_header = reinterpret_cast<udphdr *>(buffer + IPV6_HEADER_LEN);
  udp_header->source = htons(0);
  udp_header->dest   = htons(7891);
  int len = sizeof(serverAddr6);
  uint32_t data_length = (len - (IPV6_HEADER_LEN + UDP_HEADER_LEN));
  udp_header->len      = htons(data_length + sizeof(udphdr));
  udp_header->check    = htons(0);
  for (int i=0; i<10; ++i)
  {
      sprintf(buffer, "%d", i);
      nBytes = strlen(buffer) + 1;
      /*Send message to server*/
      if(sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr6,addr_size) == -1 )
      {
          printf("sendto(): %sn", strerror(errno));
          exit(0);
      }
      /*Receive message from server*/
      // nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);
      // nBytes = recvmsg(clientSocket,&m,0);
    if((nBytes = recvmsg(clientSocket,&m, 0)) == -1)
    {
      printf("recvmsg(): %sn", strerror(errno));
      // exit(0);
    }
      printf("Received from server: %sn",buffer);
  }
  return 0;
}

我对套接字编程很陌生。我有以下代码。它返回sendto():无效参数经调查,我发现这一行包含:serverAddr6。Sin6_port = hons (7891);*在代码片段中被注释掉在谷歌的帮助下,我找到了一个解决方案http://osdir.com/ml/linux.ipv6.usagi.users/2003-03/msg00004.html:

从内核源代码,似乎当你使用原始ipv6套接字,你必须设置

dest.sin6_port = htons(IPPROTO_RAW);

和http://osdir.com/ml/linux.ipv6.usagi.users/2003-03/msg00005.html:

dest.sin6_port=0;

另一方面,我不能接受这个建议的解决方案,因为我必须指定端口为0以外的其他东西。

有什么建议或解释吗?

我正在使用Python 3,并且在发送IPv6包时也遇到OSError: [Errno 22] Invalid argument。但是,当我将sendto参数的端口号设置为0时,数据包发送成功,并且脚本生成的数据包中指定的端口(您的程序中的buffer)仍然使用。

您应该使用不同的端口号来生成数据包和调用sendto。生成所需的端口号,调用端口号为0

下面是我的代码
s = socket.socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)
s.setsockopt(IPPROTO_IPV6, IP_HDRINCL, 1)
s.sendto(GenerateIPv6Packet('::1', 53, '::1', 53), ('::1', 0))
    # generate the packet with port 53 and call sendto with port 0

很抱歉用另一种编程语言回答。