Dogecoin Core  1.14.2
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 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/bitcoin-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  uint32_t scopeId; // for scoped/link-local ipv6 addresses
35 
36  public:
37  CNetAddr();
38  CNetAddr(const struct in_addr& ipv4Addr);
39  void Init();
40  void SetIP(const CNetAddr& ip);
41 
46  void SetRaw(Network network, const uint8_t *data);
47 
48  bool SetSpecial(const std::string &strName); // for Tor addresses
49  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
50  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
51  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
52  bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
53  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
54  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
55  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
56  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
57  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
58  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
59  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
60  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
61  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
62  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
63  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
64  bool IsTor() const;
65  bool IsLocal() const;
66  bool IsRoutable() const;
67  bool IsValid() const;
68  bool IsMulticast() const;
69  enum Network GetNetwork() const;
70  std::string ToString() const;
71  std::string ToStringIP() const;
72  unsigned int GetByte(int n) const;
73  uint64_t GetHash() const;
74  bool GetInAddr(struct in_addr* pipv4Addr) const;
75  std::vector<unsigned char> GetGroup() const;
76  int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
77 
78  CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
79  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
80 
81  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
82  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
83  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
84 
86 
87  template <typename Stream, typename Operation>
88  inline void SerializationOp(Stream& s, Operation ser_action) {
90  }
91 
92  friend class CSubNet;
93 };
94 
95 class CSubNet
96 {
97  protected:
101  uint8_t netmask[16];
103  bool valid;
104 
105  public:
106  CSubNet();
107  CSubNet(const CNetAddr &addr, int32_t mask);
108  CSubNet(const CNetAddr &addr, const CNetAddr &mask);
109 
110  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
111  explicit CSubNet(const CNetAddr &addr);
112 
113  bool Match(const CNetAddr &addr) const;
114 
115  std::string ToString() const;
116  bool IsValid() const;
117 
118  friend bool operator==(const CSubNet& a, const CSubNet& b);
119  friend bool operator!=(const CSubNet& a, const CSubNet& b);
120  friend bool operator<(const CSubNet& a, const CSubNet& b);
121 
123 
124  template <typename Stream, typename Operation>
125  inline void SerializationOp(Stream& s, Operation ser_action) {
129  }
130 };
131 
133 class CService : public CNetAddr
134 {
135  protected:
136  unsigned short port; // host order
137 
138  public:
139  CService();
140  CService(const CNetAddr& ip, unsigned short port);
141  CService(const struct in_addr& ipv4Addr, unsigned short port);
142  CService(const struct sockaddr_in& addr);
143  void Init();
144  void SetPort(unsigned short portIn);
145  unsigned short GetPort() const;
146  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
147  bool SetSockAddr(const struct sockaddr* paddr);
148  friend bool operator==(const CService& a, const CService& b);
149  friend bool operator!=(const CService& a, const CService& b);
150  friend bool operator<(const CService& a, const CService& b);
151  std::vector<unsigned char> GetKey() const;
152  std::string ToString() const;
153  std::string ToStringPort() const;
154  std::string ToStringIPPort() const;
155 
156  CService(const struct in6_addr& ipv6Addr, unsigned short port);
157  CService(const struct sockaddr_in6& addr);
158 
160 
161  template <typename Stream, typename Operation>
162  inline void SerializationOp(Stream& s, Operation ser_action) {
164  unsigned short portN = htons(port);
165  READWRITE(FLATDATA(portN));
166  if (ser_action.ForRead())
167  port = ntohs(portN);
168  }
169 };
170 
171 #endif // BITCOIN_NETADDRESS_H
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
uint32_t scopeId
Definition: netaddress.h:34
std::string ToStringIP() const
Definition: netaddress.cpp:243
bool IsRFC6052() const
Definition: netaddress.cpp:130
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:24
void SetRaw(Network network, const uint8_t *data)
Set raw IPv4 or IPv6 address (in network byte order)
Definition: netaddress.cpp:29
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netaddress.cpp:293
std::string ToString() const
Definition: netaddress.cpp:265
bool IsTor() const
Definition: netaddress.cpp:163
bool IsRoutable() const
Definition: netaddress.cpp:224
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:285
bool IsRFC5737() const
Definition: netaddress.cpp:113
bool IsRFC6598() const
Definition: netaddress.cpp:108
bool IsRFC1918() const
Definition: netaddress.cpp:90
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:270
std::vector< unsigned char > GetGroup() const
Definition: netaddress.cpp:301
bool IsValid() const
Definition: netaddress.cpp:188
bool IsIPv4() const
Definition: netaddress.cpp:80
bool IsRFC3849() const
Definition: netaddress.cpp:120
bool IsLocal() const
Definition: netaddress.cpp:168
uint64_t GetHash() const
Definition: netaddress.cpp:368
bool SetSpecial(const std::string &strName)
Definition: netaddress.cpp:45
int GetReachabilityFrom(const CNetAddr *paddrPartner=NULL) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:390
bool IsMulticast() const
Definition: netaddress.cpp:182
bool IsIPv6() const
Definition: netaddress.cpp:85
ADD_SERIALIZE_METHODS
Definition: netaddress.h:85
bool IsRFC4193() const
Definition: netaddress.cpp:147
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:275
bool IsRFC2544() const
Definition: netaddress.cpp:98
unsigned int GetByte(int n) const
Definition: netaddress.cpp:75
unsigned char ip[16]
Definition: netaddress.h:33
enum Network GetNetwork() const
Definition: netaddress.cpp:229
bool IsRFC6145() const
Definition: netaddress.cpp:152
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:88
void Init()
Definition: netaddress.cpp:18
bool IsRFC3964() const
Definition: netaddress.cpp:125
bool IsRFC4380() const
Definition: netaddress.cpp:136
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:280
bool IsRFC3927() const
Definition: netaddress.cpp:103
bool IsRFC4862() const
Definition: netaddress.cpp:141
bool IsRFC4843() const
Definition: netaddress.cpp:158
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:134
std::string ToStringIPPort() const
Definition: netaddress.cpp:559
std::string ToString() const
Definition: netaddress.cpp:568
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:509
ADD_SERIALIZE_METHODS
Definition: netaddress.h:159
void SetPort(unsigned short portIn)
Definition: netaddress.cpp:573
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.cpp:504
unsigned short GetPort() const
Definition: netaddress.cpp:494
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:480
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:499
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:162
std::string ToStringPort() const
Definition: netaddress.cpp:554
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:514
void Init()
Definition: netaddress.cpp:448
unsigned short port
Definition: netaddress.h:136
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:544
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:708
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:103
CNetAddr network
Network (base) address.
Definition: netaddress.h:99
ADD_SERIALIZE_METHODS
Definition: netaddress.h:122
friend bool operator==(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:703
void SerializationOp(Stream &s, Operation ser_action)
Definition: netaddress.h:125
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:101
std::string ToString() const
Definition: netaddress.cpp:660
bool IsValid() const
Definition: netaddress.cpp:698
friend bool operator<(const CSubNet &a, const CSubNet &b)
Definition: netaddress.cpp:713
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:634
Network
Definition: netaddress.h:20
@ NET_MAX
Definition: netaddress.h:26
@ NET_IPV6
Definition: netaddress.h:23
@ NET_TOR
Definition: netaddress.h:24
@ NET_IPV4
Definition: netaddress.h:22
@ NET_UNROUTABLE
Definition: netaddress.h:21
#define READWRITE(obj)
Definition: serialize.h:151
#define FLATDATA(obj)
Definition: serialize.h:347