/src/mosquitto/lib/handle_pubackcomp.c
Line | Count | Source |
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 <stdio.h> |
23 | | #include <string.h> |
24 | | |
25 | | #ifdef WITH_BROKER |
26 | | # include "mosquitto_broker_internal.h" |
27 | | #endif |
28 | | |
29 | | #include "callbacks.h" |
30 | | #include "mosquitto.h" |
31 | | #include "logging_mosq.h" |
32 | | #include "messages_mosq.h" |
33 | | #include "mosquitto/mqtt_protocol.h" |
34 | | #include "net_mosq.h" |
35 | | #include "packet_mosq.h" |
36 | | #include "read_handle.h" |
37 | | #include "send_mosq.h" |
38 | | #include "util_mosq.h" |
39 | | |
40 | | |
41 | | int handle__pubackcomp(struct mosquitto *mosq, const char *type) |
42 | 25 | { |
43 | 25 | uint8_t reason_code = 0; |
44 | 25 | uint16_t mid; |
45 | 25 | int rc; |
46 | 25 | mosquitto_property *properties = NULL; |
47 | 25 | int qos; |
48 | | |
49 | 25 | assert(mosq); |
50 | | |
51 | 25 | if(mosquitto__get_state(mosq) != mosq_cs_active){ |
52 | 0 | return MOSQ_ERR_PROTOCOL; |
53 | 0 | } |
54 | 25 | if(mosq->protocol != mosq_p_mqtt31){ |
55 | 23 | if((mosq->in_packet.command&0x0F) != 0x00){ |
56 | 1 | return MOSQ_ERR_MALFORMED_PACKET; |
57 | 1 | } |
58 | 23 | } |
59 | | |
60 | 24 | COMPAT_pthread_mutex_lock(&mosq->msgs_out.mutex); |
61 | 24 | util__increment_send_quota(mosq); |
62 | 24 | COMPAT_pthread_mutex_unlock(&mosq->msgs_out.mutex); |
63 | | |
64 | 24 | rc = packet__read_uint16(&mosq->in_packet, &mid); |
65 | 24 | if(rc){ |
66 | 0 | return rc; |
67 | 0 | } |
68 | 24 | if(type[3] == 'A'){ /* pubAck or pubComp */ |
69 | 3 | if(mosq->in_packet.command != CMD_PUBACK){ |
70 | 0 | return MOSQ_ERR_MALFORMED_PACKET; |
71 | 0 | } |
72 | 3 | qos = 1; |
73 | 21 | }else{ |
74 | 21 | if(mosq->in_packet.command != CMD_PUBCOMP){ |
75 | 0 | return MOSQ_ERR_MALFORMED_PACKET; |
76 | 0 | } |
77 | 21 | qos = 2; |
78 | 21 | } |
79 | 24 | if(mid == 0){ |
80 | 0 | return MOSQ_ERR_PROTOCOL; |
81 | 0 | } |
82 | | |
83 | 24 | if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){ |
84 | 15 | rc = packet__read_byte(&mosq->in_packet, &reason_code); |
85 | 15 | if(rc){ |
86 | 0 | return rc; |
87 | 0 | } |
88 | | |
89 | 15 | if(mosq->in_packet.remaining_length > 3){ |
90 | 15 | rc = property__read_all(CMD_PUBACK, &mosq->in_packet, &properties); |
91 | 15 | if(rc){ |
92 | 15 | return rc; |
93 | 15 | } |
94 | 15 | } |
95 | 0 | if(type[3] == 'A'){ /* pubAck or pubComp */ |
96 | 0 | if(reason_code != MQTT_RC_SUCCESS |
97 | 0 | && reason_code != MQTT_RC_NO_MATCHING_SUBSCRIBERS |
98 | 0 | && reason_code != MQTT_RC_UNSPECIFIED |
99 | 0 | && reason_code != MQTT_RC_IMPLEMENTATION_SPECIFIC |
100 | 0 | && reason_code != MQTT_RC_NOT_AUTHORIZED |
101 | 0 | && reason_code != MQTT_RC_TOPIC_NAME_INVALID |
102 | 0 | && reason_code != MQTT_RC_PACKET_ID_IN_USE |
103 | 0 | && reason_code != MQTT_RC_QUOTA_EXCEEDED |
104 | 0 | && reason_code != MQTT_RC_PAYLOAD_FORMAT_INVALID |
105 | 0 | ){ |
106 | |
|
107 | 0 | mosquitto_property_free_all(&properties); |
108 | 0 | return MOSQ_ERR_PROTOCOL; |
109 | 0 | } |
110 | 0 | }else{ |
111 | 0 | if(reason_code != MQTT_RC_SUCCESS |
112 | 0 | && reason_code != MQTT_RC_PACKET_ID_NOT_FOUND |
113 | 0 | ){ |
114 | |
|
115 | 0 | mosquitto_property_free_all(&properties); |
116 | 0 | return MOSQ_ERR_PROTOCOL; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | } |
120 | 9 | if(mosq->in_packet.pos < mosq->in_packet.remaining_length){ |
121 | 9 | mosquitto_property_free_all(&properties); |
122 | 9 | return MOSQ_ERR_MALFORMED_PACKET; |
123 | 9 | } |
124 | | |
125 | 0 | #ifdef WITH_BROKER |
126 | 0 | log__printf(NULL, MOSQ_LOG_DEBUG, "Received %s from %s (Mid: %d, RC:%d)", type, SAFE_PRINT(mosq->id), mid, reason_code); |
127 | | |
128 | | /* Immediately free, we don't do anything with Reason String or User Property at the moment */ |
129 | 0 | mosquitto_property_free_all(&properties); |
130 | |
|
131 | 0 | rc = db__message_delete_outgoing(mosq, mid, mosq_ms_wait_for_pubcomp, qos); |
132 | 0 | if(rc == MOSQ_ERR_NOT_FOUND){ |
133 | 0 | log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Received %s from %s for an unknown packet identifier %d.", type, SAFE_PRINT(mosq->id), mid); |
134 | 0 | return MOSQ_ERR_SUCCESS; |
135 | 0 | }else{ |
136 | 0 | return rc; |
137 | 0 | } |
138 | | #else |
139 | | log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received %s (Mid: %d, RC:%d)", SAFE_PRINT(mosq->id), type, mid, reason_code); |
140 | | |
141 | | rc = message__delete(mosq, mid, mosq_md_out, qos); |
142 | | if(rc == MOSQ_ERR_SUCCESS){ |
143 | | /* Only inform the client the message has been sent once. */ |
144 | | callback__on_publish(mosq, mid, reason_code, properties); |
145 | | mosquitto_property_free_all(&properties); |
146 | | }else{ |
147 | | mosquitto_property_free_all(&properties); |
148 | | |
149 | | if(rc != MOSQ_ERR_NOT_FOUND){ |
150 | | return rc; |
151 | | } |
152 | | } |
153 | | |
154 | | COMPAT_pthread_mutex_lock(&mosq->msgs_out.mutex); |
155 | | message__release_to_inflight(mosq, mosq_md_out); |
156 | | COMPAT_pthread_mutex_unlock(&mosq->msgs_out.mutex); |
157 | | |
158 | | return MOSQ_ERR_SUCCESS; |
159 | | #endif |
160 | 0 | } |