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.
|
2016-03-06 23:30:17 +01: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.
|
2016-03-06 23:30:17 +01: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>
|
2017-03-06 22:19:53 +01:00
|
|
|
#include <string.h>
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2018-04-16 12:48:42 +02:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
|
# include "mosquitto_broker_internal.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-04-29 22:37:47 +02:00
|
|
|
#include "logging_mosq.h"
|
|
|
|
|
#include "memory_mosq.h"
|
2016-03-06 23:30:17 +01:00
|
|
|
#include "mosquitto.h"
|
|
|
|
|
#include "mosquitto_internal.h"
|
2018-08-29 23:26:16 +02:00
|
|
|
#include "mqtt_protocol.h"
|
2015-04-29 22:37:47 +02:00
|
|
|
#include "packet_mosq.h"
|
2018-10-10 13:42:27 +02:00
|
|
|
#include "property_mosq.h"
|
2021-03-21 10:17:53 +01:00
|
|
|
#include "send_mosq.h"
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2018-11-02 00:50:54 +01:00
|
|
|
int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session, 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;
|
2020-10-17 02:23:08 +02:00
|
|
|
uint32_t payloadlen;
|
2014-05-08 00:27:00 +02:00
|
|
|
uint8_t will = 0;
|
|
|
|
|
uint8_t byte;
|
|
|
|
|
int rc;
|
2015-01-27 01:32:20 +01:00
|
|
|
uint8_t version;
|
2014-08-17 00:14:41 +02:00
|
|
|
char *clientid, *username, *password;
|
2020-10-17 02:23:08 +02:00
|
|
|
uint32_t headerlen;
|
|
|
|
|
uint32_t proplen = 0, varbytes;
|
2019-01-08 13:27:19 +01:00
|
|
|
mosquitto_property *local_props = NULL;
|
|
|
|
|
uint16_t receive_maximum;
|
2014-05-08 00:27:00 +02:00
|
|
|
|
|
|
|
|
assert(mosq);
|
2018-12-05 21:26:43 +01:00
|
|
|
|
|
|
|
|
if(mosq->protocol == mosq_p_mqtt31 && !mosq->id) return MOSQ_ERR_PROTOCOL;
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2014-06-11 00:30:15 +02:00
|
|
|
#if defined(WITH_BROKER) && defined(WITH_BRIDGE)
|
|
|
|
|
if(mosq->bridge){
|
2014-08-17 00:14:41 +02:00
|
|
|
clientid = mosq->bridge->remote_clientid;
|
|
|
|
|
username = mosq->bridge->remote_username;
|
|
|
|
|
password = mosq->bridge->remote_password;
|
2014-06-11 00:30:15 +02:00
|
|
|
}else{
|
|
|
|
|
clientid = mosq->id;
|
2014-08-17 00:14:41 +02:00
|
|
|
username = mosq->username;
|
|
|
|
|
password = mosq->password;
|
2014-06-11 00:30:15 +02:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
clientid = mosq->id;
|
2014-08-17 00:14:41 +02:00
|
|
|
username = mosq->username;
|
|
|
|
|
password = mosq->password;
|
2014-06-11 00:30:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
2018-12-30 22:51:05 +01:00
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
2019-01-08 13:27:19 +01:00
|
|
|
/* Generate properties from options */
|
|
|
|
|
if(!mosquitto_property_read_int16(properties, MQTT_PROP_RECEIVE_MAXIMUM, &receive_maximum, false)){
|
2019-04-13 23:59:29 +02:00
|
|
|
rc = mosquitto_property_add_int16(&local_props, MQTT_PROP_RECEIVE_MAXIMUM, mosq->msgs_in.inflight_maximum);
|
2019-01-08 13:27:19 +01:00
|
|
|
if(rc) return rc;
|
|
|
|
|
}else{
|
2019-04-13 23:59:29 +02:00
|
|
|
mosq->msgs_in.inflight_maximum = receive_maximum;
|
|
|
|
|
mosq->msgs_in.inflight_quota = receive_maximum;
|
2019-01-08 13:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-30 22:51:05 +01:00
|
|
|
version = MQTT_PROTOCOL_V5;
|
2018-10-25 14:21:42 +02:00
|
|
|
headerlen = 10;
|
2019-01-08 13:27:19 +01:00
|
|
|
proplen = 0;
|
|
|
|
|
proplen += property__get_length_all(properties);
|
|
|
|
|
proplen += property__get_length_all(local_props);
|
2018-10-25 14:21:42 +02:00
|
|
|
varbytes = packet__varint_bytes(proplen);
|
|
|
|
|
headerlen += proplen + varbytes;
|
2015-01-27 01:32:20 +01:00
|
|
|
}else if(mosq->protocol == mosq_p_mqtt311){
|
|
|
|
|
version = MQTT_PROTOCOL_V311;
|
2015-02-06 21:48:45 +01:00
|
|
|
headerlen = 10;
|
2018-12-30 22:51:05 +01:00
|
|
|
}else if(mosq->protocol == mosq_p_mqtt31){
|
|
|
|
|
version = MQTT_PROTOCOL_V31;
|
|
|
|
|
headerlen = 12;
|
2015-01-27 01:32:20 +01:00
|
|
|
}else{
|
|
|
|
|
return MOSQ_ERR_INVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 21:26:43 +01:00
|
|
|
if(clientid){
|
2020-10-17 02:23:08 +02:00
|
|
|
payloadlen = (uint32_t)(2U+strlen(clientid));
|
2018-12-05 21:26:43 +01:00
|
|
|
}else{
|
2020-10-17 02:23:08 +02:00
|
|
|
payloadlen = 2U;
|
2018-12-05 21:26:43 +01:00
|
|
|
}
|
2020-12-01 15:27:54 +01:00
|
|
|
#ifdef WITH_BROKER
|
|
|
|
|
if(mosq->will && (mosq->bridge == NULL || mosq->bridge->notifications_local_only == false)){
|
|
|
|
|
#else
|
2014-05-08 00:27:00 +02:00
|
|
|
if(mosq->will){
|
2020-12-01 15:27:54 +01:00
|
|
|
#endif
|
2014-05-08 00:27:00 +02:00
|
|
|
will = 1;
|
2018-10-24 15:07:09 +02:00
|
|
|
assert(mosq->will->msg.topic);
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2020-10-17 02:23:08 +02:00
|
|
|
payloadlen += (uint32_t)(2+strlen(mosq->will->msg.topic) + 2+(uint32_t)mosq->will->msg.payloadlen);
|
2018-10-10 13:42:27 +02:00
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
2019-10-03 11:36:24 +02:00
|
|
|
payloadlen += property__get_remaining_length(mosq->will->properties);
|
2018-10-10 13:42:27 +02:00
|
|
|
}
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
2019-05-22 00:56:22 +02:00
|
|
|
|
|
|
|
|
/* After this check we can be sure that the username and password are
|
|
|
|
|
* always valid for the current protocol, so there is no need to check
|
|
|
|
|
* username before checking password. */
|
|
|
|
|
if(mosq->protocol == mosq_p_mqtt31 || mosq->protocol == mosq_p_mqtt311){
|
|
|
|
|
if(password != NULL && username == NULL){
|
|
|
|
|
return MOSQ_ERR_INVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-17 00:14:41 +02:00
|
|
|
if(username){
|
2020-10-17 02:23:08 +02:00
|
|
|
payloadlen += (uint32_t)(2+strlen(username));
|
2019-05-22 00:56:22 +02:00
|
|
|
}
|
|
|
|
|
if(password){
|
2020-10-17 02:23:08 +02:00
|
|
|
payloadlen += (uint32_t)(2+strlen(password));
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-30 15:53:34 +02:00
|
|
|
rc = packet__alloc(&packet, CMD_CONNECT, headerlen + payloadlen);
|
2014-05-08 00:27:00 +02:00
|
|
|
if(rc){
|
2015-04-19 23:10:59 +02:00
|
|
|
mosquitto__free(packet);
|
2014-05-08 00:27:00 +02:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Variable header */
|
2015-01-27 01:32:20 +01:00
|
|
|
if(version == MQTT_PROTOCOL_V31){
|
2020-12-02 16:59:45 +01:00
|
|
|
packet__write_string(packet, PROTOCOL_NAME_v31, (uint16_t)strlen(PROTOCOL_NAME_v31));
|
2018-12-30 22:51:05 +01:00
|
|
|
}else{
|
2020-12-02 16:59:45 +01:00
|
|
|
packet__write_string(packet, PROTOCOL_NAME, (uint16_t)strlen(PROTOCOL_NAME));
|
2015-01-27 01:32:20 +01:00
|
|
|
}
|
2014-05-08 00:27:00 +02:00
|
|
|
#if defined(WITH_BROKER) && defined(WITH_BRIDGE)
|
2019-10-09 11:52:58 +02:00
|
|
|
if(mosq->bridge && mosq->bridge->protocol_version != mosq_p_mqtt5 && mosq->bridge->try_private && mosq->bridge->try_private_accepted){
|
2014-05-08 00:27:00 +02:00
|
|
|
version |= 0x80;
|
|
|
|
|
}else{
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-05-16 15:16:40 +02:00
|
|
|
packet__write_byte(packet, version);
|
2020-12-02 11:41:58 +01:00
|
|
|
byte = (uint8_t)((clean_session&0x1)<<1);
|
2014-05-08 00:27:00 +02:00
|
|
|
if(will){
|
2020-10-17 02:23:08 +02:00
|
|
|
byte = byte | (uint8_t)(((mosq->will->msg.qos&0x3)<<3) | ((will&0x1)<<2));
|
2019-11-07 14:31:42 +01:00
|
|
|
if(mosq->retain_available){
|
2020-10-17 02:23:08 +02:00
|
|
|
byte |= (uint8_t)((mosq->will->msg.retain&0x1)<<5);
|
2019-11-07 14:31:42 +01:00
|
|
|
}
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
2014-08-17 00:14:41 +02:00
|
|
|
if(username){
|
2014-05-08 00:27:00 +02:00
|
|
|
byte = byte | 0x1<<7;
|
2019-05-22 00:56:22 +02:00
|
|
|
}
|
|
|
|
|
if(mosq->password){
|
|
|
|
|
byte = byte | 0x1<<6;
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
2015-05-16 15:16:40 +02:00
|
|
|
packet__write_byte(packet, byte);
|
|
|
|
|
packet__write_uint16(packet, keepalive);
|
2014-05-08 00:27:00 +02:00
|
|
|
|
2018-12-30 22:51:05 +01:00
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
|
|
|
|
/* Write properties */
|
2019-01-08 13:27:19 +01:00
|
|
|
packet__write_varint(packet, proplen);
|
|
|
|
|
property__write_all(packet, properties, false);
|
|
|
|
|
property__write_all(packet, local_props, false);
|
2018-12-30 22:51:05 +01:00
|
|
|
}
|
2019-08-01 15:49:19 +02:00
|
|
|
mosquitto_property_free_all(&local_props);
|
2018-12-30 22:51:05 +01:00
|
|
|
|
2014-05-08 00:27:00 +02:00
|
|
|
/* Payload */
|
2018-12-05 21:26:43 +01:00
|
|
|
if(clientid){
|
2020-10-17 02:23:08 +02:00
|
|
|
packet__write_string(packet, clientid, (uint16_t)strlen(clientid));
|
2018-12-05 21:26:43 +01:00
|
|
|
}else{
|
|
|
|
|
packet__write_uint16(packet, 0);
|
|
|
|
|
}
|
2014-05-08 00:27:00 +02:00
|
|
|
if(will){
|
2018-10-10 13:42:27 +02:00
|
|
|
if(mosq->protocol == mosq_p_mqtt5){
|
|
|
|
|
/* Write will properties */
|
2018-12-20 16:32:43 +01:00
|
|
|
property__write_all(packet, mosq->will->properties, true);
|
2018-10-10 13:42:27 +02:00
|
|
|
}
|
2020-10-17 02:23:08 +02:00
|
|
|
packet__write_string(packet, mosq->will->msg.topic, (uint16_t)strlen(mosq->will->msg.topic));
|
|
|
|
|
packet__write_string(packet, (const char *)mosq->will->msg.payload, (uint16_t)mosq->will->msg.payloadlen);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
2019-05-22 00:56:22 +02:00
|
|
|
|
2014-08-17 00:14:41 +02:00
|
|
|
if(username){
|
2020-10-17 02:23:08 +02:00
|
|
|
packet__write_string(packet, username, (uint16_t)strlen(username));
|
2019-05-22 00:56:22 +02:00
|
|
|
}
|
|
|
|
|
if(password){
|
2020-10-17 02:23:08 +02:00
|
|
|
packet__write_string(packet, password, (uint16_t)strlen(password));
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mosq->keepalive = keepalive;
|
|
|
|
|
#ifdef WITH_BROKER
|
|
|
|
|
# ifdef WITH_BRIDGE
|
2015-05-18 09:53:21 +02:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending CONNECT", clientid);
|
2014-05-08 00:27:00 +02:00
|
|
|
# endif
|
|
|
|
|
#else
|
2015-05-18 09:53:21 +02:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending CONNECT", clientid);
|
2014-05-08 00:27:00 +02:00
|
|
|
#endif
|
2015-05-16 15:16:40 +02:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-08 00:27:00 +02:00
|
|
|
}
|
|
|
|
|
|