From e168b9d0614bb8cec5240a19d1605d3b61c9d495 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Tue, 28 Dec 2021 16:40:34 +0100 Subject: [PATCH] Add XLX Downloader --- DStarGatewayApp.cpp | 3 +- Makefile | 2 +- README.md | 2 +- XLXHostsFileDownloader.cpp | 66 ++++++++++++++++++++++++++++++++++++++ XLXHostsFileDownloader.h | 30 +++++++++++++++++ 5 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 XLXHostsFileDownloader.cpp create mode 100644 XLXHostsFileDownloader.h diff --git a/DStarGatewayApp.cpp b/DStarGatewayApp.cpp index 3fc26aa..3e41146 100644 --- a/DStarGatewayApp.cpp +++ b/DStarGatewayApp.cpp @@ -34,6 +34,7 @@ #include "Version.h" #include "GitVersion.h" #include "RepeaterProtocolHandlerFactory.h" +#include "XLXHostsFileDownloader.h" #include "Log.h" int main(int argc, char *argv[]) @@ -201,7 +202,7 @@ bool CDStarGatewayApp::createThread() TXLX xlxConfig; config.getXLX(xlxConfig); CLog::logInfo("XLX enabled: %d, Hosts file url: %s", int(xlxConfig.enabled), xlxConfig.url.c_str()); - m_thread->setXLX(xlxConfig.enabled, xlxConfig.url); + m_thread->setXLX(xlxConfig.enabled, xlxConfig.enabled ? CXLXHostsFileDownloader::download(xlxConfig.url) : ""); // Setup Remote TRemote remoteConfig; diff --git a/Makefile b/Makefile index 4567717..14752c7 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ DEPS = $(SRCS:.cpp=.d) all: dstargateway dstargateway : GitVersion.h $(OBJS) - g++ $(CPPFLAGS) -o dstargateway $(OBJS) -lconfig++ -pthread + g++ $(CPPFLAGS) -o dstargateway $(OBJS) -lconfig++ -lcurl -pthread %.o : %.cpp g++ $(CPPFLAGS) -MMD -MD -c $< -o $@ diff --git a/README.md b/README.md index aca9639..2c1da9d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ A wxWidgets free port of Jonathan Naylor's G4KLX ircDDBGatway. -Only depenedencies are boost (headers only) and libconfig +Only depenedencies are boost (headers only), libconfig and libcurl4-openssl-dev # Not usable yet ! diff --git a/XLXHostsFileDownloader.cpp b/XLXHostsFileDownloader.cpp new file mode 100644 index 0000000..0209378 --- /dev/null +++ b/XLXHostsFileDownloader.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010-2013,2015,2018 by Jonathan Naylor G4KLX + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include + +#include "XLXHostsFileDownloader.h" +#include "Log.h" + +size_t CXLXHostsFileDownloader::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t written = fwrite(ptr, size, nmemb, stream); + return written; +} + +/* wxHTTP randomly crashes when called on a worker thread, this must be called from main thread ! */ +std::string CXLXHostsFileDownloader::download(const std::string & xlxHostsFileURL) +{ + CURL *curl; + FILE *fp; + bool ok = false; + std::string outFileName; + char outFileNameBuf[] = "/tmp/XLXHostFile_XXXXXX"; + + CLog::logInfo("Downloading XLX host file from %s", xlxHostsFileURL.c_str()); + + curl = curl_easy_init(); + if (curl) { + int filedes = mkstemp(outFileNameBuf); + if(filedes > 0 && (fp = fdopen(filedes,"wb")) != NULL) { + curl_easy_setopt(curl, CURLOPT_URL, xlxHostsFileURL.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + CURLcode res = curl_easy_perform(curl); + ok = res == 0; + /* always cleanup */ + curl_easy_cleanup(curl); + fclose(fp); + } + } + + + if(ok) { + outFileName = std::string(outFileNameBuf); + } else { + CLog::logError("Failed to download XLx Host file from %s", xlxHostsFileURL.c_str()); + } + + return outFileName; +} diff --git a/XLXHostsFileDownloader.h b/XLXHostsFileDownloader.h new file mode 100644 index 0000000..5c43a05 --- /dev/null +++ b/XLXHostsFileDownloader.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 by Geoffrey Merck F4FXL / KC3FRA + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +#pragma once + +#include + +class CXLXHostsFileDownloader { +public: + static std::string download(const std::string & xlxHostsFileURL); +private: + static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream); +}; +