gpt4 book ai didi

c - 套接字应用程序无法接收 tcpreplay 发送的数据包

转载 作者:太空狗 更新时间:2023-10-29 12:24:59 28 4
gpt4 key购买 nike

我的服务器会在工作日的某个时间段接收到 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

我的服务器安装了三个网卡。其中两个是光纤 channel 网卡。他们每个人都有两个端口。 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。但问题是我的 socks 应用程序无法接收这些数据包。以下是我的 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 函数处停止。谁能告诉我为什么我的应用程序无法接收这些数据包?

最佳答案

根据 tcpreplay 文档,不可能在同一台计算机上发送数据包:数据包在 TCP/IP 堆栈和网卡的设备驱动程序之间注入(inject),因此运行 tcpreplay 的 TCP/IP 堆栈永远看不到数据包。尝试在同一台机器上的虚拟化环境中运行 tcpreplay,然后主机操作系统应该可以看到数据包。

引用:http://tcpreplay.synfin.net/wiki/FAQ

关于c - 套接字应用程序无法接收 tcpreplay 发送的数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745198/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com