-
+ EB06FFB5A1D9A725CCE5C48FABF4EA4E9147ACEB065ACC54AE21C6D4B4065EBF5715D2DC88D3934312517A15247FF689711EDE12328F5B8DEADB6E2B43253C35
bitcoin/src/protocol.h
(0 . 0)(1 . 150)
17484 // Copyright (c) 2009-2010 Satoshi Nakamoto
17485 // Copyright (c) 2011 The Bitcoin developers
17486 // Distributed under the MIT/X11 software license, see the accompanying
17487 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
17488
17489 #ifndef __cplusplus
17490 # error This header can only be compiled as C++.
17491 #endif
17492
17493 #ifndef __INCLUDED_PROTOCOL_H__
17494 #define __INCLUDED_PROTOCOL_H__
17495
17496 #include "serialize.h"
17497 #include <string>
17498 #include "uint256.h"
17499
17500 extern bool fTestNet;
17501 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
17502 {
17503 return testnet ? 18333 : 8333;
17504 }
17505
17506 //
17507 // Message header
17508 // (4) message start
17509 // (12) command
17510 // (4) size
17511 // (4) checksum
17512
17513 extern unsigned char pchMessageStart[4];
17514
17515 class CMessageHeader
17516 {
17517 public:
17518 CMessageHeader();
17519 CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
17520
17521 std::string GetCommand() const;
17522 bool IsValid() const;
17523
17524 IMPLEMENT_SERIALIZE
17525 (
17526 READWRITE(FLATDATA(pchMessageStart));
17527 READWRITE(FLATDATA(pchCommand));
17528 READWRITE(nMessageSize);
17529 if (nVersion >= 209)
17530 READWRITE(nChecksum);
17531 )
17532
17533 // TODO: make private (improves encapsulation)
17534 public:
17535 enum { COMMAND_SIZE=12 };
17536 char pchMessageStart[sizeof(::pchMessageStart)];
17537 char pchCommand[COMMAND_SIZE];
17538 unsigned int nMessageSize;
17539 unsigned int nChecksum;
17540 };
17541
17542 enum
17543 {
17544 NODE_NETWORK = (1 << 0),
17545 };
17546
17547 class CAddress
17548 {
17549 public:
17550 CAddress();
17551 CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
17552 explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
17553 explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
17554 explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
17555 explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
17556 explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
17557
17558 void Init();
17559
17560 IMPLEMENT_SERIALIZE
17561 (
17562 if (fRead)
17563 const_cast<CAddress*>(this)->Init();
17564 if (nType & SER_DISK)
17565 READWRITE(nVersion);
17566 if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
17567 READWRITE(nTime);
17568 READWRITE(nServices);
17569 READWRITE(FLATDATA(pchReserved)); // for IPv6
17570 READWRITE(ip);
17571 READWRITE(port);
17572 )
17573
17574 friend bool operator==(const CAddress& a, const CAddress& b);
17575 friend bool operator!=(const CAddress& a, const CAddress& b);
17576 friend bool operator<(const CAddress& a, const CAddress& b);
17577
17578 std::vector<unsigned char> GetKey() const;
17579 struct sockaddr_in GetSockAddr() const;
17580 bool IsIPv4() const;
17581 bool IsRFC1918() const;
17582 bool IsRFC3927() const;
17583 bool IsLocal() const;
17584 bool IsRoutable() const;
17585 bool IsValid() const;
17586 unsigned char GetByte(int n) const;
17587 std::string ToStringIPPort() const;
17588 std::string ToStringIP() const;
17589 std::string ToStringPort() const;
17590 std::string ToString() const;
17591 void print() const;
17592
17593 // TODO: make private (improves encapsulation)
17594 public:
17595 uint64 nServices;
17596 unsigned char pchReserved[12];
17597 unsigned int ip;
17598 unsigned short port;
17599
17600 // disk and network only
17601 unsigned int nTime;
17602
17603 // memory only
17604 unsigned int nLastTry;
17605 };
17606
17607 class CInv
17608 {
17609 public:
17610 CInv();
17611 CInv(int typeIn, const uint256& hashIn);
17612 CInv(const std::string& strType, const uint256& hashIn);
17613
17614 IMPLEMENT_SERIALIZE
17615 (
17616 READWRITE(type);
17617 READWRITE(hash);
17618 )
17619
17620 friend bool operator<(const CInv& a, const CInv& b);
17621
17622 bool IsKnownType() const;
17623 const char* GetCommand() const;
17624 std::string ToString() const;
17625 void print() const;
17626
17627 // TODO: make private (improves encapsulation)
17628 public:
17629 int type;
17630 uint256 hash;
17631 };
17632
17633 #endif // __INCLUDED_PROTOCOL_H__