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