1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZT_NETWORKCONFIGREQUESTMETADATA_HPP
#define ZT_NETWORKCONFIGREQUESTMETADATA_HPP
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "Constants.hpp"
#include "NetworkConfig.hpp"
#include "Buffer.hpp"
#include "Packet.hpp"
#include "../version.h"
/**
* Maximum length of the auth field (including terminating NULL, since it's a C-style string)
*
* Actual max length not including NULL is this minus one.
*/
#define ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH 2048
namespace ZeroTier {
/**
* Network configuration request meta data
*/
class NetworkConfigRequestMetaData
{
public:
/**
* Construct an empty meta-data object with zero/null values
*/
NetworkConfigRequestMetaData()
{
memset(this,0,sizeof(NetworkConfigRequestMetaData));
}
/**
* Initialize with defaults from this node's config and version
*/
inline void initWithDefaults()
{
memset(this,0,sizeof(NetworkConfigRequestMetaData));
vendor = ZT_VENDOR_ZEROTIER;
platform = ZT_PLATFORM_UNSPECIFIED;
architecture = ZT_ARCHITECTURE_UNSPECIFIED;
majorVersion = ZEROTIER_ONE_VERSION_MAJOR;
minorVersion = ZEROTIER_ONE_VERSION_MINOR;
revision = ZEROTIER_ONE_VERSION_REVISION;
protocolVersion = ZT_PROTO_VERSION;
}
/**
* Zero/null everything
*/
inline void clear()
{
memset(this,0,sizeof(NetworkConfigRequestMetaData));
}
template<unsigned int C>
inline void serialize(Buffer<C> &b) const
{
/* Unlike network config we always send the old fields. Newer network
* controllers will detect the presence of the new serialized data by
* detecting extra data after the terminating NULL. But always sending
* these maintains backward compatibility with old controllers. This
* appends a terminating NULL which seperates the old legacy meta-data
* from the new packed binary format that we send after. */
b.appendCString("majv=" ZEROTIER_ONE_VERSION_MAJOR_S_HEX "\nminv=" ZEROTIER_ONE_VERSION_MINOR_S_HEX "\nrevv=" ZEROTIER_ONE_VERSION_REVISION_S_HEX "\n");
b.append((uint16_t)1); // serialization version
b.append((uint64_t)buildId);
b.append((uint64_t)flags);
b.append((uint16_t)vendor);
b.append((uint16_t)platform);
b.append((uint16_t)architecture);
b.append((uint16_t)majorVersion);
b.append((uint16_t)minorVersion);
b.append((uint16_t)revision);
b.append((uint16_t)protocolVersion);
const unsigned int tl = strlen(auth);
b.append((uint16_t)tl);
b.append((const void *)auth,tl);
b.append((uint16_t)0); // extended bytes, currently 0 since unused
}
template<unsigned int C>
inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
{
memset(this,0,sizeof(NetworkConfigRequestMetaData));
unsigned int p = startAt;
// Seek past old style meta-data
while (b[p]) ++p;
++p;
if (b.template at<uint16_t>(p) != 1)
throw std::invalid_argument("unrecognized version");
p += 2;
buildId = b.template at<uint64_t>(p); p += 8;
flags = b.template at<uint64_t>(p); p += 8;
vendor = (ZT_Vendor)b.template at<uint16_t>(p); p += 2;
platform = (ZT_Platform)b.template at<uint16_t>(p); p += 2;
architecture = (ZT_Architecture)b.template at<uint16_t>(p); p += 2;
majorVersion = b.template at<uint16_t>(p); p += 2;
minorVersion = b.template at<uint16_t>(p); p += 2;
revision = b.template at<uint16_t>(p); p += 2;
protocolVersion = b.template at<uint16_t>(p); p += 2;
const unsigned int tl = b.template at<uint16_t>(p); p += 2;
memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)(ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH - 1)));
p += tl;
p += b.template at<uint16_t>(p) + 2;
return (p - startAt);
}
/**
* Authentication data (e.g. bearer=<token>) as a C-style string (always null terminated)
*/
char auth[ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH];
/**
* Build ID (currently unused, must be 0)
*/
uint64_t buildId;
/**
* Flags (currently unused, must be 0)
*/
uint64_t flags;
/**
* ZeroTier vendor or 0 for unspecified
*/
ZT_Vendor vendor;
/**
* ZeroTier platform or 0 for unspecified
*/
ZT_Platform platform;
/**
* ZeroTier architecture or 0 for unspecified
*/
ZT_Architecture architecture;
/**
* ZeroTier software major version
*/
unsigned int majorVersion;
/**
* ZeroTier software minor version
*/
unsigned int minorVersion;
/**
* ZeroTier software revision
*/
unsigned int revision;
/**
* ZeroTier protocol version
*/
unsigned int protocolVersion;
};
} // namespace ZeroTier
#endif
|