diff options
Diffstat (limited to 'src/net.cpp')
| -rw-r--r-- | src/net.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/net.cpp b/src/net.cpp index 16038c8..bb05259 100644 --- a/src/net.cpp +++ b/src/net.cpp | |||
| @@ -1560,12 +1560,41 @@ void ThreadSocketHandler2(void* parg) | |||
| 1560 | unsigned int nNodes = 0; | 1560 | unsigned int nNodes = 0; |
| 1561 | CRITICAL_BLOCK(cs_vNodes) | 1561 | CRITICAL_BLOCK(cs_vNodes) |
| 1562 | nNodes = (unsigned int)vNodes.size(); | 1562 | nNodes = (unsigned int)vNodes.size(); |
| 1563 | // Also count how many connections this one address already | ||
| 1564 | // holds. A single IP that keeps opening sockets -- a broken | ||
| 1565 | // node or a deliberate connection-exhaustion flood -- must not | ||
| 1566 | // be able to take the whole table, which is what starved the | ||
| 1567 | // relays. | ||
| 1568 | // | ||
| 1569 | // Loopback is exempt. Onion peers reach a managed-Tor node | ||
| 1570 | // through its local hidden-service listener, so every one of | ||
| 1571 | // them accepts as 127.0.0.1; capping loopback would cap all | ||
| 1572 | // inbound onion peers as a single group, which is the opposite | ||
| 1573 | // of where the network is headed. The global MAX_CONNECTIONS | ||
| 1574 | // still bounds the total, and the direct-IP flood this defends | ||
| 1575 | // against never arrives over loopback. (Per-onion-identity | ||
| 1576 | // limiting, once a peer's identity is known past accept(), is | ||
| 1577 | // the right tool for onion and is left for later.) | ||
| 1578 | bool fLoopback = (addr.GetByte(3) == 127); | ||
| 1579 | unsigned int nFromThisIP = 0; | ||
| 1580 | if (!fLoopback) | ||
| 1581 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1582 | for (vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) | ||
| 1583 | if ((*it)->addr.ip == addr.ip) | ||
| 1584 | nFromThisIP++; | ||
| 1585 | |||
| 1563 | if (nNodes >= MAX_CONNECTIONS) | 1586 | if (nNodes >= MAX_CONNECTIONS) |
| 1564 | { | 1587 | { |
| 1565 | LogPrint("net", "refusing connection from %s, already at %u\n", | 1588 | LogPrint("net", "refusing connection from %s, already at %u\n", |
| 1566 | addr.ToString().c_str(), nNodes); | 1589 | addr.ToString().c_str(), nNodes); |
| 1567 | BtfCloseSocket(hSocket); | 1590 | BtfCloseSocket(hSocket); |
| 1568 | } | 1591 | } |
| 1592 | else if (!fLoopback && nFromThisIP >= MAX_CONNECTIONS_PER_IP) | ||
| 1593 | { | ||
| 1594 | LogPrint("net", "refusing connection from %s, already %u from that address\n", | ||
| 1595 | addr.ToString().c_str(), nFromThisIP); | ||
| 1596 | BtfCloseSocket(hSocket); | ||
| 1597 | } | ||
| 1569 | else | 1598 | else |
| 1570 | { | 1599 | { |
| 1571 | LogPrint("net", "accepted connection from %s\n", addr.ToString().c_str()); | 1600 | LogPrint("net", "accepted connection from %s\n", addr.ToString().c_str()); |