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 static inline unsigned short GetDefaultPort()
0018 {
0019     return 8333;
0020 }
0021 
0022 //
0023 // Message header
0024 //  (4) message start
0025 //  (12) command
0026 //  (4) size
0027 //  (4) checksum
0028 
0029 extern unsigned char pchMessageStart[4];
0030 
0031 class CMessageHeader
0032 {
0033     public:
0034         CMessageHeader();
0035         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
0036 
0037         std::string GetCommand() const;
0038         bool IsValid() const;
0039 
0040         IMPLEMENT_SERIALIZE
0041             (
0042              READWRITE(FLATDATA(pchMessageStart));
0043              READWRITE(FLATDATA(pchCommand));
0044              READWRITE(nMessageSize);
0045              if (nVersion >= 209)
0046              READWRITE(nChecksum);
0047             )
0048 
0049     // TODO: make private (improves encapsulation)
0050     public:
0051         enum { COMMAND_SIZE=12 };
0052         char pchMessageStart[sizeof(::pchMessageStart)];
0053         char pchCommand[COMMAND_SIZE];
0054         unsigned int nMessageSize;
0055         unsigned int nChecksum;
0056 };
0057 
0058 enum
0059 {
0060     NODE_NETWORK = (1 << 0),
0061 };
0062 
0063 class CAddress
0064 {
0065     public:
0066         CAddress();
0067         CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
0068         explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
0069         explicit CAddress(const char* pszIn, int portIn, uint64 nServicesIn=NODE_NETWORK);
0070         explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK);
0071         explicit CAddress(std::string strIn, int portIn, uint64 nServicesIn=NODE_NETWORK);
0072         explicit CAddress(std::string strIn, uint64 nServicesIn=NODE_NETWORK);
0073 
0074         void Init();
0075 
0076         IMPLEMENT_SERIALIZE
0077             (
0078              if (fRead)
0079              const_cast<CAddress*>(this)->Init();
0080              if (nType & SER_DISK)
0081              READWRITE(nVersion);
0082              if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
0083              READWRITE(nTime);
0084              READWRITE(nServices);
0085              READWRITE(FLATDATA(pchReserved)); // for IPv6
0086              READWRITE(ip);
0087              READWRITE(port);
0088             )
0089 
0090         friend bool operator==(const CAddress& a, const CAddress& b);
0091         friend bool operator!=(const CAddress& a, const CAddress& b);
0092         friend bool operator<(const CAddress& a, const CAddress& b);
0093 
0094         std::vector<unsigned char> GetKey() const;
0095         struct sockaddr_in GetSockAddr() const;
0096         bool IsIPv4() const;
0097         bool IsRFC1918() const;
0098         bool IsRFC3927() const;
0099         bool IsLocal() const;
0100         bool IsRoutable() const;
0101         bool IsValid() const;
0102         unsigned char GetByte(int n) const;
0103         std::string ToStringIPPort() const;
0104         std::string ToStringIP() const;
0105         std::string ToStringPort() const;
0106         std::string ToString() const;
0107         void print() const;
0108 
0109     // TODO: make private (improves encapsulation)
0110     public:
0111         uint64 nServices;
0112         unsigned char pchReserved[12];
0113         unsigned int ip;
0114         unsigned short port;
0115 
0116         // disk and network only
0117         unsigned int nTime;
0118 
0119         // memory only
0120         unsigned int nLastTry;
0121 };
0122 
0123 class CInv
0124 {
0125     public:
0126         CInv();
0127         CInv(int typeIn, const uint256& hashIn);
0128         CInv(const std::string& strType, const uint256& hashIn);
0129 
0130         IMPLEMENT_SERIALIZE
0131         (
0132             READWRITE(type);
0133             READWRITE(hash);
0134         )
0135 
0136         friend bool operator<(const CInv& a, const CInv& b);
0137 
0138         bool IsKnownType() const;
0139         const char* GetCommand() const;
0140         std::string ToString() const;
0141         void print() const;
0142 
0143     // TODO: make private (improves encapsulation)
0144     public:
0145         int type;
0146         uint256 hash;
0147 };
0148 
0149 #endif // __INCLUDED_PROTOCOL_H__