-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
retain.c
322 lines (265 loc) · 8.75 KB
/
retain.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright (c) 2010-2019 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "util_mosq.h"
#include "utlist.h"
static struct mosquitto__retainhier *retain__add_hier_entry(struct mosquitto__retainhier *parent, struct mosquitto__retainhier **sibling, const char *topic, uint16_t len)
{
struct mosquitto__retainhier *child;
assert(sibling);
child = mosquitto__calloc(1, sizeof(struct mosquitto__retainhier));
if(!child){
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return NULL;
}
child->parent = parent;
child->topic_len = len;
child->topic = mosquitto__malloc((size_t)len+1);
if(!child->topic){
child->topic_len = 0;
mosquitto__free(child);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return NULL;
}else{
strncpy(child->topic, topic, (size_t)child->topic_len+1);
}
HASH_ADD_KEYPTR(hh, *sibling, child->topic, child->topic_len, child);
return child;
}
int retain__init(void)
{
struct mosquitto__retainhier *retainhier;
retainhier = retain__add_hier_entry(NULL, &db.retains, "", 0);
if(!retainhier) return MOSQ_ERR_NOMEM;
retainhier = retain__add_hier_entry(NULL, &db.retains, "$SYS", (uint16_t)strlen("$SYS"));
if(!retainhier) return MOSQ_ERR_NOMEM;
return MOSQ_ERR_SUCCESS;
}
void retain__clean_empty_hierarchy(struct mosquitto__retainhier *retainhier)
{
struct mosquitto__retainhier *parent;
while(retainhier){
if(retainhier->children || retainhier->retained || retainhier->parent == NULL){
/* Entry is being used */
return;
}else{
HASH_DELETE(hh, retainhier->parent->children, retainhier);
mosquitto__free(retainhier->topic);
parent = retainhier->parent;
mosquitto__free(retainhier);
retainhier = parent;
}
}
}
int retain__store(const char *topic, struct mosquitto_msg_store *stored, char **split_topics)
{
struct mosquitto__retainhier *retainhier;
struct mosquitto__retainhier *branch;
int i;
size_t slen;
assert(stored);
assert(split_topics);
HASH_FIND(hh, db.retains, split_topics[0], strlen(split_topics[0]), retainhier);
if(retainhier == NULL){
retainhier = retain__add_hier_entry(NULL, &db.retains, split_topics[0], (uint16_t)strlen(split_topics[0]));
if(!retainhier) return MOSQ_ERR_NOMEM;
}
for(i=0; split_topics[i] != NULL; i++){
slen = strlen(split_topics[i]);
HASH_FIND(hh, retainhier->children, split_topics[i], slen, branch);
if(branch == NULL){
branch = retain__add_hier_entry(retainhier, &retainhier->children, split_topics[i], (uint16_t)slen);
if(branch == NULL){
return MOSQ_ERR_NOMEM;
}
}
retainhier = branch;
}
#ifdef WITH_PERSISTENCE
if(strncmp(topic, "$SYS", 4)){
/* Retained messages count as a persistence change, but only if
* they aren't for $SYS. */
db.persistence_changes++;
}
#else
UNUSED(topic);
#endif
if(retainhier->retained){
db__msg_store_ref_dec(&retainhier->retained);
#ifdef WITH_SYS_TREE
db.retained_count--;
#endif
}
if(stored->payloadlen){
retainhier->retained = stored;
db__msg_store_ref_inc(retainhier->retained);
#ifdef WITH_SYS_TREE
db.retained_count++;
#endif
}else{
retainhier->retained = NULL;
retain__clean_empty_hierarchy(retainhier);
}
return MOSQ_ERR_SUCCESS;
}
static int retain__process(struct mosquitto__retainhier *branch, struct mosquitto *context, uint8_t sub_qos, uint32_t subscription_identifier)
{
int rc = 0;
uint8_t qos;
uint16_t mid;
mosquitto_property *properties = NULL;
struct mosquitto_msg_store *retained;
if(branch->retained->message_expiry_time > 0 && db.now_real_s >= branch->retained->message_expiry_time){
db__msg_store_ref_dec(&branch->retained);
branch->retained = NULL;
#ifdef WITH_SYS_TREE
db.retained_count--;
#endif
return MOSQ_ERR_SUCCESS;
}
retained = branch->retained;
rc = mosquitto_acl_check(context, retained->topic, retained->payloadlen, retained->payload,
retained->qos, retained->retain, MOSQ_ACL_READ);
if(rc == MOSQ_ERR_ACL_DENIED){
return MOSQ_ERR_SUCCESS;
}else if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
/* Check for original source access */
if(db.config->check_retain_source && retained->origin != mosq_mo_broker && retained->source_id){
struct mosquitto retain_ctxt;
memset(&retain_ctxt, 0, sizeof(struct mosquitto));
retain_ctxt.id = retained->source_id;
retain_ctxt.username = retained->source_username;
retain_ctxt.listener = retained->source_listener;
rc = acl__find_acls(&retain_ctxt);
if(rc) return rc;
rc = mosquitto_acl_check(&retain_ctxt, retained->topic, retained->payloadlen, retained->payload,
retained->qos, retained->retain, MOSQ_ACL_WRITE);
if(rc == MOSQ_ERR_ACL_DENIED){
return MOSQ_ERR_SUCCESS;
}else if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
}
if (db.config->upgrade_outgoing_qos){
qos = sub_qos;
} else {
qos = retained->qos;
if(qos > sub_qos) qos = sub_qos;
}
if(qos > 0){
mid = mosquitto__mid_generate(context);
}else{
mid = 0;
}
if(subscription_identifier > 0){
mosquitto_property_add_varint(&properties, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, subscription_identifier);
}
return db__message_insert(context, mid, mosq_md_out, qos, true, retained, properties, false);
}
static int retain__search(struct mosquitto__retainhier *retainhier, char **split_topics, struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier, int level)
{
struct mosquitto__retainhier *branch, *branch_tmp;
int flag = 0;
if(!strcmp(split_topics[0], "#") && split_topics[1] == NULL){
HASH_ITER(hh, retainhier->children, branch, branch_tmp){
/* Set flag to indicate that we should check for retained messages
* on "foo" when we are subscribing to e.g. "foo/#" and then exit
* this function and return to an earlier retain__search().
*/
flag = -1;
if(branch->retained){
retain__process(branch, context, sub_qos, subscription_identifier);
}
if(branch->children){
retain__search(branch, split_topics, context, sub, sub_qos, subscription_identifier, level+1);
}
}
}else{
if(!strcmp(split_topics[0], "+")){
HASH_ITER(hh, retainhier->children, branch, branch_tmp){
if(split_topics[1] != NULL){
if(retain__search(branch, &(split_topics[1]), context, sub, sub_qos, subscription_identifier, level+1) == -1
|| (split_topics[1] != NULL && !strcmp(split_topics[1], "#") && level>0)){
if(branch->retained){
retain__process(branch, context, sub_qos, subscription_identifier);
}
}
}else{
if(branch->retained){
retain__process(branch, context, sub_qos, subscription_identifier);
}
}
}
}else{
HASH_FIND(hh, retainhier->children, split_topics[0], strlen(split_topics[0]), branch);
if(branch){
if(split_topics[1] != NULL){
if(retain__search(branch, &(split_topics[1]), context, sub, sub_qos, subscription_identifier, level+1) == -1
|| (split_topics[1] != NULL && !strcmp(split_topics[1], "#") && level>0)){
if(branch->retained){
retain__process(branch, context, sub_qos, subscription_identifier);
}
}
}else{
if(branch->retained){
retain__process(branch, context, sub_qos, subscription_identifier);
}
}
}
}
}
return flag;
}
int retain__queue(struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier)
{
struct mosquitto__retainhier *retainhier;
char *local_sub;
char **split_topics;
int rc;
assert(context);
assert(sub);
if(!strncmp(sub, "$share/", strlen("$share/"))){
return MOSQ_ERR_SUCCESS;
}
rc = sub__topic_tokenise(sub, &local_sub, &split_topics, NULL);
if(rc) return rc;
HASH_FIND(hh, db.retains, split_topics[0], strlen(split_topics[0]), retainhier);
if(retainhier){
retain__search(retainhier, split_topics, context, sub, sub_qos, subscription_identifier, 0);
}
mosquitto__free(local_sub);
mosquitto__free(split_topics);
return MOSQ_ERR_SUCCESS;
}
void retain__clean(struct mosquitto__retainhier **retainhier)
{
struct mosquitto__retainhier *peer, *retainhier_tmp;
HASH_ITER(hh, *retainhier, peer, retainhier_tmp){
if(peer->retained){
db__msg_store_ref_dec(&peer->retained);
}
retain__clean(&peer->children);
mosquitto__free(peer->topic);
HASH_DELETE(hh, *retainhier, peer);
mosquitto__free(peer);
}
}