diff --git a/reflector/Configure.cpp b/reflector/Configure.cpp index b59db6d..c6546c5 100644 --- a/reflector/Configure.cpp +++ b/reflector/Configure.cpp @@ -49,7 +49,6 @@ #define JFILES "Files" #define JFILEPATH "FilePath" #define JG3TERMINALPATH "G3TerminalPath" -#define JHOSTNAME "Hostname" #define JINTERLINKPATH "InterlinkPath" #define JIPADDRESSES "IpAddresses" #define JIPV4BINDING "IPv4Binding" @@ -73,11 +72,11 @@ #define JREGISTRATIONID "RegistrationID" #define JREGISTRATIONNAME "RegistrationName" #define JSPONSOR "Sponsor" -#define JSUFFIX "Suffix" #define JSYSOPEMAIL "SysopEmail" #define JTRANSCODED "Transcoded" #define JTRANSCODER "Transcoder" #define JURF "URF" +#define JURL "URL" #define JUSRP "USRP" #define JWHITELISTPATH "WhitelistPath" #define JYSF "YSF" @@ -142,11 +141,10 @@ bool CConfigure::ReadData(const std::string &path) { ipv4.assign(ss.str()); trim(ipv4); - ss.clear(); } + ss.str(std::string()); if (CURLE_OK == curl.GetURL("https://ipv6.icanhazip.com", ss)) { - std::cout << ss.str(); ipv6.assign(ss.str()); trim(ipv6); } @@ -436,10 +434,8 @@ bool CConfigure::ReadData(const std::string &path) case ESection::nxdnid: pdb = &g_Keys.nxdniddb; break; case ESection::ysffreq: pdb = &g_Keys.ysftxrxdb; break; } - if (0 == key.compare(JHOSTNAME)) - data[pdb->hostname] = value; - else if (0 == key.compare(JSUFFIX)) - data[pdb->suffix] = value; + if (0 == key.compare(JURL)) + data[pdb->url] = value; else if (0 == key.compare(JMODE)) { if ((0==value.compare("file")) || (0==value.compare("http")) || (0==value.compare("both"))) @@ -562,7 +558,6 @@ bool CConfigure::ReadData(const std::string &path) data[g_Keys.ip.ipv6address] = ipv6; else { - std::cout << ipv6; std::cerr << "ERROR: could not detect IPv6 address at this time" << std::endl; rval = true; } @@ -670,8 +665,7 @@ bool CConfigure::ReadData(const std::string &path) }; for ( auto &item : dbs ) { - isDefined(ErrorLevel::fatal, item.first, JHOSTNAME, item.second->hostname, rval); - isDefined(ErrorLevel::fatal, item.first, JSUFFIX, item.second->suffix, rval); + isDefined(ErrorLevel::fatal, item.first, JURL, item.second->url, rval); isDefined(ErrorLevel::fatal, item.first, JMODE, item.second->mode, rval); isDefined(ErrorLevel::fatal, item.first, JREFRESHMIN, item.second->refreshmin, rval); isDefined(ErrorLevel::fatal, item.first, JFILEPATH, item.second->filepath, rval); diff --git a/reflector/JsonKeys.h b/reflector/JsonKeys.h index 02921bc..83a2b03 100644 --- a/reflector/JsonKeys.h +++ b/reflector/JsonKeys.h @@ -48,10 +48,10 @@ struct SJsonKeys { struct YSLREG { const std::string id, name, description; } ysfreflectordb; } ysf { "YSFPort", "YSFAutoLinkMod", "YSFDefaultTxFreq", "YSFDefaultRxFreq", { "ysfrefdbid", "ysfrefdbname", "ysfrefdbdesc" } }; - struct DB { const std::string hostname, suffix, mode, refreshmin, filepath; } - dmriddb { "dmrIdDbHost", "dmrIdDbSuffix", "dmrIdDbMode", "dmrIdDbRefresh", "dmrIdDbFilePath" }, - nxdniddb { "nxdnIdDbHost", "nxdnIdDbSuffix", "nxdnIdDbMode", "nxdnIdDbRefresh", "nxdnIdDbFilePath" }, - ysftxrxdb { "ysfIdDbHost", "ysfIdDbSuffix", "ysfIdDbMode", "ysfIdDbRefresh", "ysfIdDbFilePath" }; + struct DB { const std::string url, mode, refreshmin, filepath; } + dmriddb { "dmrIdDbUrl", "dmrIdDbMode", "dmrIdDbRefresh", "dmrIdDbFilePath" }, + nxdniddb { "nxdnIdDbUrl", "nxdnIdDbMode", "nxdnIdDbRefresh", "nxdnIdDbFilePath" }, + ysftxrxdb { "ysfIdDbUrl", "ysfIdDbMode", "ysfIdDbRefresh", "ysfIdDbFilePath" }; struct FILES { const std::string pid, json, white, black, interlink, terminal; } files { "pidFilePath", "jsonFilePath", "whitelistFilePath", "blacklistFilePath", "interlinkFilePath", "g3TerminalFilePath" }; }; diff --git a/reflector/Lookup.cpp b/reflector/Lookup.cpp index dcb8599..5d4d07e 100644 --- a/reflector/Lookup.cpp +++ b/reflector/Lookup.cpp @@ -16,16 +16,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include +#include +#include #include #include +#include "CurlGet.h" #include "Lookup.h" -CLookup::~CLookup() -{ - LookupClose(); -} - void CLookup::LookupClose() { keep_running = false; @@ -52,54 +49,78 @@ void CLookup::LookupInit() void CLookup::Thread() { + const unsigned long wait_cycles = m_Refresh * 6u; // the number of while loops in m_Refresh + unsigned long count = 0; while (keep_running) { - bool loaded = false; + std::stringstream ss; + bool http_loaded = false; + bool file_loaded = false; - if (m_Type != ERefreshType::file) // get the HTTP contents + // load http section first, if configured and m_Refresh minutes have lapsed + // on the first pass through this while loop (count == 0) + if (ERefreshType::file != m_Type && 0ul == count++ % wait_cycles) { - CBuffer buf; - loaded = LoadContentHttp(buf); - if (loaded) - { - Lock(); - ClearContents(); - RefreshContentHttp(buf); - Unlock(); - } + // if SIG_INT was received at this point in time, + // in might take a bit more than 10 seconds to soft close + http_loaded = LoadContentHttp(ss); } - - if (m_Type != ERefreshType::http) // get the file contents + // load the file if http was loaded or if we haven't loaded since the last mod time + if (ERefreshType::http != m_Type) { - auto lastTime = GetLastModTime(); - if (lastTime > m_LastModTime) + GetLastModTime(); + if (http_loaded || m_LastLoadTime < m_LastModTime) { - CBuffer buf; - if (LoadContentFile(buf)) - { - Lock(); - if (! loaded) - ClearContents(); - RefreshContentFile(buf); - Unlock(); - m_LastModTime = lastTime; - } + file_loaded = LoadContentFile(ss); + time(&m_LastLoadTime); } } - // now wait for a while... - for (unsigned i=0; i<20u*m_Refresh && keep_running; i++) - std::this_thread::sleep_for(std::chrono::seconds(3)); + // now update the map(s) if anything was loaded + if (http_loaded || file_loaded) + { + Lock(); + // if m_Type == ERefreshType::both, and if something was deleted from the file, + // it won't be purged from the map(s) until http is loaded + // It would be a lot of work (iterating on an unordered_map) to do otherwise! + if (http_loaded || ERefreshType::file == m_Type) + ClearContents(); + UpdateContent(ss); + Unlock(); + } + + // now wait for 10 seconds + std::this_thread::sleep_for(std::chrono::seconds(10)); } } +bool CLookup::LoadContentHttp(std::stringstream &ss) +{ + CCurlGet get; + auto code = get.GetURL(m_Url, ss); + return CURLE_OK == code; +} + +bool CLookup::LoadContentFile(std::stringstream &ss) +{ + bool rval = false; + std::ifstream file(m_Path); + if ( file ) + { + ss << file.rdbuf(); + file.close(); + rval = true; + } + return rval; +} + bool CLookup::Dump() { - CBuffer buf; + std::stringstream ss; LoadParameters(); - auto rval = LoadContentHttp(buf); + auto rval = LoadContentHttp(ss); if (rval) - std::cout << (const char *)buf.data(); + std::cout << ss.str() << std::endl; return rval; } diff --git a/reflector/Lookup.h b/reflector/Lookup.h index db52d2e..f921c81 100644 --- a/reflector/Lookup.h +++ b/reflector/Lookup.h @@ -18,20 +18,20 @@ #pragma once -#include #include #include -#include "Buffer.h" +#include #include "Callsign.h" #include "Configure.h" // compare function for std::map::find -struct CCallsignCompare +struct CCallsignHash { - bool operator() (const CCallsign &cs1, const CCallsign &cs2) const + std::size_t operator() (const CCallsign &cs) const { - return cs1.HasLowerCallsign(cs2); + std::hash hash; + return hash(cs.GetCS()); } }; @@ -42,10 +42,7 @@ class CLookup { public: // constructor - CLookup() : keep_running(true), m_LastModTime(0) {} - - // destructor - virtual ~CLookup(); + CLookup() : keep_running(true), m_LastModTime(0), m_LastLoadTime(0) {} void LookupInit(); void LookupClose(); @@ -62,16 +59,15 @@ protected: void Thread(); // refresh - virtual bool LoadContentFile(CBuffer &buf) = 0; - virtual bool LoadContentHttp(CBuffer &buf) = 0; - virtual void RefreshContentFile(const CBuffer &) = 0; - virtual void RefreshContentHttp(const CBuffer &) = 0; + bool LoadContentHttp(std::stringstream &ss); + bool LoadContentFile(std::stringstream &ss); + virtual void UpdateContent(std::stringstream &ss) = 0; std::mutex m_Mutex; ERefreshType m_Type; unsigned m_Refresh; - std::string m_Path, m_Host, m_Suffix; - std::time_t m_LastModTime; + std::string m_Path, m_Url; + std::time_t m_LastModTime, m_LastLoadTime; std::atomic keep_running; std::future m_Future; diff --git a/reflector/LookupDmr.cpp b/reflector/LookupDmr.cpp index 3b6097f..14b3a0f 100644 --- a/reflector/LookupDmr.cpp +++ b/reflector/LookupDmr.cpp @@ -17,7 +17,7 @@ // along with this program. If not, see . #include -#include +#include #include #include @@ -34,8 +34,7 @@ void CLookupDmr::LoadParameters() m_Type = g_Conf.GetRefreshType(g_Keys.dmriddb.mode); m_Refresh = g_Conf.GetUnsigned(g_Keys.dmriddb.refreshmin); m_Path.assign(g_Conf.GetString(g_Keys.dmriddb.filepath)); - m_Host.assign(g_Conf.GetString(g_Keys.dmriddb.hostname)); - m_Suffix.assign(g_Conf.GetString(g_Keys.dmriddb.suffix)); + m_Url.assign(g_Conf.GetString(g_Keys.dmriddb.url)); } uint32_t CLookupDmr::FindDmrid(const CCallsign &callsign) @@ -59,198 +58,27 @@ const CCallsign *CLookupDmr::FindCallsign(uint32_t dmrid) return nullptr; } -bool CLookupDmr::LoadContentFile(CBuffer &buffer) +void CLookupDmr::UpdateContent(std::stringstream &ss) { - buffer.clear(); - std::ifstream file; - std::streampos size; - - // open file - file.open(m_Path, std::ios::in | std::ios::binary | std::ios::ate); - if ( file.is_open() ) - { - // read file - size = file.tellg(); - if ( size > 0 ) - { - // read file into buffer - buffer.resize((int)size+1); - file.seekg(0, std::ios::beg); - file.read((char *)buffer.data(), (int)size); - } - file.close(); - } - - // done - return buffer.size() > 0; -} - -bool CLookupDmr::LoadContentHttp(CBuffer &buf) -{ - // get file from http://xlxapi.rlx.lu/api/exportdmr.php - return HttpGet(m_Host.c_str(), m_Suffix.c_str(), 80, buf); -} - -void CLookupDmr::RefreshContentFile(const CBuffer &buffer) -{ - // crack it - char *ptr1 = (char *)buffer.data(); - char *ptr2; - - // get next line - while ( (ptr2 = strchr(ptr1, '\n')) != nullptr ) - { - *ptr2 = 0; - // get items - char *dmrid; - char *callsign; - if ( ((dmrid = strtok(ptr1, ";")) != nullptr) && IsValidDmrId(dmrid) ) - { - if ( ((callsign = ::strtok(nullptr, ";")) != nullptr) ) - { - // new entry - uint32_t ui = atoi(dmrid); - CCallsign cs(callsign, ui); - if ( cs.IsValid() ) - { - m_CallsignMap.insert(std::pair(ui, cs)); - m_DmridMap.insert(std::pair(cs,ui)); - } - } - } - // next line - ptr1 = ptr2+1; - } - - std::cout << "Read " << m_DmridMap.size() << " DMR ids from file " << m_Refresh << std::endl; -} - -void CLookupDmr::RefreshContentHttp(const CBuffer &buffer) -{ - char *ptr1 = (char *)buffer.data(); - char *ptr2; - // get next line - while ( (ptr2 = strchr(ptr1, '\n')) != nullptr ) + std::string line; + while (std::getline(ss, line)) { - *ptr2 = 0; - // get items - char *dmrid; - char *callsign; - if ( ((dmrid = strtok(ptr1, ";")) != nullptr) && IsValidDmrId(dmrid) ) + std::string cs_str, id_str; + std::istringstream iss(line); + std::getline(iss, id_str, ';'); + std::getline(iss, cs_str, ';'); + auto lid = stol(id_str); + if (lid > 0 && lid < 0x1000000 && cs_str.size() < CALLSIGN_LEN) { - if ( ((callsign = strtok(nullptr, ";")) != nullptr) ) - { - // new entry - uint32_t ui = atoi(dmrid); - CCallsign cs(callsign, ui); - if ( cs.IsValid() ) - { - m_CallsignMap.insert(std::pair(ui, cs)); - m_DmridMap.insert(std::pair(cs,ui)); - } - } - } - // next line - ptr1 = ptr2+1; - } - - std::cout << "Read " << m_DmridMap.size() << " DMR ids from " << m_Host << " database " << std::endl; -} - -bool CLookupDmr::IsValidDmrId(const char *sz) -{ - bool ok = false; - size_t n = strlen(sz); - if ( (n > 0) && (n <= 8) ) - { - ok = true; - for ( size_t i = 0; (i < n) && ok; i++ ) - { - ok = ok && isdigit(sz[i]); - } - } - return ok; -} - -#define DMRID_HTTPGET_SIZEMAX (256) - -bool CLookupDmr::HttpGet(const char *hostname, const char *filename, int port, CBuffer &buffer) -{ - buffer.clear(); - int sock_id; - - // open socket - if ( (sock_id = socket(AF_INET, SOCK_STREAM, 0)) >= 0 ) - { - // get hostname address - struct sockaddr_in servaddr; - struct hostent *hp; - memset(&servaddr, 0, sizeof(servaddr)); - if( (hp = gethostbyname(hostname)) != nullptr) - { - // dns resolved - memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length); - servaddr.sin_port = htons(port); - servaddr.sin_family = AF_INET; - - // connect - if (connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) == 0) - { - // send the GET request - char request[DMRID_HTTPGET_SIZEMAX]; - ::sprintf(request, "GET /%s HTTP/1.0\r\nFrom: %s\r\nUser-Agent: urfd\r\n\r\n", filename, g_Conf.GetString(g_Keys.names.cs).c_str()); - ::write(sock_id, request, strlen(request)); - - // config receive timeouts - fd_set read_set; - struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; - FD_ZERO(&read_set); - FD_SET(sock_id, &read_set); - - // get the reply back - bool done = false; - do - { - char buf[1440]; - ssize_t len = 0; - select(sock_id+1, &read_set, nullptr, nullptr, &timeout); - //if ( (ret > 0) || ((ret < 0) && (errno == EINPROGRESS)) ) - //if ( ret >= 0 ) - //{ - usleep(5000); - len = read(sock_id, buf, 1440); - if ( len > 0 ) - { - buffer.Append((uint8_t *)buf, (int)len); - } - //} - done = (len <= 0); - - } - while (!done); - buffer.Append((uint8_t)0); - - // and disconnect - close(sock_id); - } - else - { - std::cout << "Cannot establish connection with host " << hostname << std::endl; - } + auto id = uint32_t(lid); + CCallsign cs(cs_str.c_str(), id); + m_DmridMap[cs] = id; + m_CallsignMap[id] = cs; } else { - std::cout << "Host " << hostname << " not found" << std::endl; + std::cout << "DMR Id '" << id_str << ';' << cs_str << ";' is malformed" << std::endl; } - } - else - { - std::cout << "Failed to open wget socket" << std::endl; - } - - // done - return buffer.size() > 1; + std::cout << "DMR Id database size now is " << m_DmridMap.size() << std::endl; } diff --git a/reflector/LookupDmr.h b/reflector/LookupDmr.h index d0e478b..360262f 100644 --- a/reflector/LookupDmr.h +++ b/reflector/LookupDmr.h @@ -23,21 +23,16 @@ class CLookupDmr : public CLookup { public: + ~CLookupDmr() {} uint32_t FindDmrid(const CCallsign &cs); const CCallsign *FindCallsign(uint32_t dmrid); protected: void ClearContents(); void LoadParameters(); - bool LoadContentFile(CBuffer &buf); - bool LoadContentHttp(CBuffer &buf); - void RefreshContentFile(const CBuffer &); - void RefreshContentHttp(const CBuffer &); + void UpdateContent(std::stringstream &ss); private: - std::map m_CallsignMap; - std::map m_DmridMap; - - bool IsValidDmrId(const char *); - bool HttpGet(const char *, const char *, int, CBuffer &); + std::unordered_map m_CallsignMap; + std::unordered_map m_DmridMap; }; diff --git a/reflector/LookupNxdn.cpp b/reflector/LookupNxdn.cpp index 1aac8fa..6f5e7d9 100644 --- a/reflector/LookupNxdn.cpp +++ b/reflector/LookupNxdn.cpp @@ -34,8 +34,7 @@ void CLookupNxdn::LoadParameters() m_Type = g_Conf.GetRefreshType(g_Keys.nxdniddb.mode); m_Refresh = g_Conf.GetUnsigned(g_Keys.nxdniddb.refreshmin); m_Path.assign(g_Conf.GetString(g_Keys.nxdniddb.filepath)); - m_Host.assign(g_Conf.GetString(g_Keys.nxdniddb.hostname)); - m_Suffix.assign(g_Conf.GetString(g_Keys.nxdniddb.suffix)); + m_Url.assign(g_Conf.GetString(g_Keys.nxdniddb.url)); } const CCallsign *CLookupNxdn::FindCallsign(uint16_t nxdnid) @@ -58,203 +57,27 @@ uint16_t CLookupNxdn::FindNXDNid(const CCallsign &callsign) return 0; } -bool CLookupNxdn::LoadContentFile(CBuffer &buffer) +void CLookupNxdn::UpdateContent(std::stringstream &ss) { - buffer.clear(); - std::ifstream file; - std::streampos size; - - // open file - file.open(m_Path, std::ios::in | std::ios::binary | std::ios::ate); - if ( file.is_open() ) + std::string line; + while (std::getline(ss, line)) { - // read file - size = file.tellg(); - if ( size > 0 ) + std::string cs_str, id_str; + std::istringstream iss(line); + std::getline(iss, id_str, ','); + std::getline(iss, cs_str, ','); + auto lid = stol(id_str); + if (lid > 0 && lid < 0x10000 && cs_str.size() < CALLSIGN_LEN) { - // read file into buffer - buffer.resize((int)size+1); - file.seekg (0, std::ios::beg); - file.read((char *)buffer.data(), (int)size); - - // close file - file.close(); - - // done - } - } - - // done - return buffer.size() > 0; -} - -bool CLookupNxdn::LoadContentHttp(CBuffer &buffer) -{ - // get file from xlxapi server - return HttpGet(m_Host.c_str(), m_Suffix.c_str(), 80, buffer); -} - -void CLookupNxdn::RefreshContentFile(const CBuffer &buffer) -{ - // crack it - char *ptr1 = (char *)buffer.data(); - char *ptr2; - - // get next line - while ( (ptr2 = ::strchr(ptr1, '\n')) != nullptr ) - { - *ptr2 = 0; - // get items - char *nxdnid; - char *callsign; - if ( ((nxdnid = ::strtok(ptr1, ",")) != nullptr) && IsValidNxdnId(nxdnid) ) - { - if ( ((callsign = ::strtok(nullptr, ",")) != nullptr) ) - { - // new entry - uint16_t us = atoi(nxdnid); - CCallsign cs(callsign, 0, us); - if ( cs.IsValid() ) - { - m_CallsignMap.insert(std::pair(us, cs)); - m_NxdnidMap.insert(std::pair(cs,us)); - } - } - } - // next line - ptr1 = ptr2+1; - } - - std::cout << "Read " << m_NxdnidMap.size() << " NXDN ids from file " << m_Path << std::endl; -} - -void CLookupNxdn::RefreshContentHttp(const CBuffer &buffer) -{ - char *ptr1 = (char *)buffer.data(); - char *ptr2; - // get next line - while ( (ptr2 = strchr(ptr1, '\n')) != nullptr ) - { - std::cout << "newline: " << std::string(ptr2) << std::endl; - *ptr2 = 0; - // get items - char *nxdnid; - char *callsign; - if ( ((nxdnid = ::strtok(ptr1, ",")) != nullptr) && IsValidNxdnId(nxdnid) ) - { - if ( ((callsign = ::strtok(nullptr, ",")) != nullptr) ) - { - // new entry - uint16_t us = atoi(nxdnid); - CCallsign cs(callsign, 0, us); - if ( cs.IsValid() ) - { - m_CallsignMap.insert(std::pair(us, cs)); - m_NxdnidMap.insert(std::pair(cs,us)); - } - } - } - // next line - ptr1 = ptr2+1; - } - - std::cout << "Read " << m_NxdnidMap.size() << " NXDN ids from " << m_Host << std::endl; -} - -bool CLookupNxdn::IsValidNxdnId(const char *sz) -{ - bool ok = false; - size_t n = ::strlen(sz); - if ( (n > 0) && (n <= 5) ) - { - ok = true; - for ( size_t i = 0; (i < n) && ok; i++ ) - { - ok = ok && isdigit(sz[i]); - } - } - return ok; -} - -#define NXDNID_HTTPGET_SIZEMAX (256) - -bool CLookupNxdn::HttpGet(const char *hostname, const char *filename, int port, CBuffer &buffer) -{ - int sock_id; - buffer.clear(); - - // open socket - if ( (sock_id = socket(AF_INET, SOCK_STREAM, 0)) >= 0 ) - { - // get hostname address - struct sockaddr_in servaddr; - struct hostent *hp; - memset(&servaddr,0,sizeof(servaddr)); - if( (hp = gethostbyname(hostname)) != nullptr ) - { - // dns resolved - memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length); - servaddr.sin_port = htons(port); - servaddr.sin_family = AF_INET; - - // connect - if ( ::connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) == 0) - { - // send the GET request - char request[NXDNID_HTTPGET_SIZEMAX]; - ::sprintf(request, "GET /%s HTTP/1.0\r\nFrom: %s\r\nUser-Agent: urfd\r\n\r\n", filename, g_Conf.GetString(g_Keys.names.cs).c_str()); - ::write(sock_id, request, strlen(request)); - - // config receive timeouts - fd_set read_set; - struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; - FD_ZERO(&read_set); - FD_SET(sock_id, &read_set); - - // get the reply back - bool done = false; - do - { - char buf[1440]; - ssize_t len = 0; - select(sock_id+1, &read_set, nullptr, nullptr, &timeout); - //if ( (ret > 0) || ((ret < 0) && (errno == EINPROGRESS)) ) - //if ( ret >= 0 ) - //{ - usleep(5000); - len = read(sock_id, buf, 1440); - if ( len > 0 ) - { - buffer.Append((uint8_t *)buf, (int)len); - } - //} - done = (len <= 0); - - } - while (!done); - buffer.Append((uint8_t)0); - - // and disconnect - close(sock_id); - } - else - { - std::cout << "Cannot establish connection with host " << hostname << std::endl; - } + auto id = uint16_t(lid); + CCallsign cs(cs_str.c_str(), 0, id); + m_NxdnidMap[cs] = id; + m_CallsignMap[id] = cs; } else { - std::cout << "Host " << hostname << " not found" << std::endl; + std::cout << "NXDN Id '" << id_str << ',' << cs_str << ",' is malformed" << std::endl; } - } - else - { - std::cout << "Failed to open wget socket" << std::endl; - } - - // done - return buffer.size() > 1; + std::cout << "NXDN Id database size now is " << m_NxdnidMap.size() << std::endl; } diff --git a/reflector/LookupNxdn.h b/reflector/LookupNxdn.h index 36f2be6..946ab9a 100644 --- a/reflector/LookupNxdn.h +++ b/reflector/LookupNxdn.h @@ -28,15 +28,9 @@ public: protected: void ClearContents(); void LoadParameters(); - bool LoadContentFile(CBuffer &buf); - bool LoadContentHttp(CBuffer &buf); - void RefreshContentFile(const CBuffer &); - void RefreshContentHttp(const CBuffer &); + void UpdateContent(std::stringstream &ss); private: - std::map m_CallsignMap; - std::map m_NxdnidMap; - - bool IsValidNxdnId(const char *); - bool HttpGet(const char *, const char *, int, CBuffer &); + std::unordered_map m_CallsignMap; + std::unordered_map m_NxdnidMap; }; diff --git a/reflector/LookupYsf.cpp b/reflector/LookupYsf.cpp index 382229f..2b45a12 100644 --- a/reflector/LookupYsf.cpp +++ b/reflector/LookupYsf.cpp @@ -17,7 +17,7 @@ // along with this program. If not, see . #include -#include +#include #include #include #include @@ -34,217 +34,46 @@ void CLookupYsf::LoadParameters() m_Type = g_Conf.GetRefreshType(g_Keys.ysftxrxdb.mode); m_Refresh = g_Conf.GetUnsigned(g_Keys.ysftxrxdb.refreshmin); m_Path.assign(g_Conf.GetString(g_Keys.ysftxrxdb.filepath)); - m_Host.assign(g_Conf.GetString(g_Keys.ysftxrxdb.hostname)); - m_Suffix.assign(g_Conf.GetString(g_Keys.ysftxrxdb.suffix)); + m_Url.assign(g_Conf.GetString(g_Keys.ysftxrxdb.url)); m_DefaultTx = g_Conf.GetUnsigned(g_Keys.ysf.defaulttxfreq); m_DefaultRx = g_Conf.GetUnsigned(g_Keys.ysf.defaultrxfreq); } -bool CLookupYsf::LoadContentFile(CBuffer &buffer) +void CLookupYsf::UpdateContent(std::stringstream &ss) { - buffer.clear(); - std::ifstream file; - std::streampos size; - - // open file - file.open(m_Path, std::ios::in | std::ios::binary | std::ios::ate); - if (file.is_open()) - { - // read file - size = file.tellg(); - if (size > 0) - { - // read file into buffer - buffer.resize((int)size + 1); - file.seekg(0, std::ios::beg); - file.read((char *)buffer.data(), (int)size); - } - file.close(); - } - return buffer.size() > 0; -} - -bool CLookupYsf::LoadContentHttp(CBuffer &buffer) -{ - // get file from http://xlxapi.rlx.lu/api/exportysfrepeaters.php - return HttpGet(m_Host.c_str(), m_Suffix.c_str(), 80, buffer); -} - -void CLookupYsf::RefreshContentFile(const CBuffer &buffer) -{ - // scan buffer - if (buffer.size() > 0) - { - // crack it - char *ptr1 = (char *)buffer.data(); - char *ptr2; - - // get next line - while ((ptr2 = ::strchr(ptr1, '\n')) != nullptr) - { - *ptr2 = 0; - // get items - char *callsign; - char *txfreq; - char *rxfreq; - if (((callsign = ::strtok(ptr1, ";")) != nullptr)) - { - if (((txfreq = ::strtok(nullptr, ";")) != nullptr)) - { - if (((rxfreq = ::strtok(nullptr, ";")) != nullptr)) - { - // new entry - CCallsign cs(callsign); - CYsfNode node(atoi(txfreq), atoi(rxfreq)); - if (cs.IsValid() && node.IsValid()) - { - m_map.insert(std::pair(cs, node)); - } - } - } - } - // next line - ptr1 = ptr2 + 1; - } - } - - // report - std::cout << "Read " << m_map.size() << " YSF nodes from file " << m_Path << std::endl; -} - -void CLookupYsf::RefreshContentHttp(const CBuffer &buffer) -{ - // crack it - char *ptr1 = (char *)buffer.data(); - char *ptr2; - - // get next line - while ((ptr2 = ::strchr(ptr1, '\n')) != nullptr) + std::string line; + while (std::getline(ss, line)) { - *ptr2 = 0; - // get items - char *callsign; - char *txfreq; - char *rxfreq; - if (((callsign = ::strtok(ptr1, ";")) != nullptr)) + std::string cs_str, tx_str, rx_str; + std::istringstream iss(line); + std::getline(iss, cs_str, ';'); + std::getline(iss, tx_str, ';'); + std::getline(iss, rx_str, ';'); + auto ltx = stol(tx_str); + auto lrx = stol(rx_str); + if (ltx > 40000000 && ltx < 0x100000000 && lrx > 40000000 && lrx < 0x100000000 && CCallsign(cs_str.c_str()).IsValid()) { - if (((txfreq = ::strtok(nullptr, ";")) != nullptr)) - { - if (((rxfreq = ::strtok(nullptr, ";")) != nullptr)) - { - // new entry - CCallsign cs(callsign); - CYsfNode node(atoi(txfreq), atoi(rxfreq)); - if (cs.IsValid() && node.IsValid()) - { - m_map.insert(std::pair(cs, node)); - } - } - } - } - // next line - ptr1 = ptr2 + 1; - } - - // report - std::cout << "Read " << m_map.size() << " YSF nodes from " << m_Host << " database " << std::endl; -} - -#define YSFNODE_HTTPGET_SIZEMAX (256) - -bool CLookupYsf::HttpGet(const char *hostname, const char *filename, int port, CBuffer &buffer) -{ - buffer.clear(); - int sock_id; - - // open socket - if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) >= 0) - { - // get hostname address - struct sockaddr_in servaddr; - struct hostent *hp; - memset(&servaddr, 0, sizeof(servaddr)); - if ((hp = gethostbyname(hostname)) != nullptr) - { - // dns resolved - memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length); - servaddr.sin_port = htons(port); - servaddr.sin_family = AF_INET; - - // connect - if (connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) == 0) - { - // send the GET request - char request[YSFNODE_HTTPGET_SIZEMAX]; - sprintf(request, "GET /%s HTTP/1.0\r\nFrom: %s\r\nUser-Agent: urfd\r\n\r\n", filename, g_Conf.GetString(g_Keys.names.cs).c_str()); - write(sock_id, request, strlen(request)); - - // config receive timeouts - fd_set read_set; - struct timeval timeout; - timeout.tv_sec = 5; - timeout.tv_usec = 0; - FD_ZERO(&read_set); - FD_SET(sock_id, &read_set); - - // get the reply back - bool done = false; - do - { - char buf[1440]; - ssize_t len = 0; - select(sock_id + 1, &read_set, nullptr, nullptr, &timeout); - // if ( (ret > 0) || ((ret < 0) && (errno == EINPROGRESS)) ) - // if ( ret >= 0 ) - //{ - usleep(5000); - len = read(sock_id, buf, 1440); - if (len > 0) - { - buffer.Append((uint8_t *)buf, (int)len); - } - //} - done = (len <= 0); - - } while (!done); - - buffer.Append((uint8_t)0); - - // and disconnect - close(sock_id); - } - else - { - std::cout << "Cannot establish connection with host " << hostname << std::endl; - } + m_map.emplace(CCallsign(cs_str.c_str()), CYsfNode(uint32_t(ltx), uint32_t(lrx))); } else { - std::cout << "Host " << hostname << " not found" << std::endl; + std::cout << "YSF value '" << cs_str << ';' << tx_str << ';' << rx_str << ";' is malformed" << std::endl; } } - else - { - std::cout << "Failed to open wget socket" << std::endl; - } - - // done - return buffer.size() > 1; + std::cout << "DMR Id database size now is " << m_map.size() << std::endl; } -bool CLookupYsf::FindFrequencies(const CCallsign &callsign, uint32_t *txfreq, uint32_t *rxfreq) +void CLookupYsf::FindFrequencies(const CCallsign &callsign, uint32_t &txfreq, uint32_t &rxfreq) { auto found = m_map.find(callsign); - if (found != m_map.end()) + if ( found != m_map.end() ) { - *txfreq = found->second.GetTxFrequency(); - *rxfreq = found->second.GetRxFrequency(); - return true; + txfreq = found->second.GetTxFrequency(); + rxfreq = found->second.GetRxFrequency(); } else { - *txfreq = m_DefaultTx; - *rxfreq = m_DefaultRx; - return false; + txfreq = m_DefaultTx; + rxfreq = m_DefaultRx; } } diff --git a/reflector/LookupYsf.h b/reflector/LookupYsf.h index 265511a..8c8fa90 100644 --- a/reflector/LookupYsf.h +++ b/reflector/LookupYsf.h @@ -21,25 +21,20 @@ #include "YSFNode.h" #include "Lookup.h" -using CsNodeMap = std::map; +using CsNodeMap = std::unordered_map; class CLookupYsf : public CLookup { public: - bool FindFrequencies(const CCallsign &, uint32_t *, uint32_t *); + void FindFrequencies(const CCallsign &, uint32_t &, uint32_t &); protected: void ClearContents(); void LoadParameters(); - bool LoadContentFile(CBuffer &buf); - bool LoadContentHttp(CBuffer &buf); - void RefreshContentFile(const CBuffer &); - void RefreshContentHttp(const CBuffer &); + void UpdateContent(std::stringstream &ss); private: CsNodeMap m_map; - bool HttpGet(const char *, const char *, int, CBuffer &); - unsigned m_DefaultTx, m_DefaultRx; }; diff --git a/reflector/WiresXCmdHandler.cpp b/reflector/WiresXCmdHandler.cpp index 919c886..d02bdc5 100644 --- a/reflector/WiresXCmdHandler.cpp +++ b/reflector/WiresXCmdHandler.cpp @@ -141,7 +141,7 @@ void CWiresxCmdHandler::Task(void) { // fill our info object Info = m_ReflectorWiresxInfo; - g_LYtr.FindFrequencies(Cmd.GetCallsign(), &uiNodeTxFreq, &uiNodeRxFreq); + g_LYtr.FindFrequencies(Cmd.GetCallsign(), uiNodeTxFreq, uiNodeRxFreq); Info.SetFrequencies(uiNodeTxFreq, uiNodeRxFreq); // find our client and the module it's currentlink linked to