1 /*
2 Copyright (c) 2009-2020 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 "mosquitto.h"
30 #include "logging_mosq.h"
31 #include "memory_mosq.h"
32 #include "messages_mosq.h"
33 #include "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
handle__pubrec(struct mosquitto * mosq)40 int handle__pubrec(struct mosquitto *mosq)
41 {
42 uint8_t reason_code = 0;
43 uint16_t mid;
44 int rc;
45 mosquitto_property *properties = NULL;
46
47 assert(mosq);
48
49 if(mosquitto__get_state(mosq) != mosq_cs_active){
50 return MOSQ_ERR_PROTOCOL;
51 }
52 if(mosq->in_packet.command != CMD_PUBREC){
53 return MOSQ_ERR_MALFORMED_PACKET;
54 }
55
56 rc = packet__read_uint16(&mosq->in_packet, &mid);
57 if(rc) return rc;
58 if(mid == 0) return MOSQ_ERR_PROTOCOL;
59
60 if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
61 rc = packet__read_byte(&mosq->in_packet, &reason_code);
62 if(rc) return rc;
63
64 if(reason_code != MQTT_RC_SUCCESS
65 && reason_code != MQTT_RC_NO_MATCHING_SUBSCRIBERS
66 && reason_code != MQTT_RC_UNSPECIFIED
67 && reason_code != MQTT_RC_IMPLEMENTATION_SPECIFIC
68 && reason_code != MQTT_RC_NOT_AUTHORIZED
69 && reason_code != MQTT_RC_TOPIC_NAME_INVALID
70 && reason_code != MQTT_RC_PACKET_ID_IN_USE
71 && reason_code != MQTT_RC_QUOTA_EXCEEDED){
72
73 return MOSQ_ERR_PROTOCOL;
74 }
75
76 if(mosq->in_packet.remaining_length > 3){
77 rc = property__read_all(CMD_PUBREC, &mosq->in_packet, &properties);
78 if(rc) return rc;
79
80 /* Immediately free, we don't do anything with Reason String or User Property at the moment */
81 mosquitto_property_free_all(&properties);
82 }
83 }
84
85 if(mosq->in_packet.pos < mosq->in_packet.remaining_length){
86 #ifdef WITH_BROKER
87 mosquitto_property_free_all(&properties);
88 #endif
89 return MOSQ_ERR_MALFORMED_PACKET;
90 }
91
92 #ifdef WITH_BROKER
93 log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREC from %s (Mid: %d)", mosq->id, mid);
94
95 if(reason_code < 0x80){
96 rc = db__message_update_outgoing(mosq, mid, mosq_ms_wait_for_pubcomp, 2);
97 }else{
98 return db__message_delete_outgoing(mosq, mid, mosq_ms_wait_for_pubrec, 2);
99 }
100 #else
101
102 log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PUBREC (Mid: %d)", mosq->id, mid);
103
104 if(reason_code < 0x80 || mosq->protocol != mosq_p_mqtt5){
105 rc = message__out_update(mosq, mid, mosq_ms_wait_for_pubcomp, 2);
106 }else{
107 if(!message__delete(mosq, mid, mosq_md_out, 2)){
108 /* Only inform the client the message has been sent once. */
109 pthread_mutex_lock(&mosq->callback_mutex);
110 if(mosq->on_publish_v5){
111 mosq->in_callback = true;
112 mosq->on_publish_v5(mosq, mosq->userdata, mid, reason_code, properties);
113 mosq->in_callback = false;
114 }
115 pthread_mutex_unlock(&mosq->callback_mutex);
116 }
117 util__increment_send_quota(mosq);
118 pthread_mutex_lock(&mosq->msgs_out.mutex);
119 message__release_to_inflight(mosq, mosq_md_out);
120 pthread_mutex_unlock(&mosq->msgs_out.mutex);
121 return MOSQ_ERR_SUCCESS;
122 }
123 #endif
124 if(rc == MOSQ_ERR_NOT_FOUND){
125 log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Received PUBREC from %s for an unknown packet identifier %d.", mosq->id, mid);
126 }else if(rc != MOSQ_ERR_SUCCESS){
127 return rc;
128 }
129 rc = send__pubrel(mosq, mid, NULL);
130 if(rc) return rc;
131
132 return MOSQ_ERR_SUCCESS;
133 }
134
135