I would like to write a structure with one float member periodically in a for loop to a.pcap file. I want to have as many UDP packets in the resultant .pcap file as the loop iterates.
I've built the code to accomplish the same, however, when I try to load the. pcap file in Wireshark, I get an error. "The captured file appears to have been cut short in the middle of the packet" is the error message I'm receiving.
The code which I'm using is shown below. Can anyone please help?
const char* filename = "data.pcap";
pcap_t* pcap_handle = pcap_open_dead(DLT_RAW, 65535);
pcap_dumper_t* pcap_file_3 = pcap_dump_open(pcap_handle, filename);
void saveData(float data, pcap_dumper_t* pcap_file_1) {
struct timeval tv;
gettimeofday(&tv, NULL);
/* packet header */
struct pcap_pkthdr packet_header;
packet_header.ts = tv;
packet_header.caplen = packet_header.len = sizeof(data) + sizeof(packet_header);
pcap_dump((u_char*)pcap_file_1, &packet_header, (const u_char*)&data);
}
/* for loop which sends the data to the saveData function */
for(size_t i{0U}; i < 300; i++)
{
CODE TO COMPUTE estimated_val
saveData(estimated_val, pcap_file_3);
}
pcap_dump_close(pcap_file_3);
pcap_close(pcap_handle);
caplen
and len
fields in header indicate length of data without length of header itself.
You need
packet_header.caplen = packet_header.len = sizeof(data);