Move CAddress to protocol.[ch]pp
authorGiel van Schijndel <me@mortis.eu>
Thu, 11 Aug 2011 16:40:12 +0000 (18:40 +0200)
committerGiel van Schijndel <me@mortis.eu>
Fri, 19 Aug 2011 05:24:38 +0000 (07:24 +0200)
This commit does *not* and should not modify *any* code, it only moves
it from net.h and splits it across protocol.cpp and protocol.hpp.

Signed-off-by: Giel van Schijndel <me@mortis.eu>

src/net.h
src/protocol.cpp
src/protocol.h

index 7a4706d..66f3949 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -16,7 +16,6 @@
 
 #include "protocol.h"
 
-class CAddress;
 class CAddrDB;
 class CInv;
 class CRequestTracker;
@@ -29,15 +28,7 @@ extern int nConnectTimeout;
 
 inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); }
 inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); }
-inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; }
 static const unsigned int PUBLISH_HOPS = 5;
-enum
-{
-    NODE_NETWORK = (1 << 0),
-};
-
-
-
 
 bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout=nConnectTimeout);
 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
@@ -55,222 +46,6 @@ bool BindListenPort(std::string& strError=REF(std::string()));
 void StartNode(void* parg);
 bool StopNode();
 
-static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
-
-class CAddress
-{
-public:
-    uint64 nServices;
-    unsigned char pchReserved[12];
-    unsigned int ip;
-    unsigned short port;
-
-    // disk and network only
-    unsigned int nTime;
-
-    // memory only
-    unsigned int nLastTry;
-
-    CAddress()
-    {
-        Init();
-    }
-
-    CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        ip = ipIn;
-        port = htons(portIn == 0 ? GetDefaultPort() : portIn);
-        nServices = nServicesIn;
-    }
-
-    explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        ip = sockaddr.sin_addr.s_addr;
-        port = sockaddr.sin_port;
-        nServices = nServicesIn;
-    }
-
-    explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
-    }
-
-    explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
-    }
-
-    explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
-    }
-
-    explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK)
-    {
-        Init();
-        Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
-    }
-
-    void Init()
-    {
-        nServices = NODE_NETWORK;
-        memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
-        ip = INADDR_NONE;
-        port = htons(GetDefaultPort());
-        nTime = 100000000;
-        nLastTry = 0;
-    }
-
-    IMPLEMENT_SERIALIZE
-    (
-        if (fRead)
-            const_cast<CAddress*>(this)->Init();
-        if (nType & SER_DISK)
-            READWRITE(nVersion);
-        if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
-            READWRITE(nTime);
-        READWRITE(nServices);
-        READWRITE(FLATDATA(pchReserved)); // for IPv6
-        READWRITE(ip);
-        READWRITE(port);
-    )
-
-    friend inline bool operator==(const CAddress& a, const CAddress& b)
-    {
-        return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
-                a.ip   == b.ip &&
-                a.port == b.port);
-    }
-
-    friend inline bool operator!=(const CAddress& a, const CAddress& b)
-    {
-        return (!(a == b));
-    }
-
-    friend inline bool operator<(const CAddress& a, const CAddress& b)
-    {
-        int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
-        if (ret < 0)
-            return true;
-        else if (ret == 0)
-        {
-            if (ntohl(a.ip) < ntohl(b.ip))
-                return true;
-            else if (a.ip == b.ip)
-                return ntohs(a.port) < ntohs(b.port);
-        }
-        return false;
-    }
-
-    std::vector<unsigned char> GetKey() const
-    {
-        CDataStream ss;
-        ss.reserve(18);
-        ss << FLATDATA(pchReserved) << ip << port;
-
-        #if defined(_MSC_VER) && _MSC_VER < 1300
-        return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
-        #else
-        return std::vector<unsigned char>(ss.begin(), ss.end());
-        #endif
-    }
-
-    struct sockaddr_in GetSockAddr() const
-    {
-        struct sockaddr_in sockaddr;
-        memset(&sockaddr, 0, sizeof(sockaddr));
-        sockaddr.sin_family = AF_INET;
-        sockaddr.sin_addr.s_addr = ip;
-        sockaddr.sin_port = port;
-        return sockaddr;
-    }
-
-    bool IsIPv4() const
-    {
-        return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
-    }
-
-    bool IsRFC1918() const
-    {
-      return IsIPv4() && (GetByte(3) == 10 ||
-        (GetByte(3) == 192 && GetByte(2) == 168) ||
-        (GetByte(3) == 172 &&
-          (GetByte(2) >= 16 && GetByte(2) <= 31)));
-    }
-
-    bool IsRFC3927() const
-    {
-      return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
-    }
-
-    bool IsLocal() const
-    {
-      return IsIPv4() && (GetByte(3) == 127 ||
-          GetByte(3) == 0);
-    }
-
-    bool IsRoutable() const
-    {
-        return IsValid() &&
-            !(IsRFC1918() || IsRFC3927() || IsLocal());
-    }
-
-    bool IsValid() const
-    {
-        // Clean up 3-byte shifted addresses caused by garbage in size field
-        // of addr messages from versions before 0.2.9 checksum.
-        // Two consecutive addr messages look like this:
-        // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
-        // so if the first length field is garbled, it reads the second batch
-        // of addr misaligned by 3 bytes.
-        if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
-            return false;
-
-        return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
-    }
-
-    unsigned char GetByte(int n) const
-    {
-        return ((unsigned char*)&ip)[3-n];
-    }
-
-    std::string ToStringIPPort() const
-    {
-        return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
-    }
-
-    std::string ToStringIP() const
-    {
-        return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
-    }
-
-    std::string ToStringPort() const
-    {
-        return strprintf("%u", ntohs(port));
-    }
-
-    std::string ToString() const
-    {
-        return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
-    }
-
-    void print() const
-    {
-        printf("CAddress(%s)\n", ToString().c_str());
-    }
-};
-
-
-
-
-
-
-
 enum
 {
     MSG_TX = 1,
index 0bb1da9..8d2dbfd 100644 (file)
@@ -4,6 +4,17 @@
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
 #include "protocol.h"
+#include "util.h"
+
+#ifndef __WXMSW__
+# include <arpa/inet.h>
+#endif
+
+// Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things
+bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
+bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
+
+static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
 
 CMessageHeader::CMessageHeader()
 {
@@ -59,3 +70,182 @@ bool CMessageHeader::IsValid() const
 
     return true;
 }
+
+CAddress::CAddress()
+{
+    Init();
+}
+
+CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
+{
+    Init();
+    ip = ipIn;
+    port = htons(portIn == 0 ? GetDefaultPort() : portIn);
+    nServices = nServicesIn;
+}
+
+CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
+{
+    Init();
+    ip = sockaddr.sin_addr.s_addr;
+    port = sockaddr.sin_port;
+    nServices = nServicesIn;
+}
+
+CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
+{
+    Init();
+    Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
+}
+
+CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
+{
+    Init();
+    Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
+}
+
+CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
+{
+    Init();
+    Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
+}
+
+CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
+{
+    Init();
+    Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
+}
+
+void CAddress::Init()
+{
+    nServices = NODE_NETWORK;
+    memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
+    ip = INADDR_NONE;
+    port = htons(GetDefaultPort());
+    nTime = 100000000;
+    nLastTry = 0;
+}
+
+bool operator==(const CAddress& a, const CAddress& b)
+{
+    return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
+            a.ip   == b.ip &&
+            a.port == b.port);
+}
+
+bool operator!=(const CAddress& a, const CAddress& b)
+{
+    return (!(a == b));
+}
+
+bool operator<(const CAddress& a, const CAddress& b)
+{
+    int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
+    if (ret < 0)
+        return true;
+    else if (ret == 0)
+    {
+        if (ntohl(a.ip) < ntohl(b.ip))
+            return true;
+        else if (a.ip == b.ip)
+            return ntohs(a.port) < ntohs(b.port);
+    }
+    return false;
+}
+
+std::vector<unsigned char> CAddress::GetKey() const
+{
+    CDataStream ss;
+    ss.reserve(18);
+    ss << FLATDATA(pchReserved) << ip << port;
+
+    #if defined(_MSC_VER) && _MSC_VER < 1300
+    return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
+    #else
+    return std::vector<unsigned char>(ss.begin(), ss.end());
+    #endif
+}
+
+struct sockaddr_in CAddress::GetSockAddr() const
+{
+    struct sockaddr_in sockaddr;
+    memset(&sockaddr, 0, sizeof(sockaddr));
+    sockaddr.sin_family = AF_INET;
+    sockaddr.sin_addr.s_addr = ip;
+    sockaddr.sin_port = port;
+    return sockaddr;
+}
+
+bool CAddress::IsIPv4() const
+{
+    return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
+}
+
+bool CAddress::IsRFC1918() const
+{
+  return IsIPv4() && (GetByte(3) == 10 ||
+    (GetByte(3) == 192 && GetByte(2) == 168) ||
+    (GetByte(3) == 172 &&
+      (GetByte(2) >= 16 && GetByte(2) <= 31)));
+}
+
+bool CAddress::IsRFC3927() const
+{
+  return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
+}
+
+bool CAddress::IsLocal() const
+{
+  return IsIPv4() && (GetByte(3) == 127 ||
+      GetByte(3) == 0);
+}
+
+bool CAddress::IsRoutable() const
+{
+    return IsValid() &&
+        !(IsRFC1918() || IsRFC3927() || IsLocal());
+}
+
+bool CAddress::IsValid() const
+{
+    // Clean up 3-byte shifted addresses caused by garbage in size field
+    // of addr messages from versions before 0.2.9 checksum.
+    // Two consecutive addr messages look like this:
+    // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
+    // so if the first length field is garbled, it reads the second batch
+    // of addr misaligned by 3 bytes.
+    if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
+        return false;
+
+    return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
+}
+
+unsigned char CAddress::GetByte(int n) const
+{
+    return ((unsigned char*)&ip)[3-n];
+}
+
+std::string CAddress::ToStringIPPort() const
+{
+    return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
+}
+
+std::string CAddress::ToStringIP() const
+{
+    return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
+}
+
+std::string CAddress::ToStringPort() const
+{
+    return strprintf("%u", ntohs(port));
+}
+
+std::string CAddress::ToString() const
+{
+    return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
+}
+
+void CAddress::print() const
+{
+    printf("CAddress(%s)\n", ToString().c_str());
+}
index b5baeb2..009f67d 100644 (file)
 #include "serialize.h"
 #include <string>
 
+extern bool fTestNet;
+static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
+{
+    return testnet ? 18333 : 8333;
+}
+
 //
 // Message header
 //  (4) message start
@@ -49,4 +55,69 @@ class CMessageHeader
         unsigned int nChecksum;
 };
 
+enum
+{
+    NODE_NETWORK = (1 << 0),
+};
+
+class CAddress
+{
+    public:
+        CAddress();
+        CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
+        explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
+        explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
+        explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
+        explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
+        explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
+
+        void Init();
+
+        IMPLEMENT_SERIALIZE
+            (
+             if (fRead)
+             const_cast<CAddress*>(this)->Init();
+             if (nType & SER_DISK)
+             READWRITE(nVersion);
+             if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
+             READWRITE(nTime);
+             READWRITE(nServices);
+             READWRITE(FLATDATA(pchReserved)); // for IPv6
+             READWRITE(ip);
+             READWRITE(port);
+            )
+
+        friend bool operator==(const CAddress& a, const CAddress& b);
+        friend bool operator!=(const CAddress& a, const CAddress& b);
+        friend bool operator<(const CAddress& a, const CAddress& b);
+
+        std::vector<unsigned char> GetKey() const;
+        struct sockaddr_in GetSockAddr() const;
+        bool IsIPv4() const;
+        bool IsRFC1918() const;
+        bool IsRFC3927() const;
+        bool IsLocal() const;
+        bool IsRoutable() const;
+        bool IsValid() const;
+        unsigned char GetByte(int n) const;
+        std::string ToStringIPPort() const;
+        std::string ToStringIP() const;
+        std::string ToStringPort() const;
+        std::string ToString() const;
+        void print() const;
+
+    // TODO: make private (improves encapsulation)
+    public:
+        uint64 nServices;
+        unsigned char pchReserved[12];
+        unsigned int ip;
+        unsigned short port;
+
+        // disk and network only
+        unsigned int nTime;
+
+        // memory only
+        unsigned int nLastTry;
+};
+
 #endif // __INCLUDED_PROTOCOL_H__