2012-05-25 15:41:27 +02:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
2011-12-16 16:56:36 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
2012-05-25 15:41:27 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-12-16 16:56:36 +01:00
|
|
|
#ifndef BITCOIN_NETBASE_H
|
|
|
|
#define BITCOIN_NETBASE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
#include "serialize.h"
|
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
extern int nConnectTimeout;
|
2011-12-16 16:56:36 +01:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2012-05-25 15:41:27 +02:00
|
|
|
// In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
|
|
|
|
#undef SetPort
|
2011-12-16 16:56:36 +01:00
|
|
|
#endif
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
enum Network
|
|
|
|
{
|
|
|
|
NET_UNROUTABLE,
|
|
|
|
NET_IPV4,
|
|
|
|
NET_IPV6,
|
|
|
|
NET_TOR,
|
|
|
|
NET_I2P,
|
2011-12-16 16:56:36 +01:00
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
NET_MAX,
|
|
|
|
};
|
2011-12-16 16:56:36 +01:00
|
|
|
|
|
|
|
extern int nConnectTimeout;
|
2012-05-25 15:41:27 +02:00
|
|
|
extern bool fNameLookup;
|
2011-12-16 16:56:36 +01:00
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
|
|
|
|
class CNetAddr
|
2011-12-16 16:56:36 +01:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned char ip[16]; // in network byte order
|
|
|
|
|
|
|
|
public:
|
2012-05-25 15:41:27 +02:00
|
|
|
CNetAddr();
|
|
|
|
CNetAddr(const struct in_addr& ipv4Addr);
|
|
|
|
explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
|
|
|
|
explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
|
2011-12-16 16:56:36 +01:00
|
|
|
void Init();
|
2012-05-25 15:41:27 +02:00
|
|
|
void SetIP(const CNetAddr& ip);
|
|
|
|
bool SetSpecial(const std::string &strName); // for Tor and I2P addresses
|
2011-12-16 16:56:36 +01:00
|
|
|
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
2012-05-25 15:41:27 +02:00
|
|
|
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor/I2P)
|
2011-12-16 16:56:36 +01:00
|
|
|
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 IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
2012-09-29 18:08:38 +02:00
|
|
|
bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
|
2011-12-16 16:56:36 +01:00
|
|
|
bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
|
2012-09-29 18:08:38 +02:00
|
|
|
bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
|
2011-12-16 16:56:36 +01:00
|
|
|
bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
|
|
|
|
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|
|
|
bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
|
|
|
|
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
|
2012-05-25 15:41:27 +02:00
|
|
|
bool IsTor() const;
|
|
|
|
bool IsI2P() const;
|
2011-12-16 16:56:36 +01:00
|
|
|
bool IsLocal() const;
|
|
|
|
bool IsRoutable() const;
|
|
|
|
bool IsValid() const;
|
|
|
|
bool IsMulticast() const;
|
2012-05-25 15:41:27 +02:00
|
|
|
enum Network GetNetwork() const;
|
2011-12-16 16:56:36 +01:00
|
|
|
std::string ToString() const;
|
2012-05-25 15:41:27 +02:00
|
|
|
std::string ToStringIP() const;
|
2012-09-29 18:08:38 +02:00
|
|
|
unsigned int GetByte(int n) const;
|
|
|
|
uint64 GetHash() const;
|
2011-12-16 16:56:36 +01:00
|
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const;
|
|
|
|
std::vector<unsigned char> GetGroup() const;
|
2012-05-25 15:41:27 +02:00
|
|
|
int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
|
2011-12-16 16:56:36 +01:00
|
|
|
void print() const;
|
|
|
|
|
|
|
|
#ifdef USE_IPV6
|
2012-05-25 15:41:27 +02:00
|
|
|
CNetAddr(const struct in6_addr& pipv6Addr);
|
2011-12-16 16:56:36 +01:00
|
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
|
|
|
|
#endif
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
friend bool operator==(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
friend bool operator<(const CNetAddr& a, const CNetAddr& b);
|
2011-12-16 16:56:36 +01:00
|
|
|
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
|
|
(
|
|
|
|
READWRITE(FLATDATA(ip));
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
/** A combination of a network address (CNetAddr) and a (TCP) port */
|
|
|
|
class CService : public CNetAddr
|
2011-12-16 16:56:36 +01:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned short port; // host order
|
|
|
|
|
|
|
|
public:
|
2012-05-25 15:41:27 +02:00
|
|
|
CService();
|
|
|
|
CService(const CNetAddr& ip, unsigned short port);
|
|
|
|
CService(const struct in_addr& ipv4Addr, unsigned short port);
|
|
|
|
CService(const struct sockaddr_in& addr);
|
|
|
|
explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
|
|
|
|
explicit CService(const char *pszIpPort, bool fAllowLookup = false);
|
|
|
|
explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
|
|
|
|
explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
|
2011-12-16 16:56:36 +01:00
|
|
|
void Init();
|
|
|
|
void SetPort(unsigned short portIn);
|
|
|
|
unsigned short GetPort() const;
|
2012-05-25 15:41:27 +02:00
|
|
|
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
|
|
|
|
bool SetSockAddr(const struct sockaddr* paddr);
|
|
|
|
friend bool operator==(const CService& a, const CService& b);
|
|
|
|
friend bool operator!=(const CService& a, const CService& b);
|
|
|
|
friend bool operator<(const CService& a, const CService& b);
|
2011-12-16 16:56:36 +01:00
|
|
|
std::vector<unsigned char> GetKey() const;
|
|
|
|
std::string ToString() const;
|
2012-05-25 15:41:27 +02:00
|
|
|
std::string ToStringPort() const;
|
|
|
|
std::string ToStringIPPort() const;
|
2011-12-16 16:56:36 +01:00
|
|
|
void print() const;
|
|
|
|
|
|
|
|
#ifdef USE_IPV6
|
2012-05-25 15:41:27 +02:00
|
|
|
CService(const struct in6_addr& ipv6Addr, unsigned short port);
|
|
|
|
CService(const struct sockaddr_in6& addr);
|
2011-12-16 16:56:36 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
IMPLEMENT_SERIALIZE
|
|
|
|
(
|
2012-05-25 15:41:27 +02:00
|
|
|
CService* pthis = const_cast<CService*>(this);
|
2011-12-16 16:56:36 +01:00
|
|
|
READWRITE(FLATDATA(ip));
|
|
|
|
unsigned short portN = htons(port);
|
|
|
|
READWRITE(portN);
|
|
|
|
if (fRead)
|
|
|
|
pthis->port = ntohs(portN);
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
enum Network ParseNetwork(std::string net);
|
2012-09-29 18:08:38 +02:00
|
|
|
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
|
2012-05-25 15:41:27 +02:00
|
|
|
bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5);
|
|
|
|
bool GetProxy(enum Network net, CService &addrProxy);
|
|
|
|
bool IsProxy(const CNetAddr &addr);
|
|
|
|
bool SetNameProxy(CService addrProxy, int nSocksVersion = 5);
|
|
|
|
bool GetNameProxy();
|
|
|
|
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
|
|
|
|
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);
|
2011-12-16 16:56:36 +01:00
|
|
|
|
|
|
|
#endif
|