【发布时间】:2014-03-31 21:56:51
【问题描述】:
假设我们有一个包含数据包信息的 XML 文件
<packet>
<frame_type>Ethernet</frame_type>
<local_mac_address>00-21-85-11-29-1b</local_mac_address>
<remote_mac_address>ff-ff-ff-ff-ff</remote_mac_address>
<protocol>IP</protocol>
<version>4</version>
<local_address>147.175.106.141</local_address>
<remote_address>255.255.255.255</remote_address>
<protocol_type>UDP</protocol_type>
<protocol>UDP</protocol>
<local_port>17500</local_port>
<remote_port>17500</remote_port>
<service_name></service_name>
<packets>8</packets>
</packet>
我可以使用 pugiXML 或其他一些 XML 解析器轻松解析。
使用纯 C++ 生成此类数据包的方法是什么(以正确的顺序获取数据包信息)并使用 pcap.h 中声明的函数将其保存到wireshark 可读的文件中?
pcap_dump(dumpfile, header, pkt_data);
u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data;
我应该如何使用纯 C++ 填充 pkt_data 和 header?
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
是否有我应该设置到ts、caplen、len 的数据示例?
编辑
所以经过一段时间的谷歌搜索后,我在 SO 上发现了这个 thread。
所以我使用这些结构来填充我的以太网 -> IP -> TCP 数据包,如下所示
我不熟悉 uint16_t、uint8_t 等类型。
pcap_hdr_t pcaphdr;
pcaphdr.magic_number = 0xd4c3b2a1; //0xa1b2c3d4 || 0xd4c3b2a1 <- i am on winwows (Little endian)
pcaphdr.sigfigs = 0;
pcaphdr.version_major = 2;
pcaphdr.version_minor = 4;
pcaphdr.snaplen = 65536;
pcaphdr.thiszone = 0;
pcaphdr.network = DLT_EN10MB;
ethernet_hdr_t ethernethdr;
ethernethdr.dst = ??; // I have no clue how to fill this either ...dst[0] = 0xFF? type is uint8_t.
ethernethdr.src = ??;//same as above
ethernethdr.type = 2048; //? //(I want to use IP = 0x800), it is uint16_t
//and for IP
ip_hdr_t ipp;
ipp.ip_dst = parseIPV4string(ipAddressString); //this function converts string into uint32_t
ipp.ip_src = parseIPV4string(ipAddressString);
ipp.ip_v = 4; //version
ipp.ip_hl = 20; //header length
ipp.ip_id = 12758; //id whatever id
ipp.ip_ttl = 125; //time to live
ipp.ip_p = 6; //protocol 6 = TCP
ipp.ip_off = 0;
ipp.ip_tos = 0;
//and save all this by
FILE *ptr_myfile;
ptr_myfile=fopen("test.pcap", "wb");
if (!ptr_myfile)
{
printf("Unable to open file!");
return 1;
}
fwrite(&pcaphdr, 1, sizeof(pcap_hdr_t), ptr_myfile);
fwrite(ðernethdr, 1, sizeof(ethernet_hdr_t), ptr_myfile);
fwrite(&ipp, 1, sizeof(ip_hdr_t), ptr_myfile);
fclose(ptr_myfile);
我不希望创建带有有效负载(数据)的数据包,我正在尝试处理没有数据的纯数据包 + 在 Wireshark 中检查此数据包。
【问题讨论】:
-
您想将 XML 数据包转换为 pcap 数据包吗?除非您的示例中未显示整个二进制数据/十六进制转储,否则确实没有足够的信息来执行此操作。您可以使用您获得的信息为以太网/IP/UDP 创建合成标头,但是没有数据可以放入 UDP 部分,所以它有点毫无意义。
-
我知道没有数据,我试图了解如何将数据包数据放入 u_char 变量(更多 C 的东西),但总的来说我试图理解 pcap_pkthdr 结构的概念(我知道有一个手动文件,但不清楚)winpcap.org/docs/docs_41b5/html/structpcap__pkthdr.html
-
"
Ethernet2 " 如果每个数据包都有不同的帧类型,则不能使用 pcap,而必须使用 pcap-ng。 -
可以是以太网也可以是802.3