【问题标题】:UDP packet does not arrive if not pinged beforehand如果没有事先 ping 通,UDP 数据包不会到达
【发布时间】:2016-01-10 19:15:41
【问题描述】:

我有一个代码可以将 UDP 数据包从 linux 设备(服务器)发送到另一个(客户端)。我在执行此操作时遇到问题:

  1. 重启服务器。
  2. 运行程序发送数据包,客户端没有收到。

如果然后我对客户端 ip 执行 ping 操作,然后再次运行程序,现在数据包已被客户端接收到

这是代码:

  int main (int argc, char **argv){
  int i, status, datalen, frame_length, sd, bytes;
  char *interface, *target, *src_ip, *dst_ip;
  struct ip6_hdr iphdr;
  struct udphdr udphdr;
  uint8_t *data, *src_mac, *dst_mac, *ether_frame;
  struct addrinfo hints, *res;
  struct sockaddr_in6 *ipv6;
  struct sockaddr_ll device;
  struct ifreq ifr;
  void *tmp;

  // Allocate memory for various arrays.
  src_mac = allocate_ustrmem (6);
  dst_mac = allocate_ustrmem (6);
  data = allocate_ustrmem (IP_MAXPACKET);
  ether_frame = allocate_ustrmem (IP_MAXPACKET);
  interface = allocate_strmem (INET6_ADDRSTRLEN);
  target = allocate_strmem (INET6_ADDRSTRLEN);
  src_ip = allocate_strmem (INET6_ADDRSTRLEN);
  dst_ip = allocate_strmem (INET6_ADDRSTRLEN);

  // Interface to send packet through.
  strcpy (interface, "usb0");

  // Submit request for a socket descriptor to look up interface.
  if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
    perror ("socket() failed to get socket descriptor for using ioctl() ");
    exit (EXIT_FAILURE);
  }

  // Use ioctl() to look up interface name and get its MAC address.
  memset (&ifr, 0, sizeof (ifr));
  snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), "%s", interface);
  if (ioctl (sd, SIOCGIFHWADDR, &ifr) < 0) {
    perror ("ioctl() failed to get source MAC address ");
    return (EXIT_FAILURE);
  }
  close (sd);

  // Copy source MAC address.
  memcpy (src_mac, ifr.ifr_hwaddr.sa_data, 6 * sizeof (uint8_t));

  // Report source MAC address to stdout.
  printf ("MAC address for interface %s is ", interface);
  for (i=0; i<5; i++) {
    printf ("%02x:", src_mac[i]);
  }
  printf ("%02x\n", src_mac[5]);

  // Find interface index from interface name and store index in
  // struct sockaddr_ll device, which will be used as an argument of sendto().
  memset (&device, 0, sizeof (device));
  if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
    perror ("if_nametoindex() failed to obtain interface index ");
    exit (EXIT_FAILURE);
  }
  printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);

  // Set destination MAC address
  dst_mac[0] = 0x00;
  dst_mac[1] = 0x11;
  dst_mac[2] = 0x7d;
  dst_mac[3] = 0x30;
  dst_mac[4] = 0x7f;
  dst_mac[5] = 0xd0;

  // Source IPv6 address
  strcpy (src_ip, "fe80::11:7dff:fe30:8013");

  // Destination URL or IPv6 address
  strcpy (target, "fe80:0000:0000:0000:0211:7d00:0030:7fd0");

  // Fill out hints for getaddrinfo().
  memset (&hints, 0, sizeof (hints));
  hints.ai_family = AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = hints.ai_flags | AI_CANONNAME;

  // Resolve target using getaddrinfo().
  if ((status = getaddrinfo (target, NULL, &hints, &res)) != 0) {
    fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status));
    exit (EXIT_FAILURE);
  }
  ipv6 = (struct sockaddr_in6 *) res->ai_addr;
  tmp = &(ipv6->sin6_addr);
  if (inet_ntop (AF_INET6, tmp, dst_ip, INET6_ADDRSTRLEN) == NULL) {
    status = errno;
    fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status));
    exit (EXIT_FAILURE);
  }
  freeaddrinfo (res);

  // Fill out sockaddr_ll.
  device.sll_family = AF_PACKET;
  memcpy (device.sll_addr, src_mac, 6 * sizeof (uint8_t));
  device.sll_halen = 6;

  // UDP data
  datalen = 4;
  data[0] = 'T';
  data[1] = 'E';
  data[2] = 'S';
  data[3] = 'T';

  // IPv6 header

  // IPv6 version (4 bits), Traffic class (8 bits), Flow label (20 bits)
  iphdr.ip6_flow = htonl ((6 << 28) | (0 << 20) | 0);

  // Payload length (16 bits): UDP header + UDP data
  iphdr.ip6_plen = htons (UDP_HDRLEN + datalen);

  // Next header (8 bits): 17 for UDP
  iphdr.ip6_nxt = IPPROTO_UDP;

  // Hop limit (8 bits): default to maximum value
  iphdr.ip6_hops = 255;

  // Source IPv6 address (128 bits)
  if ((status = inet_pton (AF_INET6, src_ip, &(iphdr.ip6_src))) != 1) {
    fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
    exit (EXIT_FAILURE);
  }

  // Destination IPv6 address (128 bits)
  if ((status = inet_pton (AF_INET6, dst_ip, &(iphdr.ip6_dst))) != 1) {
    fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status));
    exit (EXIT_FAILURE);
  }

  // UDP header

  // Source port number (16 bits): pick a number
  udphdr.source = htons (61616);

  // Destination port number (16 bits): pick a number
  udphdr.dest = htons (61616);

  // Length of UDP datagram (16 bits): UDP header + UDP data
  udphdr.len = htons (UDP_HDRLEN + datalen);

  // UDP checksum (16 bits)
  udphdr.check = udp6_checksum (iphdr, udphdr, data, datalen);

  // Fill out ethernet frame header.

  // Ethernet frame length = ethernet header (MAC + MAC + ethernet type) + ethernet data (IP header + UDP header + UDP data)
  frame_length = 6 + 6 + 2 + IP6_HDRLEN + UDP_HDRLEN + datalen;

  // Destination and Source MAC addresses
  memcpy (ether_frame, dst_mac, 6 * sizeof (uint8_t));
  memcpy (ether_frame + 6, src_mac, 6 * sizeof (uint8_t));

  // Next is ethernet type code (ETH_P_IPV6 for IPv6).
  // http://www.iana.org/assignments/ethernet-numbers
  ether_frame[12] = ETH_P_IPV6 / 256;
  ether_frame[13] = ETH_P_IPV6 % 256;

  // Next is ethernet frame data (IPv6 header + UDP header + UDP data).

  // IPv6 header
  memcpy (ether_frame + ETH_HDRLEN, &iphdr, IP6_HDRLEN * sizeof (uint8_t));

  // UDP header
  memcpy (ether_frame + ETH_HDRLEN + IP6_HDRLEN, &udphdr, UDP_HDRLEN * sizeof (uint8_t));

  // UDP data
  memcpy (ether_frame + ETH_HDRLEN + IP6_HDRLEN + UDP_HDRLEN, data, datalen * sizeof (uint8_t));

  // Submit request for a raw socket descriptor.
  if ((sd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) {
    perror ("socket() failed ");
    exit (EXIT_FAILURE);
  }

  // Send ethernet frame to socket.
  if ((bytes = sendto (sd, ether_frame, frame_length, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) {
    perror ("sendto() failed");
    exit (EXIT_FAILURE);
  }

  // Close socket descriptor.
  close (sd);

  // Free allocated memory.
  free (src_mac);
  free (dst_mac);
  free (data);
  free (ether_frame);
  free (interface);
  free (target);
  free (src_ip);
  free (dst_ip);

  return (EXIT_SUCCESS);
}

【问题讨论】:

  • 当您多次运行程序而没有 ping 或任何其他网络通信时会发生什么?如果您在运行程序前等待五分钟会发生什么?

标签: c linux udp ipv6


【解决方案1】:

如果dst mac 不在ARP 表中,您选择发送UDP 数据包的方法似乎会失败。不知何故,这样做会导致内核对于dst mac 不是arp(可能是因为您已手动将其插入数据包中)-但是-数据包传输仍然失败,因为它不存在于@ 987654326@ 表。 ping 导致 ARP 表被填充,然后它开始工作。

只是一个理论。我们将不得不查看内核路径来确认这一点。

你可以尝试几个实验来证实这个理论。

  1. 不要ping。运行该程序几次,看看它在后续运行中是否有效。
  2. 首先ping,然后尝试该程序(它应该可以工作),然后刷新ARP 表,再次尝试该程序(它应该失败)。使用sudo ip -s -s neigh flush all 刷新ARP 表。

您是否有任何理由不使用SOCK_DGRAM,这是执行UDP 的标准方式?

编辑

正如 cmets 中所指出的,我错过了 OP 使用 IPV6 的事实。将所有提及的ARP 替换为neighbor(和等效项)。

【讨论】:

  • 实际上,IPv6 没有 ARP - 它使用 NDP。
  • 确实如此。没注意。但这个理论仍然适用。
  • 是的,确实如此,这就是我投票 +1 的原因。我只是想你可能想更正你对 IPv6 的回答。
  • 谢谢。添加了一些单词。
  • NDP 是 ARP 的一大改进,因为 ARP 广播并中断每台主机,但 NDP 根据 IPv6 地址的最后 24 位多播到特定组,可能只命中它所针对的一个主机正在尝试解析第 2 层地址。
猜你喜欢
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 2012-03-19
  • 2012-10-01
  • 1970-01-01
  • 2021-12-10
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多