forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt5properties.cpp
272 lines (211 loc) · 7.54 KB
/
mqtt5properties.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#include "mqtt5properties.h"
#include <cstring>
#include <vector>
#include <cassert>
#include "exceptions.h"
Mqtt5PropertyBuilder::Mqtt5PropertyBuilder()
{
genericBytes.reserve(128);
clientSpecificBytes.reserve(128);
}
size_t Mqtt5PropertyBuilder::getLength()
{
length = genericBytes.size() + clientSpecificBytes.size();
return length.getLen() + genericBytes.size() + clientSpecificBytes.size();
}
const VariableByteInt &Mqtt5PropertyBuilder::getVarInt()
{
length = genericBytes.size() + clientSpecificBytes.size();
return length;
}
const std::vector<char> &Mqtt5PropertyBuilder::getGenericBytes() const
{
return genericBytes;
}
const std::vector<char> &Mqtt5PropertyBuilder::getclientSpecificBytes() const
{
return clientSpecificBytes;
}
void Mqtt5PropertyBuilder::clearClientSpecificBytes()
{
clientSpecificBytes.clear();
}
std::shared_ptr<std::vector<std::pair<std::string, std::string>>> Mqtt5PropertyBuilder::getUserProperties() const
{
return this->userProperties;
}
void Mqtt5PropertyBuilder::writeServerKeepAlive(uint16_t val)
{
writeUint16(Mqtt5Properties::ServerKeepAlive, val);
}
void Mqtt5PropertyBuilder::writeSessionExpiry(uint32_t val)
{
writeUint32(Mqtt5Properties::SessionExpiryInterval, val, genericBytes);
}
void Mqtt5PropertyBuilder::writeReceiveMax(uint16_t val)
{
writeUint16(Mqtt5Properties::ReceiveMaximum, val);
}
void Mqtt5PropertyBuilder::writeRetainAvailable(uint8_t val)
{
writeUint8(Mqtt5Properties::RetainAvailable, val);
}
void Mqtt5PropertyBuilder::writeMaxPacketSize(uint32_t val)
{
writeUint32(Mqtt5Properties::MaximumPacketSize, val, genericBytes);
}
void Mqtt5PropertyBuilder::writeAssignedClientId(const std::string &clientid)
{
writeStr(Mqtt5Properties::AssignedClientIdentifier, clientid);
}
void Mqtt5PropertyBuilder::writeMaxTopicAliases(uint16_t val)
{
writeUint16(Mqtt5Properties::TopicAliasMaximum, val);
}
void Mqtt5PropertyBuilder::writeWildcardSubscriptionAvailable(uint8_t val)
{
writeUint8(Mqtt5Properties::WildcardSubscriptionAvailable, val);
}
void Mqtt5PropertyBuilder::writeSubscriptionIdentifiersAvailable(uint8_t val)
{
writeUint8(Mqtt5Properties::SubscriptionIdentifierAvailable, val);
}
void Mqtt5PropertyBuilder::writeSharedSubscriptionAvailable(uint8_t val)
{
writeUint8(Mqtt5Properties::SharedSubscriptionAvailable, val);
}
void Mqtt5PropertyBuilder::writeContentType(const std::string &format)
{
writeStr(Mqtt5Properties::ContentType, format);
}
void Mqtt5PropertyBuilder::writePayloadFormatIndicator(uint8_t val)
{
writeUint8(Mqtt5Properties::PayloadFormatIndicator, val);
}
void Mqtt5PropertyBuilder::writeMessageExpiryInterval(uint32_t val)
{
writeUint32(Mqtt5Properties::MessageExpiryInterval, val, clientSpecificBytes);
}
void Mqtt5PropertyBuilder::writeResponseTopic(const std::string &str)
{
writeStr(Mqtt5Properties::ResponseTopic, str);
}
void Mqtt5PropertyBuilder::writeUserProperty(std::string &&key, std::string &&value)
{
if (this->userPropertyCount++ > 50)
throw ProtocolError("Trying to set more than 50 user properties. Likely a bad actor.", ReasonCodes::ImplementationSpecificError);
write2Str(Mqtt5Properties::UserProperty, key, value);
if (!this->userProperties)
this->userProperties = std::make_shared<std::vector<std::pair<std::string, std::string>>>();
std::pair<std::string, std::string> pair(std::move(key), std::move(value));
this->userProperties->push_back(std::move(pair));
}
void Mqtt5PropertyBuilder::writeCorrelationData(const std::string &correlationData)
{
writeStr(Mqtt5Properties::CorrelationData, correlationData);
}
void Mqtt5PropertyBuilder::writeTopicAlias(const uint16_t id)
{
writeUint16(Mqtt5Properties::TopicAlias, id, clientSpecificBytes);
}
void Mqtt5PropertyBuilder::writeAuthenticationMethod(const std::string &method)
{
writeStr(Mqtt5Properties::AuthenticationMethod, method);
}
void Mqtt5PropertyBuilder::writeAuthenticationData(const std::string &data)
{
writeStr(Mqtt5Properties::AuthenticationData, data);
}
void Mqtt5PropertyBuilder::writeWillDelay(uint32_t delay)
{
writeUint32(Mqtt5Properties::WillDelayInterval, delay, genericBytes);
}
void Mqtt5PropertyBuilder::setNewUserProperties(const std::shared_ptr<std::vector<std::pair<std::string, std::string>>> &userProperties)
{
assert(!this->userProperties);
assert(this->genericBytes.empty());
assert(this->clientSpecificBytes.empty());
this->userProperties = userProperties;
}
void Mqtt5PropertyBuilder::writeUint32(Mqtt5Properties prop, const uint32_t x, std::vector<char> &target) const
{
size_t pos = target.size();
const size_t newSize = pos + 5;
target.resize(newSize);
const uint8_t a = static_cast<uint8_t>(x >> 24);
const uint8_t b = static_cast<uint8_t>(x >> 16);
const uint8_t c = static_cast<uint8_t>(x >> 8);
const uint8_t d = static_cast<uint8_t>(x);
target[pos++] = static_cast<uint8_t>(prop);
target[pos++] = a;
target[pos++] = b;
target[pos++] = c;
target[pos] = d;
}
void Mqtt5PropertyBuilder::writeUint16(Mqtt5Properties prop, const uint16_t x)
{
writeUint16(prop, x, this->genericBytes);
}
void Mqtt5PropertyBuilder::writeUint16(Mqtt5Properties prop, const uint16_t x, std::vector<char> &target) const
{
size_t pos = target.size();
const size_t newSize = pos + 3;
target.resize(newSize);
const uint8_t a = static_cast<uint8_t>(x >> 8);
const uint8_t b = static_cast<uint8_t>(x);
target[pos++] = static_cast<uint8_t>(prop);
target[pos++] = a;
target[pos] = b;
}
void Mqtt5PropertyBuilder::writeUint8(Mqtt5Properties prop, const uint8_t x)
{
size_t pos = genericBytes.size();
const size_t newSize = pos + 2;
genericBytes.resize(newSize);
genericBytes[pos++] = static_cast<uint8_t>(prop);
genericBytes[pos] = x;
}
void Mqtt5PropertyBuilder::writeStr(Mqtt5Properties prop, const std::string &str)
{
if (str.length() > 65535)
throw ProtocolError("String too long.", ReasonCodes::MalformedPacket);
const uint16_t strlen = str.length();
size_t pos = genericBytes.size();
const size_t newSize = pos + strlen + 3;
genericBytes.resize(newSize);
const uint8_t a = static_cast<uint8_t>(strlen >> 8);
const uint8_t b = static_cast<uint8_t>(strlen);
genericBytes[pos++] = static_cast<uint8_t>(prop);
genericBytes[pos++] = a;
genericBytes[pos++] = b;
std::memcpy(&genericBytes[pos], str.c_str(), strlen);
}
void Mqtt5PropertyBuilder::write2Str(Mqtt5Properties prop, const std::string &one, const std::string &two)
{
size_t pos = genericBytes.size();
const size_t newSize = pos + one.length() + two.length() + 5;
genericBytes.resize(newSize);
genericBytes[pos++] = static_cast<uint8_t>(prop);
std::vector<const std::string*> strings;
strings.push_back(&one);
strings.push_back(&two);
for (const std::string *str : strings)
{
if (str->length() > 0xFFFF)
throw ProtocolError("String too long.", ReasonCodes::MalformedPacket);
const uint16_t strlen = str->length();
const uint8_t a = static_cast<uint8_t>(strlen >> 8);
const uint8_t b = static_cast<uint8_t>(strlen);
genericBytes[pos++] = a;
genericBytes[pos++] = b;
std::memcpy(&genericBytes[pos], str->c_str(), strlen);
pos += strlen;
}
}