SNode.C
Loading...
Searching...
No Matches
Connect.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "iot/mqtt/server/packets/Connect.h"
43
44#include "iot/mqtt/server/Mqtt.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "utils/Uuid.h"
49
50#include <string>
51
52#endif // DOXYGEN_SHOULD_SKIP_THIS
53
54namespace iot::mqtt::server::packets {
55
56 Connect::Connect(uint32_t remainingLength, uint8_t flags)
57 : iot::mqtt::server::ControlPacketDeserializer(remainingLength, flags, MQTT_CONNECT_FLAGS) {
58 this->flags = flags;
59 }
60
61 std::size_t Connect::deserializeVP(iot::mqtt::MqttContext* mqttContext) {
62 std::size_t consumed = 0;
63
64 switch (state) {
65 case 0: // V-Header
66 consumed += protocol.deserialize(mqttContext);
68 break;
69 }
70
71 state++;
72 [[fallthrough]];
73 case 1:
74 consumed += level.deserialize(mqttContext);
75 if (!level.isComplete()) {
76 break;
77 }
78 reflect = (level & 0x80) == 0; // msb in level == 1 -> do not reflect message to origin (try_private in mosquitto)
79 level = level & ~0x80;
80
81 state++;
82 [[fallthrough]];
83 case 2:
84 consumed += connectFlags.deserialize(mqttContext);
86 break;
87 }
88
89 reserved = (connectFlags & 0x01) != 0;
90 cleanSession = (connectFlags & 0x02) != 0;
91 willFlag = (connectFlags & 0x04) != 0;
92 willQoS = (connectFlags & 0x18) >> 3;
93 willRetain = (connectFlags & 0x20) != 0;
94 passwordFlag = (connectFlags & 0x40) != 0;
95 usernameFlag = (connectFlags & 0x80) != 0;
96
97 state++;
98 [[fallthrough]];
99 case 3:
100 consumed += keepAlive.deserialize(mqttContext);
102 break;
103 }
104
105 state++;
106 [[fallthrough]];
107 case 4: // Payload
108 consumed += clientId.deserialize(mqttContext);
110 break;
111 }
112
113 if (clientId.size() == 0) {
115 fakedClientId = true;
116 }
117
118 state++;
119 [[fallthrough]];
120 case 5:
121 if (willFlag) {
122 consumed += willTopic.deserialize(mqttContext);
124 break;
125 }
126 }
127
128 state++;
129 [[fallthrough]];
130 case 6:
131 if (willFlag) {
132 consumed += willMessage.deserialize(mqttContext);
134 break;
135 }
136 }
137
138 state++;
139 [[fallthrough]];
140 case 7:
141 if (usernameFlag) {
142 consumed += username.deserialize(mqttContext);
144 break;
145 }
146 }
147
148 state++;
149 [[fallthrough]];
150 case 8:
151 if (passwordFlag) {
152 consumed += password.deserialize(mqttContext);
154 break;
155 }
156 }
157
158 complete = true;
159 break;
160 }
161
162 return consumed;
163 }
164
165 void Connect::deliverPacket(iot::mqtt::server::Mqtt* mqtt) {
166 mqtt->printVP(*this);
167 mqtt->_onConnect(*this);
168 }
169
170 bool Connect::isFakedClientId() const {
171 return fakedClientId;
172 }
173
174} // namespace iot::mqtt::server::packets
void printVP(const iot::mqtt::ControlPacket &packet) const
Definition Mqtt.cpp:368
iot::mqtt::types::String willMessage
Definition Connect.h:108
iot::mqtt::types::String protocol
Definition Connect.h:102
iot::mqtt::types::String willTopic
Definition Connect.h:107
iot::mqtt::types::UInt8 connectFlags
Definition Connect.h:104
iot::mqtt::types::String username
Definition Connect.h:109
iot::mqtt::types::UInt8 level
Definition Connect.h:103
iot::mqtt::types::String clientId
Definition Connect.h:106
iot::mqtt::types::String password
Definition Connect.h:110
iot::mqtt::types::UInt16 keepAlive
Definition Connect.h:105
void _onConnect(const iot::mqtt::server::packets::Connect &connect)
Definition Mqtt.cpp:221
void deliverPacket(iot::mqtt::server::Mqtt *mqtt) override
Definition Connect.cpp:165
Connect(uint32_t remainingLength, uint8_t flags)
Definition Connect.cpp:56
std::size_t deserializeVP(iot::mqtt::MqttContext *mqttContext) override
Definition Connect.cpp:61
std::size_t deserialize(iot::mqtt::MqttContext *mqttContext) override
Definition String.cpp:67
String & operator=(const std::string &newValue)
Definition String.cpp:104
virtual std::size_t deserialize(iot::mqtt::MqttContext *mqttContext)
Definition TypeBase.hpp:59
UInt8 & operator=(const uint8_t &newValue)
Definition UInt8.cpp:57
static std::string getUuid()
Definition Uuid.cpp:52
#define MQTT_CONNECT_FLAGS