]>
Commit | Line | Data |
---|---|---|
507fd9d1 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT 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" |
85c579e3 | 9 | #include "utilstrencodings.h" |
51ed9ec9 | 10 | |
6853e627 | 11 | #ifndef WIN32 |
33e28c99 GS |
12 | # include <arpa/inet.h> |
13 | #endif | |
14 | ||
e4dde849 GS |
15 | static const char* ppszTypeName[] = |
16 | { | |
17 | "ERROR", | |
18 | "tx", | |
19 | "block", | |
b02ddbed | 20 | "filtered block" |
e4dde849 | 21 | }; |
507fd9d1 | 22 | |
eec37136 | 23 | CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn) |
507fd9d1 | 24 | { |
eec37136 | 25 | memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE); |
507fd9d1 | 26 | memset(pchCommand, 0, sizeof(pchCommand)); |
507fd9d1 GS |
27 | nMessageSize = -1; |
28 | nChecksum = 0; | |
29 | } | |
30 | ||
eec37136 | 31 | CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn) |
507fd9d1 | 32 | { |
eec37136 | 33 | memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE); |
62e5f8f9 | 34 | memset(pchCommand, 0, sizeof(pchCommand)); |
507fd9d1 GS |
35 | strncpy(pchCommand, pszCommand, COMMAND_SIZE); |
36 | nMessageSize = nMessageSizeIn; | |
37 | nChecksum = 0; | |
38 | } | |
39 | ||
40 | std::string CMessageHeader::GetCommand() const | |
41 | { | |
62e5f8f9 | 42 | return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE)); |
507fd9d1 GS |
43 | } |
44 | ||
eec37136 | 45 | bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const |
507fd9d1 GS |
46 | { |
47 | // Check start string | |
eec37136 | 48 | if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0) |
507fd9d1 GS |
49 | return false; |
50 | ||
51 | // Check the command string for errors | |
52 | for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) | |
53 | { | |
54 | if (*p1 == 0) | |
55 | { | |
56 | // Must be all zeros after the first zero | |
57 | for (; p1 < pchCommand + COMMAND_SIZE; p1++) | |
58 | if (*p1 != 0) | |
59 | return false; | |
60 | } | |
61 | else if (*p1 < ' ' || *p1 > 0x7E) | |
62 | return false; | |
63 | } | |
64 | ||
65 | // Message size | |
66 | if (nMessageSize > MAX_SIZE) | |
67 | { | |
5262fde0 | 68 | LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize); |
507fd9d1 GS |
69 | return false; |
70 | } | |
71 | ||
72 | return true; | |
73 | } | |
33e28c99 | 74 | |
33e28c99 | 75 | |
33e28c99 | 76 | |
67a42f92 | 77 | CAddress::CAddress() : CService() |
33e28c99 GS |
78 | { |
79 | Init(); | |
33e28c99 GS |
80 | } |
81 | ||
51ed9ec9 | 82 | CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn) |
33e28c99 GS |
83 | { |
84 | Init(); | |
67a42f92 | 85 | nServices = nServicesIn; |
33e28c99 GS |
86 | } |
87 | ||
88 | void CAddress::Init() | |
89 | { | |
90 | nServices = NODE_NETWORK; | |
33e28c99 | 91 | nTime = 100000000; |
33e28c99 GS |
92 | } |
93 | ||
e4dde849 GS |
94 | CInv::CInv() |
95 | { | |
96 | type = 0; | |
4f152496 | 97 | hash.SetNull(); |
e4dde849 GS |
98 | } |
99 | ||
100 | CInv::CInv(int typeIn, const uint256& hashIn) | |
101 | { | |
102 | type = typeIn; | |
103 | hash = hashIn; | |
104 | } | |
105 | ||
106 | CInv::CInv(const std::string& strType, const uint256& hashIn) | |
107 | { | |
c376ac35 | 108 | unsigned int i; |
e4dde849 GS |
109 | for (i = 1; i < ARRAYLEN(ppszTypeName); i++) |
110 | { | |
111 | if (strType == ppszTypeName[i]) | |
112 | { | |
113 | type = i; | |
114 | break; | |
115 | } | |
116 | } | |
117 | if (i == ARRAYLEN(ppszTypeName)) | |
5262fde0 | 118 | throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType)); |
e4dde849 GS |
119 | hash = hashIn; |
120 | } | |
121 | ||
122 | bool operator<(const CInv& a, const CInv& b) | |
123 | { | |
124 | return (a.type < b.type || (a.type == b.type && a.hash < b.hash)); | |
125 | } | |
126 | ||
127 | bool CInv::IsKnownType() const | |
128 | { | |
1d8c7a95 | 129 | return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName)); |
e4dde849 GS |
130 | } |
131 | ||
132 | const char* CInv::GetCommand() const | |
133 | { | |
134 | if (!IsKnownType()) | |
5262fde0 | 135 | throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type)); |
e4dde849 GS |
136 | return ppszTypeName[type]; |
137 | } | |
138 | ||
139 | std::string CInv::ToString() const | |
140 | { | |
7d9d134b | 141 | return strprintf("%s %s", GetCommand(), hash.ToString()); |
e4dde849 | 142 | } |