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.
501* IPv6 addresses. Otherwise an invalid subnet is created.502*/503CSubNet(const CNetAddr& addr, uint8_t mask);505/**506* Construct from a given network start and mask.507* @param[in] addr Network start. Must be IPv4 or IPv6, otherwise an invalid subnet is508* created.509* @param[in] mask Network mask, must be of the same type as `addr` and not contain 0-bits510* followed by 1-bits. Otherwise an invalid subnet is created.511*/512CSubNet(const CNetAddr& addr, const CNetAddr& mask);514/**515* Construct a single-host subnet.516* @param[in] addr The sole address to be contained in the subnet, can also be non-IPv[46].517*/518explicit CSubNet(const CNetAddr& addr);520bool Match(const CNetAddr& addr) const;522std::string ToString() const;523bool IsValid() const;525friend bool operator==(const CSubNet& a, const CSubNet& b);526friend bool operator<(const CSubNet& a, const CSubNet& b);527};529/** A combination of a network address (CNetAddr) and a (TCP) port */530class CService : public CNetAddr531{532protected:533uint16_t port; // host order535public:536CService();537CService(const CNetAddr& ip, uint16_t port);538CService(const struct in_addr& ipv4Addr, uint16_t port);539explicit CService(const struct sockaddr_in& addr);540uint16_t GetPort() const;541bool GetSockAddr(struct sockaddr* paddr, socklen_t* addrlen) const;542/**543* Set CService from a network sockaddr.544* @param[in] paddr Pointer to sockaddr structure545* @param[in] addrlen Length of sockaddr structure in bytes. This will be checked to exactly match the length of546* a socket address of the provided family, unless std::nullopt is passed547* @returns true on success548*/549bool SetSockAddr(const struct sockaddr* paddr, socklen_t addrlen);550/**551* Get the address family552* @returns AF_UNSPEC if unspecified553*/554[[nodiscard]] sa_family_t GetSAFamily() const;555friend bool operator==(const CService& a, const CService& b);556friend bool operator<(const CService& a, const CService& b);557std::vector<unsigned char> GetKey() const;558std::string ToStringAddrPort() const;560CService(const struct in6_addr& ipv6Addr, uint16_t port);561explicit CService(const struct sockaddr_in6& addr);563SERIALIZE_METHODS(CService, obj)564{565READWRITE(AsBase<CNetAddr>(obj), Using<BigEndianFormatter<2>>(obj.port));566}568friend class CServiceHash;569friend CService MaybeFlipIPv6toCJDNS(const CService& service);570};572class CServiceHash573{574public:575CServiceHash()576: m_salt_k0{FastRandomContext().rand64()},577m_salt_k1{FastRandomContext().rand64()}578{579}581CServiceHash(uint64_t salt_k0, uint64_t salt_k1) : m_salt_k0{salt_k0}, m_salt_k1{salt_k1} {}583size_t operator()(const CService& a) const noexcept584{585CSipHasher hasher(m_salt_k0, m_salt_k1);586hasher.Write(a.m_net);587hasher.Write(a.port);588hasher.Write(a.m_addr);589return static_cast<size_t>(hasher.Finalize());590}592private:593const uint64_t m_salt_k0;594const uint64_t m_salt_k1;595};597#endif // BITCOIN_NETADDRESS_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.