From 73f706bc0e88584302c953fbfa64a579cdb3a029 Mon Sep 17 00:00:00 2001 From: merttozer Date: Wed, 24 Apr 2024 18:18:33 +0300 Subject: [PATCH 1/2] Refactor MBMS-GW and HSS modules for enhanced efficiency, modularity, and compliance with modern C++ standards --- srsepc/hdr/hss/hss.h | 32 ++++++------ srsepc/hdr/mbms-gw/mbms-gw.h | 14 ++--- srsepc/src/hss/hss.cc | 52 ++++++++----------- srsepc/src/main.cc | 2 +- srsepc/src/mbms-gw/main.cc | 2 +- srsepc/src/mbms-gw/mbms-gw.cc | 90 +++++++++++++++------------------ test/50prb_screenlog_srsepc.log | 0 7 files changed, 89 insertions(+), 103 deletions(-) create mode 100644 test/50prb_screenlog_srsepc.log diff --git a/srsepc/hdr/hss/hss.h b/srsepc/hdr/hss/hss.h index 356ef398a8..ba1ca850fa 100644 --- a/srsepc/hdr/hss/hss.h +++ b/srsepc/hdr/hss/hss.h @@ -69,21 +69,21 @@ struct hss_ue_ctx_t { // Helper getters/setters void set_sqn(const uint8_t* sqn_); void set_last_rand(const uint8_t* rand_); - void get_last_rand(uint8_t* rand_); + void get_last_rand(uint8_t* rand_) const; }; class hss : public hss_interface_nas { public: - static hss* get_instance(void); - static void cleanup(void); - int init(hss_args_t* hss_args); - void stop(void); + static hss* get_instance(); + static void cleanup(); + int init(const hss_args_t& hss_args); + void stop(); - virtual bool gen_auth_info_answer(uint64_t imsi, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres); - virtual bool gen_update_loc_answer(uint64_t imsi, uint8_t* qci); + bool gen_auth_info_answer(uint64_t imsi, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres) override; + bool gen_update_loc_answer(uint64_t imsi, uint8_t* qci) override; - virtual bool resync_sqn(uint64_t imsi, uint8_t* auts); + bool resync_sqn(uint64_t imsi, uint8_t* auts) override; std::map get_ip_to_imsi() const; @@ -92,26 +92,28 @@ class hss : public hss_interface_nas virtual ~hss(); static hss* m_instance; + static std::mutex m_mutex; + std::map > m_imsi_to_ue_ctx; void gen_rand(uint8_t rand_[16]); void - gen_auth_info_answer_milenage(hss_ue_ctx_t* ue_ctx, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres); + gen_auth_info_answer_milenage(hss_ue_ctx_t* ue_ctx, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres); void gen_auth_info_answer_xor(hss_ue_ctx_t* ue_ctx, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres); void resync_sqn_milenage(hss_ue_ctx_t* ue_ctx, uint8_t* auts); void resync_sqn_xor(hss_ue_ctx_t* ue_ctx, uint8_t* auts); - void get_uint_vec_from_hex_str(const std::string& key_str, uint8_t* key, uint len); + void get_uint_vec_from_hex_str(const std::string& key_str, uint8_t* key, uint len); void increment_ue_sqn(hss_ue_ctx_t* ue_ctx); void increment_seq_after_resync(hss_ue_ctx_t* ue_ctx); void increment_sqn(uint8_t* sqn, uint8_t* next_sqn); - bool set_auth_algo(std::string auth_algo); - bool read_db_file(std::string db_file); - bool write_db_file(std::string db_file); + bool set_auth_algo(const std::string& auth_algo); + bool read_db_file(const std::string& db_file); + bool write_db_file(const std::string& db_file); hss_ue_ctx_t* get_ue_ctx(uint64_t imsi); std::string hex_string(uint8_t* hex, int size); @@ -137,9 +139,9 @@ inline void hss_ue_ctx_t::set_last_rand(const uint8_t* last_rand_) memcpy(last_rand, last_rand_, 16); } -inline void hss_ue_ctx_t::get_last_rand(uint8_t* last_rand_) +inline void hss_ue_ctx_t::get_last_rand(uint8_t* last_rand_) const { memcpy(last_rand_, last_rand, 16); } } // namespace srsepc -#endif // SRSEPC_HSS_H +#endif // SRSEPC_HSS_H \ No newline at end of file diff --git a/srsepc/hdr/mbms-gw/mbms-gw.h b/srsepc/hdr/mbms-gw/mbms-gw.h index 5ff701b7c6..5389d9f324 100644 --- a/srsepc/hdr/mbms-gw/mbms-gw.h +++ b/srsepc/hdr/mbms-gw/mbms-gw.h @@ -60,9 +60,9 @@ struct pseudo_hdr { class mbms_gw : public srsran::thread { public: - static mbms_gw* get_instance(void); - static void cleanup(void); - int init(mbms_gw_args_t* args); + static mbms_gw* get_instance(); + static void cleanup(); + int init(const mbms_gw_args_t& args); void stop(); void run_thread(); @@ -70,10 +70,12 @@ class mbms_gw : public srsran::thread /* Methods */ mbms_gw(); virtual ~mbms_gw(); - static mbms_gw* m_instance; - int init_sgi_mb_if(mbms_gw_args_t* args); - int init_m1_u(mbms_gw_args_t* args); + static mbms_gw* m_instance; + static std::mutex m_mutex; + + int init_sgi_mb_if(const mbms_gw_args_t& args); + int init_m1_u(const mbms_gw_args_t& args); void handle_sgi_md_pdu(srsran::byte_buffer_t* msg); uint16_t in_cksum(uint16_t* iphdr, int count); diff --git a/srsepc/src/hss/hss.cc b/srsepc/src/hss/hss.cc index 24e9190e13..eeb798aee7 100644 --- a/srsepc/src/hss/hss.cc +++ b/srsepc/src/hss/hss.cc @@ -31,55 +31,47 @@ namespace srsepc { -hss* hss::m_instance = NULL; -pthread_mutex_t hss_instance_mutex = PTHREAD_MUTEX_INITIALIZER; +hss* hss::m_instance = nullptr; +std::mutex hss::m_mutex; -hss::hss() -{ - return; -} +hss::hss() = default; -hss::~hss() -{ - return; -} +hss::~hss() = default; hss* hss::get_instance() { - pthread_mutex_lock(&hss_instance_mutex); - if (NULL == m_instance) { + std::lock_guard lock(m_mutex); + if (!m_instance) { m_instance = new hss(); } - pthread_mutex_unlock(&hss_instance_mutex); return (m_instance); } void hss::cleanup() { - pthread_mutex_lock(&hss_instance_mutex); - if (NULL != m_instance) { + std::lock_guard lock(m_mutex); + if (m_instance) { delete m_instance; - m_instance = NULL; + m_instance = nullptr; } - pthread_mutex_unlock(&hss_instance_mutex); } -int hss::init(hss_args_t* hss_args) +int hss::init(const hss_args_t& hss_args) { - srand(time(NULL)); + std::srand(std::time(nullptr)); /*Read user information from DB*/ - if (read_db_file(hss_args->db_file) == false) { - srsran::console("Error reading user database file %s\n", hss_args->db_file.c_str()); + if (read_db_file(hss_args.db_file) == false) { + srsran::console("Error reading user database file %s\n", hss_args.db_file.c_str()); return -1; } - mcc = hss_args->mcc; - mnc = hss_args->mnc; + mcc = hss_args.mcc; + mnc = hss_args.mnc; - db_file = hss_args->db_file; + db_file = hss_args.db_file; - m_logger.info("HSS Initialized. DB file %s, MCC: %d, MNC: %d", hss_args->db_file.c_str(), mcc, mnc); + m_logger.info("HSS Initialized. DB file %s, MCC: %d, MNC: %d", hss_args.db_file.c_str(), mcc, mnc); srsran::console("HSS Initialized.\n"); return 0; } @@ -87,10 +79,9 @@ int hss::init(hss_args_t* hss_args) void hss::stop() { write_db_file(db_file); - return; } -bool hss::read_db_file(std::string db_filename) +bool hss::read_db_file(const std::string& db_filename) { std::ifstream m_db_file; @@ -179,7 +170,7 @@ bool hss::read_db_file(std::string db_filename) return true; } -bool hss::write_db_file(std::string db_filename) +bool hss::write_db_file(const std::string& db_filename) { std::string line; uint8_t k[16]; @@ -256,7 +247,6 @@ bool hss::write_db_file(std::string db_filename) bool hss::gen_auth_info_answer(uint64_t imsi, uint8_t* k_asme, uint8_t* autn, uint8_t* rand, uint8_t* xres) { - m_logger.debug("Generating AUTH info answer"); hss_ue_ctx_t* ue_ctx = get_ue_ctx(imsi); if (ue_ctx == nullptr) { @@ -621,9 +611,9 @@ hss_ue_ctx_t* hss::get_ue_ctx(uint64_t imsi) return ue_ctx_it->second.get(); } -std::map hss::get_ip_to_imsi(void) const +std::map hss::get_ip_to_imsi() const { return m_ip_to_imsi; } -} // namespace srsepc +} // namespace srsepc \ No newline at end of file diff --git a/srsepc/src/main.cc b/srsepc/src/main.cc index 10dc8cf19f..7d850f182e 100644 --- a/srsepc/src/main.cc +++ b/srsepc/src/main.cc @@ -446,7 +446,7 @@ int main(int argc, char* argv[]) spgw_logger.set_hex_dump_max_size(args.log_args.spgw_hex_limit); hss* hss = hss::get_instance(); - if (hss->init(&args.hss_args)) { + if (hss->init(args.hss_args)) { cout << "Error initializing HSS" << endl; exit(1); } diff --git a/srsepc/src/mbms-gw/main.cc b/srsepc/src/mbms-gw/main.cc index 12b423adfd..f45ee4f110 100644 --- a/srsepc/src/mbms-gw/main.cc +++ b/srsepc/src/mbms-gw/main.cc @@ -199,7 +199,7 @@ int main(int argc, char* argv[]) mbms_gw_logger.set_hex_dump_max_size(args.log_args.mbms_gw_hex_limit); mbms_gw* mbms_gw = mbms_gw::get_instance(); - if (mbms_gw->init(&args.mbms_gw_args)) { + if (mbms_gw->init(args.mbms_gw_args)) { cout << "Error initializing MBMS-GW" << endl; exit(1); } diff --git a/srsepc/src/mbms-gw/mbms-gw.cc b/srsepc/src/mbms-gw/mbms-gw.cc index 4171d04c95..338f5daad1 100644 --- a/srsepc/src/mbms-gw/mbms-gw.cc +++ b/srsepc/src/mbms-gw/mbms-gw.cc @@ -20,8 +20,8 @@ */ #include "srsepc/hdr/mbms-gw/mbms-gw.h" -#include "srsran/common/standard_streams.h" #include "srsran/common/network_utils.h" +#include "srsran/common/standard_streams.h" #include "srsran/upper/gtpu.h" #include #include @@ -36,46 +36,36 @@ namespace srsepc { -mbms_gw* mbms_gw::m_instance = NULL; -pthread_mutex_t mbms_gw_instance_mutex = PTHREAD_MUTEX_INITIALIZER; +mbms_gw* mbms_gw::m_instance = nullptr; +std::mutex mbms_gw::m_mutex; const uint16_t MBMS_GW_BUFFER_SIZE = 2500; -mbms_gw::mbms_gw() : m_running(false), m_sgi_mb_up(false), thread("MBMS_GW") -{ - return; -} +mbms_gw::mbms_gw() : m_running(false), m_sgi_mb_up(false), thread("MBMS_GW") {} -mbms_gw::~mbms_gw() -{ - return; -} +mbms_gw::~mbms_gw() {} -mbms_gw* mbms_gw::get_instance(void) +mbms_gw* mbms_gw::get_instance() { - pthread_mutex_lock(&mbms_gw_instance_mutex); - if (NULL == m_instance) { + std::lock_guard lock(m_mutex); + if (!m_instance) { m_instance = new mbms_gw(); } - pthread_mutex_unlock(&mbms_gw_instance_mutex); - return (m_instance); + return m_instance; } -void mbms_gw::cleanup(void) +void mbms_gw::cleanup() { - pthread_mutex_lock(&mbms_gw_instance_mutex); - if (NULL != m_instance) { + std::lock_guard lock(m_mutex); + if (m_instance) { delete m_instance; - m_instance = NULL; + m_instance = nullptr; } - pthread_mutex_unlock(&mbms_gw_instance_mutex); } -int mbms_gw::init(mbms_gw_args_t* args) +int mbms_gw::init(const mbms_gw_args_t& args) { - int err; - - err = init_sgi_mb_if(args); + int err = init_sgi_mb_if(args); if (err != SRSRAN_SUCCESS) { srsran::console("Error initializing SGi-MB.\n"); m_logger.error("Error initializing SGi-MB."); @@ -106,7 +96,7 @@ void mbms_gw::stop() return; } -int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) +int mbms_gw::init_sgi_mb_if(const mbms_gw_args_t& args) { struct ifreq ifr; @@ -125,8 +115,8 @@ int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TUN | IFF_NO_PI; strncpy(ifr.ifr_ifrn.ifrn_name, - args->sgi_mb_if_name.c_str(), - std::min(args->sgi_mb_if_name.length(), (size_t)IFNAMSIZ - 1)); + args.sgi_mb_if_name.c_str(), + std::min(args.sgi_mb_if_name.length(), (size_t)IFNAMSIZ - 1)); ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = '\0'; if (ioctl(m_sgi_mb_if, TUNSETIFF, &ifr) < 0) { @@ -134,7 +124,7 @@ int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) close(m_sgi_mb_if); return SRSRAN_ERROR_CANT_START; } else { - m_logger.debug("Set TUN device name: %s", args->sgi_mb_if_name.c_str()); + m_logger.debug("Set TUN device name: %s", args.sgi_mb_if_name.c_str()); } // Bring up the interface @@ -163,9 +153,9 @@ int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) // Set IP of the interface struct sockaddr_in* addr = (struct sockaddr_in*)&ifr.ifr_addr; - if (not srsran::net_utils::set_sockaddr(addr, args->sgi_mb_if_addr.c_str(), 0)) { - m_logger.error("Invalid sgi_mb_if_addr: %s", args->sgi_mb_if_addr.c_str()); - srsran::console("Invalid sgi_mb_if_addr: %s\n", args->sgi_mb_if_addr.c_str()); + if (not srsran::net_utils::set_sockaddr(addr, args.sgi_mb_if_addr.c_str(), 0)) { + m_logger.error("Invalid sgi_mb_if_addr: %s", args.sgi_mb_if_addr.c_str()); + srsran::console("Invalid sgi_mb_if_addr: %s\n", args.sgi_mb_if_addr.c_str()); close(m_sgi_mb_if); close(sgi_mb_sock); return SRSRAN_ERROR_CANT_START; @@ -173,16 +163,18 @@ int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) if (ioctl(sgi_mb_sock, SIOCSIFADDR, &ifr) < 0) { m_logger.error( - "Failed to set TUN interface IP. Address: %s, Error: %s", args->sgi_mb_if_addr.c_str(), strerror(errno)); + "Failed to set TUN interface IP. Address: %s, Error: %s", args.sgi_mb_if_addr.c_str(), strerror(errno)); close(m_sgi_mb_if); close(sgi_mb_sock); return SRSRAN_ERROR_CANT_START; } - ifr.ifr_netmask.sa_family = AF_INET; - if (inet_pton(ifr.ifr_netmask.sa_family, args->sgi_mb_if_mask.c_str(), &((struct sockaddr_in*)&ifr.ifr_netmask)->sin_addr.s_addr ) != 1) { - m_logger.error("Invalid sgi_mb_if_mask: %s", args->sgi_mb_if_mask.c_str()); - srsran::console("Invalid sgi_mb_if_mask: %s\n", args->sgi_mb_if_mask.c_str()); + ifr.ifr_netmask.sa_family = AF_INET; + if (inet_pton(ifr.ifr_netmask.sa_family, + args.sgi_mb_if_mask.c_str(), + &((struct sockaddr_in*)&ifr.ifr_netmask)->sin_addr.s_addr) != 1) { + m_logger.error("Invalid sgi_mb_if_mask: %s", args.sgi_mb_if_mask.c_str()); + srsran::console("Invalid sgi_mb_if_mask: %s\n", args.sgi_mb_if_mask.c_str()); perror("inet_pton"); return SRSRAN_ERROR_CANT_START; } @@ -198,7 +190,7 @@ int mbms_gw::init_sgi_mb_if(mbms_gw_args_t* args) return SRSRAN_SUCCESS; } -int mbms_gw::init_m1_u(mbms_gw_args_t* args) +int mbms_gw::init_m1_u(const mbms_gw_args_t& args) { int addrlen; struct sockaddr_in addr; @@ -221,31 +213,31 @@ int mbms_gw::init_m1_u(mbms_gw_args_t* args) /* Set local interface for outbound multicast packets*/ /* The IP must be associated with a local multicast capable interface */ struct in_addr local_if; - if (inet_pton(AF_INET, args->m1u_multi_if.c_str(), &local_if.s_addr) != 1) { - m_logger.error("Invalid m1u_multi_if: %s", args->m1u_multi_if.c_str()); - srsran::console("Invalid m1u_multi_if: %s\n", args->m1u_multi_if.c_str()); + if (inet_pton(AF_INET, args.m1u_multi_if.c_str(), &local_if.s_addr) != 1) { + m_logger.error("Invalid m1u_multi_if: %s", args.m1u_multi_if.c_str()); + srsran::console("Invalid m1u_multi_if: %s\n", args.m1u_multi_if.c_str()); perror("inet_pton"); return SRSRAN_ERROR_CANT_START; } if (setsockopt(m_m1u, IPPROTO_IP, IP_MULTICAST_IF, (char*)&local_if, sizeof(struct in_addr)) < 0) { - m_logger.error("Error %s setting multicast interface %s.", strerror(errno), args->m1u_multi_if.c_str()); + m_logger.error("Error %s setting multicast interface %s.", strerror(errno), args.m1u_multi_if.c_str()); return SRSRAN_ERROR_CANT_START; } else { - printf("Multicast interface specified. Address: %s\n", args->m1u_multi_if.c_str()); + printf("Multicast interface specified. Address: %s\n", args.m1u_multi_if.c_str()); } /*Set Multicast TTL*/ - if (setsockopt(m_m1u, IPPROTO_IP, IP_MULTICAST_TTL, &args->m1u_multi_ttl, sizeof(args->m1u_multi_ttl)) < 0) { + if (setsockopt(m_m1u, IPPROTO_IP, IP_MULTICAST_TTL, &args.m1u_multi_ttl, sizeof(args.m1u_multi_ttl)) < 0) { perror("Error setting multicast ttl.\n"); return SRSRAN_ERROR_CANT_START; } bzero(&m_m1u_multi_addr, sizeof(m_m1u_multi_addr)); - m_m1u_multi_addr.sin_family = AF_INET; - m_m1u_multi_addr.sin_port = htons(GTPU_RX_PORT + 1); - if (inet_pton(m_m1u_multi_addr.sin_family, args->m1u_multi_addr.c_str(), &m_m1u_multi_addr.sin_addr.s_addr) != 1) { - m_logger.error("Invalid m1u_multi_addr: %s", args->m1u_multi_addr.c_str()); - srsran::console("Invalid m1u_multi_addr: %s\n", args->m1u_multi_addr.c_str()); + m_m1u_multi_addr.sin_family = AF_INET; + m_m1u_multi_addr.sin_port = htons(GTPU_RX_PORT + 1); + if (inet_pton(m_m1u_multi_addr.sin_family, args.m1u_multi_addr.c_str(), &m_m1u_multi_addr.sin_addr.s_addr) != 1) { + m_logger.error("Invalid m1u_multi_addr: %s", args.m1u_multi_addr.c_str()); + srsran::console("Invalid m1u_multi_addr: %s\n", args.m1u_multi_addr.c_str()); perror("inet_pton"); return SRSRAN_ERROR_CANT_START; } diff --git a/test/50prb_screenlog_srsepc.log b/test/50prb_screenlog_srsepc.log new file mode 100644 index 0000000000..e69de29bb2 From d0b15cd5ac7fece6b2855bd1504b5e1d9aa83ac0 Mon Sep 17 00:00:00 2001 From: merttozer Date: Wed, 24 Apr 2024 18:38:39 +0300 Subject: [PATCH 2/2] Remove unnecessary file --- test/50prb_screenlog_srsepc.log | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/50prb_screenlog_srsepc.log diff --git a/test/50prb_screenlog_srsepc.log b/test/50prb_screenlog_srsepc.log deleted file mode 100644 index e69de29bb2..0000000000