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.
8 CMessageHeader::CMessageHeader()
10 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
11 memset(pchCommand, 0, sizeof(pchCommand));
17 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
19 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
20 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
21 nMessageSize = nMessageSizeIn;
25 std::string CMessageHeader::GetCommand() const
27 if (pchCommand[COMMAND_SIZE-1] == 0)
28 return std::string(pchCommand, pchCommand + strlen(pchCommand));
30 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
33 bool CMessageHeader::IsValid() const
36 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
39 // Check the command string for errors
40 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
44 // Must be all zeros after the first zero
45 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
49 else if (*p1 < ' ' || *p1 > 0x7E)
54 if (nMessageSize > MAX_SIZE)
56 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);