diff --git a/Daemon.cpp b/Daemon.cpp index 00bfd817..7dec30a9 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -124,47 +124,16 @@ namespace i2p ipv4 = false; ipv6 = true; #endif - - i2p::context.SetSupportsV6 (ipv6); - i2p::context.SetSupportsV4 (ipv4); - - bool nat; i2p::config::GetOption("nat", nat); - if (nat) - { - LogPrint(eLogInfo, "Daemon: assuming be are behind NAT"); - // we are behind nat, try setting via host - std::string host; i2p::config::GetOption("host", host); - if (!i2p::config::IsDefault("host")) - { - LogPrint(eLogInfo, "Daemon: setting address for incoming connections to ", host); - i2p::context.UpdateAddress (boost::asio::ip::address::from_string (host)); - } - } - else - { - // we are not behind nat - std::string ifname; i2p::config::GetOption("ifname", ifname); - if (ifname.size()) - { - // bind to interface, we have no NAT so set external address too - auto addr = i2p::util::net::GetInterfaceAddress(ifname, ipv6); - LogPrint(eLogInfo, "Daemon: bind to network interface ", ifname, " with public address ", addr); - i2p::context.UpdateAddress(addr); - } - } - - uint16_t port; i2p::config::GetOption("port", port); if (!i2p::config::IsDefault("port")) - { + { LogPrint(eLogInfo, "Daemon: accepting incoming connections at port ", port); i2p::context.UpdatePort (port); - } - - - bool transit; i2p::config::GetOption("notransit", transit); + } i2p::context.SetSupportsV6 (ipv6); i2p::context.SetSupportsV4 (ipv4); + + bool transit; i2p::config::GetOption("notransit", transit); i2p::context.SetAcceptsTunnels (!transit); uint16_t transitTunnels; i2p::config::GetOption("limits.transittunnels", transitTunnels); SetMaxNumTransitTunnels (transitTunnels); @@ -252,14 +221,15 @@ namespace i2p bool ntcp; i2p::config::GetOption("ntcp", ntcp); bool ssu; i2p::config::GetOption("ssu", ssu); LogPrint(eLogInfo, "Daemon: starting Transports"); - if(!ssu) LogPrint(eLogDebug, "Daemon: ssu disabled"); - if(!ntcp) LogPrint(eLogDebug, "Daemon: ntcp disabled"); + if(!ssu) LogPrint(eLogInfo, "Daemon: ssu disabled"); + if(!ntcp) LogPrint(eLogInfo, "Daemon: ntcp disabled"); i2p::transport::transports.Start(ntcp, ssu); if (i2p::transport::transports.IsBoundNTCP() || i2p::transport::transports.IsBoundSSU()) { LogPrint(eLogInfo, "Daemon: Transports started"); } else { LogPrint(eLogError, "Daemon: failed to start Transports"); /** shut down netdb right away */ + i2p::transport::transports.Stop(); i2p::data::netdb.Stop(); return false; } diff --git a/RouterContext.cpp b/RouterContext.cpp index 768750bb..bb3d0110 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -49,9 +49,7 @@ namespace i2p uint16_t port; i2p::config::GetOption("port", port); if (!port) port = rand () % (30777 - 9111) + 9111; // I2P network ports range - std::string host; i2p::config::GetOption("host", host); - if (i2p::config::IsDefault("host")) - host = "127.0.0.1"; // replace default address with safe value + std::string host = i2p::util::config::GetHost(); routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ()); routerInfo.AddNTCPAddress (host.c_str(), port); routerInfo.SetCaps (i2p::data::RouterInfo::eReachable | diff --git a/Transports.cpp b/Transports.cpp index 2d572aef..a54455cc 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -114,7 +114,7 @@ namespace transport auto& addresses = context.GetRouterInfo ().GetAddresses (); for (auto address : addresses) { - if (!m_NTCPServer && enableNTCP) + if (m_NTCPServer == nullptr && enableNTCP) { m_NTCPServer = new NTCPServer (); m_NTCPServer->Start (); @@ -129,7 +129,7 @@ namespace transport if (address->transportStyle == RouterInfo::eTransportSSU) { - if (!m_SSUServer && enableSSU) + if (m_SSUServer == nullptr && enableSSU) { if (address->host.is_v4()) m_SSUServer = new SSUServer (address->port); diff --git a/util.cpp b/util.cpp index 89bcda6c..5913d9a7 100644 --- a/util.cpp +++ b/util.cpp @@ -461,5 +461,30 @@ namespace net } } +namespace config +{ + std::string GetHost(bool ipv4, bool ipv6) + { + std::string host; + if(ipv6) + host = "::"; + else if(ipv4) + host = "127.0.0.1"; + bool nat; i2p::config::GetOption("nat", nat); + if (nat) + { + if (!i2p::config::IsDefault("host")) + i2p::config::GetOption("host", host); + } + else + { + // we are not behind nat + std::string ifname; i2p::config::GetOption("ifname", ifname); + if (ifname.size()) + host = i2p::util::net::GetInterfaceAddress(ifname, ipv6).to_string(); // bind to interface, we have no NAT so set external address too + } + return host; + } +} // config } // util } // i2p diff --git a/util.h b/util.h index 7c393e02..8995f12b 100644 --- a/util.h +++ b/util.h @@ -68,6 +68,12 @@ namespace util int GetMTU (const boost::asio::ip::address& localAddress); const boost::asio::ip::address GetInterfaceAddress(const std::string & ifname, bool ipv6=false); } + + namespace config + { + /** get the host to use from out config, for use in RouterContext.cpp */ + std::string GetHost(bool ipv4=true, bool ipv6=true); + } } }