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