Coverage Report

Created: 2023-06-07 06:10

/src/mosquitto/lib/send_unsubscribe.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 <string.h>
23
24
#ifdef WITH_BROKER
25
#  include "mosquitto_broker_internal.h"
26
#endif
27
28
#include "mosquitto.h"
29
#include "logging_mosq.h"
30
#include "memory_mosq.h"
31
#include "mqtt_protocol.h"
32
#include "packet_mosq.h"
33
#include "property_mosq.h"
34
#include "send_mosq.h"
35
#include "util_mosq.h"
36
37
38
int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *const *const topic, const mosquitto_property *properties)
39
0
{
40
0
  struct mosquitto__packet *packet = NULL;
41
0
  uint32_t packetlen;
42
0
  uint16_t local_mid;
43
0
  int rc;
44
0
  int i;
45
0
  size_t tlen;
46
47
0
  assert(mosq);
48
0
  assert(topic);
49
50
0
  packetlen = 2;
51
0
  for(i=0; i<topic_count; i++){
52
0
    tlen = strlen(topic[i]);
53
0
    if(tlen > UINT16_MAX){
54
0
      return MOSQ_ERR_INVAL;
55
0
    }
56
0
    packetlen += 2U+(uint16_t)tlen;
57
0
  }
58
59
0
  if(mosq->protocol == mosq_p_mqtt5){
60
0
    packetlen += property__get_remaining_length(properties);
61
0
  }
62
63
0
  rc = packet__alloc(&packet, CMD_UNSUBSCRIBE | 2, packetlen);
64
0
  if(rc){
65
0
    return rc;
66
0
  }
67
68
  /* Variable header */
69
0
  local_mid = mosquitto__mid_generate(mosq);
70
0
  if(mid) *mid = (int)local_mid;
71
0
  packet__write_uint16(packet, local_mid);
72
73
0
  if(mosq->protocol == mosq_p_mqtt5){
74
    /* We don't use User Property yet. */
75
0
    property__write_all(packet, properties, true);
76
0
  }
77
78
  /* Payload */
79
0
  for(i=0; i<topic_count; i++){
80
0
    packet__write_string(packet, topic[i], (uint16_t)strlen(topic[i]));
81
0
  }
82
83
0
#ifdef WITH_BROKER
84
0
# ifdef WITH_BRIDGE
85
0
  for(i=0; i<topic_count; i++){
86
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", SAFE_PRINT(mosq->id), local_mid, topic[i]);
87
0
  }
88
0
# endif
89
#else
90
  for(i=0; i<topic_count; i++){
91
    log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending UNSUBSCRIBE (Mid: %d, Topic: %s)", SAFE_PRINT(mosq->id), local_mid, topic[i]);
92
  }
93
#endif
94
0
  return packet__queue(mosq, packet);
95
0
}
96