Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "protocol.h"
7 
8 #include "util.h"
9 
10 #ifndef WIN32
11 # include <arpa/inet.h>
12 #endif
13 
14 static const char* ppszTypeName[] =
15 {
16  "ERROR",
17  "tx",
18  "block",
19  "filtered block"
20 };
21 
23 {
24  memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
25  memset(pchCommand, 0, sizeof(pchCommand));
26  pchCommand[1] = 1;
27  nMessageSize = -1;
28  nChecksum = 0;
29 }
30 
31 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
32 {
33  memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
34  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
35  nMessageSize = nMessageSizeIn;
36  nChecksum = 0;
37 }
38 
39 std::string CMessageHeader::GetCommand() const
40 {
41  if (pchCommand[COMMAND_SIZE-1] == 0)
42  return std::string(pchCommand, pchCommand + strlen(pchCommand));
43  else
44  return std::string(pchCommand, pchCommand + COMMAND_SIZE);
45 }
46 
48 {
49  // Check start string
50  if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
51  return false;
52 
53  // Check the command string for errors
54  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
55  {
56  if (*p1 == 0)
57  {
58  // Must be all zeros after the first zero
59  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
60  if (*p1 != 0)
61  return false;
62  }
63  else if (*p1 < ' ' || *p1 > 0x7E)
64  return false;
65  }
66 
67  // Message size
68  if (nMessageSize > MAX_SIZE)
69  {
70  LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
71  return false;
72  }
73 
74  return true;
75 }
76 
77 
78 
80 {
81  Init();
82 }
83 
84 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
85 {
86  Init();
87  nServices = nServicesIn;
88 }
89 
91 {
93  nTime = 100000000;
94  nLastTry = 0;
95 }
96 
98 {
99  type = 0;
100  hash = 0;
101 }
102 
103 CInv::CInv(int typeIn, const uint256& hashIn)
104 {
105  type = typeIn;
106  hash = hashIn;
107 }
108 
109 CInv::CInv(const std::string& strType, const uint256& hashIn)
110 {
111  unsigned int i;
112  for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
113  {
114  if (strType == ppszTypeName[i])
115  {
116  type = i;
117  break;
118  }
119  }
120  if (i == ARRAYLEN(ppszTypeName))
121  throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType));
122  hash = hashIn;
123 }
124 
125 bool operator<(const CInv& a, const CInv& b)
126 {
127  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
128 }
129 
130 bool CInv::IsKnownType() const
131 {
132  return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
133 }
134 
135 const char* CInv::GetCommand() const
136 {
137  if (!IsKnownType())
138  throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
139  return ppszTypeName[type];
140 }
141 
142 std::string CInv::ToString() const
143 {
144  return strprintf("%s %s", GetCommand(), hash.ToString());
145 }
146 
147 void CInv::print() const
148 {
149  LogPrintf("CInv(%s)\n", ToString());
150 }
151 
uint64_t nServices
Definition: protocol.h:95
static const char * ppszTypeName[]
Definition: protocol.cpp:14
const char * GetCommand() const
Definition: protocol.cpp:135
void print() const
Definition: protocol.cpp:147
void Init()
Definition: protocol.cpp:90
inv message data
Definition: protocol.h:105
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:56
bool IsKnownType() const
Definition: protocol.cpp:130
#define strprintf
Definition: util.h:116
unsigned int nChecksum
Definition: protocol.h:58
#define LogPrintf(...)
Definition: util.h:117
#define MESSAGE_START_SIZE
Definition: chainparams.h:16
int type
Definition: protocol.h:127
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:94
int64_t nLastTry
Definition: protocol.h:101
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:125
uint256 hash
Definition: protocol.h:128
std::string GetCommand() const
Definition: protocol.cpp:39
IMPLEMENT_SERIALIZE(READWRITE(FLATDATA(pchMessageStart));READWRITE(FLATDATA(pchCommand));READWRITE(nMessageSize);READWRITE(nChecksum);) public char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:37
256-bit unsigned integer
Definition: uint256.h:531
unsigned int nTime
Definition: protocol.h:98
static const unsigned int MAX_SIZE
Definition: serialize.h:30
CAddress()
Definition: protocol.cpp:79
const CChainParams & Params()
Return the currently selected parameters.
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:7
std::string ToString() const
Definition: uint256.h:340
#define ARRAYLEN(array)
Definition: util.h:45
unsigned int nMessageSize
Definition: protocol.h:57
CInv()
Definition: protocol.cpp:97
std::string ToString() const
Definition: protocol.cpp:142
bool IsValid() const
Definition: protocol.cpp:47