生成数据包
我想知道如何在c中生成数据包。假设我们有一个类型如下:
struct ipheader {
int version;
int hdLen;
int tos;
int totLen;
int id;
......
int dstIp;
}
我们有一个 ipheader 类型:
struct ipheader ip;
//init the ip
.....
如何从“ip”生成一个数据包(只是 ip 标头部分)。谁能告诉我怎么做?
我想知道如何生成包含 mac 地址、ip 标头、tcp 标头、有效负载等信息的数据包。然后我可以使用“pcap_sendpacket”函数发送我生成的数据包。有人可以给我举个小例子吗?
I want to know how to generate a packet in c. Supposed we have a type as follows:
struct ipheader {
int version;
int hdLen;
int tos;
int totLen;
int id;
......
int dstIp;
}
And we have a ipheader type:
struct ipheader ip;
//init the ip
.....
How can I generate a packet(just ip header part) from "ip". Could anyone show me how?
I want to know to generate packets with information such as mac address, ip header, tcp header, payload. Then I can use the "pcap_sendpacket" function to send the packets I have generated. Could someone give me a little example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建自定义 ip 数据包,如下所示。下面的代码片段还创建了 ip 数据包内的自定义 TCP 部分。此外,自定义函数校验和会生成数据包的校验和。您可以使用 rawsocket 将此数据包直接发送到网络。我认为代码很简单并且不言自明。如果您有任何疑问,请告诉我。
You can create custom ip packet as below. The below code snippet also makes the custom TCP part which is inside the ip packet. Also the custom function checksum generates the checksum for the packet. You can use rawsocket to send this packet directly to the network. I think the code is easy and self explanatory. Let me know if you have any queries.
由于您没有指定详细信息(对我来说也看起来像家庭作业),我所能做的就是给出指示。
因此,如果您正确构建结构并使用原始套接字,则只需将此结构写入套接字即可。
Since you haven't specified the details (looks suspiciously like homework to me as well), all i can do is give pointers.
So, provided you properly structure your struct and use raw sockets, you only need to write this struct to the socket.