Coverage Report

Created: 2025-08-26 06:48

/src/mosquitto/lib/send_disconnect.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
23
#ifdef WITH_BROKER
24
#  include "mosquitto_broker_internal.h"
25
#  include "sys_tree.h"
26
#endif
27
28
#include "mosquitto.h"
29
#include "mosquitto_internal.h"
30
#include "logging_mosq.h"
31
#include "mosquitto/mqtt_protocol.h"
32
#include "packet_mosq.h"
33
#include "property_mosq.h"
34
#include "send_mosq.h"
35
36
37
int send__disconnect(struct mosquitto *mosq, uint8_t reason_code, const mosquitto_property *properties)
38
0
{
39
0
  struct mosquitto__packet *packet = NULL;
40
0
  int rc;
41
0
  uint32_t remaining_length;
42
43
0
  assert(mosq);
44
0
#ifdef WITH_BROKER
45
0
#  ifdef WITH_BRIDGE
46
0
  if(mosq->bridge){
47
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending DISCONNECT", SAFE_PRINT(mosq->id));
48
0
  }else
49
#  else
50
  {
51
    log__printf(mosq, MOSQ_LOG_DEBUG, "Sending DISCONNECT to %s (rc%d)", SAFE_PRINT(mosq->id), reason_code);
52
  }
53
#  endif
54
#else
55
  log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending DISCONNECT", SAFE_PRINT(mosq->id));
56
#endif
57
0
  assert(mosq);
58
59
0
  if(mosq->protocol == mosq_p_mqtt5 && (reason_code != 0 || properties)){
60
0
    remaining_length = 1;
61
0
    if(properties){
62
0
      remaining_length += mosquitto_property_get_remaining_length(properties);
63
0
    }
64
0
  }else{
65
0
    remaining_length = 0;
66
0
  }
67
68
0
  rc = packet__alloc(&packet, CMD_DISCONNECT, remaining_length);
69
0
  if(rc){
70
0
    mosquitto_FREE(packet);
71
0
    return rc;
72
0
  }
73
0
  if(mosq->protocol == mosq_p_mqtt5 && (reason_code != 0 || properties)){
74
0
    packet__write_byte(packet, reason_code);
75
0
    if(properties){
76
0
      property__write_all(packet, properties, true);
77
0
    }
78
0
  }
79
80
0
#ifdef WITH_BROKER
81
0
  metrics__int_inc(mosq_counter_mqtt_disconnect_sent, 1);
82
0
#endif
83
0
  return packet__queue(mosq, packet);
84
0
}
85