Dash Core  0.12.2.1
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 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)
9 #include "config/dash-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 {
25 
27 };
28 
30 class CNetAddr
31 {
32  protected:
33  unsigned char ip[16]; // in network byte order
34 
35  public:
36  CNetAddr();
37  CNetAddr(const struct in_addr& ipv4Addr);
38  void Init();
39  void SetIP(const CNetAddr& ip);
40 
45  void SetRaw(Network network, const uint8_t *data);
46 
47  bool SetSpecial(const std::string &strName); // for Tor addresses
48  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
49  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
50  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
51  bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
52  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
53  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
54  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
55  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
56  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
57  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
58  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
59  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
60  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
61  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
62  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
63  bool IsTor() const;
64  bool IsLocal() const;
65  bool IsRoutable() const;
66  bool IsValid() const;
67  bool IsMulticast() const;
68  enum Network GetNetwork() const;
69  std::string ToString() const;
70  std::string ToStringIP(bool fUseGetnameinfo = true) const;
71  unsigned int GetByte(int n) const;
72  uint64_t GetHash() const;
73  bool GetInAddr(struct in_addr* pipv4Addr) const;
74  std::vector<unsigned char> GetGroup() const;
75  int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
76 
77  CNetAddr(const struct in6_addr& pipv6Addr);
78  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
79 
80  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
81  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
82  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
83 
85 
86  template <typename Stream, typename Operation>
87  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
89  }
90 
91  friend class CSubNet;
92 };
93 
94 class CSubNet
95 {
96  protected:
100  uint8_t netmask[16];
102  bool valid;
103 
104  public:
105  CSubNet();
106  CSubNet(const CNetAddr &addr, int32_t mask);
107  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
108 
109  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
110  explicit CSubNet(const CNetAddr &addr);
111 
112  bool Match(const CNetAddr &addr) const;
113 
114  std::string ToString() const;
115  bool IsValid() const;
116 
117  friend bool operator==(const CSubNet& a, const CSubNet& b);
118  friend bool operator!=(const CSubNet& a, const CSubNet& b);
119  friend bool operator<(const CSubNet& a, const CSubNet& b);
120 
122 
123  template <typename Stream, typename Operation>
124  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
128  }
129 };
130 
132 class CService : public CNetAddr
133 {
134  protected:
135  unsigned short port; // host order
136 
137  public:
138  CService();
139  CService(const CNetAddr& ip, unsigned short port);
140  CService(const struct in_addr& ipv4Addr, unsigned short port);
141  CService(const struct sockaddr_in& addr);
142  void Init();
143  void SetPort(unsigned short portIn);
144  unsigned short GetPort() const;
145  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
146  bool SetSockAddr(const struct sockaddr* paddr);
147  friend bool operator==(const CService& a, const CService& b);
148  friend bool operator!=(const CService& a, const CService& b);
149  friend bool operator<(const CService& a, const CService& b);
150  std::vector<unsigned char> GetKey() const;
151  std::string ToString(bool fUseGetnameinfo = true) const;
152  std::string ToStringPort() const;
153  std::string ToStringIPPort(bool fUseGetnameinfo = true) const;
154 
155  CService(const struct in6_addr& ipv6Addr, unsigned short port);
156  CService(const struct sockaddr_in6& addr);
157 
159 
160  template <typename Stream, typename Operation>
161  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
163  unsigned short portN = htons(port);
164  READWRITE(FLATDATA(portN));
165  if (ser_action.ForRead())
166  port = ntohs(portN);
167  }
168 };
169 
170 #endif // BITCOIN_NETADDRESS_H
bool IsRFC6052() const
Definition: netaddress.cpp:128
bool IsRFC5737() const
Definition: netaddress.cpp:111
bool IsLocal() const
Definition: netaddress.cpp:166
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:510
#define READWRITE(obj)
Definition: serialize.h:175
void Init()
Definition: netaddress.cpp:449
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:544
bool IsRFC4862() const
Definition: netaddress.cpp:139
std::string ToStringIP(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:241
std::string ToString() const
Definition: netaddress.cpp:660
bool IsRFC4193() const
Definition: netaddress.cpp:145
bool IsRFC4843() const
Definition: netaddress.cpp:156
ADD_SERIALIZE_METHODS
Definition: netaddress.h:158
bool IsValid() const
Definition: netaddress.cpp:186
bool IsRFC2544() const
Definition: netaddress.cpp:96
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:100
void SetRaw(Network network, const uint8_t *data)
Definition: netaddress.cpp:28
bool IsRFC3927() const
Definition: netaddress.cpp:101
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:302
bool IsRFC3849() const
Definition: netaddress.cpp:118
#define FLATDATA(obj)
Definition: serialize.h:387
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:708
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:713
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:23
unsigned short GetPort() const
Definition: netaddress.cpp:495
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:271
bool IsIPv6() const
Definition: netaddress.cpp:83
bool IsIPv4() const
Definition: netaddress.cpp:78
std::string ToStringIPPort(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:559
CNetAddr network
Network (base) address.
Definition: netaddress.h:98
bool IsRFC6598() const
Definition: netaddress.cpp:106
void SetPort(unsigned short portIn)
Definition: netaddress.cpp:573
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.cpp:505
unsigned short port
Definition: netaddress.h:135
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:276
bool IsRFC6145() const
Definition: netaddress.cpp:150
void Init()
Definition: netaddress.cpp:18
int GetReachabilityFrom(const CNetAddr *paddrPartner=NULL) const
Definition: netaddress.cpp:391
Network
Definition: netaddress.h:19
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:281
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:294
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:102
bool IsRFC3964() const
Definition: netaddress.cpp:123
uint64_t GetHash() const
Definition: netaddress.cpp:369
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: netaddress.h:124
std::string ToStringPort() const
Definition: netaddress.cpp:554
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:481
ADD_SERIALIZE_METHODS
Definition: netaddress.h:121
bool IsValid() const
Definition: netaddress.cpp:698
bool IsRFC1918() const
Definition: netaddress.cpp:88
std::string ToString() const
Definition: netaddress.cpp:266
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: netaddress.h:161
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:500
bool IsRoutable() const
Definition: netaddress.cpp:222
unsigned int GetByte(int n) const
Definition: netaddress.cpp:73
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:44
ADD_SERIALIZE_METHODS
Definition: netaddress.h:84
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:286
bool IsMulticast() const
Definition: netaddress.cpp:180
bool IsRFC4380() const
Definition: netaddress.cpp:134
enum Network GetNetwork() const
Definition: netaddress.cpp:227
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:515
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: netaddress.h:87
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:703
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:634
unsigned char ip[16]
Definition: netaddress.h:33
bool IsTor() const
Definition: netaddress.cpp:161