protocol.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "protocol.h"
  3. #include "util.h"
  4. #include "netbase.h"
  5. #ifndef WIN32
  6. # include <arpa/inet.h>
  7. #endif
  8. static const char* ppszTypeName[] =
  9. {
  10. "ERROR",
  11. "tx",
  12. "block",
  13. };
  14. CMessageHeader::CMessageHeader()
  15. {
  16. memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
  17. memset(pchCommand, 0, sizeof(pchCommand));
  18. pchCommand[1] = 1;
  19. nMessageSize = -1;
  20. nChecksum = 0;
  21. }
  22. CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
  23. {
  24. memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
  25. strncpy(pchCommand, pszCommand, COMMAND_SIZE);
  26. nMessageSize = nMessageSizeIn;
  27. nChecksum = 0;
  28. }
  29. std::string CMessageHeader::GetCommand() const
  30. {
  31. if (pchCommand[COMMAND_SIZE-1] == 0)
  32. return std::string(pchCommand, pchCommand + strlen(pchCommand));
  33. else
  34. return std::string(pchCommand, pchCommand + COMMAND_SIZE);
  35. }
  36. bool CMessageHeader::IsValid() const
  37. {
  38. // Check start string
  39. if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
  40. return false;
  41. // Check the command string for errors
  42. for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
  43. {
  44. if (*p1 == 0)
  45. {
  46. // Must be all zeros after the first zero
  47. for (; p1 < pchCommand + COMMAND_SIZE; p1++)
  48. if (*p1 != 0)
  49. return false;
  50. }
  51. else if (*p1 < ' ' || *p1 > 0x7E)
  52. return false;
  53. }
  54. // Message size
  55. if (nMessageSize > MAX_SIZE)
  56. {
  57. printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
  58. return false;
  59. }
  60. return true;
  61. }
  62. CAddress::CAddress() : CService()
  63. {
  64. Init();
  65. }
  66. CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
  67. {
  68. Init();
  69. nServices = nServicesIn;
  70. }
  71. void CAddress::Init()
  72. {
  73. nServices = NODE_NETWORK;
  74. nTime = 100000000;
  75. nLastTry = 0;
  76. }
  77. CInv::CInv()
  78. {
  79. type = 0;
  80. hash = 0;
  81. }
  82. CInv::CInv(int typeIn, const uint256& hashIn)
  83. {
  84. type = typeIn;
  85. hash = hashIn;
  86. }
  87. CInv::CInv(const std::string& strType, const uint256& hashIn)
  88. {
  89. unsigned int i;
  90. for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
  91. {
  92. if (strType == ppszTypeName[i])
  93. {
  94. type = i;
  95. break;
  96. }
  97. }
  98. if (i == ARRAYLEN(ppszTypeName))
  99. throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
  100. hash = hashIn;
  101. }
  102. bool operator<(const CInv& a, const CInv& b)
  103. {
  104. return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
  105. }
  106. bool CInv::IsKnownType() const
  107. {
  108. return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
  109. }
  110. const char* CInv::GetCommand() const
  111. {
  112. if (!IsKnownType())
  113. throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
  114. return ppszTypeName[type];
  115. }
  116. std::string CInv::ToString() const
  117. {
  118. return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
  119. }
  120. void CInv::print() const
  121. {
  122. printf("CInv(%s)\n", ToString().c_str());
  123. }