NetworkConfigRequestMetaData.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  28. #include <string>
  29. #include "Dictionary.hpp"
  30. #endif
  31. namespace ZeroTier {
  32. /**
  33. * Network configuration request meta data
  34. */
  35. class NetworkConfigRequestMetaData
  36. {
  37. public:
  38. NetworkConfigRequestMetaData()
  39. {
  40. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  41. }
  42. template<unsigned int C>
  43. inline void serialize(Buffer<C> &b) const
  44. {
  45. // Unlike network config we always send the old fields. Newer network
  46. // controllers will detect the presence of the new serialized data by
  47. // detecting extra data after the terminating NULL. But always sending
  48. // these maintains backward compatibility with old controllers.
  49. b.appendCString("majv="ZEROTIER_ONE_VERSION_MAJOR_S"\nminv="ZEROTIER_ONE_VERSION_MINOR_S"\nrevv="ZEROTIER_ONE_VERSION_REVISION_S"\n");
  50. b.append((uint16_t)1); // version
  51. b.append((uint64_t)buildId);
  52. b.append((uint64_t)flags);
  53. b.append((uint16_t)vendor);
  54. b.append((uint16_t)platform);
  55. b.append((uint16_t)architecture);
  56. b.append((uint16_t)majorVersion);
  57. b.append((uint16_t)minorVersion);
  58. b.append((uint16_t)revision);
  59. unsigned int tl = (unsigned int)strlen(_auth);
  60. if (tl > 255) tl = 255; // sanity check
  61. b.append((uint8_t)tl);
  62. b.append((const void *)auth,tl);
  63. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  64. }
  65. template<unsigned int C>
  66. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  67. {
  68. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  69. unsigned int p = startAt;
  70. // Seek past old style meta-data
  71. while (b[p]) ++p;
  72. if (b.template at<uint16_t>(p) != 1)
  73. throw std::invalid_argument("unrecognized version");
  74. p += 2;
  75. buildId = b.template at<uint64_t>(p); p += 8;
  76. flags = b.template at<uint64_t>(p); p += 8;
  77. vendor = (ZT_Vendor)b.template at<uint16_t>(p); p += 2;
  78. platform = (ZT_Platform)b.template at<uint16_t>(p); p += 2;
  79. architecture = (ZT_Architecture)b.template at<uint16_t>(p); p += 2;
  80. majorVersion = b.template at<uint16_t>(p); p += 2;
  81. minorVersion = b.template at<uint16_t>(p); p += 2;
  82. revision = b.template at<uint16_t>(p); p += 2;
  83. unsigned int tl = (unsigned int)b[p++];
  84. memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH));
  85. // auth[] is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1 and so will always end up null-terminated since we zeroed the structure
  86. p += tl;
  87. p += b.template at<uint16_t>(p) + 2;
  88. return (p - startAt);
  89. }
  90. /**
  91. * Build ID (currently unused, must be 0)
  92. */
  93. uint64_t buildId;
  94. /**
  95. * Flags (currently unused, must be 0)
  96. */
  97. uint64_t flags;
  98. /**
  99. * ZeroTier vendor or 0 for unspecified
  100. */
  101. ZT_Vendor vendor;
  102. /**
  103. * ZeroTier platform or 0 for unspecified
  104. */
  105. ZT_Platform platform;
  106. /**
  107. * ZeroTier architecture or 0 for unspecified
  108. */
  109. ZT_Architecture architecture;
  110. /**
  111. * ZeroTier software major version
  112. */
  113. unsigned int majorVersion;
  114. /**
  115. * ZeroTier software minor version
  116. */
  117. unsigned int minorVersion;
  118. /**
  119. * ZeroTier software revision
  120. */
  121. unsigned int revision;
  122. /**
  123. * Authentication data (e.g. bearer=<token>)
  124. */
  125. char auth[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
  126. };
  127. } // namespace ZeroTier
  128. #endif