Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MBMS-GW and HSS Classes for Modern C++ Compliance and Enhanced Maintainability #1326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions srsepc/hdr/hss/hss.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, uint64_t> get_ip_to_imsi() const;

Expand All @@ -92,26 +92,28 @@ class hss : public hss_interface_nas
virtual ~hss();
static hss* m_instance;

static std::mutex m_mutex;

std::map<uint64_t, std::unique_ptr<hss_ue_ctx_t> > 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);
Expand All @@ -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
14 changes: 8 additions & 6 deletions srsepc/hdr/mbms-gw/mbms-gw.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ 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();

private:
/* 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);

Expand Down
52 changes: 21 additions & 31 deletions srsepc/src/hss/hss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,66 +31,57 @@

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<std::mutex> 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<std::mutex> 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;
}

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;

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -621,9 +611,9 @@ hss_ue_ctx_t* hss::get_ue_ctx(uint64_t imsi)
return ue_ctx_it->second.get();
}

std::map<std::string, uint64_t> hss::get_ip_to_imsi(void) const
std::map<std::string, uint64_t> hss::get_ip_to_imsi() const
{
return m_ip_to_imsi;
}

} // namespace srsepc
} // namespace srsepc
2 changes: 1 addition & 1 deletion srsepc/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion srsepc/src/mbms-gw/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading