]> Git Repo - VerusCoin.git/blame - src/protocol.cpp
Typesafe strprintf/error/LogPrint functions
[VerusCoin.git] / src / protocol.cpp
CommitLineData
507fd9d1 1// Copyright (c) 2009-2010 Satoshi Nakamoto
db0e8ccd 2// Copyright (c) 2009-2013 The Bitcoin developers
507fd9d1 3// Distributed under the MIT/X11 software license, see the accompanying
3a25a2b9 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
507fd9d1
GS
5
6#include "protocol.h"
51ed9ec9 7
33e28c99 8#include "util.h"
51ed9ec9 9
6853e627 10#ifndef WIN32
33e28c99
GS
11# include <arpa/inet.h>
12#endif
13
e4dde849
GS
14static const char* ppszTypeName[] =
15{
16 "ERROR",
17 "tx",
18 "block",
b02ddbed 19 "filtered block"
e4dde849 20};
507fd9d1
GS
21
22CMessageHeader::CMessageHeader()
23{
0e4b3175 24 memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
507fd9d1
GS
25 memset(pchCommand, 0, sizeof(pchCommand));
26 pchCommand[1] = 1;
27 nMessageSize = -1;
28 nChecksum = 0;
29}
30
31CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
32{
0e4b3175 33 memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
507fd9d1
GS
34 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
35 nMessageSize = nMessageSizeIn;
36 nChecksum = 0;
37}
38
39std::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
47bool CMessageHeader::IsValid() const
48{
49 // Check start string
0e4b3175 50 if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
507fd9d1
GS
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 {
881a85a2 70 LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
507fd9d1
GS
71 return false;
72 }
73
74 return true;
75}
33e28c99 76
33e28c99 77
33e28c99 78
67a42f92 79CAddress::CAddress() : CService()
33e28c99
GS
80{
81 Init();
33e28c99
GS
82}
83
51ed9ec9 84CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
33e28c99
GS
85{
86 Init();
67a42f92 87 nServices = nServicesIn;
33e28c99
GS
88}
89
90void CAddress::Init()
91{
92 nServices = NODE_NETWORK;
33e28c99
GS
93 nTime = 100000000;
94 nLastTry = 0;
95}
96
e4dde849
GS
97CInv::CInv()
98{
99 type = 0;
100 hash = 0;
101}
102
103CInv::CInv(int typeIn, const uint256& hashIn)
104{
105 type = typeIn;
106 hash = hashIn;
107}
108
109CInv::CInv(const std::string& strType, const uint256& hashIn)
110{
c376ac35 111 unsigned int i;
e4dde849
GS
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.c_str()));
122 hash = hashIn;
123}
124
125bool operator<(const CInv& a, const CInv& b)
126{
127 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
128}
129
130bool CInv::IsKnownType() const
131{
1d8c7a95 132 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
e4dde849
GS
133}
134
135const 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
142std::string CInv::ToString() const
143{
1c06aa98 144 return strprintf("%s %s", GetCommand(), hash.ToString().c_str());
e4dde849
GS
145}
146
147void CInv::print() const
148{
881a85a2 149 LogPrintf("CInv(%s)\n", ToString().c_str());
e4dde849 150}
67a42f92 151
This page took 0.122549 seconds and 4 git commands to generate.