diff --git a/lib/include/srsran/common/string_helpers.h b/lib/include/srsran/common/string_helpers.h index d20692568..a55ed0254 100644 --- a/lib/include/srsran/common/string_helpers.h +++ b/lib/include/srsran/common/string_helpers.h @@ -15,6 +15,8 @@ #include "srsran/srslog/bundled/fmt/format.h" #include +#include +#include #include #include #include @@ -119,6 +121,49 @@ const char* to_c_str(fmt::basic_memory_buffer& mem_buffer) return mem_buffer.data(); } +static inline bool replace(std::string& str, const std::string& from, const std::string& to) +{ + size_t start_pos = str.find(from); + if (start_pos == std::string::npos) + return false; + str.replace(start_pos, from.length(), to); + return true; +} + +static inline std::vector split_string(const std::string& str, char delimiter) +{ + std::vector tokens; + std::string token; + std::istringstream tokenStream(str); + + while (std::getline(tokenStream, token, delimiter)) { + tokens.push_back(token); + } + return tokens; +} + +static inline void get_uint_vec_from_hex_str(const std::string& key_str, uint8_t* key, uint len) +{ + const char* pos = key_str.c_str(); + + for (uint count = 0; count < len; count++) { + sscanf(pos, "%2hhx", &key[count]); + pos += 2; + } + return; +} + +static inline std::string hex_string(uint8_t* hex, int size) +{ + std::stringstream ss; + + ss << std::hex << std::setfill('0'); + for (int i = 0; i < size; i++) { + ss << std::setw(2) << static_cast(hex[i]); + } + return ss.str(); +} + } // namespace srsran #endif // SRSRAN_STRING_HELPERS_H diff --git a/srsepc/hdr/hss/hss.h b/srsepc/hdr/hss/hss.h index 953eb2941..43e12d28e 100644 --- a/srsepc/hdr/hss/hss.h +++ b/srsepc/hdr/hss/hss.h @@ -24,7 +24,7 @@ #include "srsran/interfaces/epc_interfaces.h" #include "srsran/srslog/srslog.h" #include -#include + #include #define LTE_FDD_ENB_IND_HE_N_BITS 5 @@ -94,7 +94,6 @@ private: 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); - std::vector split_string(const std::string& str, char delimiter); 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); diff --git a/srsepc/src/hss/hss.cc b/srsepc/src/hss/hss.cc index 114d28d30..2c6e5468d 100644 --- a/srsepc/src/hss/hss.cc +++ b/srsepc/src/hss/hss.cc @@ -11,6 +11,7 @@ */ #include "srsepc/hdr/hss/hss.h" #include "srsran/common/security.h" +#include "srsran/common/string_helpers.h" #include #include // for printing uint64_t #include @@ -94,7 +95,7 @@ bool hss::read_db_file(std::string db_filename) while (std::getline(m_db_file, line)) { if (line[0] != '#' && line.length() > 0) { uint column_size = 10; - std::vector split = split_string(line, ','); + std::vector split = srsran::split_string(line, ','); if (split.size() != column_size) { m_logger.error("Error parsing UE database. Wrong number of columns in .csv"); m_logger.error("Columns: %zd, Expected %d.", split.size(), column_size); @@ -115,20 +116,20 @@ bool hss::read_db_file(std::string db_filename) return false; } ue_ctx->imsi = strtoull(split[2].c_str(), nullptr, 10); - get_uint_vec_from_hex_str(split[3], ue_ctx->key, 16); + srsran::get_uint_vec_from_hex_str(split[3], ue_ctx->key, 16); if (split[4] == std::string("op")) { ue_ctx->op_configured = true; - get_uint_vec_from_hex_str(split[5], ue_ctx->op, 16); + srsran::get_uint_vec_from_hex_str(split[5], ue_ctx->op, 16); srsran::compute_opc(ue_ctx->key, ue_ctx->op, ue_ctx->opc); } else if (split[4] == std::string("opc")) { ue_ctx->op_configured = false; - get_uint_vec_from_hex_str(split[5], ue_ctx->opc, 16); + srsran::get_uint_vec_from_hex_str(split[5], ue_ctx->opc, 16); } else { m_logger.error("Neither OP nor OPc configured."); return false; } - get_uint_vec_from_hex_str(split[6], ue_ctx->amf, 2); - get_uint_vec_from_hex_str(split[7], ue_ctx->sqn, 6); + srsran::get_uint_vec_from_hex_str(split[6], ue_ctx->amf, 2); + srsran::get_uint_vec_from_hex_str(split[7], ue_ctx->sqn, 6); m_logger.debug("Added user from DB, IMSI: %015" PRIu64 "", ue_ctx->imsi); m_logger.debug(ue_ctx->key, 16, "User Key : "); @@ -214,19 +215,19 @@ bool hss::write_db_file(std::string db_filename) m_db_file << ","; m_db_file << std::setfill('0') << std::setw(15) << it->second->imsi; m_db_file << ","; - m_db_file << hex_string(it->second->key, 16); + m_db_file << srsran::hex_string(it->second->key, 16); m_db_file << ","; if (it->second->op_configured) { m_db_file << "op,"; - m_db_file << hex_string(it->second->op, 16); + m_db_file << srsran::hex_string(it->second->op, 16); } else { m_db_file << "opc,"; - m_db_file << hex_string(it->second->opc, 16); + m_db_file << srsran::hex_string(it->second->opc, 16); } m_db_file << ","; - m_db_file << hex_string(it->second->amf, 2); + m_db_file << srsran::hex_string(it->second->amf, 2); m_db_file << ","; - m_db_file << hex_string(it->second->sqn, 6); + m_db_file << srsran::hex_string(it->second->sqn, 6); m_db_file << ","; m_db_file << it->second->qci; if (it->second->static_ip_addr != "0.0.0.0") { @@ -611,41 +612,6 @@ hss_ue_ctx_t* hss::get_ue_ctx(uint64_t imsi) return ue_ctx_it->second.get(); } -/* Helper functions*/ -std::vector hss::split_string(const std::string& str, char delimiter) -{ - std::vector tokens; - std::string token; - std::istringstream tokenStream(str); - - while (std::getline(tokenStream, token, delimiter)) { - tokens.push_back(token); - } - return tokens; -} - -void hss::get_uint_vec_from_hex_str(const std::string& key_str, uint8_t* key, uint len) -{ - const char* pos = key_str.c_str(); - - for (uint count = 0; count < len; count++) { - sscanf(pos, "%2hhx", &key[count]); - pos += 2; - } - return; -} - -std::string hss::hex_string(uint8_t* hex, int size) -{ - std::stringstream ss; - - ss << std::hex << std::setfill('0'); - for (int i = 0; i < size; i++) { - ss << std::setw(2) << static_cast(hex[i]); - } - return ss.str(); -} - std::map hss::get_ip_to_imsi(void) const { return m_ip_to_imsi; diff --git a/srsue/hdr/stack/upper/nas.h b/srsue/hdr/stack/upper/nas.h index d919a6050..42d643b63 100644 --- a/srsue/hdr/stack/upper/nas.h +++ b/srsue/hdr/stack/upper/nas.h @@ -258,20 +258,6 @@ private: return true; } - std::vector split_string(const std::string input) - { - std::vector list; - std::stringstream ss(input); - while (ss.good()) { - std::string substr; - getline(ss, substr, ','); - if (not substr.empty()) { - list.push_back(strtol(substr.c_str(), nullptr, 10)); - } - } - return list; - } - // NAS Idle procedures class plmn_search_proc; // PLMN selection proc (fwd declared)