/src/mosquitto/lib/send_subscribe.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2009-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <assert.h> |
22 | | #include <string.h> |
23 | | |
24 | | #ifdef WITH_BROKER |
25 | | # include "mosquitto_broker_internal.h" |
26 | | #endif |
27 | | |
28 | | #include "mosquitto.h" |
29 | | #include "mosquitto_internal.h" |
30 | | #include "logging_mosq.h" |
31 | | #include "memory_mosq.h" |
32 | | #include "mqtt_protocol.h" |
33 | | #include "packet_mosq.h" |
34 | | #include "property_mosq.h" |
35 | | #include "send_mosq.h" |
36 | | #include "util_mosq.h" |
37 | | |
38 | | |
39 | | int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, int topic_qos, const mosquitto_property *properties) |
40 | 0 | { |
41 | 0 | struct mosquitto__packet *packet = NULL; |
42 | 0 | uint32_t packetlen; |
43 | 0 | uint16_t local_mid; |
44 | 0 | int rc; |
45 | 0 | int i; |
46 | 0 | size_t tlen; |
47 | |
|
48 | 0 | assert(mosq); |
49 | 0 | assert(topic); |
50 | | |
51 | 0 | packetlen = 2; |
52 | 0 | if(mosq->protocol == mosq_p_mqtt5){ |
53 | 0 | packetlen += property__get_remaining_length(properties); |
54 | 0 | } |
55 | 0 | for(i=0; i<topic_count; i++){ |
56 | 0 | tlen = strlen(topic[i]); |
57 | 0 | if(tlen > UINT16_MAX){ |
58 | 0 | return MOSQ_ERR_INVAL; |
59 | 0 | } |
60 | 0 | packetlen += 2U+(uint16_t)tlen + 1U; |
61 | 0 | } |
62 | | |
63 | 0 | rc = packet__alloc(&packet, CMD_SUBSCRIBE | 2, packetlen); |
64 | 0 | if(rc){ |
65 | 0 | mosquitto__FREE(packet); |
66 | 0 | return rc; |
67 | 0 | } |
68 | | |
69 | | /* Variable header */ |
70 | 0 | local_mid = mosquitto__mid_generate(mosq); |
71 | 0 | if(mid) *mid = (int)local_mid; |
72 | 0 | packet__write_uint16(packet, local_mid); |
73 | |
|
74 | 0 | if(mosq->protocol == mosq_p_mqtt5){ |
75 | 0 | property__write_all(packet, properties, true); |
76 | 0 | } |
77 | | |
78 | | /* Payload */ |
79 | 0 | for(i=0; i<topic_count; i++){ |
80 | 0 | packet__write_string(packet, topic[i], (uint16_t)strlen(topic[i])); |
81 | 0 | packet__write_byte(packet, (uint8_t)topic_qos); |
82 | 0 | } |
83 | |
|
84 | 0 | #ifdef WITH_BROKER |
85 | 0 | # ifdef WITH_BRIDGE |
86 | 0 | log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d, Options: 0x%02x)", SAFE_PRINT(mosq->id), local_mid, topic[0], topic_qos&0x03, topic_qos&0xFC); |
87 | 0 | # endif |
88 | | #else |
89 | | for(i=0; i<topic_count; i++){ |
90 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d, Options: 0x%02x)", SAFE_PRINT(mosq->id), local_mid, topic[i], topic_qos&0x03, topic_qos&0xFC); |
91 | | } |
92 | | #endif |
93 | |
|
94 | 0 | return packet__queue(mosq, packet); |
95 | 0 | } |