Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
netbase.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_NETBASE_H
6 #define BITCOIN_NETBASE_H
7 
8 #if defined(HAVE_CONFIG_H)
9 #include "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 extern int nConnectTimeout;
20 
21 #ifdef WIN32
22 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
23 #undef SetPort
24 #endif
25 
26 enum Network
27 {
32 
34 };
35 
36 extern int nConnectTimeout;
37 extern bool fNameLookup;
38 
40 class CNetAddr
41 {
42  protected:
43  unsigned char ip[16]; // in network byte order
44 
45  public:
46  CNetAddr();
47  CNetAddr(const struct in_addr& ipv4Addr);
48  explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
49  explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
50  void Init();
51  void SetIP(const CNetAddr& ip);
52  bool SetSpecial(const std::string &strName); // for Tor addresses
53  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
54  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
55  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
56  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
57  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
58  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
59  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
60  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
61  bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
62  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
63  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
64  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
65  bool IsTor() const;
66  bool IsLocal() const;
67  bool IsRoutable() const;
68  bool IsValid() const;
69  bool IsMulticast() const;
70  enum Network GetNetwork() const;
71  std::string ToString() const;
72  std::string ToStringIP() const;
73  unsigned int GetByte(int n) const;
74  uint64_t GetHash() const;
75  bool GetInAddr(struct in_addr* pipv4Addr) const;
76  std::vector<unsigned char> GetGroup() const;
77  int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
78  void print() const;
79 
80  CNetAddr(const struct in6_addr& pipv6Addr);
81  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
82 
83  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
84  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
85  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
86 
88  (
89  READWRITE(FLATDATA(ip));
90  )
91 };
92 
94 class CService : public CNetAddr
95 {
96  protected:
97  unsigned short port; // host order
98 
99  public:
100  CService();
101  CService(const CNetAddr& ip, unsigned short port);
102  CService(const struct in_addr& ipv4Addr, unsigned short port);
103  CService(const struct sockaddr_in& addr);
104  explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
105  explicit CService(const char *pszIpPort, bool fAllowLookup = false);
106  explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
107  explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
108  void Init();
109  void SetPort(unsigned short portIn);
110  unsigned short GetPort() const;
111  bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
112  bool SetSockAddr(const struct sockaddr* paddr);
113  friend bool operator==(const CService& a, const CService& b);
114  friend bool operator!=(const CService& a, const CService& b);
115  friend bool operator<(const CService& a, const CService& b);
116  std::vector<unsigned char> GetKey() const;
117  std::string ToString() const;
118  std::string ToStringPort() const;
119  std::string ToStringIPPort() const;
120  void print() const;
121 
122  CService(const struct in6_addr& ipv6Addr, unsigned short port);
123  CService(const struct sockaddr_in6& addr);
124 
126  (
127  CService* pthis = const_cast<CService*>(this);
128  READWRITE(FLATDATA(ip));
129  unsigned short portN = htons(port);
130  READWRITE(portN);
131  if (fRead)
132  pthis->port = ntohs(portN);
133  )
134 };
135 
136 typedef std::pair<CService, int> proxyType;
137 
138 enum Network ParseNetwork(std::string net);
139 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
140 bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5);
141 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
142 bool IsProxy(const CNetAddr &addr);
143 bool SetNameProxy(CService addrProxy, int nSocksVersion = 5);
144 bool HaveNameProxy();
145 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
146 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0);
147 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
148 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
149 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
150 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
151 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
153 std::string NetworkErrorString(int err);
154 
155 #endif
bool IsRFC4843() const
Definition: netbase.cpp:665
#define READWRITE(obj)
Definition: serialize.h:92
void SetIP(const CNetAddr &ip)
Definition: netbase.cpp:546
bool Lookup(const char *pszName, CService &addr, int portDefault=0, bool fAllowLookup=true)
Definition: netbase.cpp:151
uint64_t GetHash() const
Definition: netbase.cpp:875
bool HaveNameProxy()
Definition: netbase.cpp:457
std::string ToStringIP() const
Definition: netbase.cpp:750
void print() const
Definition: netbase.cpp:883
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Definition: netbase.cpp:800
enum Network ParseNetwork(std::string net)
Definition: netbase.cpp:35
bool IsRFC6145() const
Definition: netbase.cpp:659
bool LookupNumeric(const char *pszName, CService &addr, int portDefault=0)
Definition: netbase.cpp:161
u_int SOCKET
Definition: compat.h:47
bool IsRFC4380() const
Definition: netbase.cpp:643
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:54
bool IsIPv6() const
Definition: netbase.cpp:609
int nConnectTimeout
Definition: netbase.h:36
bool IsLocal() const
Definition: netbase.cpp:675
#define FLATDATA(obj)
Definition: serialize.h:309
unsigned int GetByte(int n) const
Definition: netbase.cpp:599
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:462
bool IsRFC3927() const
Definition: netbase.cpp:622
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:782
bool LookupHostNumeric(const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions=0)
Definition: netbase.cpp:128
bool IsRFC4862() const
Definition: netbase.cpp:648
bool IsIPv4() const
Definition: netbase.cpp:604
bool IsRFC1918() const
Definition: netbase.cpp:614
int GetReachabilityFrom(const CNetAddr *paddrPartner=NULL) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netbase.cpp:902
bool IsRFC3964() const
Definition: netbase.cpp:632
bool IsMulticast() const
Definition: netbase.cpp:689
void SplitHostPort(std::string in, int &portOut, std::string &hostOut)
Definition: netbase.cpp:43
bool IsValid() const
Definition: netbase.cpp:695
bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion=5)
Definition: netbase.cpp:419
bool IsRFC4193() const
Definition: netbase.cpp:654
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:94
unsigned short port
Definition: netbase.h:97
bool IsTor() const
Definition: netbase.cpp:670
bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, const char *pszDest, int portDefault=0, int nTimeout=nConnectTimeout)
Definition: netbase.cpp:504
void Init()
Definition: netbase.cpp:541
bool IsRFC6052() const
Definition: netbase.cpp:637
bool fNameLookup
Definition: netbase.cpp:31
bool IsRoutable() const
Definition: netbase.cpp:731
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netbase.cpp:792
Network
Definition: netbase.h:26
std::vector< unsigned char > GetGroup() const
Definition: netbase.cpp:808
bool SetNameProxy(CService addrProxy, int nSocksVersion=5)
Definition: netbase.cpp:439
bool ConnectSocket(const CService &addr, SOCKET &hSocketRet, int nTimeout=nConnectTimeout)
Definition: netbase.cpp:471
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netbase.h:40
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:787
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netbase.cpp:777
bool IsRFC3849() const
Definition: netbase.cpp:627
unsigned char ip[16]
Definition: netbase.h:43
std::pair< CService, int > proxyType
Definition: netbase.h:136
bool SetSpecial(const std::string &strName)
Definition: netbase.cpp:553
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
Definition: netbase.cpp:1143
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:430
bool LookupHost(const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions=0, bool fAllowLookup=true)
Definition: netbase.cpp:115
std::string ToString() const
Definition: netbase.cpp:772
enum Network GetNetwork() const
Definition: netbase.cpp:736
CNetAddr()
Definition: netbase.cpp:567