Coverage Report

Created: 2026-01-13 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
129
{
43
129
  uint8_t reason_code = 0;
44
129
  uint16_t mid;
45
129
  int rc;
46
129
  mosquitto_property *properties = NULL;
47
48
129
  assert(mosq);
49
50
129
  if(mosquitto__get_state(mosq) != mosq_cs_active){
51
6
    return MOSQ_ERR_PROTOCOL;
52
6
  }
53
123
  if(mosq->in_packet.command != CMD_PUBREC){
54
4
    return MOSQ_ERR_MALFORMED_PACKET;
55
4
  }
56
57
119
  rc = packet__read_uint16(&mosq->in_packet, &mid);
58
119
  if(rc){
59
2
    return rc;
60
2
  }
61
117
  if(mid == 0){
62
1
    return MOSQ_ERR_PROTOCOL;
63
1
  }
64
65
116
  if(mosq->protocol == mosq_p_mqtt5 && mosq->in_packet.remaining_length > 2){
66
101
    rc = packet__read_byte(&mosq->in_packet, &reason_code);
67
101
    if(rc){
68
4
      return rc;
69
4
    }
70
71
97
    if(reason_code != MQTT_RC_SUCCESS
72
51
        && reason_code != MQTT_RC_NO_MATCHING_SUBSCRIBERS
73
45
        && reason_code != MQTT_RC_UNSPECIFIED
74
43
        && reason_code != MQTT_RC_IMPLEMENTATION_SPECIFIC
75
40
        && reason_code != MQTT_RC_NOT_AUTHORIZED
76
39
        && reason_code != MQTT_RC_TOPIC_NAME_INVALID
77
36
        && reason_code != MQTT_RC_PACKET_ID_IN_USE
78
35
        && reason_code != MQTT_RC_QUOTA_EXCEEDED
79
32
        && reason_code != MQTT_RC_PAYLOAD_FORMAT_INVALID){
80
81
28
      return MOSQ_ERR_PROTOCOL;
82
28
    }
83
84
69
    if(mosq->in_packet.remaining_length > 3){
85
69
      rc = property__read_all(CMD_PUBREC, &mosq->in_packet, &properties);
86
69
      if(rc){
87
25
        return rc;
88
25
      }
89
90
      /* Immediately free, we don't do anything with Reason String or User Property at the moment */
91
44
      mosquitto_property_free_all(&properties);
92
44
    }
93
69
  }
94
95
59
  if(mosq->in_packet.pos < mosq->in_packet.remaining_length){
96
40
#ifdef WITH_BROKER
97
40
    mosquitto_property_free_all(&properties);
98
40
#endif
99
40
    return MOSQ_ERR_MALFORMED_PACKET;
100
40
  }
101
102
19
#ifdef WITH_BROKER
103
19
  log__printf(NULL, MOSQ_LOG_DEBUG, "Received PUBREC from %s (Mid: %d)", SAFE_PRINT(mosq->id), mid);
104
105
19
  if(reason_code < 0x80){
106
13
    rc = db__message_update_outgoing(mosq, mid, mosq_ms_wait_for_pubcomp, 2, true);
107
13
  }else{
108
6
    return db__message_delete_outgoing(mosq, mid, mosq_ms_wait_for_pubrec, 2);
109
6
  }
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
13
  if(rc == MOSQ_ERR_NOT_FOUND){
129
13
    log__printf(mosq, MOSQ_LOG_WARNING, "Warning: Received PUBREC from %s for an unknown packet identifier %d.", SAFE_PRINT(mosq->id), mid);
130
13
  }else if(rc != MOSQ_ERR_SUCCESS){
131
0
    return rc;
132
0
  }
133
13
  rc = send__pubrel(mosq, mid, NULL);
134
13
  if(rc){
135
13
    return rc;
136
13
  }
137
138
0
  return MOSQ_ERR_SUCCESS;
139
13
}