@@ -492,75 +492,59 @@ namespace pcpp
492492 if (m_LightPcapNg == nullptr )
493493 {
494494 PCPP_LOG_ERROR (" Pcapng file device '" << m_FileName << " ' not opened" );
495- return " " ;
495+ return {} ;
496496 }
497497
498498 light_pcapng_file_info* fileInfo = light_pcang_get_file_info (toLightPcapNgT (m_LightPcapNg));
499- if (fileInfo == nullptr )
500- return " " ;
501- char * res = fileInfo->os_desc ;
502- size_t len = fileInfo->os_desc_size ;
503- if (len == 0 || res == nullptr )
504- return " " ;
505-
506- return std::string (res, len);
499+ if (fileInfo == nullptr || fileInfo->os_desc == nullptr || fileInfo->os_desc_size == 0 )
500+ return {};
501+
502+ return std::string (fileInfo->os_desc , fileInfo->os_desc_size );
507503 }
508504
509505 std::string PcapNgFileReaderDevice::getHardware () const
510506 {
511507 if (m_LightPcapNg == nullptr )
512508 {
513509 PCPP_LOG_ERROR (" Pcapng file device '" << m_FileName << " ' not opened" );
514- return " " ;
510+ return {} ;
515511 }
516512
517513 light_pcapng_file_info* fileInfo = light_pcang_get_file_info (toLightPcapNgT (m_LightPcapNg));
518- if (fileInfo == nullptr )
519- return " " ;
520- char * res = fileInfo->hardware_desc ;
521- size_t len = fileInfo->hardware_desc_size ;
522- if (len == 0 || res == nullptr )
523- return " " ;
524-
525- return std::string (res, len);
514+ if (fileInfo == nullptr || fileInfo->hardware_desc == nullptr || fileInfo->hardware_desc_size == 0 )
515+ return {};
516+
517+ return std::string (fileInfo->hardware_desc , fileInfo->hardware_desc_size );
526518 }
527519
528520 std::string PcapNgFileReaderDevice::getCaptureApplication () const
529521 {
530522 if (m_LightPcapNg == nullptr )
531523 {
532524 PCPP_LOG_ERROR (" Pcapng file device '" << m_FileName << " ' not opened" );
533- return " " ;
525+ return {} ;
534526 }
535527
536528 light_pcapng_file_info* fileInfo = light_pcang_get_file_info (toLightPcapNgT (m_LightPcapNg));
537- if (fileInfo == nullptr )
538- return " " ;
539- char * res = fileInfo->user_app_desc ;
540- size_t len = fileInfo->user_app_desc_size ;
541- if (len == 0 || res == nullptr )
542- return " " ;
543-
544- return std::string (res, len);
529+ if (fileInfo == nullptr || fileInfo->user_app_desc == nullptr || fileInfo->user_app_desc_size == 0 )
530+ return {};
531+
532+ return std::string (fileInfo->user_app_desc , fileInfo->user_app_desc_size );
545533 }
546534
547535 std::string PcapNgFileReaderDevice::getCaptureFileComment () const
548536 {
549537 if (m_LightPcapNg == nullptr )
550538 {
551539 PCPP_LOG_ERROR (" Pcapng file device '" << m_FileName << " ' not opened" );
552- return " " ;
540+ return {} ;
553541 }
554542
555543 light_pcapng_file_info* fileInfo = light_pcang_get_file_info (toLightPcapNgT (m_LightPcapNg));
556- if (fileInfo == nullptr )
557- return " " ;
558- char * res = fileInfo->file_comment ;
559- size_t len = fileInfo->file_comment_size ;
560- if (len == 0 || res == nullptr )
561- return " " ;
562-
563- return std::string (res, len);
544+ if (fileInfo == nullptr || fileInfo->file_comment == nullptr || fileInfo->file_comment_size == 0 )
545+ return {};
546+
547+ return std::string (fileInfo->file_comment , fileInfo->file_comment_size );
564548 }
565549
566550 // ~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -867,38 +851,6 @@ namespace pcpp
867851 m_CompressionLevel = compressionLevel;
868852 }
869853
870- bool PcapNgFileWriterDevice::open (const std::string& os, const std::string& hardware, const std::string& captureApp,
871- const std::string& fileComment)
872- {
873- if (m_LightPcapNg != nullptr )
874- {
875- PCPP_LOG_DEBUG (" Pcap-ng descriptor already opened. Nothing to do" );
876- return true ;
877- }
878-
879- m_NumOfPacketsNotWritten = 0 ;
880- m_NumOfPacketsWritten = 0 ;
881-
882- light_pcapng_file_info* info =
883- light_create_file_info (os.c_str (), hardware.c_str (), captureApp.c_str (), fileComment.c_str ());
884-
885- m_LightPcapNg = toLightPcapNgHandle (light_pcapng_open_write (m_FileName.c_str (), info, m_CompressionLevel));
886- if (m_LightPcapNg == nullptr )
887- {
888- PCPP_LOG_ERROR (" Error opening file writer device for file '"
889- << m_FileName << " ': light_pcapng_open_write returned nullptr" );
890-
891- light_free_file_info (info);
892-
893- m_DeviceOpened = false ;
894- return false ;
895- }
896-
897- m_DeviceOpened = true ;
898- PCPP_LOG_DEBUG (" pcap-ng writer device for file '" << m_FileName << " ' opened successfully" );
899- return true ;
900- }
901-
902854 bool PcapNgFileWriterDevice::writePacket (RawPacket const & packet, const std::string& comment)
903855 {
904856 if (m_LightPcapNg == nullptr )
@@ -955,6 +907,30 @@ namespace pcpp
955907
956908 bool PcapNgFileWriterDevice::open ()
957909 {
910+ return openWrite ();
911+ }
912+
913+ bool PcapNgFileWriterDevice::open (bool appendMode)
914+ {
915+ return appendMode ? openAppend () : openWrite ();
916+ }
917+
918+ bool PcapNgFileWriterDevice::open (const std::string& os, const std::string& hardware, const std::string& captureApp,
919+ const std::string& fileComment)
920+ {
921+ PcapNgMetadata metadata;
922+ metadata.os = os;
923+ metadata.hardware = hardware;
924+ metadata.captureApplication = captureApp;
925+ metadata.comment = fileComment;
926+ return openWrite (&metadata);
927+ }
928+
929+ bool PcapNgFileWriterDevice::openWrite (PcapNgMetadata const * metadata)
930+ {
931+ // TODO: Ambiguity in the API
932+ // If the user calls open() and then open(true) - should we close the first one or report failure?
933+ // Currently the method reports a success, but the opened device would not match the appendMode.
958934 if (m_LightPcapNg != nullptr )
959935 {
960936 PCPP_LOG_DEBUG (" Pcap-ng descriptor already opened. Nothing to do" );
@@ -964,7 +940,16 @@ namespace pcpp
964940 m_NumOfPacketsNotWritten = 0 ;
965941 m_NumOfPacketsWritten = 0 ;
966942
967- light_pcapng_file_info* info = light_create_default_file_info ();
943+ light_pcapng_file_info* info;
944+ if (metadata == nullptr )
945+ {
946+ info = light_create_default_file_info ();
947+ }
948+ else
949+ {
950+ info = light_create_file_info (metadata->os .c_str (), metadata->hardware .c_str (),
951+ metadata->captureApplication .c_str (), metadata->comment .c_str ());
952+ }
968953
969954 m_LightPcapNg = toLightPcapNgHandle (light_pcapng_open_write (m_FileName.c_str (), info, m_CompressionLevel));
970955 if (m_LightPcapNg == nullptr )
@@ -983,10 +968,16 @@ namespace pcpp
983968 return true ;
984969 }
985970
986- bool PcapNgFileWriterDevice::open ( bool appendMode )
971+ bool PcapNgFileWriterDevice::openAppend ( )
987972 {
988- if (!appendMode)
989- return open ();
973+ // TODO: Ambiguity in the API
974+ // If the user calls open() and then open(true) - should we close the first one or report failure?
975+ // Currently the method reports a success, but the opened device would not match the appendMode.
976+ if (m_LightPcapNg != nullptr )
977+ {
978+ PCPP_LOG_DEBUG (" Pcap-ng descriptor already opened. Nothing to do" );
979+ return true ;
980+ }
990981
991982 m_NumOfPacketsNotWritten = 0 ;
992983 m_NumOfPacketsWritten = 0 ;
0 commit comments