Fabcoin Core  0.16.2
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2017 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 FABCOIN_NETADDRESS_H
6 #define FABCOIN_NETADDRESS_H
7 
8 #if defined(HAVE_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 enum Network
20 {
26 
28 };
29 
31 class CNetAddr
32 {
33  protected:
34  unsigned char ip[16]; // in network byte order
35  uint32_t scopeId; // for scoped/link-local ipv6 addresses
36 
37  public:
38  CNetAddr();
39  CNetAddr(const struct in_addr& ipv4Addr);
40  void Init();
41  void SetIP(const CNetAddr& ip);
42 
47  void SetRaw(Network network, const uint8_t *data);
48 
53  bool SetInternal(const std::string& name);
54 
55  bool SetSpecial(const std::string &strName); // for Tor addresses
56  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
57  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
58  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
59  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
60  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
61  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
62  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
63  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
64  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
65  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
66  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
67  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
68  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
69  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
70  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
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  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);
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(FLATDATA(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);
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(FLATDATA(netmask));
135  READWRITE(FLATDATA(valid));
136  }
137 };
138 
140 class CService : public CNetAddr
141 {
142  protected:
143  unsigned short 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  CService(const struct sockaddr_in& addr);
150  void Init();
151  unsigned short GetPort() const;
152  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
153  bool SetSockAddr(const struct sockaddr* paddr);
154  friend bool operator==(const CService& a, const CService& b);
155  friend bool operator!=(const CService& a, const CService& b);
156  friend bool operator<(const CService& a, const CService& b);
157  std::vector<unsigned char> GetKey() const;
158  std::string ToString() const;
159  std::string ToStringPort() const;
160  std::string ToStringIPPort() const;
161 
162  CService(const struct in6_addr& ipv6Addr, unsigned short port);
163  CService(const struct sockaddr_in6& addr);
164 
166 
167  template <typename Stream, typename Operation>
168  inline void SerializationOp(Stream& s, Operation ser_action) {
170  unsigned short portN = htons(port);
171  READWRITE(FLATDATA(portN));
172  if (ser_action.ForRead())
173  port = ntohs(portN);
174  }
175 };
176 
177 #endif // FABCOIN_NETADDRESS_H
friend class CSubNet
Definition: netaddress.h:99
bool IsRFC4843() const
Definition: netaddress.cpp:173
#define READWRITE(obj)
Definition: serialize.h:179
bool IsInternal() const
Definition: netaddress.cpp:241
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:27
uint64_t GetHash() const
Definition: netaddress.cpp:396
std::string ToStringIP() const
Definition: netaddress.cpp:263
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:315
bool IsRFC6145() const
Definition: netaddress.cpp:167
bool IsRFC4380() const
Definition: netaddress.cpp:151
bool IsRFC5737() const
Definition: netaddress.cpp:128
CNetAddr network
Network (base) address.
Definition: netaddress.h:106
bool IsRFC2544() const
Definition: netaddress.cpp:113
bool IsIPv6() const
Definition: netaddress.cpp:100
bool IsLocal() const
Definition: netaddress.cpp:183
#define FLATDATA(obj)
Definition: serialize.h:388
ADD_SERIALIZE_METHODS
Definition: netaddress.h:165
unsigned int GetByte(int n) const
Definition: netaddress.cpp:90
bool IsRFC3927() const
Definition: netaddress.cpp:118
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:418
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:297
#define a(i)
bool IsRFC4862() const
Definition: netaddress.cpp:156
bool IsIPv4() const
Definition: netaddress.cpp:95
bool IsRFC1918() const
Definition: netaddress.cpp:105
uint32_t scopeId
Definition: netaddress.h:35
bool IsRFC3964() const
Definition: netaddress.cpp:140
bool IsValid() const
Definition: netaddress.cpp:197
const char * name
Definition: rest.cpp:36
bool IsRFC4193() const
Definition: netaddress.cpp:162
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
unsigned short port
Definition: netaddress.h:143
Network
Definition: netaddress.h:19
bool IsTor() const
Definition: netaddress.cpp:178
void Init()
Definition: netaddress.cpp:21
bool IsRFC6052() const
Definition: netaddress.cpp:145
bool IsRoutable() const
Definition: netaddress.cpp:236
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:110
#define b(i, j)
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:307
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:168
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:323
bool IsRFC6598() const
Definition: netaddress.cpp:123
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:302
ADD_SERIALIZE_METHODS
Definition: netaddress.h:92
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:292
bool IsRFC3849() const
Definition: netaddress.cpp:135
unsigned char ip[16]
Definition: netaddress.h:34
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)
Definition: netaddress.cpp:60
std::string ToString() const
Definition: netaddress.cpp:287
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:48
uint8_t const * data
Definition: sha3.h:19
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:95
ADD_SERIALIZE_METHODS
Definition: netaddress.h:129
enum Network GetNetwork() const
Definition: netaddress.cpp:246