]> Git Repo - VerusCoin.git/blob - src/protocol.cpp
Copyright header updates s/2013/2014 on files whose last git commit was done in 2014.
[VerusCoin.git] / src / protocol.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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
8 #include "util.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     "filtered block"
20 };
21
22 CMessageHeader::CMessageHeader()
23 {
24     memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
25     memset(pchCommand, 0, sizeof(pchCommand));
26     pchCommand[1] = 1;
27     nMessageSize = -1;
28     nChecksum = 0;
29 }
30
31 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
32 {
33     memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
34     strncpy(pchCommand, pszCommand, COMMAND_SIZE);
35     nMessageSize = nMessageSizeIn;
36     nChecksum = 0;
37 }
38
39 std::string CMessageHeader::GetCommand() const
40 {
41     if (pchCommand[COMMAND_SIZE-1] == 0)
42         return std::string(pchCommand, pchCommand + strlen(pchCommand));
43     else
44         return std::string(pchCommand, pchCommand + COMMAND_SIZE);
45 }
46
47 bool CMessageHeader::IsValid() const
48 {
49     // Check start string
50     if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
51         return false;
52
53     // Check the command string for errors
54     for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
55     {
56         if (*p1 == 0)
57         {
58             // Must be all zeros after the first zero
59             for (; p1 < pchCommand + COMMAND_SIZE; p1++)
60                 if (*p1 != 0)
61                     return false;
62         }
63         else if (*p1 < ' ' || *p1 > 0x7E)
64             return false;
65     }
66
67     // Message size
68     if (nMessageSize > MAX_SIZE)
69     {
70         LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
71         return false;
72     }
73
74     return true;
75 }
76
77
78
79 CAddress::CAddress() : CService()
80 {
81     Init();
82 }
83
84 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
85 {
86     Init();
87     nServices = nServicesIn;
88 }
89
90 void CAddress::Init()
91 {
92     nServices = NODE_NETWORK;
93     nTime = 100000000;
94     nLastTry = 0;
95 }
96
97 CInv::CInv()
98 {
99     type = 0;
100     hash = 0;
101 }
102
103 CInv::CInv(int typeIn, const uint256& hashIn)
104 {
105     type = typeIn;
106     hash = hashIn;
107 }
108
109 CInv::CInv(const std::string& strType, const uint256& hashIn)
110 {
111     unsigned int i;
112     for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
113     {
114         if (strType == ppszTypeName[i])
115         {
116             type = i;
117             break;
118         }
119     }
120     if (i == ARRAYLEN(ppszTypeName))
121         throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType));
122     hash = hashIn;
123 }
124
125 bool operator<(const CInv& a, const CInv& b)
126 {
127     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
128 }
129
130 bool CInv::IsKnownType() const
131 {
132     return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
133 }
134
135 const char* CInv::GetCommand() const
136 {
137     if (!IsKnownType())
138         throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
139     return ppszTypeName[type];
140 }
141
142 std::string CInv::ToString() const
143 {
144     return strprintf("%s %s", GetCommand(), hash.ToString());
145 }
146
147 void CInv::print() const
148 {
149     LogPrintf("CInv(%s)\n", ToString());
150 }
151
This page took 0.030383 seconds and 4 git commands to generate.