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 (deprecated) (2001:10::/28)
67  bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
68  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
69  bool IsRFC6052() const; // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
70  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in RFC2765)
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 = nullptr) const;
84 
85  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
86  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
87 
88  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
89  friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
90  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
91 
93 
94  template <typename Stream, typename Operation>
95  inline void SerializationOp(Stream& s, Operation ser_action) {
96  READWRITE(ip);
97  }
98 
99  friend class CSubNet;
100 };
101 
102 class CSubNet
103 {
104  protected:
108  uint8_t netmask[16];
110  bool valid;
111 
112  public:
113  CSubNet();
114  CSubNet(const CNetAddr &addr, int32_t mask);
115  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
116 
117  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
118  explicit CSubNet(const CNetAddr &addr);
119 
120  bool Match(const CNetAddr &addr) const;
121 
122  std::string ToString() const;
123  bool IsValid() const;
124 
125  friend bool operator==(const CSubNet& a, const CSubNet& b);
126  friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
127  friend bool operator<(const CSubNet& a, const CSubNet& b);
128 
130 
131  template <typename Stream, typename Operation>
132  inline void SerializationOp(Stream& s, Operation ser_action) {
133  READWRITE(network);
134  READWRITE(netmask);
135  READWRITE(valid);
136  }
137 };
138 
140 class CService : public CNetAddr
141 {
142  protected:
143  uint16_t port; // host order
144 
145  public:
146  CService();
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);
150  unsigned short GetPort() const;
151  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
152  bool SetSockAddr(const struct sockaddr* paddr);
153  friend bool operator==(const CService& a, const CService& b);
154  friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
155  friend bool operator<(const CService& a, const CService& b);
156  std::vector<unsigned char> GetKey() const;
157  std::string ToString() const;
158  std::string ToStringPort() const;
159  std::string ToStringIPPort() const;
160 
161  CService(const struct in6_addr& ipv6Addr, unsigned short port);
162  explicit CService(const struct sockaddr_in6& addr);
163 
165 
166  template <typename Stream, typename Operation>
167  inline void SerializationOp(Stream& s, Operation ser_action) {
168  READWRITE(ip);
169  READWRITE(WrapBigEndian(port));
170  }
171 };
172 
173 #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:511
friend class CSubNet
Definition: netaddress.h:99
bool IsLocal() const
Definition: netaddress.cpp:223
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:143
bool IsInternal() const
Definition: netaddress.cpp:305
CNetAddr network
Network (base) address.
Definition: netaddress.h:106
bool IsBindAny() const
Definition: netaddress.cpp:114
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:376
std::string ToString() const
Definition: netaddress.cpp:351
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:394
BigEndian< I > WrapBigEndian(I &n)
Definition: serialize.h:530
enum Network GetNetwork() const
Definition: netaddress.cpp:310
bool IsRFC2544() const
Definition: netaddress.cpp:142
ADD_SERIALIZE_METHODS
Definition: netaddress.h:164
bool IsRFC4862() const
Definition: netaddress.cpp:185
bool IsValid() const
Definition: netaddress.cpp:247
std::vector< unsigned char > GetGroup() const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:413
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.h:89
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.h:126
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:154
std::string ToStringIP() const
Definition: netaddress.cpp:327
const char * name
Definition: rest.cpp:39
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
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:295
uint64_t GetHash() const
Definition: netaddress.cpp:489
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:110
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:167
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:361
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:92
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:356
unsigned char ip[16]
Definition: netaddress.h:35
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:132
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 IsRFC7343() const
Definition: netaddress.cpp:207
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:218
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:95
ADD_SERIALIZE_METHODS
Definition: netaddress.h:129
bool IsRFC3964() const
Definition: netaddress.cpp:169
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:22