Skip to content

Commit 9d40f4c

Browse files
authored
Merge pull request #43 from space-jam-/fix-extract-merge
Fix extract merge
2 parents 35d73eb + ac0159a commit 9d40f4c

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

tools/data_structs/pcap_buff.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,11 @@ buff_error_t pcap_buff_from_file(pcap_buff_t* pcap_buff, char* filename)
4646
return BUFF_ENONE;
4747
}
4848

49-
pkt_info_t pcap_buff_next_packet(pcap_buff_t* pcap_buff)
49+
pkt_info_t pcap_buff_get_info(pcap_buff_t* pcap_buff)
5050
{
51-
pcap_pkthdr_t* curr_hdr = pcap_buff->hdr;
52-
53-
if(curr_hdr == NULL){
51+
if(pcap_buff->hdr == NULL){
5452
pcap_buff->hdr = (pcap_pkthdr_t*)(pcap_buff->_buff->data + sizeof(pcap_file_header_t));
5553
pcap_buff->idx = 0;
56-
} else {
57-
const int64_t curr_cap_len = curr_hdr->caplen;
58-
pcap_pkthdr_t* next_hdr = (pcap_pkthdr_t*)((char*)(curr_hdr+1) + curr_cap_len);
59-
pcap_buff->hdr = next_hdr;
60-
pcap_buff->idx++;
6154
}
6255

6356
pcap_buff->pkt = (char*)(pcap_buff->hdr + 1);
@@ -103,6 +96,23 @@ pkt_info_t pcap_buff_next_packet(pcap_buff_t* pcap_buff)
10396
return PKT_OK;
10497
}
10598

99+
pkt_info_t pcap_buff_next_packet(pcap_buff_t* pcap_buff)
100+
{
101+
pcap_pkthdr_t* curr_hdr = pcap_buff->hdr;
102+
103+
if(curr_hdr == NULL){
104+
pcap_buff->hdr = (pcap_pkthdr_t*)(pcap_buff->_buff->data + sizeof(pcap_file_header_t));
105+
pcap_buff->idx = 0;
106+
} else {
107+
const int64_t curr_cap_len = curr_hdr->caplen;
108+
pcap_pkthdr_t* next_hdr = (pcap_pkthdr_t*)((char*)(curr_hdr+1) + curr_cap_len);
109+
pcap_buff->hdr = next_hdr;
110+
pcap_buff->idx++;
111+
}
112+
113+
return pcap_buff_get_info(pcap_buff);
114+
}
115+
106116
bool pcap_buff_eof(pcap_buff_t* pcap_buff)
107117
{
108118
buff_t* buff = pcap_buff->_buff;

tools/data_structs/pcap_buff.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ buff_error_t pcap_buff_init(char* filename, int64_t snaplen, int64_t max_filesiz
3232
/* The underlying buff is read only */
3333
buff_error_t pcap_buff_from_file(pcap_buff_t* pcap_buff, char* filename);
3434

35-
/* Adjust hdr, data, idx, ftr to point to the next packet */
35+
/* Return information about the current packet */
36+
pkt_info_t pcap_buff_get_info(pcap_buff_t* pcap_buff);
37+
38+
/* Adjust hdr, data, idx, ftr to point to the next packet and return packet information */
3639
pkt_info_t pcap_buff_next_packet(pcap_buff_t* pcap_buff);
3740

3841
/* Get the filename used by the buff_t. */

tools/exact-pcap-extract.c

+15-23
Original file line numberDiff line numberDiff line change
@@ -463,45 +463,29 @@ int main (int argc, char** argv)
463463
i64 count = 0;
464464
for(int i = 0; !lstop ; i++)
465465
{
466-
begin_loop:
467466
ch_log_debug1("\n%i ######\n", i );
468467

469-
/* Check all the FD in case we've read everything */
470-
ch_log_debug1("Checking for EOF\n");
471-
bool all_eof = true;
472-
for(int i = 0; i < rd_buffs_count; i++){
473-
all_eof &= pcap_buff_eof(&rd_buffs[i]);
474-
}
475-
if(all_eof){
476-
ch_log_info("All files empty, exiting now\n");
477-
break;
478-
}
479-
468+
int32_t eof_buffs = 0;
480469
ch_log_debug1("Looking for minimum timestamp index on %i buffers\n",
481470
rd_buffs_count);
482471
/* Find the read buffer with the earliest timestamp */
483472
int64_t min_idx = 0;
484473
for(int buff_idx = 0; buff_idx < rd_buffs_count; buff_idx++ ){
485-
if(pcap_buff_eof(&rd_buffs[buff_idx])){
486-
if(min_idx == buff_idx){
487-
min_idx = buff_idx+1;
488-
}
489-
continue;
490-
}
491-
492-
pkt_info = pcap_buff_next_packet(&rd_buffs[buff_idx]);
474+
pkt_info = pcap_buff_get_info(&rd_buffs[buff_idx]);
493475
const char* cur_filename = pcap_buff_get_filename(&rd_buffs[buff_idx]);
494476
const uint64_t pkt_idx = rd_buffs[buff_idx].idx;
495477

496478
switch(pkt_info){
497479
case PKT_PADDING:
498480
ch_log_debug1("Skipping over packet %i (buffer %i) because len=0\n", pkt_idx, buff_idx);
481+
pcap_buff_next_packet(&rd_buffs[buff_idx]);
499482
dropped_padding++;
500483
buff_idx--;
501484
continue;
502485
case PKT_RUNT:
503486
if(options.skip_runts){
504487
ch_log_debug1("Skipping over runt frame %i (buffer %i) \n", pkt_idx, buff_idx);
488+
pcap_buff_next_packet(&rd_buffs[buff_idx]);
505489
dropped_runts++;
506490
buff_idx--;
507491
continue;
@@ -510,17 +494,23 @@ int main (int argc, char** argv)
510494
case PKT_ERROR:
511495
ch_log_debug1("Skipping over damaged packet %i (buffer %i) because flags = 0x%02x\n",
512496
pkt_idx, buff_idx, rd_buffs[buff_idx].ftr->flags);
497+
pcap_buff_next_packet(&rd_buffs[buff_idx]);
513498
dropped_errors++;
514499
buff_idx--;
515500
continue;
516501
case PKT_EOF:
517502
ch_log_debug1("End of file \"%s\"\n", cur_filename);
518-
goto begin_loop;
519-
break;
503+
eof_buffs++;
504+
505+
/* All buffers are EOF, exit main loop. */
506+
if(eof_buffs == rd_buffs_count){
507+
goto extract_done;
508+
}
509+
continue;
520510
case PKT_OVER_SNAPLEN:
521511
ch_log_fatal("Packet with index %d (%s) does not comply with snaplen: %d (data len is %d)\n",
522512
pkt_idx, cur_filename, rd_buffs[buff_idx].snaplen, rd_buffs[buff_idx].hdr->len);
523-
case PKT_SNAPPED:
513+
case PKT_SNAPPED: // Fall through
524514
if(options.verbose){
525515
ch_log_warn("Packet has been snapped shorter (%d) than it's wire length (%d) [%s].\n",
526516
rd_buffs[buff_idx].hdr->caplen, rd_buffs[buff_idx].hdr->len, cur_filename);
@@ -602,10 +592,12 @@ int main (int argc, char** argv)
602592

603593
packets_total++;
604594
count++;
595+
pcap_buff_next_packet(&rd_buffs[min_idx]);
605596
if(options.max_count && count >= options.max_count){
606597
break;
607598
}
608599
}
600+
extract_done:
609601

610602
ch_log_info("Finished writing %li packets total (Runts=%li, Errors=%li, Padding=%li). Closing\n", packets_total, dropped_runts, dropped_errors, dropped_padding);
611603

0 commit comments

Comments
 (0)