From a3c8fe44dd0c0b4d845d3e19e608449a0ebc70bd Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 19:11:47 +0200 Subject: [PATCH 1/2] use climits for MAX_ULONG check if received ip from api has at least a : --- src/IPRefresher.cpp | 4 ++++ src/api/TelegramAPI.cpp | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 724129c..14d5335 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -16,6 +16,7 @@ #include #include #include +#include void IPRefresher::checkIPAdress(bool force) { FileLogger logger; @@ -26,6 +27,9 @@ void IPRefresher::checkIPAdress(bool force) { if (ip.empty()) { //no internet connection (or other error) Logger::warning("no internet connection"); + } else if (ip.find(':') == ULONG_MAX) { + // error when ip doesn't contain a : + Logger::warning("an error occured when getting the global ip"); } else { std::string oldip = logger.readip(); diff --git a/src/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index 3df25ea..0c26a34 100644 --- a/src/api/TelegramAPI.cpp +++ b/src/api/TelegramAPI.cpp @@ -3,8 +3,9 @@ // #include "api/TelegramAPI.h" +#include "Logger.h" -#include +#include int TelegramAPI::sendMessage(const std::string& text) { Hashmap args; @@ -15,7 +16,6 @@ int TelegramAPI::sendMessage(const std::string& text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); - unsigned const long ULONG_MAX = -1; if (reply.find("\"error_code\"") != ULONG_MAX) { Logger::error("failed to refresh the ip (Dynu API)"); return -1; From 1756b0cf5ec3bc660ae19820de0f8c05be4e5431 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 19:15:08 +0200 Subject: [PATCH 2/2] check also read ip from file if valid --- src/FileLogger.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 79b1a5d..a851d91 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "FileLogger.h" @@ -24,5 +25,9 @@ std::string FileLogger::readip() { in >> ip; - return ip; + // when received ip has no : return 0.0.0.0 + if (ip.find(':') == ULONG_MAX) + return "0.0.0.0"; + else + return ip; }