Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 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.
4 
5 #ifndef BITCOIN_NETADDRESS_H
6 #define BITCOIN_NETADDRESS_H
7 
8 #if defined(HAVE_CONFIG_H)
9 #include <config/dash-config.h>
10 #endif
11 
12 #include <compat.h>
13 #include <serialize.h>
14 
15 #include <stdint.h>
16 #include <string>
17 #include <vector>
18 
19 extern bool fAllowPrivateNet;
20 
21 enum Network
22 {
28 
30 };
31 
33 class CNetAddr
34 {
35  protected:
36  unsigned char ip[16]; // in network byte order
37  uint32_t scopeId; // for scoped/link-local ipv6 addresses
38 
39  public:
40  CNetAddr();
41  explicit CNetAddr(const struct in_addr& ipv4Addr);
42  void SetIP(const CNetAddr& ip);
43 
44  private:
49  void SetRaw(Network network, const uint8_t *data);
50 
51  public:
56  bool SetInternal(const std::string& name);
57 
58  bool SetSpecial(const std::string &strName); // for Tor addresses
59  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
60  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
61  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
62  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
63  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
64  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
65  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
66  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
67  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
68  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
69  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
70  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
71  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
72  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
73  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
74  bool IsTor() const;
75  bool IsLocal() const;
76  bool IsRoutable() const;
77  bool IsInternal() const;
78  bool IsValid() const;
79  enum Network GetNetwork() const;
80  std::string ToString() const;
81  std::string ToStringIP(bool fUseGetnameinfo = true) const;
82  unsigned int GetByte(int n) const;
83  uint64_t GetHash() const;
84  bool GetInAddr(struct in_addr* pipv4Addr) const;
85  std::vector<unsigned char> GetGroup() const;
86  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
87 
88  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
89  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
90 
91  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
92  friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
93  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
94 
96 
97  template <typename Stream, typename Operation>
98  inline void SerializationOp(Stream& s, Operation ser_action) {
100  }
101 
102  friend class CSubNet;
103 };
104 
105 class CSubNet
106 {
107  protected:
111  uint8_t netmask[16];
113  bool valid;
114 
115  public:
116  CSubNet();
117  CSubNet(const CNetAddr &addr, int32_t mask);
118  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
119 
120  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
121  explicit CSubNet(const CNetAddr &addr);
122 
123  bool Match(const CNetAddr &addr) const;
124 
125  std::string ToString() const;
126  bool IsValid() const;
127 
128  friend bool operator==(const CSubNet& a, const CSubNet& b);
129  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
130  friend bool operator<(const CSubNet& a, const CSubNet& b);
131 
133 
134  template <typename Stream, typename Operation>
135  inline void SerializationOp(Stream& s, Operation ser_action) {
139  }
140 };
141 
143 class CService : public CNetAddr
144 {
145  protected:
146  unsigned short port; // host order
147 
148  public:
149  CService();
150  CService(const CNetAddr& ip, unsigned short port);
151  CService(const struct in_addr& ipv4Addr, unsigned short port);
152  explicit CService(const struct sockaddr_in& addr);
153  void SetPort(unsigned short portIn);
154  unsigned short GetPort() const;
155  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
156  bool SetSockAddr(const struct sockaddr* paddr);
157  friend bool operator==(const CService& a, const CService& b);
158  friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
159  friend bool operator<(const CService& a, const CService& b);
160  std::vector<unsigned char> GetKey() const;
161  std::string ToString(bool fUseGetnameinfo = true) const;
162  std::string ToStringPort() const;
163  std::string ToStringIPPort(bool fUseGetnameinfo = true) const;
164 
165  CService(const struct in6_addr& ipv6Addr, unsigned short port);
166  explicit CService(const struct sockaddr_in6& addr);
167 
169 
170  template <typename Stream, typename Operation>
171  inline void SerializationOp(Stream& s, Operation ser_action) {
173  unsigned short portN = htons(port);
174  READWRITE(FLATDATA(portN));
175  if (ser_action.ForRead())
176  port = ntohs(portN);
177  }
178 };
179 
180 #endif // BITCOIN_NETADDRESS_H
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:414
std::string ToStringPort() const
Definition: netaddress.cpp:567
unsigned short GetPort() const
Definition: netaddress.cpp:512
bool IsLocal() const
Definition: netaddress.cpp:177
bool IsRFC4380() const
Definition: netaddress.cpp:145
#define READWRITE(obj)
Definition: serialize.h:165
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:26
bool IsIPv6() const
Definition: netaddress.cpp:94
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:517
void SetPort(unsigned short portIn)
Definition: netaddress.cpp:586
bool IsInternal() const
Definition: netaddress.cpp:239
CNetAddr network
Network (base) address.
Definition: netaddress.h:109
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:303
std::string ToString() const
Definition: netaddress.cpp:288
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:311
#define FLATDATA(obj)
Definition: serialize.h:370
std::string ToStringIP(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:261
enum Network GetNetwork() const
Definition: netaddress.cpp:244
bool IsRFC2544() const
Definition: netaddress.cpp:107
ADD_SERIALIZE_METHODS
Definition: netaddress.h:168
bool IsRFC4862() const
Definition: netaddress.cpp:150
bool IsValid() const
Definition: netaddress.cpp:191
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:319
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:92
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:129
bool IsIPv4() const
Definition: netaddress.cpp:89
bool IsRFC5737() const
Definition: netaddress.cpp:122
uint32_t scopeId
Definition: netaddress.h:37
bool IsRFC6145() const
Definition: netaddress.cpp:161
bool IsRFC6052() const
Definition: netaddress.cpp:139
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:527
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.h:158
const char * name
Definition: rest.cpp:36
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
unsigned short port
Definition: netaddress.h:146
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:522
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:557
Network
Definition: netaddress.h:21
unsigned int GetByte(int n) const
Definition: netaddress.cpp:84
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:716
bool IsRFC3849() const
Definition: netaddress.cpp:129
bool IsRoutable() const
Definition: netaddress.cpp:230
uint64_t GetHash() const
Definition: netaddress.cpp:392
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:113
constexpr T mask
Definition: bits.hpp:45
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:171
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:647
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:111
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:33
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:298
bool IsValid() const
Definition: netaddress.cpp:711
bool IsRFC1918() const
Definition: netaddress.cpp:99
bool IsRFC6598() const
Definition: netaddress.cpp:117
std::string ToString() const
Definition: netaddress.cpp:673
bool IsRFC3927() const
Definition: netaddress.cpp:112
ADD_SERIALIZE_METHODS
Definition: netaddress.h:95
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:293
bool fAllowPrivateNet
Definition: netaddress.cpp:18
unsigned char ip[16]
Definition: netaddress.h:36
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:721
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:135
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:31
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:59
std::string ToStringIPPort(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:572
bool IsRFC4843() const
Definition: netaddress.cpp:167
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:498
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:47
bool IsRFC4193() const
Definition: netaddress.cpp:156
bool IsTor() const
Definition: netaddress.cpp:172
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:98
ADD_SERIALIZE_METHODS
Definition: netaddress.h:132
bool IsRFC3964() const
Definition: netaddress.cpp:134
Released under the MIT license