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