Debian Codegraph @

crt/aws-c-mqtt/source/mqtt_subscription_set.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include "aws/mqtt/private/mqtt_subscription_set.h"
7
8 #include "aws/mqtt/private/client_impl_shared.h"
9
10 #define SUBSCRIPTION_SET_DEFAULT_BRANCH_FACTOR 10
11 #define SUBSCRIPTION_SET_DEFAULT_ENTRY_COUNT 50
12
13 struct aws_mqtt_subscription_set_subscription_record *aws_mqtt_subscription_set_subscription_record_new(
14 struct aws_allocator *allocator,
15 const struct aws_mqtt_subscription_set_subscription_options *subscription) {
16 struct aws_mqtt_subscription_set_subscription_record *record =
17 aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_subscription_set_subscription_record));
18
19 record->allocator = allocator;
20 aws_byte_buf_init_copy_from_cursor(&record->topic_filter, allocator, subscription->topic_filter);
21 record->subscription_view = *subscription;
22 record->subscription_view.topic_filter = aws_byte_cursor_from_buf(&record->topic_filter);
23
24 return record;
25 }
26
27 void aws_mqtt_subscription_set_subscription_record_destroy(
28 struct aws_mqtt_subscription_set_subscription_record *record) {
29 if (record == NULL) {
30 return;
31 }
32
33 aws_byte_buf_clean_up(&record->topic_filter);
34 aws_mem_release(record->allocator, record);
35 }
36
37 static void s_aws_mqtt_subscription_set_subscription_record_hash_destroy(void *element) {
38 struct aws_mqtt_subscription_set_subscription_record *record = element;
39
40 aws_mqtt_subscription_set_subscription_record_destroy(record);
41 }
42
43 static struct aws_mqtt_subscription_set_topic_tree_node *s_aws_mqtt_subscription_set_node_new(
44 struct aws_allocator *allocator,
45 struct aws_mqtt_subscription_set_topic_tree_node *parent) {
46
47 struct aws_mqtt_subscription_set_topic_tree_node *node =
48 aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_subscription_set_topic_tree_node));
49 node->allocator = allocator;
50 aws_hash_table_init(
51 &node->children,
52 allocator,
53 SUBSCRIPTION_SET_DEFAULT_BRANCH_FACTOR,
54 aws_hash_byte_cursor_ptr,
55 aws_mqtt_byte_cursor_hash_equality,
56 NULL,
57 NULL);
58 node->ref_count = 1;
59 node->parent = parent;
60
61 return node;
62 }
63
64 struct aws_mqtt_subscription_set *aws_mqtt_subscription_set_new(struct aws_allocator *allocator) {
65
66 struct aws_mqtt_subscription_set *subscription_set =
67 aws_mem_calloc(allocator, 1, sizeof(struct aws_mqtt_subscription_set));
68
69 subscription_set->allocator = allocator;
70 subscription_set->root = s_aws_mqtt_subscription_set_node_new(allocator, NULL);
71
72 aws_hash_table_init(
73 &subscription_set->subscriptions,
74 allocator,
75 SUBSCRIPTION_SET_DEFAULT_ENTRY_COUNT,
76 aws_hash_byte_cursor_ptr,
77 aws_mqtt_byte_cursor_hash_equality,
78 NULL,
79 s_aws_mqtt_subscription_set_subscription_record_hash_destroy);
80
81 return subscription_set;
82 }
83
84 static int s_subscription_set_node_destroy_hash_foreach_wrap(void *context, struct aws_hash_element *elem);
85
86 static void s_aws_mqtt_subscription_set_node_destroy_node(struct aws_mqtt_subscription_set_topic_tree_node *node) {
87 aws_hash_table_foreach(&node->children, s_subscription_set_node_destroy_hash_foreach_wrap, NULL);
88 aws_hash_table_clean_up(&node->children);
89
90 if (node->on_cleanup && node->callback_user_data) {
91 node->on_cleanup(node->callback_user_data);
92 }
93
94 aws_byte_buf_clean_up(&node->topic_segment);
95
96 aws_mem_release(node->allocator, node);
97 }
98
99 static void s_aws_mqtt_subscription_set_node_destroy_tree(struct aws_mqtt_subscription_set_topic_tree_node *tree) {
100 if (tree == NULL) {
101 return;
102 }
103
104 if (tree->parent != NULL) {
105 aws_hash_table_remove(&tree->parent->children, &tree->topic_segment, NULL, NULL);
106 }
107
108 s_aws_mqtt_subscription_set_node_destroy_node(tree);
109 }
110
111 static int s_subscription_set_node_destroy_hash_foreach_wrap(void *context, struct aws_hash_element *elem) {
112 (void)context;
113
114 s_aws_mqtt_subscription_set_node_destroy_node(elem->value);
115
116 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE | AWS_COMMON_HASH_TABLE_ITER_DELETE;
117 }
118
119 void aws_mqtt_subscription_set_destroy(struct aws_mqtt_subscription_set *subscription_set) {
120 if (subscription_set == NULL) {
121 return;
122 }
123
124 s_aws_mqtt_subscription_set_node_destroy_tree(subscription_set->root);
125
126 aws_hash_table_clean_up(&subscription_set->subscriptions);
127
128 aws_mem_release(subscription_set->allocator, subscription_set);
129 }
130
131 static struct aws_mqtt_subscription_set_topic_tree_node *s_aws_mqtt_subscription_set_get_existing_subscription_node(
132 const struct aws_mqtt_subscription_set *subscription_set,
133 struct aws_byte_cursor topic_filter) {
134
135 struct aws_mqtt_subscription_set_topic_tree_node *current_node = subscription_set->root;
136
137 struct aws_byte_cursor topic_segment;
138 AWS_ZERO_STRUCT(topic_segment);
139 while (aws_byte_cursor_next_split(&topic_filter, '/', &topic_segment)) {
140 struct aws_hash_element *hash_element = NULL;
141 aws_hash_table_find(&current_node->children, &topic_segment, &hash_element);
142
143 if (hash_element == NULL) {
144 return NULL;
145 } else {
146 current_node = hash_element->value;
147 }
148 }
149
150 if (!current_node->is_subscription) {
151 return NULL;
152 }
153
154 return current_node;
155 }
156
157 bool aws_mqtt_subscription_set_is_subscribed(
158 const struct aws_mqtt_subscription_set *subscription_set,
159 struct aws_byte_cursor topic_filter) {
160
161 struct aws_hash_element *element = NULL;
162 aws_hash_table_find(&subscription_set->subscriptions, &topic_filter, &element);
163
164 return element && (element->value != NULL);
165 }
166
167 bool aws_mqtt_subscription_set_is_in_topic_tree(
168 const struct aws_mqtt_subscription_set *subscription_set,
169 struct aws_byte_cursor topic_filter) {
170 struct aws_mqtt_subscription_set_topic_tree_node *existing_node =
171 s_aws_mqtt_subscription_set_get_existing_subscription_node(subscription_set, topic_filter);
172
173 return existing_node != NULL;
174 }
175
176 /*
177 * Walks the existing tree creating nodes as necessary to reach the subscription leaf implied by the topic filter.
178 * Returns the node representing the final level of the topic filter. Each existing node has its ref count increased by
179 * one. Newly-created nodes start with a ref count of one. Given that the topic filter has been validated, the only
180 * possible error is a memory allocation error which is a crash anyways.
181 *
182 * If the leaf node already exists and has a cleanup callback, it will be invoked and both the callback and its user
183 * data will be cleared . The returned node will always have is_subscription set to true.
184 */
185 static struct aws_mqtt_subscription_set_topic_tree_node *
186 s_aws_mqtt_subscription_set_create_or_reference_topic_filter_path(
187 struct aws_mqtt_subscription_set_topic_tree_node *root,
188 struct aws_byte_cursor topic_filter) {
189
190 struct aws_mqtt_subscription_set_topic_tree_node *current_node = root;
191 ++root->ref_count;
192
193 /*
194 * Invariants:
195 * (1) No failure allowed (allocation failure = crash)
196 * (2) The ref count of current_node is always correct *before* the loop condition is evaluated
197 */
198 struct aws_byte_cursor topic_segment;
199 AWS_ZERO_STRUCT(topic_segment);
200 while (aws_byte_cursor_next_split(&topic_filter, '/', &topic_segment)) {
201
202 struct aws_hash_element *hash_element = NULL;
203 aws_hash_table_find(&current_node->children, &topic_segment, &hash_element);
204
205 if (hash_element == NULL) {
206 struct aws_mqtt_subscription_set_topic_tree_node *new_node =
207 s_aws_mqtt_subscription_set_node_new(current_node->allocator, current_node);
208
209 aws_byte_buf_init_copy_from_cursor(&new_node->topic_segment, new_node->allocator, topic_segment);
210 new_node->topic_segment_cursor = aws_byte_cursor_from_buf(&new_node->topic_segment);
211
212 aws_hash_table_put(&current_node->children, &new_node->topic_segment_cursor, new_node, NULL);
213
214 current_node = new_node;
215 } else {
216 current_node = hash_element->value;
217 ++current_node->ref_count;
218 }
219 }
220
221 return current_node;
222 }
223
224 void aws_mqtt_subscription_set_add_subscription(
225 struct aws_mqtt_subscription_set *subscription_set,
226 const struct aws_mqtt_subscription_set_subscription_options *subscription_options) {
227
228 AWS_FATAL_ASSERT(aws_mqtt_is_valid_topic_filter(&subscription_options->topic_filter));
229
230 aws_hash_table_remove(&subscription_set->subscriptions, &subscription_options->topic_filter, NULL, NULL);
231
232 struct aws_mqtt_subscription_set_subscription_record *record =
233 aws_mqtt_subscription_set_subscription_record_new(subscription_set->allocator, subscription_options);
234 aws_hash_table_put(&subscription_set->subscriptions, &record->topic_filter, record, NULL);
235
236 struct aws_mqtt_subscription_set_topic_tree_node *subscription_node =
237 s_aws_mqtt_subscription_set_get_existing_subscription_node(
238 subscription_set, subscription_options->topic_filter);
239 if (subscription_node == NULL) {
240 subscription_node = s_aws_mqtt_subscription_set_create_or_reference_topic_filter_path(
241 subscription_set->root, subscription_options->topic_filter);
242 }
243
244 if (subscription_node->on_cleanup) {
245 (*subscription_node->on_cleanup)(subscription_node->callback_user_data);
246 subscription_node->on_cleanup = NULL;
247 }
248
249 subscription_node->is_subscription = true;
250
251 subscription_node->on_publish_received = subscription_options->on_publish_received;
252 subscription_node->on_cleanup = subscription_options->on_cleanup;
253 subscription_node->callback_user_data = subscription_options->callback_user_data;
254 }
255
256 void aws_mqtt_subscription_set_remove_subscription(
257 struct aws_mqtt_subscription_set *subscription_set,
258 struct aws_byte_cursor topic_filter) {
259
260 aws_hash_table_remove(&subscription_set->subscriptions, &topic_filter, NULL, NULL);
261
262 if (!aws_mqtt_subscription_set_is_in_topic_tree(subscription_set, topic_filter)) {
263 return;
264 }
265
266 struct aws_mqtt_subscription_set_topic_tree_node *current_node = subscription_set->root;
267
268 struct aws_byte_cursor topic_segment;
269 AWS_ZERO_STRUCT(topic_segment);
270 while (aws_byte_cursor_next_split(&topic_filter, '/', &topic_segment)) {
271 --current_node->ref_count;
272
273 if (current_node->ref_count == 0) {
274 s_aws_mqtt_subscription_set_node_destroy_tree(current_node);
275 return;
276 }
277
278 struct aws_hash_element *hash_element = NULL;
279 aws_hash_table_find(&current_node->children, &topic_segment, &hash_element);
280
281 /* We previously validated the full path; this must exist */
282 current_node = hash_element->value;
283 }
284
285 --current_node->ref_count;
286 if (current_node->ref_count == 0) {
287 s_aws_mqtt_subscription_set_node_destroy_tree(current_node);
288 return;
289 }
290
291 if (current_node->on_cleanup) {
292 (*current_node->on_cleanup)(current_node->callback_user_data);
293 current_node->on_cleanup = NULL;
294 }
295
296 current_node->on_publish_received = NULL;
297 current_node->is_subscription = false;
298 }
299
300 struct aws_mqtt_subscription_set_path_continuation {
301 struct aws_byte_cursor current_fragment;
302 struct aws_mqtt_subscription_set_topic_tree_node *current_node;
303 };
304
305 static void s_add_subscription_set_path_continuation(
306 struct aws_array_list *paths,
307 struct aws_byte_cursor fragment,
308 struct aws_mqtt_subscription_set_topic_tree_node *node) {
309 if (node == NULL) {
310 return;
311 }
312
313 struct aws_mqtt_subscription_set_path_continuation path = {
314 .current_fragment = fragment,
315 .current_node = node,
316 };
317
318 aws_array_list_push_back(paths, &path);
319 }
320
321 #define SUBSCRIPTION_SET_PATH_FRAGMENT_DEFAULT 10
322
323 AWS_STATIC_STRING_FROM_LITERAL(s_single_level_wildcard, "+");
324 AWS_STATIC_STRING_FROM_LITERAL(s_multi_level_wildcard, "#");
325
326 static struct aws_mqtt_subscription_set_topic_tree_node *s_aws_mqtt_subscription_set_node_find_child(
327 struct aws_mqtt_subscription_set_topic_tree_node *node,
328 struct aws_byte_cursor fragment) {
329 struct aws_hash_element *element = NULL;
330 aws_hash_table_find(&node->children, &fragment, &element);
331
332 if (element == NULL) {
333 return NULL;
334 }
335
336 return element->value;
337 }
338
339 static void s_invoke_on_publish_received(
340 struct aws_mqtt_subscription_set_topic_tree_node *node,
341 const struct aws_mqtt_subscription_set_publish_received_options *publish_options) {
342 if (node == NULL || !node->is_subscription || node->on_publish_received == NULL) {
343 return;
344 }
345
346 (*node->on_publish_received)(
347 publish_options->connection,
348 &publish_options->topic,
349 &publish_options->payload,
350 publish_options->dup,
351 publish_options->qos,
352 publish_options->retain,
353 node->callback_user_data);
354 }
355
356 void aws_mqtt_subscription_set_on_publish_received(
357 const struct aws_mqtt_subscription_set *subscription_set,
358 const struct aws_mqtt_subscription_set_publish_received_options *publish_options) {
359
360 struct aws_byte_cursor slw_cursor = aws_byte_cursor_from_string(s_single_level_wildcard);
361 struct aws_byte_cursor mlw_cursor = aws_byte_cursor_from_string(s_multi_level_wildcard);
362
363 struct aws_array_list tree_paths;
364 aws_array_list_init_dynamic(
365 &tree_paths,
366 subscription_set->allocator,
367 SUBSCRIPTION_SET_PATH_FRAGMENT_DEFAULT,
368 sizeof(struct aws_mqtt_subscription_set_path_continuation));
369
370 struct aws_byte_cursor empty_cursor;
371 AWS_ZERO_STRUCT(empty_cursor);
372 s_add_subscription_set_path_continuation(&tree_paths, empty_cursor, subscription_set->root);
373
374 while (aws_array_list_length(&tree_paths) > 0) {
375 struct aws_mqtt_subscription_set_path_continuation path_continuation;
376 AWS_ZERO_STRUCT(path_continuation);
377
378 size_t path_count = aws_array_list_length(&tree_paths);
379 aws_array_list_get_at(&tree_paths, &path_continuation, path_count - 1);
380 aws_array_list_pop_back(&tree_paths);
381
382 /*
383 * Invoke multi-level wildcard check before checking split result; this allows a subscription like
384 * 'a/b/#' to match an incoming 'a/b'
385 */
386 struct aws_mqtt_subscription_set_topic_tree_node *mlw_node =
387 s_aws_mqtt_subscription_set_node_find_child(path_continuation.current_node, mlw_cursor);
388 s_invoke_on_publish_received(mlw_node, publish_options);
389
390 struct aws_byte_cursor next_fragment = path_continuation.current_fragment;
391 if (!aws_byte_cursor_next_split(&publish_options->topic, '/', &next_fragment)) {
392 s_invoke_on_publish_received(path_continuation.current_node, publish_options);
393 continue;
394 }
395
396 struct aws_mqtt_subscription_set_topic_tree_node *slw_node =
397 s_aws_mqtt_subscription_set_node_find_child(path_continuation.current_node, slw_cursor);
398 s_add_subscription_set_path_continuation(&tree_paths, next_fragment, slw_node);
399
400 struct aws_mqtt_subscription_set_topic_tree_node *fragment_node =
401 s_aws_mqtt_subscription_set_node_find_child(path_continuation.current_node, next_fragment);
402 s_add_subscription_set_path_continuation(&tree_paths, next_fragment, fragment_node);
403 }
404
405 aws_array_list_clean_up(&tree_paths);
406 }
407
408 static int s_subscription_set_subscriptions_hash_get_wrap(void *context, struct aws_hash_element *elem) {
409 struct aws_array_list *subscriptions = context;
410 struct aws_mqtt_subscription_set_subscription_record *record = elem->value;
411
412 aws_array_list_push_back(subscriptions, &record->subscription_view);
413
414 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
415 }
416
417 void aws_mqtt_subscription_set_get_subscriptions(
418 struct aws_mqtt_subscription_set *subscription_set,
419 struct aws_array_list *subscriptions) {
420 AWS_ZERO_STRUCT(*subscriptions);
421
422 size_t subscription_count = aws_hash_table_get_entry_count(&subscription_set->subscriptions);
423 aws_array_list_init_dynamic(
424 subscriptions,
425 subscription_set->allocator,
426 subscription_count,
427 sizeof(struct aws_mqtt_subscription_set_subscription_options));
428
429 aws_hash_table_foreach(
430 &subscription_set->subscriptions, s_subscription_set_subscriptions_hash_get_wrap, subscriptions);
431 }