1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 # error This header can only be compiled as C++.
10 #ifndef __INCLUDED_PROTOCOL_H__
11 #define __INCLUDED_PROTOCOL_H__
14 #include <netinet/in.h>
17 #include "serialize.h"
22 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
24 return testnet ? 18333 : 8333;
34 extern unsigned char pchMessageStart[4];
40 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
42 std::string GetCommand() const;
47 READWRITE(FLATDATA(pchMessageStart));
48 READWRITE(FLATDATA(pchCommand));
49 READWRITE(nMessageSize);
54 // TODO: make private (improves encapsulation)
56 enum { COMMAND_SIZE=12 };
57 char pchMessageStart[sizeof(::pchMessageStart)];
58 char pchCommand[COMMAND_SIZE];
59 unsigned int nMessageSize;
60 unsigned int nChecksum;
65 NODE_NETWORK = (1 << 0),
72 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
73 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
74 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
75 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
76 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
77 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
84 const_cast<CAddress*>(this)->Init();
87 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
90 READWRITE(FLATDATA(pchReserved)); // for IPv6
95 friend bool operator==(const CAddress& a, const CAddress& b);
96 friend bool operator!=(const CAddress& a, const CAddress& b);
97 friend bool operator<(const CAddress& a, const CAddress& b);
99 std::vector<unsigned char> GetKey() const;
100 struct sockaddr_in GetSockAddr() const;
102 bool IsRFC1918() const;
103 bool IsRFC3927() const;
104 bool IsLocal() const;
105 bool IsRoutable() const;
106 bool IsValid() const;
107 unsigned char GetByte(int n) const;
108 std::string ToStringIPPort() const;
109 std::string ToStringIP() const;
110 std::string ToStringPort() const;
111 std::string ToString() const;
114 // TODO: make private (improves encapsulation)
117 unsigned char pchReserved[12];
121 // disk and network only
125 unsigned int nLastTry;
132 CInv(int typeIn, const uint256& hashIn);
133 CInv(const std::string& strType, const uint256& hashIn);
141 friend bool operator<(const CInv& a, const CInv& b);
143 bool IsKnownType() const;
144 const char* GetCommand() const;
145 std::string ToString() const;
148 // TODO: make private (improves encapsulation)
154 #endif // __INCLUDED_PROTOCOL_H__