1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_NETADDRESS_H
6 #define BITCOIN_NETADDRESS_H
8 #if defined(HAVE_CONFIG_H)
9 #include "config/bitcoin-config.h"
13 #include "serialize.h"
30 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
34 unsigned char ip
[16]; // in network byte order
35 uint32_t scopeId
; // for scoped/link-local ipv6 addresses
39 explicit CNetAddr(const struct in_addr
& ipv4Addr
);
41 void SetIP(const CNetAddr
& ip
);
44 * Set raw IPv4 or IPv6 address (in network byte order)
45 * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
47 void SetRaw(Network network
, const uint8_t *data
);
50 * Transform an arbitrary string into a non-routable ipv6 address.
51 * Useful for mapping resolved addresses back to their source.
53 bool SetInternal(const std::string
& name
);
55 bool SetSpecial(const std::string
&strName
); // for Tor addresses
56 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
57 bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
58 bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
59 bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
60 bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
61 bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
62 bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
63 bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
64 bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
65 bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
66 bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
67 bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
68 bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
69 bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
70 bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
73 bool IsRoutable() const;
74 bool IsInternal() const;
76 enum Network
GetNetwork() const;
77 std::string
ToString() const;
78 std::string
ToStringIP() const;
79 unsigned int GetByte(int n
) const;
80 uint64_t GetHash() const;
81 bool GetInAddr(struct in_addr
* pipv4Addr
) const;
82 std::vector
<unsigned char> GetGroup() const;
83 int GetReachabilityFrom(const CNetAddr
*paddrPartner
= nullptr) const;
85 explicit CNetAddr(const struct in6_addr
& pipv6Addr
, const uint32_t scope
= 0);
86 bool GetIn6Addr(struct in6_addr
* pipv6Addr
) const;
88 friend bool operator==(const CNetAddr
& a
, const CNetAddr
& b
);
89 friend bool operator!=(const CNetAddr
& a
, const CNetAddr
& b
);
90 friend bool operator<(const CNetAddr
& a
, const CNetAddr
& b
);
92 ADD_SERIALIZE_METHODS
;
94 template <typename Stream
, typename Operation
>
95 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
96 READWRITE(FLATDATA(ip
));
105 /// Network (base) address
107 /// Netmask, in network byte order
109 /// Is this value valid? (only used to signal parse errors)
114 CSubNet(const CNetAddr
&addr
, int32_t mask
);
115 CSubNet(const CNetAddr
&addr
, const CNetAddr
&mask
);
117 //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
118 explicit CSubNet(const CNetAddr
&addr
);
120 bool Match(const CNetAddr
&addr
) const;
122 std::string
ToString() const;
123 bool IsValid() const;
125 friend bool operator==(const CSubNet
& a
, const CSubNet
& b
);
126 friend bool operator!=(const CSubNet
& a
, const CSubNet
& b
);
127 friend bool operator<(const CSubNet
& a
, const CSubNet
& b
);
129 ADD_SERIALIZE_METHODS
;
131 template <typename Stream
, typename Operation
>
132 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
134 READWRITE(FLATDATA(netmask
));
135 READWRITE(FLATDATA(valid
));
139 /** A combination of a network address (CNetAddr) and a (TCP) port */
140 class CService
: public CNetAddr
143 unsigned short port
; // host order
147 CService(const CNetAddr
& ip
, unsigned short port
);
148 CService(const struct in_addr
& ipv4Addr
, unsigned short port
);
149 explicit CService(const struct sockaddr_in
& addr
);
151 unsigned short GetPort() const;
152 bool GetSockAddr(struct sockaddr
* paddr
, socklen_t
*addrlen
) const;
153 bool SetSockAddr(const struct sockaddr
* paddr
);
154 friend bool operator==(const CService
& a
, const CService
& b
);
155 friend bool operator!=(const CService
& a
, const CService
& b
);
156 friend bool operator<(const CService
& a
, const CService
& b
);
157 std::vector
<unsigned char> GetKey() const;
158 std::string
ToString() const;
159 std::string
ToStringPort() const;
160 std::string
ToStringIPPort() const;
162 CService(const struct in6_addr
& ipv6Addr
, unsigned short port
);
163 explicit CService(const struct sockaddr_in6
& addr
);
165 ADD_SERIALIZE_METHODS
;
167 template <typename Stream
, typename Operation
>
168 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
169 READWRITE(FLATDATA(ip
));
170 unsigned short portN
= htons(port
);
171 READWRITE(FLATDATA(portN
));
172 if (ser_action
.ForRead())
177 #endif // BITCOIN_NETADDRESS_H