{"action":"created","comment":{"url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments/37593066","id":37593066,"diff_hunk":"@@ -776,6 +775,173 @@ void SocketSendData(CNode *pnode)\n \n static list<CNode*> vNodesDisconnected;\n \n+static bool ReverseCompareNodeMinPingTime(CNode *a, CNode *b)\n+{\n+    return a->nMinPingUsecTime > b->nMinPingUsecTime;\n+}\n+\n+static bool ReverseCompareNodeTimeConnected(CNode *a, CNode *b)\n+{\n+    return a->nTimeConnected > b->nTimeConnected;\n+}\n+\n+class CompareNetGroupKeyed\n+{\n+    std::vector<unsigned char> vchSecretKey;\n+public:\n+    CompareNetGroupKeyed()\n+    {\n+        vchSecretKey.resize(32, 0);\n+        GetRandBytes(vchSecretKey.data(), vchSecretKey.size());\n+    }\n+\n+    bool operator()(CNode *a, CNode *b)\n+    {\n+        std::vector<unsigned char> vchGroupA, vchGroupB;\n+        CSHA256 hashA, hashB;\n+        std::vector<unsigned char> vchA(32), vchB(32);\n+\n+        vchGroupA = a->addr.GetGroup();\n+        vchGroupB = b->addr.GetGroup();\n+\n+        hashA.Write(begin_ptr(vchGroupA), vchGroupA.size());\n+        hashB.Write(begin_ptr(vchGroupB), vchGroupB.size());\n+\n+        hashA.Write(begin_ptr(vchSecretKey), vchSecretKey.size());\n+        hashB.Write(begin_ptr(vchSecretKey), vchSecretKey.size());\n+\n+        hashA.Finalize(begin_ptr(vchA));\n+        hashB.Finalize(begin_ptr(vchB));\n+\n+        return vchA < vchB;\n+    }\n+};\n+\n+static bool AttemptToEvictConnection(bool fPreferNewConnection) {\n+    std::vector<CNode*> vEvictionCandidates;\n+    {\n+        LOCK(cs_vNodes);\n+\n+        BOOST_FOREACH(CNode *node, vNodes) {\n+            if (node->fWhitelisted)\n+                continue;\n+            if (!node->fInbound)\n+                continue;\n+            if (node->fDisconnect)\n+                continue;\n+            if (node->addr.IsLocal())\n+                continue;\n+            vEvictionCandidates.push_back(node);\n+        }\n+    }\n+\n+    // Protect connections with certain characteristics\n+    static CompareNetGroupKeyed comparerNetGroupKeyed;\n+    std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), comparerNetGroupKeyed);\n+    vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());\n+\n+    std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);\n+    vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());\n+\n+    std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);\n+    vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(64, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());\n+\n+    if (vEvictionCandidates.empty())\n+        return false;\n+\n+    // Identify CNetAddr with the most connections\n+    CNetAddr naMostConnections;\n+    unsigned int nMostConnections = 0;\n+    std::map<CNetAddr, std::vector<CNode*> > mapAddrCounts;","path":"src/net.cpp","position":89,"original_position":89,"commit_id":"1f1c87438db3e707b2bfc0e35b12e054df5c0661","original_commit_id":"1f1c87438db3e707b2bfc0e35b12e054df5c0661","user":{"login":"TheBlueMatt","id":649246,"avatar_url":"https://avatars.githubusercontent.com/u/649246?v=3","gravatar_id":"","url":"https://api.github.com/users/TheBlueMatt","html_url":"https://github.com/TheBlueMatt","followers_url":"https://api.github.com/users/TheBlueMatt/followers","following_url":"https://api.github.com/users/TheBlueMatt/following{/other_user}","gists_url":"https://api.github.com/users/TheBlueMatt/gists{/gist_id}","starred_url":"https://api.github.com/users/TheBlueMatt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheBlueMatt/subscriptions","organizations_url":"https://api.github.com/users/TheBlueMatt/orgs","repos_url":"https://api.github.com/users/TheBlueMatt/repos","events_url":"https://api.github.com/users/TheBlueMatt/events{/privacy}","received_events_url":"https://api.github.com/users/TheBlueMatt/received_events","type":"User","site_admin":false},"body":"Why not map to a counter and keep one more variable with a node that is in the NetAddr with the most connections?","created_at":"2015-08-20T23:32:57Z","updated_at":"2015-08-20T23:32:57Z","html_url":"https://github.com/bitcoin/bitcoin/pull/6374#discussion_r37593066","pull_request_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374","_links":{"self":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments/37593066"},"html":{"href":"https://github.com/bitcoin/bitcoin/pull/6374#discussion_r37593066"},"pull_request":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374"}}},"pull_request":{"url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374","id":39201297,"html_url":"https://github.com/bitcoin/bitcoin/pull/6374","diff_url":"https://github.com/bitcoin/bitcoin/pull/6374.diff","patch_url":"https://github.com/bitcoin/bitcoin/pull/6374.patch","issue_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/6374","number":6374,"state":"open","locked":false,"title":"Connection slot exhaustion DoS mitigation","user":{"login":"pstratem","id":620611,"avatar_url":"https://avatars.githubusercontent.com/u/620611?v=3","gravatar_id":"","url":"https://api.github.com/users/pstratem","html_url":"https://github.com/pstratem","followers_url":"https://api.github.com/users/pstratem/followers","following_url":"https://api.github.com/users/pstratem/following{/other_user}","gists_url":"https://api.github.com/users/pstratem/gists{/gist_id}","starred_url":"https://api.github.com/users/pstratem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pstratem/subscriptions","organizations_url":"https://api.github.com/users/pstratem/orgs","repos_url":"https://api.github.com/users/pstratem/repos","events_url":"https://api.github.com/users/pstratem/events{/privacy}","received_events_url":"https://api.github.com/users/pstratem/received_events","type":"User","site_admin":false},"body":"Connection slots are a limited resource which can be the target of DoS attacks.\r\n\r\nThis issue was introduced in 2011 by 5a3e82f.\r\n\r\nIn mitigating this issue it is important to take steps to avoid network partitioning.\r\n\r\nI have taken the approach of protecting connections with various properties from eviction.\r\n\r\nOf the nodes still available for eviction the most recently connected node from the CNetAddr with the most connections is selected and evicted.\r\n\r\nThe largest class of protected connections is those which have been connected for the longest time. \r\nThe intent is to maintain the strong bias towards these connections which exists today and thus avoid any risk of network partition.","created_at":"2015-07-04T02:13:33Z","updated_at":"2015-08-20T23:32:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"065d6c027ac6bb0d3348c8cbf0174c0103e9cbe8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374/commits","review_comments_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374/comments","review_comment_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/6374/comments","statuses_url":"https://api.github.com/repos/bitcoin/bitcoin/statuses/1f1c87438db3e707b2bfc0e35b12e054df5c0661","head":{"label":"pstratem:connlimit-fix","ref":"connlimit-fix","sha":"1f1c87438db3e707b2bfc0e35b12e054df5c0661","user":{"login":"pstratem","id":620611,"avatar_url":"https://avatars.githubusercontent.com/u/620611?v=3","gravatar_id":"","url":"https://api.github.com/users/pstratem","html_url":"https://github.com/pstratem","followers_url":"https://api.github.com/users/pstratem/followers","following_url":"https://api.github.com/users/pstratem/following{/other_user}","gists_url":"https://api.github.com/users/pstratem/gists{/gist_id}","starred_url":"https://api.github.com/users/pstratem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pstratem/subscriptions","organizations_url":"https://api.github.com/users/pstratem/orgs","repos_url":"https://api.github.com/users/pstratem/repos","events_url":"https://api.github.com/users/pstratem/events{/privacy}","received_events_url":"https://api.github.com/users/pstratem/received_events","type":"User","site_admin":false},"repo":{"id":10467146,"name":"bitcoin","full_name":"pstratem/bitcoin","owner":{"login":"pstratem","id":620611,"avatar_url":"https://avatars.githubusercontent.com/u/620611?v=3","gravatar_id":"","url":"https://api.github.com/users/pstratem","html_url":"https://github.com/pstratem","followers_url":"https://api.github.com/users/pstratem/followers","following_url":"https://api.github.com/users/pstratem/following{/other_user}","gists_url":"https://api.github.com/users/pstratem/gists{/gist_id}","starred_url":"https://api.github.com/users/pstratem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pstratem/subscriptions","organizations_url":"https://api.github.com/users/pstratem/orgs","repos_url":"https://api.github.com/users/pstratem/repos","events_url":"https://api.github.com/users/pstratem/events{/privacy}","received_events_url":"https://api.github.com/users/pstratem/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/pstratem/bitcoin","description":"Bitcoin integration/staging tree","fork":true,"url":"https://api.github.com/repos/pstratem/bitcoin","forks_url":"https://api.github.com/repos/pstratem/bitcoin/forks","keys_url":"https://api.github.com/repos/pstratem/bitcoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pstratem/bitcoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pstratem/bitcoin/teams","hooks_url":"https://api.github.com/repos/pstratem/bitcoin/hooks","issue_events_url":"https://api.github.com/repos/pstratem/bitcoin/issues/events{/number}","events_url":"https://api.github.com/repos/pstratem/bitcoin/events","assignees_url":"https://api.github.com/repos/pstratem/bitcoin/assignees{/user}","branches_url":"https://api.github.com/repos/pstratem/bitcoin/branches{/branch}","tags_url":"https://api.github.com/repos/pstratem/bitcoin/tags","blobs_url":"https://api.github.com/repos/pstratem/bitcoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pstratem/bitcoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pstratem/bitcoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/pstratem/bitcoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pstratem/bitcoin/statuses/{sha}","languages_url":"https://api.github.com/repos/pstratem/bitcoin/languages","stargazers_url":"https://api.github.com/repos/pstratem/bitcoin/stargazers","contributors_url":"https://api.github.com/repos/pstratem/bitcoin/contributors","subscribers_url":"https://api.github.com/repos/pstratem/bitcoin/subscribers","subscription_url":"https://api.github.com/repos/pstratem/bitcoin/subscription","commits_url":"https://api.github.com/repos/pstratem/bitcoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/pstratem/bitcoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/pstratem/bitcoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/pstratem/bitcoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/pstratem/bitcoin/contents/{+path}","compare_url":"https://api.github.com/repos/pstratem/bitcoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pstratem/bitcoin/merges","archive_url":"https://api.github.com/repos/pstratem/bitcoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pstratem/bitcoin/downloads","issues_url":"https://api.github.com/repos/pstratem/bitcoin/issues{/number}","pulls_url":"https://api.github.com/repos/pstratem/bitcoin/pulls{/number}","milestones_url":"https://api.github.com/repos/pstratem/bitcoin/milestones{/number}","notifications_url":"https://api.github.com/repos/pstratem/bitcoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pstratem/bitcoin/labels{/name}","releases_url":"https://api.github.com/repos/pstratem/bitcoin/releases{/id}","created_at":"2013-06-03T23:33:55Z","updated_at":"2015-04-27T18:46:09Z","pushed_at":"2015-08-19T00:44:43Z","git_url":"git://github.com/pstratem/bitcoin.git","ssh_url":"git@github.com:pstratem/bitcoin.git","clone_url":"https://github.com/pstratem/bitcoin.git","svn_url":"https://github.com/pstratem/bitcoin","homepage":"http://www.bitcoin.org","size":51383,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bitcoin:master","ref":"master","sha":"87f37e259d6deb52ee464edde7aece687eea97a5","user":{"login":"bitcoin","id":528860,"avatar_url":"https://avatars.githubusercontent.com/u/528860?v=3","gravatar_id":"","url":"https://api.github.com/users/bitcoin","html_url":"https://github.com/bitcoin","followers_url":"https://api.github.com/users/bitcoin/followers","following_url":"https://api.github.com/users/bitcoin/following{/other_user}","gists_url":"https://api.github.com/users/bitcoin/gists{/gist_id}","starred_url":"https://api.github.com/users/bitcoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitcoin/subscriptions","organizations_url":"https://api.github.com/users/bitcoin/orgs","repos_url":"https://api.github.com/users/bitcoin/repos","events_url":"https://api.github.com/users/bitcoin/events{/privacy}","received_events_url":"https://api.github.com/users/bitcoin/received_events","type":"Organization","site_admin":false},"repo":{"id":1181927,"name":"bitcoin","full_name":"bitcoin/bitcoin","owner":{"login":"bitcoin","id":528860,"avatar_url":"https://avatars.githubusercontent.com/u/528860?v=3","gravatar_id":"","url":"https://api.github.com/users/bitcoin","html_url":"https://github.com/bitcoin","followers_url":"https://api.github.com/users/bitcoin/followers","following_url":"https://api.github.com/users/bitcoin/following{/other_user}","gists_url":"https://api.github.com/users/bitcoin/gists{/gist_id}","starred_url":"https://api.github.com/users/bitcoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitcoin/subscriptions","organizations_url":"https://api.github.com/users/bitcoin/orgs","repos_url":"https://api.github.com/users/bitcoin/repos","events_url":"https://api.github.com/users/bitcoin/events{/privacy}","received_events_url":"https://api.github.com/users/bitcoin/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bitcoin/bitcoin","description":"Bitcoin Core integration/staging tree","fork":false,"url":"https://api.github.com/repos/bitcoin/bitcoin","forks_url":"https://api.github.com/repos/bitcoin/bitcoin/forks","keys_url":"https://api.github.com/repos/bitcoin/bitcoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bitcoin/bitcoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bitcoin/bitcoin/teams","hooks_url":"https://api.github.com/repos/bitcoin/bitcoin/hooks","issue_events_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/events{/number}","events_url":"https://api.github.com/repos/bitcoin/bitcoin/events","assignees_url":"https://api.github.com/repos/bitcoin/bitcoin/assignees{/user}","branches_url":"https://api.github.com/repos/bitcoin/bitcoin/branches{/branch}","tags_url":"https://api.github.com/repos/bitcoin/bitcoin/tags","blobs_url":"https://api.github.com/repos/bitcoin/bitcoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bitcoin/bitcoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bitcoin/bitcoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/bitcoin/bitcoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bitcoin/bitcoin/statuses/{sha}","languages_url":"https://api.github.com/repos/bitcoin/bitcoin/languages","stargazers_url":"https://api.github.com/repos/bitcoin/bitcoin/stargazers","contributors_url":"https://api.github.com/repos/bitcoin/bitcoin/contributors","subscribers_url":"https://api.github.com/repos/bitcoin/bitcoin/subscribers","subscription_url":"https://api.github.com/repos/bitcoin/bitcoin/subscription","commits_url":"https://api.github.com/repos/bitcoin/bitcoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/bitcoin/bitcoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/bitcoin/bitcoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/bitcoin/bitcoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/bitcoin/bitcoin/contents/{+path}","compare_url":"https://api.github.com/repos/bitcoin/bitcoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bitcoin/bitcoin/merges","archive_url":"https://api.github.com/repos/bitcoin/bitcoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bitcoin/bitcoin/downloads","issues_url":"https://api.github.com/repos/bitcoin/bitcoin/issues{/number}","pulls_url":"https://api.github.com/repos/bitcoin/bitcoin/pulls{/number}","milestones_url":"https://api.github.com/repos/bitcoin/bitcoin/milestones{/number}","notifications_url":"https://api.github.com/repos/bitcoin/bitcoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bitcoin/bitcoin/labels{/name}","releases_url":"https://api.github.com/repos/bitcoin/bitcoin/releases{/id}","created_at":"2010-12-19T15:16:43Z","updated_at":"2015-08-20T17:41:41Z","pushed_at":"2015-08-20T23:07:16Z","git_url":"git://github.com/bitcoin/bitcoin.git","ssh_url":"git@github.com:bitcoin/bitcoin.git","clone_url":"https://github.com/bitcoin/bitcoin.git","svn_url":"https://github.com/bitcoin/bitcoin","homepage":"https://bitcoin.org/en/download","size":133919,"stargazers_count":7366,"watchers_count":7366,"language":"C++","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":5099,"mirror_url":null,"open_issues_count":438,"forks":5099,"open_issues":438,"watchers":7366,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374"},"html":{"href":"https://github.com/bitcoin/bitcoin/pull/6374"},"issue":{"href":"https://api.github.com/repos/bitcoin/bitcoin/issues/6374"},"comments":{"href":"https://api.github.com/repos/bitcoin/bitcoin/issues/6374/comments"},"review_comments":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374/comments"},"review_comment":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bitcoin/bitcoin/pulls/6374/commits"},"statuses":{"href":"https://api.github.com/repos/bitcoin/bitcoin/statuses/1f1c87438db3e707b2bfc0e35b12e054df5c0661"}}}}