diff --git a/src/cdmriddir.cpp b/src/cdmriddir.cpp index 5d62db3..ddbfe6d 100644 --- a/src/cdmriddir.cpp +++ b/src/cdmriddir.cpp @@ -28,6 +28,7 @@ #include "cdmriddir.h" #include "cdmriddirfile.h" #include "cdmriddirhttp.h" +#include "cdmriddirspecial.h" //////////////////////////////////////////////////////////////////////////////////////// // constructor & destructor diff --git a/src/cdmriddir.h b/src/cdmriddir.h index 23334e3..af907ed 100644 --- a/src/cdmriddir.h +++ b/src/cdmriddir.h @@ -67,6 +67,13 @@ public: // find const CCallsign *FindCallsign(uint32); uint32 FindDmrid(const CCallsign &); + + // validation + bool IsValidDmrid(const char *); + + // data + std::map m_CallsignMap; + std::map m_DmridMap; protected: // thread @@ -75,13 +82,8 @@ protected: // reload helpers bool Reload(void); virtual bool NeedReload(void) { return false; } - bool IsValidDmrid(const char *); protected: - // data - std::map m_CallsignMap; - std::map m_DmridMap; - // Lock() std::mutex m_Mutex; diff --git a/src/cdmriddirspecial.cpp b/src/cdmriddirspecial.cpp new file mode 100644 index 0000000..c9a059e --- /dev/null +++ b/src/cdmriddirspecial.cpp @@ -0,0 +1,177 @@ +// +// cdmriddirspecial.cpp +// xlxd +// +// Created by Jean-Luc Deltombe (LX3JL) on 29/12/2017. +// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// +// ---------------------------------------------------------------------------- +// This file is part of xlxd. +// +// xlxd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// xlxd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Foobar. If not, see . +// ---------------------------------------------------------------------------- + +#include +#include +#include +#include "main.h" +#include "cdmriddirfile.h" +#include "cdmriddirhttp.h" +#include "cdmriddirspecial.h" + + +#if (EXTENDED_DMRID_CHECKS == 1) +CDmridDirSpecial g_DmridDirSpecial; +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// constructor & destructor + +CDmridDirSpecial::CDmridDirSpecial() +{ +} + +CDmridDirSpecial::~CDmridDirSpecial() +{ +} + +//////////////////////////////////////////////////////////////////////////////////////// +// init & close + +bool CDmridDirSpecial::Init(void) +{ + Reload(); + return 0; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// Reload + +bool CDmridDirSpecial::Reload(void) +{ + CBuffer buffer; + bool ok = false; + + if ( LoadContent(&buffer) ) + { + Lock(); + { + ok = RefreshContent(buffer); + } + Unlock(); + } + return ok; +} + + +//////////////////////////////////////////////////////////////////////////////////////// +// refresh + +bool CDmridDirSpecial::LoadContent(CBuffer *buffer) +{ + bool ok = false; + std::ifstream file; + std::streampos size; + + // open file + file.open(SPECIALIDDB_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); + + // close file + file.close(); + + // update time + GetLastModTime(&m_LastModTime); + + // done + ok = true; + } + } + + // done + return ok; +} + +bool CDmridDirSpecial::RefreshContent(const CBuffer &buffer) +{ + bool ok = false; + int count = 0; + + // scan buffer + if ( buffer.size() > 0 ) + { + // crack it + char *ptr1 = (char *)buffer.data(); + char *ptr2; + + // get next line + while ( (ptr2 = ::strchr(ptr1, '\n')) != NULL ) + { + *ptr2 = 0; + // get items + char *dmrid; + char *callsign; + //std::cout << "Read callsign: " << &callsign << " ID: " << &dmrid << std::endl; + if ( ((dmrid = ::strtok(ptr1, ";")) != NULL) && g_DmridDir.IsValidDmrid(dmrid) ) + { + if ( ((callsign = ::strtok(NULL, ";")) != NULL) ) + { + // new entry + uint32 ui = atoi(dmrid); + CCallsign cs(callsign, ui); + if ( cs.IsValid() ) + { + g_DmridDir.m_CallsignMap.insert(std::pair(ui, cs)); + g_DmridDir.m_DmridMap.insert(std::pair(cs,ui)); + count++; + } + } + } + // next line + ptr1 = ptr2+1; + } + + // done + ok = true; + } + + // report + std::cout << "Read " << count << " special ids from file " << SPECIALIDDB_PATH << std::endl; + + // done + return ok; +} + + +bool CDmridDirSpecial::GetLastModTime(time_t *time) +{ + bool ok = false; + + struct stat fileStat; + if( ::stat(SPECIALIDDB_PATH, &fileStat) != -1 ) + { + *time = fileStat.st_mtime; + ok = true; + } + return ok; +} diff --git a/src/cdmriddirspecial.h b/src/cdmriddirspecial.h new file mode 100644 index 0000000..bde0a1f --- /dev/null +++ b/src/cdmriddirspecial.h @@ -0,0 +1,66 @@ +// +// cdmrididirspecial.h +// xlxd +// +// Created by Jean-Luc Deltombe (LX3JL) on 29/12/2017. +// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// +// ---------------------------------------------------------------------------- +// This file is part of xlxd. +// +// xlxd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// xlxd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Foobar. If not, see . +// ---------------------------------------------------------------------------- + +#ifndef cdmrididirspecial_h +#define cdmrididirspecial_h + +#include "cdmriddir.h" + +//////////////////////////////////////////////////////////////////////////////////////// + +class CDmridDirSpecial +{ +public: + // constructor + CDmridDirSpecial(); + + // destructor + ~CDmridDirSpecial(); + + // init & close + bool Init(void); + + // locks + void Lock(void) { m_Mutex.lock(); } + void Unlock(void) { m_Mutex.unlock(); } + + // refresh + bool LoadContent(CBuffer *); + bool RefreshContent(const CBuffer &); + +protected: + // reload helpers + bool Reload(void); + bool GetLastModTime(time_t *); + +protected: + // data + time_t m_LastModTime; + + // Lock() + std::mutex m_Mutex; + }; + +//////////////////////////////////////////////////////////////////////////////////////// +#endif /* cdmrididirspecial_h */ diff --git a/src/creflector.cpp b/src/creflector.cpp index 2eeea8b..879679a 100644 --- a/src/creflector.cpp +++ b/src/creflector.cpp @@ -28,6 +28,7 @@ #include "cgatekeeper.h" #include "cdmriddirfile.h" #include "cdmriddirhttp.h" +#include "cdmriddirspecial.h" #include "ctranscoder.h" #include "cysfnodedirfile.h" #include "cysfnodedirhttp.h" @@ -106,6 +107,11 @@ bool CReflector::Start(void) // init dmrid directory g_DmridDir.Init(); + +#if (EXTENDED_DMRID_CHECKS == 1) + // add special ids + g_DmridDirSpecial.Init(); +#endif // init wiresx node directory g_YsfNodeDir.Init(); diff --git a/src/main.h b/src/main.h index d520ea0..5fb6eb9 100644 --- a/src/main.h +++ b/src/main.h @@ -155,8 +155,9 @@ // Extended DMR ID checks --------------------------------------- -//#define EXTENDED_DMRID_CHECKS // Also allow extended "callsigns" like "IPSC_EU2" +//#define EXTENDED_DMRID_CHECKS 1 // Also allow extended "callsigns" like "IPSC_EU2" // used for incoming DMR-DL master connections +#define SPECIALIDDB_PATH "/xlxd/specialid.dat" // File to read special (DMR) IDs from // Wires-X node database ---------------------------------------- @@ -230,6 +231,11 @@ extern CGateKeeper g_GateKeeper; extern CYsfNodeDirFile g_YsfNodeDir; #endif +#if (EXTENDED_DMRID_CHECKS == 1) + class CDmridDirSpecial; + extern CDmridDirSpecial g_DmridDirSpecial; +#endif + class CTranscoder; extern CTranscoder g_Transcoder;