|
|
@ -1,49 +1,52 @@ |
|
|
|
// Copyright (c) 2011 The Bitcoin developers
|
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_NETBASE_H |
|
|
|
#ifndef BITCOIN_NETBASE_H |
|
|
|
#define BITCOIN_NETBASE_H |
|
|
|
#define BITCOIN_NETBASE_H |
|
|
|
|
|
|
|
|
|
|
|
#include <string> |
|
|
|
#include <string> |
|
|
|
#include <vector> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "serialize.h" |
|
|
|
|
|
|
|
#include "compat.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern int nConnectTimeout; |
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32 |
|
|
|
#ifdef WIN32 |
|
|
|
#include <winsock2.h> |
|
|
|
// In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
|
|
|
|
#include <mswsock.h> |
|
|
|
#undef SetPort |
|
|
|
#include <ws2tcpip.h> |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
|
|
|
|
|
#include <sys/socket.h> |
|
|
|
|
|
|
|
#include <arpa/inet.h> |
|
|
|
|
|
|
|
#include <netdb.h> |
|
|
|
|
|
|
|
#include <net/if.h> |
|
|
|
|
|
|
|
#include <ifaddrs.h> |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
#ifdef BSD |
|
|
|
|
|
|
|
#include <netinet/in.h> |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#include "serialize.h" |
|
|
|
enum Network |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
NET_UNROUTABLE, |
|
|
|
|
|
|
|
NET_IPV4, |
|
|
|
|
|
|
|
NET_IPV6, |
|
|
|
|
|
|
|
NET_TOR, |
|
|
|
|
|
|
|
NET_I2P, |
|
|
|
|
|
|
|
|
|
|
|
typedef int SOCKET; |
|
|
|
NET_MAX, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
extern int nConnectTimeout; |
|
|
|
extern int nConnectTimeout; |
|
|
|
|
|
|
|
extern bool fNameLookup; |
|
|
|
|
|
|
|
|
|
|
|
// IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
|
|
|
|
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */ |
|
|
|
class CIP |
|
|
|
class CNetAddr |
|
|
|
{ |
|
|
|
{ |
|
|
|
protected: |
|
|
|
protected: |
|
|
|
unsigned char ip[16]; // in network byte order
|
|
|
|
unsigned char ip[16]; // in network byte order
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
CIP(); |
|
|
|
CNetAddr(); |
|
|
|
CIP(const struct in_addr& ipv4Addr); |
|
|
|
CNetAddr(const struct in_addr& ipv4Addr); |
|
|
|
CIP(const char *pszIp, bool fAllowLookup = false); |
|
|
|
explicit CNetAddr(const char *pszIp, bool fAllowLookup = false); |
|
|
|
CIP(const std::string &strIp, bool fAllowLookup = false); |
|
|
|
explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false); |
|
|
|
void Init(); |
|
|
|
void Init(); |
|
|
|
void SetIP(const CIP& ip); |
|
|
|
void SetIP(const CNetAddr& ip); |
|
|
|
|
|
|
|
bool SetSpecial(const std::string &strName); // for Tor and I2P addresses
|
|
|
|
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
|
|
|
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
|
|
|
|
|
|
|
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor/I2P)
|
|
|
|
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
|
|
|
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
|
|
|
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|
|
|
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|
|
|
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
|
|
|
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
|
|
@ -54,25 +57,30 @@ class CIP |
|
|
|
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|
|
|
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|
|
|
bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
|
|
|
|
bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
|
|
|
|
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
|
|
|
|
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
|
|
|
|
|
|
|
|
bool IsTor() const; |
|
|
|
|
|
|
|
bool IsI2P() const; |
|
|
|
bool IsLocal() const; |
|
|
|
bool IsLocal() const; |
|
|
|
bool IsRoutable() const; |
|
|
|
bool IsRoutable() const; |
|
|
|
bool IsValid() const; |
|
|
|
bool IsValid() const; |
|
|
|
bool IsMulticast() const; |
|
|
|
bool IsMulticast() const; |
|
|
|
|
|
|
|
enum Network GetNetwork() const; |
|
|
|
std::string ToString() const; |
|
|
|
std::string ToString() const; |
|
|
|
|
|
|
|
std::string ToStringIP() const; |
|
|
|
int GetByte(int n) const; |
|
|
|
int GetByte(int n) const; |
|
|
|
int64 GetHash() const; |
|
|
|
int64 GetHash() const; |
|
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const; |
|
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const; |
|
|
|
std::vector<unsigned char> GetGroup() const; |
|
|
|
std::vector<unsigned char> GetGroup() const; |
|
|
|
|
|
|
|
int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const; |
|
|
|
void print() const; |
|
|
|
void print() const; |
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_IPV6 |
|
|
|
#ifdef USE_IPV6 |
|
|
|
CIP(const struct in6_addr& pipv6Addr); |
|
|
|
CNetAddr(const struct in6_addr& pipv6Addr); |
|
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const; |
|
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
friend bool operator==(const CIP& a, const CIP& b); |
|
|
|
friend bool operator==(const CNetAddr& a, const CNetAddr& b); |
|
|
|
friend bool operator!=(const CIP& a, const CIP& b); |
|
|
|
friend bool operator!=(const CNetAddr& a, const CNetAddr& b); |
|
|
|
friend bool operator<(const CIP& a, const CIP& b); |
|
|
|
friend bool operator<(const CNetAddr& a, const CNetAddr& b); |
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_SERIALIZE |
|
|
|
IMPLEMENT_SERIALIZE |
|
|
|
( |
|
|
|
( |
|
|
@ -80,41 +88,43 @@ class CIP |
|
|
|
) |
|
|
|
) |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class CIPPort : public CIP |
|
|
|
/** A combination of a network address (CNetAddr) and a (TCP) port */ |
|
|
|
|
|
|
|
class CService : public CNetAddr |
|
|
|
{ |
|
|
|
{ |
|
|
|
protected: |
|
|
|
protected: |
|
|
|
unsigned short port; // host order
|
|
|
|
unsigned short port; // host order
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
CIPPort(); |
|
|
|
CService(); |
|
|
|
CIPPort(const CIP& ip, unsigned short port); |
|
|
|
CService(const CNetAddr& ip, unsigned short port); |
|
|
|
CIPPort(const struct in_addr& ipv4Addr, unsigned short port); |
|
|
|
CService(const struct in_addr& ipv4Addr, unsigned short port); |
|
|
|
CIPPort(const struct sockaddr_in& addr); |
|
|
|
CService(const struct sockaddr_in& addr); |
|
|
|
CIPPort(const char *pszIp, int port, bool fAllowLookup = false); |
|
|
|
explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false); |
|
|
|
CIPPort(const char *pszIpPort, bool fAllowLookup = false); |
|
|
|
explicit CService(const char *pszIpPort, bool fAllowLookup = false); |
|
|
|
CIPPort(const std::string& strIp, int port, bool fAllowLookup = false); |
|
|
|
explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false); |
|
|
|
CIPPort(const std::string& strIpPort, bool fAllowLookup = false); |
|
|
|
explicit CService(const std::string& strIpPort, bool fAllowLookup = false); |
|
|
|
void Init(); |
|
|
|
void Init(); |
|
|
|
void SetPort(unsigned short portIn); |
|
|
|
void SetPort(unsigned short portIn); |
|
|
|
unsigned short GetPort() const; |
|
|
|
unsigned short GetPort() const; |
|
|
|
bool GetSockAddr(struct sockaddr_in* paddr) const; |
|
|
|
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; |
|
|
|
bool ConnectSocket(SOCKET& hSocketRet, int nTimeout = nConnectTimeout) const; |
|
|
|
bool SetSockAddr(const struct sockaddr* paddr); |
|
|
|
friend bool operator==(const CIPPort& a, const CIPPort& b); |
|
|
|
friend bool operator==(const CService& a, const CService& b); |
|
|
|
friend bool operator!=(const CIPPort& a, const CIPPort& b); |
|
|
|
friend bool operator!=(const CService& a, const CService& b); |
|
|
|
friend bool operator<(const CIPPort& a, const CIPPort& b); |
|
|
|
friend bool operator<(const CService& a, const CService& b); |
|
|
|
std::vector<unsigned char> GetKey() const; |
|
|
|
std::vector<unsigned char> GetKey() const; |
|
|
|
std::string ToString() const; |
|
|
|
std::string ToString() const; |
|
|
|
|
|
|
|
std::string ToStringPort() const; |
|
|
|
|
|
|
|
std::string ToStringIPPort() const; |
|
|
|
void print() const; |
|
|
|
void print() const; |
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_IPV6 |
|
|
|
#ifdef USE_IPV6 |
|
|
|
CIPPort(const struct in6_addr& ipv6Addr, unsigned short port); |
|
|
|
CService(const struct in6_addr& ipv6Addr, unsigned short port); |
|
|
|
bool GetSockAddr6(struct sockaddr_in6* paddr) const; |
|
|
|
CService(const struct sockaddr_in6& addr); |
|
|
|
CIPPort(const struct sockaddr_in6& addr); |
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_SERIALIZE |
|
|
|
IMPLEMENT_SERIALIZE |
|
|
|
( |
|
|
|
( |
|
|
|
CIPPort* pthis = const_cast<CIPPort*>(this); |
|
|
|
CService* pthis = const_cast<CService*>(this); |
|
|
|
READWRITE(FLATDATA(ip)); |
|
|
|
READWRITE(FLATDATA(ip)); |
|
|
|
unsigned short portN = htons(port); |
|
|
|
unsigned short portN = htons(port); |
|
|
|
READWRITE(portN); |
|
|
|
READWRITE(portN); |
|
|
@ -123,13 +133,18 @@ class CIPPort : public CIP |
|
|
|
) |
|
|
|
) |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
bool LookupHost(const char *pszName, std::vector<CIP>& vIP, int nMaxSolutions = 0, bool fAllowLookup = true); |
|
|
|
enum Network ParseNetwork(std::string net); |
|
|
|
bool LookupHostNumeric(const char *pszName, std::vector<CIP>& vIP, int nMaxSolutions = 0); |
|
|
|
bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5); |
|
|
|
bool Lookup(const char *pszName, CIPPort& addr, int portDefault = 0, bool fAllowLookup = true); |
|
|
|
bool GetProxy(enum Network net, CService &addrProxy); |
|
|
|
bool LookupNumeric(const char *pszName, CIPPort& addr, int portDefault = 0); |
|
|
|
bool IsProxy(const CNetAddr &addr); |
|
|
|
|
|
|
|
bool SetNameProxy(CService addrProxy, int nSocksVersion = 5); |
|
|
|
// Settings
|
|
|
|
bool GetNameProxy(); |
|
|
|
extern int fUseProxy; |
|
|
|
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true); |
|
|
|
extern CIPPort addrProxy; |
|
|
|
bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0); |
|
|
|
|
|
|
|
bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); |
|
|
|
|
|
|
|
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); |
|
|
|
|
|
|
|
bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); |
|
|
|
|
|
|
|
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); |
|
|
|
|
|
|
|
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); |
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|