Raven Core  3.0.0
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_NETADDRESS_H
7 #define RAVEN_NETADDRESS_H
8 
9 #if defined(HAVE_CONFIG_H)
10 #include "config/raven-config.h"
11 #endif
12 
13 #include "compat.h"
14 #include "serialize.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 Init();
42  void SetIP(const CNetAddr& ip);
43 
48  void SetRaw(Network network, const uint8_t *data);
49 
54  bool SetInternal(const std::string& name);
55 
56  bool SetSpecial(const std::string &strName); // for Tor addresses
57  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
58  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
59  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
60  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
61  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
62  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
63  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
64  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
65  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
66  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
67  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
68  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
69  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
70  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
71  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
72  bool IsTor() const;
73  bool IsLocal() const;
74  bool IsRoutable() const;
75  bool IsInternal() const;
76  bool IsValid() const;
77  enum Network GetNetwork() const;
78  std::string ToString() const;
79  std::string ToStringIP() const;
80  unsigned int GetByte(int n) const;
81  uint64_t GetHash() const;
82  bool GetInAddr(struct in_addr* pipv4Addr) const;
83  std::vector<unsigned char> GetGroup() const;
84  int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
85 
86  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
87  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
88 
89  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
90  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
91  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
92 
94 
95  template <typename Stream, typename Operation>
96  inline void SerializationOp(Stream& s, Operation ser_action) {
97  READWRITE(FLATDATA(ip));
98  }
99 
100  friend class CSubNet;
101 };
102 
103 class CSubNet
104 {
105  protected:
109  uint8_t netmask[16];
111  bool valid;
112 
113  public:
114  CSubNet();
115  CSubNet(const CNetAddr &addr, int32_t mask);
116  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
117 
118  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
119  explicit CSubNet(const CNetAddr &addr);
120 
121  bool Match(const CNetAddr &addr) const;
122 
123  std::string ToString() const;
124  bool IsValid() const;
125 
126  friend bool operator==(const CSubNet& a, const CSubNet& b);
127  friend bool operator!=(const CSubNet& a, const CSubNet& b);
128  friend bool operator<(const CSubNet& a, const CSubNet& b);
129 
131 
132  template <typename Stream, typename Operation>
133  inline void SerializationOp(Stream& s, Operation ser_action) {
134  READWRITE(network);
135  READWRITE(FLATDATA(netmask));
136  READWRITE(FLATDATA(valid));
137  }
138 };
139 
141 class CService : public CNetAddr
142 {
143  protected:
144  unsigned short port; // host order
145 
146  public:
147  CService();
148  CService(const CNetAddr& ip, unsigned short port);
149  CService(const struct in_addr& ipv4Addr, unsigned short port);
150  explicit CService(const struct sockaddr_in& addr);
151  void Init();
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;
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) {
171  unsigned short portN = htons(port);
172  READWRITE(FLATDATA(portN));
173  if (ser_action.ForRead())
174  port = ntohs(portN);
175  }
176 };
177 
178 #endif // RAVEN_NETADDRESS_H
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:419
friend class CSubNet
Definition: netaddress.h:100
bool IsLocal() const
Definition: netaddress.cpp:184
bool IsRFC4380() const
Definition: netaddress.cpp:152
#define READWRITE(obj)
Definition: serialize.h:163
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:28
bool IsIPv6() const
Definition: netaddress.cpp:101
bool IsInternal() const
Definition: netaddress.cpp:242
CNetAddr network
Network (base) address.
Definition: netaddress.h:107
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:308
std::string ToString() const
Definition: netaddress.cpp:288
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:316
#define FLATDATA(obj)
Definition: serialize.h:366
enum Network GetNetwork() const
Definition: netaddress.cpp:247
bool IsRFC2544() const
Definition: netaddress.cpp:114
ADD_SERIALIZE_METHODS
Definition: netaddress.h:166
bool IsRFC4862() const
Definition: netaddress.cpp:157
bool IsValid() const
Definition: netaddress.cpp:198
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:324
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:298
bool IsIPv4() const
Definition: netaddress.cpp:96
bool IsRFC5737() const
Definition: netaddress.cpp:129
uint32_t scopeId
Definition: netaddress.h:36
bool IsRFC6145() const
Definition: netaddress.cpp:168
bool IsRFC6052() const
Definition: netaddress.cpp:146
std::string ToStringIP() const
Definition: netaddress.cpp:264
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:141
unsigned short port
Definition: netaddress.h:144
Network
Definition: netaddress.h:20
void Init()
Definition: netaddress.cpp:22
unsigned int GetByte(int n) const
Definition: netaddress.cpp:91
bool IsRFC3849() const
Definition: netaddress.cpp:136
bool IsRoutable() const
Definition: netaddress.cpp:237
uint64_t GetHash() const
Definition: netaddress.cpp:397
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:111
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:169
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:303
bool IsRFC1918() const
Definition: netaddress.cpp:106
bool IsRFC6598() const
Definition: netaddress.cpp:124
bool IsRFC3927() const
Definition: netaddress.cpp:119
ADD_SERIALIZE_METHODS
Definition: netaddress.h:93
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:293
unsigned char ip[16]
Definition: netaddress.h:35
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:133
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:33
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:61
bool IsRFC4843() const
Definition: netaddress.cpp:174
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:49
bool IsRFC4193() const
Definition: netaddress.cpp:163
bool IsTor() const
Definition: netaddress.cpp:179
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:96
ADD_SERIALIZE_METHODS
Definition: netaddress.h:130
bool IsRFC3964() const
Definition: netaddress.cpp:141