From c967b688ffec2a7878632e78cce293902748bc3f Mon Sep 17 00:00:00 2001 From: Merlin Chlosta Date: Thu, 4 Nov 2021 09:03:04 +0000 Subject: [PATCH] emergency handler for NAS PCAP to close file Previously NAS PCAP wasn't closed/fflushed on unclean exit, resulting in missing or corrupted PCAP. --- lib/include/srsran/common/nas_pcap.h | 13 ++++--------- lib/src/common/nas_pcap.cc | 13 +++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/include/srsran/common/nas_pcap.h b/lib/include/srsran/common/nas_pcap.h index c1bc5fd76..f93c17a27 100644 --- a/lib/include/srsran/common/nas_pcap.h +++ b/lib/include/srsran/common/nas_pcap.h @@ -22,22 +22,17 @@ namespace srsran { class nas_pcap { public: - nas_pcap() - { - enable_write = false; - ue_id = 0; - pcap_file = NULL; - } + nas_pcap(); void enable(); uint32_t open(std::string filename_, uint32_t ue_id = 0, srsran_rat_t rat_type = srsran_rat_t::lte); void close(); void write_nas(uint8_t* pdu, uint32_t pdu_len_bytes); private: - bool enable_write; + bool enable_write = false; std::string filename; - FILE* pcap_file; - uint32_t ue_id; + FILE* pcap_file = nullptr; + uint32_t ue_id = 0; void pack_and_write(uint8_t* pdu, uint32_t pdu_len_bytes); }; diff --git a/lib/src/common/nas_pcap.cc b/lib/src/common/nas_pcap.cc index 6d45d2a09..1011d0d12 100644 --- a/lib/src/common/nas_pcap.cc +++ b/lib/src/common/nas_pcap.cc @@ -13,10 +13,22 @@ #include "srsran/common/nas_pcap.h" #include "srsran/common/pcap.h" #include "srsran/srsran.h" +#include "srsran/support/emergency_handlers.h" #include namespace srsran { +/// Try to flush the contents of the pcap class before the application is killed. +static void emergency_cleanup_handler(void* data) +{ + reinterpret_cast(data)->close(); +} + +nas_pcap::nas_pcap() +{ + add_emergency_cleanup_handler(emergency_cleanup_handler, this); +} + void nas_pcap::enable() { enable_write = true; @@ -42,6 +54,7 @@ void nas_pcap::close() { fprintf(stdout, "Saving NAS PCAP file (DLT=%d) to %s \n", NAS_LTE_DLT, filename.c_str()); DLT_PCAP_Close(pcap_file); + pcap_file = nullptr; } void nas_pcap::write_nas(uint8_t* pdu, uint32_t pdu_len_bytes)