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
asciilifeform_let... 17 static inline unsigned short GetDefaultPort()
genesis 18 {
asciilifeform_let... 19 return 8333;
genesis 20 }
genesis 21
genesis 22 //
genesis 23 // Message header
genesis 24 // (4) message start
genesis 25 // (12) command
genesis 26 // (4) size
genesis 27 // (4) checksum
genesis 28
genesis 29 extern unsigned char pchMessageStart[4];
genesis 30
genesis 31 class CMessageHeader
genesis 32 {
genesis 33 public:
genesis 34 CMessageHeader();
genesis 35 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
genesis 36
genesis 37 std::string GetCommand() const;
genesis 38 bool IsValid() const;
genesis 39
genesis 40 IMPLEMENT_SERIALIZE
genesis 41 (
genesis 42 READWRITE(FLATDATA(pchMessageStart));
genesis 43 READWRITE(FLATDATA(pchCommand));
genesis 44 READWRITE(nMessageSize);
genesis 45 if (nVersion >= 209)
genesis 46 READWRITE(nChecksum);
genesis 47 )
genesis 48
genesis 49 // TODO: make private (improves encapsulation)
genesis 50 public:
genesis 51 enum { COMMAND_SIZE=12 };
genesis 52 char pchMessageStart[sizeof(::pchMessageStart)];
genesis 53 char pchCommand[COMMAND_SIZE];
genesis 54 unsigned int nMessageSize;
genesis 55 unsigned int nChecksum;
genesis 56 };
genesis 57
genesis 58 enum
genesis 59 {
genesis 60 NODE_NETWORK = (1 << 0),
genesis 61 };
genesis 62
genesis 63 class CAddress
genesis 64 {
genesis 65 public:
genesis 66 CAddress();
genesis 67 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
genesis 68 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
asciilifeform_dns... 69 explicit CAddress(const char* pszIn, int portIn, uint64 nServicesIn=NODE_NETWORK);
asciilifeform_dns... 70 explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK);
asciilifeform_dns... 71 explicit CAddress(std::string strIn, int portIn, uint64 nServicesIn=NODE_NETWORK);
asciilifeform_dns... 72 explicit CAddress(std::string strIn, uint64 nServicesIn=NODE_NETWORK);
genesis 73
genesis 74 void Init();
genesis 75
genesis 76 IMPLEMENT_SERIALIZE
genesis 77 (
genesis 78 if (fRead)
genesis 79 const_cast<CAddress*>(this)->Init();
genesis 80 if (nType & SER_DISK)
genesis 81 READWRITE(nVersion);
genesis 82 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
genesis 83 READWRITE(nTime);
genesis 84 READWRITE(nServices);
genesis 85 READWRITE(FLATDATA(pchReserved)); // for IPv6
genesis 86 READWRITE(ip);
genesis 87 READWRITE(port);
genesis 88 )
genesis 89
genesis 90 friend bool operator==(const CAddress& a, const CAddress& b);
genesis 91 friend bool operator!=(const CAddress& a, const CAddress& b);
genesis 92 friend bool operator<(const CAddress& a, const CAddress& b);
genesis 93
genesis 94 std::vector<unsigned char> GetKey() const;
genesis 95 struct sockaddr_in GetSockAddr() const;
genesis 96 bool IsIPv4() const;
genesis 97 bool IsRFC1918() const;
genesis 98 bool IsRFC3927() const;
genesis 99 bool IsLocal() const;
genesis 100 bool IsRoutable() const;
genesis 101 bool IsValid() const;
genesis 102 unsigned char GetByte(int n) const;
genesis 103 std::string ToStringIPPort() const;
genesis 104 std::string ToStringIP() const;
genesis 105 std::string ToStringPort() const;
genesis 106 std::string ToString() const;
genesis 107 void print() const;
genesis 108
genesis 109 // TODO: make private (improves encapsulation)
genesis 110 public:
genesis 111 uint64 nServices;
genesis 112 unsigned char pchReserved[12];
genesis 113 unsigned int ip;
genesis 114 unsigned short port;
genesis 115
genesis 116 // disk and network only
genesis 117 unsigned int nTime;
genesis 118
genesis 119 // memory only
genesis 120 unsigned int nLastTry;
genesis 121 };
genesis 122
genesis 123 class CInv
genesis 124 {
genesis 125 public:
genesis 126 CInv();
genesis 127 CInv(int typeIn, const uint256& hashIn);
genesis 128 CInv(const std::string& strType, const uint256& hashIn);
genesis 129
genesis 130 IMPLEMENT_SERIALIZE
genesis 131 (
genesis 132 READWRITE(type);
genesis 133 READWRITE(hash);
genesis 134 )
genesis 135
genesis 136 friend bool operator<(const CInv& a, const CInv& b);
genesis 137
genesis 138 bool IsKnownType() const;
genesis 139 const char* GetCommand() const;
genesis 140 std::string ToString() const;
genesis 141 void print() const;
genesis 142
genesis 143 // TODO: make private (improves encapsulation)
genesis 144 public:
genesis 145 int type;
genesis 146 uint256 hash;
genesis 147 };
genesis 148
genesis 149 #endif // __INCLUDED_PROTOCOL_H__