BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2018 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)
10 #endif
11 
12 #include <compat.h>
13 #include <serialize.h>
14 #include <span.h>
15 
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
19 
20 enum Network
21 {
27 
29 };
30 
32 class CNetAddr
33 {
34  protected:
35  unsigned char ip[16]; // in network byte order
36  uint32_t scopeId; // for scoped/link-local ipv6 addresses
37 
38  public:
39  CNetAddr();
40  explicit CNetAddr(const struct in_addr& ipv4Addr);
41  void SetIP(const CNetAddr& ip);
42 
43  private:
48  void SetRaw(Network network, const uint8_t *data);
49 
50  public:
55  bool SetInternal(const std::string& name);
56 
57  bool SetSpecial(const std::string &strName); // for Tor addresses
58  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
59  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
60  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
61  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
62  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
63  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
64  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
65  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
66  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
67  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
68  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
69  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
70  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
71  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
72  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
73  bool IsTor() const;
74  bool IsLocal() const;
75  bool IsRoutable() const;
76  bool IsInternal() const;
77  bool IsValid() const;
78  enum Network GetNetwork() const;
79  std::string ToString() const;
80  std::string ToStringIP() const;
81  unsigned int GetByte(int n) const;
82  uint64_t GetHash() const;
83  bool GetInAddr(struct in_addr* pipv4Addr) const;
84  std::vector<unsigned char> GetGroup() const;
85  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
86 
87  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
88  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
89 
90  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
91  friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
92  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
93 
95 
96  template <typename Stream, typename Operation>
97  inline void SerializationOp(Stream& s, Operation ser_action) {
98  READWRITE(ip);
99  }
100 
101  friend class CSubNet;
102 };
103 
104 class CSubNet
105 {
106  protected:
110  uint8_t netmask[16];
112  bool valid;
113 
114  public:
115  CSubNet();
116  CSubNet(const CNetAddr &addr, int32_t mask);
117  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
118 
119  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
120  explicit CSubNet(const CNetAddr &addr);
121 
122  bool Match(const CNetAddr &addr) const;
123 
124  std::string ToString() const;
125  bool IsValid() const;
126 
127  friend bool operator==(const CSubNet& a, const CSubNet& b);
128  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
129  friend bool operator<(const CSubNet& a, const CSubNet& b);
130 
132 
133  template <typename Stream, typename Operation>
134  inline void SerializationOp(Stream& s, Operation ser_action) {
137  READWRITE(valid);
138  }
139 };
140 
142 class CService : public CNetAddr
143 {
144  protected:
145  uint16_t port; // host order
146 
147  public:
148  CService();
149  CService(const CNetAddr& ip, unsigned short port);
150  CService(const struct in_addr& ipv4Addr, unsigned short port);
151  explicit CService(const struct sockaddr_in& addr);
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) { return !(a == 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;
162 
163  CService(const struct in6_addr& ipv6Addr, unsigned short port);
164  explicit CService(const struct sockaddr_in6& addr);
165 
167 
168  template <typename Stream, typename Operation>
169  inline void SerializationOp(Stream& s, Operation ser_action) {
170  READWRITE(ip);
172  }
173 };
174 
175 #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:407
std::string ToStringPort() const
Definition: netaddress.cpp:560
unsigned short GetPort() const
Definition: netaddress.cpp:505
bool IsLocal() const
Definition: netaddress.cpp:174
bool IsRFC4380() const
Definition: netaddress.cpp:142
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:23
bool IsIPv6() const
Definition: netaddress.cpp:91
uint16_t port
Definition: netaddress.h:145
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:510
bool IsInternal() const
Definition: netaddress.cpp:232
CNetAddr network
Network (base) address.
Definition: netaddress.h:108
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:293
std::string ToString() const
Definition: netaddress.cpp:278
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:301
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:519
enum Network GetNetwork() const
Definition: netaddress.cpp:237
bool IsRFC2544() const
Definition: netaddress.cpp:104
ADD_SERIALIZE_METHODS
Definition: netaddress.h:166
bool IsRFC4862() const
Definition: netaddress.cpp:147
bool IsValid() const
Definition: netaddress.cpp:188
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:312
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:91
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:128
bool IsIPv4() const
Definition: netaddress.cpp:86
bool IsRFC5737() const
Definition: netaddress.cpp:119
uint32_t scopeId
Definition: netaddress.h:36
bool IsRFC6145() const
Definition: netaddress.cpp:158
bool IsRFC6052() const
Definition: netaddress.cpp:136
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:520
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.h:156
std::string ToStringIP() const
Definition: netaddress.cpp:254
const char * name
Definition: rest.cpp:37
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:142
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:515
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:550
Network
Definition: netaddress.h:20
unsigned int GetByte(int n) const
Definition: netaddress.cpp:81
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:704
bool IsRFC3849() const
Definition: netaddress.cpp:126
bool IsRoutable() const
Definition: netaddress.cpp:227
uint64_t GetHash() const
Definition: netaddress.cpp:385
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:112
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:169
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:635
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:110
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:32
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:288
bool IsValid() const
Definition: netaddress.cpp:699
bool IsRFC1918() const
Definition: netaddress.cpp:96
bool IsRFC6598() const
Definition: netaddress.cpp:114
std::string ToString() const
Definition: netaddress.cpp:661
bool IsRFC3927() const
Definition: netaddress.cpp:109
ADD_SERIALIZE_METHODS
Definition: netaddress.h:94
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:283
std::string ToStringIPPort() const
Definition: netaddress.cpp:565
unsigned char ip[16]
Definition: netaddress.h:35
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:709
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:134
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:28
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:56
std::string ToString() const
Definition: netaddress.cpp:574
bool IsRFC4843() const
Definition: netaddress.cpp:164
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:491
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:44
#define READWRITE(...)
Definition: serialize.h:173
bool IsRFC4193() const
Definition: netaddress.cpp:153
bool IsTor() const
Definition: netaddress.cpp:169
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:97
ADD_SERIALIZE_METHODS
Definition: netaddress.h:131
bool IsRFC3964() const
Definition: netaddress.cpp:131