2014-05-08 00:27:00 +02:00
|
|
|
/*
|
2021-11-03 23:08:24 +01:00
|
|
|
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
|
2014-05-08 00:27:00 +02:00
|
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
2020-11-25 18:34:21 +01:00
|
|
|
are made available under the terms of the Eclipse Public License 2.0
|
2014-05-08 00:27:00 +02:00
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
2021-10-05 16:20:37 +02:00
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
The Eclipse Public License is available at
|
2020-11-25 18:34:21 +01:00
|
|
|
https://www.eclipse.org/legal/epl-2.0/
|
2014-05-08 00:27:00 +02:00
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
2021-10-05 16:20:37 +02:00
|
|
|
|
2021-01-20 12:46:18 +01:00
|
|
|
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
2020-12-01 19:21:59 +01:00
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
Contributors:
|
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-08-16 12:14:51 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2018-04-16 12:48:42 +02:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
|
# include "mosquitto_broker_internal.h"
|
2023-03-11 00:33:20 +01:00
|
|
|
# include "sys_tree.h"
|
2018-04-16 12:48:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
2015-04-29 22:37:47 +02:00
|
|
|
#include "mosquitto.h"
|
|
|
|
|
#include "mosquitto_internal.h"
|
|
|
|
|
#include "logging_mosq.h"
|
2023-12-23 13:59:58 +01:00
|
|
|
#include "mosquitto/mqtt_protocol.h"
|
2015-04-29 22:37:47 +02:00
|
|
|
#include "net_mosq.h"
|
|
|
|
|
#include "packet_mosq.h"
|
2018-10-10 13:42:27 +02:00
|
|
|
#include "property_mosq.h"
|
2015-04-29 22:37:47 +02:00
|
|
|
#include "send_mosq.h"
|
|
|
|
|
#include "util_mosq.h"
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2015-05-16 16:24:24 +02:00
|
|
|
int send__pingreq(struct mosquitto *mosq)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
assert(mosq);
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGREQ to %s", SAFE_PRINT(mosq->id));
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGREQ", SAFE_PRINT(mosq->id));
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2018-10-30 12:00:16 +01:00
|
|
|
rc = send__simple_command(mosq, CMD_PINGREQ);
|
2014-05-08 00:27:00 +02:00
|
|
|
if(rc == MOSQ_ERR_SUCCESS){
|
|
|
|
|
mosq->ping_t = mosquitto_time();
|
2023-06-28 23:03:14 +02:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
|
metrics__int_inc(mosq_counter_mqtt_pingreq_sent, 1);
|
|
|
|
|
#endif
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2015-05-16 16:24:24 +02:00
|
|
|
int send__pingresp(struct mosquitto *mosq)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGRESP to %s", SAFE_PRINT(mosq->id));
|
2023-06-28 23:03:14 +02:00
|
|
|
metrics__int_inc(mosq_counter_mqtt_pingresp_sent, 1);
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGRESP", SAFE_PRINT(mosq->id));
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2018-10-30 12:00:16 +01:00
|
|
|
return send__simple_command(mosq, CMD_PINGRESP);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2020-07-10 12:03:29 +02:00
|
|
|
int send__puback(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBACK to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code);
|
2023-06-28 23:03:14 +02:00
|
|
|
metrics__int_inc(mosq_counter_mqtt_puback_sent, 1);
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBACK (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code);
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2019-01-08 13:27:19 +01:00
|
|
|
util__increment_receive_quota(mosq);
|
2018-10-25 14:21:42 +02:00
|
|
|
/* We don't use Reason String or User Property yet. */
|
2020-07-10 12:03:29 +02:00
|
|
|
return send__command_with_mid(mosq, CMD_PUBACK, mid, false, reason_code, properties);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2020-07-10 12:03:29 +02:00
|
|
|
int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBCOMP to %s (m%d)", SAFE_PRINT(mosq->id), mid);
|
2023-06-28 23:03:14 +02:00
|
|
|
metrics__int_inc(mosq_counter_mqtt_pubcomp_sent, 1);
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBCOMP (m%d)", SAFE_PRINT(mosq->id), mid);
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2019-01-08 13:27:19 +01:00
|
|
|
util__increment_receive_quota(mosq);
|
2018-10-25 14:21:42 +02:00
|
|
|
/* We don't use Reason String or User Property yet. */
|
2020-07-10 12:03:29 +02:00
|
|
|
return send__command_with_mid(mosq, CMD_PUBCOMP, mid, false, 0, properties);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-07-10 12:03:29 +02:00
|
|
|
int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREC to %s (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code);
|
2023-06-28 23:03:14 +02:00
|
|
|
metrics__int_inc(mosq_counter_mqtt_pubrec_sent, 1);
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREC (m%d, rc%d)", SAFE_PRINT(mosq->id), mid, reason_code);
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2019-04-17 17:15:41 +02:00
|
|
|
if(reason_code >= 0x80 && mosq->protocol == mosq_p_mqtt5){
|
2019-01-08 13:27:19 +01:00
|
|
|
util__increment_receive_quota(mosq);
|
|
|
|
|
}
|
2018-10-25 14:21:42 +02:00
|
|
|
/* We don't use Reason String or User Property yet. */
|
2020-07-10 12:03:29 +02:00
|
|
|
return send__command_with_mid(mosq, CMD_PUBREC, mid, false, reason_code, properties);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2020-07-10 12:03:29 +02:00
|
|
|
int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
|
|
|
|
#ifdef WITH_BROKER
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREL to %s (m%d)", SAFE_PRINT(mosq->id), mid);
|
2023-06-28 23:03:14 +02:00
|
|
|
metrics__int_inc(mosq_counter_mqtt_pubrel_sent, 1);
|
2014-05-08 00:27:00 +02:00
|
|
|
#else
|
2021-11-15 23:29:19 +01:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREL (m%d)", SAFE_PRINT(mosq->id), mid);
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2018-10-25 14:21:42 +02:00
|
|
|
/* We don't use Reason String or User Property yet. */
|
2020-07-10 12:03:29 +02:00
|
|
|
return send__command_with_mid(mosq, CMD_PUBREL|2, mid, false, 0, properties);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
/* For PUBACK, PUBCOMP, PUBREC, and PUBREL */
|
2018-11-02 00:50:54 +01:00
|
|
|
int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup, uint8_t reason_code, const mosquitto_property *properties)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
2015-04-19 23:10:59 +02:00
|
|
|
struct mosquitto__packet *packet = NULL;
|
2014-05-08 00:27:00 +02:00
|
|
|
int rc;
|
2021-06-30 15:53:34 +02:00
|
|
|
uint32_t remaining_length;
|
2014-05-08 00:27:00 +02:00
|
|
|
|
|
|
|
|
assert(mosq);
|
|
|
|
|
|
|
|
|
|
if(dup){
|
2021-06-30 15:53:34 +02:00
|
|
|
command |= 8;
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
2021-06-30 15:53:34 +02:00
|
|
|
remaining_length = 2;
|
2018-10-25 14:21:42 +02:00
|
|
|
|
2018-10-10 13:42:27 +02:00
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
2019-01-17 17:52:44 +01:00
|
|
|
if(reason_code != 0 || properties){
|
2021-06-30 15:53:34 +02:00
|
|
|
remaining_length += 1;
|
2019-01-17 17:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(properties){
|
2024-03-17 16:07:27 +01:00
|
|
|
remaining_length += mosquitto_property_get_remaining_length(properties);
|
2019-01-17 17:52:44 +01:00
|
|
|
}
|
2018-10-10 13:42:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-30 15:53:34 +02:00
|
|
|
rc = packet__alloc(&packet, command, remaining_length);
|
2014-05-08 00:27:00 +02:00
|
|
|
if(rc){
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 13:42:27 +02:00
|
|
|
packet__write_uint16(packet, mid);
|
|
|
|
|
|
|
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
2019-01-17 17:52:44 +01:00
|
|
|
if(reason_code != 0 || properties){
|
|
|
|
|
packet__write_byte(packet, reason_code);
|
|
|
|
|
}
|
|
|
|
|
if(properties){
|
|
|
|
|
property__write_all(packet, properties, true);
|
|
|
|
|
}
|
2018-10-10 13:42:27 +02:00
|
|
|
}
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2015-05-16 15:16:40 +02:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 23:08:07 +02:00
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
/* For DISCONNECT, PINGREQ and PINGRESP */
|
2015-05-16 16:24:24 +02:00
|
|
|
int send__simple_command(struct mosquitto *mosq, uint8_t command)
|
2014-05-08 00:27:00 +02:00
|
|
|
{
|
2015-04-19 23:10:59 +02:00
|
|
|
struct mosquitto__packet *packet = NULL;
|
2014-05-08 00:27:00 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
assert(mosq);
|
|
|
|
|
|
2021-06-30 15:53:34 +02:00
|
|
|
rc = packet__alloc(&packet, command, 0);
|
2014-05-08 00:27:00 +02:00
|
|
|
if(rc){
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-16 15:16:40 +02:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|