由于sin6_port值,sendto在Linux上的UDP Raw Socket ipv6上返回无效参数?

时间:2015-09-11 03:03:32

标签: c++ linux sockets udp ipv6

#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(): %s\n", 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(): %s\n", strerror(errno));
      // exit(0);
    }
      printf("Received from server: %s\n",buffer);
  }

  return 0;
}

我是套接字编程的新手。我有以下代码。它回来了 sendto():参数无效 经过调查,我发现该行包含:     serverAddr6.sin6_port = htons(7891); *在代码段中注释掉了 在Google的帮助下,我找到了解决方案 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以外的其他东西。

有任何建议或解释吗?

1 个答案:

答案 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

很抱歉使用其他编程语言回答。