Update to 0.3.0 (New upstream + new RPC calls)
[novacoin.git] / src / protocol.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2013 NovaCoin Developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7 #include "protocol.h"
8 #include "util.h"
9 #include "netbase.h"
10
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
14
15 // The message start string is designed to be unlikely to occur in normal data.
16 // The characters are rarely used upper ascii, not valid as UTF-8, and produce
17 // a large 4-byte int at any alignment.
18
19 static unsigned char pchMessageStartTestNew[4] = { 0xcd, 0xf2, 0xc0, 0xef };
20 static unsigned char pchMessageStartPPCoin[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
21
22 void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent)
23 {
24     if (fTestNet)
25         memcpy(pchMessageStart, pchMessageStartTestNew, sizeof(pchMessageStartTestNew));
26     else
27         memcpy(pchMessageStart, pchMessageStartPPCoin, sizeof(pchMessageStartPPCoin));
28 }
29
30 static const char* ppszTypeName[] =
31 {
32     "ERROR",
33     "tx",
34     "block",
35 };
36
37 CMessageHeader::CMessageHeader()
38 {
39     GetMessageStart(pchMessageStart);
40     memset(pchCommand, 0, sizeof(pchCommand));
41     pchCommand[1] = 1;
42     nMessageSize = -1;
43     nChecksum = 0;
44 }
45
46 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
47 {
48     GetMessageStart(pchMessageStart);
49     strncpy(pchCommand, pszCommand, COMMAND_SIZE);
50     nMessageSize = nMessageSizeIn;
51     nChecksum = 0;
52 }
53
54 std::string CMessageHeader::GetCommand() const
55 {
56     if (pchCommand[COMMAND_SIZE-1] == 0)
57         return std::string(pchCommand, pchCommand + strlen(pchCommand));
58     else
59         return std::string(pchCommand, pchCommand + COMMAND_SIZE);
60 }
61
62 bool CMessageHeader::IsValid() const
63 {
64     // Check start string
65     unsigned char pchMessageStartProtocol[4];
66     GetMessageStart(pchMessageStartProtocol);
67     if (memcmp(pchMessageStart, pchMessageStartProtocol, sizeof(pchMessageStart)) != 0)
68         return false;
69
70     // Check the command string for errors
71     for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
72     {
73         if (*p1 == 0)
74         {
75             // Must be all zeros after the first zero
76             for (; p1 < pchCommand + COMMAND_SIZE; p1++)
77                 if (*p1 != 0)
78                     return false;
79         }
80         else if (*p1 < ' ' || *p1 > 0x7E)
81             return false;
82     }
83
84     // Message size
85     if (nMessageSize > MAX_SIZE)
86     {
87         printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
88         return false;
89     }
90
91     return true;
92 }
93
94
95
96 CAddress::CAddress() : CService()
97 {
98     Init();
99 }
100
101 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
102 {
103     Init();
104     nServices = nServicesIn;
105 }
106
107 void CAddress::Init()
108 {
109     nServices = NODE_NETWORK;
110     nTime = 100000000;
111     nLastTry = 0;
112 }
113
114 CInv::CInv()
115 {
116     type = 0;
117     hash = 0;
118 }
119
120 CInv::CInv(int typeIn, const uint256& hashIn)
121 {
122     type = typeIn;
123     hash = hashIn;
124 }
125
126 CInv::CInv(const std::string& strType, const uint256& hashIn)
127 {
128     unsigned int i;
129     for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
130     {
131         if (strType == ppszTypeName[i])
132         {
133             type = i;
134             break;
135         }
136     }
137     if (i == ARRAYLEN(ppszTypeName))
138         throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
139     hash = hashIn;
140 }
141
142 bool operator<(const CInv& a, const CInv& b)
143 {
144     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
145 }
146
147 bool CInv::IsKnownType() const
148 {
149     return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
150 }
151
152 const char* CInv::GetCommand() const
153 {
154     if (!IsKnownType())
155         throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
156     return ppszTypeName[type];
157 }
158
159 std::string CInv::ToString() const
160 {
161     return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
162 }
163
164 void CInv::print() const
165 {
166     printf("CInv(%s)\n", ToString().c_str());
167 }
168