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