-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_impl.c
172 lines (159 loc) · 5.51 KB
/
mqtt_impl.c
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
/*
* Copyright (C) 2022 Mikhail Burakov. This file is part of mqttfs.
*
* mqttfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mqttfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mqttfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mqtt_impl.h"
#include <arpa/inet.h>
#include <stddef.h>
#include <sys/uio.h>
#include <unistd.h>
#ifndef UNCONST
#define UNCONST(op) ((void*)(uintptr_t)(op))
#endif // UNCONST
#ifndef LENGTH
#define LENGTH(op) (sizeof(op) / sizeof *(op))
#endif // LENGTH
// TODO(mburakov): Implement more robust sending-receiving.
static size_t EncodeLength(uint32_t length, uint8_t digits[4]) {
if (length > 268435455) return 0;
size_t result = 0;
for (;;) {
digits[result] = length & 0x7f;
length = length >> 7;
if (length) {
digits[result] |= 0x80;
result++;
} else {
return result + 1;
}
}
}
_Bool SendConnectMessage(int fd, uint16_t keepalive) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
uint16_t protocol_name_length;
char protocol_name[4];
uint8_t protocol_level;
uint8_t connect_flags;
uint16_t keepalive;
uint16_t client_id_length;
} connect_message = {
.packet_type = 0x10,
.message_length = 12,
.protocol_name_length = htons(4),
.protocol_name = {'M', 'Q', 'T', 'T'},
.protocol_level = 4,
.connect_flags = 0x02,
.keepalive = htons(keepalive),
.client_id_length = 0,
};
_Static_assert(sizeof(connect_message) == 14,
"Unexpected connect message size");
return write(fd, &connect_message, sizeof(connect_message)) ==
sizeof(connect_message);
}
_Bool ReceiveConnectAck(int fd) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
uint8_t connack_flags;
uint8_t return_code;
} connect_ack;
_Static_assert(sizeof(connect_ack) == 4, "Unexpected connect ack size");
return read(fd, &connect_ack, sizeof(connect_ack)) == sizeof(connect_ack) &&
connect_ack.packet_type == 0x20 && connect_ack.message_length == 2 &&
connect_ack.connack_flags == 0x00 && connect_ack.return_code == 0;
}
_Bool SendSubscribeMessage(int fd) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
uint16_t packet_identifier;
uint16_t topic_length;
char topic[3];
uint8_t qos;
} subscribe_message = {
.packet_type = 0x82,
.message_length = 8,
.packet_identifier = htons(1),
.topic_length = htons(3),
.topic = {'+', '/', '#'},
.qos = 0x00,
};
_Static_assert(sizeof(subscribe_message) == 10,
"Unexpected subscribe message size");
return write(fd, &subscribe_message, sizeof(subscribe_message)) ==
sizeof(subscribe_message);
}
_Bool ReceiveSubscribeAck(int fd) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
uint16_t packet_identifier;
uint8_t return_code;
} subscribe_ack;
_Static_assert(sizeof(subscribe_ack) == 5, "Unexpected subscribe ack size");
return read(fd, &subscribe_ack, sizeof(subscribe_ack)) ==
sizeof(subscribe_ack) &&
subscribe_ack.packet_type == 0x90 &&
subscribe_ack.message_length == 3 &&
subscribe_ack.packet_identifier == htons(1) &&
subscribe_ack.return_code == 0;
}
_Bool SendPingMessage(int fd) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
} ping_message = {
.packet_type = 0xd0,
.message_length = 0,
};
_Static_assert(sizeof(ping_message) == 2, "Unexpected ping message size");
return write(fd, &ping_message, sizeof(ping_message)) == sizeof(ping_message);
}
_Bool SendDisconnectMessage(int fd) {
struct __attribute__((__packed__)) {
uint8_t packet_type;
uint8_t message_length;
} disconnect_message = {
.packet_type = 0xe0,
.message_length = 0,
};
_Static_assert(sizeof(disconnect_message) == 2,
"Unexpected disconnect message size");
return write(fd, &disconnect_message, sizeof(disconnect_message)) ==
sizeof(disconnect_message);
}
_Bool SendPublishMessage(int fd, const char* topic, uint16_t topic_size,
const void* payload, uint32_t payload_size) {
static const uint8_t kPacketType = 0x30;
uint8_t length_digits[4];
size_t length_digits_count = EncodeLength(
sizeof(topic_size) + topic_size + payload_size, length_digits);
if (!length_digits_count) return 0;
uint16_t topic_size_no = htons(topic_size);
struct iovec iov[] = {
{.iov_base = UNCONST(&kPacketType), .iov_len = sizeof(kPacketType)},
{.iov_base = UNCONST(length_digits), .iov_len = length_digits_count},
{.iov_base = &topic_size_no, .iov_len = sizeof(topic_size_no)},
{.iov_base = UNCONST(topic), .iov_len = topic_size},
{.iov_base = UNCONST(payload), .iov_len = payload_size},
};
ssize_t write_length = 0;
for (size_t idx = 0; idx < LENGTH(iov); idx++)
write_length += iov[idx].iov_len;
return writev(fd, iov, LENGTH(iov)) == write_length;
}