1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "utilstrencodings.h"
12 # include <arpa/inet.h>
15 static const char* ppszTypeName
[] =
23 CMessageHeader::CMessageHeader(const MessageStartChars
& pchMessageStartIn
)
25 memcpy(pchMessageStart
, pchMessageStartIn
, MESSAGE_START_SIZE
);
26 memset(pchCommand
, 0, sizeof(pchCommand
));
31 CMessageHeader::CMessageHeader(const MessageStartChars
& pchMessageStartIn
, const char* pszCommand
, unsigned int nMessageSizeIn
)
33 memcpy(pchMessageStart
, pchMessageStartIn
, MESSAGE_START_SIZE
);
34 memset(pchCommand
, 0, sizeof(pchCommand
));
35 strncpy(pchCommand
, pszCommand
, COMMAND_SIZE
);
36 nMessageSize
= nMessageSizeIn
;
40 std::string
CMessageHeader::GetCommand() const
42 return std::string(pchCommand
, pchCommand
+ strnlen(pchCommand
, COMMAND_SIZE
));
45 bool CMessageHeader::IsValid(const MessageStartChars
& pchMessageStartIn
) const
48 if (memcmp(pchMessageStart
, pchMessageStartIn
, MESSAGE_START_SIZE
) != 0)
51 // Check the command string for errors
52 for (const char* p1
= pchCommand
; p1
< pchCommand
+ COMMAND_SIZE
; p1
++)
56 // Must be all zeros after the first zero
57 for (; p1
< pchCommand
+ COMMAND_SIZE
; p1
++)
61 else if (*p1
< ' ' || *p1
> 0x7E)
66 if (nMessageSize
> MAX_SIZE
)
68 LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize
);
77 CAddress::CAddress() : CService()
82 CAddress::CAddress(CService ipIn
, uint64_t nServicesIn
) : CService(ipIn
)
85 nServices
= nServicesIn
;
90 nServices
= NODE_NETWORK
;
100 CInv::CInv(int typeIn
, const uint256
& hashIn
)
106 CInv::CInv(const std::string
& strType
, const uint256
& hashIn
)
109 for (i
= 1; i
< ARRAYLEN(ppszTypeName
); i
++)
111 if (strType
== ppszTypeName
[i
])
117 if (i
== ARRAYLEN(ppszTypeName
))
118 throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType
));
122 bool operator<(const CInv
& a
, const CInv
& b
)
124 return (a
.type
< b
.type
|| (a
.type
== b
.type
&& a
.hash
< b
.hash
));
127 bool CInv::IsKnownType() const
129 return (type
>= 1 && type
< (int)ARRAYLEN(ppszTypeName
));
132 const char* CInv::GetCommand() const
135 throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type
));
136 return ppszTypeName
[type
];
139 std::string
CInv::ToString() const
141 return strprintf("%s %s", GetCommand(), hash
.ToString());