SNode.C
Loading...
Searching...
No Matches
Transmitter.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 "web/websocket/Transmitter.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "log/Logger.h"
47#include "utils/hexdump.h"
48
49#include <endian.h>
50#include <string>
51#include <vector>
52
53#endif /* DOXYGEN_SHOULD_SKIP_THIS */
54
55#ifndef WSMAXFRAMEPAYLOADLENGTH
56#define WSMAXFRAMEPAYLOADLENGTH 16384
57#endif
58
59namespace web::websocket {
60
61 Transmitter::Transmitter(bool masking)
62 : masking(masking) {
63 }
64
67
68 void Transmitter::sendMessage(uint8_t opCode, const char* message, std::size_t messageLength) {
69 send(true, opCode, message, messageLength);
70 }
71
72 void Transmitter::sendMessageStart(uint8_t opCode, const char* message, std::size_t messageLength) {
73 send(false, opCode, message, messageLength);
74 }
75
76 void Transmitter::sendMessageFrame(const char* message, std::size_t messageLength) {
77 send(false, 0, message, messageLength);
78 }
79
80 void Transmitter::sendMessageEnd(const char* message, std::size_t messageLength) {
81 send(true, 0, message, messageLength);
82 }
83
84 std::size_t Transmitter::getPayloadTotalSent() const {
85 return payloadTotalSent;
86 }
87
88 void Transmitter::send(bool end, uint8_t opCode, const char* message, std::size_t messageLength) {
89 std::size_t messageOffset = 0;
90
91 do {
92 const std::size_t sendFrameLength =
93 (messageLength - messageOffset <= WSMAXFRAMEPAYLOADLENGTH) ? messageLength - messageOffset : WSMAXFRAMEPAYLOADLENGTH;
94
95 const bool fin = (sendFrameLength == messageLength - messageOffset) && end;
96
97 sendFrame(fin, opCode, message + messageOffset, sendFrameLength);
98
99 messageOffset += sendFrameLength;
100
101 opCode = 0; // continuation
102 } while (messageLength - messageOffset > 0);
103
104 payloadTotalSent += messageLength;
105 }
106
107 void Transmitter::sendFrame(bool fin, uint8_t opCode, const char* payload, uint64_t payloadLength) {
108 uint64_t length = 0;
109
110 if (payloadLength < 126) {
111 length = payloadLength;
112 } else if (payloadLength < 0x10000) {
113 length = 126;
114 } else {
115 length = 127;
116 }
117
118 char header[2];
119 header[0] = static_cast<char>((fin ? 0b10000000 : 0) | opCode);
120
121 header[1] = static_cast<char>((masking ? 0b10000000 : 0) | length);
122
123 sendFrameData(header, 2);
124
125 switch (length) {
126 case 126:
127 sendFrameData(static_cast<uint16_t>(payloadLength));
128 break;
129 case 127:
130 sendFrameData(payloadLength);
131 break;
132 }
133
134 union MaskingKey {
135 uint32_t keyAsValue;
136 char keyAsBytes[4];
137 };
138
139 MaskingKey maskingKeyAsArray = {.keyAsValue = distribution(randomDevice)};
140
141 if (payloadLength > 0) {
142 LOG(TRACE) << "WebSocket send: Frame data\n" << utils::hexDump(payload, payloadLength, 32, true);
143 }
144
145 if (masking) {
146 sendFrameData(maskingKeyAsArray.keyAsBytes, 4);
147
148 std::vector<char> maskedPayload(payload, payload + payloadLength);
149 for (uint64_t i = 0; i < payloadLength; i++) {
150 maskedPayload[static_cast<std::size_t>(i)] =
151 static_cast<char>(maskedPayload[static_cast<std::size_t>(i)] ^ maskingKeyAsArray.keyAsBytes[i % 4]);
152 }
153 sendFrameData(maskedPayload.data(), payloadLength);
154 } else {
155 sendFrameData(payload, payloadLength);
156 }
157 }
158
159 void Transmitter::sendFrameData(uint8_t data) const {
160 if (!closeSent) {
161 sendFrameData(reinterpret_cast<char*>(&data), sizeof(uint8_t));
162 }
163 }
164
165 void Transmitter::sendFrameData(uint16_t data) const {
166 if (!closeSent) {
167 uint16_t sendData = htobe16(data);
168 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint16_t));
169 }
170 }
171
172 void Transmitter::sendFrameData(uint32_t data) const {
173 if (!closeSent) {
174 uint32_t sendData = htobe32(data);
175 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint32_t));
176 }
177 }
178
179 void Transmitter::sendFrameData(uint64_t data) const {
180 if (!closeSent) {
181 uint64_t sendData = htobe64(data);
182 sendFrameData(reinterpret_cast<char*>(&sendData), sizeof(uint64_t));
183 }
184 }
185
186 void Transmitter::sendFrameData(const char* frame, uint64_t frameLength) const {
187 if (!closeSent) {
188 uint64_t frameOffset = 0;
189
190 do {
191 const std::size_t sendChunkLen =
192 (frameLength - frameOffset <= SIZE_MAX) ? static_cast<std::size_t>(frameLength - frameOffset) : SIZE_MAX;
193
194 sendFrameChunk(frame + frameOffset, sendChunkLen);
195
196 frameOffset += sendChunkLen;
197 } while (frameLength - frameOffset > 0);
198 }
199 }
200
201} // namespace web::websocket
#define WSMAXFRAMEPAYLOADLENGTH
void sendMessageEnd(const char *message, std::size_t messageLength)
void sendFrameData(const char *frame, uint64_t frameLength) const
std::random_device randomDevice
Definition Transmitter.h:88
void sendFrameData(uint8_t data) const
void sendFrameData(uint16_t data) const
void sendMessage(uint8_t opCode, const char *message, std::size_t messageLength)
void sendFrameData(uint32_t data) const
virtual void sendFrameChunk(const char *data, std::size_t dataLength) const =0
void send(bool end, uint8_t opCode, const char *message, std::size_t messageLength)
void sendFrame(bool fin, uint8_t opCode, const char *payload, uint64_t payloadLength)
void sendFrameData(uint64_t data) const
std::uniform_int_distribution< uint32_t > distribution
Definition Transmitter.h:89
std::size_t getPayloadTotalSent() const
void sendMessageStart(uint8_t opCode, const char *message, std::size_t messageLength)
void sendMessageFrame(const char *message, std::size_t messageLength)
Definition Config.h:59
std::string hexDump(const char *bytes, uint64_t length, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:66