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