@@ -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