Cleaned string helpers

master
David Rupprecht 3 years ago committed by Andre Puschmann
parent 36af79b9d5
commit e8a464228d

@ -15,6 +15,8 @@
#include "srsran/srslog/bundled/fmt/format.h" #include "srsran/srslog/bundled/fmt/format.h"
#include <algorithm> #include <algorithm>
#include <fstream>
#include <iomanip>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@ -119,6 +121,49 @@ const char* to_c_str(fmt::basic_memory_buffer<char, N>& mem_buffer)
return mem_buffer.data(); 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<std::string> split_string(const std::string& str, char delimiter)
{
std::vector<std::string> 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<unsigned>(hex[i]);
}
return ss.str();
}
} // namespace srsran } // namespace srsran
#endif // SRSRAN_STRING_HELPERS_H #endif // SRSRAN_STRING_HELPERS_H

@ -24,7 +24,7 @@
#include "srsran/interfaces/epc_interfaces.h" #include "srsran/interfaces/epc_interfaces.h"
#include "srsran/srslog/srslog.h" #include "srsran/srslog/srslog.h"
#include <cstddef> #include <cstddef>
#include <fstream>
#include <map> #include <map>
#define LTE_FDD_ENB_IND_HE_N_BITS 5 #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_milenage(hss_ue_ctx_t* ue_ctx, uint8_t* auts);
void resync_sqn_xor(hss_ue_ctx_t* ue_ctx, uint8_t* auts); void resync_sqn_xor(hss_ue_ctx_t* ue_ctx, uint8_t* auts);
std::vector<std::string> 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 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_ue_sqn(hss_ue_ctx_t* ue_ctx);

@ -11,6 +11,7 @@
*/ */
#include "srsepc/hdr/hss/hss.h" #include "srsepc/hdr/hss/hss.h"
#include "srsran/common/security.h" #include "srsran/common/security.h"
#include "srsran/common/string_helpers.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <inttypes.h> // for printing uint64_t #include <inttypes.h> // for printing uint64_t
#include <iomanip> #include <iomanip>
@ -94,7 +95,7 @@ bool hss::read_db_file(std::string db_filename)
while (std::getline(m_db_file, line)) { while (std::getline(m_db_file, line)) {
if (line[0] != '#' && line.length() > 0) { if (line[0] != '#' && line.length() > 0) {
uint column_size = 10; uint column_size = 10;
std::vector<std::string> split = split_string(line, ','); std::vector<std::string> split = srsran::split_string(line, ',');
if (split.size() != column_size) { if (split.size() != column_size) {
m_logger.error("Error parsing UE database. Wrong number of columns in .csv"); m_logger.error("Error parsing UE database. Wrong number of columns in .csv");
m_logger.error("Columns: %zd, Expected %d.", split.size(), column_size); 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; return false;
} }
ue_ctx->imsi = strtoull(split[2].c_str(), nullptr, 10); 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")) { if (split[4] == std::string("op")) {
ue_ctx->op_configured = true; 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); srsran::compute_opc(ue_ctx->key, ue_ctx->op, ue_ctx->opc);
} else if (split[4] == std::string("opc")) { } else if (split[4] == std::string("opc")) {
ue_ctx->op_configured = false; 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 { } else {
m_logger.error("Neither OP nor OPc configured."); m_logger.error("Neither OP nor OPc configured.");
return false; return false;
} }
get_uint_vec_from_hex_str(split[6], ue_ctx->amf, 2); srsran::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[7], ue_ctx->sqn, 6);
m_logger.debug("Added user from DB, IMSI: %015" PRIu64 "", ue_ctx->imsi); m_logger.debug("Added user from DB, IMSI: %015" PRIu64 "", ue_ctx->imsi);
m_logger.debug(ue_ctx->key, 16, "User Key : "); 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 << ",";
m_db_file << std::setfill('0') << std::setw(15) << it->second->imsi; m_db_file << std::setfill('0') << std::setw(15) << it->second->imsi;
m_db_file << ","; 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 << ","; m_db_file << ",";
if (it->second->op_configured) { if (it->second->op_configured) {
m_db_file << "op,"; m_db_file << "op,";
m_db_file << hex_string(it->second->op, 16); m_db_file << srsran::hex_string(it->second->op, 16);
} else { } else {
m_db_file << "opc,"; 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 << ",";
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 << ",";
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 << ",";
m_db_file << it->second->qci; m_db_file << it->second->qci;
if (it->second->static_ip_addr != "0.0.0.0") { 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(); return ue_ctx_it->second.get();
} }
/* Helper functions*/
std::vector<std::string> hss::split_string(const std::string& str, char delimiter)
{
std::vector<std::string> 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<unsigned>(hex[i]);
}
return ss.str();
}
std::map<std::string, uint64_t> hss::get_ip_to_imsi(void) const std::map<std::string, uint64_t> hss::get_ip_to_imsi(void) const
{ {
return m_ip_to_imsi; return m_ip_to_imsi;

@ -258,20 +258,6 @@ private:
return true; return true;
} }
std::vector<uint8_t> split_string(const std::string input)
{
std::vector<uint8_t> 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 // NAS Idle procedures
class plmn_search_proc; // PLMN selection proc (fwd declared) class plmn_search_proc; // PLMN selection proc (fwd declared)

Loading…
Cancel
Save