aboutsummaryrefslogtreecommitdiff
path: root/src/netbase.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-01-03 23:33:31 +0100
committerPieter Wuille <pieter.wuille@gmail.com>2012-01-06 18:55:37 +0100
commit67a42f929b1434f647c63922fd02dc2b93b28060 (patch)
tree9c4313e815bd77e817f2dc5b796347d343458d0e /src/netbase.h
parent7486c64dd8436febbe59e82dbb875e83ad6b5194 (diff)
Network stack refactor
This introduces CNetAddr and CService, respectively wrapping an (IPv6) IP address and an IP+port combination. This functionality used to be part of CAddress, which also contains network flags and connection attempt information. These extra fields are however not always necessary. These classes, along with logic for creating connections and doing name lookups, are moved to netbase.{h,cpp}, which does not depend on headers.h. Furthermore, CNetAddr is mostly IPv6-ready, though IPv6 functionality is not yet enabled for the application itself.
Diffstat (limited to 'src/netbase.h')
-rw-r--r--src/netbase.h138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/netbase.h b/src/netbase.h
new file mode 100644
index 0000000000..6f06f8fe08
--- /dev/null
+++ b/src/netbase.h
@@ -0,0 +1,138 @@
+// Copyright (c) 2011 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file license.txt or http://www.opensource.org/licenses/mit-license.php.
+#ifndef BITCOIN_NETBASE_H
+#define BITCOIN_NETBASE_H
+
+#include <string>
+#include <vector>
+
+#ifdef WIN32
+#define _WIN32_WINNT 0x0501
+#include <winsock2.h>
+#include <mswsock.h>
+#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
+
+#include "serialize.h"
+#include "compat.h"
+
+extern int nConnectTimeout;
+
+
+// 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:
+ CNetAddr();
+ CNetAddr(const struct in_addr& ipv4Addr);
+ CNetAddr(const char *pszIp, bool fAllowLookup = false);
+ CNetAddr(const std::string &strIp, bool fAllowLookup = false);
+ void Init();
+ void SetIP(const CNetAddr& ip);
+ bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
+ 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)
+ bool IsRFC3964() const; // IPv6 6to4 tunneling (2002::/16)
+ bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
+ bool IsRFC4380() const; // IPv6 Teredo tunneling (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 IsLocal() const;
+ bool IsRoutable() const;
+ bool IsValid() const;
+ bool IsMulticast() const;
+ std::string ToString() const;
+ std::string ToStringIP() const;
+ int GetByte(int n) const;
+ int64 GetHash() const;
+ bool GetInAddr(struct in_addr* pipv4Addr) const;
+ std::vector<unsigned char> GetGroup() const;
+ void print() const;
+
+#ifdef USE_IPV6
+ CNetAddr(const struct in6_addr& pipv6Addr);
+ bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
+#endif
+
+ 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);
+
+ IMPLEMENT_SERIALIZE
+ (
+ READWRITE(FLATDATA(ip));
+ )
+};
+
+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);
+ CService(const char *pszIp, int port, bool fAllowLookup = false);
+ CService(const char *pszIpPort, bool fAllowLookup = false);
+ CService(const std::string& strIp, int port, bool fAllowLookup = false);
+ CService(const std::string& strIpPort, bool fAllowLookup = false);
+ void Init();
+ void SetPort(unsigned short portIn);
+ unsigned short GetPort() const;
+ bool GetSockAddr(struct sockaddr_in* paddr) const;
+ 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;
+ void print() const;
+
+#ifdef USE_IPV6
+ CService(const struct in6_addr& ipv6Addr, unsigned short port);
+ bool GetSockAddr6(struct sockaddr_in6* paddr) const;
+ CService(const struct sockaddr_in6& addr);
+#endif
+
+ IMPLEMENT_SERIALIZE
+ (
+ CService* pthis = const_cast<CService*>(this);
+ READWRITE(FLATDATA(ip));
+ unsigned short portN = htons(port);
+ READWRITE(portN);
+ if (fRead)
+ pthis->port = ntohs(portN);
+ )
+};
+
+bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions = 0, bool fAllowLookup = true);
+bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions = 0);
+bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
+bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
+bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
+
+// Settings
+extern int fUseProxy;
+extern CService addrProxy;
+
+#endif