简体   繁体   English

C ++ - 构建带有标头的数据包并通过UDP套接字发送它

[英]C++ - Constructing a packet with headers and sending it through a UDP socket

I am trying to build a packet that will be sent via UDP. 我正在尝试构建一个将通过UDP发送的数据包。 However I am not getting the correct data on the receiving side. 但是我没有在接收方获得正确的数据。

In the packet I want to include an IP Header, UDP Header, and the data that needs to be sent. 在数据包中我想要包括IP标头,UDP标头和需要发送的数据。 In this case I just want to send the word "Hello" along with random header information. 在这种情况下,我只想发送单词“Hello”以及随机标题信息。

char *data = "Hello";
char *packet = (char *)malloc(sizeof(struct iphdr) + sizeof(struct udphdr) + strlen(data));
struct iphdr *ip = (struct iphdr*) packet;
struct udphdr *udp = (struct udphdr*) (packet + sizeof(struct iphdr));
char *send_buff = (char *) (packet + sizeof(struct iphdr) + sizeof(struct udphdr));
ip->saddr = inet_addr("1.2.3.4");
ip->daddr = inet_addr("5.6.7.8");
ip->ttl = 5;
udp->source = 5950;
udp->dest = 5950;
udp->len = sizeof(struct udphdr);
udp->check = 0;
strcpy(send_buff, data);

sendto(sock, packet, (sizeof(struct iphdr) + sizeof(struct udphdr) + strlen(data)), ROUTER_IP);

The problem I'm having is that the receiving end just gets random data so I'm assuming the number of bytes is incorrect somewhere. 我遇到的问题是接收端只是获取随机数据所以我假设字节数在某处是不正确的。

On the receiving side I have it print out one of the fields of the IP header as a test, but it's not correct. 在接收方,我打印出IP标头的一个字段作为测试,但它不正确。

char recv_buff[1000];
int recv_bytes = recvfrom(sock, recv_buff, sizeof(recv_buff));
struct iphdr *ip = (struct iphdr*) recv_buff;
cout << static_cast<int16_t>(ip->ttl) << endl;

Am I putting the packet together wrong or is there a problem on the receiving end? 我把数据包放在一起是错误的还是接收端有问题? I used this example http://www.winlab.rutgers.edu/~zhibinwu/html/c_prog.htm as a reference for putting together the packet. 我使用这个例子http://www.winlab.rutgers.edu/~zhibinwu/html/c_prog.htm作为汇总数据包的参考。

You are creating the socket as socket(AF_INET, SOCK_DGRAM, 0); 您正在将套接字创建为socket(AF_INET, SOCK_DGRAM, 0); meaning that it's a datagram (=UDP, typically) socket, so the network stack will automatically include IP header & UDP headers, etc. 这意味着它是一个数据报(通常是UDP)套接字,因此网络堆栈将自动包含IP头和UDP头等。

But since you are trying to create your own IP and UDP headers you must create a raw socket, then send the packet (and also calculate the checksum as your reference code is doing). 但是,既然您正在尝试创建自己的IP和UDP标头,则必须创建一个原始套接字,然后发送数据包(并且还会像参考代码那样计算校验和)。

To create a raw socket, use socket(AF_INET, SOCK_RAW, 0) . 要创建原始套接字,请使用socket(AF_INET, SOCK_RAW, 0)

Besides the problem with not using raw sockets, you also don't set eg port numbers correctly. 除了不使用原始套接字的问题,您也没有正确设置端口号。 The have to be in network byte-order, so you should use eg htons for that. 必须是网络字节顺序,所以你应该使用例如htons There are also other fields that should be in network byte orders. 还有其他字段应该在网络字节顺序中。

if you are using your own ip and udp headers ON TOP of the stack's I hope you are parsing the data after removing both headers of yours and stack's. 如果你在堆栈顶部使用自己的ip和udp头文件,我希望你在删除你的头文件和堆栈之后解析数据。 If the receiving socket is RAW, you will get the ip and udp headers of the stack as well. 如果接收套接字是RAW,您也将获得堆栈的ip和udp标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM