diff --git a/daemon/Daemon.cpp b/daemon/Daemon.cpp index 744c1e6c..0ecc446a 100644 --- a/daemon/Daemon.cpp +++ b/daemon/Daemon.cpp @@ -79,7 +79,7 @@ namespace util i2p::config::Init(); i2p::config::ParseCmdline(argc, argv); - std::string config; i2p::config::GetOption("conf", config); + std::string config; i2p::config::GetOption("conf", config); std::string datadir; if(DaemonDataDir != "") { datadir = DaemonDataDir; @@ -111,9 +111,9 @@ namespace util certsdir = i2p::fs::GetCertsDir(); - std::string logs = ""; i2p::config::GetOption("log", logs); - std::string logfile = ""; i2p::config::GetOption("logfile", logfile); - std::string loglevel = ""; i2p::config::GetOption("loglevel", loglevel); + std::string logs = ""; i2p::config::GetOption("log", logs); + std::string logfile = ""; i2p::config::GetOption("logfile", logfile); + std::string loglevel = ""; i2p::config::GetOption("loglevel", loglevel); bool logclftime; i2p::config::GetOption("logclftime", logclftime); /* setup logging */ @@ -254,17 +254,17 @@ namespace util } bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2); if (ssu2) - { + { bool published; i2p::config::GetOption("ssu2.published", published); if (published) { uint16_t ssu2port; i2p::config::GetOption("ssu2.port", ssu2port); i2p::context.PublishSSU2Address (ssu2port, true, ipv4, ipv6); // publish - } + } else i2p::context.PublishSSU2Address (0, false, ipv4, ipv6); // unpublish } - + bool transit; i2p::config::GetOption("notransit", transit); i2p::context.SetAcceptsTunnels (!transit); uint16_t transitTunnels; i2p::config::GetOption("limits.transittunnels", transitTunnels); diff --git a/daemon/HTTPServer.cpp b/daemon/HTTPServer.cpp index 97295b59..beb5528d 100644 --- a/daemon/HTTPServer.cpp +++ b/daemon/HTTPServer.cpp @@ -324,9 +324,9 @@ namespace http { default: s << tr("Unknown"); } - if (address->IsV6 ()) + if (address->IsV6 ()) { - if (address->IsV4 ()) s << "v4"; + if (address->IsV4 ()) s << "v4"; s << "v6"; } s << "\r\n"; diff --git a/daemon/HTTPServer.h b/daemon/HTTPServer.h index 8e1520b8..8646253f 100644 --- a/daemon/HTTPServer.h +++ b/daemon/HTTPServer.h @@ -98,8 +98,8 @@ namespace http void ShowSAMSessions (std::stringstream& s); void ShowI2PTunnels (std::stringstream& s); void ShowLocalDestination (std::stringstream& s, const std::string& b32, uint32_t token); - void ShowSAMSession (std::stringstream& s, const std::string& id); - void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id); + void ShowSAMSession (std::stringstream& s, const std::string& id); + void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id); } // http } // i2p diff --git a/daemon/I2PControl.cpp b/daemon/I2PControl.cpp index 53463a45..46a219bf 100644 --- a/daemon/I2PControl.cpp +++ b/daemon/I2PControl.cpp @@ -66,29 +66,28 @@ namespace client m_SSLContext.use_private_key_file (i2pcp_key, boost::asio::ssl::context::pem); // handlers - m_MethodHandlers["Authenticate"] = &I2PControlService::AuthenticateHandler; - m_MethodHandlers["Echo"] = &I2PControlService::EchoHandler; - m_MethodHandlers["I2PControl"] = &I2PControlService::I2PControlHandler; - m_MethodHandlers["RouterInfo"] = &I2PControlService::RouterInfoHandler; - m_MethodHandlers["RouterManager"] = &I2PControlService::RouterManagerHandler; - m_MethodHandlers["NetworkSetting"] = &I2PControlService::NetworkSettingHandler; - m_MethodHandlers["ClientServicesInfo"] = &I2PControlService::ClientServicesInfoHandler; + m_MethodHandlers["Authenticate"] = &I2PControlService::AuthenticateHandler; + m_MethodHandlers["Echo"] = &I2PControlService::EchoHandler; + m_MethodHandlers["I2PControl"] = &I2PControlService::I2PControlHandler; + m_MethodHandlers["RouterInfo"] = &I2PControlService::RouterInfoHandler; + m_MethodHandlers["RouterManager"] = &I2PControlService::RouterManagerHandler; + m_MethodHandlers["NetworkSetting"] = &I2PControlService::NetworkSettingHandler; + m_MethodHandlers["ClientServicesInfo"] = &I2PControlService::ClientServicesInfoHandler; // I2PControl m_I2PControlHandlers["i2pcontrol.password"] = &I2PControlService::PasswordHandler; // RouterInfo - m_RouterInfoHandlers["i2p.router.uptime"] = &I2PControlService::UptimeHandler; - m_RouterInfoHandlers["i2p.router.version"] = &I2PControlService::VersionHandler; - m_RouterInfoHandlers["i2p.router.status"] = &I2PControlService::StatusHandler; - m_RouterInfoHandlers["i2p.router.netdb.knownpeers"] = &I2PControlService::NetDbKnownPeersHandler; - m_RouterInfoHandlers["i2p.router.netdb.activepeers"] = &I2PControlService::NetDbActivePeersHandler; - m_RouterInfoHandlers["i2p.router.net.bw.inbound.1s"] = &I2PControlService::InboundBandwidth1S; - m_RouterInfoHandlers["i2p.router.net.bw.outbound.1s"] = &I2PControlService::OutboundBandwidth1S; - m_RouterInfoHandlers["i2p.router.net.status"] = &I2PControlService::NetStatusHandler; + m_RouterInfoHandlers["i2p.router.uptime"] = &I2PControlService::UptimeHandler; + m_RouterInfoHandlers["i2p.router.version"] = &I2PControlService::VersionHandler; + m_RouterInfoHandlers["i2p.router.status"] = &I2PControlService::StatusHandler; + m_RouterInfoHandlers["i2p.router.netdb.knownpeers"] = &I2PControlService::NetDbKnownPeersHandler; + m_RouterInfoHandlers["i2p.router.netdb.activepeers"] = &I2PControlService::NetDbActivePeersHandler; + m_RouterInfoHandlers["i2p.router.net.bw.inbound.1s"] = &I2PControlService::InboundBandwidth1S; + m_RouterInfoHandlers["i2p.router.net.bw.outbound.1s"] = &I2PControlService::OutboundBandwidth1S; + m_RouterInfoHandlers["i2p.router.net.status"] = &I2PControlService::NetStatusHandler; m_RouterInfoHandlers["i2p.router.net.tunnels.participating"] = &I2PControlService::TunnelsParticipatingHandler; - m_RouterInfoHandlers["i2p.router.net.tunnels.successrate"] = -&I2PControlService::TunnelsSuccessRateHandler; + m_RouterInfoHandlers["i2p.router.net.tunnels.successrate"] = &I2PControlService::TunnelsSuccessRateHandler; m_RouterInfoHandlers["i2p.router.net.total.received.bytes"] = &I2PControlService::NetTotalReceivedBytes; m_RouterInfoHandlers["i2p.router.net.total.sent.bytes"] = &I2PControlService::NetTotalSentBytes; @@ -104,10 +103,10 @@ namespace client // ClientServicesInfo m_ClientServicesInfoHandlers["I2PTunnel"] = &I2PControlService::I2PTunnelInfoHandler; m_ClientServicesInfoHandlers["HTTPProxy"] = &I2PControlService::HTTPProxyInfoHandler; - m_ClientServicesInfoHandlers["SOCKS"] = &I2PControlService::SOCKSInfoHandler; - m_ClientServicesInfoHandlers["SAM"] = &I2PControlService::SAMInfoHandler; - m_ClientServicesInfoHandlers["BOB"] = &I2PControlService::BOBInfoHandler; - m_ClientServicesInfoHandlers["I2CP"] = &I2PControlService::I2CPInfoHandler; + m_ClientServicesInfoHandlers["SOCKS"] = &I2PControlService::SOCKSInfoHandler; + m_ClientServicesInfoHandlers["SAM"] = &I2PControlService::SAMInfoHandler; + m_ClientServicesInfoHandlers["BOB"] = &I2PControlService::BOBInfoHandler; + m_ClientServicesInfoHandlers["I2CP"] = &I2PControlService::I2CPInfoHandler; } I2PControlService::~I2PControlService () @@ -168,7 +167,7 @@ namespace client Accept (); if (ecode) { - LogPrint (eLogError, "I2PControl: Accept error: ", ecode.message ()); + LogPrint (eLogError, "I2PControl: Accept error: ", ecode.message ()); return; } LogPrint (eLogDebug, "I2PControl: New request from ", socket->lowest_layer ().remote_endpoint ()); @@ -290,12 +289,12 @@ namespace client { ss << "\"" << name << "\":"; if (value.length () > 0) - { + { if (quotes) ss << "\"" << value << "\""; else ss << value; - } + } else ss << "null"; } @@ -479,7 +478,7 @@ namespace client void I2PControlService::NetTotalSentBytes (std::ostringstream& results) { - InsertParam (results, "i2p.router.net.total.sent.bytes", (double)i2p::transport::transports.GetTotalSentBytes ()); + InsertParam (results, "i2p.router.net.total.sent.bytes", (double)i2p::transport::transports.GetTotalSentBytes ()); } @@ -507,7 +506,7 @@ namespace client m_ShutdownTimer.expires_from_now (boost::posix_time::seconds(1)); // 1 second to make sure response has been sent m_ShutdownTimer.async_wait ( [](const boost::system::error_code& ecode) - { + { Daemon.running = 0; }); } @@ -521,7 +520,7 @@ namespace client m_ShutdownTimer.expires_from_now (boost::posix_time::seconds(timeout + 1)); // + 1 second m_ShutdownTimer.async_wait ( [](const boost::system::error_code& ecode) - { + { Daemon.running = 0; }); } diff --git a/daemon/UPnP.cpp b/daemon/UPnP.cpp index fe6a565e..6ea4dc24 100644 --- a/daemon/UPnP.cpp +++ b/daemon/UPnP.cpp @@ -93,7 +93,7 @@ namespace transport #endif isError = err != UPNPDISCOVER_SUCCESS; -#else // MINIUPNPC_API_VERSION >= 8 +#else // MINIUPNPC_API_VERSION >= 8 err = 0; m_Devlist = upnpDiscover (UPNP_RESPONSE_TIMEOUT, NULL, NULL, 0); isError = m_Devlist == NULL; diff --git a/daemon/UPnP.h b/daemon/UPnP.h index e8220e24..59f3b785 100644 --- a/daemon/UPnP.h +++ b/daemon/UPnP.h @@ -51,7 +51,7 @@ namespace transport private: void Discover (); - int CheckMapping (const char* port, const char* type); + int CheckMapping (const char* port, const char* type); void PortMapping (); void TryPortMapping (std::shared_ptr address); void CloseMapping (); @@ -80,7 +80,7 @@ namespace transport } } -#else // USE_UPNP +#else // USE_UPNP namespace i2p { namespace transport { /* class stub */ diff --git a/libi2pd/Base.h b/libi2pd/Base.h index 073d9b40..79152e02 100644 --- a/libi2pd/Base.h +++ b/libi2pd/Base.h @@ -24,8 +24,8 @@ namespace data { size_t ByteStreamToBase32 (const uint8_t * InBuf, size_t len, char * outBuf, size_t outLen); /** - Compute the size for a buffer to contain encoded base64 given that the size of the input is input_size bytes - */ + * Compute the size for a buffer to contain encoded base64 given that the size of the input is input_size bytes + */ size_t Base64EncodingBufferSize(const size_t input_size); std::string ToBase64Standard (const std::string& in); // using standard table, for Proxy-Authorization diff --git a/libi2pd/Blinding.cpp b/libi2pd/Blinding.cpp index 9975c584..65e5f78c 100644 --- a/libi2pd/Blinding.cpp +++ b/libi2pd/Blinding.cpp @@ -99,7 +99,7 @@ namespace data static size_t BlindECDSA (i2p::data::SigningKeyType sigType, const uint8_t * key, const uint8_t * seed, Fn blind, Args&&...args) // blind is BlindEncodedPublicKeyECDSA or BlindEncodedPrivateKeyECDSA { - size_t publicKeyLength = 0; + size_t publicKeyLength = 0; EC_GROUP * group = nullptr; switch (sigType) { @@ -147,8 +147,8 @@ namespace data memcpy (m_PublicKey.data (), identity->GetSigningPublicKeyBuffer (), len); m_SigType = identity->GetSigningKeyType (); if (m_SigType == i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519) - m_BlindedSigType = i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519; // 7 -> 11 - else + m_BlindedSigType = i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519; // 7 -> 11 + else m_BlindedSigType = m_SigType; } diff --git a/libi2pd/Blinding.h b/libi2pd/Blinding.h index 2f670882..c78db003 100644 --- a/libi2pd/Blinding.h +++ b/libi2pd/Blinding.h @@ -28,8 +28,8 @@ namespace data const uint8_t * GetPublicKey () const { return m_PublicKey.data (); }; size_t GetPublicKeyLen () const { return m_PublicKey.size (); }; - SigningKeyType GetSigType () const { return m_SigType; }; - SigningKeyType GetBlindedSigType () const { return m_BlindedSigType; }; + SigningKeyType GetSigType () const { return m_SigType; }; + SigningKeyType GetBlindedSigType () const { return m_BlindedSigType; }; bool IsValid () const { return GetSigType (); }; // signature type 0 means invalid void GetSubcredential (const uint8_t * blinded, size_t len, uint8_t * subcredential) const; // 32 bytes diff --git a/libi2pd/Config.cpp b/libi2pd/Config.cpp index 3bc0dfa4..87a9e091 100644 --- a/libi2pd/Config.cpp +++ b/libi2pd/Config.cpp @@ -277,7 +277,7 @@ namespace config { ("ssu2.published", value()->default_value(false), "Publish SSU2 (default: disabled)") ("ssu2.port", value()->default_value(0), "Port to listen for incoming SSU2 packets (default: auto)") ; - + options_description nettime("Time sync options"); nettime.add_options() ("nettime.enabled", value()->default_value(false), "Disable time sync (default: disabled)") diff --git a/libi2pd/Config.h b/libi2pd/Config.h index dac5fc80..79463e65 100644 --- a/libi2pd/Config.h +++ b/libi2pd/Config.h @@ -29,16 +29,16 @@ namespace config { extern boost::program_options::variables_map m_Options; /** - * @brief Initialize list of acceptable parameters + * @brief Initialize list of acceptable parameters * * Should be called before any Parse* functions. */ void Init(); /** - * @brief Parse cmdline parameters, and show help if requested - * @param argc Cmdline arguments count, should be passed from main(). - * @param argv Cmdline parameters array, should be passed from main() + * @brief Parse cmdline parameters, and show help if requested + * @param argc Cmdline arguments count, should be passed from main(). + * @param argv Cmdline parameters array, should be passed from main() * * If --help is given in parameters, shows its list with description * and terminates the program with exitcode 0. @@ -52,8 +52,8 @@ namespace config { void ParseCmdline(int argc, char* argv[], bool ignoreUnknown = false); /** - * @brief Load and parse given config file - * @param path Path to config file + * @brief Load and parse given config file + * @param path Path to config file * * If error occurred when opening file path is points to, * we show the error message and terminate program. @@ -67,14 +67,14 @@ namespace config { void ParseConfig(const std::string& path); /** - * @brief Used to combine options from cmdline, config and default values + * @brief Used to combine options from cmdline, config and default values */ void Finalize(); /** - * @brief Accessor to parameters by name - * @param name Name of the requested parameter - * @param value Variable where to store option + * @brief Accessor to parameters by name + * @param name Name of the requested parameter + * @param value Variable where to store option * @return this function returns false if parameter not found * * Example: uint16_t port; GetOption("sam.port", port); @@ -98,9 +98,9 @@ namespace config { bool GetOptionAsAny(const std::string& name, boost::any& value); /** - * @brief Set value of given parameter - * @param name Name of settable parameter - * @param value New parameter value + * @brief Set value of given parameter + * @param name Name of settable parameter + * @param value New parameter value * @return true if value set up successful, false otherwise * * Example: uint16_t port = 2827; SetOption("bob.port", port); @@ -116,8 +116,8 @@ namespace config { } /** - * @brief Check is value explicitly given or default - * @param name Name of checked parameter + * @brief Check is value explicitly given or default + * @param name Name of checked parameter * @return true if value set to default, false otherwise */ bool IsDefault(const char *name); diff --git a/libi2pd/Crypto.cpp b/libi2pd/Crypto.cpp index 0e00a2e8..d7fb965e 100644 --- a/libi2pd/Crypto.cpp +++ b/libi2pd/Crypto.cpp @@ -1313,8 +1313,8 @@ namespace crypto for (const auto& it: bufs) SHA256_Update (&ctx, it.first, it.second); SHA256_Final (m_H, &ctx); - } - + } + void NoiseSymmetricState::MixKey (const uint8_t * sharedSecret) { HKDF (m_CK, sharedSecret, 32, "", m_CK); @@ -1330,7 +1330,7 @@ namespace crypto SHA256_Init (&ctx); SHA256_Update (&ctx, hh, 32); SHA256_Update (&ctx, pub, 32); - SHA256_Final (state.m_H, &ctx); // h = MixHash(pub) = SHA256(hh || pub) + SHA256_Final (state.m_H, &ctx); // h = MixHash(pub) = SHA256(hh || pub) } void InitNoiseNState (NoiseSymmetricState& state, const uint8_t * pub) @@ -1368,12 +1368,12 @@ namespace crypto }; // SHA256 ("Noise_XKchaobfse+hs1+hs2+hs3_25519_ChaChaPoly_SHA256") static const uint8_t hh[32] = { - 0xdc, 0x85, 0xe6, 0xaf, 0x7b, 0x02, 0x65, 0x0c, 0xf1, 0xf9, 0x0d, 0x71, 0xfb, 0xc6, 0xd4, 0x53, + 0xdc, 0x85, 0xe6, 0xaf, 0x7b, 0x02, 0x65, 0x0c, 0xf1, 0xf9, 0x0d, 0x71, 0xfb, 0xc6, 0xd4, 0x53, 0xa7, 0xcf, 0x6d, 0xbf, 0xbd, 0x52, 0x5e, 0xa5, 0xb5, 0x79, 0x1c, 0x47, 0xb3, 0x5e, 0xbc, 0x33 }; // SHA256 (protocolNameHash) InitNoiseState (state, protocolNameHash, hh, pub); } - + void InitNoiseIKState (NoiseSymmetricState& state, const uint8_t * pub) { static const uint8_t protocolNameHash[32] = diff --git a/libi2pd/Crypto.h b/libi2pd/Crypto.h index 726bd0a1..c6dcd2cc 100644 --- a/libi2pd/Crypto.h +++ b/libi2pd/Crypto.h @@ -30,24 +30,24 @@ // recognize openssl version and features #if (defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER >= 0x3050200fL)) // LibreSSL 3.5.2 and above -# define LEGACY_OPENSSL 0 +# define LEGACY_OPENSSL 0 #elif ((OPENSSL_VERSION_NUMBER < 0x010100000) || defined(LIBRESSL_VERSION_NUMBER)) // 1.0.2 and below or LibreSSL -# define LEGACY_OPENSSL 1 -# define X509_getm_notBefore X509_get_notBefore -# define X509_getm_notAfter X509_get_notAfter +# define LEGACY_OPENSSL 1 +# define X509_getm_notBefore X509_get_notBefore +# define X509_getm_notAfter X509_get_notAfter #else -# define LEGACY_OPENSSL 0 -# if (OPENSSL_VERSION_NUMBER >= 0x010101000) // 1.1.1 -# define OPENSSL_HKDF 1 -# define OPENSSL_EDDSA 1 -# define OPENSSL_X25519 1 +# define LEGACY_OPENSSL 0 +# if (OPENSSL_VERSION_NUMBER >= 0x010101000) // 1.1.1 +# define OPENSSL_HKDF 1 +# define OPENSSL_EDDSA 1 +# define OPENSSL_X25519 1 # if (OPENSSL_VERSION_NUMBER != 0x030000000) // 3.0.0, regression in SipHash -# define OPENSSL_SIPHASH 1 +# define OPENSSL_SIPHASH 1 # endif -# endif -# if !defined OPENSSL_NO_CHACHA && !defined OPENSSL_NO_POLY1305 // some builds might not include them -# define OPENSSL_AEAD_CHACHA20_POLY1305 1 -# endif +# endif +# if !defined OPENSSL_NO_CHACHA && !defined OPENSSL_NO_POLY1305 // some builds might not include them +# define OPENSSL_AEAD_CHACHA20_POLY1305 1 +# endif #endif namespace i2p @@ -385,7 +385,7 @@ inline int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) if (dh->p) BN_free (dh->p); if (dh->q) BN_free (dh->q); if (dh->g) BN_free (dh->g); - dh->p = p; dh->q = q; dh->g = g; return 1; + dh->p = p; dh->q = q; dh->g = g; return 1; } inline int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) { diff --git a/libi2pd/Datagram.cpp b/libi2pd/Datagram.cpp index 2affc8be..30635b09 100644 --- a/libi2pd/Datagram.cpp +++ b/libi2pd/Datagram.cpp @@ -324,7 +324,7 @@ namespace datagram auto path = m_RoutingSession->GetSharedRoutingPath(); if (path && m_RoutingSession->IsRatchets () && - m_LastUse > m_RoutingSession->GetLastActivityTimestamp ()*1000 + DATAGRAM_SESSION_PATH_TIMEOUT) + m_LastUse > m_RoutingSession->GetLastActivityTimestamp ()*1000 + DATAGRAM_SESSION_PATH_TIMEOUT) { m_RoutingSession->SetSharedRoutingPath (nullptr); path = nullptr; diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index f55454b7..719f5830 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -350,8 +350,8 @@ namespace client { s->AddECIESx25519Key (data.k, data.t); }); - } - + } + void LeaseSetDestination::ProcessGarlicMessage (std::shared_ptr msg) { m_Service.post (std::bind (&LeaseSetDestination::HandleGarlicMessage, shared_from_this (), msg)); @@ -417,7 +417,7 @@ namespace client std::lock_guard lock(m_RemoteLeaseSetsMutex); auto it = m_RemoteLeaseSets.find (key); if (it != m_RemoteLeaseSets.end () && - it->second->GetStoreType () == buf[DATABASE_STORE_TYPE_OFFSET]) // update only if same type + it->second->GetStoreType () == buf[DATABASE_STORE_TYPE_OFFSET]) // update only if same type { leaseSet = it->second; if (leaseSet->IsNewer (buf + offset, len - offset)) @@ -587,7 +587,7 @@ namespace client LogPrint (eLogError, "Destination: Can't publish LeaseSet, no more floodfills found"); m_ExcludedFloodfills.clear (); return; - } + } auto outbound = m_Pool->GetNextOutboundTunnel (nullptr, floodfill->GetCompatibleTransports (false)); auto inbound = m_Pool->GetNextInboundTunnel (nullptr, floodfill->GetCompatibleTransports (true)); if (!outbound || !inbound) @@ -599,22 +599,22 @@ namespace client { outbound = m_Pool->GetNextOutboundTunnel (nullptr, floodfill->GetCompatibleTransports (false)); if (outbound) - { + { inbound = m_Pool->GetNextInboundTunnel (nullptr, floodfill->GetCompatibleTransports (true)); if (!inbound) LogPrint (eLogError, "Destination: Can't publish LeaseSet. No inbound tunnels"); - } + } else LogPrint (eLogError, "Destination: Can't publish LeaseSet. No outbound tunnels"); - } + } else LogPrint (eLogError, "Destination: Can't publish LeaseSet, no more floodfills found"); if (!floodfill || !outbound || !inbound) { m_ExcludedFloodfills.clear (); return; - } - } + } + } m_ExcludedFloodfills.insert (floodfill->GetIdentHash ()); LogPrint (eLogDebug, "Destination: Publish LeaseSet of ", GetIdentHash ().ToBase32 ()); RAND_bytes ((uint8_t *)&m_PublishReplyToken, 4); diff --git a/libi2pd/ECIESX25519AEADRatchetSession.h b/libi2pd/ECIESX25519AEADRatchetSession.h index 0c18814f..301f597a 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.h +++ b/libi2pd/ECIESX25519AEADRatchetSession.h @@ -212,7 +212,7 @@ namespace garlic uint8_t m_NSREncodedKey[32], m_NSRH[32], m_NSRKey[32]; // new session reply, for incoming only std::shared_ptr m_EphemeralKeys; SessionState m_State = eSessionStateNew; - uint64_t m_SessionCreatedTimestamp = 0, m_LastActivityTimestamp = 0, // incoming (in seconds) + uint64_t m_SessionCreatedTimestamp = 0, m_LastActivityTimestamp = 0, // incoming (in seconds) m_LastSentTimestamp = 0; // in milliseconds std::shared_ptr m_SendTagset, m_NSRSendTagset; std::unique_ptr m_Destination;// TODO: might not need it @@ -229,7 +229,7 @@ namespace garlic { return m_Destination ? *m_Destination : i2p::data::IdentHash (); } - }; + }; // single session for all incoming messages class RouterIncomingRatchetSession: public ECIESX25519AEADRatchetSession diff --git a/libi2pd/Ed25519.cpp b/libi2pd/Ed25519.cpp index 791bd685..0c6eb4f7 100644 --- a/libi2pd/Ed25519.cpp +++ b/libi2pd/Ed25519.cpp @@ -33,7 +33,7 @@ namespace crypto BN_add (l, l, tmp); BN_sub_word (two_252_2, 2); // 2^252 - 2 - // -121665*inv(121666) + // -121665*inv(121666) d = BN_new (); BN_set_word (tmp, 121666); BN_mod_inverse (tmp, tmp, q, ctx); @@ -61,7 +61,7 @@ namespace crypto BN_mod (By, By, q, ctx); // % q // precalculate Bi256 table - Bi256Carry = { Bx, By }; // B + Bi256Carry = { Bx, By }; // B for (int i = 0; i < 32; i++) { Bi256[i][0] = Bi256Carry; // first point @@ -215,7 +215,7 @@ namespace crypto if (!t1) { t1 = BN_CTX_get (ctx); BN_mul (t1, p1.x, p1.y, ctx); } if (!t2) { t2 = BN_CTX_get (ctx); BN_mul (t2, p2.x, p2.y, ctx); } BN_mul (t3, t1, t2, ctx); - BN_mul (t3, t3, d, ctx); // C = d*t1*t2 + BN_mul (t3, t3, d, ctx); // C = d*t1*t2 if (p1.z) { @@ -264,9 +264,9 @@ namespace crypto else { BN_mul (t2, p.x, p.y, ctx); // t = x*y - BN_sqr (t2, t2, ctx); // t2 = t^2 + BN_sqr (t2, t2, ctx); // t2 = t^2 } - BN_mul (t2, t2, d, ctx); // t2 = C = d*t^2 + BN_mul (t2, t2, d, ctx); // t2 = C = d*t^2 if (p.z) BN_sqr (z2, p.z, ctx); // z2 = D = z^2 else @@ -349,7 +349,7 @@ namespace crypto BN_mod_inverse (y, p.z, q, ctx); BN_mod_mul (x, p.x, y, q, ctx); // x = x/z BN_mod_mul (y, p.y, y, q, ctx); // y = y/z - return EDDSAPoint{x, y}; + return EDDSAPoint{x, y}; } else return EDDSAPoint{BN_dup (p.x), BN_dup (p.y)}; @@ -506,13 +506,13 @@ namespace crypto std::swap (z2, z3); } BN_mod_inverse (z2, z2, q, ctx); - BIGNUM * res = BN_new (); // not from ctx + BIGNUM * res = BN_new (); // not from ctx BN_mod_mul(res, x2, z2, q, ctx); BN_CTX_end (ctx); return res; } - void Ed25519::ScalarMul (const uint8_t * p, const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const + void Ed25519::ScalarMul (const uint8_t * p, const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const { BIGNUM * p1 = DecodeBN<32> (p); uint8_t k[32]; @@ -524,7 +524,7 @@ namespace crypto BN_free (p1); BN_free (n); BN_free (q1); } - void Ed25519::ScalarMulB (const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const + void Ed25519::ScalarMulB (const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const { BIGNUM *p1 = BN_new (); BN_set_word (p1, 9); uint8_t k[32]; diff --git a/libi2pd/Ed25519.h b/libi2pd/Ed25519.h index 28d4e930..470d802f 100644 --- a/libi2pd/Ed25519.h +++ b/libi2pd/Ed25519.h @@ -85,8 +85,8 @@ namespace crypto EDDSAPoint DecodePublicKey (const uint8_t * buf, BN_CTX * ctx) const; void EncodePublicKey (const EDDSAPoint& publicKey, uint8_t * buf, BN_CTX * ctx) const; #if !OPENSSL_X25519 - void ScalarMul (const uint8_t * p, const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const; // p is point, e is number for x25519 - void ScalarMulB (const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const; + void ScalarMul (const uint8_t * p, const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const; // p is point, e is number for x25519 + void ScalarMulB (const uint8_t * e, uint8_t * buf, BN_CTX * ctx) const; #endif void BlindPublicKey (const uint8_t * pub, const uint8_t * seed, uint8_t * blinded); // for encrypted LeaseSet2, pub - 32, seed - 64, blinded - 32 void BlindPrivateKey (const uint8_t * priv, const uint8_t * seed, uint8_t * blindedPriv, uint8_t * blindedPub); // for encrypted LeaseSet2, pub - 32, seed - 64, blinded - 32 diff --git a/libi2pd/Elligator.cpp b/libi2pd/Elligator.cpp index 712e514a..25e09893 100644 --- a/libi2pd/Elligator.cpp +++ b/libi2pd/Elligator.cpp @@ -189,7 +189,7 @@ namespace crypto // assume a < p, so don't check for a % p = 0, but a = 0 only if (BN_is_zero(a)) return 0; BIGNUM * r = BN_CTX_get (ctx); - BN_mod_exp (r, a, p12, p, ctx); // r = a^((p-1)/2) mod p + BN_mod_exp (r, a, p12, p, ctx); // r = a^((p-1)/2) mod p if (BN_is_word(r, 1)) return 1; else if (BN_is_zero(r)) diff --git a/libi2pd/FS.h b/libi2pd/FS.h index d51aa955..7911c6a0 100644 --- a/libi2pd/FS.h +++ b/libi2pd/FS.h @@ -83,8 +83,8 @@ namespace fs { /** * @brief Set datadir either from cmdline option or using autodetection - * @param cmdline_param Value of cmdline parameter --datadir= - * @param isService Value of cmdline parameter --service + * @param cmdline_param Value of cmdline parameter --datadir= + * @param isService Value of cmdline parameter --service * * Examples of autodetected paths: * @@ -93,11 +93,11 @@ namespace fs { * Mac: /Library/Application Support/i2pd/ or ~/Library/Application Support/i2pd/ * Unix: /var/lib/i2pd/ (system=1) >> ~/.i2pd/ or /tmp/i2pd/ */ - void DetectDataDir(const std::string & cmdline_datadir, bool isService = false); + void DetectDataDir(const std::string & cmdline_datadir, bool isService = false); /** * @brief Set certsdir either from cmdline option or using autodetection - * @param cmdline_param Value of cmdline parameter --certsdir= + * @param cmdline_param Value of cmdline parameter --certsdir= * * Examples of autodetected paths: * @@ -106,7 +106,7 @@ namespace fs { * Mac: /Library/Application Support/i2pd/ or ~/Library/Application Support/i2pd/certificates * Unix: /var/lib/i2pd/certificates (system=1) >> ~/.i2pd/ or /tmp/i2pd/certificates */ - void SetCertsDir(const std::string & cmdline_certsdir); + void SetCertsDir(const std::string & cmdline_certsdir); /** * @brief Create subdirectories inside datadir @@ -115,7 +115,7 @@ namespace fs { /** * @brief Get list of files in directory - * @param path Path to directory + * @param path Path to directory * @param files Vector to store found files * @return true on success and false if directory not exists */ diff --git a/libi2pd/Family.cpp b/libi2pd/Family.cpp index 09dc30dd..9a0700d0 100644 --- a/libi2pd/Family.cpp +++ b/libi2pd/Family.cpp @@ -148,8 +148,8 @@ namespace data if (it != m_SigningKeys.end ()) return it->second.second; return 0; - } - + } + std::string CreateFamilySignature (const std::string& family, const IdentHash& ident) { auto filename = i2p::fs::DataDirPath("family", (family + ".key")); diff --git a/libi2pd/Garlic.cpp b/libi2pd/Garlic.cpp index ef92c6f5..9daea1f0 100644 --- a/libi2pd/Garlic.cpp +++ b/libi2pd/Garlic.cpp @@ -293,14 +293,14 @@ namespace garlic size_t size = 0; if (isDestination) { - buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination + buf[size] = eGarlicDeliveryTypeDestination << 5;// delivery instructions flag destination size++; memcpy (buf + size, m_Destination->GetIdentHash (), 32); size += 32; } else { - buf[size] = 0;// delivery instructions flag local + buf[size] = 0;// delivery instructions flag local size++; } @@ -487,8 +487,8 @@ namespace garlic void GarlicDestination::SubmitECIESx25519Key (const uint8_t * key, uint64_t tag) { AddECIESx25519Key (key, tag); - } - + } + void GarlicDestination::HandleGarlicMessage (std::shared_ptr msg) { uint8_t * buf = msg->GetPayload (); @@ -744,7 +744,7 @@ namespace garlic LogPrint (eLogError, "Garlic: Message is too short"); break; } - buf += GetI2NPMessageLength (buf, len - offset); // I2NP + buf += GetI2NPMessageLength (buf, len - offset); // I2NP buf += 4; // CloveID buf += 8; // Date buf += 3; // Certificate @@ -1024,7 +1024,7 @@ namespace garlic uint32_t ts = i2p::util::GetSecondsSinceEpoch (); for (auto it: files) if (ts >= i2p::fs::GetLastUpdateTime (it) + INCOMING_TAGS_EXPIRATION_TIMEOUT) - i2p::fs::Remove (it); + i2p::fs::Remove (it); } void GarlicDestination::HandleECIESx25519GarlicClove (const uint8_t * buf, size_t len) diff --git a/libi2pd/Gost.cpp b/libi2pd/Gost.cpp index 5e84a95d..2dafc9ae 100644 --- a/libi2pd/Gost.cpp +++ b/libi2pd/Gost.cpp @@ -96,7 +96,7 @@ namespace crypto EC_POINT * C = EC_POINT_new (m_Group); EC_POINT_mul (m_Group, C, z1, pub, z2, ctx); // z1*P + z2*pub BIGNUM * x = BN_CTX_get (ctx); - GetXY (C, x, nullptr); // Cx + GetXY (C, x, nullptr); // Cx BN_mod (x, x, q, ctx); // Cx % q bool ret = !BN_cmp (x, r); // Cx = r ? EC_POINT_free (C); @@ -111,8 +111,8 @@ namespace crypto BN_CTX * ctx = BN_CTX_new (); BN_CTX_start (ctx); EC_POINT * C = EC_POINT_new (m_Group); // C = k*P = (rx, ry) - EC_POINT * Q = nullptr; - if (EC_POINT_set_compressed_coordinates_GFp (m_Group, C, r, isNegativeY ? 1 : 0, ctx)) + EC_POINT * Q = nullptr; + if (EC_POINT_set_compressed_coordinates_GFp (m_Group, C, r, isNegativeY ? 1 : 0, ctx)) { EC_POINT * S = EC_POINT_new (m_Group); // S = s*P EC_POINT_mul (m_Group, S, s, nullptr, nullptr, ctx); diff --git a/libi2pd/HTTP.cpp b/libi2pd/HTTP.cpp index c5792a37..e994b9b3 100644 --- a/libi2pd/HTTP.cpp +++ b/libi2pd/HTTP.cpp @@ -279,7 +279,7 @@ namespace http method = tokens[0]; uri = tokens[1]; version = tokens[2]; - expect = HEADER_LINE; + expect = HEADER_LINE; } else { @@ -363,7 +363,7 @@ namespace http return false; /* no header */ if (it->second.find("gzip") != std::string::npos) return true; /* gotcha! */ - if (includingI2PGzip && it->second.find("x-i2p-gzip") != std::string::npos) + if (includingI2PGzip && it->second.find("x-i2p-gzip") != std::string::npos) return true; return false; } @@ -409,7 +409,7 @@ namespace http /* all ok */ version = tokens[0]; status = tokens[2]; - expect = HEADER_LINE; + expect = HEADER_LINE; } else { std::string line = str.substr(pos, eol - pos); auto p = parse_header_line(line); @@ -460,7 +460,7 @@ namespace http case 304: ptr = "Not Modified"; break; case 307: ptr = "Temporary Redirect"; break; /* client error */ - case 400: ptr = "Bad Request"; break; + case 400: ptr = "Bad Request"; break; case 401: ptr = "Unauthorized"; break; case 403: ptr = "Forbidden"; break; case 404: ptr = "Not Found"; break; @@ -471,7 +471,7 @@ namespace http case 502: ptr = "Bad Gateway"; break; case 503: ptr = "Not Implemented"; break; case 504: ptr = "Gateway Timeout"; break; - default: ptr = "Unknown Status"; break; + default: ptr = "Unknown Status"; break; } return ptr; } diff --git a/libi2pd/HTTP.h b/libi2pd/HTTP.h index 6a322c18..9445a01a 100644 --- a/libi2pd/HTTP.h +++ b/libi2pd/HTTP.h @@ -161,7 +161,7 @@ namespace http /** * @brief Merge HTTP response content with Transfer-Encoding: chunked - * @param in Input stream + * @param in Input stream * @param out Output stream * @return true on success, false otherwise */ diff --git a/libi2pd/I2NPProtocol.cpp b/libi2pd/I2NPProtocol.cpp index 229bfbf3..fec96939 100644 --- a/libi2pd/I2NPProtocol.cpp +++ b/libi2pd/I2NPProtocol.cpp @@ -171,7 +171,7 @@ namespace i2p std::shared_ptr CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest, const std::set& excludedFloodfills, std::shared_ptr replyTunnel, const uint8_t * replyKey, - const uint8_t * replyTag, bool replyECIES) + const uint8_t * replyTag, bool replyECIES) { int cnt = excludedFloodfills.size (); auto m = cnt > 7 ? NewI2NPMessage () : NewI2NPShortMessage (); @@ -244,7 +244,7 @@ namespace i2p } std::shared_ptr CreateDatabaseStoreMsg (std::shared_ptr router, - uint32_t replyToken, std::shared_ptr replyTunnel) + uint32_t replyToken, std::shared_ptr replyTunnel) { if (!router) // we send own RouterInfo router = context.GetSharedRouterInfo (); @@ -629,7 +629,7 @@ namespace i2p // we send it to reply tunnel transports.SendMessage (clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET, CreateTunnelGatewayMsg (bufbe32toh (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET), - i2p::garlic::WrapECIESX25519Message (replyMsg, noiseState.m_CK + 32, tag))); + i2p::garlic::WrapECIESX25519Message (replyMsg, noiseState.m_CK + 32, tag))); } else { diff --git a/libi2pd/I2NPProtocol.h b/libi2pd/I2NPProtocol.h index f55975c4..e60f6a9c 100644 --- a/libi2pd/I2NPProtocol.h +++ b/libi2pd/I2NPProtocol.h @@ -150,7 +150,7 @@ namespace tunnel std::shared_ptr from; I2NPMessage (): buf (nullptr),len (I2NP_HEADER_SIZE + 2), - offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header + offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header // header accessors uint8_t * GetHeader () { return GetBuffer (); }; @@ -274,8 +274,8 @@ namespace tunnel uint32_t replyTunnelID, bool exploratory = false, std::set * excludedPeers = nullptr); std::shared_ptr CreateLeaseSetDatabaseLookupMsg (const i2p::data::IdentHash& dest, const std::set& excludedFloodfills, - std::shared_ptr replyTunnel, - const uint8_t * replyKey, const uint8_t * replyTag, bool replyECIES = false); + std::shared_ptr replyTunnel, + const uint8_t * replyKey, const uint8_t * replyTag, bool replyECIES = false); std::shared_ptr CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, std::vector routers); std::shared_ptr CreateDatabaseStoreMsg (std::shared_ptr router = nullptr, uint32_t replyToken = 0, std::shared_ptr replyTunnel = nullptr); diff --git a/libi2pd/Identity.cpp b/libi2pd/Identity.cpp index 8aacf4de..cff0c37d 100644 --- a/libi2pd/Identity.cpp +++ b/libi2pd/Identity.cpp @@ -64,7 +64,7 @@ namespace data { case SIGNING_KEY_TYPE_ECDSA_SHA256_P256: { - size_t padding = 128 - i2p::crypto::ECDSAP256_KEY_LENGTH; // 64 = 128 - 64 + size_t padding = 128 - i2p::crypto::ECDSAP256_KEY_LENGTH; // 64 = 128 - 64 RAND_bytes (m_StandardIdentity.signingKey, padding); memcpy (m_StandardIdentity.signingKey + padding, signingKey, i2p::crypto::ECDSAP256_KEY_LENGTH); break; @@ -788,7 +788,7 @@ namespace data keys.m_OfflineSignature.resize (pubKeyLen + m_Public->GetSignatureLen () + 6); htobe32buf (keys.m_OfflineSignature.data (), expires); // expires htobe16buf (keys.m_OfflineSignature.data () + 4, type); // type - GenerateSigningKeyPair (type, keys.m_SigningPrivateKey, keys.m_OfflineSignature.data () + 6); // public key + GenerateSigningKeyPair (type, keys.m_SigningPrivateKey, keys.m_OfflineSignature.data () + 6); // public key Sign (keys.m_OfflineSignature.data (), pubKeyLen + 6, keys.m_OfflineSignature.data () + 6 + pubKeyLen); // signature // recreate signer keys.m_Signer = nullptr; diff --git a/libi2pd/Identity.h b/libi2pd/Identity.h index 8f35845d..10f1d5ed 100644 --- a/libi2pd/Identity.h +++ b/libi2pd/Identity.h @@ -120,7 +120,7 @@ namespace data CryptoKeyType GetCryptoKeyType () const; void DropVerifier () const; // to save memory - bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); } + bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); } void RecalculateIdentHash(uint8_t * buff=nullptr); static i2p::crypto::Verifier * CreateVerifier (SigningKeyType keyType); @@ -222,7 +222,7 @@ namespace data RoutingDestination () {}; virtual ~RoutingDestination () {}; - virtual std::shared_ptr GetIdentity () const = 0; + virtual std::shared_ptr GetIdentity () const = 0; virtual void Encrypt (const uint8_t * data, uint8_t * encrypted) const = 0; // encrypt data for virtual bool IsDestination () const = 0; // for garlic diff --git a/libi2pd/LeaseSet.cpp b/libi2pd/LeaseSet.cpp index 0af06377..c844ab60 100644 --- a/libi2pd/LeaseSet.cpp +++ b/libi2pd/LeaseSet.cpp @@ -582,7 +582,7 @@ namespace data // helper for ExtractClientAuthData static inline bool GetAuthCookie (const uint8_t * authClients, int numClients, const uint8_t * okm, uint8_t * authCookie) { - // try to find clientCookie_i for clientID_i = okm[44:51] + // try to find clientCookie_i for clientID_i = okm[44:51] for (int i = 0; i < numClients; i++) { if (!memcmp (okm + 44, authClients + i*40, 8)) // clientID_i @@ -606,7 +606,7 @@ namespace data { const uint8_t * ephemeralPublicKey = buf + offset; offset += 32; // ephemeralPublicKey uint16_t numClients = bufbe16toh (buf + offset); offset += 2; // clients - const uint8_t * authClients = buf + offset; offset += numClients*40; // authClients + const uint8_t * authClients = buf + offset; offset += numClients*40; // authClients if (offset > len) { LogPrint (eLogError, "LeaseSet2: Too many clients ", numClients, " in DH auth data"); @@ -632,7 +632,7 @@ namespace data { const uint8_t * authSalt = buf + offset; offset += 32; // authSalt uint16_t numClients = bufbe16toh (buf + offset); offset += 2; // clients - const uint8_t * authClients = buf + offset; offset += numClients*40; // authClients + const uint8_t * authClients = buf + offset; offset += numClients*40; // authClients if (offset > len) { LogPrint (eLogError, "LeaseSet2: Too many clients ", numClients, " in PSK auth data"); @@ -737,7 +737,7 @@ namespace data htobe64buf (m_Buffer + offset, ts); offset += 8; // end date } - // we don't sign it yet. must be signed later on + // we don't sign it yet. must be signed later on } LocalLeaseSet::LocalLeaseSet (std::shared_ptr identity, const uint8_t * buf, size_t len): @@ -995,7 +995,7 @@ namespace data ek.GenerateKeys (); // esk and epk memcpy (authData, ek.GetPublicKey (), 32); authData += 32; // epk htobe16buf (authData, authKeys->size ()); authData += 2; // num clients - uint8_t authInput[100]; // sharedSecret || cpk_i || subcredential || publishedTimestamp + uint8_t authInput[100]; // sharedSecret || cpk_i || subcredential || publishedTimestamp memcpy (authInput + 64, subcredential, 36); for (auto& it: *authKeys) { diff --git a/libi2pd/LeaseSet.h b/libi2pd/LeaseSet.h index 8d501cb1..a79a5870 100644 --- a/libi2pd/LeaseSet.h +++ b/libi2pd/LeaseSet.h @@ -128,8 +128,8 @@ namespace data }; /** - validate lease set buffer signature and extract expiration timestamp - @returns true if the leaseset is well formed and signature is valid + * validate lease set buffer signature and extract expiration timestamp + * @returns true if the leaseset is well formed and signature is valid */ bool LeaseSetBufferValidate(const uint8_t * ptr, size_t sz, uint64_t & expires); diff --git a/libi2pd/Log.cpp b/libi2pd/Log.cpp index 25b580b1..e90b5e2b 100644 --- a/libi2pd/Log.cpp +++ b/libi2pd/Log.cpp @@ -46,7 +46,7 @@ namespace log { #ifndef _WIN32 /** - * @brief Maps our log levels to syslog one + * @brief Maps our log levels to syslog one * @return syslog priority LOG_*, as defined in syslog.h */ static inline int GetSyslogPrio (enum LogLevel l) { @@ -113,11 +113,11 @@ namespace log { std::string str_tolower(std::string s) { std::transform(s.begin(), s.end(), s.begin(), - // static_cast(std::tolower) // wrong - // [](int c){ return std::tolower(c); } // wrong - // [](char c){ return std::tolower(c); } // wrong - [](unsigned char c){ return std::tolower(c); } // correct - ); + // static_cast(std::tolower) // wrong + // [](int c){ return std::tolower(c); } // wrong + // [](char c){ return std::tolower(c); } // wrong + [](unsigned char c){ return std::tolower(c); } // correct + ); return s; } @@ -170,7 +170,7 @@ namespace log { break; case eLogStdout: default: - std::cout << TimeAsString(msg->timestamp) + std::cout << TimeAsString(msg->timestamp) << "@" << short_tid << "/" << LogMsgColors[msg->level] << g_LogLevelStr[msg->level] << LogMsgColors[eNumLogLevels] << " - " << msg->text << std::endl; diff --git a/libi2pd/Log.h b/libi2pd/Log.h index 08c401a9..465e10bc 100644 --- a/libi2pd/Log.h +++ b/libi2pd/Log.h @@ -52,7 +52,7 @@ namespace log { { private: - enum LogType m_Destination; + enum LogType m_Destination; enum LogLevel m_MinLevel; std::shared_ptr m_LogStream; std::string m_Logfile; @@ -75,7 +75,7 @@ namespace log { /** * @brief Makes formatted string from unix timestamp - * @param ts Second since epoch + * @param ts Second since epoch * * This function internally caches the result for last provided value */ @@ -86,52 +86,52 @@ namespace log { Log (); ~Log (); - LogType GetLogType () { return m_Destination; }; + LogType GetLogType () { return m_Destination; }; LogLevel GetLogLevel () { return m_MinLevel; }; void Start (); void Stop (); /** - * @brief Sets minimal allowed level for log messages - * @param level String with wanted minimal msg level + * @brief Sets minimal allowed level for log messages + * @param level String with wanted minimal msg level */ - void SetLogLevel (const std::string& level); + void SetLogLevel (const std::string& level); /** * @brief Sets log destination to logfile - * @param path Path to logfile + * @param path Path to logfile */ void SendTo (const std::string &path); /** * @brief Sets log destination to given output stream - * @param os Output stream + * @param os Output stream */ void SendTo (std::shared_ptr os); /** - * @brief Sets format for timestamps in log - * @param format String with timestamp format + * @brief Sets format for timestamps in log + * @param format String with timestamp format */ void SetTimeFormat (std::string format) { m_TimeFormat = format; }; #ifndef _WIN32 /** * @brief Sets log destination to syslog - * @param name Wanted program name + * @param name Wanted program name * @param facility Wanted log category */ void SendTo (const char *name, int facility); #endif /** - * @brief Format log message and write to output stream/syslog - * @param msg Pointer to processed message + * @brief Format log message and write to output stream/syslog + * @param msg Pointer to processed message */ void Append(std::shared_ptr &); - /** @brief Reopen log file */ + /** @brief Reopen log file */ void Reopen(); }; @@ -144,8 +144,8 @@ namespace log { */ struct LogMsg { std::time_t timestamp; - std::string text; /**< message text as single string */ - LogLevel level; /**< message level */ + std::string text; /**< message text as single string */ + LogLevel level; /**< message level */ std::thread::id tid; /**< id of thread that generated message */ LogMsg (LogLevel lvl, std::time_t ts, std::string&& txt): timestamp(ts), text(std::move(txt)), level(lvl) {} @@ -153,7 +153,7 @@ namespace log { Log & Logger(); - typedef std::function ThrowFunction; + typedef std::function ThrowFunction; ThrowFunction GetThrowFunction (); void SetThrowFunction (ThrowFunction f); } // log diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 98357480..0b20c964 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -66,7 +66,7 @@ namespace transport { MixHash (sessionRequest + 32, 32); // encrypted payload - int paddingLength = sessionRequestLen - 64; + int paddingLength = sessionRequestLen - 64; if (paddingLength > 0) MixHash (sessionRequest + 64, paddingLength); MixHash (epub, 32); @@ -130,7 +130,7 @@ namespace transport // m3p2Len auto bufLen = i2p::context.GetRouterInfo ().GetBufferLen (); m3p2Len = bufLen + 4 + 16; // (RI header + RI + MAC for now) TODO: implement options - htobe16buf (options + 4, m3p2Len); + htobe16buf (options + 4, m3p2Len); // fill m3p2 payload (RouterInfo block) m_SessionConfirmedBuffer = new uint8_t[m3p2Len + 48]; // m3p1 is 48 bytes uint8_t * m3p2 = m_SessionConfirmedBuffer + 48; @@ -231,11 +231,11 @@ namespace transport auto ts = i2p::util::GetSecondsSinceEpoch (); uint32_t tsA = bufbe32toh (options + 8); if (tsA < ts - NTCP2_CLOCK_SKEW || tsA > ts + NTCP2_CLOCK_SKEW) - { + { LogPrint (eLogWarning, "NTCP2: SessionRequest time difference ", (int)(ts - tsA), " exceeds clock skew"); clockSkew = true; // we send SessionCreate to let Alice know our time and then close session - } + } } else { @@ -320,7 +320,7 @@ namespace transport } NTCP2Session::NTCP2Session (NTCP2Server& server, std::shared_ptr in_RemoteRouter, - std::shared_ptr addr): + std::shared_ptr addr): TransportSession (in_RemoteRouter, NTCP2_ESTABLISH_TIMEOUT), m_Server (server), m_Socket (m_Server.GetService ()), m_IsEstablished (false), m_IsTerminated (false), @@ -418,7 +418,7 @@ namespace transport void NTCP2Session::DeleteNextReceiveBuffer (uint64_t ts) { if (m_NextReceivedBuffer && !m_IsReceiving && - ts > m_LastActivityTimestamp + NTCP2_RECEIVE_BUFFER_DELETION_TIMEOUT) + ts > m_LastActivityTimestamp + NTCP2_RECEIVE_BUFFER_DELETION_TIMEOUT) { delete[] m_NextReceivedBuffer; m_NextReceivedBuffer = nullptr; @@ -484,9 +484,9 @@ namespace transport if (clockSkew) { // we don't care about padding, send SessionCreated and close session - SendSessionCreated (); + SendSessionCreated (); m_Server.GetService ().post (std::bind (&NTCP2Session::Terminate, shared_from_this ())); - } + } else if (paddingLen > 0) { if (paddingLen <= NTCP2_SESSION_REQUEST_MAX_SIZE - 64) // session request is 287 bytes max @@ -496,7 +496,7 @@ namespace transport } else { - LogPrint (eLogWarning, "NTCP2: SessionRequest padding length ", (int)paddingLen, " is too long"); + LogPrint (eLogWarning, "NTCP2: SessionRequest padding length ", (int)paddingLen, " is too long"); Terminate (); } } @@ -549,7 +549,7 @@ namespace transport } else { - LogPrint (eLogWarning, "NTCP2: SessionCreated padding length ", (int)paddingLen, " is too long"); + LogPrint (eLogWarning, "NTCP2: SessionCreated padding length ", (int)paddingLen, " is too long"); Terminate (); } } @@ -1126,11 +1126,11 @@ namespace transport { if (!m_SendKey || #if OPENSSL_SIPHASH - !m_SendMDCtx + !m_SendMDCtx #else - !m_SendSipKey + !m_SendSipKey #endif - ) return; + ) return; m_NextSendBuffer = new uint8_t[49]; // 49 = 12 bytes message + 16 bytes MAC + 2 bytes size + up to 19 padding block // termination block m_NextSendBuffer[2] = eNTCP2BlkTermination; @@ -1164,7 +1164,7 @@ namespace transport else if (m_SendQueue.size () > NTCP2_MAX_OUTGOING_QUEUE_SIZE) { LogPrint (eLogWarning, "NTCP2: Outgoing messages queue size to ", - GetIdentHashBase64(), " exceeds ", NTCP2_MAX_OUTGOING_QUEUE_SIZE); + GetIdentHashBase64(), " exceeds ", NTCP2_MAX_OUTGOING_QUEUE_SIZE); Terminate (); } } @@ -1177,7 +1177,7 @@ namespace transport NTCP2Server::NTCP2Server (): RunnableServiceWithWork ("NTCP2"), m_TerminationTimer (GetService ()), - m_ProxyType(eNoProxy), m_Resolver(GetService ()) + m_ProxyType(eNoProxy), m_Resolver(GetService ()) { } diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index f327e0a1..ef36895a 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -489,7 +489,7 @@ namespace data { auto r = std::make_shared(path); if (r->GetRouterIdentity () && !r->IsUnreachable () && r->HasValidAddresses () && - ts < r->GetTimestamp () + 24*60*60*NETDB_MAX_OFFLINE_EXPIRATION_TIMEOUT*1000LL) + ts < r->GetTimestamp () + 24*60*60*NETDB_MAX_OFFLINE_EXPIRATION_TIMEOUT*1000LL) { r->DeleteBuffer (); if (m_RouterInfos.emplace (r->GetIdentHash (), r).second) @@ -620,7 +620,7 @@ namespace data } // make router reachable back if too few routers or floodfills if (it.second->IsUnreachable () && (total - deletedCount < NETDB_MIN_ROUTERS || - (it.second->IsFloodfill () && totalFloodfills - deletedFloodfillsCount < NETDB_MIN_FLOODFILLS))) + (it.second->IsFloodfill () && totalFloodfills - deletedFloodfillsCount < NETDB_MIN_FLOODFILLS))) it.second->SetUnreachable (false); // find & mark expired routers if (!it.second->IsReachable () && it.second->IsSSU (false)) @@ -643,7 +643,7 @@ namespace data } // m_RouterInfos iteration m_RouterInfoBuffersPool.CleanUpMt (); - + if (updatedCount > 0) LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers"); if (deletedCount > 0) @@ -672,7 +672,7 @@ namespace data else ++it; } - } + } } void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct) @@ -688,7 +688,7 @@ namespace data if (floodfill) { if (direct && !floodfill->IsReachableFrom (i2p::context.GetRouterInfo ()) && - !i2p::transport::transports.IsConnected (floodfill->GetIdentHash ())) + !i2p::transport::transports.IsConnected (floodfill->GetIdentHash ())) direct = false; // floodfill can't be reached directly if (direct) transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ())); @@ -971,7 +971,7 @@ namespace data else { if (lookupType == DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP || - lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP) + lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP) { auto router = FindRouter (ident); if (router) @@ -1136,7 +1136,7 @@ namespace data m_PublishExcluded.insert (floodfill->GetIdentHash ()); m_PublishReplyToken = replyToken; if (floodfill->IsReachableFrom (i2p::context.GetRouterInfo ()) || // are we able to connect? - i2p::transport::transports.IsConnected (floodfill->GetIdentHash ())) // already connected ? + i2p::transport::transports.IsConnected (floodfill->GetIdentHash ())) // already connected ? // send directly transports.SendMessage (floodfill->GetIdentHash (), CreateDatabaseStoreMsg (i2p::context.GetSharedRouterInfo (), replyToken)); else @@ -1377,7 +1377,7 @@ namespace data return res; } - std::shared_ptr NetDb::GetRandomRouterInFamily (FamilyID fam) const + std::shared_ptr NetDb::GetRandomRouterInFamily (FamilyID fam) const { return GetRandomRouter( [fam](std::shared_ptr router)->bool diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index b244fa83..ad88274a 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -123,7 +123,7 @@ namespace data void ClearRouterInfos () { m_RouterInfos.clear (); }; std::shared_ptr NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); }; - + uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; }; private: diff --git a/libi2pd/NetDbRequests.h b/libi2pd/NetDbRequests.h index 16ea430d..cf2f0915 100644 --- a/libi2pd/NetDbRequests.h +++ b/libi2pd/NetDbRequests.h @@ -60,7 +60,7 @@ namespace data void Start (); void Stop (); - std::shared_ptr CreateRequest (const IdentHash& destination, bool isExploratory, RequestedDestination::RequestComplete requestComplete = nullptr); + std::shared_ptr CreateRequest (const IdentHash& destination, bool isExploratory, RequestedDestination::RequestComplete requestComplete = nullptr); void RequestComplete (const IdentHash& ident, std::shared_ptr r); std::shared_ptr FindRequest (const IdentHash& ident) const; void ManageRequests (); diff --git a/libi2pd/Poly1305.cpp b/libi2pd/Poly1305.cpp index 23098d74..20b3ab2a 100644 --- a/libi2pd/Poly1305.cpp +++ b/libi2pd/Poly1305.cpp @@ -1,12 +1,13 @@ -#include "Poly1305.h" /** - This code is licensed under the MCGSI Public License - Copyright 2018 Jeff Becker - - Kovri go write your own code - + * This code is licensed under the MCGSI Public License + * Copyright 2018 Jeff Becker + * + *Kovri go write your own code + * */ +#include "Poly1305.h" + #if !OPENSSL_AEAD_CHACHA20_POLY1305 namespace i2p { diff --git a/libi2pd/Poly1305.h b/libi2pd/Poly1305.h index f91a037e..db659b84 100644 --- a/libi2pd/Poly1305.h +++ b/libi2pd/Poly1305.h @@ -5,6 +5,7 @@ * Kovri go write your own code * */ + #ifndef LIBI2PD_POLY1305_H #define LIBI2PD_POLY1305_H #include diff --git a/libi2pd/Queue.h b/libi2pd/Queue.h index d43567a5..441f8c3a 100644 --- a/libi2pd/Queue.h +++ b/libi2pd/Queue.h @@ -28,7 +28,7 @@ namespace util void Put (Element e) { - std::unique_lock l(m_QueueMutex); + std::unique_lock l(m_QueueMutex); m_Queue.push (std::move(e)); m_NonEmpty.notify_one (); } @@ -38,7 +38,7 @@ namespace util { if (!vec.empty ()) { - std::unique_lock l(m_QueueMutex); + std::unique_lock l(m_QueueMutex); for (const auto& it: vec) m_Queue.push (std::move(it)); m_NonEmpty.notify_one (); diff --git a/libi2pd/Reseed.cpp b/libi2pd/Reseed.cpp index 3fd3a27f..4c23b4cc 100644 --- a/libi2pd/Reseed.cpp +++ b/libi2pd/Reseed.cpp @@ -187,31 +187,31 @@ namespace data } s.seekg (1, std::ios::cur); // su3 file format version SigningKeyType signatureType; - s.read ((char *)&signatureType, 2); // signature type + s.read ((char *)&signatureType, 2); // signature type signatureType = be16toh (signatureType); uint16_t signatureLength; - s.read ((char *)&signatureLength, 2); // signature length + s.read ((char *)&signatureLength, 2); // signature length signatureLength = be16toh (signatureLength); s.seekg (1, std::ios::cur); // unused uint8_t versionLength; - s.read ((char *)&versionLength, 1); // version length + s.read ((char *)&versionLength, 1); // version length s.seekg (1, std::ios::cur); // unused uint8_t signerIDLength; - s.read ((char *)&signerIDLength, 1); // signer ID length + s.read ((char *)&signerIDLength, 1); // signer ID length uint64_t contentLength; - s.read ((char *)&contentLength, 8); // content length + s.read ((char *)&contentLength, 8); // content length contentLength = be64toh (contentLength); s.seekg (1, std::ios::cur); // unused uint8_t fileType; - s.read ((char *)&fileType, 1); // file type - if (fileType != 0x00) // zip file + s.read ((char *)&fileType, 1); // file type + if (fileType != 0x00) // zip file { LogPrint (eLogError, "Reseed: Can't handle file type ", (int)fileType); return 0; } s.seekg (1, std::ios::cur); // unused uint8_t contentType; - s.read ((char *)&contentType, 1); // content type + s.read ((char *)&contentType, 1); // content type if (contentType != 0x03) // reseed data { LogPrint (eLogError, "Reseed: Unexpected content type ", (int)contentType); @@ -688,7 +688,7 @@ namespace data { boost::asio::ip::tcp::endpoint ep = *it; if ((ep.address ().is_v4 () && i2p::context.SupportsV4 ()) || - (ep.address ().is_v6 () && i2p::context.SupportsV6 ())) + (ep.address ().is_v6 () && i2p::context.SupportsV6 ())) { s.lowest_layer().connect (ep, ecode); if (!ecode) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 14b1383c..9f8b6626 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -65,13 +65,13 @@ namespace i2p port = rand () % (30777 - 9111) + 9111; // I2P network ports range if (port == 9150) port = 9151; // Tor browser } - bool ipv4; i2p::config::GetOption("ipv4", ipv4); - bool ipv6; i2p::config::GetOption("ipv6", ipv6); - bool ssu; i2p::config::GetOption("ssu", ssu); - bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); - bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2); - bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg); - bool nat; i2p::config::GetOption("nat", nat); + bool ipv4; i2p::config::GetOption("ipv4", ipv4); + bool ipv6; i2p::config::GetOption("ipv6", ipv6); + bool ssu; i2p::config::GetOption("ssu", ssu); + bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2); + bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2); + bool ygg; i2p::config::GetOption("meshnets.yggdrasil", ygg); + bool nat; i2p::config::GetOption("nat", nat); if ((ntcp2 || ygg) && !m_NTCP2Keys) NewNTCP2Keys (); @@ -123,11 +123,11 @@ namespace i2p if (ssu2Published) routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address_v4::from_string (host), port); else - { + { addressCaps |= i2p::data::RouterInfo::AddressCaps::eV4; routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro); - } - } + } + } } if (ipv6) { @@ -168,12 +168,12 @@ namespace i2p if (ssu2Published) routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address_v6::from_string (host), port); else - { + { if (!ipv4) // no other ssu2 addresses yet routerInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro); addressCaps |= i2p::data::RouterInfo::AddressCaps::eV6; - } - } + } + } } if (ygg) { @@ -224,7 +224,7 @@ namespace i2p std::ofstream fk (i2p::fs::DataDirPath (SSU2_KEYS), std::ofstream::binary | std::ofstream::out); fk.write ((char *)m_SSU2Keys.get (), sizeof (SSU2PrivateKeys)); } - + void RouterContext::SetStatus (RouterStatus status) { if (status != m_Status) @@ -313,7 +313,7 @@ namespace i2p if (updated) UpdateRouterInfo (); } - + void RouterContext::UpdateNTCP2Address (bool enable) { auto& addresses = m_RouterInfo.GetAddresses (); @@ -360,8 +360,8 @@ namespace i2p } if (updated) UpdateRouterInfo (); - } - + } + void RouterContext::UpdateSSU2Address (bool enable) { auto& addresses = m_RouterInfo.GetAddresses (); @@ -392,14 +392,14 @@ namespace i2p if (updated) UpdateRouterInfo (); } - + void RouterContext::UpdateAddress (const boost::asio::ip::address& host) { bool updated = false; for (auto& address : m_RouterInfo.GetAddresses ()) { if (address->host != host && address->IsCompatible (host) && - !i2p::util::net::IsYggdrasilAddress (address->host)) + !i2p::util::net::IsYggdrasilAddress (address->host)) { address->host = host; if (host.is_v6 () && address->transportStyle == i2p::data::RouterInfo::eTransportSSU) @@ -643,12 +643,12 @@ namespace i2p break; case i2p::data::RouterInfo::eTransportNTCP: foundNTCP2 = true; - break; + break; case i2p::data::RouterInfo::eTransportSSU2: foundSSU2 = true; - break; + break; default: ; - } + } } port = addr->port; } @@ -690,17 +690,17 @@ namespace i2p { bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2); if (ssu2) - { + { bool ssu2Published; i2p::config::GetOption("ssu2.published", ssu2Published); if (ssu2Published) { uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port); m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address::from_string ("::1"), ssu2Port); - } + } else m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::eV6); - } - } + } + } m_RouterInfo.EnableV6 (); } else @@ -731,12 +731,12 @@ namespace i2p break; case i2p::data::RouterInfo::eTransportNTCP: foundNTCP2 = true; - break; + break; case i2p::data::RouterInfo::eTransportSSU2: foundSSU2 = true; - break; + break; default: ; - } + } } if (addr->port) port = addr->port; } @@ -776,11 +776,11 @@ namespace i2p { uint16_t ssu2Port; i2p::config::GetOption ("ssu2.port", ssu2Port); m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, boost::asio::ip::address::from_string ("127.0.0.1"), ssu2Port); - } + } else m_RouterInfo.AddSSU2Address (m_SSU2Keys->staticPublicKey, m_SSU2Keys->intro, i2p::data::RouterInfo::eV6); - } - } + } + } m_RouterInfo.EnableV4 (); } else @@ -883,7 +883,7 @@ namespace i2p } std::shared_ptr oldIdentity; if (m_Keys.GetPublic ()->GetSigningKeyType () == i2p::data::SIGNING_KEY_TYPE_DSA_SHA1 || - m_Keys.GetPublic ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ELGAMAL) + m_Keys.GetPublic ()->GetCryptoKeyType () == i2p::data::CRYPTO_KEY_TYPE_ELGAMAL) { // update keys LogPrint (eLogInfo, "Router: router keys are obsolete. Creating new"); @@ -935,7 +935,7 @@ namespace i2p UpdateNTCP2Address (true); // enable NTCP2 } else - UpdateNTCP2Address (false); // disable NTCP2 + UpdateNTCP2Address (false); // disable NTCP2 // read SSU2 bool ssu2; i2p::config::GetOption("ssu2.enabled", ssu2); @@ -960,7 +960,7 @@ namespace i2p } else UpdateSSU2Address (false); // disable SSU2 - + return true; } @@ -1103,5 +1103,5 @@ namespace i2p delete x; } return *m_SSU2StaticKeys; - } + } } diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index 905fb3b8..eb5db38f 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -68,7 +68,7 @@ namespace garlic uint8_t staticPrivateKey[32]; uint8_t intro[32]; }; - + public: RouterContext (); @@ -86,7 +86,7 @@ namespace garlic return std::shared_ptr (this, [](i2p::garlic::GarlicDestination *) {}); } - + const uint8_t * GetNTCP2StaticPublicKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPublicKey : nullptr; }; const uint8_t * GetNTCP2StaticPrivateKey () const { return m_NTCP2Keys ? m_NTCP2Keys->staticPrivateKey : nullptr; }; const uint8_t * GetNTCP2IV () const { return m_NTCP2Keys ? m_NTCP2Keys->iv : nullptr; }; @@ -96,7 +96,7 @@ namespace garlic const uint8_t * GetSSU2StaticPrivateKey () const { return m_SSU2Keys ? m_SSU2Keys->staticPrivateKey : nullptr; }; const uint8_t * GetSSU2IntroKey () const { return m_SSU2Keys ? m_SSU2Keys->intro : nullptr; }; i2p::crypto::X25519Keys& GetSSU2StaticKeys (); - + uint32_t GetUptime () const; // in seconds uint64_t GetLastUpdateTime () const { return m_LastUpdateTime; }; uint64_t GetBandwidthLimit () const { return m_BandwidthLimit; }; diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index 6a6f2c6c..34f38919 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -33,16 +33,16 @@ namespace data { if (len > size ()) len = size (); memcpy (data (), buf, len); - } - + } + RouterInfo::RouterInfo (): m_Buffer (nullptr) { m_Addresses = boost::make_shared(); // create empty list } RouterInfo::RouterInfo (const std::string& fullPath): - m_FamilyID (0), m_IsUpdated (false), m_IsUnreachable (false), - m_SupportedTransports (0),m_ReachableTransports (0), + m_FamilyID (0), m_IsUpdated (false), m_IsUnreachable (false), + m_SupportedTransports (0),m_ReachableTransports (0), m_Caps (0), m_Version (0) { m_Addresses = boost::make_shared(); // create empty list @@ -51,8 +51,8 @@ namespace data } RouterInfo::RouterInfo (std::shared_ptr&& buf, size_t len): - m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false), - m_SupportedTransports (0), m_ReachableTransports (0), + m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false), + m_SupportedTransports (0), m_ReachableTransports (0), m_Caps (0), m_Version (0) { if (len <= MAX_RI_BUFFER_SIZE) @@ -68,12 +68,12 @@ namespace data m_Buffer = nullptr; m_IsUnreachable = true; } - } - + } + RouterInfo::RouterInfo (const uint8_t * buf, size_t len): RouterInfo (std::make_shared (buf, len), len) { - } + } RouterInfo::~RouterInfo () { @@ -160,7 +160,7 @@ namespace data { m_IsUnreachable = true; return; - } + } m_RouterIdentity = std::make_shared(m_Buffer->data (), m_BufferLen); size_t identityLen = m_RouterIdentity->GetFullLen (); if (identityLen >= m_BufferLen) @@ -241,7 +241,7 @@ namespace data // skip unknown address s.seekg (size, std::ios_base::cur); if (s) continue; else return; - } + } while (r < size) { char key[255], value[255]; @@ -288,7 +288,7 @@ namespace data } else if (address->IsSSU2 ()) Base64ToByteStream (value, strlen (value), address->i, 32); - else + else Base64ToByteStream (value, strlen (value), iV2, 32); } else if (!strcmp (key, "v")) @@ -297,7 +297,7 @@ namespace data isV2 = true; else LogPrint (eLogWarning, "RouterInfo: Unexpected value ", value, " for v"); - } + } else if (key[0] == 'i') { // introducers @@ -344,7 +344,7 @@ namespace data if (isHost) { if (address->host.is_v6 ()) - supportedTransports |= (i2p::util::net::IsYggdrasilAddress (address->host) ? eNTCP2V6Mesh : eNTCP2V6); + supportedTransports |= (i2p::util::net::IsYggdrasilAddress (address->host) ? eNTCP2V6Mesh : eNTCP2V6); else supportedTransports |= eNTCP2V4; m_ReachableTransports |= supportedTransports; @@ -366,7 +366,7 @@ namespace data if (isIntroKey) { if (isHost) - supportedTransports |= address->host.is_v4 () ? eSSUV4 : eSSUV6; + supportedTransports |= address->host.is_v4 () ? eSSUV4 : eSSUV6; else if (address->caps & AddressCaps::eV6) { supportedTransports |= eSSUV6; @@ -383,7 +383,7 @@ namespace data { if (!it.iExp) it.iExp = m_Timestamp/1000 + NETDB_INTRODUCEE_EXPIRATION_TIMEOUT; if (ts <= it.iExp && it.iPort > 0 && - ((it.iHost.is_v4 () && address->IsV4 ()) || (it.iHost.is_v6 () && address->IsV6 ()))) + ((it.iHost.is_v4 () && address->IsV4 ()) || (it.iHost.is_v6 () && address->IsV6 ()))) numValid++; else it.iPort = 0; @@ -408,12 +408,12 @@ namespace data { if (address->host.is_v4 ()) m_ReachableTransports |= eSSU2V4; if (address->host.is_v6 ()) m_ReachableTransports |= eSSU2V6; - } - } - if (supportedTransports) + } + } + if (supportedTransports) { if (!(m_SupportedTransports & supportedTransports)) // avoid duplicates - { + { addresses->push_back(address); if (address->transportStyle == eTransportSSU && isV2) { @@ -427,10 +427,10 @@ namespace data ssu2addr->ssu.reset (new SSUExt ()); ssu2addr->ssu->mtu = address->ssu->mtu; for (const auto& introducer: address->ssu->introducers) if (!introducer.iPort) // SSU2 - ssu2addr->ssu->introducers.push_back (introducer); + ssu2addr->ssu->introducers.push_back (introducer); addresses->push_back(ssu2addr); - } - } + } + } m_SupportedTransports |= supportedTransports; } } @@ -483,10 +483,10 @@ namespace data { isNetId = true; if (atoi (value) != i2p::context.GetNetID ()) - { + { LogPrint (eLogError, "RouterInfo: Unexpected ", ROUTER_INFO_PROPERTY_NETID, "=", value); m_IsUnreachable = true; - } + } } // family else if (!strcmp (key, ROUTER_INFO_PROPERTY_FAMILY)) @@ -498,7 +498,7 @@ namespace data { if (netdb.GetFamilies ().VerifyFamily (family, GetIdentHash (), value)) m_FamilyID = netdb.GetFamilies ().GetFamilyID (family); - else + else LogPrint (eLogWarning, "RouterInfo: Family ", family, " signature verification failed"); } @@ -689,7 +689,7 @@ namespace data if (addr->IsV4 ()) m_SupportedTransports |= eSSU2V4; if (addr->IsV6 ()) m_SupportedTransports |= eSSU2V6; m_Addresses->push_back(std::move(addr)); - } + } void RouterInfo::AddSSU2Address (const uint8_t * staticKey, const uint8_t * introKey, const boost::asio::ip::address& host, int port) @@ -706,24 +706,24 @@ namespace data memcpy (addr->s, staticKey, 32); memcpy (addr->i, introKey, 32); if (addr->IsV4 ()) - { + { m_SupportedTransports |= eSSU2V4; m_ReachableTransports |= eSSU2V4; - } + } if (addr->IsV6 ()) - { + { m_SupportedTransports |= eSSU2V6; m_ReachableTransports |= eSSU2V6; - } + } m_Addresses->push_back(std::move(addr)); - } - + } + bool RouterInfo::AddIntroducer (const Introducer& introducer) { for (auto& addr : *m_Addresses) { if (addr->transportStyle == eTransportSSU && - ((addr->IsV4 () && introducer.iHost.is_v4 ()) || (addr->IsV6 () && introducer.iHost.is_v6 ()))) + ((addr->IsV4 () && introducer.iHost.is_v4 ()) || (addr->IsV6 () && introducer.iHost.is_v6 ()))) { for (auto& intro: addr->ssu->introducers) if (intro.iTag == introducer.iTag) return false; // already presented @@ -740,7 +740,7 @@ namespace data for (auto& addr: *m_Addresses) { if (addr->transportStyle == eTransportSSU && - ((addr->IsV4 () && e.address ().is_v4 ()) || (addr->IsV6 () && e.address ().is_v6 ()))) + ((addr->IsV4 () && e.address ().is_v4 ()) || (addr->IsV6 () && e.address ().is_v6 ()))) { for (auto it = addr->ssu->introducers.begin (); it != addr->ssu->introducers.end (); ++it) if (boost::asio::ip::udp::endpoint (it->iHost, it->iPort) == e) @@ -894,8 +894,8 @@ namespace data { return (address->transportStyle == eTransportSSU2) && address->IsV4(); }); - } - + } + std::shared_ptr RouterInfo::GetSSU2V6Address () const { return GetAddress ( @@ -903,8 +903,8 @@ namespace data { return (address->transportStyle == eTransportSSU2) && address->IsV6(); }); - } - + } + template std::shared_ptr RouterInfo::GetAddress (Filter filter) const { @@ -939,7 +939,7 @@ namespace data return address->IsSSU2 () && !memcmp (address->s, key, 32) && address->IsV6 () == isV6; }); } - + std::shared_ptr RouterInfo::GetPublishedNTCP2V4Address () const { return GetAddress ( @@ -1057,17 +1057,17 @@ namespace data if (len > m_Buffer->size ()) len = m_Buffer->size (); memcpy (m_Buffer->data (), buf, len); m_BufferLen = len; - } + } std::shared_ptr RouterInfo::NewBuffer () const { return netdb.NewRouterInfoBuffer (); - } - + } + void RouterInfo::RefreshTimestamp () { - m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); - } + m_Timestamp = i2p::util::GetMillisecondsSinceEpoch (); + } void LocalRouterInfo::CreateBuffer (const PrivateKeys& privateKeys) { @@ -1088,13 +1088,13 @@ namespace data } else LogPrint (eLogError, "RouterInfo: Our RouterInfo is too long ", len + signatureLen); - } + } void LocalRouterInfo::UpdateCaps (uint8_t caps) { SetCaps (caps); UpdateCapsProperty (); - } + } void LocalRouterInfo::UpdateCapsProperty () { @@ -1153,7 +1153,7 @@ namespace data { WriteString ("NTCP2", s); if (address.IsPublishedNTCP2 () && !address.host.is_unspecified () && address.port) - isPublished = true; + isPublished = true; else { WriteString ("caps", properties); @@ -1212,12 +1212,12 @@ namespace data // caps std::string caps; if (address.published) - { + { isPublished = true; if (address.IsIntroducer ()) caps += CAPS_FLAG_SSU_INTRODUCER; - } + } else - { + { if (address.IsV4 ()) caps += CAPS_FLAG_V4; if (address.IsV6 ()) caps += CAPS_FLAG_V6; if (caps.empty ()) caps += CAPS_FLAG_V4; @@ -1228,8 +1228,8 @@ namespace data properties << '='; WriteString (caps, properties); properties << ';'; - } - } + } + } else WriteString ("", s); @@ -1265,7 +1265,7 @@ namespace data i++; } if (address.transportStyle == eTransportSSU) - { + { i = 0; for (const auto& introducer: address.ssu->introducers) { @@ -1275,13 +1275,13 @@ namespace data properties << ';'; i++; } - } + } i = 0; for (const auto& introducer: address.ssu->introducers) { if (address.IsSSU2 ()) WriteString ("ih" + boost::lexical_cast(i), properties); - else + else WriteString ("ikey" + boost::lexical_cast(i), properties); properties << '='; char value[64]; @@ -1302,7 +1302,7 @@ namespace data properties << ';'; i++; } - } + } i = 0; for (const auto& introducer: address.ssu->introducers) { @@ -1326,7 +1326,7 @@ namespace data properties << ';'; } if (address.transportStyle == eTransportSSU || address.IsSSU2 ()) - { + { // write mtu if (address.ssu && address.ssu->mtu) { @@ -1373,7 +1373,7 @@ namespace data uint16_t size = htobe16 (properties.str ().size ()); s.write ((char *)&size, sizeof (size)); s.write (properties.str ().c_str (), properties.str ().size ()); - } + } void LocalRouterInfo::SetProperty (const std::string& key, const std::string& value) { @@ -1391,18 +1391,18 @@ namespace data if (it != m_Properties.end ()) return it->second; return ""; - } + } void LocalRouterInfo::WriteString (const std::string& str, std::ostream& s) const { uint8_t len = str.size (); s.write ((char *)&len, 1); s.write (str.c_str (), len); - } + } std::shared_ptr LocalRouterInfo::NewBuffer () const { return std::make_shared (); - } + } } } diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index e838f993..a93f2a64 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -56,7 +56,7 @@ namespace data const uint8_t COST_SSU_DIRECT = 9; const uint8_t COST_SSU_THROUGH_INTRODUCERS = 11; const uint8_t COST_SSU2_NON_PUBLISHED = 15; - + const size_t MAX_RI_BUFFER_SIZE = 3072; // if RouterInfo exceeds 3K we consider it as malformed, might extend later class RouterInfo: public RoutingDestination { @@ -146,11 +146,11 @@ namespace data return !(*this == other); } - bool IsNTCP2 () const { return transportStyle == eTransportNTCP; }; - bool IsSSU2 () const { return transportStyle == eTransportSSU2; }; + bool IsNTCP2 () const { return transportStyle == eTransportNTCP; }; + bool IsSSU2 () const { return transportStyle == eTransportSSU2; }; bool IsPublishedNTCP2 () const { return IsNTCP2 () && published; }; bool IsReachableSSU () const { return (bool)ssu && (published || UsesIntroducer ()); }; - bool UsesIntroducer () const { return (bool)ssu && !ssu->introducers.empty (); }; + bool UsesIntroducer () const { return (bool)ssu && !ssu->introducers.empty (); }; bool IsIntroducer () const { return caps & eSSUIntroducer; }; bool IsPeerTesting () const { return caps & eSSUTesting; }; @@ -166,7 +166,7 @@ namespace data Buffer () = default; Buffer (const uint8_t * buf, size_t len); }; - + typedef std::vector > Addresses; RouterInfo (const std::string& fullPath); @@ -181,7 +181,7 @@ namespace data std::string GetIdentHashBase64 () const { return GetIdentHash ().ToBase64 (); }; uint64_t GetTimestamp () const { return m_Timestamp; }; int GetVersion () const { return m_Version; }; - virtual void SetProperty (const std::string& key, const std::string& value) {}; + virtual void SetProperty (const std::string& key, const std::string& value) {}; virtual void ClearProperties () {}; Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr std::shared_ptr GetNTCP2AddressWithStaticKey (const uint8_t * key) const; @@ -272,7 +272,7 @@ namespace data void SetBufferLen (size_t len) { m_BufferLen = len; }; void RefreshTimestamp (); const Addresses& GetAddresses () const { return *m_Addresses; }; - + private: bool LoadFile (const std::string& fullPath); @@ -285,7 +285,7 @@ namespace data template std::shared_ptr GetAddress (Filter filter) const; virtual std::shared_ptr NewBuffer () const; - + private: FamilyID m_FamilyID; @@ -309,22 +309,22 @@ namespace data void CreateBuffer (const PrivateKeys& privateKeys); void UpdateCaps (uint8_t caps); - void SetProperty (const std::string& key, const std::string& value) override; - void DeleteProperty (const std::string& key); - std::string GetProperty (const std::string& key) const; + void SetProperty (const std::string& key, const std::string& value) override; + void DeleteProperty (const std::string& key); + std::string GetProperty (const std::string& key) const; void ClearProperties () override { m_Properties.clear (); }; - + private: void WriteToStream (std::ostream& s) const; void UpdateCapsProperty (); void WriteString (const std::string& str, std::ostream& s) const; std::shared_ptr NewBuffer () const override; - + private: - std::map m_Properties; - }; + std::map m_Properties; + }; } } diff --git a/libi2pd/SSU.cpp b/libi2pd/SSU.cpp index 31d065bd..eec55857 100644 --- a/libi2pd/SSU.cpp +++ b/libi2pd/SSU.cpp @@ -273,14 +273,14 @@ namespace transport void SSUServer::HandleReceivedFrom (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet) { if (!ecode - || ecode == boost::asio::error::connection_refused - || ecode == boost::asio::error::connection_reset - || ecode == boost::asio::error::network_unreachable - || ecode == boost::asio::error::host_unreachable + || ecode == boost::asio::error::connection_refused + || ecode == boost::asio::error::connection_reset + || ecode == boost::asio::error::network_unreachable + || ecode == boost::asio::error::host_unreachable #ifdef _WIN32 // windows can throw WinAPI error, which is not handled by ASIO - || ecode.value() == boost::winapi::ERROR_CONNECTION_REFUSED_ - || ecode.value() == boost::winapi::ERROR_NETWORK_UNREACHABLE_ - || ecode.value() == boost::winapi::ERROR_HOST_UNREACHABLE_ + || ecode.value() == boost::winapi::ERROR_CONNECTION_REFUSED_ + || ecode.value() == boost::winapi::ERROR_NETWORK_UNREACHABLE_ + || ecode.value() == boost::winapi::ERROR_HOST_UNREACHABLE_ #endif ) // just try continue reading when received ICMP response otherwise socket can crash, @@ -332,14 +332,14 @@ namespace transport void SSUServer::HandleReceivedFromV6 (const boost::system::error_code& ecode, std::size_t bytes_transferred, SSUPacket * packet) { if (!ecode - || ecode == boost::asio::error::connection_refused - || ecode == boost::asio::error::connection_reset - || ecode == boost::asio::error::network_unreachable - || ecode == boost::asio::error::host_unreachable + || ecode == boost::asio::error::connection_refused + || ecode == boost::asio::error::connection_reset + || ecode == boost::asio::error::network_unreachable + || ecode == boost::asio::error::host_unreachable #ifdef _WIN32 // windows can throw WinAPI error, which is not handled by ASIO - || ecode.value() == boost::winapi::ERROR_CONNECTION_REFUSED_ - || ecode.value() == boost::winapi::ERROR_NETWORK_UNREACHABLE_ - || ecode.value() == boost::winapi::ERROR_HOST_UNREACHABLE_ + || ecode.value() == boost::winapi::ERROR_CONNECTION_REFUSED_ + || ecode.value() == boost::winapi::ERROR_NETWORK_UNREACHABLE_ + || ecode.value() == boost::winapi::ERROR_HOST_UNREACHABLE_ #endif ) // just try continue reading when received ICMP response otherwise socket can crash, @@ -582,7 +582,7 @@ namespace transport "] through introducer ", introducer->iHost, ":", introducer->iPort); session->WaitForIntroduction (); if ((address->host.is_v4 () && i2p::context.GetStatus () == eRouterStatusFirewalled) || - (address->host.is_v6 () && i2p::context.GetStatusV6 () == eRouterStatusFirewalled)) + (address->host.is_v6 () && i2p::context.GetStatusV6 () == eRouterStatusFirewalled)) { uint8_t buf[1]; Send (buf, 0, remoteEndpoint); // send HolePunch @@ -676,7 +676,7 @@ namespace transport for (const auto& s : sessions) { if (s.second->GetRelayTag () && s.second->GetState () == eSessionStateEstablished && - ts < s.second->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_EXPIRATION) + ts < s.second->GetCreationTime () + SSU_TO_INTRODUCER_SESSION_EXPIRATION) ret.push_back (s.second); else if (s.second->GetRemoteIdentity ()) excluded.insert (s.second->GetRemoteIdentity ()->GetIdentHash ()); diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index dc036ba8..6d14bbd7 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -25,8 +25,8 @@ namespace transport uint64_t data = 0; i2p::crypto::ChaCha20 ((uint8_t *)&data, 8, kh, nonce, (uint8_t *)&data); return data; - } - + } + SSU2Session::SSU2Session (SSU2Server& server, std::shared_ptr in_RemoteRouter, std::shared_ptr addr): TransportSession (in_RemoteRouter, SSU2_CONNECT_TIMEOUT), @@ -41,17 +41,17 @@ namespace transport InitNoiseXKState1 (*m_NoiseState, m_Address->s); m_RemoteEndpoint = boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port); RAND_bytes ((uint8_t *)&m_DestConnID, 8); - RAND_bytes ((uint8_t *)&m_SourceConnID, 8); - } + RAND_bytes ((uint8_t *)&m_SourceConnID, 8); + } else { // incoming InitNoiseXKState1 (*m_NoiseState, i2p::context.GetSSU2StaticPublicKey ()); - } + } } - + SSU2Session::~SSU2Session () - { + { } void SSU2Session::Connect () @@ -61,7 +61,7 @@ namespace transport SendSessionRequest (token); else SendTokenRequest (); - } + } bool SSU2Session::Introduce (std::shared_ptr session, uint32_t relayTag) { @@ -106,10 +106,10 @@ namespace transport session->m_DestConnID = ~session->m_SourceConnID; m_Server.AddSession (session); SendData (payload, payloadSize); - + return true; - } - + } + void SSU2Session::Terminate () { if (m_State != eSSU2SessionStateTerminated) @@ -122,14 +122,14 @@ namespace transport m_SendQueue.clear (); LogPrint (eLogDebug, "SSU2: Session terminated"); } - } + } void SSU2Session::TerminateByTimeout () { SendTermination (); m_Server.GetService ().post (std::bind (&SSU2Session::Terminate, shared_from_this ())); - } - + } + void SSU2Session::Established () { m_State = eSSU2SessionStateEstablished; @@ -139,27 +139,27 @@ namespace transport SetTerminationTimeout (SSU2_TERMINATION_TIMEOUT); transports.PeerConnected (shared_from_this ()); if (m_OnEstablished) m_OnEstablished (); - } + } void SSU2Session::Done () { m_Server.GetService ().post (std::bind (&SSU2Session::Terminate, shared_from_this ())); } - + void SSU2Session::SendI2NPMessages (const std::vector >& msgs) - { + { m_Server.GetService ().post (std::bind (&SSU2Session::PostI2NPMessages, shared_from_this (), msgs)); - } + } void SSU2Session::PostI2NPMessages (std::vector > msgs) { for (auto it: msgs) m_SendQueue.push_back (it); SendQueue (); - } - + } + bool SSU2Session::SendQueue () - { + { if (!m_SendQueue.empty () && m_SentPackets.size () <= m_WindowSize) { auto nextResend = i2p::util::GetSecondsSinceEpoch () + SSU2_RESEND_INTERVAL; @@ -173,24 +173,24 @@ namespace transport { m_SendQueue.pop_front (); packet->payloadSize += CreateI2NPBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize, std::move (msg)); - } + } else if (len > SSU2_MAX_PAYLOAD_SIZE - 32) // message too long - { + { m_SendQueue.pop_front (); SendFragmentedMessage (msg); - } + } else { // send right a way if (packet->payloadSize + 16 < SSU2_MAX_PAYLOAD_SIZE) - packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); + packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); uint32_t packetNum = SendData (packet->payload, packet->payloadSize); packet->nextResendTime = nextResend; m_SentPackets.emplace (packetNum, packet); packet = std::make_shared(); - packet->payloadSize += CreateAckBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); - } - }; + packet->payloadSize += CreateAckBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); + } + }; if (packet->payloadSize) { if (packet->payloadSize + 16 < SSU2_MAX_PAYLOAD_SIZE) @@ -198,11 +198,11 @@ namespace transport uint32_t packetNum = SendData (packet->payload, packet->payloadSize); packet->nextResendTime = nextResend; m_SentPackets.emplace (packetNum, packet); - } + } return true; } return false; - } + } void SSU2Session::SendFragmentedMessage (std::shared_ptr msg) { @@ -214,7 +214,7 @@ namespace transport auto size = CreateFirstFragmentBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - 32 - packet->payloadSize, msg); if (!size) return; packet->payloadSize += size; - packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); + packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); uint32_t firstPacketNum = SendData (packet->payload, packet->payloadSize); packet->nextResendTime = nextResend; m_SentPackets.emplace (firstPacketNum, packet); @@ -223,13 +223,13 @@ namespace transport { packet = std::make_shared(); packet->payloadSize += CreateFollowOnFragmentBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize - 16, msg, fragmentNum, msgID); - packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); + packet->payloadSize += CreatePaddingBlock (packet->payload + packet->payloadSize, SSU2_MAX_PAYLOAD_SIZE - packet->payloadSize); uint32_t followonPacketNum = SendData (packet->payload, packet->payloadSize); packet->nextResendTime = nextResend; m_SentPackets.emplace (followonPacketNum, packet); - } - } - + } + } + void SSU2Session::Resend (uint64_t ts) { if (m_SentPackets.empty ()) return; @@ -243,11 +243,11 @@ namespace transport { uint32_t packetNum = SendData (it->second->payload, it->second->payloadSize); it->second->numResends++; - it->second->nextResendTime = ts + it->second->numResends*SSU2_RESEND_INTERVAL; + it->second->nextResendTime = ts + it->second->numResends*SSU2_RESEND_INTERVAL; m_LastActivityTimestamp = ts; - resentPackets.emplace (packetNum, it->second); + resentPackets.emplace (packetNum, it->second); it = m_SentPackets.erase (it); - } + } } else it++; @@ -256,12 +256,12 @@ namespace transport #if (__cplusplus >= 201703L) // C++ 17 or higher m_SentPackets.merge (resentPackets); #else - m_SentPackets.insert (resentPackets.begin (), resentPackets.end ()); -#endif - } + m_SentPackets.insert (resentPackets.begin (), resentPackets.end ()); +#endif + } SendQueue (); - } - + } + bool SSU2Session::ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len) { // we are Bob @@ -277,29 +277,29 @@ namespace transport break; case eSSU2TokenRequest: ProcessTokenRequest (header, buf, len); - break; + break; default: - { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); - return false; - } + { + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + return false; + } } return true; - } - + } + void SSU2Session::SendSessionRequest (uint64_t token) { // we are Alice m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair (); - + Header header; - uint8_t headerX[48], payload[40]; + uint8_t headerX[48], payload[40]; // fill packet header.h.connID = m_DestConnID; // dest id header.h.packetNum = 0; header.h.type = eSSU2SessionRequest; header.h.flags[0] = 2; // ver - header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID + header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[2] = 0; // flag memcpy (headerX, &m_SourceConnID, 8); // source id memcpy (headerX + 8, &token, 8); // token @@ -310,8 +310,8 @@ namespace transport htobe32buf (payload + 3, i2p::util::GetSecondsSinceEpoch ()); size_t payloadSize = 7; payloadSize += CreatePaddingBlock (payload + payloadSize, 40 - payloadSize, 1); - // KDF for session request - m_NoiseState->MixHash ({ {header.buf, 16}, {headerX, 16} }); // h = SHA256(h || header) + // KDF for session request + m_NoiseState->MixHash ({ {header.buf, 16}, {headerX, 16} }); // h = SHA256(h || header) m_NoiseState->MixHash (m_EphemeralKeys->GetPublicKey (), 32); // h = SHA256(h || aepk); uint8_t sharedSecret[32]; m_EphemeralKeys->Agree (m_Address->s, sharedSecret); @@ -327,7 +327,7 @@ namespace transport // send m_Server.AddPendingOutgoingSession (shared_from_this ()); m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint); - } + } void SSU2Session::ProcessSessionRequest (Header& header, uint8_t * buf, size_t len) { @@ -335,7 +335,7 @@ namespace transport const uint8_t nonce[12] = {0}; uint8_t headerX[48]; i2p::crypto::ChaCha20 (buf + 16, 48, i2p::context.GetSSU2IntroKey (), nonce, headerX); - memcpy (&m_DestConnID, headerX, 8); + memcpy (&m_DestConnID, headerX, 8); uint64_t token; memcpy (&token, headerX + 8, 8); if (!token || token != m_Server.GetIncomingToken (m_RemoteEndpoint)) @@ -343,29 +343,29 @@ namespace transport LogPrint (eLogDebug, "SSU2: SessionRequest token mismatch. Retry"); SendRetry (); return; - } - // KDF for session request - m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) + } + // KDF for session request + m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || aepk); uint8_t sharedSecret[32]; i2p::context.GetSSU2StaticKeys ().Agree (headerX + 16, sharedSecret); m_NoiseState->MixKey (sharedSecret); // decrypt - uint8_t * payload = buf + 64; + uint8_t * payload = buf + 64; std::vector decryptedPayload(len - 80); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false)) { LogPrint (eLogWarning, "SSU2: SessionRequest AEAD verification failed "); return; - } + } m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || encrypted payload from Session Request) for SessionCreated // payload HandlePayload (decryptedPayload.data (), decryptedPayload.size ()); - + m_Server.AddSession (shared_from_this ()); SendSessionCreated (headerX + 16); - } + } void SSU2Session::SendSessionCreated (const uint8_t * X) { @@ -373,15 +373,15 @@ namespace transport m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair (); uint8_t kh2[32]; i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32) - + // fill packet Header header; - uint8_t headerX[48], payload[64]; + uint8_t headerX[48], payload[64]; header.h.connID = m_DestConnID; // dest id header.h.packetNum = 0; header.h.type = eSSU2SessionCreated; header.h.flags[0] = 2; // ver - header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID + header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[2] = 0; // flag memcpy (headerX, &m_SourceConnID, 8); // source id RAND_bytes (headerX + 8, 8); // token @@ -398,10 +398,10 @@ namespace transport htobe16buf (payload + payloadSize + 1, 4); htobe32buf (payload + payloadSize + 3, m_RelayTag); payloadSize += 7; - } + } payloadSize += CreatePaddingBlock (payload + payloadSize, 64 - payloadSize); // KDF for SessionCreated - m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) + m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk); uint8_t sharedSecret[32]; m_EphemeralKeys->Agree (X, sharedSecret); @@ -416,8 +416,8 @@ namespace transport i2p::crypto::ChaCha20 (headerX, 48, kh2, nonce, headerX); // send m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint); - } - + } + bool SSU2Session::ProcessSessionCreated (uint8_t * buf, size_t len) { // we are Alice @@ -427,14 +427,14 @@ namespace transport uint8_t kh2[32]; i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32) header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12)); - if (header.h.type != eSSU2SessionCreated) - // this situation is valid, because it might be Retry with different encryption + if (header.h.type != eSSU2SessionCreated) + // this situation is valid, because it might be Retry with different encryption return false; const uint8_t nonce[12] = {0}; uint8_t headerX[48]; i2p::crypto::ChaCha20 (buf + 16, 48, kh2, nonce, headerX); // KDF for SessionCreated - m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) + m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header) m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk); uint8_t sharedSecret[32]; m_EphemeralKeys->Agree (headerX + 16, sharedSecret); @@ -442,23 +442,23 @@ namespace transport // decrypt uint8_t * payload = buf + 64; std::vector decryptedPayload(len - 80); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false)) { LogPrint (eLogWarning, "SSU2: SessionCreated AEAD verification failed "); return false; - } + } m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || encrypted payload from SessionCreated) for SessionConfirmed // payload HandlePayload (decryptedPayload.data (), decryptedPayload.size ()); - + m_Server.AddSession (shared_from_this ()); SendSessionConfirmed (headerX + 16); KDFDataPhase (m_KeyDataSend, m_KeyDataReceive); - Established (); - + Established (); + return true; - } + } void SSU2Session::SendSessionConfirmed (const uint8_t * Y) { @@ -478,9 +478,9 @@ namespace transport size_t payloadSize = CreateRouterInfoBlock (payload, maxPayloadSize, i2p::context.GetSharedRouterInfo ()); // TODO: check is RouterInfo doesn't fit and split by two fragments if (payloadSize < maxPayloadSize) - payloadSize += CreatePaddingBlock (payload + payloadSize, maxPayloadSize - payloadSize); + payloadSize += CreatePaddingBlock (payload + payloadSize, maxPayloadSize - payloadSize); // KDF for Session Confirmed part 1 - m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header) + m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header) // Encrypt part 1 uint8_t part1[48]; uint8_t nonce[12]; @@ -502,7 +502,7 @@ namespace transport // send m_Server.Send (header.buf, 16, part1, 48, payload, payloadSize, m_RemoteEndpoint); m_SendPacketNum++; - } + } bool SSU2Session::ProcessSessionConfirmed (uint8_t * buf, size_t len) { @@ -513,11 +513,11 @@ namespace transport uint8_t kh2[32]; i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessionConfirmed", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessionConfirmed", 32) header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12)); - if (header.h.type != eSSU2SessionConfirmed) + if (header.h.type != eSSU2SessionConfirmed) { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); return false; - } + } // check if fragmented if ((header.h.flags[0] & 0x0F) > 1) { @@ -530,7 +530,7 @@ namespace transport memcpy (m_SessionConfirmedFragment1->payload, buf + 16, len - 16); m_SessionConfirmedFragment1->payloadSize = len - 16; return true; // wait for second fragment - } + } else { // second fragment @@ -541,15 +541,15 @@ namespace transport memcpy (fullMsg + 16 + m_SessionConfirmedFragment1->payloadSize, buf + 16, len - 16); buf = fullMsg; len += m_SessionConfirmedFragment1->payloadSize; - } - } + } + } // KDF for Session Confirmed part 1 - m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header) + m_NoiseState->MixHash (header.buf, 16); // h = SHA256(h || header) // decrypt part1 uint8_t nonce[12]; CreateNonce (1, nonce); uint8_t S[32]; - if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, 32, m_NoiseState->m_H, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, 32, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, S, 32, false)) { LogPrint (eLogWarning, "SSU2: SessionConfirmed part 1 AEAD verification failed "); @@ -564,12 +564,12 @@ namespace transport memset (nonce, 0, 12); uint8_t * payload = buf + 64; std::vector decryptedPayload(len - 80); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, decryptedPayload.data (), decryptedPayload.size (), false)) { LogPrint (eLogWarning, "SSU2: SessionConfirmed part 2 AEAD verification failed "); return false; - } + } m_NoiseState->MixHash (payload, len - 64); // h = SHA256(h || ciphertext); // payload // handle RouterInfo block that must be first @@ -577,38 +577,38 @@ namespace transport { LogPrint (eLogError, "SSU2: SessionConfirmed unexpected first block type ", (int)decryptedPayload[0]); return false; - } + } size_t riSize = bufbe16toh (decryptedPayload.data () + 1); if (riSize + 3 > decryptedPayload.size ()) { LogPrint (eLogError, "SSU2: SessionConfirmed RouterInfo block is too long ", riSize); return false; - } + } LogPrint (eLogDebug, "SSU2: RouterInfo in SessionConfirmed"); auto ri = ExtractRouterInfo (decryptedPayload.data () + 3, riSize); if (!ri) { LogPrint (eLogError, "SSU2: SessionConfirmed malformed RouterInfo block"); return false; - } + } SetRemoteIdentity (ri->GetRouterIdentity ()); m_Server.AddSessionByRouterHash (shared_from_this ()); // we know remote router now - m_Address = ri->GetSSU2AddressWithStaticKey (S, m_RemoteEndpoint.address ().is_v6 ()); + m_Address = ri->GetSSU2AddressWithStaticKey (S, m_RemoteEndpoint.address ().is_v6 ()); if (!m_Address) { LogPrint (eLogError, "SSU2: No SSU2 address with static key found in SessionConfirmed"); return false; - } + } i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, ri->GetBuffer (), ri->GetBufferLen ())); // TODO: should insert ri // handle other blocks HandlePayload (decryptedPayload.data () + riSize + 3, decryptedPayload.size () - riSize - 3); KDFDataPhase (m_KeyDataReceive, m_KeyDataSend); Established (); - SendQuickAck (); - + SendQuickAck (); + return true; - } + } void SSU2Session::KDFDataPhase (uint8_t * keydata_ab, uint8_t * keydata_ba) { @@ -618,19 +618,19 @@ namespace transport i2p::crypto::HKDF (keydata, nullptr, 0, "HKDFSSU2DataKeys", keydata_ab); // keydata_ab = HKDF(keydata, ZEROLEN, "HKDFSSU2DataKeys", 64) // ba i2p::crypto::HKDF (keydata + 32, nullptr, 0, "HKDFSSU2DataKeys", keydata_ba); // keydata_ba = HKDF(keydata + 32, ZEROLEN, "HKDFSSU2DataKeys", 64) - } - + } + void SSU2Session::SendTokenRequest () { // we are Alice Header header; - uint8_t h[32], payload[40]; + uint8_t h[32], payload[40]; // fill packet header.h.connID = m_DestConnID; // dest id RAND_bytes (header.buf + 8, 4); // random packet num header.h.type = eSSU2TokenRequest; header.h.flags[0] = 2; // ver - header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID + header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[2] = 0; // flag memcpy (h, header.buf, 16); memcpy (h + 16, &m_SourceConnID, 8); // source id @@ -653,7 +653,7 @@ namespace transport // send m_Server.AddPendingOutgoingSession (shared_from_this ()); m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint); - } + } void SSU2Session::ProcessTokenRequest (Header& header, uint8_t * buf, size_t len) { @@ -662,32 +662,32 @@ namespace transport uint8_t h[32]; memcpy (h, header.buf, 16); i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16); - memcpy (&m_DestConnID, h + 16, 8); + memcpy (&m_DestConnID, h + 16, 8); // decrypt CreateNonce (be32toh (header.h.packetNum), nonce); - uint8_t * payload = buf + 32; - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, + uint8_t * payload = buf + 32; + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, i2p::context.GetSSU2IntroKey (), nonce, payload, len - 48, false)) { LogPrint (eLogWarning, "SSU2: TokenRequest AEAD verification failed "); return; - } + } // payload HandlePayload (payload, len - 48); SendRetry (); - } + } void SSU2Session::SendRetry () { // we are Bob Header header; - uint8_t h[32], payload[64]; + uint8_t h[32], payload[64]; // fill packet header.h.connID = m_DestConnID; // dest id RAND_bytes (header.buf + 8, 4); // random packet num header.h.type = eSSU2Retry; header.h.flags[0] = 2; // ver - header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID + header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[2] = 0; // flag memcpy (h, header.buf, 16); memcpy (h + 16, &m_SourceConnID, 8); // source id @@ -711,8 +711,8 @@ namespace transport i2p::crypto::ChaCha20 (h + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, h + 16); // send m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, m_RemoteEndpoint); - } - + } + bool SSU2Session::ProcessRetry (uint8_t * buf, size_t len) { // we are Alice @@ -720,11 +720,11 @@ namespace transport memcpy (header.buf, buf, 16); header.ll[0] ^= CreateHeaderMask (m_Address->i, buf + (len - 24)); header.ll[1] ^= CreateHeaderMask (m_Address->i, buf + (len - 12)); - if (header.h.type != eSSU2Retry) + if (header.h.type != eSSU2Retry) { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); return false; - } + } uint8_t nonce[12] = {0}; uint64_t headerX[2]; // sourceConnID, token i2p::crypto::ChaCha20 (buf + 16, 16, m_Address->i, nonce, (uint8_t *)headerX); @@ -735,18 +735,18 @@ namespace transport uint8_t h[32]; memcpy (h, header.buf, 16); memcpy (h + 16, &headerX, 16); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, m_Address->i, nonce, payload, len - 48, false)) { LogPrint (eLogWarning, "SSU2: Retry AEAD verification failed "); return false; - } + } HandlePayload (payload, len - 48); - + InitNoiseXKState1 (*m_NoiseState, m_Address->s); // reset Noise TODO: check state SendSessionRequest (headerX[1]); return true; - } + } void SSU2Session::SendHolePunch (uint32_t nonce, const boost::asio::ip::udp::endpoint& ep, const uint8_t * introKey) { @@ -758,7 +758,7 @@ namespace transport RAND_bytes (header.buf + 8, 4); // random packet num header.h.type = eSSU2HolePunch; header.h.flags[0] = 2; // ver - header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID + header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID header.h.flags[2] = 0; // flag memcpy (h, header.buf, 16); uint64_t c = !header.h.connID; @@ -784,8 +784,8 @@ namespace transport i2p::crypto::ChaCha20 (h + 16, 16, introKey, n, h + 16); // send m_Server.Send (header.buf, 16, h + 16, 16, payload, payloadSize, ep); - } - + } + bool SSU2Session::ProcessHolePunch (uint8_t * buf, size_t len) { // we are Alice @@ -793,11 +793,11 @@ namespace transport memcpy (header.buf, buf, 16); header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 24)); header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12)); - if (header.h.type != eSSU2HolePunch) + if (header.h.type != eSSU2HolePunch) { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); return false; - } + } uint8_t nonce[12] = {0}; uint64_t headerX[2]; // sourceConnID, token i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX); @@ -808,7 +808,7 @@ namespace transport uint8_t h[32]; memcpy (h, header.buf, 16); memcpy (h + 16, &headerX, 16); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, i2p::context.GetSSU2IntroKey (), nonce, payload, len - 48, false)) { LogPrint (eLogWarning, "SSU2: HolePunch AEAD verification failed "); @@ -818,11 +818,11 @@ namespace transport HandlePayload (payload, len - 48); // connect to Charlie if (m_State == eSSU2SessionStateIntroduced) - { + { m_State = eSSU2SessionStateUnknown; Connect (); } - + return true; } @@ -833,11 +833,11 @@ namespace transport memcpy (header.buf, buf, 16); header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 24)); header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12)); - if (header.h.type != eSSU2PeerTest) + if (header.h.type != eSSU2PeerTest) { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); return false; - } + } uint8_t nonce[12] = {0}; uint64_t headerX[2]; // sourceConnID, token i2p::crypto::ChaCha20 (buf + 16, 16, i2p::context.GetSSU2IntroKey (), nonce, (uint8_t *)headerX); @@ -848,7 +848,7 @@ namespace transport uint8_t h[32]; memcpy (h, header.buf, 16); memcpy (h + 16, &headerX, 16); - if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, + if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 48, h, 32, i2p::context.GetSSU2IntroKey (), nonce, payload, len - 48, false)) { LogPrint (eLogWarning, "SSU2: PeerTest AEAD verification failed "); @@ -856,15 +856,15 @@ namespace transport } HandlePayload (payload, len - 48); return true; - } - + } + uint32_t SSU2Session::SendData (const uint8_t * buf, size_t len) { if (len < 8) { LogPrint (eLogWarning, "SSU2: Data message payload is too short ", (int)len); return 0; - } + } Header header; header.h.connID = m_DestConnID; header.h.packetNum = htobe32 (m_SendPacketNum); @@ -881,36 +881,36 @@ namespace transport m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); m_NumSentBytes += len + 32; return m_SendPacketNum - 1; - } - + } + void SSU2Session::ProcessData (uint8_t * buf, size_t len) { Header header; header.ll[0] = m_SourceConnID; memcpy (header.buf + 8, buf + 8, 8); header.ll[1] ^= CreateHeaderMask (m_KeyDataReceive + 32, buf + (len - 12)); - if (header.h.type != eSSU2Data) + if (header.h.type != eSSU2Data) { - LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); + LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type); return; - } + } uint8_t payload[SSU2_MTU]; size_t payloadSize = len - 32; - uint32_t packetNum = be32toh (header.h.packetNum); + uint32_t packetNum = be32toh (header.h.packetNum); uint8_t nonce[12]; CreateNonce (packetNum, nonce); - if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, payloadSize, header.buf, 16, + if (!i2p::crypto::AEADChaCha20Poly1305 (buf + 16, payloadSize, header.buf, 16, m_KeyDataReceive, nonce, payload, payloadSize, false)) { LogPrint (eLogWarning, "SSU2: Data AEAD verification failed "); return; - } + } m_LastActivityTimestamp = i2p::util::GetSecondsSinceEpoch (); m_NumReceivedBytes += len; if (UpdateReceivePacketNum (packetNum)) HandlePayload (payload, payloadSize); - } - + } + void SSU2Session::HandlePayload (const uint8_t * buf, size_t len) { size_t offset = 0; @@ -935,16 +935,16 @@ namespace transport LogPrint (eLogDebug, "SSU2: Options"); break; case eSSU2BlkRouterInfo: - { - // not from SessionConfirmed + { + // not from SessionConfirmed LogPrint (eLogDebug, "SSU2: RouterInfo"); - auto ri = ExtractRouterInfo (buf + offset, size); + auto ri = ExtractRouterInfo (buf + offset, size); if (ri) - i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, ri->GetBuffer (), ri->GetBufferLen ())); // TODO: should insert ri - break; - } + i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, ri->GetBuffer (), ri->GetBufferLen ())); // TODO: should insert ri + break; + } case eSSU2BlkI2NPMessage: - { + { LogPrint (eLogDebug, "SSU2: I2NP message"); auto nextMsg = NewI2NPShortMessage (); nextMsg->len = nextMsg->offset + size + 7; // 7 more bytes for full I2NP header @@ -952,66 +952,66 @@ namespace transport nextMsg->FromNTCP2 (); // SSU2 has the same format as NTCP2 m_Handler.PutNextMessage (std::move (nextMsg)); m_IsDataReceived = true; - break; - } + break; + } case eSSU2BlkFirstFragment: LogPrint (eLogDebug, "SSU2: First fragment"); HandleFirstFragment (buf + offset, size); m_IsDataReceived = true; - break; + break; case eSSU2BlkFollowOnFragment: LogPrint (eLogDebug, "SSU2: Follow-on fragment"); HandleFollowOnFragment (buf + offset, size); m_IsDataReceived = true; - break; + break; case eSSU2BlkTermination: LogPrint (eLogDebug, "SSU2: Termination"); Terminate (); - break; + break; case eSSU2BlkRelayRequest: LogPrint (eLogDebug, "SSU2: RelayRequest"); HandleRelayRequest (buf + offset, size); - break; + break; case eSSU2BlkRelayResponse: LogPrint (eLogDebug, "SSU2: RelayResponse"); HandleRelayResponse (buf + offset, size); - break; + break; case eSSU2BlkRelayIntro: LogPrint (eLogDebug, "SSU2: RelayIntro"); HandleRelayIntro (buf + offset, size); - break; + break; case eSSU2BlkPeerTest: LogPrint (eLogDebug, "SSU2: PeerTest"); HandlePeerTest (buf + offset, size); - break; + break; case eSSU2BlkNextNonce: - break; + break; case eSSU2BlkAck: LogPrint (eLogDebug, "SSU2: Ack"); HandleAck (buf + offset, size); - break; + break; case eSSU2BlkAddress: { - boost::asio::ip::udp::endpoint ep; + boost::asio::ip::udp::endpoint ep; if (ExtractEndpoint (buf + offset, size, ep)) - LogPrint (eLogInfo, "SSU2: Our external address is ", ep); + LogPrint (eLogInfo, "SSU2: Our external address is ", ep); break; - } + } case eSSU2BlkIntroKey: - break; + break; case eSSU2BlkRelayTagRequest: LogPrint (eLogDebug, "SSU2: RelayTagRequest"); HandleRelayRequest (buf + offset, size); - if (!m_RelayTag) - { + if (!m_RelayTag) + { RAND_bytes ((uint8_t *)&m_RelayTag, 4); m_Server.AddRelay (m_RelayTag, shared_from_this ()); - } - break; + } + break; case eSSU2BlkRelayTag: LogPrint (eLogDebug, "SSU2: RelayTag"); m_RelayTag = bufbe32toh (buf + offset); - break; + break; case eSSU2BlkNewToken: { LogPrint (eLogDebug, "SSU2: New token"); @@ -1019,30 +1019,30 @@ namespace transport memcpy (&token, buf + offset + 4, 8); m_Server.UpdateOutgoingToken (m_RemoteEndpoint, token, bufbe32toh (buf + offset)); break; - } + } case eSSU2BlkPathChallenge: - break; + break; case eSSU2BlkPathResponse: - break; + break; case eSSU2BlkFirstPacketNumber: - break; + break; case eSSU2BlkPadding: LogPrint (eLogDebug, "SSU2: Padding"); - break; + break; default: LogPrint (eLogWarning, "SSU2: Unknown block type ", (int)blk); - } + } offset += size; - } - } + } + } void SSU2Session::HandleAck (const uint8_t * buf, size_t len) { if (m_SentPackets.empty ()) return; - if (len < 5) return; + if (len < 5) return; // acnt uint32_t ackThrough = bufbe32toh (buf); - uint32_t firstPacketNum = ackThrough > buf[4] ? ackThrough - buf[4] : 0; + uint32_t firstPacketNum = ackThrough > buf[4] ? ackThrough - buf[4] : 0; HandleAckRange (firstPacketNum, ackThrough); // acnt // ranges len -= 5; @@ -1056,7 +1056,7 @@ namespace transport firstPacketNum = lastPacketNum - *ranges; ranges++; // acks len -= 2; HandleAckRange (firstPacketNum, lastPacketNum); - } + } } void SSU2Session::HandleAckRange (uint32_t firstPacketNum, uint32_t lastPacketNum) @@ -1069,28 +1069,28 @@ namespace transport while (it1 != m_SentPackets.end () && it1->first <= lastPacketNum) it1++; if (it1 != m_SentPackets.end () && it1 != m_SentPackets.begin ()) it1--; m_SentPackets.erase (it, it1); - } + } void SSU2Session::HandleFirstFragment (const uint8_t * buf, size_t len) { uint32_t msgID; memcpy (&msgID, buf + 1, 4); auto msg = NewI2NPMessage (); // same format as I2NP message block - msg->len = msg->offset + len + 7; + msg->len = msg->offset + len + 7; memcpy (msg->GetNTCP2Header (), buf, len); std::shared_ptr m; bool found = false; auto it = m_IncompleteMessages.find (msgID); if (it != m_IncompleteMessages.end ()) { - found = true; + found = true; m = it->second; - } + } else - { + { m = std::make_shared(); m_IncompleteMessages.emplace (msgID, m); - } + } m->msg = msg; m->nextFragmentNum = 1; m->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch (); @@ -1100,8 +1100,8 @@ namespace transport m->msg->FromNTCP2 (); m_Handler.PutNextMessage (std::move (m->msg)); m_IncompleteMessages.erase (it); - } - } + } + } void SSU2Session::HandleFollowOnFragment (const uint8_t * buf, size_t len) { @@ -1129,20 +1129,20 @@ namespace transport { m_Handler.PutNextMessage (std::move (it->second->msg)); m_IncompleteMessages.erase (it); - } - else + } + else it->second->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch (); - } + } return; } - } + } else { // follow-on fragment before first fragment auto msg = std::make_shared (); msg->nextFragmentNum = 0; it = m_IncompleteMessages.emplace (msgID, msg).first; - } + } // insert out of sequence fragment auto fragment = std::make_shared (); memcpy (fragment->buf, buf + 5, len -5); @@ -1150,7 +1150,7 @@ namespace transport fragment->isLast = isLast; it->second->outOfSequenceFragments.emplace (fragmentNum, fragment); it->second->lastFragmentInsertTime = i2p::util::GetSecondsSinceEpoch (); - } + } bool SSU2Session::ConcatOutOfSequenceFragments (std::shared_ptr m) { @@ -1167,21 +1167,21 @@ namespace transport else break; return isLast; - } + } void SSU2Session::HandleRelayRequest (const uint8_t * buf, size_t len) { // we are Bob uint32_t relayTag = bufbe32toh (buf + 5); // relay tag auto session = m_Server.FindRelaySession (relayTag); - if (!session) + if (!session) { LogPrint (eLogWarning, "SSU2: Session with relay tag ", relayTag, " not found"); return; // TODO: send relay response - } + } session->m_RelaySessions.emplace (bufbe32toh (buf + 1), // nonce - std::make_pair (shared_from_this (), i2p::util::GetSecondsSinceEpoch ()) ); - + std::make_pair (shared_from_this (), i2p::util::GetSecondsSinceEpoch ()) ); + // send relay intro to Charlie auto r = i2p::data::netdb.FindRouter (GetRemoteIdentity ()->GetIdentHash ()); // Alice's RI uint8_t payload[SSU2_MAX_PAYLOAD_SIZE]; @@ -1192,7 +1192,7 @@ namespace transport if (payloadSize < SSU2_MAX_PAYLOAD_SIZE) payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize); session->SendData (payload, payloadSize); - } + } void SSU2Session::HandleRelayIntro (const uint8_t * buf, size_t len) { @@ -1200,21 +1200,21 @@ namespace transport auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice if (!r) { - LogPrint (eLogError, "SSU2: RelayIntro unknown router to introduce"); + LogPrint (eLogError, "SSU2: RelayIntro unknown router to introduce"); return; - } + } SignedData s; s.Insert ((const uint8_t *)"RelayRequestData", 16); // prologue s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash s.Insert (i2p::context.GetIdentHash (), 32); // chash s.Insert (buf + 33, 14); // nonce, relay tag, timestamp, ver, asz uint8_t asz = buf[46]; - s.Insert (buf + 47, asz); // Alice Port, Alice IP + s.Insert (buf + 47, asz); // Alice Port, Alice IP if (!s.Verify (r->GetIdentity (), buf + 47 + asz)) { LogPrint (eLogWarning, "SSU2: RelayIntro signature verification failed"); return; // TODO: send relay response - } + } // send relay response to Bob uint8_t payload[SSU2_MAX_PAYLOAD_SIZE]; @@ -1228,13 +1228,13 @@ namespace transport { auto r = i2p::data::netdb.FindRouter (buf + 1); // Alice if (r) - { + { auto addr = ep.address ().is_v6 () ? r->GetSSU2V6Address () : r->GetSSU2V4Address (); if (addr) SendHolePunch (bufbe32toh (buf + 33), ep, addr->i); - } - } - } + } + } + } void SSU2Session::HandleRelayResponse (const uint8_t * buf, size_t len) { @@ -1249,34 +1249,34 @@ namespace transport { // we are Alice, message from Bob if (!buf[1]) // status code accepted? - { + { // verify signature - uint8_t csz = buf[11]; + uint8_t csz = buf[11]; SignedData s; s.Insert ((const uint8_t *)"RelayAgreementOK", 16); // prologue s.Insert (GetRemoteIdentity ()->GetIdentHash (), 32); // bhash - s.Insert (buf + 2, 10 + csz); // nonce, timestamp, ver, csz and Charlie's endpoint + s.Insert (buf + 2, 10 + csz); // nonce, timestamp, ver, csz and Charlie's endpoint if (s.Verify (it->second.first->GetRemoteIdentity (), buf + 12 + csz)) - { - // update Charlie's endpoint and connect + { + // update Charlie's endpoint and connect if (it->second.first->m_State == eSSU2SessionStateIntroduced && - ExtractEndpoint (buf + 12, csz, it->second.first->m_RemoteEndpoint)) - { + ExtractEndpoint (buf + 12, csz, it->second.first->m_RemoteEndpoint)) + { it->second.first->m_State = eSSU2SessionStateUnknown; it->second.first->Connect (); - } - } + } + } else LogPrint (eLogWarning, "SSU2: RelayResponse signature verification failed"); } else LogPrint (eLogWarning, "SSU2: RelayResponse status code=", (int)buf[1]); - } + } m_RelaySessions.erase (it); } else - LogPrint (eLogWarning, "SSU2: RelayResponse unknown nonce ", bufbe32toh (buf + 2)); - } + LogPrint (eLogWarning, "SSU2: RelayResponse unknown nonce ", bufbe32toh (buf + 2)); + } void SSU2Session::HandlePeerTest (const uint8_t * buf, size_t len) { @@ -1287,8 +1287,8 @@ namespace transport break; case 2: // Charlie from Bob break; - case 3: // Bob from Charlie - { + case 3: // Bob from Charlie + { auto it = m_PeerTests.find (nonce); if (it != m_PeerTests.end () && it->second.first) { @@ -1297,22 +1297,22 @@ namespace transport if (payloadSize < SSU2_MAX_PAYLOAD_SIZE) payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MAX_PAYLOAD_SIZE - payloadSize); it->second.first->SendData (payload, payloadSize); - } + } break; } case 4: // Alice from Bob - break; + break; case 5: // Alice from Chralie 1 break; case 6: // Chralie from Alice - break; + break; case 7: // Alice from Charlie 2 break; default: LogPrint (eLogWarning, "SSU2: PeerTest unexpected msg num ", buf[0]); - } - } - + } + } + bool SSU2Session::ExtractEndpoint (const uint8_t * buf, size_t size, boost::asio::ip::udp::endpoint& ep) { if (size < 2) return false; @@ -1329,11 +1329,11 @@ namespace transport memcpy (bytes.data (), buf + 2, 16); ep = boost::asio::ip::udp::endpoint (boost::asio::ip::address_v6 (bytes), port); } - else - { + else + { LogPrint (eLogWarning, "SSU2: Address size ", int(size), " is not supported"); return false; - } + } return true; } @@ -1346,21 +1346,21 @@ namespace transport { memcpy (buf + 2, ep.address ().to_v4 ().to_bytes ().data (), 4); size = 6; - } + } else if (ep.address ().is_v6 ()) { if (len < 18) return 0; memcpy (buf + 2, ep.address ().to_v6 ().to_bytes ().data (), 16); size = 18; - } + } else { LogPrint (eLogWarning, "SSU2: Wrong address type ", ep.address ().to_string ()); return 0; } return size; - } - + } + size_t SSU2Session::CreateAddressBlock (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep) { if (len < 9) return 0; @@ -1368,8 +1368,8 @@ namespace transport size_t size = CreateEndpoint (buf + 3, len - 3, ep); if (!size) return 0; htobe16buf (buf + 1, size); - return size + 3; - } + return size + 3; + } size_t SSU2Session::CreateRouterInfoBlock (uint8_t * buf, size_t len, std::shared_ptr r) { @@ -1377,22 +1377,22 @@ namespace transport buf[0] = eSSU2BlkRouterInfo; size_t size = r->GetBufferLen (); if (size + 5 < len) - { + { memcpy (buf + 5, r->GetBuffer (), size); buf[3] = 0; // flag - } - else - { + } + else + { i2p::data::GzipDeflator deflator; size = deflator.Deflate (r->GetBuffer (), r->GetBufferLen (), buf + 5, len - 5); if (!size) return 0; // doesn't fit buf[3] = SSU2_ROUTER_INFO_FLAG_GZIP; // flag - } + } htobe16buf (buf + 1, size + 2); // size buf[4] = 1; // frag return size + 5; - } - + } + size_t SSU2Session::CreateAckBlock (uint8_t * buf, size_t len) { if (len < 8) return 0; @@ -1412,16 +1412,16 @@ namespace transport { acnt++; it++; - } - // ranges + } + // ranges uint32_t lastNum = ackThrough - acnt; it++; while (it != m_OutOfSequencePackets.rend () && lastNum > m_ReceivePacketNum && numRanges < 8) { if (lastNum - (*it) < 255) - { + { buf[7 + numRanges*2] = lastNum - (*it); // NACKs - lastNum = *it; + lastNum = *it; uint8_t numAcks = 0; while (it != m_OutOfSequencePackets.rend () && numAcks < 255 && lastNum > m_ReceivePacketNum && *it == lastNum - 1) { @@ -1434,13 +1434,13 @@ namespace transport } else break; - } - } - } + } + } + } buf[7] = acnt; // acnt htobe16buf (buf + 1, 5 + numRanges*2); return 8; - } + } size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize) { @@ -1449,15 +1449,15 @@ namespace transport if (paddingSize > len) paddingSize = len; else if (paddingSize < minSize) paddingSize = minSize; if (paddingSize) - { + { buf[0] = eSSU2BlkPadding; htobe16buf (buf + 1, paddingSize); memset (buf + 3, 0, paddingSize); - } + } else return 0; return paddingSize + 3; - } + } size_t SSU2Session::CreateI2NPBlock (uint8_t * buf, size_t len, std::shared_ptr&& msg) { @@ -1469,7 +1469,7 @@ namespace transport htobe16buf (buf + 1, msgLen); // size memcpy (buf + 3, msgBuf, msgLen); return msgLen + 3; - } + } size_t SSU2Session::CreateFirstFragmentBlock (uint8_t * buf, size_t len, std::shared_ptr msg) { @@ -1484,18 +1484,18 @@ namespace transport memcpy (buf + 3, msgBuf, msgLen); msg->offset = (msgBuf - buf) + msgLen; return msgLen + 3; - } + } size_t SSU2Session::CreateFollowOnFragmentBlock (uint8_t * buf, size_t len, std::shared_ptr msg, uint8_t& fragmentNum, uint32_t msgID) - { - if (len < 8) return 0; + { + if (len < 8) return 0; bool isLast = true; auto msgLen = msg->len - msg->offset; - if (msgLen + 8 > len) + if (msgLen + 8 > len) { msgLen = len - 8; isLast = false; - } + } buf[0] = eSSU2BlkFollowOnFragment; htobe16buf (buf + 1, msgLen); // size fragmentNum++; @@ -1517,13 +1517,13 @@ namespace transport memcpy (buf + 4, GetRemoteIdentity ()->GetIdentHash (), 32); // Alice router hash memcpy (buf + 36, introData, introDataLen); return payloadSize + 3; - } + } size_t SSU2Session::CreateRelayResponseBlock (uint8_t * buf, size_t len, uint32_t nonce) { buf[0] = eSSU2BlkRelayResponse; buf[3] = 0; // flag - buf[4] = 0; // code, accept + buf[4] = 0; // code, accept htobe32buf (buf + 5, nonce); // nonce htobe32buf (buf + 9, i2p::util::GetSecondsSinceEpoch ()); // timestamp buf[13] = 2; // ver @@ -1539,9 +1539,9 @@ namespace transport size_t payloadSize = 12 + csz + i2p::context.GetIdentity ()->GetSignatureLen (); htobe16buf (buf + 1, payloadSize); // size return payloadSize + 3; - } + } - size_t SSU2Session::CreatePeerTestBlock (uint8_t * buf, size_t len, uint8_t msg, + size_t SSU2Session::CreatePeerTestBlock (uint8_t * buf, size_t len, uint8_t msg, const uint8_t * routerHash, const uint8_t * signedData, size_t signedDataLen) { buf[0] = eSSU2BlkPeerTest; @@ -1555,7 +1555,7 @@ namespace transport memcpy (buf + 38, signedData, signedDataLen); return payloadSize + 3; } - + std::shared_ptr SSU2Session::ExtractRouterInfo (const uint8_t * buf, size_t size) { if (size < 2) return nullptr; @@ -1570,12 +1570,12 @@ namespace transport ri = std::make_shared(uncompressed, uncompressedSize); else LogPrint (eLogInfo, "SSU2: RouterInfo decompression failed ", uncompressedSize); - } + } else ri = std::make_shared(buf + 2, size - 2); return ri; - } - + } + void SSU2Session::CreateNonce (uint64_t seqn, uint8_t * nonce) { memset (nonce, 0, 4); @@ -1584,7 +1584,7 @@ namespace transport bool SSU2Session::UpdateReceivePacketNum (uint32_t packetNum) { - if (packetNum <= m_ReceivePacketNum) return false; // duplicate + if (packetNum <= m_ReceivePacketNum) return false; // duplicate if (packetNum == m_ReceivePacketNum + 1) { for (auto it = m_OutOfSequencePackets.begin (); it != m_OutOfSequencePackets.end ();) @@ -1593,24 +1593,24 @@ namespace transport { packetNum++; it = m_OutOfSequencePackets.erase (it); - } + } else break; - } + } m_ReceivePacketNum = packetNum; - } + } else m_OutOfSequencePackets.insert (packetNum); return true; - } - + } + void SSU2Session::SendQuickAck () { uint8_t payload[SSU2_MTU]; size_t payloadSize = CreateAckBlock (payload, SSU2_MTU); payloadSize += CreatePaddingBlock (payload + payloadSize, SSU2_MTU - payloadSize); SendData (payload, payloadSize); - } + } void SSU2Session::SendTermination () { @@ -1621,7 +1621,7 @@ namespace transport memset (payload + 3, 0, 9); payloadSize += CreatePaddingBlock (payload + payloadSize, 32 - payloadSize); SendData (payload, payloadSize); - } + } void SSU2Session::CleanUp (uint64_t ts) { @@ -1639,7 +1639,7 @@ namespace transport { m_ReceivePacketNum = *m_OutOfSequencePackets.rbegin (); m_OutOfSequencePackets.clear (); - } + } for (auto it = m_RelaySessions.begin (); it != m_RelaySessions.end ();) { if (ts > it->second.second + SSU2_RELAY_NONCE_EXPIRATION_TIMEOUT) @@ -1649,7 +1649,7 @@ namespace transport } else ++it; - } + } for (auto it = m_PeerTests.begin (); it != m_PeerTests.end ();) { if (ts > it->second.second + SSU2_PEER_TEST_EXPIRATION_TIMEOUT) @@ -1659,20 +1659,20 @@ namespace transport } else ++it; - } + } } void SSU2Session::FlushData () { - bool sent = SendQueue (); // if we have something to send + bool sent = SendQueue (); // if we have something to send if (m_IsDataReceived) - { + { if (!sent) SendQuickAck (); m_Handler.Flush (); m_IsDataReceived = false; - } + } } - + SSU2Server::SSU2Server (): RunnableServiceWithWork ("SSU2"), m_ReceiveService ("SSU2r"), m_SocketV4 (m_ReceiveService.GetService ()), m_SocketV6 (m_ReceiveService.GetService ()), @@ -1701,31 +1701,31 @@ namespace transport { uint16_t p; i2p::config::GetOption ("port", p); if (p) port = p; - } - } + } + } if (port) - { + { if (address->IsV4 ()) { found = true; OpenSocket (boost::asio::ip::udp::endpoint (boost::asio::ip::udp::v4(), port)); m_ReceiveService.GetService ().post( - [this]() - { + [this]() + { Receive (m_SocketV4); }); - } + } if (address->IsV6 ()) - { + { found = true; OpenSocket (boost::asio::ip::udp::endpoint (boost::asio::ip::udp::v6(), port)); m_ReceiveService.GetService ().post( - [this]() - { + [this]() + { Receive (m_SocketV6); }); - } - } + } + } else LogPrint (eLogError, "SSU2: Can't start server because port not specified"); } @@ -1733,19 +1733,19 @@ namespace transport if (found) m_ReceiveService.Start (); ScheduleTermination (); - } + } } - + void SSU2Server::Stop () { if (context.SupportsV4 () || context.SupportsV6 ()) m_ReceiveService.Stop (); - + if (IsRunning ()) m_TerminationTimer.cancel (); - + StopIOService (); - } + } boost::asio::ip::udp::socket& SSU2Server::OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint) { @@ -1762,12 +1762,12 @@ namespace transport } catch (std::exception& ex ) { - LogPrint (eLogError, "SSU2: Failed to bind to ", localEndpoint, ": ", ex.what()); + LogPrint (eLogError, "SSU2: Failed to bind to ", localEndpoint, ": ", ex.what()); ThrowFatal ("Unable to start SSU2 transport on ", localEndpoint, ": ", ex.what ()); } return socket; } - + void SSU2Server::Receive (boost::asio::ip::udp::socket& socket) { Packet * packet = m_PacketsPool.AcquireMt (); @@ -1775,7 +1775,7 @@ namespace transport std::bind (&SSU2Server::HandleReceivedFrom, this, std::placeholders::_1, std::placeholders::_2, packet, std::ref (socket))); } - void SSU2Server::HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred, + void SSU2Server::HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred, Packet * packet, boost::asio::ip::udp::socket& socket) { if (!ecode) @@ -1808,7 +1808,7 @@ namespace transport } } GetService ().post (std::bind (&SSU2Server::HandleReceivedPackets, this, packets)); - } + } else GetService ().post (std::bind (&SSU2Server::HandleReceivedPacket, this, packet)); Receive (socket); @@ -1834,42 +1834,42 @@ namespace transport ProcessNextPacket (packet->buf, packet->len, packet->from); m_PacketsPool.ReleaseMt (packet); if (m_LastSession) m_LastSession->FlushData (); - } - } - + } + } + void SSU2Server::HandleReceivedPackets (std::vector packets) { for (auto& packet: packets) ProcessNextPacket (packet->buf, packet->len, packet->from); m_PacketsPool.ReleaseMt (packets); if (m_LastSession) m_LastSession->FlushData (); - } - + } + void SSU2Server::AddSession (std::shared_ptr session) { if (session) - { + { m_Sessions.emplace (session->GetConnID (), session); AddSessionByRouterHash (session); - } - } + } + } void SSU2Server::RemoveSession (uint64_t connID) { auto it = m_Sessions.find (connID); if (it != m_Sessions.end ()) - { + { auto ident = it->second->GetRemoteIdentity (); if (ident) m_SessionsByRouterHash.erase (ident->GetIdentHash ()); m_Sessions.erase (it); - } - } + } + } - void SSU2Server::AddSessionByRouterHash (std::shared_ptr session) + void SSU2Server::AddSessionByRouterHash (std::shared_ptr session) { if (session) - { + { auto ident = session->GetRemoteIdentity (); if (ident) { @@ -1882,11 +1882,11 @@ namespace transport GetService ().post (std::bind (&SSU2Session::Terminate, ret.first->second)); // update session ret.first->second = session; - } - } - } - } - + } + } + } + } + void SSU2Server::AddPendingOutgoingSession (std::shared_ptr session) { if (session) @@ -1897,11 +1897,11 @@ namespace transport { m_Relays.emplace (tag, relay); } - + void SSU2Server::RemoveRelay (uint32_t tag) { m_Relays.erase (tag); - } + } std::shared_ptr SSU2Server::FindRelaySession (uint32_t tag) { @@ -1915,7 +1915,7 @@ namespace transport } return nullptr; } - + void SSU2Server::ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint) { if (len < 24) return; @@ -1940,7 +1940,7 @@ namespace transport break; case eSSU2SessionStateUnknown: m_LastSession->ProcessSessionConfirmed (buf, len); - break; + break; case eSSU2SessionStateIntroduced: m_LastSession->SetRemoteEndpoint (senderEndpoint); m_LastSession->ProcessHolePunch (buf, len); @@ -1948,12 +1948,12 @@ namespace transport case eSSU2SessionStatePeerTest: m_LastSession->SetRemoteEndpoint (senderEndpoint); m_LastSession->ProcessPeerTest (buf, len); - break; + break; default: LogPrint (eLogWarning, "SSU2: Invalid session state ", (int)m_LastSession->GetState ()); - } - } - else + } + } + else { // check pending sessions if it's SessionCreated or Retry auto it1 = m_PendingOutgoingSessions.find (senderEndpoint); @@ -1970,11 +1970,11 @@ namespace transport auto session = std::make_shared (*this); session->SetRemoteEndpoint (senderEndpoint); session->ProcessFirstIncomingMessage (connID, buf, len); - } - } - } + } + } + } - void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen, + void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to) { std::vector bufs @@ -1985,15 +1985,15 @@ namespace transport boost::system::error_code ec; if (to.address ().is_v6 ()) m_SocketV6.send_to (bufs, to, 0, ec); - else + else m_SocketV4.send_to (bufs, to, 0, ec); - if (!ec) + if (!ec) i2p::transport::transports.UpdateSentBytes (headerLen + payloadLen); else LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to); - } - - void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen, + } + + void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen, const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to) { std::vector bufs @@ -2005,34 +2005,34 @@ namespace transport boost::system::error_code ec; if (to.address ().is_v6 ()) m_SocketV6.send_to (bufs, to, 0, ec); - else + else m_SocketV4.send_to (bufs, to, 0, ec); - + if (!ec) i2p::transport::transports.UpdateSentBytes (headerLen + headerXLen + payloadLen); - else + else LogPrint (eLogError, "SSU2: Send exception: ", ec.message (), " to ", to); } - + bool SSU2Server::CreateSession (std::shared_ptr router, std::shared_ptr address) { if (router && address) { - if (address->UsesIntroducer ()) + if (address->UsesIntroducer ()) GetService ().post (std::bind (&SSU2Server::ConnectThroughIntroducer, this, router, address)); - else + else GetService ().post ( [this, router, address]() { auto session = std::make_shared (*this, router, address); session->Connect (); - }); - } + }); + } else return false; return true; - } + } void SSU2Server::ConnectThroughIntroducer (std::shared_ptr router, std::shared_ptr address) @@ -2047,8 +2047,8 @@ namespace transport { it1->second->Introduce (session, it.iTag); return; - } - } + } + } // we have to start a new session to an introducer std::shared_ptr r; uint32_t relayTag = 0; @@ -2059,13 +2059,13 @@ namespace transport { relayTag = it.iTag; if (relayTag) break; - } - } + } + } if (r) { if (relayTag) - { - // introducer and tag found connect to it through SSU2 + { + // introducer and tag found connect to it through SSU2 auto addr = address->IsV6 () ? r->GetSSU2V6Address () : r->GetSSU2V4Address (); if (addr) { @@ -2074,19 +2074,19 @@ namespace transport [session, s, relayTag]() { s->Introduce (session, relayTag); - }); + }); s->Connect (); - } - } + } + } } else { // introducers not found, try to request them for (auto& it: address->ssu->introducers) i2p::data::netdb.RequestDestination (it.iKey); - } - } - + } + } + void SSU2Server::ScheduleTermination () { m_TerminationTimer.expires_from_now (boost::posix_time::seconds(SSU2_TERMINATION_CHECK_TIMEOUT)); @@ -2104,7 +2104,7 @@ namespace transport if (it->second->IsTerminationTimeoutExpired (ts)) { //it->second->Terminate (); - it = m_PendingOutgoingSessions.erase (it); + it = m_PendingOutgoingSessions.erase (it); } else it++; @@ -2117,20 +2117,20 @@ namespace transport if (it->second->IsEstablished ()) it->second->TerminateByTimeout (); if (it->second == m_LastSession) - m_LastSession = nullptr; - it = m_Sessions.erase (it); + m_LastSession = nullptr; + it = m_Sessions.erase (it); } else { it->second->CleanUp (ts); it++; - } + } } for (auto it = m_IncomingTokens.begin (); it != m_IncomingTokens.end (); ) { if (ts > it->second.second) - it = m_IncomingTokens.erase (it); + it = m_IncomingTokens.erase (it); else it++; } @@ -2138,11 +2138,11 @@ namespace transport for (auto it = m_OutgoingTokens.begin (); it != m_OutgoingTokens.end (); ) { if (ts > it->second.second) - it = m_OutgoingTokens.erase (it); + it = m_OutgoingTokens.erase (it); else it++; } - + ScheduleTermination (); } } @@ -2153,7 +2153,7 @@ namespace transport m_ResendTimer.async_wait (std::bind (&SSU2Server::HandleResendTimer, this, std::placeholders::_1)); } - + void SSU2Server::HandleResendTimer (const boost::system::error_code& ecode) { if (ecode != boost::asio::error::operation_aborted) @@ -2162,21 +2162,21 @@ namespace transport for (auto it: m_Sessions) it.second->Resend (ts); ScheduleResend (); - } - } - + } + } + void SSU2Server::UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp) { m_OutgoingTokens[ep] = {token, exp}; - } + } - uint64_t SSU2Server::FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const + uint64_t SSU2Server::FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const { auto it = m_OutgoingTokens.find (ep); if (it != m_OutgoingTokens.end ()) return it->second.first; return 0; - } + } uint64_t SSU2Server::GetIncomingToken (const boost::asio::ip::udp::endpoint& ep) { @@ -2187,6 +2187,6 @@ namespace transport RAND_bytes ((uint8_t *)&token, 8); m_IncomingTokens.emplace (ep, std::make_pair (token, i2p::util::GetSecondsSinceEpoch () + SSU2_TOKEN_EXPIRATION_TIMEOUT)); return token; - } + } } } diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index 1cca7d82..cce60549 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -35,9 +35,9 @@ namespace transport const size_t SSU2_MAX_PAYLOAD_SIZE = SSU2_MTU - 32; const int SSU2_RESEND_INTERVAL = 3; // in seconds const int SSU2_MAX_NUM_RESENDS = 5; - const int SSU2_INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds + const int SSU2_INCOMPLETE_MESSAGES_CLEANUP_TIMEOUT = 30; // in seconds const size_t SSU2_MAX_WINDOW_SIZE = 128; // in packets - + enum SSU2MessageType { eSSU2SessionRequest = 0, @@ -94,17 +94,17 @@ namespace transport size_t len; bool isLast; }; - + std::shared_ptr msg; int nextFragmentNum; uint32_t lastFragmentInsertTime; // in seconds - std::map > outOfSequenceFragments; - }; - + std::map > outOfSequenceFragments; + }; + // RouterInfo flags const uint8_t SSU2_ROUTER_INFO_FLAG_REQUEST_FLOOD = 0x01; - const uint8_t SSU2_ROUTER_INFO_FLAG_GZIP = 0x02; - + const uint8_t SSU2_ROUTER_INFO_FLAG_GZIP = 0x02; + class SSU2Server; class SSU2Session: public TransportSession, public std::enable_shared_from_this { @@ -124,20 +124,20 @@ namespace transport struct SentPacket { uint8_t payload[SSU2_MAX_PAYLOAD_SIZE]; - size_t payloadSize = 0; + size_t payloadSize = 0; uint32_t nextResendTime; // in seconds int numResends = 0; - }; + }; struct SessionConfirmedFragment { Header header; uint8_t payload[SSU2_MAX_PAYLOAD_SIZE]; size_t payloadSize; - }; - + }; + typedef std::function OnEstablished; - + public: SSU2Session (SSU2Server& server, std::shared_ptr in_RemoteRouter = nullptr, @@ -147,7 +147,7 @@ namespace transport void SetRemoteEndpoint (const boost::asio::ip::udp::endpoint& ep) { m_RemoteEndpoint = ep; }; const boost::asio::ip::udp::endpoint& GetRemoteEndpoint () const { return m_RemoteEndpoint; }; void SetOnEstablished (OnEstablished e) { m_OnEstablished = e; }; - + void Connect (); bool Introduce (std::shared_ptr session, uint32_t relayTag); void Terminate (); @@ -161,7 +161,7 @@ namespace transport uint64_t GetConnID () const { return m_SourceConnID; }; SSU2SessionState GetState () const { return m_State; }; void SetState (SSU2SessionState state) { m_State = state; }; - + bool ProcessFirstIncomingMessage (uint64_t connID, uint8_t * buf, size_t len); bool ProcessSessionCreated (uint8_t * buf, size_t len); bool ProcessSessionConfirmed (uint8_t * buf, size_t len); @@ -169,17 +169,17 @@ namespace transport bool ProcessHolePunch (uint8_t * buf, size_t len); bool ProcessPeerTest (uint8_t * buf, size_t len); void ProcessData (uint8_t * buf, size_t len); - + private: void Established (); void PostI2NPMessages (std::vector > msgs); bool SendQueue (); void SendFragmentedMessage (std::shared_ptr msg); - + void ProcessSessionRequest (Header& header, uint8_t * buf, size_t len); void ProcessTokenRequest (Header& header, uint8_t * buf, size_t len); - + void SendSessionRequest (uint64_t token = 0); void SendSessionCreated (const uint8_t * X); void SendSessionConfirmed (const uint8_t * Y); @@ -190,8 +190,8 @@ namespace transport void SendQuickAck (); void SendTermination (); void SendHolePunch (uint32_t nonce, const boost::asio::ip::udp::endpoint& ep, const uint8_t * introKey); - - void HandlePayload (const uint8_t * buf, size_t len); + + void HandlePayload (const uint8_t * buf, size_t len); void HandleAck (const uint8_t * buf, size_t len); void HandleAckRange (uint32_t firstPacketNum, uint32_t lastPacketNum); bool ExtractEndpoint (const uint8_t * buf, size_t size, boost::asio::ip::udp::endpoint& ep); @@ -206,7 +206,7 @@ namespace transport void HandleRelayIntro (const uint8_t * buf, size_t len); void HandleRelayResponse (const uint8_t * buf, size_t len); void HandlePeerTest (const uint8_t * buf, size_t len); - + size_t CreateAddressBlock (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& ep); size_t CreateRouterInfoBlock (uint8_t * buf, size_t len, std::shared_ptr r); size_t CreateAckBlock (uint8_t * buf, size_t len); @@ -217,7 +217,7 @@ namespace transport size_t CreateRelayIntroBlock (uint8_t * buf, size_t len, const uint8_t * introData, size_t introDataLen); size_t CreateRelayResponseBlock (uint8_t * buf, size_t len, uint32_t nonce); // Charlie size_t CreatePeerTestBlock (uint8_t * buf, size_t len, uint8_t msg, const uint8_t * routerHash, const uint8_t * signedData, size_t signedDataLen); - + private: SSU2Server& m_Server; @@ -228,12 +228,12 @@ namespace transport boost::asio::ip::udp::endpoint m_RemoteEndpoint; uint64_t m_DestConnID, m_SourceConnID; SSU2SessionState m_State; - uint8_t m_KeyDataSend[64], m_KeyDataReceive[64]; + uint8_t m_KeyDataSend[64], m_KeyDataReceive[64]; uint32_t m_SendPacketNum, m_ReceivePacketNum; std::set m_OutOfSequencePackets; // packet nums > receive packet num std::map > m_SentPackets; // packetNum -> packet std::map > m_IncompleteMessages; // I2NP - std::map, uint64_t > > m_RelaySessions; // nonce->(Alice, timestamp) for Bob or nonce->(Charlie, timestamp) for Alice + std::map, uint64_t > > m_RelaySessions; // nonce->(Alice, timestamp) for Bob or nonce->(Charlie, timestamp) for Alice std::map, uint64_t > > m_PeerTests; // same as for relay sessions std::list > m_SendQueue; i2p::I2NPMessagesHandler m_Handler; @@ -243,14 +243,14 @@ namespace transport OnEstablished m_OnEstablished; // callback from Established }; - class SSU2Server: private i2p::util::RunnableServiceWithWork + class SSU2Server: private i2p::util::RunnableServiceWithWork { struct Packet { - uint8_t buf[SSU2_MTU]; + uint8_t buf[SSU2_MTU]; size_t len; boost::asio::ip::udp::endpoint from; - }; + }; class ReceiveService: public i2p::util::RunnableService { @@ -260,8 +260,8 @@ namespace transport boost::asio::io_service& GetService () { return GetIOService (); }; void Start () { StartIOService (); }; void Stop () { StopIOService (); }; - }; - + }; + public: SSU2Server (); @@ -270,33 +270,33 @@ namespace transport void Start (); void Stop (); boost::asio::io_service& GetService () { return GetIOService (); }; - + void AddSession (std::shared_ptr session); void RemoveSession (uint64_t connID); void AddSessionByRouterHash (std::shared_ptr session); void AddPendingOutgoingSession (std::shared_ptr session); void AddRelay (uint32_t tag, std::shared_ptr relay); - void RemoveRelay (uint32_t tag); + void RemoveRelay (uint32_t tag); std::shared_ptr FindRelaySession (uint32_t tag); - - void Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen, + + void Send (const uint8_t * header, size_t headerLen, const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to); - void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen, + void Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen, const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to); - + bool CreateSession (std::shared_ptr router, std::shared_ptr address); void UpdateOutgoingToken (const boost::asio::ip::udp::endpoint& ep, uint64_t token, uint32_t exp); uint64_t FindOutgoingToken (const boost::asio::ip::udp::endpoint& ep) const; uint64_t GetIncomingToken (const boost::asio::ip::udp::endpoint& ep); - + private: boost::asio::ip::udp::socket& OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint); void Receive (boost::asio::ip::udp::socket& socket); - void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred, + void HandleReceivedFrom (const boost::system::error_code& ecode, size_t bytes_transferred, Packet * packet, boost::asio::ip::udp::socket& socket); void HandleReceivedPacket (Packet * packet); void HandleReceivedPackets (std::vector packets); @@ -310,16 +310,16 @@ namespace transport void ConnectThroughIntroducer (std::shared_ptr router, std::shared_ptr address); - + private: - ReceiveService m_ReceiveService; + ReceiveService m_ReceiveService; boost::asio::ip::udp::socket m_SocketV4, m_SocketV6; std::unordered_map > m_Sessions; std::map > m_SessionsByRouterHash; std::map > m_PendingOutgoingSessions; std::map > m_IncomingTokens, m_OutgoingTokens; // remote endpoint -> (token, expires in seconds) - std::map > m_Relays; // we are introducer, relay tag -> session + std::map > m_Relays; // we are introducer, relay tag -> session i2p::util::MemoryPoolMt m_PacketsPool; boost::asio::deadline_timer m_TerminationTimer, m_ResendTimer; std::shared_ptr m_LastSession; @@ -327,8 +327,8 @@ namespace transport public: // for HTTP/I2PControl - const decltype(m_Sessions)& GetSSU2Sessions () const { return m_Sessions; }; - }; + const decltype(m_Sessions)& GetSSU2Sessions () const { return m_Sessions; }; + }; } } diff --git a/libi2pd/SSUData.cpp b/libi2pd/SSUData.cpp index 1b1b9bab..6365381e 100644 --- a/libi2pd/SSUData.cpp +++ b/libi2pd/SSUData.cpp @@ -171,7 +171,7 @@ namespace transport return; } - // find message with msgID + // find message with msgID auto it = m_IncompleteMessages.find (msgID); if (it == m_IncompleteMessages.end ()) { @@ -318,7 +318,7 @@ namespace transport sentMessage->numResends = 0; } auto& fragments = sentMessage->fragments; - size_t payloadSize = m_PacketSize - sizeof (SSUHeader) - 9; // 9 = flag + #frg(1) + messageID(4) + frag info (3) + size_t payloadSize = m_PacketSize - sizeof (SSUHeader) - 9; // 9 = flag + #frg(1) + messageID(4) + frag info (3) size_t len = msg->GetLength (); uint8_t * msgBuf = msg->GetSSUHeader (); diff --git a/libi2pd/SSUSession.cpp b/libi2pd/SSUSession.cpp index dc9a7ae8..817133e8 100644 --- a/libi2pd/SSUSession.cpp +++ b/libi2pd/SSUSession.cpp @@ -296,8 +296,8 @@ namespace transport { LogPrint (eLogWarning, "SSU: Clock adjusted by ", -offset, " seconds"); i2p::util::AdjustTimeOffset (-offset); - } - } + } + } else if (std::abs (offset) > SSU_CLOCK_SKEW) { LogPrint (eLogError, "SSU: Clock skew detected ", offset, ". Check your clock"); @@ -388,11 +388,11 @@ namespace transport // fill extended options, 3 bytes extended options don't change message size bool isV4 = m_RemoteEndpoint.address ().is_v4 (); if ((isV4 && i2p::context.GetStatus () == eRouterStatusOK) || - (!isV4 && i2p::context.GetStatusV6 () == eRouterStatusOK)) // we don't need relays + (!isV4 && i2p::context.GetStatusV6 () == eRouterStatusOK)) // we don't need relays { // tell out peer to now assign relay tag flag = SSU_HEADER_EXTENDED_OPTIONS_INCLUDED; - *payload = 2; payload++; // 1 byte length + *payload = 2; payload++; // 1 byte length uint16_t flags = 0; // clear EXTENDED_OPTIONS_FLAG_REQUEST_RELAY_TAG htobe16buf (payload, flags); payload += 2; @@ -1020,7 +1020,7 @@ namespace transport for (auto it = m_RelayRequests.begin (); it != m_RelayRequests.end ();) { if (ts > it->second.second + SSU_CONNECT_TIMEOUT) - it = m_RelayRequests.erase (it); + it = m_RelayRequests.erase (it); else ++it; } diff --git a/libi2pd/Signature.cpp b/libi2pd/Signature.cpp index 458a4fd7..ebc188a9 100644 --- a/libi2pd/Signature.cpp +++ b/libi2pd/Signature.cpp @@ -130,7 +130,7 @@ namespace crypto else { size_t l = 64; - uint8_t sig[64]; // temporary buffer for signature. openssl issue #7232 + uint8_t sig[64]; // temporary buffer for signature. openssl issue #7232 EVP_DigestSign (m_MDCtx, sig, &l, buf, len); memcpy (signature, sig, 64); } diff --git a/libi2pd/Streaming.cpp b/libi2pd/Streaming.cpp index c32a75af..af0a359f 100644 --- a/libi2pd/Streaming.cpp +++ b/libi2pd/Streaming.cpp @@ -1249,7 +1249,7 @@ namespace stream return s; } - void StreamingDestination::SendPing (std::shared_ptr remote) + void StreamingDestination::SendPing (std::shared_ptr remote) { auto s = std::make_shared (m_Owner->GetService (), *this, remote, 0); s->SendPing (); @@ -1277,7 +1277,7 @@ namespace stream { m_PacketsPool.CleanUp (); m_I2NPMsgsPool.CleanUp (); - } + } } bool StreamingDestination::DeleteStream (uint32_t recvStreamID) @@ -1287,7 +1287,7 @@ namespace stream return false; auto s = it->second; m_Owner->GetService ().post ([this, s] () - { + { s->Close (); // try to send FIN s->Terminate (false); DeleteStream (s); diff --git a/libi2pd/Timestamp.cpp b/libi2pd/Timestamp.cpp index c8d752a6..99507398 100644 --- a/libi2pd/Timestamp.cpp +++ b/libi2pd/Timestamp.cpp @@ -254,6 +254,6 @@ namespace util void AdjustTimeOffset (int64_t offset) { g_TimeOffset += offset; - } + } } } diff --git a/libi2pd/Timestamp.h b/libi2pd/Timestamp.h index 518cd055..995ea36f 100644 --- a/libi2pd/Timestamp.h +++ b/libi2pd/Timestamp.h @@ -27,7 +27,7 @@ namespace util void GetCurrentDate (char * date); // returns date as YYYYMMDD string, 9 bytes void GetDateString (uint64_t timestamp, char * date); // timestap is seconds since epoch, returns date as YYYYMMDD string, 9 bytes void AdjustTimeOffset (int64_t offset); // in seconds from current - + class NTPTimeSync { public: diff --git a/libi2pd/Transports.cpp b/libi2pd/Transports.cpp index 007d3ffc..8b656561 100644 --- a/libi2pd/Transports.cpp +++ b/libi2pd/Transports.cpp @@ -219,7 +219,7 @@ namespace transport } // create SSU2 server if (enableSSU2) m_SSU2Server = new SSU2Server (); - + // bind to interfaces bool ipv4; i2p::config::GetOption("ipv4", ipv4); if (ipv4) @@ -285,7 +285,7 @@ namespace transport if (m_SSUServer) DetectExternalIP (); } if (m_SSU2Server) m_SSU2Server->Start (); - + m_PeerCleanupTimer->expires_from_now (boost::posix_time::seconds(5*SESSION_CREATION_TIMEOUT)); m_PeerCleanupTimer->async_wait (std::bind (&Transports::HandlePeerCleanupTimer, this, std::placeholders::_1)); @@ -307,14 +307,14 @@ namespace transport delete m_SSUServer; m_SSUServer = nullptr; } - + if (m_SSU2Server) { m_SSU2Server->Stop (); delete m_SSU2Server; m_SSU2Server = nullptr; } - + if (m_NTCP2Server) { m_NTCP2Server->Stop (); @@ -569,7 +569,7 @@ namespace transport } else peer.numAttempts += 2; - } + } LogPrint (eLogInfo, "Transports: No compatble NTCP2 or SSU addresses available"); i2p::data::netdb.SetUnreachable (ident, true); // we are here because all connection attempts failed peer.Done (); @@ -829,18 +829,18 @@ namespace transport } return i2p::data::netdb.FindRouter (ident); } - + void Transports::RestrictRoutesToFamilies(const std::set& families) { std::lock_guard lock(m_FamilyMutex); m_TrustedFamilies.clear(); for (auto fam : families) - { + { boost::to_lower (fam); auto id = i2p::data::netdb.GetFamilies ().GetFamilyID (fam); - if (id) + if (id) m_TrustedFamilies.push_back (id); - } + } } void Transports::RestrictRoutesToRouters(std::set routers) diff --git a/libi2pd/Tunnel.cpp b/libi2pd/Tunnel.cpp index f5812a70..b578f6c1 100644 --- a/libi2pd/Tunnel.cpp +++ b/libi2pd/Tunnel.cpp @@ -108,7 +108,7 @@ namespace tunnel else { if (m_Config->IsShort () && m_Config->GetLastHop () && - m_Config->GetLastHop ()->ident->GetIdentHash () != m_Config->GetLastHop ()->nextIdent) + m_Config->GetLastHop ()->ident->GetIdentHash () != m_Config->GetLastHop ()->nextIdent) { // add garlic key/tag for reply uint8_t key[32]; @@ -403,7 +403,7 @@ namespace tunnel return tunnel; } - std::shared_ptr Tunnels::CreateTunnelPool (int numInboundHops, int numOutboundHops, + std::shared_ptr Tunnels::CreateTunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels, int inboundVariance, int outboundVariance) { auto pool = std::make_shared (numInboundHops, numOutboundHops, numInboundTunnels, numOutboundTunnels, inboundVariance, outboundVariance); @@ -822,7 +822,7 @@ namespace tunnel template std::shared_ptr Tunnels::CreateTunnel (std::shared_ptr config, - std::shared_ptr pool, std::shared_ptr outboundTunnel) + std::shared_ptr pool, std::shared_ptr outboundTunnel) { auto newTunnel = std::make_shared (config); newTunnel->SetTunnelPool (pool); diff --git a/libi2pd/Tunnel.h b/libi2pd/Tunnel.h index c391b512..503b7f9c 100644 --- a/libi2pd/Tunnel.h +++ b/libi2pd/Tunnel.h @@ -215,7 +215,7 @@ namespace tunnel void PostTunnelData (const std::vector >& msgs); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr tunnel); - std::shared_ptr CreateTunnelPool (int numInboundHops, int numOuboundHops, + std::shared_ptr CreateTunnelPool (int numInboundHops, int numOuboundHops, int numInboundTunnels, int numOutboundTunnels, int inboundVariance, int outboundVariance); void DeleteTunnelPool (std::shared_ptr pool); void StopTunnelPool (std::shared_ptr pool); @@ -226,7 +226,7 @@ namespace tunnel template std::shared_ptr CreateTunnel (std::shared_ptr config, - std::shared_ptr pool, std::shared_ptr outboundTunnel = nullptr); + std::shared_ptr pool, std::shared_ptr outboundTunnel = nullptr); template std::shared_ptr GetPendingTunnel (uint32_t replyMsgID, const std::map >& pendingTunnels); diff --git a/libi2pd/TunnelConfig.cpp b/libi2pd/TunnelConfig.cpp index 7801f20f..e19b515d 100644 --- a/libi2pd/TunnelConfig.cpp +++ b/libi2pd/TunnelConfig.cpp @@ -167,7 +167,7 @@ namespace tunnel memset (clearText + SHORT_REQUEST_RECORD_MORE_FLAGS_OFFSET, 0, 2); clearText[SHORT_REQUEST_RECORD_LAYER_ENCRYPTION_TYPE] = 0; // AES htobe32buf (clearText + SHORT_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetMinutesSinceEpoch ()); - htobe32buf (clearText + SHORT_REQUEST_RECORD_REQUEST_EXPIRATION_OFFSET , 600); // +10 minutes + htobe32buf (clearText + SHORT_REQUEST_RECORD_REQUEST_EXPIRATION_OFFSET , 600); // +10 minutes htobe32buf (clearText + SHORT_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID); memset (clearText + SHORT_REQUEST_RECORD_PADDING_OFFSET, 0, SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE - SHORT_REQUEST_RECORD_PADDING_OFFSET); // encrypt diff --git a/libi2pd/TunnelConfig.h b/libi2pd/TunnelConfig.h index 88c3addc..9dcf2c02 100644 --- a/libi2pd/TunnelConfig.h +++ b/libi2pd/TunnelConfig.h @@ -91,7 +91,7 @@ namespace tunnel TunnelConfig (const std::vector >& peers, uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent, bool isShort, - i2p::data::RouterInfo::CompatibleTransports farEndTransports = i2p::data::RouterInfo::eAllTransports): // outbound + i2p::data::RouterInfo::CompatibleTransports farEndTransports = i2p::data::RouterInfo::eAllTransports): // outbound m_IsShort (isShort), m_FarEndTransports (farEndTransports) { CreatePeers (peers); diff --git a/libi2pd/TunnelPool.cpp b/libi2pd/TunnelPool.cpp index 2ad3f14d..204ac294 100644 --- a/libi2pd/TunnelPool.cpp +++ b/libi2pd/TunnelPool.cpp @@ -30,7 +30,7 @@ namespace tunnel { peers.push_back (r->GetRouterIdentity ()); if (r->GetVersion () < i2p::data::NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION || - r->GetRouterIdentity ()->GetCryptoKeyType () != i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD) + r->GetRouterIdentity ()->GetCryptoKeyType () != i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD) isShort = false; } } @@ -40,11 +40,11 @@ namespace tunnel std::reverse (peers.begin (), peers.end ()); } - TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, + TunnelPool::TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels, int inboundVariance, int outboundVariance): m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), - m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance), + m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance), m_IsActive (true), m_CustomPeerSelector(nullptr) { if (m_NumInboundTunnels > TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY) @@ -54,11 +54,11 @@ namespace tunnel if (m_InboundVariance < 0 && m_NumInboundHops + m_InboundVariance <= 0) m_InboundVariance = m_NumInboundHops ? -m_NumInboundHops + 1 : 0; if (m_OutboundVariance < 0 && m_NumOutboundHops + m_OutboundVariance <= 0) - m_OutboundVariance = m_NumOutboundHops ? -m_NumOutboundHops + 1 : 0; + m_OutboundVariance = m_NumOutboundHops ? -m_NumOutboundHops + 1 : 0; if (m_InboundVariance > 0 && m_NumInboundHops + m_InboundVariance > STANDARD_NUM_RECORDS) m_InboundVariance = (m_NumInboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumInboundHops : 0; if (m_OutboundVariance > 0 && m_NumOutboundHops + m_OutboundVariance > STANDARD_NUM_RECORDS) - m_OutboundVariance = (m_NumOutboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumOutboundHops : 0; + m_OutboundVariance = (m_NumOutboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumOutboundHops : 0; m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL; } @@ -227,7 +227,7 @@ namespace tunnel if (it->IsEstablished () && it != excluded && (compatible & it->GetFarEndTransports ())) { if (it->IsSlow () || (HasLatencyRequirement() && it->LatencyIsKnown() && - !it->LatencyFitsRange(m_MinLatency, m_MaxLatency))) + !it->LatencyFitsRange(m_MinLatency, m_MaxLatency))) { i++; skipped = true; continue; @@ -423,7 +423,7 @@ namespace tunnel LogPrint (eLogDebug, "Tunnels: Test of ", msgID, " successful. ", dlt, " milliseconds"); int numHops = 0; if (test.first) numHops += test.first->GetNumHops (); - if (test.second) numHops += test.second->GetNumHops (); + if (test.second) numHops += test.second->GetNumHops (); // restore from test failed state if any if (test.first) { @@ -431,7 +431,7 @@ namespace tunnel test.first->SetState (eTunnelStateEstablished); // update latency uint64_t latency = 0; - if (numHops) latency = dlt*test.first->GetNumHops ()/numHops; + if (numHops) latency = dlt*test.first->GetNumHops ()/numHops; if (!latency) latency = dlt/2; test.first->AddLatencySample(latency); } @@ -441,7 +441,7 @@ namespace tunnel test.second->SetState (eTunnelStateEstablished); // update latency uint64_t latency = 0; - if (numHops) latency = dlt*test.second->GetNumHops ()/numHops; + if (numHops) latency = dlt*test.second->GetNumHops ()/numHops; if (!latency) latency = dlt/2; test.second->AddLatencySample(latency); } @@ -511,7 +511,7 @@ namespace tunnel return false; } if ((i == numHops - 1) && (!hop->IsV4 () || // doesn't support ipv4 - (inbound && !hop->IsReachable ()))) // IBGW is not reachable + (inbound && !hop->IsReachable ()))) // IBGW is not reachable { auto hop1 = nextHop (prevHop, true); if (hop1) hop = hop1; @@ -530,25 +530,25 @@ namespace tunnel // calculate num hops int numHops; if (isInbound) - { - numHops = m_NumInboundHops; + { + numHops = m_NumInboundHops; if (m_InboundVariance) { int offset = rand () % (std::abs (m_InboundVariance) + 1); if (m_InboundVariance < 0) offset = -offset; numHops += offset; - } + } } else - { + { numHops = m_NumOutboundHops; if (m_OutboundVariance) { int offset = rand () % (std::abs (m_OutboundVariance) + 1); if (m_OutboundVariance < 0) offset = -offset; numHops += offset; - } - } + } + } // peers is empty if (numHops <= 0) return true; // custom peer selector in use ? @@ -715,7 +715,7 @@ namespace tunnel auto tunnel = tunnels.CreateInboundTunnel ( m_NumOutboundHops > 0 ? std::make_shared(outboundTunnel->GetInvertedPeers (), outboundTunnel->IsShortBuildMessage ()) : nullptr, - shared_from_this (), outboundTunnel); + shared_from_this (), outboundTunnel); if (tunnel->IsEstablished ()) // zero hops TunnelCreated (tunnel); } diff --git a/libi2pd/TunnelPool.h b/libi2pd/TunnelPool.h index da2a739c..d8c60d69 100644 --- a/libi2pd/TunnelPool.h +++ b/libi2pd/TunnelPool.h @@ -61,7 +61,7 @@ namespace tunnel { public: - TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, + TunnelPool (int numInboundHops, int numOutboundHops, int numInboundTunnels, int numOutboundTunnels, int inboundVariance, int outboundVariance); ~TunnelPool (); @@ -131,7 +131,7 @@ namespace tunnel private: std::shared_ptr m_LocalDestination; - int m_NumInboundHops, m_NumOutboundHops, m_NumInboundTunnels, m_NumOutboundTunnels, + int m_NumInboundHops, m_NumOutboundHops, m_NumInboundTunnels, m_NumOutboundTunnels, m_InboundVariance, m_OutboundVariance; std::shared_ptr > m_ExplicitPeers; mutable std::mutex m_InboundTunnelsMutex; diff --git a/libi2pd/util.cpp b/libi2pd/util.cpp index 2eff35c5..07681dbd 100644 --- a/libi2pd/util.cpp +++ b/libi2pd/util.cpp @@ -517,7 +517,7 @@ namespace net bool IsLocalAddress (const boost::asio::ip::address& addr) { - auto mtu = // TODO: implement better + auto mtu = // TODO: implement better #ifdef _WIN32 GetMTUWindows(addr, 0); #else diff --git a/libi2pd_client/AddressBook.cpp b/libi2pd_client/AddressBook.cpp index d5b6179b..4f80f4c4 100644 --- a/libi2pd_client/AddressBook.cpp +++ b/libi2pd_client/AddressBook.cpp @@ -372,7 +372,7 @@ namespace client { auto addr = FindAddress (address); if (!addr) - LookupAddress (address); // TODO: + LookupAddress (address); // TODO: return addr; } } @@ -494,7 +494,7 @@ namespace client auto it = m_Addresses.find (name); if (it != m_Addresses.end ()) // already exists ? { - if (it->second->IsIdentHash () && it->second->identHash != ident->GetIdentHash () && // address changed? + if (it->second->IsIdentHash () && it->second->identHash != ident->GetIdentHash () && // address changed? ident->GetSigningKeyType () != i2p::data::SIGNING_KEY_TYPE_DSA_SHA1) // don't replace by DSA { it->second->identHash = ident->GetIdentHash (); @@ -858,9 +858,9 @@ namespace client if (!m_LastModified.empty()) req.AddHeader("If-Modified-Since", m_LastModified); /* convert url to relative */ - url.schema = ""; - url.host = ""; - req.uri = url.to_string(); + url.schema = ""; + url.host = ""; + req.uri = url.to_string(); req.version = "HTTP/1.1"; auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, dest_port); std::string request = req.to_string(); diff --git a/libi2pd_client/AddressBook.h b/libi2pd_client/AddressBook.h index 04600792..ded87de5 100644 --- a/libi2pd_client/AddressBook.h +++ b/libi2pd_client/AddressBook.h @@ -116,7 +116,7 @@ namespace client private: std::mutex m_AddressBookMutex; - std::map > m_Addresses; + std::map > m_Addresses; std::map > m_Resolvers; // local destination->resolver std::mutex m_LookupsMutex; std::map m_Lookups; // nonce -> address @@ -162,7 +162,7 @@ namespace client private: std::shared_ptr m_LocalDestination; - std::map m_LocalAddresses; + std::map m_LocalAddresses; }; } } diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index ac42c39a..41b2a494 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -898,7 +898,7 @@ namespace client bool socksproxy; i2p::config::GetOption("socksproxy.enabled", socksproxy); if (socksproxy) { - std::string httpProxyKeys; i2p::config::GetOption("httpproxy.keys", httpProxyKeys); + std::string httpProxyKeys; i2p::config::GetOption("httpproxy.keys", httpProxyKeys); // we still need httpProxyKeys to compare with sockProxyKeys std::string socksProxyKeys; i2p::config::GetOption("socksproxy.keys", socksProxyKeys); std::string socksProxyAddr; i2p::config::GetOption("socksproxy.address", socksProxyAddr); diff --git a/libi2pd_client/I2CP.cpp b/libi2pd_client/I2CP.cpp index bddf80eb..cc0837b7 100644 --- a/libi2pd_client/I2CP.cpp +++ b/libi2pd_client/I2CP.cpp @@ -24,7 +24,7 @@ namespace client { I2CPDestination::I2CPDestination (boost::asio::io_service& service, std::shared_ptr owner, - std::shared_ptr identity, bool isPublic, const std::map& params): + std::shared_ptr identity, bool isPublic, const std::map& params): LeaseSetDestination (service, isPublic, ¶ms), m_Owner (owner), m_Identity (identity), m_EncryptionKeyType (m_Identity->GetCryptoKeyType ()), m_IsCreatingLeaseSet (false), m_LeaseSetCreationTimer (service) @@ -208,12 +208,12 @@ namespace client if (leases.empty ()) leases = remote->GetNonExpiredLeases (true); // with threshold if (!leases.empty ()) - { + { remoteLease = leases[rand () % leases.size ()]; auto leaseRouter = i2p::data::netdb.FindRouter (remoteLease->tunnelGateway); outboundTunnel = GetTunnelPool ()->GetNextOutboundTunnel (nullptr, leaseRouter ? leaseRouter->GetCompatibleTransports (false) : (i2p::data::RouterInfo::CompatibleTransports)i2p::data::RouterInfo::eAllTransports); - } + } if (remoteLease && outboundTunnel) remoteSession->SetSharedRoutingPath (std::make_shared ( i2p::garlic::GarlicRoutingPath{outboundTunnel, remoteLease, 10000, 0, 0})); // 10 secs RTT @@ -453,8 +453,8 @@ namespace client { auto len = m_SendQueue.Get (m_SendBuffer, I2CP_MAX_MESSAGE_LENGTH); boost::asio::async_write (*socket, boost::asio::buffer (m_SendBuffer, len), - boost::asio::transfer_all (),std::bind(&I2CPSession::HandleI2CPMessageSent, - shared_from_this (), std::placeholders::_1, std::placeholders::_2)); + boost::asio::transfer_all (),std::bind(&I2CPSession::HandleI2CPMessageSent, + shared_from_this (), std::placeholders::_1, std::placeholders::_2)); } else m_IsSending = false; @@ -538,7 +538,7 @@ namespace client LogPrint (eLogError, "I2CP: Create session duplicate address ", identity->GetIdentHash ().ToBase32 ()); SendSessionStatusMessage (eI2CPSessionStatusInvalid); // invalid return; - } + } uint16_t optionsSize = bufbe16toh (buf + offset); offset += 2; if (optionsSize > len - offset) @@ -561,16 +561,16 @@ namespace client std::make_shared(m_Owner.GetService (), shared_from_this (), identity, true, params): std::make_shared(shared_from_this (), identity, true, params); if (m_Owner.InsertSession (shared_from_this ())) - { + { SendSessionStatusMessage (eI2CPSessionStatusCreated); // created LogPrint (eLogDebug, "I2CP: Session ", m_SessionID, " created"); m_Destination->Start (); - } + } else { LogPrint (eLogError, "I2CP: Session already exists"); SendSessionStatusMessage (eI2CPSessionStatusRefused); - } + } } else { @@ -712,7 +712,7 @@ namespace client { if (offset + 4 > len) return; uint16_t keyType = bufbe16toh (buf + offset); offset += 2; // encryption type - uint16_t keyLen = bufbe16toh (buf + offset); offset += 2; // private key length + uint16_t keyLen = bufbe16toh (buf + offset); offset += 2; // private key length if (offset + keyLen > len) return; if (keyType == i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD) m_Destination->SetECIESx25519EncryptionPrivateKey (buf + offset); @@ -1030,13 +1030,13 @@ namespace client for (const auto& it: m_Sessions) { if (it.second) - { + { auto dest = it.second->GetDestination (); if (dest && dest->GetIdentHash () == ident) return it.second; - } + } } return nullptr; - } + } } } diff --git a/libi2pd_client/I2CP.h b/libi2pd_client/I2CP.h index f8a45d1e..e38da0ac 100644 --- a/libi2pd_client/I2CP.h +++ b/libi2pd_client/I2CP.h @@ -69,7 +69,7 @@ namespace client eI2CPSessionStatusInvalid = 3, eI2CPSessionStatusRefused = 4 }; - + // params const char I2CP_PARAM_MESSAGE_RELIABILITY[] = "i2cp.messageReliability"; diff --git a/libi2pd_client/I2PTunnel.h b/libi2pd_client/I2PTunnel.h index cf994765..447a345d 100644 --- a/libi2pd_client/I2PTunnel.h +++ b/libi2pd_client/I2PTunnel.h @@ -31,7 +31,7 @@ namespace client const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds const int I2P_TUNNEL_DESTINATION_REQUEST_TIMEOUT = 10; // in seconds // for HTTP tunnels - const char X_I2P_DEST_HASH[] = "X-I2P-DestHash"; // hash in base64 + const char X_I2P_DEST_HASH[] = "X-I2P-DestHash"; // hash in base64 const char X_I2P_DEST_B64[] = "X-I2P-DestB64"; // full address in base64 const char X_I2P_DEST_B32[] = "X-I2P-DestB32"; // .b32.i2p address @@ -43,7 +43,7 @@ namespace client std::shared_ptr leaseSet, int port = 0); // to I2P I2PTunnelConnection (I2PService * owner, std::shared_ptr socket, std::shared_ptr stream); // to I2P using simplified API - I2PTunnelConnection (I2PService * owner, std::shared_ptr stream, std::shared_ptr socket, + I2PTunnelConnection (I2PService * owner, std::shared_ptr stream, std::shared_ptr socket, const boost::asio::ip::tcp::endpoint& target, bool quiet = true); // from I2P ~I2PTunnelConnection (); void I2PConnect (const uint8_t * msg = nullptr, size_t len = 0); diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp index d5c62d79..fc56b2c9 100644 --- a/libi2pd_client/SAM.cpp +++ b/libi2pd_client/SAM.cpp @@ -1078,7 +1078,7 @@ namespace client auto s = shared_from_this (); newSocket->GetSocket ().async_connect (ep, [s, newSocket, stream](const boost::system::error_code& ecode) - { + { if (!ecode) { s->m_Owner.AddSocket (newSocket); @@ -1446,8 +1446,8 @@ namespace client void SAMBridge::SendTo (const std::vector& bufs, const boost::asio::ip::udp::endpoint& ep) { m_DatagramSocket.send_to (bufs, ep); - } - + } + void SAMBridge::ReceiveDatagram () { m_DatagramSocket.async_receive_from ( diff --git a/libi2pd_client/SAM.h b/libi2pd_client/SAM.h index b36f0958..88990d7c 100644 --- a/libi2pd_client/SAM.h +++ b/libi2pd_client/SAM.h @@ -250,7 +250,7 @@ namespace client /** send raw data to remote endpoint from our UDP Socket */ void SendTo (const std::vector& bufs, const boost::asio::ip::udp::endpoint& ep); - + void AddSocket(std::shared_ptr socket); void RemoveSocket(const std::shared_ptr & socket);