raw
genesis                 1 // Copyright (c) 2009-2010 Satoshi Nakamoto
genesis 2 // Copyright (c) 2011 The Bitcoin developers
genesis 3 // Distributed under the MIT/X11 software license, see the accompanying
genesis 4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
genesis 5
genesis 6 #ifndef __cplusplus
genesis 7 # error This header can only be compiled as C++.
genesis 8 #endif
genesis 9
genesis 10 #ifndef __INCLUDED_PROTOCOL_H__
genesis 11 #define __INCLUDED_PROTOCOL_H__
genesis 12
genesis 13 #include "serialize.h"
genesis 14 #include <string>
genesis 15 #include "uint256.h"
genesis 16
genesis 17 extern bool fTestNet;
genesis 18 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
genesis 19 {
genesis 20 return testnet ? 18333 : 8333;
genesis 21 }
genesis 22
genesis 23 //
genesis 24 // Message header
genesis 25 // (4) message start
genesis 26 // (12) command
genesis 27 // (4) size
genesis 28 // (4) checksum
genesis 29
genesis 30 extern unsigned char pchMessageStart[4];
genesis 31
genesis 32 class CMessageHeader
genesis 33 {
genesis 34 public:
genesis 35 CMessageHeader();
genesis 36 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
genesis 37
genesis 38 std::string GetCommand() const;
genesis 39 bool IsValid() const;
genesis 40
genesis 41 IMPLEMENT_SERIALIZE
genesis 42 (
genesis 43 READWRITE(FLATDATA(pchMessageStart));
genesis 44 READWRITE(FLATDATA(pchCommand));
genesis 45 READWRITE(nMessageSize);
genesis 46 if (nVersion >= 209)
genesis 47 READWRITE(nChecksum);
genesis 48 )
genesis 49
genesis 50 // TODO: make private (improves encapsulation)
genesis 51 public:
genesis 52 enum { COMMAND_SIZE=12 };
genesis 53 char pchMessageStart[sizeof(::pchMessageStart)];
genesis 54 char pchCommand[COMMAND_SIZE];
genesis 55 unsigned int nMessageSize;
genesis 56 unsigned int nChecksum;
genesis 57 };
genesis 58
genesis 59 enum
genesis 60 {
genesis 61 NODE_NETWORK = (1 << 0),
genesis 62 };
genesis 63
genesis 64 class CAddress
genesis 65 {
genesis 66 public:
genesis 67 CAddress();
genesis 68 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
genesis 69 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
genesis 70 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
genesis 71 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
genesis 72 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
genesis 73 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
genesis 74
genesis 75 void Init();
genesis 76
genesis 77 IMPLEMENT_SERIALIZE
genesis 78 (
genesis 79 if (fRead)
genesis 80 const_cast<CAddress*>(this)->Init();
genesis 81 if (nType & SER_DISK)
genesis 82 READWRITE(nVersion);
genesis 83 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
genesis 84 READWRITE(nTime);
genesis 85 READWRITE(nServices);
genesis 86 READWRITE(FLATDATA(pchReserved)); // for IPv6
genesis 87 READWRITE(ip);
genesis 88 READWRITE(port);
genesis 89 )
genesis 90
genesis 91 friend bool operator==(const CAddress& a, const CAddress& b);
genesis 92 friend bool operator!=(const CAddress& a, const CAddress& b);
genesis 93 friend bool operator<(const CAddress& a, const CAddress& b);
genesis 94
genesis 95 std::vector<unsigned char> GetKey() const;
genesis 96 struct sockaddr_in GetSockAddr() const;
genesis 97 bool IsIPv4() const;
genesis 98 bool IsRFC1918() const;
genesis 99 bool IsRFC3927() const;
genesis 100 bool IsLocal() const;
genesis 101 bool IsRoutable() const;
genesis 102 bool IsValid() const;
genesis 103 unsigned char GetByte(int n) const;
genesis 104 std::string ToStringIPPort() const;
genesis 105 std::string ToStringIP() const;
genesis 106 std::string ToStringPort() const;
genesis 107 std::string ToString() const;
genesis 108 void print() const;
genesis 109
genesis 110 // TODO: make private (improves encapsulation)
genesis 111 public:
genesis 112 uint64 nServices;
genesis 113 unsigned char pchReserved[12];
genesis 114 unsigned int ip;
genesis 115 unsigned short port;
genesis 116
genesis 117 // disk and network only
genesis 118 unsigned int nTime;
genesis 119
genesis 120 // memory only
genesis 121 unsigned int nLastTry;
genesis 122 };
genesis 123
genesis 124 class CInv
genesis 125 {
genesis 126 public:
genesis 127 CInv();
genesis 128 CInv(int typeIn, const uint256& hashIn);
genesis 129 CInv(const std::string& strType, const uint256& hashIn);
genesis 130
genesis 131 IMPLEMENT_SERIALIZE
genesis 132 (
genesis 133 READWRITE(type);
genesis 134 READWRITE(hash);
genesis 135 )
genesis 136
genesis 137 friend bool operator<(const CInv& a, const CInv& b);
genesis 138
genesis 139 bool IsKnownType() const;
genesis 140 const char* GetCommand() const;
genesis 141 std::string ToString() const;
genesis 142 void print() const;
genesis 143
genesis 144 // TODO: make private (improves encapsulation)
genesis 145 public:
genesis 146 int type;
genesis 147 uint256 hash;
genesis 148 };
genesis 149
genesis 150 #endif // __INCLUDED_PROTOCOL_H__