Back to home page

Bitcoin sources

 
 

    


0001 // Copyright (c) 2009-2010 Satoshi Nakamoto
0002 // Copyright (c) 2011 The Bitcoin developers
0003 // Distributed under the MIT/X11 software license, see the accompanying
0004 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
0005 
0006 #ifndef __cplusplus
0007 # error This header can only be compiled as C++.
0008 #endif
0009 
0010 #ifndef __INCLUDED_PROTOCOL_H__
0011 #define __INCLUDED_PROTOCOL_H__
0012 
0013 #include "serialize.h"
0014 #include <string>
0015 #include "uint256.h"
0016 
0017 extern bool fTestNet;
0018 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
0019 {
0020     return testnet ? 18333 : 8333;
0021 }
0022 
0023 //
0024 // Message header
0025 //  (4) message start
0026 //  (12) command
0027 //  (4) size
0028 //  (4) checksum
0029 
0030 extern unsigned char pchMessageStart[4];
0031 
0032 class CMessageHeader
0033 {
0034     public:
0035         CMessageHeader();
0036         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
0037 
0038         std::string GetCommand() const;
0039         bool IsValid() const;
0040 
0041         IMPLEMENT_SERIALIZE
0042             (
0043              READWRITE(FLATDATA(pchMessageStart));
0044              READWRITE(FLATDATA(pchCommand));
0045              READWRITE(nMessageSize);
0046              if (nVersion >= 209)
0047              READWRITE(nChecksum);
0048             )
0049 
0050     // TODO: make private (improves encapsulation)
0051     public:
0052         enum { COMMAND_SIZE=12 };
0053         char pchMessageStart[sizeof(::pchMessageStart)];
0054         char pchCommand[COMMAND_SIZE];
0055         unsigned int nMessageSize;
0056         unsigned int nChecksum;
0057 };
0058 
0059 enum
0060 {
0061     NODE_NETWORK = (1 << 0),
0062 };
0063 
0064 class CAddress
0065 {
0066     public:
0067         CAddress();
0068         CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
0069         explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
0070         explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
0071         explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
0072         explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
0073         explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
0074 
0075         void Init();
0076 
0077         IMPLEMENT_SERIALIZE
0078             (
0079              if (fRead)
0080              const_cast<CAddress*>(this)->Init();
0081              if (nType & SER_DISK)
0082              READWRITE(nVersion);
0083              if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
0084              READWRITE(nTime);
0085              READWRITE(nServices);
0086              READWRITE(FLATDATA(pchReserved)); // for IPv6
0087              READWRITE(ip);
0088              READWRITE(port);
0089             )
0090 
0091         friend bool operator==(const CAddress& a, const CAddress& b);
0092         friend bool operator!=(const CAddress& a, const CAddress& b);
0093         friend bool operator<(const CAddress& a, const CAddress& b);
0094 
0095         std::vector<unsigned char> GetKey() const;
0096         struct sockaddr_in GetSockAddr() const;
0097         bool IsIPv4() const;
0098         bool IsRFC1918() const;
0099         bool IsRFC3927() const;
0100         bool IsLocal() const;
0101         bool IsRoutable() const;
0102         bool IsValid() const;
0103         unsigned char GetByte(int n) const;
0104         std::string ToStringIPPort() const;
0105         std::string ToStringIP() const;
0106         std::string ToStringPort() const;
0107         std::string ToString() const;
0108         void print() const;
0109 
0110     // TODO: make private (improves encapsulation)
0111     public:
0112         uint64 nServices;
0113         unsigned char pchReserved[12];
0114         unsigned int ip;
0115         unsigned short port;
0116 
0117         // disk and network only
0118         unsigned int nTime;
0119 
0120         // memory only
0121         unsigned int nLastTry;
0122 };
0123 
0124 class CInv
0125 {
0126     public:
0127         CInv();
0128         CInv(int typeIn, const uint256& hashIn);
0129         CInv(const std::string& strType, const uint256& hashIn);
0130 
0131         IMPLEMENT_SERIALIZE
0132         (
0133             READWRITE(type);
0134             READWRITE(hash);
0135         )
0136 
0137         friend bool operator<(const CInv& a, const CInv& b);
0138 
0139         bool IsKnownType() const;
0140         const char* GetCommand() const;
0141         std::string ToString() const;
0142         void print() const;
0143 
0144     // TODO: make private (improves encapsulation)
0145     public:
0146         int type;
0147         uint256 hash;
0148 };
0149 
0150 #endif // __INCLUDED_PROTOCOL_H__