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