c - 为什么路由器不转发我的数据包?

标签 c linux networking

我使用原始套接字将 TCP 数据包从计算机 B 发送到计算机 A。

首先,我创建一个套接字

int s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);

并告诉内核不要为我填充 ip header,因为我的数据包括 ip header 和 tcp header

int optval = 1;
setsockopt(s, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(optval));

并设置目标地址

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(33333);
addr.sin_addr.s_addr = inet_addr("192.168.200.135");

并发送数据

sendto(s, data, len, 0, (struct sockaddr*)&addr, sizeof(addr));

我的网络环境如下图 network environment

在情况 A 中,两台计算机通过交换机连接。而在案例B中,计算机B连接到路由器,路由器连接到交换机。

我的程序在案例 A 中运行良好。我可以使用原始套接字在计算机 A 上接收数据包,并可以使用 wireshark 捕获该数据包。

在案例 B 中,我可以从计算机 B ping 到计算机 A,并从 B 到 A。我可以在计算机 A 上使用原始套接字和 wireshark 来捕获 B 和 A 之间发送的一些 TCP 数据包。但是,我在计算机 B 上的原始套接字发送的数据包无法在 A 上捕获。路由器似乎没有转发我的数据包。我的包裹有问题吗?

我的发送包数据的程序如下:

//data to send
char data[48] =
{
    //iphdr:
    0x45, 0x00, 0x00, 0x30, //version:4 header_length:5 TOS:0 length:0x30
    0x00, 0x01, 0x00, 0x00, //identification:1 flags:0 fragment_offset:0
    0x40, 0x06, 0x2f, 0x47, //TTL:0x40 protocol:6(TCP) header_checksum:0x2f47
    0xc0, 0xa8, 0x01, 0xa8, //source_IP:192.168.1.168
    0xc0, 0xa8, 0xc8, 0x87, //destination_IP:192.168.200.135
    //tcphdr:
    0x56, 0xce, 0x82, 0x35, //source_port:22222 destination_port:33333
    0x00, 0x00, 0x00, 0x00, //sequence_number:0
    0x00, 0x00, 0x00, 0x00, //ack_number:0
    0x50, 0x00, 0xaa, 0xaa, //header_length:5 window_size:0xaaaa
    0xeb, 0xbd, 0x00, 0x00, //checksum:0xebbd urgent_pointer:0
    //tcp content:
    0x7a, 0x68, 0x73, 0x00,
    0x39, 0x30, 0xce, 0x56, 
};
int len = 48;
//open the socket
int s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
//don't fill header
int optval = 1;
setsockopt(s, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(optval));
//destination addr
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("192.168.200.135");
//send
sendto(s, data, len, 0, (struct sockaddr*)&addr, sizeof(addr));

最佳答案

  1. 路由器 192.168.1.1 应该是您的默认路由器。检查机器 A 中的配置。

关于c - 为什么路由器不转发我的数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17485475/

相关文章:

c - 使用 proc 的内核模块程序

c# - 使用 P/Invoke Interop Assistant 时的数据结构和堆栈损坏

C破解: replace printf to collect output and return complete string by using a line buffer

linux - 如何限制一组docker容器的内存资源?

linux - linux 上的 qt openGL 支持

python - 如果 api 调用失败则重新运行执行 - [Python2.7]

unit-testing - Robolectric:在测试中模拟网络错误

c++ - 使用二进制空间划分树的多边形测试中的快速点

linux - 汇编语言编译器

python - 使用 python 脚本将文件复制到网络路径