From 48b8705fc68a39b007ddc2b32145e7561b6af428 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 4 Oct 2023 08:53:59 -0700 Subject: [PATCH] https://telecominfraproject.atlassian.net/browse/WIFI-7831 Signed-off-by: stephb9959 --- CMakeLists.txt | 1 + build | 2 +- src/framework/AppServiceRegistry.h | 77 ++++++++++++++---------------- src/framework/ow_constants.h | 1 + src/framework/utils.cpp | 76 +++++++++++++++++++++++++++++ src/framework/utils.h | 34 +++++++++++++ 6 files changed, 149 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b3f6c7..31d8c0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,6 +157,7 @@ target_link_libraries( owfms PUBLIC ${ZLIB_LIBRARIES} ${AWSSDK_LINK_LIBRARIES} fmt::fmt + resolv CppKafka::cppkafka ) diff --git a/build b/build index 9d60796..da2d398 100644 --- a/build +++ b/build @@ -1 +1 @@ -11 \ No newline at end of file +14 \ No newline at end of file diff --git a/src/framework/AppServiceRegistry.h b/src/framework/AppServiceRegistry.h index 4855c67..49c1e90 100644 --- a/src/framework/AppServiceRegistry.h +++ b/src/framework/AppServiceRegistry.h @@ -11,10 +11,12 @@ #include "Poco/File.h" #include "Poco/StreamCopier.h" +#include "Poco/JSON/Object.h" +#include "Poco/JSON/Parser.h" #include "framework/MicroServiceFuncs.h" -#include "nlohmann/json.hpp" +// #include "nlohmann/json.hpp" namespace OpenWifi { @@ -28,11 +30,11 @@ namespace OpenWifi { if (F.exists()) { std::ostringstream OS; std::ifstream IF(FileName); - Poco::StreamCopier::copyStream(IF, OS); - Registry_ = nlohmann::json::parse(OS.str()); + Poco::JSON::Parser P; + Registry_ = P.parse(IF).extract(); } } catch (...) { - Registry_ = nlohmann::json::parse("{}"); + Registry_ = Poco::makeShared(); } } @@ -44,54 +46,47 @@ namespace OpenWifi { inline ~AppServiceRegistry() { Save(); } inline void Save() { - std::istringstream IS(to_string(Registry_)); std::ofstream OF; OF.open(FileName, std::ios::binary | std::ios::trunc); - Poco::StreamCopier::copyStream(IS, OF); + Registry_->stringify(OF); } - inline void Set(const char *Key, uint64_t Value) { - Registry_[Key] = Value; + void Set(const char *key, const std::vector &V) { + Poco::JSON::Array Arr; + for(const auto &s:V) { + Arr.add(s); + } + Registry_->set(key,Arr); + Save(); + } + + template void Set(const char *key, const T &Value) { + Registry_->set(key,Value); Save(); } - inline void Set(const char *Key, const std::string &Value) { - Registry_[Key] = Value; - Save(); - } + bool Get(const char *key, std::vector &Value) { + if(Registry_->has(key) && !Registry_->isNull(key) && Registry_->isArray(key)) { + auto Arr = Registry_->get(key); + for(const auto &v:Arr) { + Value.emplace_back(v); + } + return true; + } + return false; + } - inline void Set(const char *Key, bool Value) { - Registry_[Key] = Value; - Save(); - } - - inline bool Get(const char *Key, bool &Value) { - if (Registry_[Key].is_boolean()) { - Value = Registry_[Key].get(); - return true; - } - return false; - } - - inline bool Get(const char *Key, uint64_t &Value) { - if (Registry_[Key].is_number_unsigned()) { - Value = Registry_[Key].get(); - return true; - } - return false; - } - - inline bool Get(const char *Key, std::string &Value) { - if (Registry_[Key].is_string()) { - Value = Registry_[Key].get(); - return true; - } - return false; - } + template bool Get(const char *key, T &Value) { + if(Registry_->has(key) && !Registry_->isNull(key)) { + Value = Registry_->getValue(key); + return true; + } + return false; + } private: std::string FileName; - nlohmann::json Registry_; + Poco::JSON::Object::Ptr Registry_; }; inline auto AppServiceRegistry() { return AppServiceRegistry::instance(); } diff --git a/src/framework/ow_constants.h b/src/framework/ow_constants.h index d45ba28..33f8819 100644 --- a/src/framework/ow_constants.h +++ b/src/framework/ow_constants.h @@ -429,6 +429,7 @@ namespace OpenWifi::RESTAPI::Errors { static const struct msg InvalidRadsecIPAddress { 1188, "Invalid Radsec IP Address." }; static const struct msg InvalidRadsecPort { 1189, "Invalid Radsec Port." }; static const struct msg InvalidRadsecSecret { 1190, "Invalid Radsec Secret." }; + static const struct msg InvalidRadiusServer { 1191, "Invalid Radius Server." }; static const struct msg SimulationDoesNotExist { 7000, "Simulation Instance ID does not exist." diff --git a/src/framework/utils.cpp b/src/framework/utils.cpp index 3ec4a3d..f526e79 100644 --- a/src/framework/utils.cpp +++ b/src/framework/utils.cpp @@ -14,6 +14,8 @@ #include #include +#include + namespace OpenWifi::Utils { bool NormalizeMac(std::string &Mac) { @@ -866,4 +868,78 @@ namespace OpenWifi::Utils { return password; } +// Function to query NAPTR records for a domain and return them in a vector + std::vector getNAPTRRecords(const std::string& domain) { + std::vector naptrRecords; + + unsigned char buf[4096]; + ns_msg handle; + ns_initparse(buf, NS_PACKETSZ, &handle); + + // Query NAPTR records for the given domain + int response = res_query(domain.c_str(), ns_c_in, ns_t_naptr, buf, sizeof(buf)); + if (response < 0) { + return naptrRecords; + } + + if(ns_initparse(buf, response, &handle) < 0) { + return naptrRecords; + } + + // Iterate through the DNS response and extract NAPTR records + int count = ns_msg_count(handle, ns_s_an); + for (int i = 0; i < count; ++i) { + ns_rr rr; + if (ns_parserr(&handle, ns_s_an, i, &rr) == 0) { + char rdata[256]; + ns_sprintrr(&handle, &rr, nullptr, nullptr, rdata, sizeof(rdata)); + NAPTRRecord record; + std::istringstream os(rdata); + os >> record.name >> record.ttl >> record.rclass >> record.rtype >> record.order >> record.preference >> record.flags + >> record.service >> record.regexp >> record.replacement; + naptrRecords.push_back(record); + } + } + + return naptrRecords; + } + + std::vector getSRVRecords(const std::string& domain) { + std::vector srvRecords; + + // Buffer to hold the DNS response + unsigned char buf[4096]; + ns_msg handle; + ns_initparse(buf, NS_PACKETSZ, &handle); + + // Query NAPTR records for the given domain + int response = res_query(domain.c_str(), ns_c_in, ns_t_srv, buf, sizeof(buf)); + if (response < 0) { + std::cerr << "DNS query failed for " << domain << ": " << hstrerror(h_errno) << std::endl; + return srvRecords; + } + + if(ns_initparse(buf, response, &handle) < 0) { + return srvRecords; + } + + // Iterate through the DNS response and extract NAPTR records + int count = ns_msg_count(handle, ns_s_an); + for (int i = 0; i < count; ++i) { + ns_rr rr; + if (ns_parserr(&handle, ns_s_an, i, &rr) == 0) { + char rdata[256]; + ns_sprintrr(&handle, &rr, nullptr, nullptr, rdata, sizeof(rdata)); + SrvRecord record; + std::istringstream os(rdata); + os >> record.name >> record.ttl >> record.rclass >> record.rtype >> record.pref >> record.weight >> + record.port >> record.srvname ; + srvRecords.push_back(record); + } + } + + return srvRecords; + } + + } // namespace OpenWifi::Utils diff --git a/src/framework/utils.h b/src/framework/utils.h index 669e7b4..fa0d58d 100644 --- a/src/framework/utils.h +++ b/src/framework/utils.h @@ -283,4 +283,38 @@ namespace OpenWifi::Utils { bool ValidX509Certificate(const std::string &Cert); bool ValidX509Certificate(const std::vector &Certs); + struct NAPTRRecord { + std::string name; + std::string ttl; + std::string rclass; + std::string rtype; + uint32_t order=0; + uint32_t preference=0; + std::string flags; + std::string service; + std::string regexp; + std::string replacement; + }; + +// Function to query NAPTR records for a domain and return them in a vector + std::vector getNAPTRRecords(const std::string& domain); + struct SrvRecord { + std::string name; + std::string ttl; + std::string rclass; + std::string rtype; + uint32_t pref = 0; + uint32_t weight = 0; + uint32_t port = 0; + std::string srvname; + }; + + std::vector getSRVRecords(const std::string& domain); + + struct HostNameServerResult{ + std::string Hostname; + uint32_t Port; + }; + + } // namespace OpenWifi::Utils