Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 The Bitcoin Core developers
3 : // Distributed under the MIT 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 : #include "utilstrencodings.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 :
23 21206 : CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
24 : {
25 21206 : memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
26 21206 : memset(pchCommand, 0, sizeof(pchCommand));
27 21206 : nMessageSize = -1;
28 21206 : nChecksum = 0;
29 21206 : }
30 :
31 20974 : CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
32 : {
33 20974 : memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
34 20974 : memset(pchCommand, 0, sizeof(pchCommand));
35 20974 : strncpy(pchCommand, pszCommand, COMMAND_SIZE);
36 20974 : nMessageSize = nMessageSizeIn;
37 20974 : nChecksum = 0;
38 20974 : }
39 :
40 21203 : std::string CMessageHeader::GetCommand() const
41 : {
42 42406 : return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
43 : }
44 :
45 21203 : bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
46 : {
47 : // Check start string
48 21203 : if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
49 : return false;
50 :
51 : // Check the command string for errors
52 180376 : for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
53 : {
54 159173 : if (*p1 == 0)
55 : {
56 : // Must be all zeros after the first zero
57 116466 : for (; p1 < pchCommand + COMMAND_SIZE; p1++)
58 116466 : if (*p1 != 0)
59 : return false;
60 : }
61 137970 : else if (*p1 < ' ' || *p1 > 0x7E)
62 : return false;
63 : }
64 :
65 : // Message size
66 21203 : if (nMessageSize > MAX_SIZE)
67 : {
68 0 : LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
69 0 : return false;
70 : }
71 :
72 : return true;
73 : }
74 :
75 :
76 :
77 6416 : CAddress::CAddress() : CService()
78 : {
79 : Init();
80 6416 : }
81 :
82 961 : CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
83 : {
84 : Init();
85 961 : nServices = nServicesIn;
86 961 : }
87 :
88 500 : void CAddress::Init()
89 : {
90 7877 : nServices = NODE_NETWORK;
91 7877 : nTime = 100000000;
92 500 : }
93 :
94 18656 : CInv::CInv()
95 : {
96 18656 : type = 0;
97 18656 : hash.SetNull();
98 18656 : }
99 :
100 20859 : CInv::CInv(int typeIn, const uint256& hashIn)
101 : {
102 20859 : type = typeIn;
103 20859 : hash = hashIn;
104 20859 : }
105 :
106 0 : CInv::CInv(const std::string& strType, const uint256& hashIn)
107 : {
108 : unsigned int i;
109 0 : for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
110 : {
111 0 : if (strType == ppszTypeName[i])
112 : {
113 0 : type = i;
114 0 : break;
115 : }
116 : }
117 0 : if (i == ARRAYLEN(ppszTypeName))
118 0 : throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType));
119 0 : hash = hashIn;
120 0 : }
121 :
122 336218 : bool operator<(const CInv& a, const CInv& b)
123 : {
124 655395 : return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
125 : }
126 :
127 363 : bool CInv::IsKnownType() const
128 : {
129 12581 : return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
130 : }
131 :
132 12218 : const char* CInv::GetCommand() const
133 : {
134 12218 : if (!IsKnownType())
135 0 : throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
136 12218 : return ppszTypeName[type];
137 : }
138 :
139 11855 : std::string CInv::ToString() const
140 : {
141 23710 : return strprintf("%s %s", GetCommand(), hash.ToString());
142 288 : }
|