Anoncoin  0.9.4
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Copyright (c) 2013-2015 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef ANONCOIN_NETBASE_H
7 #define ANONCOIN_NETBASE_H
8 
9 #if defined(HAVE_CONFIG_H)
10 #include "config/anoncoin-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 #ifdef WIN32
21 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
22 #undef SetPort
23 #endif
24 
25 #ifdef ENABLE_I2PSAM
26 #include "i2pwrapper.h"
27 #endif
28 
29 enum Network
30 {
35 #ifdef ENABLE_I2PSAM
36  NET_NATIVE_I2P,
37 #endif
39 };
40 
41 extern int nConnectTimeout;
42 extern bool fNameLookup;
43 
45 class CNetAddr
46 {
47  protected:
48  unsigned char ip[16]; // in network byte order
49 #ifdef ENABLE_I2PSAM
50  unsigned char i2pDest[NATIVE_I2P_DESTINATION_SIZE]; // I2P Destination
51 #endif
52  public:
53  CNetAddr();
54  CNetAddr(const struct in_addr& ipv4Addr);
55  explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
56  explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
57  void Init();
58  void SetIP(const CNetAddr& ip);
59  bool SetSpecial(const std::string &strName); // for Tor addresses
60  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
61  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
62  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
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 IsValid() const;
76  bool IsMulticast() 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 = NULL) const;
85  void print() const;
86 #ifdef ENABLE_I2PSAM
87  bool IsNativeI2P() const;
88  std::string GetI2PDestination() const;
89 #endif
90  CNetAddr(const struct in6_addr& pipv6Addr);
91  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
92 
93  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
94  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
95  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
96 
98  (
99  READWRITE(FLATDATA(ip));
100 #ifdef ENABLE_I2PSAM
101  if (!(nType & SER_IPADDRONLY)) {
102  READWRITE(FLATDATA(i2pDest));
103  }
104 #endif
105  )
106 };
107 
109 class CService : public CNetAddr
110 {
111  protected:
112  unsigned short port; // host order
113 
114  public:
115  CService();
116  CService(const CNetAddr& ip, unsigned short port);
117  CService(const struct in_addr& ipv4Addr, unsigned short port);
118  CService(const struct sockaddr_in& addr);
119  explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
120  explicit CService(const char *pszIpPort, bool fAllowLookup = false);
121  explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
122  explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
123  void Init();
124  void SetPort(unsigned short portIn);
125  unsigned short GetPort() const;
126  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
127  bool SetSockAddr(const struct sockaddr* paddr);
128  friend bool operator==(const CService& a, const CService& b);
129  friend bool operator!=(const CService& a, const CService& b);
130  friend bool operator<(const CService& a, const CService& b);
131  std::vector<unsigned char> GetKey() const;
132  std::string ToString() const;
133  std::string ToStringPort() const;
134  std::string ToStringIPPort() const;
135 #ifdef ENABLE_I2PSAM
136  std::string ToStringI2pPort() const;
137 #endif
138  void print() const;
139 
140  CService(const struct in6_addr& ipv6Addr, unsigned short port);
141  CService(const struct sockaddr_in6& addr);
142 
144  (
145  CService* pthis = const_cast<CService*>(this);
146  READWRITE(FLATDATA(ip));
147 #ifdef ENABLE_I2PSAM
148  if (!(nType & SER_IPADDRONLY)) {
149  READWRITE(FLATDATA(i2pDest));
150  }
151 #endif
152  unsigned short portN = htons(port);
153  READWRITE(portN);
154  if (fRead)
155  pthis->port = ntohs(portN);
156  )
157 };
158 
160 
161 enum Network ParseNetwork(std::string net);
162 std::string GetNetworkName(enum Network net);
163 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
164 bool SetProxy(enum Network net, CService addrProxy);
165 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
166 bool IsProxy(const CNetAddr &addr);
167 bool SetNameProxy(CService addrProxy);
168 bool HaveNameProxy();
169 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
170 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0);
171 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
172 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
173 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
174 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
175 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
177 std::string NetworkErrorString(int err);
178 #endif
bool IsRFC4843() const
Definition: netbase.cpp:766
#define READWRITE(obj)
Definition: serialize.h:101
void SetIP(const CNetAddr &ip)
Definition: netbase.cpp:613
bool Lookup(const char *pszName, CService &addr, int portDefault=0, bool fAllowLookup=true)
Definition: netbase.cpp:243
uint64_t GetHash() const
Definition: netbase.cpp:1039
bool HaveNameProxy()
Definition: netbase.cpp:501
std::string ToStringIP() const
Definition: netbase.cpp:882
void print() const
Definition: netbase.cpp:1051
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netbase.cpp:948
enum Network ParseNetwork(std::string net)
Definition: netbase.cpp:52
bool IsRFC6145() const
Definition: netbase.cpp:760
bool LookupNumeric(const char *pszName, CService &addr, int portDefault=0)
Definition: netbase.cpp:253
u_int SOCKET
Definition: compat.h:48
bool IsRFC4380() const
Definition: netbase.cpp:744
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:63
bool IsIPv6() const
Definition: netbase.cpp:706
int nConnectTimeout
Definition: netbase.cpp:47
bool IsLocal() const
Definition: netbase.cpp:792
#define FLATDATA(obj)
Definition: serialize.h:318
unsigned int GetByte(int n) const
Definition: netbase.cpp:696
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:506
bool IsRFC3927() const
Definition: netbase.cpp:723
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:922
bool LookupHostNumeric(const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions=0)
Definition: netbase.cpp:220
bool IsRFC4862() const
Definition: netbase.cpp:749
bool IsIPv4() const
Definition: netbase.cpp:701
bool IsRFC1918() const
Definition: netbase.cpp:715
int GetReachabilityFrom(const CNetAddr *paddrPartner=NULL) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netbase.cpp:1070
CService proxyType
Definition: netbase.h:159
bool IsRFC3964() const
Definition: netbase.cpp:733
bool IsMulticast() const
Definition: netbase.cpp:810
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: netbase.cpp:77
bool IsValid() const
Definition: netbase.cpp:816
bool IsRFC4193() const
Definition: netbase.cpp:755
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:109
unsigned short port
Definition: netbase.h:112
#define NATIVE_I2P_DESTINATION_SIZE
Definition: i2pwrapper.h:13
bool IsTor() const
Definition: netbase.cpp:771
bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, const char *pszDest, int portDefault=0, int nTimeout=nConnectTimeout)
Definition: netbase.cpp:573
void Init()
Definition: netbase.cpp:605
bool SetProxy(enum Network net, CService addrProxy)
Definition: netbase.cpp:467
bool IsRFC6052() const
Definition: netbase.cpp:738
bool fNameLookup
Definition: netbase.cpp:48
bool IsRoutable() const
Definition: netbase.cpp:855
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netbase.cpp:940
Network
Definition: netbase.h:29
std::vector< unsigned char > GetGroup() const
Definition: netbase.cpp:959
bool ConnectSocket(const CService &addr, SOCKET &hSocketRet, int nTimeout=nConnectTimeout)
Definition: netbase.cpp:541
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netbase.h:45
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:931
bool SetNameProxy(CService addrProxy)
Definition: netbase.cpp:485
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:913
bool IsRFC3849() const
Definition: netbase.cpp:728
unsigned char ip[16]
Definition: netbase.h:48
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:63
bool SetSpecial(const std::string &strName)
Returns TRUE if the address name can be looked up and resolved.
Definition: netbase.cpp:626
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: netbase.cpp:1357
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:476
bool LookupHost(const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions=0, bool fAllowLookup=true)
Definition: netbase.cpp:207
std::string ToString() const
Definition: netbase.cpp:908
enum Network GetNetwork() const
Definition: netbase.cpp:864
CNetAddr()
Definition: netbase.cpp:658