Get rid of fTrickle parameter.
[novacoin.git] / src / protocol.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "protocol.h"
7 #include "util.h"
8 #include "netbase.h"
9
10 #ifndef WIN32
11 # include <arpa/inet.h>
12 #endif
13
14 static const std::string forfill[] = { "ERROR", "tx", "block" }; //TODO: Replace with initializer list constructor when c++11 comes
15 static const std::vector<std::string> vpszTypeName(forfill, forfill + 3);
16
17 CMessageHeader::CMessageHeader() : nMessageSize(std::numeric_limits<uint32_t>::max()), nChecksum(0)
18 {
19     memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
20     memset(pchCommand, 0, sizeof(pchCommand));
21     pchCommand[1] = 1;
22 }
23
24 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nMessageSize(nMessageSizeIn), nChecksum(0)
25 {
26     memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
27     strncpy(pchCommand, pszCommand, COMMAND_SIZE);
28 }
29
30 std::string CMessageHeader::GetCommand() const
31 {
32     if (pchCommand[COMMAND_SIZE-1] == 0)
33         return std::string(pchCommand, pchCommand + strlen(pchCommand));
34     else
35         return std::string(pchCommand, pchCommand + COMMAND_SIZE);
36 }
37
38 bool CMessageHeader::IsValid() const
39 {
40     // Check start string
41     if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
42         return false;
43
44     // Check the command string for errors
45     for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
46     {
47         if (*p1 == 0)
48         {
49             // Must be all zeros after the first zero
50             for (; p1 < pchCommand + COMMAND_SIZE; p1++)
51                 if (*p1 != 0)
52                     return false;
53         }
54         else if (*p1 < ' ' || *p1 > 0x7E)
55             return false;
56     }
57
58     // Message size
59     if (nMessageSize > MAX_SIZE)
60     {
61         printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
62         return false;
63     }
64
65     return true;
66 }
67
68 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
69 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
70 CInv::CInv() : type(0), hash(0) { }
71 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
72 CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn)
73 {
74     unsigned int i;
75     for (i = 1; i < vpszTypeName.size(); ++i)
76     {
77         if (strType.compare(vpszTypeName[i]) == 0) {
78             type = i;
79             break;
80         }
81     }
82     if (i == vpszTypeName.size())
83         throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
84 }
85
86 bool operator<(const CInv& a, const CInv& b)
87 {
88     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
89 }
90
91 bool CInv::IsKnownType() const
92 {
93     return (type >= 1 && type < (int)vpszTypeName.size());
94 }
95
96 const char* CInv::GetCommand() const
97 {
98     if (!IsKnownType())
99         throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
100     return vpszTypeName[type].c_str();
101 }
102
103 std::string CInv::ToString() const
104 {
105     return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
106 }
107
108 void CInv::print() const
109 {
110     printf("CInv(%s)\n", ToString().c_str());
111 }
112