我有一个代码将一个UDP数据报包从linux装备(服务器)发送到另一个(客户端)。这个时候我有一个问题:
如果那时我对客户端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);
}
答案 0 :(得分:3)
如果UDP
表中没有dst mac
,那么您选择发送ARP
数据包的方法似乎就会失败。不知何故,这样做会导致内核arp
不dst mac
(可能是因为你已经手动将其插入数据包) - 但是 - 数据包传输失败了,因为它没有存在于ARP
表中。 ping
导致ARP
表被填充,然后它开始工作。
只是一个理论。我们将不得不查看内核路径来确认这一点。
您可以尝试几个实验来证实这一理论。
ping
。运行该程序几次以查看它是否适用于后续运行。ping
,然后尝试程序(它应该工作),然后刷新ARP
表,再次尝试程序(它应该失败)。使用sudo ip -s -s neigh flush all
刷新ARP
表。您是否有任何理由不使用SOCK_DGRAM
,这是执行UDP
的标准方法?
修改强>
正如评论中指出的那样,我错过了OP正在使用IPV6
的事实。将ARP
的所有提及替换为neighbor
(等效)。