From d0c5dc4e2496204cf88e0b0c73091485d87be441 Mon Sep 17 00:00:00 2001 From: sehraf Date: Wed, 10 Jun 2020 21:22:45 +0200 Subject: [PATCH] i2p: bob: convert to RS_DBG --- .../src/services/autoproxy/p3i2pbob.cc | 129 +++++++++--------- 1 file changed, 63 insertions(+), 66 deletions(-) diff --git a/libretroshare/src/services/autoproxy/p3i2pbob.cc b/libretroshare/src/services/autoproxy/p3i2pbob.cc index ef8e1ad48..c1b32fa76 100644 --- a/libretroshare/src/services/autoproxy/p3i2pbob.cc +++ b/libretroshare/src/services/autoproxy/p3i2pbob.cc @@ -26,7 +26,6 @@ #include "pqi/p3peermgr.h" #include "rsitems/rsconfigitems.h" -#include "util/rsdebug.h" #include "util/radix32.h" #include "util/radix64.h" #include "util/rsdebug.h" @@ -52,8 +51,6 @@ static const int sleepFactorDefault = 10; // 0.5s static const int sleepFactorFast = 1; // 0.05s static const int sleepFactorSlow = 20; // 1s -RS_SET_CONTEXT_DEBUG_LEVEL(0) - static const rstime_t selfCheckPeroid = 30; void doSleep(useconds_t timeToSleepMS) { @@ -83,7 +80,7 @@ bool p3I2pBob::isEnabled() bool p3I2pBob::initialSetup(std::string &addr, uint16_t &/*port*/) { - std::cout << "p3I2pBob::initialSetup" << std::endl; + RS_DBG("") << std::endl; // update config { @@ -96,7 +93,7 @@ bool p3I2pBob::initialSetup(std::string &addr, uint16_t &/*port*/) } } - std::cout << "p3I2pBob::initialSetup config updated" << std::endl; + RS_DBG("config updated") << std::endl; // request keys // p3I2pBob::stateMachineBOB expects mProcessing to be set therefore @@ -106,12 +103,12 @@ bool p3I2pBob::initialSetup(std::string &addr, uint16_t &/*port*/) fakeTicket->task = autoProxyTask::receiveKey; processTaskAsync(fakeTicket); - std::cout << "p3I2pBob::initialSetup fakeTicket requested" << std::endl; + RS_DBG("fakeTicket requested") << std::endl; // now start thread start("I2P-BOB gen key"); - std::cout << "p3I2pBob::initialSetup thread started" << std::endl; + RS_DBG("thread started") << std::endl; int counter = 0; // wait for keys @@ -125,24 +122,24 @@ bool p3I2pBob::initialSetup(std::string &addr, uint16_t &/*port*/) break; if (++counter > 30) { - std::cout << "p3I2pBob::initialSetup timeout!" << std::endl; + RS_DBG4("timeout!") << std::endl; return false; } } - std::cout << "p3I2pBob::initialSetup got keys" << std::endl; + RS_DBG("got keys") << std::endl; // stop thread fullstop(); - std::cout << "p3I2pBob::initialSetup thread stopped" << std::endl; + RS_DBG("thread stopped") << std::endl; { RS_STACK_MUTEX(mLock); addr = mSetting.address.base32; } - std::cout << "p3I2pBob::initialSetup addr '" << addr << "'" << std::endl; + RS_DBG4("addr '" + addr + "'") << std::endl; return true; } @@ -160,7 +157,7 @@ void p3I2pBob::processTaskAsync(taskTicket *ticket) } break; default: - RsDbg() << __PRETTY_FUNCTION__ << " unknown task" << std::endl; + RS_DBG("unknown task") << std::endl; rsAutoProxyMonitor::taskError(ticket); break; } @@ -175,7 +172,7 @@ void p3I2pBob::processTaskSync(taskTicket *ticket) case autoProxyTask::status: // check if everything needed is set if (!data) { - RsDbg() << __PRETTY_FUNCTION__ << " autoProxyTask::status data is missing" << std::endl; + RS_DBG("autoProxyTask::status data is missing") << std::endl; rsAutoProxyMonitor::taskError(ticket); break; } @@ -189,7 +186,7 @@ void p3I2pBob::processTaskSync(taskTicket *ticket) case autoProxyTask::getSettings: // check if everything needed is set if (!data) { - RsDbg() << __PRETTY_FUNCTION__ << " autoProxyTask::getSettings data is missing" << std::endl; + RS_DBG("autoProxyTask::getSettings data is missing") << std::endl; rsAutoProxyMonitor::taskError(ticket); break; } @@ -203,7 +200,7 @@ void p3I2pBob::processTaskSync(taskTicket *ticket) case autoProxyTask::setSettings: // check if everything needed is set if (!data) { - RsDbg() << __PRETTY_FUNCTION__ << " autoProxyTask::setSettings data is missing" << std::endl; + RS_DBG("autoProxyTask::setSettings data is missing") << std::endl; rsAutoProxyMonitor::taskError(ticket); break; } @@ -223,7 +220,7 @@ void p3I2pBob::processTaskSync(taskTicket *ticket) break; case autoProxyTask::getErrorInfo: if (!data) { - RsDbg() << __PRETTY_FUNCTION__ << " autoProxyTask::getErrorInfo data is missing" << std::endl; + RS_DBG("autoProxyTask::getErrorInfo data is missing") << std::endl; rsAutoProxyMonitor::taskError(ticket); } else { RS_STACK_MUTEX(mLock); @@ -232,7 +229,7 @@ void p3I2pBob::processTaskSync(taskTicket *ticket) } break; default: - RsDbg() << __PRETTY_FUNCTION__ << " unknown task" << std::endl; + RS_DBG("unknown task") << std::endl; rsAutoProxyMonitor::taskError(ticket); break; } @@ -253,7 +250,7 @@ void p3I2pBob::threadTick() RS_STACK_MUTEX(mLock); std::stringstream ss; ss << "data_tick mState: " << mState << " mTask: " << mTask << " mBOBState: " << mBOBState << " mPending: " << mPending.size(); - Dbg4() << __PRETTY_FUNCTION__ << " " + ss.str() << std::endl; + RS_DBG4("" + ss.str()) << std::endl; } sleepTime += stateMachineController(); @@ -294,13 +291,13 @@ int p3I2pBob::stateMachineBOB() while (answer.find("OK Listing done") == std::string::npos) { std::stringstream ss; ss << "stateMachineBOB status check: read loop, counter: " << counter; - Dbg3() << __PRETTY_FUNCTION__ << " " + ss.str() << std::endl; + RS_DBG3("" + ss.str()) << std::endl; answer += recv(); counter++; } if (answer.find(mTunnelName) == std::string::npos) { - RsDbg() << __PRETTY_FUNCTION__ << " status check: tunnel down!" << std::endl; + RS_DBG("status check: tunnel down!") << std::endl; // signal error *((bool *)mProcessing->data) = true; } @@ -340,8 +337,8 @@ int p3I2pBob::stateMachineBOB_locked_failure(const std::string &answer, const bo return sleepFactorDefault; } - RsDbg() << __PRETTY_FUNCTION__ << " FAILED to run command '" + currentState.command + "'" << std::endl; - RsDbg() << __PRETTY_FUNCTION__ << " '" + answer + "'" << std::endl; + RS_DBG("FAILED to run command '" + currentState.command + "'") << std::endl; + RS_DBG("'" + answer + "'") << std::endl; mErrorMsg.append("FAILED to run command '" + currentState.command + "'" + '\n'); mErrorMsg.append("reason '" + answer + "'" + '\n'); @@ -388,14 +385,14 @@ int p3I2pBob::stateMachineController() return stateMachineController_locked_idle(); case csDoConnect: if (!connectI2P()) { - RsDbg() << __PRETTY_FUNCTION__ << " doConnect: unable to connect" << std::endl; + RS_DBG("doConnect: unable to connect") << std::endl; mStateOld = mState; mState = csError; mErrorMsg = "unable to connect to BOB port"; return sleepFactorSlow; } - Dbg4() << __PRETTY_FUNCTION__ << " doConnect: connected" << std::endl; + RS_DBG4("doConnect: connected") << std::endl; mState = csConnected; break; case csConnected: @@ -403,7 +400,7 @@ int p3I2pBob::stateMachineController() case csWaitForBob: // check connection problems if (mSocket == 0) { - RsDbg() << __PRETTY_FUNCTION__ << " waitForBob: conection lost" << std::endl; + RS_DBG("waitForBob: conection lost") << std::endl; mStateOld = mState; mState = csError; mErrorMsg = "connection lost to BOB"; @@ -413,21 +410,21 @@ int p3I2pBob::stateMachineController() // check for finished BOB protocol if (mBOBState == bsCleared) { // done - Dbg4() << __PRETTY_FUNCTION__ << " waitForBob: mBOBState == bsCleared" << std::endl; + RS_DBG4("waitForBob: mBOBState == bsCleared") << std::endl; mState = csDoDisconnect; } break; case csDoDisconnect: if (!disconnectI2P() || mSocket != 0) { // just in case - RsDbg() << __PRETTY_FUNCTION__ << " doDisconnect: can't disconnect" << std::endl; + RS_DBG("doDisconnect: can't disconnect") << std::endl; mStateOld = mState; mState = csError; mErrorMsg = "unable to disconnect from BOB"; return sleepFactorDefault; } - Dbg4() << __PRETTY_FUNCTION__ << " doDisconnect: disconnected" << std::endl; + RS_DBG4("doDisconnect: disconnected") << std::endl; mState = csDisconnected; break; case csDisconnected: @@ -458,7 +455,7 @@ int p3I2pBob::stateMachineController_locked_idle() mProcessing->task == autoProxyTask::stop || mProcessing->task == autoProxyTask::proxyStatusCheck)) { // skip since we are not enabled - Dbg1() << __PRETTY_FUNCTION__ << " disabled -> skipping ticket" << std::endl; + RS_DBG1("disabled -> skipping ticket") << std::endl; rsAutoProxyMonitor::taskDone(mProcessing, autoProxyStatus::disabled); mProcessing = NULL; } else { @@ -480,7 +477,7 @@ int p3I2pBob::stateMachineController_locked_idle() mTask = ctRunCheck; break; default: - Dbg1() << __PRETTY_FUNCTION__ << " unknown async task" << std::endl; + RS_DBG1("unknown async task") << std::endl; rsAutoProxyMonitor::taskError(mProcessing); mProcessing = NULL; break; @@ -528,28 +525,28 @@ int p3I2pBob::stateMachineController_locked_connected() case ctRunSetUp: // when we have a key use it for server tunnel! if(mSetting.address.privateKey.empty()) { - Dbg4() << __PRETTY_FUNCTION__ << " setting mBOBState = setnickC" << std::endl; + RS_DBG4("setting mBOBState = setnickC") << std::endl; mBOBState = bsSetnickC; } else { - Dbg4() << __PRETTY_FUNCTION__ << " setting mBOBState = setnickS" << std::endl; + RS_DBG4("setting mBOBState = setnickS") << std::endl; mBOBState = bsSetnickS; } break; case ctRunShutDown: // shut down existing tunnel - Dbg4() << __PRETTY_FUNCTION__ << " setting mBOBState = getnick" << std::endl; + RS_DBG4("setting mBOBState = getnick") << std::endl; mBOBState = bsGetnick; break; case ctRunCheck: - Dbg4() << __PRETTY_FUNCTION__ << " setting mBOBState = list" << std::endl; + RS_DBG4("setting mBOBState = list") << std::endl; mBOBState = bsList; break; case ctRunGetKeys: - Dbg4() << __PRETTY_FUNCTION__ << " setting mBOBState = setnickN" << std::endl; + RS_DBG4("setting mBOBState = setnickN") << std::endl; mBOBState = bsSetnickN; break; case ctIdle: - RsDbg() << __PRETTY_FUNCTION__ << " task is idle. This should not happen!" << std::endl; + RS_DBG("task is idle. This should not happen!") << std::endl; break; } @@ -565,7 +562,7 @@ int p3I2pBob::stateMachineController_locked_disconnected() if(errorHappened) { // reset old state mStateOld = csIdel; - RsDbg() << __PRETTY_FUNCTION__ << " error during process!" << std::endl; + RS_DBG("error during process!") << std::endl; } // answer ticket @@ -594,12 +591,12 @@ int p3I2pBob::stateMachineController_locked_disconnected() mTask = mTaskOld; if (!errorHappened) { - Dbg4() << __PRETTY_FUNCTION__ << " run check result: ok" << std::endl; + RS_DBG4("run check result: ok") << std::endl; break; } // switch to error newState = csError; - RsDbg() << __PRETTY_FUNCTION__ << " run check result: error" << std::endl; + RS_DBG("run check result: error") << std::endl; mErrorMsg = "Connection check failed. Will try to restart tunnel."; break; @@ -622,7 +619,7 @@ int p3I2pBob::stateMachineController_locked_disconnected() mTask = mTaskOld; break; case ctIdle: - RsDbg() << __PRETTY_FUNCTION__ << " task is idle. This should not happen!" << std::endl; + RS_DBG("task is idle. This should not happen!") << std::endl; rsAutoProxyMonitor::taskError(mProcessing); } mProcessing = NULL; @@ -638,14 +635,14 @@ int p3I2pBob::stateMachineController_locked_error() { // wait for bob protocoll if (mBOBState != bsCleared) { - Dbg4() << __PRETTY_FUNCTION__ << " waiting for BOB" << std::endl; + RS_DBG4("waiting for BOB") << std::endl; return sleepFactorFast; } #if 0 std::stringstream ss; ss << "stateMachineController_locked_error: mProcessing: " << (mProcessing ? "not null" : "null"); - Dbg4() << __PRETTY_FUNCTION__ << " " + ss.str() << std::endl; + RS_DBG4("" + ss.str()) << std::endl; #endif // try to finish ticket @@ -653,7 +650,7 @@ int p3I2pBob::stateMachineController_locked_error() switch (mTask) { case ctRunCheck: // connection check failed at some point - RsDbg() << __PRETTY_FUNCTION__ << " failed to check proxy status (it's likely dead)!" << std::endl; + RS_DBG("failed to check proxy status (it's likely dead)!") << std::endl; *((bool *)mProcessing->data) = true; mState = csDoDisconnect; mStateOld = csIdel; @@ -661,7 +658,7 @@ int p3I2pBob::stateMachineController_locked_error() break; case ctRunShutDown: // not a big deal though - RsDbg() << __PRETTY_FUNCTION__ << " failed to shut down tunnel (it's likely dead though)!" << std::endl; + RS_DBG("failed to shut down tunnel (it's likely dead though)!") << std::endl; mState = csDoDisconnect; mStateOld = csIdel; mErrorMsg.clear(); @@ -669,14 +666,14 @@ int p3I2pBob::stateMachineController_locked_error() case ctIdle: // should not happen but we need to deal with it // this will produce some error messages in the log and finish the task (marked as failed) - RsDbg() << __PRETTY_FUNCTION__ << " task is idle. This should not happen!" << std::endl; + RS_DBG("task is idle. This should not happen!") << std::endl; mState = csDoDisconnect; mStateOld = csIdel; mErrorMsg.clear(); break; case ctRunGetKeys: case ctRunSetUp: - RsDbg() << __PRETTY_FUNCTION__ << " failed to receive key / start up" << std::endl; + RS_DBG("failed to receive key / start up") << std::endl; mStateOld = csError; mState = csDoDisconnect; // keep the error message @@ -687,7 +684,7 @@ int p3I2pBob::stateMachineController_locked_error() // periodically retry if (mLastProxyCheck < time(NULL) - (selfCheckPeroid >> 1) && mTask == ctRunSetUp) { - RsDbg() << __PRETTY_FUNCTION__ << " retrying" << std::endl; + RS_DBG("retrying") << std::endl; mLastProxyCheck = time(NULL); mErrorMsg.clear(); @@ -700,7 +697,7 @@ int p3I2pBob::stateMachineController_locked_error() // check for new tickets if (!mPending.empty()) { - Dbg4() << __PRETTY_FUNCTION__ << " processing new ticket" << std::endl; + RS_DBG4("processing new ticket") << std::endl; // reset and try new task mTask = ctIdle; @@ -731,7 +728,7 @@ RsSerialiser *p3I2pBob::setupSerialiser() bool p3I2pBob::saveList(bool &cleanup, std::list &lst) { - Dbg4() << __PRETTY_FUNCTION__ << std::endl; + RS_DBG4("") << std::endl; cleanup = true; RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet; @@ -766,7 +763,7 @@ bool p3I2pBob::saveList(bool &cleanup, std::list &lst) bool p3I2pBob::loadList(std::list &load) { - Dbg4() << __PRETTY_FUNCTION__ << std::endl; + RS_DBG4("") << std::endl; for(std::list::const_iterator it = load.begin(); it!=load.end(); ++it) { RsConfigKeyValueSet *vitem = dynamic_cast(*it); @@ -786,7 +783,7 @@ bool p3I2pBob::loadList(std::list &load) getKVSUInt(kit, kConfigKeyOutQuantity, mSetting.outQuantity) getKVSUInt(kit, kConfigKeyOutVariance, mSetting.outVariance) else - RsDbg() << __PRETTY_FUNCTION__ << " unknown key: " + kit->key << std::endl; + RS_DBG("unknown key: " + kit->key) << std::endl; } } delete vitem; @@ -850,7 +847,7 @@ void p3I2pBob::getStates(bobStates *bs) std::string p3I2pBob::executeCommand(const std::string &command) { - Dbg4() << __PRETTY_FUNCTION__ << " running '" + command + "'" << std::endl; + RS_DBG4("running '" + command + "'") << std::endl; std::string copy = command; copy.push_back('\n'); @@ -862,7 +859,7 @@ std::string p3I2pBob::executeCommand(const std::string &command) // receive answer (trailing new line is already removed!) std::string ans = recv(); - Dbg4() << __PRETTY_FUNCTION__ << " answer '" + ans + "'" << std::endl; + RS_DBG4("answer '" + ans + "'") << std::endl; return ans; } @@ -872,7 +869,7 @@ bool p3I2pBob::connectI2P() // there is only one thread that touches mSocket - no need for a lock if (mSocket != 0) { - RsDbg() << __PRETTY_FUNCTION__ << " mSocket != 0" << std::endl; + RS_DBG("mSocket != 0") << std::endl; return false; } @@ -880,21 +877,21 @@ bool p3I2pBob::connectI2P() mSocket = unix_socket(PF_INET, SOCK_STREAM, 0); if (mSocket < 0) { - RsDbg() << __PRETTY_FUNCTION__ << " Failed to open socket! Socket Error: " + socket_errorType(errno) << std::endl; + RS_DBG("Failed to open socket! Socket Error: " + socket_errorType(errno)) << std::endl; return false; } // connect int err = unix_connect(mSocket, mI2PProxyAddr); if (err != 0) { - RsDbg() << __PRETTY_FUNCTION__ << " Failed to connect to BOB! Socket Error: " + socket_errorType(errno) << std::endl; + RS_DBG("Failed to connect to BOB! Socket Error: " + socket_errorType(errno)) << std::endl; return false; } // receive hello msg recv(); - Dbg4() << __PRETTY_FUNCTION__ << " done" << std::endl; + RS_DBG4("done") << std::endl; return true; } @@ -903,17 +900,17 @@ bool p3I2pBob::disconnectI2P() // there is only one thread that touches mSocket - no need for a lock if (mSocket == 0) { - RsDbg() << __PRETTY_FUNCTION__ << " mSocket == 0" << std::endl; + RS_DBG("mSocket == 0") << std::endl; return true; } int err = unix_close(mSocket); if (err != 0) { - RsDbg() << __PRETTY_FUNCTION__ << " Failed to close socket! Socket Error: " + socket_errorType(errno) << std::endl; + RS_DBG("Failed to close socket! Socket Error: " + socket_errorType(errno)) << std::endl; return false; } - Dbg4() << __PRETTY_FUNCTION__ << " done" << std::endl; + RS_DBG4("done") << std::endl; mSocket = 0; return true; } @@ -934,7 +931,7 @@ std::string toString(const std::string &a, const int8_t b) { void p3I2pBob::finalizeSettings_locked() { - Dbg4() << __PRETTY_FUNCTION__ << std::endl; + RS_DBG4("") << std::endl; sockaddr_storage_clear(mI2PProxyAddr); // get i2p proxy addr @@ -945,8 +942,8 @@ void p3I2pBob::finalizeSettings_locked() sockaddr_storage_setipv4(mI2PProxyAddr, (sockaddr_in*)&proxy); sockaddr_storage_setport(mI2PProxyAddr, 2827); - Dbg4() << __PRETTY_FUNCTION__ << " using " + sockaddr_storage_tostring(mI2PProxyAddr) << std::endl; - Dbg4() << __PRETTY_FUNCTION__ << " using " + mSetting.address.base32 << std::endl; + RS_DBG4("using " + sockaddr_storage_tostring(mI2PProxyAddr)) << std::endl; + RS_DBG4("using " + mSetting.address.base32) << std::endl; peerState ps; mPeerMgr->getOwnNetStatus(ps); @@ -961,7 +958,7 @@ void p3I2pBob::finalizeSettings_locked() std::vector tmp(len); RSRandom::random_bytes(tmp.data(), len); const std::string location = Radix32::encode(tmp.data(), len); - Dbg4() << __PRETTY_FUNCTION__ << " using suffix " + location << std::endl; + RS_DBG4("using suffix " + location) << std::endl; mTunnelName = "RetroShare-" + location; const std::string setnick = "setnick RetroShare-" + location; @@ -1029,7 +1026,7 @@ void p3I2pBob::finalizeSettings_locked() void p3I2pBob::updateSettings_locked() { - Dbg4() << __PRETTY_FUNCTION__ << std::endl; + RS_DBG4("") << std::endl; sockaddr_storage proxy; mPeerMgr->getProxyServerAddress(RS_HIDDEN_TYPE_I2P, proxy); @@ -1105,7 +1102,7 @@ std::string p3I2pBob::recv() // sanity check if (length != bufferSize) { // expectation: a full buffer was peeked) - Dbg1() << __PRETTY_FUNCTION__ << " peeked less than bufferSize but also didn't found a new line character" << std::endl; + RS_DBG1("peeked less than bufferSize but also didn't found a new line character") << std::endl; } // this should never happen assert(length <= bufferSize);