-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_t.cpp
More file actions
69 lines (57 loc) · 2.09 KB
/
Copy pathwebsocket_t.cpp
File metadata and controls
69 lines (57 loc) · 2.09 KB
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
#include "websocket_t.h"
#include "mqtt_t.h"
#include "utils.h"
#include <cstring>
namespace websocket {
//mqtt can travel over websocket, so passing a mqtt here will also be needed
WebSocketRFC::~WebSocketRFC() noexcept {}
WebSocketRFC::WebSocketRFC(const IParseable::type &res)
: IParseable<Result_t,WebSocketRFC>{res} , m_maskKey{0}
{}
WebSocketRFC &WebSocketRFC::operator()(const IParseable::type &res)
{
PRINTDBG("websocket_t.cpp");
struct EthL4 eth = utils::GetEthL4(res.data);
MAYBEUNUSED size_t offset = 0;
MAYBEUNUSED size_t total = res.out.len;
switch (eth.type) {
case EthL4::TCP: {
// peel off
offset = eth.options_len + (sizeof(struct ether_header) + sizeof(struct ip) + sizeof(tcphdr));
const char *pdata = (const char*)res.data+offset;
if (pdata && (unsigned char)pdata[0] == WebSocketRFC::StartHeader)
{
unsigned int weblen = (unsigned char)(pdata[1] & WebSocketRFC::PayloadLenMask);
auto mqtt = mqtt::MqttRFC{res};
mqtt.WebSocketOffset = weblen;
jsonb.add(tjson::JsonField{"protocol", "WebSocket"});
if (mqtt.WebSocketOffset > WebSocketRFC::MaskOffset) {
mqtt.WebSocketMask.value = utils::tobin<unsigned int>(pdata+WebSocketRFC::MaskOffset);
jsonb.add(tjson::JsonField{"mask", mqtt.WebSocketMask.value});
}
if (mqtt().Valid) {
//in case we have mqtt serialized - add it here
if (mqtt.WebSocketMask.value && mqtt.WebSocketOffset) {
jsonb.add(tjson::JsonField{"WebSocket->MQTT", true});
for (const auto &json : mqtt.jsonb.fields) {
jsonb.add(json);
}
}
}
Valid = true;
}
break;
}
FALLTROUGH;
case EthL4::UDP: //Websocket can't go over udp - fall trough default
case EthL4::UNKNOWN:
default:
break;
}
return *this;
}
WebSocketRFC& WebSocketRFC ::operator()()
{
return this->operator()(this->value);
}
} //ns websocket