2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2016-07-08 08:42:24 +00:00
|
|
|
Copyright (c) 2009-2016 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
2016-03-06 22:30:17 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
The Eclipse Public License is available at
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
2016-03-06 22:30:17 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "logging_mosq.h"
|
|
|
|
#include "memory_mosq.h"
|
2016-03-06 22:30:17 +00:00
|
|
|
#include "mosquitto.h"
|
|
|
|
#include "mosquitto_internal.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mqtt3_protocol.h"
|
|
|
|
#include "packet_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_BROKER
|
2016-07-08 09:10:04 +00:00
|
|
|
# include "mosquitto_broker_internal.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__packet *packet = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
int payloadlen;
|
|
|
|
uint8_t will = 0;
|
|
|
|
uint8_t byte;
|
|
|
|
int rc;
|
2015-01-27 00:32:20 +00:00
|
|
|
uint8_t version;
|
2014-08-16 22:14:41 +00:00
|
|
|
char *clientid, *username, *password;
|
2015-02-06 20:48:45 +00:00
|
|
|
int headerlen;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(mosq);
|
|
|
|
assert(mosq->id);
|
|
|
|
|
2014-06-10 22:30:15 +00:00
|
|
|
#if defined(WITH_BROKER) && defined(WITH_BRIDGE)
|
|
|
|
if(mosq->bridge){
|
2014-08-16 22:14:41 +00:00
|
|
|
clientid = mosq->bridge->remote_clientid;
|
|
|
|
username = mosq->bridge->remote_username;
|
|
|
|
password = mosq->bridge->remote_password;
|
2014-06-10 22:30:15 +00:00
|
|
|
}else{
|
|
|
|
clientid = mosq->id;
|
2014-08-16 22:14:41 +00:00
|
|
|
username = mosq->username;
|
|
|
|
password = mosq->password;
|
2014-06-10 22:30:15 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
clientid = mosq->id;
|
2014-08-16 22:14:41 +00:00
|
|
|
username = mosq->username;
|
|
|
|
password = mosq->password;
|
2014-06-10 22:30:15 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-27 00:32:20 +00:00
|
|
|
if(mosq->protocol == mosq_p_mqtt31){
|
|
|
|
version = MQTT_PROTOCOL_V31;
|
2015-02-06 20:48:45 +00:00
|
|
|
headerlen = 12;
|
2015-01-27 00:32:20 +00:00
|
|
|
}else if(mosq->protocol == mosq_p_mqtt311){
|
|
|
|
version = MQTT_PROTOCOL_V311;
|
2015-02-06 20:48:45 +00:00
|
|
|
headerlen = 10;
|
2015-01-27 00:32:20 +00:00
|
|
|
}else{
|
|
|
|
return MOSQ_ERR_INVAL;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!packet) return MOSQ_ERR_NOMEM;
|
|
|
|
|
2014-06-10 22:30:15 +00:00
|
|
|
payloadlen = 2+strlen(clientid);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(mosq->will){
|
|
|
|
will = 1;
|
|
|
|
assert(mosq->will->topic);
|
|
|
|
|
|
|
|
payloadlen += 2+strlen(mosq->will->topic) + 2+mosq->will->payloadlen;
|
|
|
|
}
|
2014-08-16 22:14:41 +00:00
|
|
|
if(username){
|
|
|
|
payloadlen += 2+strlen(username);
|
|
|
|
if(password){
|
|
|
|
payloadlen += 2+strlen(password);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
packet->command = CONNECT;
|
2015-02-06 20:48:45 +00:00
|
|
|
packet->remaining_length = headerlen+payloadlen;
|
2015-05-16 13:16:40 +00:00
|
|
|
rc = packet__alloc(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Variable header */
|
2015-01-27 00:32:20 +00:00
|
|
|
if(version == MQTT_PROTOCOL_V31){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, PROTOCOL_NAME_v31, strlen(PROTOCOL_NAME_v31));
|
2015-01-27 00:32:20 +00:00
|
|
|
}else if(version == MQTT_PROTOCOL_V311){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, PROTOCOL_NAME_v311, strlen(PROTOCOL_NAME_v311));
|
2015-01-27 00:32:20 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
#if defined(WITH_BROKER) && defined(WITH_BRIDGE)
|
|
|
|
if(mosq->bridge && mosq->bridge->try_private && mosq->bridge->try_private_accepted){
|
|
|
|
version |= 0x80;
|
|
|
|
}else{
|
|
|
|
}
|
|
|
|
#endif
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_byte(packet, version);
|
2014-05-07 22:27:00 +00:00
|
|
|
byte = (clean_session&0x1)<<1;
|
|
|
|
if(will){
|
|
|
|
byte = byte | ((mosq->will->retain&0x1)<<5) | ((mosq->will->qos&0x3)<<3) | ((will&0x1)<<2);
|
|
|
|
}
|
2014-08-16 22:14:41 +00:00
|
|
|
if(username){
|
2014-05-07 22:27:00 +00:00
|
|
|
byte = byte | 0x1<<7;
|
|
|
|
if(mosq->password){
|
|
|
|
byte = byte | 0x1<<6;
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_byte(packet, byte);
|
|
|
|
packet__write_uint16(packet, keepalive);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
/* Payload */
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, clientid, strlen(clientid));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(will){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, mosq->will->topic, strlen(mosq->will->topic));
|
|
|
|
packet__write_string(packet, (const char *)mosq->will->payload, mosq->will->payloadlen);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-08-16 22:14:41 +00:00
|
|
|
if(username){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, username, strlen(username));
|
2014-08-16 22:14:41 +00:00
|
|
|
if(password){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__write_string(packet, password, strlen(password));
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mosq->keepalive = keepalive;
|
|
|
|
#ifdef WITH_BROKER
|
|
|
|
# ifdef WITH_BRIDGE
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending CONNECT", clientid);
|
2014-05-07 22:27:00 +00:00
|
|
|
# endif
|
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending CONNECT", clientid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 13:16:40 +00:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|