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