d4b2808efa5bad4ef7e01c38eef2d4b91631ef2d
[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 // Network ID, previously known as pchMessageStart
11 uint32_t nNetworkID = 0xe5e9e8e4;
12
13 static const std::vector<const char*> vpszTypeName = { "ERROR", "tx", "block" };
14
15 CMessageHeader::CMessageHeader() : nMessageSize(std::numeric_limits<uint32_t>::max()), nChecksum(0)
16 {
17     nNetworkID = ::nNetworkID;
18     memset(pchCommand, 0, sizeof(pchCommand));
19     pchCommand[1] = 1;
20 }
21
22 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nMessageSize(nMessageSizeIn), nChecksum(0)
23 {
24     nNetworkID = ::nNetworkID;
25     strncpy(pchCommand, pszCommand, COMMAND_SIZE);
26 }
27
28 std::string CMessageHeader::GetCommand() const
29 {
30     if (pchCommand[COMMAND_SIZE-1] == 0)
31         return std::string(pchCommand, pchCommand + strlen(pchCommand));
32     else
33         return std::string(pchCommand, pchCommand + COMMAND_SIZE);
34 }
35
36 bool CMessageHeader::IsValid() const
37 {
38     // Check start string
39     if (nNetworkID != ::nNetworkID)
40         return false;
41
42     // Check the command string for errors
43     for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
44     {
45         if (*p1 == 0)
46         {
47             // Must be all zeros after the first zero
48             for (; p1 < pchCommand + COMMAND_SIZE; p1++)
49                 if (*p1 != 0)
50                     return false;
51         }
52         else if (*p1 < ' ' || *p1 > 0x7E)
53             return false;
54     }
55
56     // Message size
57     if (nMessageSize > MAX_SIZE)
58     {
59         printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
60         return false;
61     }
62
63     return true;
64 }
65
66 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
67 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
68 CInv::CInv() : type(0), hash(0) { }
69 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
70 CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn)
71 {
72     unsigned int i;
73     for (i = 1; i < vpszTypeName.size(); ++i)
74     {
75         if (strType.compare(vpszTypeName[i]) == 0) {
76             type = i;
77             break;
78         }
79     }
80     if (i == vpszTypeName.size())
81         throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
82 }
83
84 bool operator<(const CInv& a, const CInv& b)
85 {
86     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
87 }
88
89 bool CInv::IsKnownType() const
90 {
91     return (type >= 1 && type < (int)vpszTypeName.size());
92 }
93
94 const char* CInv::GetCommand() const
95 {
96     if (!IsKnownType())
97         throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
98     return vpszTypeName[type];
99 }
100
101 std::string CInv::ToString() const
102 {
103     return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
104 }