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