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