我的服务器将在工作日的某个时间段接收UDP多播数据包。我写了一个c套接字应用程序来接收这些UDP组播数据包。 由于我的服务器只能在一定时间内接收数据包,为了进行测试,我使用tcpdump捕获了数据包。
tcpdump -i enp0s25 -s 1600 -w /home/user/log/capture.pcap
然后,我用tcpreplay重放捕获的数据包。
tcpreplay -i p1p2 --pktlen /home/user/log/capture.pcap
这是'ifconfig'输出。
enp0s25: flags=4163<UP,BROADCAST,RUNNING,MULTICAST mtu 1500
inet 192.168.3.6 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 2400:2410:2a41:2e00:1d6f:2fde:2436:608f prefixlen 64 scopeid 0x0<global
inet6 fe80::555d:f7f2:a114:51df prefixlen 64 scopeid 0x20<link
ether 98:90:96:a7:6f:f3 txqueuelen 1000 (Ethernet)
RX packets 45031 bytes 50053845 (47.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14129 bytes 1784865 (1.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 20 memory 0xf9100000-f9120000
lo: flags=73<UP,LOOPBACK,RUNNING mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
p1p1: flags=4099<UP,BROADCAST,MULTICAST mtu 1500
ether 00:02:c9:5a:77:62 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 218 dropped 207 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
p1p2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST mtu 1500
inet6 fe80::2acf:23a9:3c1a:89a0 prefixlen 64 scopeid 0x20<link
ether 00:02:c9:5a:77:63 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 329583 bytes 50655424 (48.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
p4p1: flags=4099<UP,BROADCAST,MULTICAST mtu 1500
ether 00:0f:53:29:e9:30 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 26
p4p2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST mtu 1500
ether 00:0f:53:29:e9:31 txqueuelen 1000 (Ethernet)
RX packets 329583 bytes 51973756 (49.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 35
我的服务器中安装了三个NIC。其中两个是光纤通道NIC。每个都有两个端口。 p1p1和p1p2是相同的物理网卡,p4p1和p4p2是相同的物理网卡。 我将光缆从p1p2连接到p4p2 ,因此tcpreplay应该将数据包从p1p2发送到p4p2。
然后,我检查了是否有数据包进入p4p2。
tcpdump -i p4p2 -nnn
输出的一部分:
18:05:21.062023 IP 10.10.31.31.3131 > 239.239.31.31.3131: UDP, length 97
18:05:21.062035 IP 10.10.31.31.3131 > 239.239.31.31.3131: UDP, length 97
似乎tcpreplay会将捕获的数据包重新发送到p4p2。但问题是我的袜子应用程序无法接收这些数据包。以下是我的C套接字应用程序的一部分。
#pragma pack(1)
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte
int main() {
int sock, status, socklen;
uint8_t buffer[MAXBUFSIZE];
struct sockaddr_in saddr;
struct ip_mreq imreq;
memset(&saddr, 0x00, sizeof(saddr));
memset(&imreq, 0x00, sizeof(imreq));
memset(buffer, 0x00, MAXBUFSIZE);
// open a UDP socket
sock = socket(PF_INET, SOCK_DGRAM, 0);
if ( sock < 0 ) {
perror("Error creating socket");
return(0);
}
imreq.imr_multiaddr.s_addr = inet_addr("239.239.31.31");
imreq.imr_interface.s_addr = htonl(INADDR_ANY); // use DEFAULT interface
// JOIN multicast group on default interface
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&imreq, sizeof(struct ip_mreq));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(3131);
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 ) {
perror("Error binding socket to interface");
return(0);
}
socklen = sizeof(struct sockaddr_in);
// receive 500 packets from socket
int m = 0;
for (; m < 500; m++) {
status = recvfrom(sock, buffer, MAXBUFSIZE, 0, (struct sockaddr *)&saddr, &socklen);
if(status < 0) {
perror("receive error!\n");
} else if (status > 0) {
// DO sth usefully here.
}
}
// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);
return 0;
}
当我运行上面的应用程序时,它将停止在recvfrom函数。谁能告诉我为什么我的应用程序无法接收这些数据包?
答案 0 :(得分:0)
根据tcpreplay文档,不可能在同一台计算机上发送数据包:数据包是在TCP / IP堆栈和网卡的设备驱动程序之间注入的,因此运行tcpreplay的TCP / IP堆栈永远不会看到数据包。 尝试在同一台机器上的虚拟化环境中运行tcpreplay,然后主机操作系统应该看到数据包。