/src/mosquitto/lib/handle_suback.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 | | |
23 | | #ifdef WITH_BROKER |
24 | | # include "mosquitto_broker_internal.h" |
25 | | #endif |
26 | | |
27 | | #include "callbacks.h" |
28 | | #include "mosquitto.h" |
29 | | #include "mosquitto_internal.h" |
30 | | #include "logging_mosq.h" |
31 | | #include "mosquitto/mqtt_protocol.h" |
32 | | #include "packet_mosq.h" |
33 | | #include "property_mosq.h" |
34 | | #include "read_handle.h" |
35 | | #include "util_mosq.h" |
36 | | |
37 | | |
38 | | int handle__suback(struct mosquitto *mosq) |
39 | 85 | { |
40 | 85 | uint16_t mid; |
41 | 85 | uint8_t qos; |
42 | 85 | int *granted_qos; |
43 | 85 | int qos_count; |
44 | 85 | int i = 0; |
45 | 85 | int rc; |
46 | 85 | mosquitto_property *properties = NULL; |
47 | | |
48 | 85 | assert(mosq); |
49 | | |
50 | 85 | if(mosquitto__get_state(mosq) != mosq_cs_active){ |
51 | 9 | return MOSQ_ERR_PROTOCOL; |
52 | 9 | } |
53 | 76 | if(mosq->in_packet.command != CMD_SUBACK){ |
54 | 4 | return MOSQ_ERR_MALFORMED_PACKET; |
55 | 4 | } |
56 | | |
57 | 72 | #ifdef WITH_BROKER |
58 | 72 | if(mosq->bridge == NULL){ |
59 | | /* Client is not a bridge, so shouldn't be sending SUBACK */ |
60 | 0 | return MOSQ_ERR_PROTOCOL; |
61 | 0 | } |
62 | 72 | log__printf(NULL, MOSQ_LOG_DEBUG, "Received SUBACK from %s", SAFE_PRINT(mosq->id)); |
63 | | #else |
64 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received SUBACK", SAFE_PRINT(mosq->id)); |
65 | | #endif |
66 | 72 | rc = packet__read_uint16(&mosq->in_packet, &mid); |
67 | 72 | if(rc) return rc; |
68 | 71 | if(mid == 0) return MOSQ_ERR_PROTOCOL; |
69 | | |
70 | 70 | if(mosq->protocol == mosq_p_mqtt5){ |
71 | 32 | rc = property__read_all(CMD_SUBACK, &mosq->in_packet, &properties); |
72 | 32 | if(rc) return rc; |
73 | 32 | } |
74 | | |
75 | 61 | qos_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos); |
76 | 61 | if(qos_count == 0){ |
77 | 16 | mosquitto_property_free_all(&properties); |
78 | 16 | return MOSQ_ERR_PROTOCOL; |
79 | 16 | } |
80 | 45 | granted_qos = mosquitto_malloc((size_t)qos_count*sizeof(int)); |
81 | 45 | if(!granted_qos){ |
82 | 0 | mosquitto_property_free_all(&properties); |
83 | 0 | return MOSQ_ERR_NOMEM; |
84 | 0 | } |
85 | 4.19M | while(mosq->in_packet.pos < mosq->in_packet.remaining_length){ |
86 | 4.19M | rc = packet__read_byte(&mosq->in_packet, &qos); |
87 | 4.19M | if(rc){ |
88 | 0 | mosquitto_FREE(granted_qos); |
89 | 0 | mosquitto_property_free_all(&properties); |
90 | 0 | return rc; |
91 | 0 | } |
92 | 4.19M | granted_qos[i] = (int)qos; |
93 | 4.19M | i++; |
94 | 4.19M | } |
95 | 45 | #ifdef WITH_BROKER |
96 | | /* Immediately free, we don't do anything with Reason String or User Property at the moment */ |
97 | 45 | mosquitto_property_free_all(&properties); |
98 | | #else |
99 | | callback__on_subscribe(mosq, mid, qos_count, granted_qos, properties); |
100 | | mosquitto_property_free_all(&properties); |
101 | | #endif |
102 | 45 | mosquitto_FREE(granted_qos); |
103 | | |
104 | 45 | return MOSQ_ERR_SUCCESS; |
105 | 45 | } |