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__
13 #include "serialize.h"
17 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
19 return testnet ? 18333 : 8333;
29 extern unsigned char pchMessageStart[4];
35 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
37 std::string GetCommand() const;
42 READWRITE(FLATDATA(pchMessageStart));
43 READWRITE(FLATDATA(pchCommand));
44 READWRITE(nMessageSize);
49 // TODO: make private (improves encapsulation)
51 enum { COMMAND_SIZE=12 };
52 char pchMessageStart[sizeof(::pchMessageStart)];
53 char pchCommand[COMMAND_SIZE];
54 unsigned int nMessageSize;
55 unsigned int nChecksum;
60 NODE_NETWORK = (1 << 0),
67 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
68 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
69 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
70 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
71 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
72 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
79 const_cast<CAddress*>(this)->Init();
82 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
85 READWRITE(FLATDATA(pchReserved)); // for IPv6
90 friend bool operator==(const CAddress& a, const CAddress& b);
91 friend bool operator!=(const CAddress& a, const CAddress& b);
92 friend bool operator<(const CAddress& a, const CAddress& b);
94 std::vector<unsigned char> GetKey() const;
95 struct sockaddr_in GetSockAddr() const;
97 bool IsRFC1918() const;
98 bool IsRFC3927() const;
100 bool IsRoutable() const;
101 bool IsValid() const;
102 unsigned char GetByte(int n) const;
103 std::string ToStringIPPort() const;
104 std::string ToStringIP() const;
105 std::string ToStringPort() const;
106 std::string ToString() const;
109 // TODO: make private (improves encapsulation)
112 unsigned char pchReserved[12];
116 // disk and network only
120 unsigned int nLastTry;
123 #endif // __INCLUDED_PROTOCOL_H__