Coverage Report

Created: 2025-06-20 06:45

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