NetworkConfigRequestMetaData.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "Packet.hpp"
  27. #include "../version.h"
  28. /**
  29. * Maximum length of the auth field (including terminating NULL, since it's a C-style string)
  30. *
  31. * Actual max length not including NULL is this minus one.
  32. */
  33. #define ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH 2048
  34. namespace ZeroTier {
  35. /**
  36. * Network configuration request meta data
  37. */
  38. class NetworkConfigRequestMetaData
  39. {
  40. public:
  41. /**
  42. * Construct an empty meta-data object with zero/null values
  43. */
  44. NetworkConfigRequestMetaData()
  45. {
  46. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  47. }
  48. /**
  49. * Initialize with defaults from this node's config and version
  50. */
  51. inline void initWithDefaults()
  52. {
  53. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  54. vendor = ZT_VENDOR_ZEROTIER;
  55. platform = ZT_PLATFORM_UNSPECIFIED;
  56. architecture = ZT_ARCHITECTURE_UNSPECIFIED;
  57. majorVersion = ZEROTIER_ONE_VERSION_MAJOR;
  58. minorVersion = ZEROTIER_ONE_VERSION_MINOR;
  59. revision = ZEROTIER_ONE_VERSION_REVISION;
  60. protocolVersion = ZT_PROTO_VERSION;
  61. }
  62. /**
  63. * Zero/null everything
  64. */
  65. inline void clear()
  66. {
  67. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  68. }
  69. template<unsigned int C>
  70. inline void serialize(Buffer<C> &b) const
  71. {
  72. /* Unlike network config we always send the old fields. Newer network
  73. * controllers will detect the presence of the new serialized data by
  74. * detecting extra data after the terminating NULL. But always sending
  75. * these maintains backward compatibility with old controllers. This
  76. * appends a terminating NULL which seperates the old legacy meta-data
  77. * from the new packed binary format that we send after. */
  78. b.appendCString("majv=" ZEROTIER_ONE_VERSION_MAJOR_S_HEX "\nminv=" ZEROTIER_ONE_VERSION_MINOR_S_HEX "\nrevv=" ZEROTIER_ONE_VERSION_REVISION_S_HEX "\n");
  79. b.append((uint16_t)1); // serialization version
  80. b.append((uint64_t)buildId);
  81. b.append((uint64_t)flags);
  82. b.append((uint16_t)vendor);
  83. b.append((uint16_t)platform);
  84. b.append((uint16_t)architecture);
  85. b.append((uint16_t)majorVersion);
  86. b.append((uint16_t)minorVersion);
  87. b.append((uint16_t)revision);
  88. b.append((uint16_t)protocolVersion);
  89. const unsigned int tl = strlen(auth);
  90. b.append((uint16_t)tl);
  91. b.append((const void *)auth,tl);
  92. b.append((uint16_t)0); // extended bytes, currently 0 since unused
  93. }
  94. template<unsigned int C>
  95. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  96. {
  97. memset(this,0,sizeof(NetworkConfigRequestMetaData));
  98. unsigned int p = startAt;
  99. // Seek past old style meta-data
  100. while (b[p]) ++p;
  101. ++p;
  102. if (b.template at<uint16_t>(p) != 1)
  103. throw std::invalid_argument("unrecognized version");
  104. p += 2;
  105. buildId = b.template at<uint64_t>(p); p += 8;
  106. flags = b.template at<uint64_t>(p); p += 8;
  107. vendor = (ZT_Vendor)b.template at<uint16_t>(p); p += 2;
  108. platform = (ZT_Platform)b.template at<uint16_t>(p); p += 2;
  109. architecture = (ZT_Architecture)b.template at<uint16_t>(p); p += 2;
  110. majorVersion = b.template at<uint16_t>(p); p += 2;
  111. minorVersion = b.template at<uint16_t>(p); p += 2;
  112. revision = b.template at<uint16_t>(p); p += 2;
  113. protocolVersion = b.template at<uint16_t>(p); p += 2;
  114. const unsigned int tl = b.template at<uint16_t>(p); p += 2;
  115. memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)(ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH - 1)));
  116. p += tl;
  117. p += b.template at<uint16_t>(p) + 2;
  118. return (p - startAt);
  119. }
  120. /**
  121. * Authentication data (e.g. bearer=<token>) as a C-style string (always null terminated)
  122. */
  123. char auth[ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH];
  124. /**
  125. * Build ID (currently unused, must be 0)
  126. */
  127. uint64_t buildId;
  128. /**
  129. * Flags (currently unused, must be 0)
  130. */
  131. uint64_t flags;
  132. /**
  133. * ZeroTier vendor or 0 for unspecified
  134. */
  135. ZT_Vendor vendor;
  136. /**
  137. * ZeroTier platform or 0 for unspecified
  138. */
  139. ZT_Platform platform;
  140. /**
  141. * ZeroTier architecture or 0 for unspecified
  142. */
  143. ZT_Architecture architecture;
  144. /**
  145. * ZeroTier software major version
  146. */
  147. unsigned int majorVersion;
  148. /**
  149. * ZeroTier software minor version
  150. */
  151. unsigned int minorVersion;
  152. /**
  153. * ZeroTier software revision
  154. */
  155. unsigned int revision;
  156. /**
  157. * ZeroTier protocol version
  158. */
  159. unsigned int protocolVersion;
  160. };
  161. } // namespace ZeroTier
  162. #endif