Strip wxWidgets out of DPlusAuthenticator

master
Geoffrey Merck 4 years ago
parent dfd7cf6d66
commit 0a8cc0987b

@ -16,20 +16,23 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <assert.h>
#include <cstring>
#include <boost/algorithm/string.hpp>
#include "Thread.h"
#include "DPlusAuthenticator.h" #include "DPlusAuthenticator.h"
#include "UDPReaderWriter.h" #include "UDPReaderWriter.h"
#include "DStarDefines.h" #include "DStarDefines.h"
#include "Utils.h" #include "Utils.h"
#include "Defs.h" #include "Defs.h"
const wxString OPENDSTAR_HOSTNAME = wxT("auth.dstargateway.org"); const std::string OPENDSTAR_HOSTNAME = "auth.dstargateway.org";
const unsigned int OPENDSTAR_PORT = 20001U; const unsigned int OPENDSTAR_PORT = 20001U;
const unsigned int TCP_TIMEOUT = 10U; const unsigned int TCP_TIMEOUT = 10U;
CDPlusAuthenticator::CDPlusAuthenticator(const wxString& loginCallsign, const wxString& gatewayCallsign, const wxString& address, CCacheManager* cache) : CDPlusAuthenticator::CDPlusAuthenticator(const std::string& loginCallsign, const std::string& gatewayCallsign, const std::string& address, CCacheManager* cache) :
wxThread(wxTHREAD_JOINABLE),
m_loginCallsign(loginCallsign), m_loginCallsign(loginCallsign),
m_gatewayCallsign(gatewayCallsign), m_gatewayCallsign(gatewayCallsign),
m_address(address), m_address(address),
@ -37,16 +40,16 @@ m_cache(cache),
m_timer(1U, 6U * 3600U), // 6 hours m_timer(1U, 6U * 3600U), // 6 hours
m_killed(false) m_killed(false)
{ {
wxASSERT(!loginCallsign.IsEmpty()); assert(!loginCallsign.empty());
wxASSERT(!gatewayCallsign.IsEmpty()); assert(!gatewayCallsign.empty());
wxASSERT(cache != NULL); assert(cache != NULL);
m_gatewayCallsign.Truncate(LONG_CALLSIGN_LENGTH - 1U); m_gatewayCallsign = m_gatewayCallsign.substr(LONG_CALLSIGN_LENGTH - 1U);
m_loginCallsign.Trim(); boost::trim(m_loginCallsign);
m_gatewayCallsign.Trim(); boost::trim(m_gatewayCallsign);
if (m_loginCallsign.IsEmpty()) if (m_loginCallsign.empty())
m_loginCallsign = m_gatewayCallsign; m_loginCallsign = m_gatewayCallsign;
} }
@ -62,7 +65,7 @@ void CDPlusAuthenticator::start()
void* CDPlusAuthenticator::Entry() void* CDPlusAuthenticator::Entry()
{ {
wxLogMessage(wxT("Starting the D-Plus Authenticator thread")); printf("Starting the D-Plus Authenticator thread");
authenticate(m_loginCallsign, OPENDSTAR_HOSTNAME, OPENDSTAR_PORT, '2', true); authenticate(m_loginCallsign, OPENDSTAR_HOSTNAME, OPENDSTAR_PORT, '2', true);
@ -81,14 +84,14 @@ void* CDPlusAuthenticator::Entry()
} }
} }
catch (std::exception& e) { catch (std::exception& e) {
wxString message(e.what(), wxConvLocal); std::string message(e.what());
wxLogError(wxT("Exception raised in the D-Plus Authenticator thread - \"%s\""), message.c_str()); printf("Exception raised in the D-Plus Authenticator thread - \"%s\"", message.c_str());
} }
catch (...) { catch (...) {
wxLogError(wxT("Unknown exception raised in the D-Plus Authenticator thread")); printf("Unknown exception raised in the D-Plus Authenticator thread");
} }
wxLogMessage(wxT("Stopping the D-Plus Authenticator thread")); printf("Stopping the D-Plus Authenticator thread");
return NULL; return NULL;
} }
@ -100,7 +103,7 @@ void CDPlusAuthenticator::stop()
Wait(); Wait();
} }
bool CDPlusAuthenticator::authenticate(const wxString& callsign, const wxString& hostname, unsigned int port, unsigned char id, bool writeToCache) bool CDPlusAuthenticator::authenticate(const std::string& callsign, const std::string& hostname, unsigned int port, unsigned char id, bool writeToCache)
{ {
CTCPReaderWriterClient socket(hostname, port, m_address); CTCPReaderWriterClient socket(hostname, port, m_address);
@ -116,8 +119,8 @@ bool CDPlusAuthenticator::authenticate(const wxString& callsign, const wxString&
buffer[2U] = 0x01U; buffer[2U] = 0x01U;
buffer[3U] = 0x00U; buffer[3U] = 0x00U;
for (unsigned int i = 0U; i < callsign.Len(); i++) for (unsigned int i = 0U; i < callsign.length(); i++)
buffer[i + 4U] = callsign.GetChar(i); buffer[i + 4U] = callsign[i];
buffer[12U] = 'D'; buffer[12U] = 'D';
buffer[13U] = 'V'; buffer[13U] = 'V';
@ -157,34 +160,34 @@ bool CDPlusAuthenticator::authenticate(const wxString& callsign, const wxString&
// Ensure that we get exactly len - 2U bytes from the TCP stream // Ensure that we get exactly len - 2U bytes from the TCP stream
ret = read(socket, buffer + 2U, len - 2U); ret = read(socket, buffer + 2U, len - 2U);
if (!ret) { if (!ret) {
wxLogError(wxT("Short read from %s:%u"), hostname.c_str(), port); printf("Short read from %s:%u", hostname.c_str(), port);
break; break;
} }
if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) { if ((buffer[1U] & 0xC0U) != 0xC0U || buffer[2U] != 0x01U) {
wxLogError(wxT("Invalid packet received from %s:%u"), hostname.c_str(), port); printf("Invalid packet received from %s:%u", hostname.c_str(), port);
CUtils::dump(wxT("Details:"), buffer, len); CUtils::dump("Details:", buffer, len);
break; break;
} }
for (unsigned int i = 8U; (i + 25U) < len; i += 26U) { for (unsigned int i = 8U; (i + 25U) < len; i += 26U) {
wxString address = wxString((char*)(buffer + i + 0U), wxConvLocal, 16U); std::string address = std::string((char*)(buffer + i + 0U), 16U);
wxString name = wxString((char*)(buffer + i + 16U), wxConvLocal, LONG_CALLSIGN_LENGTH); std::string name = std::string((char*)(buffer + i + 16U), LONG_CALLSIGN_LENGTH);
address.Trim(); boost::trim(address);
name.Trim(); boost::trim(name);
// Get the active flag // Get the active flag
bool active = (buffer[i + 25U] & 0x80U) == 0x80U; bool active = (buffer[i + 25U] & 0x80U) == 0x80U;
// An empty name or IP address or an inactive gateway/reflector is not written out // An empty name or IP address or an inactive gateway/reflector is not written out
if (address.Len() > 0U && name.Len() > 0U && !name.Left(3U).IsSameAs(wxT("XRF")) && active && writeToCache){ if (address.length() > 0U && name.length() > 0U && !name.compare(0, 3, "XRF") == 0 && active && writeToCache){
if (name.Left(3U).IsSameAs(wxT("REF"))) if (name.compare(0, 3, "REF") == 0)
wxLogMessage(wxT("D-Plus: %s\t%s"), name.c_str(), address.c_str()); printf("D-Plus: %s\t%s", name.c_str(), address.c_str());
name.Append(wxT(" ")); name += " ";
name.Truncate(LONG_CALLSIGN_LENGTH - 1U); name = name.substr(LONG_CALLSIGN_LENGTH - 1U);
name.Append(wxT("G")); name += "G";
m_cache->updateGateway(name, address, DP_DPLUS, false, true); m_cache->updateGateway(name, address, DP_DPLUS, false, true);
} }
} }
@ -192,7 +195,7 @@ bool CDPlusAuthenticator::authenticate(const wxString& callsign, const wxString&
ret = read(socket, buffer + 0U, 2U); ret = read(socket, buffer + 0U, 2U);
} }
wxLogMessage(wxT("Registered with %s using callsign %s"), hostname.c_str(), callsign.c_str()); printf("Registered with %s using callsign %s", hostname.c_str(), callsign.c_str());
socket.close(); socket.close();
@ -201,7 +204,7 @@ bool CDPlusAuthenticator::authenticate(const wxString& callsign, const wxString&
return true; return true;
} }
bool CDPlusAuthenticator::poll(const wxString& callsign, const wxString& hostname, unsigned int port, unsigned char id) bool CDPlusAuthenticator::poll(const std::string& callsign, const std::string& hostname, unsigned int port, unsigned char id)
{ {
CUDPReaderWriter socket(m_address, 0U); CUDPReaderWriter socket(m_address, 0U);
bool ret = socket.open(); bool ret = socket.open();
@ -216,8 +219,8 @@ bool CDPlusAuthenticator::poll(const wxString& callsign, const wxString& hostnam
buffer[2U] = 0x01U; buffer[2U] = 0x01U;
buffer[3U] = 0x01U; buffer[3U] = 0x01U;
for (unsigned int i = 0U; i < callsign.Len(); i++) for (unsigned int i = 0U; i < callsign.length(); i++)
buffer[i + 4U] = callsign.GetChar(i); buffer[i + 4U] = callsign[i];
buffer[12U] = 'D'; buffer[12U] = 'D';
buffer[13U] = 'V'; buffer[13U] = 'V';
@ -228,8 +231,8 @@ bool CDPlusAuthenticator::poll(const wxString& callsign, const wxString& hostnam
buffer[18U] = '9'; buffer[18U] = '9';
buffer[19U] = '9'; buffer[19U] = '9';
for (unsigned int i = 0U; i < callsign.Len(); i++) for (unsigned int i = 0U; i < callsign.length(); i++)
buffer[i + 20U] = callsign.GetChar(i); buffer[i + 20U] = callsign[i];
buffer[28U] = 'W'; buffer[28U] = 'W';
buffer[29U] = '7'; buffer[29U] = '7';

@ -20,6 +20,7 @@
#ifndef DPlusAuthenticator_H #ifndef DPlusAuthenticator_H
#define DPlusAuthenticator_H #define DPlusAuthenticator_H
#include "Thread.h"
#include "TCPReaderWriterClient.h" #include "TCPReaderWriterClient.h"
#include "CacheManager.h" #include "CacheManager.h"
#include "Timer.h" #include "Timer.h"
@ -27,9 +28,9 @@
#include <netinet/in.h> #include <netinet/in.h>
class CDPlusAuthenticator : public wxThread { class CDPlusAuthenticator : public CThread {
public: public:
CDPlusAuthenticator(const wxString& loginCallsign, const wxString& gatewayCallsign, const wxString& address, CCacheManager* cache); CDPlusAuthenticator(const std::string& loginCallsign, const std::string& gatewayCallsign, const std::string& address, CCacheManager* cache);
virtual ~CDPlusAuthenticator(); virtual ~CDPlusAuthenticator();
virtual void start(); virtual void start();
@ -39,15 +40,15 @@ public:
virtual void stop(); virtual void stop();
private: private:
wxString m_loginCallsign; std::string m_loginCallsign;
wxString m_gatewayCallsign; std::string m_gatewayCallsign;
wxString m_address; std::string m_address;
CCacheManager* m_cache; CCacheManager* m_cache;
CTimer m_timer; CTimer m_timer;
bool m_killed; bool m_killed;
bool poll(const wxString& callsign, const wxString& hostname, unsigned int port, unsigned char id); bool poll(const std::string& callsign, const std::string& hostname, unsigned int port, unsigned char id);
bool authenticate(const wxString& callsign, const wxString& hostname, unsigned int port, unsigned char id, bool writeToCache); bool authenticate(const std::string& callsign, const std::string& hostname, unsigned int port, unsigned char id, bool writeToCache);
bool read(CTCPReaderWriterClient& socket, unsigned char* buffer, unsigned int len) const; bool read(CTCPReaderWriterClient& socket, unsigned char* buffer, unsigned int len) const;
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.