Skip to content
Snippets Groups Projects
Commit 31bd9703 authored by Stefan Spettel's avatar Stefan Spettel
Browse files

refact(pcf): remove unused code and resynch with utils from common-src

parent c02641ef
No related branches found
No related tags found
1 merge request!46Resynch utils and common from common-src and use new FlowDirection fix
Pipeline #45586 passed
Showing
with 3 additions and 1555 deletions
......@@ -33,6 +33,8 @@ include_directories(${SRC_TOP_DIR}/${MOUNTED_COMMON}/config)
include(${SRC_TOP_DIR}/${MOUNTED_COMMON}/model/common_model/common_model.cmake)
include(${SRC_TOP_DIR}/${MOUNTED_COMMON}/model/pcf/pcf_model.cmake)
include(${SRC_TOP_DIR}/${MOUNTED_COMMON}/model/smf/smf_model.cmake)
include(${SRC_TOP_DIR}/${MOUNTED_COMMON}/utils/utils.cmake)
include(${SRC_TOP_DIR}/${MOUNTED_COMMON}/common/common.cmake)
file(GLOB PCF_API_SERVER_src_files
${PCF_API_SERVER_DIR}/pcf-api-server.cpp
......
......@@ -29,7 +29,6 @@
#pragma once
#include "conversions.hpp"
#include "pcf.h"
#include "pcf_app.hpp"
#include "string.hpp"
......@@ -68,7 +67,7 @@ class pcf_http2_server {
void stop();
private:
util::uint_generator<uint32_t> m_promise_id_generator;
oai::utils::uint_generator<uint32_t> m_promise_id_generator;
std::string m_address;
uint32_t m_port;
bool running_server;
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file 3gpp_23.003.h
\brief
\author Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_3GPP_23_003_SEEN
#define FILE_3GPP_23_003_SEEN
#include <stdint.h>
typedef struct plmn_s {
uint8_t mcc_digit2 : 4;
uint8_t mcc_digit1 : 4;
uint8_t mcc_digit3 : 4;
uint8_t mnc_digit3 : 4;
uint8_t mnc_digit2 : 4;
uint8_t mnc_digit1 : 4;
} plmn_t;
#define INVALID_TAC_0000 (uint16_t) 0x0000
#define INVALID_TAC_FFFE (uint16_t) 0xFFFE
#define INVALID_TAC (uint32_t) 0x00000000
#define INVALID_TMSI \
UINT32_MAX /*!< \brief The network shall not allocate a TMSI with all 32 \
bits equal to 1 (this is because the TMSI must be stored in \
the SIM, and the SIM uses 4 octets with all bits \
equal to 1 to indicate that no valid TMSI is \
available). */
typedef uint16_t tac_t;
typedef struct tai_s {
plmn_t plmn; /*!< \brief <MCC> + <MNC> */
tac_t tac; /*!< \brief Tracking Area Code */
} tai_t;
typedef struct eci_s {
uint32_t gnb_id : 20;
uint32_t cell_id : 8;
uint32_t empty : 4;
} ci_t;
typedef struct cgi_s {
plmn_t plmn;
ci_t cell_identity; // 28 bits
} cgi_t;
typedef struct nr_tai_s /*5G ADD it*/
{
plmn_t plmn;
uint32_t tac : 24;
} nr_tai_t;
typedef struct nr_cell_identity_s /*5G ADD it */
{
uint32_t gnb_id;
uint8_t cell_id : 4;
} nr_cell_identity_t;
typedef struct nr_cgi_s /*5G ADD it */
{
plmn_t plmn;
nr_cell_identity_t cell_identity;
} nr_cgi_t;
typedef struct fiveG_s_tmsi_s /*5G ADD it */
{
uint16_t amf_set_id : 10;
uint8_t amf_pointer : 6;
uint32_t fiveG_s_tmsi; // 32
} fiveG_s_tmsi_t;
typedef struct fiveG_s_gua_s /*5G ADD it */
{
plmn_t plmn;
uint8_t region_id;
uint16_t amf_set_id : 10;
uint8_t amf_pointer : 6;
} fiveG_s_gua_t;
typedef struct amf_set_id_s /*5G ADD it*/
{
uint16_t amf_set_id : 10;
} amf_set_id_t;
typedef struct allowed_nssai /*5G ADD it*/
{
uint8_t sST;
uint32_t sD : 24;
} allowed_nssai;
// typedef struct allowed_nssai_s /*5G ADD it*/
// {
// allowed_nssai* s_nssai;
// uint32_t count;
// } allowed_nssai_t;
#endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#ifndef FILE_3GPP_29_510_PCF_SEEN
#define FILE_3GPP_29_510_PCF_SEEN
#include <vector>
#include <nlohmann/json.hpp>
#include "3gpp_23.003.h"
// Section 28.4, TS23.003
typedef struct s_nssai {
uint8_t sST;
std::string sD;
s_nssai(const uint8_t& sst, const std::string sd) : sST(sst), sD(sd) {}
s_nssai() : sST(), sD() {}
s_nssai(const s_nssai& p) : sST(p.sST), sD(p.sD) {}
bool operator==(const struct s_nssai& s) const {
if ((s.sST == this->sST) && (s.sD.compare(this->sD) == 0)) {
return true;
} else {
return false;
}
}
s_nssai& operator=(const s_nssai& s) {
sST = s.sST;
sD = s.sD;
return *this;
}
} snssai_t;
typedef struct dnai_s {
} dnai_t;
typedef struct patch_item_s {
std::string op;
std::string path;
// std::string from;
std::string value;
nlohmann::json to_json() const {
nlohmann::json json_data = {};
json_data["op"] = op;
json_data["path"] = path;
json_data["value"] = value;
return json_data;
}
} patch_item_t;
#define NSSF_CURL_TIMEOUT_MS 100L
#define NNRF_NFM_BASE "/nnrf-nfm/"
#define NSSF_NF_REGISTER_URL "/nf-instances/"
// #### new data types
typedef struct tac_range_s {
std::string start;
std::string end;
std::string pattern;
} tac_range_t;
typedef struct tai_range_s {
plmn_t plmnid;
std::vector<tac_range_t> tac_range_list;
// std::string Nid;
} tai_range_t;
#endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#ifndef FILE_3GPP_29_571_SEEN
#define FILE_3GPP_29_571_SEEN
#include "3gpp_23.003.h"
#include "3gpp_29.510.h"
#include <vector>
enum access_type_e { ACESS_3GPP = 1, ACESS_NON_3GPP = 2 };
static const std::vector<std::string> access_type_e2str = {
"3GPP_ACCESS", "NON_3GPP_ACCESS"};
typedef struct sd_range_s {
std::string start;
std::string end;
} sd_range_t;
typedef struct snssai_extension_s {
std::vector<sd_range_t> sd_ranges;
bool wildcard_sd;
} snssai_extension_t;
typedef struct ext_snssai_s {
snssai_t snssai;
snssai_extension_t snssai_extension;
} ext_snssai_t;
#endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file 3gpp_commons.h
\brief
\author Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_3GPP_COMMONS_SEEN
#define FILE_3GPP_COMMONS_SEEN
#include <stdint.h>
// 8.2 Recovery
typedef struct recovery_s {
uint8_t restart_counter;
} recovery_t;
#endif /* FILE_3GPP_COMMONS_SEEN */
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file common_defs.h
\brief
\author Sebastien ROUX, Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_COMMON_DEFS_SEEN
#define FILE_COMMON_DEFS_SEEN
#include <arpa/inet.h>
#define RETURNclear (int) 2
#define RETURNerror (int) 1
#define RETURNok (int) 0
//------------------------------------------------------------------------------
#define IPV4_STR_ADDR_TO_INADDR(AdDr_StR, InAdDr, MeSsAgE) \
do { \
if (inet_aton(AdDr_StR, &InAdDr) <= 0) { \
throw(MeSsAgE); \
} \
} while (0)
#define NIPADDR(addr) \
(uint8_t)(addr & 0x000000FF), (uint8_t) ((addr & 0x0000FF00) >> 8), \
(uint8_t) ((addr & 0x00FF0000) >> 16), \
(uint8_t) ((addr & 0xFF000000) >> 24)
#ifndef UNUSED
#define UNUSED(x) (void) (x)
#endif
#endif /* FILE_COMMON_DEFS_SEEN */
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file common_root_types.c
\brief
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file common_root_types.h
\brief
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_COMMON_ROOT_TYPES_SEEN
#define FILE_COMMON_ROOT_TYPES_SEEN
#include <stdint.h>
#include <inttypes.h>
#include <arpa/inet.h>
//------------------------------------------------------------------------------
#define PRIORITY_LEVEL_MAX (15)
#define PRIORITY_LEVEL_MIN (1)
#define BEARERS_PER_UE (11)
#define IMEI_DIGITS_MAX (15)
#define IMEISV_DIGITS_MAX (16)
#define MAX_APN_PER_UE (5)
#define PROC_ID_FMT "0x%" PRIx64
// TEIDs
typedef uint32_t teid_t;
#define TEID_FMT "0x%" PRIx32
#define TEID_SCAN_FMT SCNx32
#define INVALID_TEID ((teid_t) 0x00000000)
#define UNASSIGNED_TEID ((teid_t) 0x00000000)
// SEIDs
typedef uint64_t seid_t;
#define SEID_FMT "0x%" PRIx64
#define SEID_SCAN_FMT SCNx64
#define INVALID_SEID ((seid_t) 0x00000000)
#define UNASSIGNED_SEID ((seid_t) 0x00000000)
//------------------------------------------------------------------------------
// IMSI
typedef uint64_t imsi64_t;
#define IMSI_64_FMT "%" SCNu64
#define INVALID_IMSI64 (imsi64_t) 0
//------------------------------------------------------------------------------
typedef uint64_t bitrate_t;
#define PRIORITY_LEVEL_FMT "0x%" PRIu8
#define QCI_FMT "0x%" PRIu8
#define QCI_SCAN_FMT SCNu8
#define PRE_EMPTION_CAPABILITY_FMT "0x%" PRIu8
#define PRE_EMPTION_VULNERABILITY_FMT "0x%" PRIu8
#endif /* FILE_COMMON_ROOT_TYPES_SEEN */
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file endpoint.hpp
\brief
\author Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_ENDPOINT_HPP_SEEN
#define FILE_ENDPOINT_HPP_SEEN
#include "conversions.hpp"
#include <arpa/inet.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <string.h>
class endpoint {
public:
struct sockaddr_storage addr_storage;
socklen_t addr_storage_len;
endpoint()
: addr_storage(), addr_storage_len(sizeof(struct sockaddr_storage)){};
endpoint(const endpoint& e)
: addr_storage(e.addr_storage), addr_storage_len(e.addr_storage_len){};
endpoint(const struct sockaddr_storage& addr, const socklen_t len)
: addr_storage(addr), addr_storage_len(len){};
endpoint(const struct in_addr& addr, const uint16_t port) {
struct sockaddr_in* addr_in = (struct sockaddr_in*) &addr_storage;
addr_in->sin_family = AF_INET;
addr_in->sin_port = htons(port);
addr_in->sin_addr.s_addr = addr.s_addr;
addr_storage_len = sizeof(struct sockaddr_in);
};
endpoint(const struct in6_addr& addr6, const uint16_t port) {
struct sockaddr_in6* addr_in6 = (struct sockaddr_in6*) &addr_storage;
addr_in6->sin6_family = AF_INET6;
addr_in6->sin6_port = htons(port);
addr_in6->sin6_flowinfo = 0;
memcpy(&addr_in6->sin6_addr, &addr6, sizeof(struct in6_addr));
addr_in6->sin6_scope_id = 0;
addr_storage_len = sizeof(struct sockaddr_in6);
};
uint16_t port() const {
return ntohs(((struct sockaddr_in*) &addr_storage)->sin_port);
}
sa_family_t family() const { return addr_storage.ss_family; }
std::string toString() const {
std::string str;
if (addr_storage.ss_family == AF_INET) {
struct sockaddr_in* addr_in = (struct sockaddr_in*) &addr_storage;
str.append(conv::toString(addr_in->sin_addr));
str.append(":").append(std::to_string(ntohs(addr_in->sin_port)));
} else if (addr_storage.ss_family == AF_INET6) {
struct sockaddr_in6* addr_in6 = (struct sockaddr_in6*) &addr_storage;
str.append(conv::toString(addr_in6->sin6_addr));
str.append(":").append(std::to_string(ntohs(addr_in6->sin6_port)));
}
return str;
}
};
#endif
......@@ -22,7 +22,6 @@
#ifndef FILE_PCF_SEEN
#define FILE_PCF_SEEN
#include "3gpp_29.571.h"
#include <nlohmann/json.hpp>
#include <netinet/in.h>
#include <arpa/inet.h>
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file conversions.cpp
\brief
\author Sebastien ROUX
\company Eurecom
*/
#include "conversions.hpp"
#include "logger.hpp"
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
#include <inttypes.h>
#include <arpa/inet.h>
static const char hex_to_ascii_table[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
static const signed char ascii_to_hex_table[0x100] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1};
void conv::hexa_to_ascii(uint8_t* from, char* to, size_t length) {
size_t i;
for (i = 0; i < length; i++) {
uint8_t upper = (from[i] & 0xf0) >> 4;
uint8_t lower = from[i] & 0x0f;
to[2 * i] = hex_to_ascii_table[upper];
to[2 * i + 1] = hex_to_ascii_table[lower];
}
}
int conv::ascii_to_hex(uint8_t* dst, const char* h) {
const unsigned char* hex = (const unsigned char*) h;
unsigned i = 0;
for (;;) {
int high, low;
while (*hex && isspace(*hex)) hex++;
if (!*hex) return 1;
high = ascii_to_hex_table[*hex++];
if (high < 0) return 0;
while (*hex && isspace(*hex)) hex++;
if (!*hex) return 0;
low = ascii_to_hex_table[*hex++];
if (low < 0) return 0;
dst[i++] = (high << 4) | low;
}
}
//------------------------------------------------------------------------------
std::string conv::mccToString(
const uint8_t digit1, const uint8_t digit2, const uint8_t digit3) {
std::string s = {};
uint16_t mcc16 = digit1 * 100 + digit2 * 10 + digit3;
// s.append(std::to_string(digit1)).append(std::to_string(digit2)).append(std::to_string(digit3));
s.append(std::to_string(mcc16));
return s;
}
//------------------------------------------------------------------------------
std::string conv::mncToString(
const uint8_t digit1, const uint8_t digit2, const uint8_t digit3) {
std::string s = {};
uint16_t mcc16 = 0;
if (digit3 == 0x0F) {
mcc16 = digit1 * 10 + digit2;
} else {
mcc16 = digit1 * 100 + digit2 * 10 + digit3;
}
s.append(std::to_string(mcc16));
return s;
}
//------------------------------------------------------------------------------
struct in_addr conv::fromString(const std::string addr4) {
unsigned char buf[sizeof(struct in6_addr)] = {};
auto ret = inet_pton(AF_INET, addr4.c_str(), buf);
if (ret != 1) {
Logger::pcf_app().error(
__PRETTY_FUNCTION__ + std::string{" Failed to convert "} + addr4);
}
struct in_addr* ia = (struct in_addr*) buf;
return *ia;
}
//------------------------------------------------------------------------------
struct in6_addr conv::fromStringV6(const std::string& addr6) {
unsigned char buf[sizeof(struct in6_addr)] = {};
struct in6_addr ipv6_addr {};
if (inet_pton(AF_INET6, addr6.c_str(), buf) == 1) {
memcpy(&ipv6_addr, buf, sizeof(struct in6_addr));
}
return ipv6_addr;
}
//------------------------------------------------------------------------------
std::string conv::toString(const struct in_addr& inaddr) {
std::string s = {};
char str[INET6_ADDRSTRLEN] = {};
if (inet_ntop(AF_INET, (const void*) &inaddr, str, INET6_ADDRSTRLEN) ==
NULL) {
s.append("Error in_addr");
} else {
s.append(str);
}
return s;
}
//------------------------------------------------------------------------------
std::string conv::toString(const struct in6_addr& in6addr) {
std::string s = {};
char str[INET6_ADDRSTRLEN] = {};
if (inet_ntop(AF_INET6, (const void*) &in6addr, str, INET6_ADDRSTRLEN) ==
nullptr) {
s.append("Error in6_addr");
} else {
s.append(str);
}
return s;
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file conversions.hpp
\brief
\author Sebastien ROUX, Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_CONVERSIONS_HPP_SEEN
#define FILE_CONVERSIONS_HPP_SEEN
#include <stdint.h>
#include <string>
#include <netinet/in.h>
/* Used to format an uint32_t containing an ipv4 address */
#define IN_ADDR_FMT "%u.%u.%u.%u"
#define PRI_IN_ADDR(aDDRESS) \
(uint8_t)((aDDRESS.s_addr) & 0x000000ff), \
(uint8_t) (((aDDRESS.s_addr) & 0x0000ff00) >> 8), \
(uint8_t) (((aDDRESS.s_addr) & 0x00ff0000) >> 16), \
(uint8_t) (((aDDRESS.s_addr) & 0xff000000) >> 24)
#define IPV4_ADDR_DISPLAY_8(aDDRESS) \
(aDDRESS)[0], (aDDRESS)[1], (aDDRESS)[2], (aDDRESS)[3]
class conv {
public:
static void hexa_to_ascii(uint8_t* from, char* to, size_t length);
static int ascii_to_hex(uint8_t* dst, const char* h);
static struct in_addr fromString(const std::string addr4);
static struct in6_addr fromStringV6(const std::string& addr6);
static std::string toString(const struct in_addr& inaddr);
static std::string toString(const struct in6_addr& in6addr);
static std::string mccToString(
const uint8_t digit1, const uint8_t digit2, const uint8_t digit3);
static std::string mncToString(
const uint8_t digit1, const uint8_t digit2, const uint8_t digit3);
};
#endif /* FILE_CONVERSIONS_HPP_SEEN */
/* From https://gist.github.com/javiermon/6272065#file-gateway_netlink-c */
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include <fstream> // std::ifstream
#include <string>
#include "common_defs.h"
#include "get_gateway_netlink.hpp"
#include "logger.hpp"
#define BUFFER_SIZE 4096
using namespace std;
//------------------------------------------------------------------------------
bool util::get_iface_l2_addr(const std::string& iface, std::string& mac) {
std::string mac_address_path =
fmt::format("/sys/class/net/{}/address", iface);
std::ifstream mac_address_in(
mac_address_path.c_str(), ios_base::in | ios_base::binary);
char wb[32];
mac_address_in.get(wb, 32);
mac.assign(wb);
Logger::pcf_app().error("Found IFace %s MAC %s", iface.c_str(), mac.c_str());
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
return true;
// ifr = {};
// strncpy ((char *) ifr.ifr_name, ifname, IFNAMSIZ);
// if (ioctl(sd, SIOCGIFFLAGS, &ifr) == 0) {
// if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
// if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
// memcpy(pdn_mac_address, ifr.ifr_hwaddr.sa_data, 6);
// }
// }
// }
}
//------------------------------------------------------------------------------
bool util::get_gateway_and_iface(std::string& gw, std::string& iface) {
int received_bytes = 0, msg_len = 0, route_attribute_len = 0;
int sock = -1;
unsigned int msgseq = 0;
struct nlmsghdr *nlh, *nlmsg;
struct rtmsg* route_entry;
// This struct contain route attributes (route type)
struct rtattr* route_attribute;
char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE + 1];
char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
char* ptr = buffer;
struct timeval tv;
if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
perror("socket failed");
return false;
}
memset(msgbuf, 0, sizeof(msgbuf));
memset(gateway_address, 0, sizeof(gateway_address));
memset(interface, 0, sizeof(interface));
memset(buffer, 0, sizeof(buffer));
/* point the header and the msg structure pointers into the buffer */
nlmsg = (struct nlmsghdr*) msgbuf;
/* Fill in the nlmsg header*/
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlmsg->nlmsg_type =
RTM_GETROUTE; // Get the routes from kernel routing table .
nlmsg->nlmsg_flags =
NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.
nlmsg->nlmsg_seq = msgseq++; // Sequence of the message packet.
nlmsg->nlmsg_pid = getpid(); // PID of process sending the request.
/* 1 Sec Timeout to avoid stall */
tv.tv_sec = 1;
setsockopt(
sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval*) &tv,
sizeof(struct timeval));
/* send msg */
if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
perror("send failed");
return false;
}
/* receive response */
do {
received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0);
if (received_bytes < 0) {
perror("Error in recv");
return false;
}
nlh = (struct nlmsghdr*) ptr;
/* Check if the header is valid */
if ((NLMSG_OK(nlmsg, received_bytes) == 0) ||
(nlmsg->nlmsg_type == NLMSG_ERROR)) {
perror("Error in received packet");
return false;
}
/* If we received all data break */
if (nlh->nlmsg_type == NLMSG_DONE)
break;
else {
ptr += received_bytes;
msg_len += received_bytes;
}
/* Break if its not a multi part message */
if ((nlmsg->nlmsg_flags & NLM_F_MULTI) == 0) break;
} while ((nlmsg->nlmsg_seq != msgseq) ||
(nlmsg->nlmsg_pid != static_cast<unsigned int>(getpid())));
/* parse response */
for (; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes)) {
/* Get the route data */
route_entry = (struct rtmsg*) NLMSG_DATA(nlh);
/* We are just interested in main routing table */
if (route_entry->rtm_table != RT_TABLE_MAIN) continue;
route_attribute = (struct rtattr*) RTM_RTA(route_entry);
route_attribute_len = RTM_PAYLOAD(nlh);
/* Loop through all attributes */
for (; RTA_OK(route_attribute, route_attribute_len);
route_attribute = RTA_NEXT(route_attribute, route_attribute_len)) {
switch (route_attribute->rta_type) {
case RTA_OIF:
if_indextoname(*(int*) RTA_DATA(route_attribute), interface);
break;
case RTA_GATEWAY:
inet_ntop(
AF_INET, RTA_DATA(route_attribute), gateway_address,
sizeof(gateway_address));
break;
default:
break;
}
}
if ((*gateway_address) && (*interface)) {
gw.assign(gateway_address);
iface.assign(interface);
break;
}
}
close(sock);
return true;
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file get_gateway_netlink.hpp
\brief
\author Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_GET_GATEWAY_NETLINK_HPP_SEEN
#define FILE_GET_GATEWAY_NETLINK_HPP_SEEN
#include <string>
namespace util {
bool get_iface_l2_addr(const std::string& iface, std::string& mac);
bool get_gateway_and_iface(std::string& gw, std::string& iface);
} // namespace util
#endif /* FILE_GET_GATEWAY_NETLINK_HPP_SEEN */
/* From https://gist.github.com/javiermon/6272065#file-gateway_netlink-c */
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common_defs.h"
#include "if.hpp"
#include "logger.hpp"
#include <errno.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#define BUFFER_SIZE 4096
//------------------------------------------------------------------------------
int get_gateway_and_iface(std::string* gw, std::string* iface) {
int received_bytes = 0, msg_len = 0, route_attribute_len = 0;
int sock = -1;
unsigned int msgseq = 0;
struct nlmsghdr *nlh, *nlmsg;
struct rtmsg* route_entry;
// This struct contain route attributes (route type)
struct rtattr* route_attribute;
char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE];
char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE];
char* ptr = buffer;
struct timeval tv;
int rv = RETURNok;
if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
Logger::system().error("socket raw/NETLINK_ROUTE failed");
return EXIT_FAILURE;
}
memset(msgbuf, 0, sizeof(msgbuf));
memset(gateway_address, 0, sizeof(gateway_address));
memset(interface, 0, sizeof(interface));
memset(buffer, 0, sizeof(buffer));
/* point the header and the msg structure pointers into the buffer */
nlmsg = (struct nlmsghdr*) msgbuf;
/* Fill in the nlmsg header*/
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlmsg->nlmsg_type =
RTM_GETROUTE; // Get the routes from kernel routing table .
nlmsg->nlmsg_flags =
NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.
nlmsg->nlmsg_seq = msgseq++; // Sequence of the message packet.
nlmsg->nlmsg_pid = getpid(); // PID of process sending the request.
/* 1 Sec Timeout to avoid stall */
tv.tv_sec = 1;
setsockopt(
sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval*) &tv,
sizeof(struct timeval));
/* send msg */
if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) {
Logger::system().error("send socket raw/NETLINK_ROUTE failed");
return EXIT_FAILURE;
}
/* receive response */
do {
received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0);
if (received_bytes < 0) {
Logger::system().error("recv socket raw/NETLINK_ROUTE failed");
return EXIT_FAILURE;
}
nlh = (struct nlmsghdr*) ptr;
/* Check if the header is valid */
if ((NLMSG_OK(nlmsg, received_bytes) == 0) ||
(nlmsg->nlmsg_type == NLMSG_ERROR)) {
Logger::system().error("recv msg raw/NETLINK_ROUTE failed");
return EXIT_FAILURE;
}
/* If we received all data break */
if (nlh->nlmsg_type == NLMSG_DONE)
break;
else {
ptr += received_bytes;
msg_len += received_bytes;
}
/* Break if its not a multi part message */
if ((nlmsg->nlmsg_flags & NLM_F_MULTI) == 0) break;
} while ((nlmsg->nlmsg_seq != msgseq) ||
(nlmsg->nlmsg_pid != static_cast<unsigned int>(getpid())));
/* parse response */
for (; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes)) {
/* Get the route data */
route_entry = (struct rtmsg*) NLMSG_DATA(nlh);
/* We are just interested in main routing table */
if (route_entry->rtm_table != RT_TABLE_MAIN) continue;
route_attribute = (struct rtattr*) RTM_RTA(route_entry);
route_attribute_len = RTM_PAYLOAD(nlh);
/* Loop through all attributes */
for (; RTA_OK(route_attribute, route_attribute_len);
route_attribute = RTA_NEXT(route_attribute, route_attribute_len)) {
switch (route_attribute->rta_type) {
case RTA_OIF:
if_indextoname(*(int*) RTA_DATA(route_attribute), interface);
break;
case RTA_GATEWAY:
inet_ntop(
AF_INET, RTA_DATA(route_attribute), gateway_address,
sizeof(gateway_address));
break;
default:
break;
}
}
if ((*gateway_address) && (*interface)) {
*gw = std::string(gateway_address);
if (iface) {
*iface = std::string(interface);
}
break;
} else {
rv = RETURNerror;
}
}
close(sock);
return rv;
}
//------------------------------------------------------------------------------
int get_inet_addr_from_iface(
const std::string& if_name, struct in_addr& inet_addr) {
struct ifreq ifr;
char str[INET_ADDRSTRLEN];
memset(&ifr, 0, sizeof(ifr));
int fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, (const char*) if_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
close(fd);
Logger::system().error(
"Failed to probe %s inet addr: error %s\n", if_name.c_str(),
strerror(errno));
return RETURNerror;
}
close(fd);
struct sockaddr_in* ipaddr = (struct sockaddr_in*) &ifr.ifr_addr;
// check
if (inet_ntop(
AF_INET, (const void*) &ipaddr->sin_addr, str, INET_ADDRSTRLEN) ==
NULL) {
return RETURNerror;
}
inet_addr.s_addr = ipaddr->sin_addr.s_addr;
return RETURNok;
}
//------------------------------------------------------------------------------
int get_mtu_from_iface(const std::string& if_name, uint32_t& mtu) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
int fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, (const char*) if_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFMTU, &ifr)) {
close(fd);
Logger::system().error(
"Failed to probe %s MTU: error %s\n", if_name.c_str(), strerror(errno));
return RETURNerror;
}
close(fd);
mtu = ifr.ifr_mtu;
return RETURNok;
}
//------------------------------------------------------------------------------
int get_inet_addr_infos_from_iface(
const std::string& if_name, struct in_addr& inet_addr,
struct in_addr& inet_network, unsigned int& mtu) {
struct ifreq ifr;
char str[INET_ADDRSTRLEN];
inet_addr.s_addr = INADDR_ANY;
inet_network.s_addr = INADDR_ANY;
mtu = 0;
memset(&ifr, 0, sizeof(ifr));
int fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, (const char*) if_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
close(fd);
Logger::system().error(
"Failed to probe %s inet addr: error %s\n", if_name.c_str(),
strerror(errno));
return RETURNerror;
}
struct sockaddr_in* ipaddr = (struct sockaddr_in*) &ifr.ifr_addr;
// check
if (inet_ntop(
AF_INET, (const void*) &ipaddr->sin_addr, str, INET_ADDRSTRLEN) ==
NULL) {
close(fd);
return RETURNerror;
}
inet_addr.s_addr = ipaddr->sin_addr.s_addr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, (const char*) if_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFNETMASK, &ifr)) {
close(fd);
Logger::system().error(
"Failed to probe %s inet netmask: error %s\n", if_name.c_str(),
strerror(errno));
return RETURNerror;
}
ipaddr = (struct sockaddr_in*) &ifr.ifr_netmask;
// check
if (inet_ntop(
AF_INET, (const void*) &ipaddr->sin_addr, str, INET_ADDRSTRLEN) ==
NULL) {
close(fd);
return RETURNerror;
}
inet_network.s_addr = ipaddr->sin_addr.s_addr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, (const char*) if_name.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFMTU, &ifr)) {
Logger::system().error(
"Failed to probe %s MTU: error %s\n", if_name.c_str(), strerror(errno));
} else {
mtu = ifr.ifr_mtu;
}
close(fd);
return RETURNok;
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file get_gateway_netlink.h
\brief
\author Lionel Gauthier
\company Eurecom
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_IF_HPP_SEEN
#define FILE_IF_HPP_SEEN
#include <string>
int get_gateway_and_iface(std::string* gw /*OUT*/, std::string* iface /*OUT*/);
int get_inet_addr_from_iface(
const std::string& if_name, struct in_addr& inet_addr);
int get_mtu_from_iface(const std::string& if_name, uint32_t& mtu);
int get_inet_addr_infos_from_iface(
const std::string& if_name, struct in_addr& inet_addr,
struct in_addr& inet_netmask, unsigned int& mtu);
#endif /* FILE_IF_HPP_SEEN */
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
*file except in compliance with the License. You may obtain a copy of the
*License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#include "string.hpp"
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <stdarg.h>
#include <regex>
template<class T>
class Buffer {
public:
explicit Buffer(size_t size) {
msize = size;
mbuf = new T[msize];
}
~Buffer() {
if (mbuf) delete[] mbuf;
}
T* get() { return mbuf; }
private:
Buffer();
size_t msize;
T* mbuf;
};
std::string util::string_format(const char* format, ...) {
va_list args;
va_start(args, format);
size_t size = vsnprintf(NULL, 0, format, args) + 1; // Extra space for '\0'
va_end(args);
Buffer<char> buf(size);
va_start(args, format);
vsnprintf(buf.get(), size, format, args);
va_end(args);
return std::string(buf.get(), size - 1); // We don't want the '\0' inside
}
// Licence : https://creativecommons.org/licenses/by-sa/4.0/legalcode
// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring#217605
// trim from start
std::string& util::ltrim(std::string& s) {
s.erase(
s.begin(),
std::find_if(
s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
std::string& util::rtrim(std::string& s) {
s.erase(
std::find_if(
s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace)))
.base(),
s.end());
return s;
}
// trim from both ends
std::string& util::trim(std::string& s) {
return util::ltrim(util::rtrim(s));
}
// extract query param from given querystring
std::string query_param_tmp;
//
std::string util::get_query_param(std::string querystring, std::string param) {
std::regex reList("([^=]*)=([^&]*)&?");
query_param_tmp.clear();
std::for_each(
std::sregex_iterator(querystring.begin(), querystring.end(), reList),
std::sregex_iterator(), [param](std::smatch match) {
if (match[1] == param) {
query_param_tmp = match[2].str().c_str();
return;
}
});
return query_param_tmp;
}
\ No newline at end of file
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
*file except in compliance with the License. You may obtain a copy of the
*License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file string.hpp
\brief
\author Lionel GAUTHIER
\date 2018
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_STRING_HPP_FILE_SEEN
#define FILE_STRING_HPP_FILE_SEEN
#include <string>
namespace util {
std::string string_format(const char* format, ...);
std::string& ltrim(std::string& s);
// trim from end
std::string& rtrim(std::string& s);
// trim from both ends
std::string& trim(std::string& s);
// extract query param from given querystring
std::string get_query_param(std::string querystring, std::string param);
} // namespace util
#endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file uint_uid_generator.hpp
\author Lionel GAUTHIER
\date 2019
\email: lionel.gauthier@eurecom.fr
*/
#ifndef FILE_UINT_GENERATOR_HPP_SEEN
#define FILE_UINT_GENERATOR_HPP_SEEN
#include <mutex>
#include <set>
namespace util {
template<class UINT>
class uint_generator {
private:
UINT uid_generator;
std::mutex m_uid_generator;
std::set<UINT> uid_generated;
std::mutex m_uid_generated;
public:
uint_generator() : m_uid_generator(), m_uid_generated() {
uid_generator = 0;
uid_generated = {};
};
uint_generator(uint_generator const&) = delete;
void operator=(uint_generator const&) = delete;
UINT get_uid() {
std::unique_lock<std::mutex> lr(m_uid_generator);
UINT uid = ++uid_generator;
while (true) {
// may happen race conditions here
std::unique_lock<std::mutex> ld(m_uid_generated);
if (uid_generated.count(uid) == 0) {
uid_generated.insert(uid);
ld.unlock();
lr.unlock();
return uid;
}
uid = ++uid_generator;
}
}
void free_uid(UINT uid) {
std::unique_lock<std::mutex> l(m_uid_generated);
uid_generated.erase(uid);
l.unlock();
}
};
template<class UINT>
class uint_uid_generator {
private:
UINT uid_generator;
std::mutex m_uid_generator;
std::set<UINT> uid_generated;
std::mutex m_uid_generated;
uint_uid_generator() : m_uid_generator(), m_uid_generated() {
uid_generator = 0;
uid_generated = {};
};
public:
static uint_uid_generator& get_instance() {
static uint_uid_generator instance;
return instance;
}
uint_uid_generator(uint_uid_generator const&) = delete;
void operator=(uint_uid_generator const&) = delete;
UINT get_uid() {
std::unique_lock<std::mutex> lr(m_uid_generator);
UINT uid = ++uid_generator;
while (true) {
// may happen race conditions here
std::unique_lock<std::mutex> ld(m_uid_generated);
if (uid_generated.count(uid) == 0) {
uid_generated.insert(uid);
lr.unlock();
ld.unlock();
return uid;
}
uid = ++uid_generator;
}
}
void free_uid(UINT uid) {
std::unique_lock<std::mutex> l(m_uid_generated);
uid_generated.erase(uid);
l.unlock();
}
};
} // namespace util
#endif // FILE_UINT_GENERATOR_HPP_SEEN
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment