diff options
| author | Bitflash <admin@bitflash.network> | 2026-08-18 10:34:30 -0300 |
|---|---|---|
| committer | Bitflash <admin@bitflash.network> | 2026-08-18 10:34:30 -0300 |
| commit | beb5c1435a0d85b7b24d4d371933758ebed90907 (patch) | |
| tree | 79501a648da78ec6962405fabd58e2d70ca6589f /src/net.cpp | |
| parent | 243b635267b6d34ef3b4b50ed559f743db161ad7 (diff) | |
| parent | 33092ffe8ee5fb5f7c080a44831e5557b63deb63 (diff) | |
Merge pull request 'net: cap inbound connections per address' (#60) from net-per-ip-connlimit into main
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 3660735..4603d2d 100644 --- a/src/net.cpp +++ b/src/net.cpp | |||
| @@ -1622,12 +1622,41 @@ void ThreadSocketHandler2(void* parg) | |||
| 1622 | unsigned int nNodes = 0; | 1622 | unsigned int nNodes = 0; |
| 1623 | CRITICAL_BLOCK(cs_vNodes) | 1623 | CRITICAL_BLOCK(cs_vNodes) |
| 1624 | nNodes = (unsigned int)vNodes.size(); | 1624 | nNodes = (unsigned int)vNodes.size(); |
| 1625 | // Also count how many connections this one address already | ||
| 1626 | // holds. A single IP that keeps opening sockets -- a broken | ||
| 1627 | // node or a deliberate connection-exhaustion flood -- must not | ||
| 1628 | // be able to take the whole table, which is what starved the | ||
| 1629 | // relays. | ||
| 1630 | // | ||
| 1631 | // Loopback is exempt. Onion peers reach a managed-Tor node | ||
| 1632 | // through its local hidden-service listener, so every one of | ||
| 1633 | // them accepts as 127.0.0.1; capping loopback would cap all | ||
| 1634 | // inbound onion peers as a single group, which is the opposite | ||
| 1635 | // of where the network is headed. The global MAX_CONNECTIONS | ||
| 1636 | // still bounds the total, and the direct-IP flood this defends | ||
| 1637 | // against never arrives over loopback. (Per-onion-identity | ||
| 1638 | // limiting, once a peer's identity is known past accept(), is | ||
| 1639 | // the right tool for onion and is left for later.) | ||
| 1640 | bool fLoopback = (addr.GetByte(3) == 127); | ||
| 1641 | unsigned int nFromThisIP = 0; | ||
| 1642 | if (!fLoopback) | ||
| 1643 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1644 | for (vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) | ||
| 1645 | if ((*it)->addr.ip == addr.ip) | ||
| 1646 | nFromThisIP++; | ||
| 1647 | |||
| 1625 | if (nNodes >= MAX_CONNECTIONS) | 1648 | if (nNodes >= MAX_CONNECTIONS) |
| 1626 | { | 1649 | { |
| 1627 | LogPrint("net", "refusing connection from %s, already at %u\n", | 1650 | LogPrint("net", "refusing connection from %s, already at %u\n", |
| 1628 | addr.ToString().c_str(), nNodes); | 1651 | addr.ToString().c_str(), nNodes); |
| 1629 | BtfCloseSocket(hSocket); | 1652 | BtfCloseSocket(hSocket); |
| 1630 | } | 1653 | } |
| 1654 | else if (!fLoopback && nFromThisIP >= MAX_CONNECTIONS_PER_IP) | ||
| 1655 | { | ||
| 1656 | LogPrint("net", "refusing connection from %s, already %u from that address\n", | ||
| 1657 | addr.ToString().c_str(), nFromThisIP); | ||
| 1658 | BtfCloseSocket(hSocket); | ||
| 1659 | } | ||
| 1631 | else | 1660 | else |
| 1632 | { | 1661 | { |
| 1633 | LogPrint("net", "accepted connection from %s\n", addr.ToString().c_str()); | 1662 | LogPrint("net", "accepted connection from %s\n", addr.ToString().c_str()); |