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