Coverage Report

Created: 2025-11-07 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/lib/handle_pubrel.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__pubrel(struct mosquitto *mosq)
42
9
{
43
9
  uint8_t reason_code;
44
9
  uint16_t mid;
45
#ifndef WITH_BROKER
46
  struct mosquitto_message_all *message = NULL;
47
#endif
48
9
  int rc;
49
9
  mosquitto_property *properties = NULL;
50
51
9
  assert(mosq);
52
53
9
  if(mosquitto__get_state(mosq) != mosq_cs_active){
54
0
    return MOSQ_ERR_PROTOCOL;
55
0
  }
56
9
  if(mosq->protocol != mosq_p_mqtt31 && mosq->in_packet.command != (CMD_PUBREL|2)){
57
0
    return MOSQ_ERR_MALFORMED_PACKET;
58
0
  }
59
60
9
  if(mosq->protocol != mosq_p_mqtt31){
61
6
    if((mosq->in_packet.command&0x0F) != 0x02){
62
0
      return MOSQ_ERR_PROTOCOL;
63
0
    }
64
6
  }
65
9
  rc = packet__read_uint16(&mosq->in_packet, &mid);
66
9
  if(rc){
67
0
    return rc;
68
0
  }
69
9
  if(mid == 0){
70
0
    return MOSQ_ERR_PROTOCOL;
71
0
  }
72
73
9
  if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
74
3
    rc = packet__read_byte(&mosq->in_packet, &reason_code);
75
3
    if(rc){
76
0
      return rc;
77
0
    }
78
79
3
    if(reason_code != MQTT_RC_SUCCESS && reason_code != MQTT_RC_PACKET_ID_NOT_FOUND){
80
0
      return MOSQ_ERR_PROTOCOL;
81
0
    }
82
83
3
    if(mosq->in_packet.remaining_length > 3){
84
3
      rc = property__read_all(CMD_PUBREL, &mosq->in_packet, &properties);
85
3
      if(rc){
86
2
        return rc;
87
2
      }
88
      /* Immediately free, we don't do anything with Reason String or
89
       * User Property at the moment */
90
1
      mosquitto_property_free_all(&properties);
91
1
    }
92
3
  }
93
94
7
  if(mosq->in_packet.pos < mosq->in_packet.remaining_length){
95
6
    return MOSQ_ERR_MALFORMED_PACKET;
96
6
  }
97
98
1
#ifdef WITH_BROKER
99
1
  log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREL from %s (Mid: %d)", SAFE_PRINT(mosq->id), mid);
100
101
1
  rc = db__message_release_incoming(mosq, mid);
102
1
  if(rc == MOSQ_ERR_NOT_FOUND){
103
    /* Message not found. Still send a PUBCOMP anyway because this could be
104
     * due to a repeated PUBREL after a client has reconnected. */
105
1
  }else if(rc != MOSQ_ERR_SUCCESS){
106
0
    return rc;
107
0
  }
108
109
1
  rc = send__pubcomp(mosq, mid, NULL);
110
1
  if(rc){
111
1
    return rc;
112
1
  }
113
#else
114
  log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received PUBREL (Mid: %d)", SAFE_PRINT(mosq->id), mid);
115
116
  rc = send__pubcomp(mosq, mid, NULL);
117
  if(rc){
118
    message__remove(mosq, mid, mosq_md_in, &message, 2);
119
    return rc;
120
  }
121
122
  rc = message__remove(mosq, mid, mosq_md_in, &message, 2);
123
  if(rc == MOSQ_ERR_SUCCESS){
124
    /* Only pass the message on if we have removed it from the queue - this
125
     * prevents multiple callbacks for the same message. */
126
    callback__on_message(mosq, &message->msg, message->properties);
127
    message__cleanup(&message);
128
  }else if(rc == MOSQ_ERR_NOT_FOUND){
129
    return MOSQ_ERR_SUCCESS;
130
  }else{
131
    return rc;
132
  }
133
#endif
134
135
0
  return MOSQ_ERR_SUCCESS;
136
1
}