NetworkConfigRequestMetaData.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_NETWORKCONFIGREQUESTMETADATA_HPP
  19. #define ZT_NETWORKCONFIGREQUESTMETADATA_HPP
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "Constants.hpp"
  24. #include "NetworkConfig.hpp"
  25. #include "Buffer.hpp"
  26. #include "../version.h"
  27. namespace ZeroTier {
  28. /**
  29. * Network configuration request meta data
  30. */
  31. class NetworkConfigRequestMetaData
  32. {
  33. public:
  34. NetworkConfigRequestMetaData() :
  35. buildId(0),
  36. flags(0),
  37. vendor(ZT_VENDOR_ZEROTIER),
  38. platform(ZT_PLATFORM_UNSPECIFIED),
  39. architecture(ZT_ARCHITECTURE_UNSPECIFIED),
  40. majorVersion(ZEROTIER_ONE_VERSION_MAJOR),
  41. minorVersion(ZEROTIER_ONE_VERSION_MINOR),
  42. revision(ZEROTIER_ONE_VERSION_REVISION)
  43. {
  44. memset(auth,0,sizeof(auth));
  45. }
  46. NetworkConfigRequestMetaData(bool foo)
  47. {
  48. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  49. }
  50. template<unsigned int C>
  51. inline void serialize(Buffer<C> &b) const
  52. {
  53. // Unlike network config we always send the old fields. Newer network
  54. // controllers will detect the presence of the new serialized data by
  55. // detecting extra data after the terminating NULL. But always sending
  56. // these maintains backward compatibility with old controllers.
  57. b.appendCString("majv="ZEROTIER_ONE_VERSION_MAJOR_S"\nminv="ZEROTIER_ONE_VERSION_MINOR_S"\nrevv="ZEROTIER_ONE_VERSION_REVISION_S"\n");
  58. b.append((uint16_t)1); // version
  59. b.append((uint64_t)buildId);
  60. b.append((uint64_t)flags);
  61. b.append((uint16_t)vendor);
  62. b.append((uint16_t)platform);
  63. b.append((uint16_t)architecture);
  64. b.append((uint16_t)majorVersion);
  65. b.append((uint16_t)minorVersion);
  66. b.append((uint16_t)revision);
  67. unsigned int tl = (unsigned int)strlen(auth);
  68. if (tl > 255) tl = 255; // sanity check
  69. b.append((uint8_t)tl);
  70. b.append((const void *)auth,tl);
  71. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  72. }
  73. template<unsigned int C>
  74. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  75. {
  76. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  77. unsigned int p = startAt;
  78. // Seek past old style meta-data
  79. while (b[p]) ++p;
  80. if (b.template at<uint16_t>(p) != 1)
  81. throw std::invalid_argument("unrecognized version");
  82. p += 2;
  83. buildId = b.template at<uint64_t>(p); p += 8;
  84. flags = b.template at<uint64_t>(p); p += 8;
  85. vendor = (ZT_Vendor)b.template at<uint16_t>(p); p += 2;
  86. platform = (ZT_Platform)b.template at<uint16_t>(p); p += 2;
  87. architecture = (ZT_Architecture)b.template at<uint16_t>(p); p += 2;
  88. majorVersion = b.template at<uint16_t>(p); p += 2;
  89. minorVersion = b.template at<uint16_t>(p); p += 2;
  90. revision = b.template at<uint16_t>(p); p += 2;
  91. unsigned int tl = (unsigned int)b[p++];
  92. memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH));
  93. // auth[] is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1 and so will always end up null-terminated since we zeroed the structure
  94. p += tl;
  95. p += b.template at<uint16_t>(p) + 2;
  96. return (p - startAt);
  97. }
  98. inline void clear()
  99. {
  100. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  101. }
  102. /**
  103. * Build ID (currently unused, must be 0)
  104. */
  105. uint64_t buildId;
  106. /**
  107. * Flags (currently unused, must be 0)
  108. */
  109. uint64_t flags;
  110. /**
  111. * ZeroTier vendor or 0 for unspecified
  112. */
  113. ZT_Vendor vendor;
  114. /**
  115. * ZeroTier platform or 0 for unspecified
  116. */
  117. ZT_Platform platform;
  118. /**
  119. * ZeroTier architecture or 0 for unspecified
  120. */
  121. ZT_Architecture architecture;
  122. /**
  123. * ZeroTier software major version
  124. */
  125. unsigned int majorVersion;
  126. /**
  127. * ZeroTier software minor version
  128. */
  129. unsigned int minorVersion;
  130. /**
  131. * ZeroTier software revision
  132. */
  133. unsigned int revision;
  134. /**
  135. * Authentication data (e.g. bearer=<token>)
  136. */
  137. char auth[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  138. };
  139. } // namespace ZeroTier
  140. #endif