C – 将结构写入文件(.pcap)
发布时间:2020-12-16 03:35:20 所属栏目:百科 来源:网络整理
导读:我正在尝试编写一个.pcap文件,这可以在Wireshark中使用. 为了做到这一点,我有一些结构与各种数据类型,我需要写入一个文件. (见代码) 所以,我创建结构体实例,填写数据,使用FILE * fp = fopen(“test.pcap”,“w”),然后我不确定如何正确地写入文件.我相信我应
我正在尝试编写一个.pcap文件,这可以在Wireshark中使用.
为了做到这一点,我有一些结构与各种数据类型,我需要写入一个文件. (见代码) 所以,我创建结构体实例,填写数据,使用FILE * fp = fopen(“test.pcap”,“w”),然后我不确定如何正确地写入文件.我相信我应该使用memcpy,但我不知道做最好的方法.我以前主要是使用C图书馆来做到这一点.有什么建议么? typedef struct pcap_hdr_s { uint32_t magic_number; /* magic number */ uint16_t version_major; /* major version number */ uint16_t version_minor; /* minor version number */ int32_t thiszone; /* GMT to local correction */ uint32_t sigfigs; /* accuracy of timestamps */ uint32_t snaplen; /* max length of captured packets,in octets */ uint32_t network; /* data link type */ } pcap_hdr_t; typedef struct pcaprec_hdr_s { uint32_t ts_sec; /* timestamp seconds */ uint32_t ts_usec; /* timestamp microseconds */ uint32_t incl_len; /* number of octets of packet saved in file */ uint32_t orig_len; /* actual length of packet */ } pcaprec_hdr_t; typedef struct ethernet_hdr_s { uint8_t dst[6]; /* destination host address */ uint8_t src[6]; /* source host address */ uint16_t type; /* IP? ARP? RARP? etc */ } ethernet_hdr_t; typedef struct ip_hdr_s { uint8_t ip_hl:4,/* both fields are 4 bits */ ip_v:4; uint8_t ip_tos; uint16_t ip_len; uint16_t ip_id; uint16_t ip_off; uint8_t ip_ttl; uint8_t ip_p; uint16_t ip_sum; uint32_t ip_src; uint32_t ip_dst; }ip_hdr_t; typedef struct udp_header { uint16_t src; uint16_t dst; uint16_t length; uint16_t checksum; } udp_header_t; 解决方法
使用
fwrite().你需要检查这个信息,但我认为.pcap文件是用二进制模式写的.
例: pcaprec_hdr_t pcaprec_hdr; // fill pcaprec_hdr with valid info FILE* pFile = NULL; pFile = fopen ("myfile.pcap","wb"); // open for writing in binary mode fwrite (&pcaprec_hdr,1,sizeof(pcaprec_hdr_t),pFile); fclose(pFile); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |