From 05d01f44a0c29642fdeef1a8e0b52b48aa40550a Mon Sep 17 00:00:00 2001 From: Tom Early Date: Wed, 15 Apr 2020 10:04:39 -0700 Subject: [PATCH] database complete for gateway map --- DPlusAuthenticator.cpp | 38 ++++++++++++---------------------- DPlusAuthenticator.h | 5 ++--- QnetDB.cpp | 4 ++-- QnetGateway.cpp | 2 +- QnetLink.cpp | 46 +++++++++++++++++++++++++----------------- QnetLink.h | 4 ++-- defaults | 2 +- 7 files changed, 48 insertions(+), 53 deletions(-) diff --git a/DPlusAuthenticator.cpp b/DPlusAuthenticator.cpp index b4d2112..00ccf47 100644 --- a/DPlusAuthenticator.cpp +++ b/DPlusAuthenticator.cpp @@ -29,6 +29,7 @@ #include #include "DPlusAuthenticator.h" +#include "Utilities.h" CDPlusAuthenticator::CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address) : m_loginCallsign(loginCallsign), @@ -36,25 +37,25 @@ m_address(address) { assert(loginCallsign.size()); - Trim(m_loginCallsign); + trim(m_loginCallsign); } CDPlusAuthenticator::~CDPlusAuthenticator() { } -bool CDPlusAuthenticator::Process(CQnetDB &db, const bool reflectors, const bool repeaters) +int CDPlusAuthenticator::Process(CQnetDB &db, const bool reflectors, const bool repeaters) // return true if everything went okay { int result = client.Open(m_address, AF_UNSPEC, "20001"); if (result) { fprintf(stderr, "DPlus Authorization failed: %s\n", gai_strerror(result)); - return true; + return 0; } return authenticate(db, reflectors, repeaters); } -bool CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const bool repeaters) +int CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const bool repeaters) { unsigned char* buffer = new unsigned char[4096U]; ::memset(buffer, ' ', 56U); @@ -73,11 +74,11 @@ bool CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const fprintf(stderr, "ERROR: could not write opening phrase\n"); client.Close(); delete[] buffer; - return true; + return 0; } int ret = client.ReadExact(buffer, 2U); - unsigned int returned = 0; + unsigned int rval = 0; while (ret == 2) { unsigned int len = (buffer[1U] & 0x0FU) * 256U + buffer[0U]; @@ -85,20 +86,20 @@ bool CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const ret = client.ReadExact(buffer + 2U, len - 2U); if (0 > ret) { fprintf(stderr, "Problem reading line, it returned %d\n", errno); - return true; + return rval; } if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) { fprintf(stderr, "Invalid packet received from 20001\n"); - return true; + return rval; } for (unsigned int i = 8U; (i + 25U) < len; i += 26U) { std::string address((char *)(buffer + i)); std::string name((char *)(buffer + i + 16U)); - Trim(address); - Trim(name); + trim(address); + trim(name); name.resize(6, ' '); // Get the active flag @@ -106,7 +107,7 @@ bool CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const // An empty name or IP address or an inactive gateway/reflector is not added if (address.size()>0U && name.size()>0U && active) { - returned++; + rval++; if (reflectors && 0==name.compare(0, 3, "REF")) db.UpdateGW(name.c_str(), address.c_str(), 20001); else if (repeaters && name.compare(0, 3, "REF")) @@ -118,22 +119,9 @@ bool CDPlusAuthenticator::authenticate(CQnetDB &db, const bool reflectors, const } printf("Probably authorized DPlus on %s using callsign %s\n", m_address.c_str(), m_loginCallsign.c_str()); - printf("%s returned %u systems\n", m_address.c_str(), returned); client.Close(); delete[] buffer; - return false; -} - -void CDPlusAuthenticator::Trim(std::string &s) -{ - auto it = s.begin(); - while (it!=s.end() && isspace(*it)) - s.erase(it); - auto rit = s.rbegin(); - while (rit!=s.rend() && isspace(*rit)) { - s.resize(s.size() - 1); - rit = s.rbegin(); - } + return rval; } diff --git a/DPlusAuthenticator.h b/DPlusAuthenticator.h index 8f8b270..b34b8c6 100644 --- a/DPlusAuthenticator.h +++ b/DPlusAuthenticator.h @@ -30,13 +30,12 @@ public: CDPlusAuthenticator(const std::string &loginCallsign, const std::string &address); ~CDPlusAuthenticator(); - bool Process(CQnetDB &qn, const bool reflectors, const bool repeaters); + int Process(CQnetDB &qn, const bool reflectors, const bool repeaters); private: std::string m_loginCallsign; std::string m_address; CTCPReaderWriterClient client; - void Trim(std::string &s); - bool authenticate(CQnetDB &db, const bool reflectors, const bool repeaters); + int authenticate(CQnetDB &db, const bool reflectors, const bool repeaters); }; diff --git a/QnetDB.cpp b/QnetDB.cpp index e459765..98368bc 100644 --- a/QnetDB.cpp +++ b/QnetDB.cpp @@ -262,7 +262,7 @@ void CQnetDB::ClearGW() } } -static int countcallback(void *count, int argc, char **argv, char **azColName) { +static int countcallback(void *count, int /*argc*/, char **argv, char **/*azColName*/) { auto c = (int *)count; *c = atoi(argv[0]); return 0; @@ -271,7 +271,7 @@ static int countcallback(void *count, int argc, char **argv, char **azColName) { int CQnetDB::Count(const char *table) { if (NULL == db) - return; + return 0; std::string sql("SELECT COUNT(*) FROM "); sql.append(table); diff --git a/QnetGateway.cpp b/QnetGateway.cpp index 01ad76d..f1b9768 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -54,7 +54,7 @@ #define CFG_DIR "/usr/local/etc" #endif -const std::string GW_VERSION("QnetGateway-409"); +const std::string GW_VERSION("QnetGateway-415"); static std::atomic keep_running(true); diff --git a/QnetLink.cpp b/QnetLink.cpp index f65729a..4d83a09 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -54,7 +54,7 @@ #include "QnetLink.h" #include "Utilities.h" -#define LINK_VERSION "QnetLink-409" +#define LINK_VERSION "QnetLink-415" #ifndef BIN_DIR #define BIN_DIR "/usr/local/bin" #endif @@ -290,17 +290,21 @@ void CQnetLink::RptrAckThread(char *arg) } /* Open text file of repeaters, reflectors */ -bool CQnetLink::load_gwys(const std::string &filename) +void CQnetLink::LoadGateways(const std::string &filename) { + const std::string website("auth.dstargateway.org"); + int dplus = 0; // DPlus Authenticate if (dplus_authorize && !dplus_priority) { - CDPlusAuthenticator auth(login_call, std::string("auth.dstargateway.org")); - if (auth.Process(qnDB, dplus_reflectors, dplus_repeaters)) + CDPlusAuthenticator auth(login_call, std::string(website.c_str())); + dplus = auth.Process(qnDB, dplus_reflectors, dplus_repeaters); + if (0 == dplus) fprintf(stdout, "DPlus Authorization failed.\n"); else fprintf(stderr, "DPlus Authorization complete!\n"); } + int count = 0; std::ifstream hostfile(filename); if (hostfile.is_open()) { std::string line; @@ -312,25 +316,31 @@ bool CQnetLink::load_gwys(const std::string &filename) unsigned short port; iss >> host >> address >> port; qnDB.UpdateGW(host.c_str(), address.c_str(), port); + count++; } } hostfile.close(); } + if (dplus_authorize) { + if (! dplus_priority) + printf("#Gateways: %s=%d %s=%d Total=%d\n", website.c_str(), dplus, filename.c_str(), count, qnDB.Count("GATEWAYS")); + } else { + printf("#Gateways: %s=%d\n", filename.c_str(), count); + } + // DPlus Authenticate if (dplus_authorize && dplus_priority) { - CDPlusAuthenticator auth(login_call, std::string("auth.dstargateway.org")); - if (auth.Process(qnDB, dplus_reflectors, dplus_repeaters)) + CDPlusAuthenticator auth(login_call, std::string(website.c_str())); + dplus = auth.Process(qnDB, dplus_reflectors, dplus_repeaters); + if (0 == dplus) { + printf("#Gateways: %s=%d\n", filename.c_str(), count); fprintf(stdout, "DPlus Authorization failed.\n"); - else + } else { fprintf(stderr, "DPlus Authorization completed!\n"); + printf("#Gateways %s=%d %s=%d Total=%d\n", filename.c_str(), count, website.c_str(), dplus, qnDB.Count("GATEWAYS")); + } } - - auto count = qnDB.Count("GATEWAYS"); - if (count) - printf("Loaded %d gateways from %s%s\n", count, filename.c_str(), dplus_authorize ? " and auth.dstargateway.org" : ""); - - return true; } /* compute checksum */ @@ -423,7 +433,7 @@ void CQnetLink::PrintCallsigns(const std::string &key, const std::set &set, const std::string &delimiters = ","); void PrintCallsigns(const std::string &key, const std::set &set); - bool load_gwys(const std::string &filename); + void LoadGateways(const std::string &filename); void calcPFCS(unsigned char *packet, int len); - bool read_config(const char *); + bool ReadConfig(const char *); bool srv_open(); void srv_close(); static void sigCatch(int signum); diff --git a/defaults b/defaults index 42b4c27..da79bbf 100644 --- a/defaults +++ b/defaults @@ -1,5 +1,5 @@ # -# Copyright (c) 2019 by Thomas A. Early N7TAE +# Copyright (c) 2019-2020 by Thomas A. Early N7TAE # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by