don't delete BOBDestination if used by another BOBSession

This commit is contained in:
orignal 2023-07-06 13:08:39 -04:00
parent 5f43026986
commit b8f998f76a
2 changed files with 16 additions and 18 deletions

View File

@ -357,13 +357,13 @@ namespace client
os << data << std::endl; os << data << std::endl;
} }
void BOBCommandSession::BuildStatusLine(bool currentTunnel, BOBDestination *dest, std::string &out) void BOBCommandSession::BuildStatusLine(bool currentTunnel, std::shared_ptr<BOBDestination> dest, std::string &out)
{ {
// helper lambdas // helper lambdas
const auto issetStr = [](const std::string &str) { return str.empty() ? "not_set" : str; }; // for inhost, outhost const auto issetStr = [](const std::string &str) { return str.empty() ? "not_set" : str; }; // for inhost, outhost
const auto issetNum = [&issetStr](const int p) { return issetStr(p == 0 ? "" : std::to_string(p)); }; // for inport, outport const auto issetNum = [&issetStr](const int p) { return issetStr(p == 0 ? "" : std::to_string(p)); }; // for inport, outport
const auto destExists = [](const BOBDestination * const dest) { return dest != nullptr; }; const auto destExists = [](const BOBDestination * const dest) { return dest != nullptr; };
const auto destReady = [](const BOBDestination * const dest) { return dest->IsRunning(); }; const auto destReady = [](const BOBDestination * const dest) { return dest && dest->IsRunning(); };
const auto bool_str = [](const bool v) { return v ? "true" : "false"; }; // bool -> str const auto bool_str = [](const bool v) { return v ? "true" : "false"; }; // bool -> str
// tunnel info // tunnel info
@ -373,9 +373,9 @@ namespace client
const std::string outhost = issetStr(currentTunnel ? m_OutHost : dest->GetOutHost()); const std::string outhost = issetStr(currentTunnel ? m_OutHost : dest->GetOutHost());
const std::string inport = issetNum(currentTunnel ? m_InPort : dest->GetInPort()); const std::string inport = issetNum(currentTunnel ? m_InPort : dest->GetInPort());
const std::string outport = issetNum(currentTunnel ? m_OutPort : dest->GetOutPort()); const std::string outport = issetNum(currentTunnel ? m_OutPort : dest->GetOutPort());
const bool keys = destExists(dest); // key must exist when destination is created const bool keys = destExists(dest.get ()); // key must exist when destination is created
const bool starting = destExists(dest) && !destReady(dest); const bool starting = destExists(dest.get ()) && !destReady(dest.get ());
const bool running = destExists(dest) && destReady(dest); const bool running = destExists(dest.get ()) && destReady(dest.get ());
const bool stopping = false; const bool stopping = false;
// build line // build line
@ -446,7 +446,7 @@ namespace client
if (!m_CurrentDestination) if (!m_CurrentDestination)
{ {
m_CurrentDestination = new BOBDestination (i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options), // deleted in clear command m_CurrentDestination = std::make_shared<BOBDestination> (i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options), // deleted in clear command
m_Nickname, m_InHost, m_OutHost, m_InPort, m_OutPort, m_IsQuiet); m_Nickname, m_InHost, m_OutHost, m_InPort, m_OutPort, m_IsQuiet);
m_Owner.AddDestination (m_Nickname, m_CurrentDestination); m_Owner.AddDestination (m_Nickname, m_CurrentDestination);
} }
@ -666,7 +666,8 @@ namespace client
SendReplyError ("Address Not found"); SendReplyError ("Address Not found");
return; return;
} }
auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination (); auto localDestination = (m_CurrentDestination && m_CurrentDestination->IsRunning ()) ?
m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination ();
if (!localDestination) if (!localDestination)
{ {
SendReplyError ("No local destination"); SendReplyError ("No local destination");
@ -880,8 +881,6 @@ namespace client
{ {
if (IsRunning ()) if (IsRunning ())
Stop (); Stop ();
for (const auto& it: m_Destinations)
delete it.second;
} }
void BOBCommandChannel::Start () void BOBCommandChannel::Start ()
@ -898,9 +897,9 @@ namespace client
StopIOService (); StopIOService ();
} }
void BOBCommandChannel::AddDestination (const std::string& name, BOBDestination * dest) void BOBCommandChannel::AddDestination (const std::string& name, std::shared_ptr<BOBDestination> dest)
{ {
m_Destinations[name] = dest; m_Destinations.emplace (name, dest);
} }
void BOBCommandChannel::DeleteDestination (const std::string& name) void BOBCommandChannel::DeleteDestination (const std::string& name)
@ -909,12 +908,11 @@ namespace client
if (it != m_Destinations.end ()) if (it != m_Destinations.end ())
{ {
it->second->Stop (); it->second->Stop ();
delete it->second;
m_Destinations.erase (it); m_Destinations.erase (it);
} }
} }
BOBDestination * BOBCommandChannel::FindDestination (const std::string& name) std::shared_ptr<BOBDestination> BOBCommandChannel::FindDestination (const std::string& name)
{ {
auto it = m_Destinations.find (name); auto it = m_Destinations.find (name);
if (it != m_Destinations.end ()) if (it != m_Destinations.end ())

View File

@ -228,7 +228,7 @@ namespace client
void SendReplyError (const char * msg); void SendReplyError (const char * msg);
void SendRaw (const char * data); void SendRaw (const char * data);
void BuildStatusLine(bool currentTunnel, BOBDestination *destination, std::string &out); void BuildStatusLine(bool currentTunnel, std::shared_ptr<BOBDestination> destination, std::string &out);
private: private:
@ -240,7 +240,7 @@ namespace client
uint16_t m_InPort, m_OutPort; uint16_t m_InPort, m_OutPort;
i2p::data::PrivateKeys m_Keys; i2p::data::PrivateKeys m_Keys;
std::map<std::string, std::string> m_Options; std::map<std::string, std::string> m_Options;
BOBDestination * m_CurrentDestination; std::shared_ptr<BOBDestination> m_CurrentDestination;
}; };
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len); typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
@ -255,9 +255,9 @@ namespace client
void Stop (); void Stop ();
boost::asio::io_service& GetService () { return GetIOService (); }; boost::asio::io_service& GetService () { return GetIOService (); };
void AddDestination (const std::string& name, BOBDestination * dest); void AddDestination (const std::string& name, std::shared_ptr<BOBDestination> dest);
void DeleteDestination (const std::string& name); void DeleteDestination (const std::string& name);
BOBDestination * FindDestination (const std::string& name); std::shared_ptr<BOBDestination> FindDestination (const std::string& name);
private: private:
@ -267,7 +267,7 @@ namespace client
private: private:
boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::tcp::acceptor m_Acceptor;
std::map<std::string, BOBDestination *> m_Destinations; std::map<std::string, std::shared_ptr<BOBDestination> > m_Destinations;
std::map<std::string, BOBCommandHandler> m_CommandHandlers; std::map<std::string, BOBCommandHandler> m_CommandHandlers;
std::map<std::string, std::string> m_HelpStrings; std::map<std::string, std::string> m_HelpStrings;