Bitcoin Core  0.18.99
P2P Digital Currency
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{0}; // 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:
51  bool SetInternal(const std::string& name);
52 
53  bool SetSpecial(const std::string &strName); // for Tor addresses
54  bool IsBindAny() const; // INADDR_ANY equivalent
55  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
56  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
57  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
58  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
59  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
60  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
61  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
62  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
63  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
64  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
65  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
66  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
67  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
68  bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
69  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
70  bool IsTor() const;
71  bool IsLocal() const;
72  bool IsRoutable() const;
73  bool IsInternal() const;
74  bool IsValid() const;
75  enum Network GetNetwork() const;
76  std::string ToString() const;
77  std::string ToStringIP() const;
78  unsigned int GetByte(int n) const;
79  uint64_t GetHash() const;
80  bool GetInAddr(struct in_addr* pipv4Addr) const;
81  std::vector<unsigned char> GetGroup() const;
82  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
83 
84  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
85  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
86 
87  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
88  friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
89  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
90 
92 
93  template <typename Stream, typename Operation>
94  inline void SerializationOp(Stream& s, Operation ser_action) {
95  READWRITE(ip);
96  }
97 
98  friend class CSubNet;
99 };
100 
101 class CSubNet
102 {
103  protected:
107  uint8_t netmask[16];
109  bool valid;
110 
111  public:
112  CSubNet();
113  CSubNet(const CNetAddr &addr, int32_t mask);
114  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
115 
116  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
117  explicit CSubNet(const CNetAddr &addr);
118 
119  bool Match(const CNetAddr &addr) const;
120 
121  std::string ToString() const;
122  bool IsValid() const;
123 
124  friend bool operator==(const CSubNet& a, const CSubNet& b);
125  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
126  friend bool operator<(const CSubNet& a, const CSubNet& b);
127 
129 
130  template <typename Stream, typename Operation>
131  inline void SerializationOp(Stream& s, Operation ser_action) {
132  READWRITE(network);
133  READWRITE(netmask);
134  READWRITE(valid);
135  }
136 };
137 
139 class CService : public CNetAddr
140 {
141  protected:
142  uint16_t port; // host order
143 
144  public:
145  CService();
146  CService(const CNetAddr& ip, unsigned short port);
147  CService(const struct in_addr& ipv4Addr, unsigned short port);
148  explicit CService(const struct sockaddr_in& addr);
149  unsigned short GetPort() const;
150  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
151  bool SetSockAddr(const struct sockaddr* paddr);
152  friend bool operator==(const CService& a, const CService& b);
153  friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
154  friend bool operator<(const CService& a, const CService& b);
155  std::vector<unsigned char> GetKey() const;
156  std::string ToString() const;
157  std::string ToStringPort() const;
158  std::string ToStringIPPort() const;
159 
160  CService(const struct in6_addr& ipv6Addr, unsigned short port);
161  explicit CService(const struct sockaddr_in6& addr);
162 
164 
165  template <typename Stream, typename Operation>
166  inline void SerializationOp(Stream& s, Operation ser_action) {
167  READWRITE(ip);
168  READWRITE(WrapBigEndian(port));
169  }
170 };
171 
172 #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:506
friend class CSubNet
Definition: netaddress.h:98
bool IsLocal() const
Definition: netaddress.cpp:218
bool IsRFC4380() const
Definition: netaddress.cpp:180
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:27
bool IsIPv6() const
Definition: netaddress.cpp:129
uint16_t port
Definition: netaddress.h:142
bool IsInternal() const
Definition: netaddress.cpp:300
CNetAddr network
Network (base) address.
Definition: netaddress.h:105
bool IsBindAny() const
Definition: netaddress.cpp:114
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:371
std::string ToString() const
Definition: netaddress.cpp:346
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:389
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:530
enum Network GetNetwork() const
Definition: netaddress.cpp:305
bool IsRFC2544() const
Definition: netaddress.cpp:142
ADD_SERIALIZE_METHODS
Definition: netaddress.h:163
bool IsRFC4862() const
Definition: netaddress.cpp:185
bool IsValid() const
Definition: netaddress.cpp:242
std::vector< unsigned char > GetGroup() const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:408
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:88
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:125
bool IsIPv4() const
Definition: netaddress.cpp:124
bool IsRFC5737() const
Definition: netaddress.cpp:157
uint32_t scopeId
Definition: netaddress.h:36
bool IsRFC6145() const
Definition: netaddress.cpp:196
bool IsRFC6052() const
Definition: netaddress.cpp:174
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.h:153
std::string ToStringIP() const
Definition: netaddress.cpp:322
const char * name
Definition: rest.cpp:38
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:139
Network
Definition: netaddress.h:20
unsigned int GetByte(int n) const
Definition: netaddress.cpp:109
bool IsRFC3849() const
Definition: netaddress.cpp:164
bool IsRoutable() const
Definition: netaddress.cpp:290
uint64_t GetHash() const
Definition: netaddress.cpp:484
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:109
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:166
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:356
bool IsRFC1918() const
Definition: netaddress.cpp:134
bool IsRFC6598() const
Definition: netaddress.cpp:152
bool IsRFC3927() const
Definition: netaddress.cpp:147
ADD_SERIALIZE_METHODS
Definition: netaddress.h:91
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:351
unsigned char ip[16]
Definition: netaddress.h:35
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:131
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:32
bool SetSpecial(const std::string &strName)
Try to make this a dummy address that maps the specified onion address into IPv6 using OnionCat&#39;s ran...
Definition: netaddress.cpp:84
bool IsRFC4843() const
Definition: netaddress.cpp:202
bool SetInternal(const std::string &name)
Try to make this a dummy address that maps the specified name into IPv6 like so: (0xFD + sha256("bitc...
Definition: netaddress.cpp:62
#define READWRITE(...)
Definition: serialize.h:184
bool IsRFC4193() const
Definition: netaddress.cpp:191
bool IsTor() const
Definition: netaddress.cpp:213
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:94
ADD_SERIALIZE_METHODS
Definition: netaddress.h:128
bool IsRFC3964() const
Definition: netaddress.cpp:169
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:22