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