Merge #10816: Properly forbid -salvagewallet and -zapwallettxes for multi wallet.
[bitcoinplatinum.git] / src / netaddress.h
blob80716600d17f004e765561b142fa9aeada2f3971
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"
10 #endif
12 #include "compat.h"
13 #include "serialize.h"
15 #include <stdint.h>
16 #include <string>
17 #include <vector>
19 enum Network
21 NET_UNROUTABLE = 0,
22 NET_IPV4,
23 NET_IPV6,
24 NET_TOR,
25 NET_INTERNAL,
27 NET_MAX,
30 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
31 class CNetAddr
33 protected:
34 unsigned char ip[16]; // in network byte order
35 uint32_t scopeId; // for scoped/link-local ipv6 addresses
37 public:
38 CNetAddr();
39 CNetAddr(const struct in_addr& ipv4Addr);
40 void Init();
41 void SetIP(const CNetAddr& ip);
43 /**
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);
49 /**
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)
71 bool IsTor() const;
72 bool IsLocal() const;
73 bool IsRoutable() const;
74 bool IsInternal() const;
75 bool IsValid() 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 = NULL) const;
85 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));
99 friend class CSubNet;
102 class CSubNet
104 protected:
105 /// Network (base) address
106 CNetAddr network;
107 /// Netmask, in network byte order
108 uint8_t netmask[16];
109 /// Is this value valid? (only used to signal parse errors)
110 bool valid;
112 public:
113 CSubNet();
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) {
133 READWRITE(network);
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
142 protected:
143 unsigned short port; // host order
145 public:
146 CService();
147 CService(const CNetAddr& ip, unsigned short port);
148 CService(const struct in_addr& ipv4Addr, unsigned short port);
149 CService(const struct sockaddr_in& addr);
150 void Init();
151 void SetPort(unsigned short portIn);
152 unsigned short GetPort() const;
153 bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
154 bool SetSockAddr(const struct sockaddr* paddr);
155 friend bool operator==(const CService& a, const CService& b);
156 friend bool operator!=(const CService& a, const CService& b);
157 friend bool operator<(const CService& a, const CService& b);
158 std::vector<unsigned char> GetKey() const;
159 std::string ToString() const;
160 std::string ToStringPort() const;
161 std::string ToStringIPPort() const;
163 CService(const struct in6_addr& ipv6Addr, unsigned short port);
164 CService(const struct sockaddr_in6& addr);
166 ADD_SERIALIZE_METHODS;
168 template <typename Stream, typename Operation>
169 inline void SerializationOp(Stream& s, Operation ser_action) {
170 READWRITE(FLATDATA(ip));
171 unsigned short portN = htons(port);
172 READWRITE(FLATDATA(portN));
173 if (ser_action.ForRead())
174 port = ntohs(portN);
178 #endif // BITCOIN_NETADDRESS_H