-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathudp_discovery_protocol.cpp
218 lines (185 loc) · 6.28 KB
/
udp_discovery_protocol.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "udp_discovery_protocol.hpp"
namespace udpdiscovery {
namespace impl {
bool SerializeString(SerializeDirection direction, std::string* value,
int value_size, BufferView* buffer_view) {
switch (direction) {
case kSerialize:
buffer_view->InsertBack(*value, value_size);
break;
case kParse:
if (!buffer_view->CanRead(value_size)) {
return false;
}
value->resize(value_size);
for (int i = 0; i < (int)value_size; ++i) {
(*value)[i] = buffer_view->Read();
}
break;
}
return true;
}
ProtocolVersion GetProtocolVersion(uint8_t version) {
if (version == kProtocolVersion0) {
return kProtocolVersion0;
} else if (version == kProtocolVersion1) {
return kProtocolVersion1;
}
return kProtocolVersionUnknown;
}
PacketType GetPacketType(uint8_t packet_type) {
if (packet_type == kPacketIAmHere) {
return kPacketIAmHere;
} else if (packet_type == kPacketIAmOutOfHere) {
return kPacketIAmOutOfHere;
}
return kPacketTypeUnknown;
}
} // namespace impl
bool Packet::Serialize(ProtocolVersion protocol_version,
std::string& buffer_out) {
if (protocol_version == kProtocolVersion0) {
if (user_data_.size() > kMaxUserDataSizeV0) {
return false;
}
} else if (protocol_version == kProtocolVersion1) {
if (user_data_.size() > kMaxUserDataSizeV1) {
return false;
}
}
impl::BufferView buffer_view(&buffer_out);
return Serialize(protocol_version, impl::kSerialize, &buffer_view);
}
ProtocolVersion Packet::Parse(const std::string& buffer) {
impl::BufferView buffer_view(const_cast<std::string*>(&buffer));
uint8_t magic[4];
if (!impl::SerializeUnsignedIntegerBigEndian(impl::kParse, &magic[0],
&buffer_view)) {
return kProtocolVersionUnknown;
}
if (!impl::SerializeUnsignedIntegerBigEndian(impl::kParse, &magic[1],
&buffer_view)) {
return kProtocolVersionUnknown;
}
if (!impl::SerializeUnsignedIntegerBigEndian(impl::kParse, &magic[2],
&buffer_view)) {
return kProtocolVersionUnknown;
}
if (!impl::SerializeUnsignedIntegerBigEndian(impl::kParse, &magic[3],
&buffer_view)) {
return kProtocolVersionUnknown;
}
uint8_t version = kProtocolVersion0;
if (!impl::SerializeUnsignedIntegerBigEndian(impl::kParse, &version,
&buffer_view)) {
return kProtocolVersionUnknown;
}
ProtocolVersion protocol_version = kProtocolVersionUnknown;
if (magic[0] == 'R' && magic[1] == 'N' && magic[2] == '6' &&
magic[3] == 'U') {
protocol_version = kProtocolVersion0;
} else if (magic[0] == 'S' && magic[1] == 'O' && magic[2] == '7' &&
magic[3] == 'V') {
protocol_version = impl::GetProtocolVersion(version);
if (protocol_version == kProtocolVersionUnknown ||
protocol_version == kProtocolVersion0) {
return kProtocolVersionUnknown;
}
}
if (protocol_version == kProtocolVersionUnknown) {
return kProtocolVersionUnknown;
}
if (!Serialize(protocol_version, impl::kParse, &buffer_view)) {
return kProtocolVersionUnknown;
}
return protocol_version;
}
bool Packet::Serialize(ProtocolVersion protocol_version,
impl::SerializeDirection direction,
impl::BufferView* buffer_view) {
if (direction == impl::kSerialize) {
if (protocol_version == kProtocolVersion0) {
buffer_view->push_back('R');
buffer_view->push_back('N');
buffer_view->push_back('6');
buffer_view->push_back('U');
} else if (protocol_version == kProtocolVersion1) {
buffer_view->push_back('S');
buffer_view->push_back('O');
buffer_view->push_back('7');
buffer_view->push_back('V');
} else {
return false;
}
uint8_t version = protocol_version;
impl::SerializeUnsignedIntegerBigEndian(impl::kSerialize, &version,
buffer_view);
}
uint8_t reserved = 0;
for (int i = 0; i < 3; ++i) {
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &reserved,
buffer_view)) {
return false;
}
}
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &packet_type_,
buffer_view)) {
return false;
}
if (direction == impl::kParse) {
if (impl::GetPacketType(packet_type_) == kPacketTypeUnknown) {
return false;
}
}
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &application_id_,
buffer_view)) {
return false;
}
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &peer_id_,
buffer_view)) {
return false;
}
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &snapshot_index_,
buffer_view)) {
return false;
}
uint16_t user_data_size = (uint16_t)user_data_.size();
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &user_data_size,
buffer_view)) {
return false;
}
if (direction == impl::kParse) {
if (protocol_version == kProtocolVersion0) {
if (user_data_size > kMaxUserDataSizeV0) {
return false;
}
} else if (protocol_version == kProtocolVersion1) {
if (user_data_size > kMaxUserDataSizeV1) {
return false;
}
}
}
uint16_t padding_size = 0;
if (protocol_version == kProtocolVersion0) {
if (!impl::SerializeUnsignedIntegerBigEndian(direction, &padding_size,
buffer_view)) {
return false;
}
if (padding_size > kMaxPaddingSizeV0) {
return false;
}
}
// End of serializing header.
if (direction == impl::kParse) {
if (buffer_view->LeftUnparsed() != user_data_size + padding_size) {
return false;
}
}
if (!impl::SerializeString(direction, &user_data_, user_data_size,
buffer_view)) {
return false;
}
// Do not serialize padding even for protocol version 0.
return true;
}
} // namespace udpdiscovery