From 557f8eac7aa96059270a36358642fbce93ac0478 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 28 Jul 2015 01:58:25 +0200 Subject: [PATCH 1/2] implement uacomment config parameter which can add comments to user agent as per BIP-0014 --- src/net.cpp | 2 +- src/rpcnet.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 3cece520de..42ca69e096 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -445,7 +445,7 @@ void CNode::PushVersion() else LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, - nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector()), nBestHeight, true); + nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector()), nBestHeight, true); } diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index ed903f9fd3..f9c7d3ad30 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -444,7 +444,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) UniValue obj(UniValue::VOBJ); obj.push_back(Pair("version", CLIENT_VERSION)); obj.push_back(Pair("subversion", - FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector()))); + FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector()))); obj.push_back(Pair("protocolversion",PROTOCOL_VERSION)); obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices))); obj.push_back(Pair("timeoffset", GetTimeOffset())); From 7b79cbd722d35b8113d5136b06d4a8e5fd569fc6 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 31 Jul 2015 18:05:42 +0200 Subject: [PATCH 2/2] limit total length of user agent comments Reworked-By: Wladimir J. van der Laan --- src/init.cpp | 7 +++++++ src/main.cpp | 2 +- src/net.cpp | 3 ++- src/net.h | 5 +++++ src/rpcnet.cpp | 3 +-- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index a03571c31a..91519e2935 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1018,6 +1018,13 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) RegisterNodeSignals(GetNodeSignals()); + // format user agent, check total size + strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector()); + if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) { + return InitError(strprintf("Total length of network version string %i exceeds maximum of %i characters. Reduce the number and/or size of uacomments.", + strSubVersion.size(), MAX_SUBVERSION_LENGTH)); + } + if (mapArgs.count("-onlynet")) { std::set nets; BOOST_FOREACH(const std::string& snet, mapMultiArgs["-onlynet"]) { diff --git a/src/main.cpp b/src/main.cpp index d470ba9003..b1f5e3ffd7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3859,7 +3859,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, if (!vRecv.empty()) vRecv >> addrFrom >> nNonce; if (!vRecv.empty()) { - vRecv >> LIMITED_STRING(pfrom->strSubVer, 256); + vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH); pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer); } if (!vRecv.empty()) diff --git a/src/net.cpp b/src/net.cpp index 42ca69e096..080d9bb347 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -83,6 +83,7 @@ CAddrMan addrman; int nMaxConnections = 125; int nWhiteConnections = 0; bool fAddressesInitialized = false; +std::string strSubVersion; vector vNodes; CCriticalSection cs_vNodes; @@ -445,7 +446,7 @@ void CNode::PushVersion() else LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nBestHeight, addrMe.ToString(), id); PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, - nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector()), nBestHeight, true); + nLocalHostNonce, strSubVersion, nBestHeight, true); } diff --git a/src/net.h b/src/net.h index 658f586b12..62f84bbe45 100644 --- a/src/net.h +++ b/src/net.h @@ -46,6 +46,8 @@ static const unsigned int MAX_INV_SZ = 50000; static const unsigned int MAX_ADDR_TO_SEND = 1000; /** Maximum length of incoming protocol messages (no message over 2 MiB is currently acceptable). */ static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 2 * 1024 * 1024; +/** Maximum length of strSubVer in `version` message */ +static const unsigned int MAX_SUBVERSION_LENGTH = 256; /** -listen default */ static const bool DEFAULT_LISTEN = true; /** -upnp default */ @@ -166,6 +168,9 @@ extern CCriticalSection cs_vAddedNodes; extern NodeId nLastNodeId; extern CCriticalSection cs_nLastNodeId; +/** Subversion as sent to the P2P network in `version` messages */ +extern std::string strSubVersion; + struct LocalServiceInfo { int nScore; int nPort; diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index f9c7d3ad30..2528e2cbd5 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -443,8 +443,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) UniValue obj(UniValue::VOBJ); obj.push_back(Pair("version", CLIENT_VERSION)); - obj.push_back(Pair("subversion", - FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, mapMultiArgs.count("-uacomment") ? mapMultiArgs["-uacomment"] : std::vector()))); + obj.push_back(Pair("subversion", strSubVersion)); obj.push_back(Pair("protocolversion",PROTOCOL_VERSION)); obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices))); obj.push_back(Pair("timeoffset", GetTimeOffset()));