b4a3da2642
These are technically static libs, but not in any way shared libs. They are used solely only by this repo and really only by the hub. Most important, no header files are installed and basically none of the normal rules for reusable libraries are applied to these files.
249 lines
9.4 KiB
C++
249 lines
9.4 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef FLOWEE_NETBASE_H
|
|
#define FLOWEE_NETBASE_H
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
#include "config/flowee-config.h"
|
|
#endif
|
|
|
|
#include "compat.h"
|
|
#include "serialize.h"
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
extern int nConnectTimeout;
|
|
extern bool fNameLookup;
|
|
|
|
#ifdef WIN32
|
|
// In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
|
|
#undef SetPort
|
|
#endif
|
|
|
|
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
|
|
class CNetAddr
|
|
{
|
|
protected:
|
|
unsigned char ip[16]; // in network byte order
|
|
|
|
public:
|
|
|
|
enum Network
|
|
{
|
|
NET_UNROUTABLE = 0,
|
|
NET_IPV4,
|
|
NET_IPV6,
|
|
NET_TOR,
|
|
|
|
NET_MAX,
|
|
};
|
|
CNetAddr();
|
|
CNetAddr(const struct in_addr& ipv4Addr);
|
|
explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
|
|
explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
|
|
void Init();
|
|
void SetIP(const CNetAddr& ip);
|
|
|
|
/**
|
|
* Set raw IPv4 or IPv6 address (in network byte order)
|
|
* @note Only NET_IPV4 and NET_IPV6 are allowed for network.
|
|
*/
|
|
void SetRaw(Network network, const uint8_t *data);
|
|
|
|
bool SetSpecial(const std::string &strName); // for Tor addresses
|
|
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)
|
|
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
|
bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
|
|
bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
|
|
bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
|
|
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
|
bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
|
|
bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
|
|
bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
|
|
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)
|
|
bool IsTor() const;
|
|
bool IsLocal() const;
|
|
bool IsRoutable() const;
|
|
bool IsValid() const;
|
|
bool IsMulticast() const;
|
|
enum Network GetNetwork() const;
|
|
std::string ToString() const;
|
|
std::string ToStringIP() const;
|
|
unsigned int GetByte(int n) const;
|
|
uint64_t GetHash() const;
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const;
|
|
std::vector<unsigned char> GetGroup() const;
|
|
int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
|
|
|
|
CNetAddr(const struct in6_addr& pipv6Addr);
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
|
|
|
|
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);
|
|
|
|
ADD_SERIALIZE_METHODS
|
|
|
|
template <typename Stream, typename Operation>
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
READWRITE(FLATDATA(ip));
|
|
}
|
|
|
|
friend class CSubNet;
|
|
};
|
|
|
|
inline Log::SilentItem operator<<(Log::SilentItem item, const CNetAddr&) { return item; }
|
|
inline Log::Item operator<<(Log::Item item, const CNetAddr&n) {
|
|
if (item.isEnabled())
|
|
item << n.ToString();
|
|
return item.maybespace();
|
|
}
|
|
|
|
class CSubNet
|
|
{
|
|
protected:
|
|
/// Network (base) address
|
|
CNetAddr network;
|
|
/// Netmask, in network byte order
|
|
uint8_t netmask[16];
|
|
/// Is this value valid? (only used to signal parse errors)
|
|
bool valid;
|
|
|
|
public:
|
|
CSubNet();
|
|
explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
|
|
|
|
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
|
|
explicit CSubNet(const CNetAddr &addr);
|
|
|
|
bool Match(const CNetAddr &addr) const;
|
|
|
|
std::string ToString() const;
|
|
bool IsValid() const;
|
|
|
|
friend bool operator==(const CSubNet& a, const CSubNet& b);
|
|
friend bool operator!=(const CSubNet& a, const CSubNet& b);
|
|
friend bool operator<(const CSubNet& a, const CSubNet& b);
|
|
|
|
ADD_SERIALIZE_METHODS
|
|
|
|
template <typename Stream, typename Operation>
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
READWRITE(network);
|
|
READWRITE(FLATDATA(netmask));
|
|
READWRITE(FLATDATA(valid));
|
|
}
|
|
};
|
|
|
|
/** A combination of a network address (CNetAddr) and a (TCP) port */
|
|
class CService : public CNetAddr
|
|
{
|
|
protected:
|
|
unsigned short port; // host order
|
|
|
|
public:
|
|
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);
|
|
void Init();
|
|
void SetPort(unsigned short portIn);
|
|
unsigned short GetPort() const;
|
|
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);
|
|
std::vector<unsigned char> GetKey() const;
|
|
std::string ToString() const;
|
|
std::string ToStringPort() const;
|
|
std::string ToStringIPPort() const;
|
|
|
|
CService(const struct in6_addr& ipv6Addr, unsigned short port);
|
|
CService(const struct sockaddr_in6& addr);
|
|
|
|
ADD_SERIALIZE_METHODS
|
|
|
|
template <typename Stream, typename Operation>
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
READWRITE(FLATDATA(ip));
|
|
unsigned short portN = htons(port);
|
|
READWRITE(FLATDATA(portN));
|
|
if (ser_action.ForRead())
|
|
port = ntohs(portN);
|
|
}
|
|
};
|
|
|
|
inline Log::SilentItem operator<<(Log::SilentItem item, const CService&) { return item; }
|
|
inline Log::Item operator<<(Log::Item item, const CService &s) {
|
|
if (item.isEnabled())
|
|
item << s.ToString().c_str();
|
|
return item;
|
|
}
|
|
|
|
|
|
class proxyType
|
|
{
|
|
public:
|
|
proxyType(): randomize_credentials(false) {}
|
|
proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
|
|
|
|
bool IsValid() const { return proxy.IsValid(); }
|
|
|
|
CService proxy;
|
|
bool randomize_credentials;
|
|
};
|
|
|
|
CNetAddr::Network ParseNetwork(std::string net);
|
|
std::string GetNetworkName(CNetAddr::Network net);
|
|
bool SetProxy(CNetAddr::Network net, const proxyType &addrProxy);
|
|
bool GetProxy(CNetAddr::Network net, proxyType &proxyInfoOut);
|
|
bool IsProxy(const CNetAddr &addr);
|
|
bool SetNameProxy(const proxyType &addrProxy);
|
|
bool HaveNameProxy();
|
|
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
|
|
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, bool *outProxyConnectionFailed = 0);
|
|
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
|
|
/** Return readable error string for a network error code */
|
|
std::string NetworkErrorString(int err);
|
|
/** Close socket and set hSocket to INVALID_SOCKET */
|
|
bool CloseSocket(SOCKET& hSocket);
|
|
/** Disable or enable blocking-mode for a socket */
|
|
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
|
|
/**
|
|
* Convert milliseconds to a struct timeval for e.g. select.
|
|
*/
|
|
struct timeval MillisToTimeval(int64_t nTimeout);
|
|
|
|
#endif
|