Skip to content

Commit

Permalink
https://telecominfraproject.atlassian.net/browse/WIFI-7831
Browse files Browse the repository at this point in the history
Signed-off-by: stephb9959 <[email protected]>
  • Loading branch information
stephb9959 committed Oct 4, 2023
1 parent 29d6f2d commit 60860ad
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 48 deletions.
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,17 @@ INSTALL(TARGETS owgw

target_link_libraries(owgw PUBLIC
${Poco_LIBRARIES}
${ZLIB_LIBRARIES})
${ZLIB_LIBRARIES}
)

if(NOT SMALL_BUILD)
target_link_libraries(owgw PUBLIC
${MySQL_LIBRARIES} ${ZLIB_LIBRARIES}
CppKafka::cppkafka
fmt::fmt
)
${MySQL_LIBRARIES}
${ZLIB_LIBRARIES}
CppKafka::cppkafka
fmt::fmt
resolv
)
if(UNIX AND NOT APPLE)
target_link_libraries(owgw PUBLIC PocoJSON)
endif()
Expand Down
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29
33
79 changes: 37 additions & 42 deletions src/framework/AppServiceRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<Poco::JSON::Object::Ptr>();
}
} catch (...) {
Registry_ = nlohmann::json::parse("{}");
Registry_ = Poco::makeShared<Poco::JSON::Object>();
}
}

Expand All @@ -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<std::string> &V) {
Poco::JSON::Array Arr;
for(const auto &s:V) {
Arr.add(s);
}
Registry_->set(key,Arr);
Save();
}

template<class T> 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();
}

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<bool>();
return true;
}
return false;
}

inline bool Get(const char *Key, uint64_t &Value) {
if (Registry_[Key].is_number_unsigned()) {
Value = Registry_[Key].get<uint64_t>();
return true;
}
return false;
}

inline bool Get(const char *Key, std::string &Value) {
if (Registry_[Key].is_string()) {
Value = Registry_[Key].get<std::string>();
return true;
}
return false;
}
bool Get(const char *key, std::vector<std::string> &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;
}

template<class T> bool Get(const char *key, T &Value) {
if(Registry_->has(key) && !Registry_->isNull(key)) {
Value = Registry_->getValue<T>(key);
return true;
}
return false;
}

private:
std::string FileName;
nlohmann::json Registry_;
Poco::JSON::Object::Ptr Registry_;
};

inline auto AppServiceRegistry() { return AppServiceRegistry::instance(); }
Expand Down
1 change: 1 addition & 0 deletions src/framework/ow_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
76 changes: 76 additions & 0 deletions src/framework/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <string>
#include <algorithm>

#include <resolv.h>

namespace OpenWifi::Utils {

bool NormalizeMac(std::string &Mac) {
Expand Down Expand Up @@ -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<NAPTRRecord> getNAPTRRecords(const std::string& domain) {
std::vector<NAPTRRecord> 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<SrvRecord> getSRVRecords(const std::string& domain) {
std::vector<SrvRecord> 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
34 changes: 34 additions & 0 deletions src/framework/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,38 @@ namespace OpenWifi::Utils {
bool ValidX509Certificate(const std::string &Cert);
bool ValidX509Certificate(const std::vector<std::string> &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<NAPTRRecord> 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<SrvRecord> getSRVRecords(const std::string& domain);

struct HostNameServerResult{
std::string Hostname;
uint32_t Port;
};


} // namespace OpenWifi::Utils

0 comments on commit 60860ad

Please sign in to comment.