Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1001case NET_IPV4:1002case NET_IPV6:1003valid = true;1004assert(addr.m_addr.size() <= sizeof(netmask));1005memset(netmask, 0xFF, addr.m_addr.size());1006break;1007case NET_ONION:1008case NET_I2P:1009case NET_CJDNS:1010valid = true;1011break;1012case NET_INTERNAL:1013case NET_UNROUTABLE:1014case NET_MAX:1015return;1016}1018network = addr;1019}1021/**1022* @returns True if this subnet is valid, the specified address is valid, and1023* the specified address belongs in this subnet.1024*/1025bool CSubNet::Match(const CNetAddr &addr) const1026{1027if (!valid || !addr.IsValid() || network.m_net != addr.m_net)1028return false;1030switch (network.m_net) {1031case NET_IPV4:1032case NET_IPV6:1033break;1034case NET_ONION:1035case NET_I2P:1036case NET_CJDNS:1037case NET_INTERNAL:1038return addr == network;1039case NET_UNROUTABLE:1040case NET_MAX:1041return false;1042}1044assert(network.m_addr.size() == addr.m_addr.size());1045for (size_t x = 0; x < addr.m_addr.size(); ++x) {1046if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {1047return false;1048}1049}1050return true;1051}1053std::string CSubNet::ToString() const1054{1055std::string suffix;1057switch (network.m_net) {1058case NET_IPV4:1059case NET_IPV6: {1060assert(network.m_addr.size() <= sizeof(netmask));1062uint8_t cidr = 0;1064for (size_t i = 0; i < network.m_addr.size(); ++i) {1065if (netmask[i] == 0x00) {1066break;1067}1068cidr += NetmaskBits(netmask[i]);1069}1071suffix = strprintf("/%u", cidr);1072break;1073}1074case NET_ONION:1075case NET_I2P:1076case NET_CJDNS:1077case NET_INTERNAL:1078case NET_UNROUTABLE:1079case NET_MAX:1080break;1081}1083return network.ToStringAddr() + suffix;1084}1086bool CSubNet::IsValid() const1087{1088return valid;1089}1091bool operator==(const CSubNet& a, const CSubNet& b)1092{1093return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);1094}1096bool operator<(const CSubNet& a, const CSubNet& b)1097{1098return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));1099}