2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2019-02-28 16:56:15 +00:00
|
|
|
Copyright (c) 2009-2019 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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
2018-08-16 10:14:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
#include <assert.h>
|
2014-09-14 17:08:09 +00:00
|
|
|
#include <time.h>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2016-07-08 09:10:04 +00:00
|
|
|
#include "mosquitto_broker_internal.h"
|
2019-01-18 21:30:34 +00:00
|
|
|
#include "alias_mosq.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "memory_mosq.h"
|
2015-04-29 20:23:59 +00:00
|
|
|
#include "packet_mosq.h"
|
2018-10-25 09:24:19 +00:00
|
|
|
#include "property_mosq.h"
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "time_mosq.h"
|
2019-09-25 11:17:17 +00:00
|
|
|
#include "util_mosq.h"
|
2019-05-21 13:35:43 +00:00
|
|
|
#include "will_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#include "uthash.h"
|
|
|
|
|
2015-09-03 16:46:55 +00:00
|
|
|
struct mosquitto *context__init(struct mosquitto_db *db, mosq_sock_t sock)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
struct mosquitto *context;
|
|
|
|
char address[1024];
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
context = mosquitto__calloc(1, sizeof(struct mosquitto));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!context) return NULL;
|
|
|
|
|
2018-03-08 12:43:41 +00:00
|
|
|
context->pollfd_index = -1;
|
2019-09-24 10:54:05 +00:00
|
|
|
mosquitto__set_state(context, mosq_cs_new);
|
2014-05-07 22:27:00 +00:00
|
|
|
context->sock = sock;
|
|
|
|
context->last_msg_in = mosquitto_time();
|
2016-02-28 17:24:43 +00:00
|
|
|
context->next_msg_out = mosquitto_time() + 60;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->keepalive = 60; /* Default to 60s */
|
2018-11-27 10:02:10 +00:00
|
|
|
context->clean_start = true;
|
2014-05-07 22:27:00 +00:00
|
|
|
context->id = NULL;
|
|
|
|
context->last_mid = 0;
|
|
|
|
context->will = NULL;
|
|
|
|
context->username = NULL;
|
|
|
|
context->password = NULL;
|
|
|
|
context->listener = NULL;
|
|
|
|
context->acl_list = NULL;
|
2019-04-13 21:59:29 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
/* is_bridge records whether this client is a bridge or not. This could be
|
|
|
|
* done by looking at context->bridge for bridges that we create ourself,
|
|
|
|
* but incoming bridges need some other way of being recorded. */
|
|
|
|
context->is_bridge = false;
|
|
|
|
|
|
|
|
context->in_packet.payload = NULL;
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(&context->in_packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
context->out_packet = NULL;
|
|
|
|
context->current_out_packet = NULL;
|
|
|
|
|
|
|
|
context->address = NULL;
|
2014-10-26 21:17:08 +00:00
|
|
|
if((int)sock >= 0){
|
2015-05-16 17:43:06 +00:00
|
|
|
if(!net__socket_get_address(sock, address, 1024)){
|
2015-04-19 21:10:59 +00:00
|
|
|
context->address = mosquitto__strdup(address);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
if(!context->address){
|
|
|
|
/* getpeername and inet_ntop failed and not a bridge */
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context);
|
2014-05-07 22:27:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context->bridge = NULL;
|
2019-04-13 21:59:29 +00:00
|
|
|
context->msgs_in.inflight_maximum = db->config->max_inflight_messages;
|
|
|
|
context->msgs_out.inflight_maximum = db->config->max_inflight_messages;
|
|
|
|
context->msgs_in.inflight_quota = db->config->max_inflight_messages;
|
|
|
|
context->msgs_out.inflight_quota = db->config->max_inflight_messages;
|
2019-01-09 17:56:01 +00:00
|
|
|
context->maximum_qos = 2;
|
2014-05-07 22:27:00 +00:00
|
|
|
#ifdef WITH_TLS
|
|
|
|
context->ssl = NULL;
|
|
|
|
#endif
|
|
|
|
|
2014-10-26 21:17:08 +00:00
|
|
|
if((int)context->sock >= 0){
|
2014-06-23 22:47:48 +00:00
|
|
|
HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(context->sock), context);
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will result in any outgoing packets going unsent. If we're disconnected
|
|
|
|
* forcefully then it is usually an error condition and shouldn't be a problem,
|
|
|
|
* but it will mean that CONNACK messages will never get sent for bad protocol
|
|
|
|
* versions for example.
|
|
|
|
*/
|
2015-05-16 14:24:24 +00:00
|
|
|
void context__cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__packet *packet;
|
2019-02-06 13:42:49 +00:00
|
|
|
#ifdef WITH_BRIDGE
|
2014-11-18 23:34:54 +00:00
|
|
|
int i;
|
2019-02-06 13:42:49 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(!context) return;
|
|
|
|
|
|
|
|
#ifdef WITH_BRIDGE
|
|
|
|
if(context->bridge){
|
2014-11-18 23:34:54 +00:00
|
|
|
for(i=0; i<db->bridge_count; i++){
|
|
|
|
if(db->bridges[i] == context){
|
|
|
|
db->bridges[i] = NULL;
|
2014-06-28 00:38:58 +00:00
|
|
|
}
|
2014-11-18 23:34:54 +00:00
|
|
|
}
|
2015-09-22 13:14:01 +00:00
|
|
|
mosquitto__free(context->bridge->local_clientid);
|
|
|
|
context->bridge->local_clientid = NULL;
|
|
|
|
|
|
|
|
mosquitto__free(context->bridge->local_username);
|
|
|
|
context->bridge->local_username = NULL;
|
|
|
|
|
|
|
|
mosquitto__free(context->bridge->local_password);
|
|
|
|
context->bridge->local_password = NULL;
|
|
|
|
|
2017-07-19 21:04:56 +00:00
|
|
|
if(context->bridge->remote_clientid != context->id){
|
|
|
|
mosquitto__free(context->bridge->remote_clientid);
|
|
|
|
}
|
2015-09-22 13:14:01 +00:00
|
|
|
context->bridge->remote_clientid = NULL;
|
|
|
|
|
2017-07-19 21:04:56 +00:00
|
|
|
if(context->bridge->remote_username != context->username){
|
|
|
|
mosquitto__free(context->bridge->remote_username);
|
|
|
|
}
|
2015-09-22 13:14:01 +00:00
|
|
|
context->bridge->remote_username = NULL;
|
|
|
|
|
2017-07-19 21:04:56 +00:00
|
|
|
if(context->bridge->remote_password != context->password){
|
|
|
|
mosquitto__free(context->bridge->remote_password);
|
|
|
|
}
|
2015-09-22 13:14:01 +00:00
|
|
|
context->bridge->remote_password = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-07-19 21:04:56 +00:00
|
|
|
|
2019-01-18 21:30:34 +00:00
|
|
|
alias__free_all(context);
|
|
|
|
|
2019-04-04 09:55:24 +00:00
|
|
|
mosquitto__free(context->auth_method);
|
|
|
|
context->auth_method = NULL;
|
|
|
|
|
2017-07-19 21:04:56 +00:00
|
|
|
mosquitto__free(context->username);
|
|
|
|
context->username = NULL;
|
|
|
|
|
|
|
|
mosquitto__free(context->password);
|
|
|
|
context->password = NULL;
|
|
|
|
|
2015-05-18 08:29:22 +00:00
|
|
|
net__socket_close(db, context);
|
2019-02-27 10:53:36 +00:00
|
|
|
if(do_free || context->clean_start){
|
2015-05-18 08:29:22 +00:00
|
|
|
sub__clean_session(db, context);
|
2015-05-16 14:24:24 +00:00
|
|
|
db__messages_delete(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-09-22 13:42:56 +00:00
|
|
|
|
|
|
|
mosquitto__free(context->address);
|
|
|
|
context->address = NULL;
|
2014-06-29 22:16:10 +00:00
|
|
|
|
2017-07-16 21:52:01 +00:00
|
|
|
context__send_will(db, context);
|
2017-06-27 13:33:02 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
if(context->id){
|
2019-02-03 22:01:33 +00:00
|
|
|
context__remove_from_by_id(db, context);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context->id);
|
2014-05-07 22:27:00 +00:00
|
|
|
context->id = NULL;
|
|
|
|
}
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(&(context->in_packet));
|
2014-09-22 23:32:34 +00:00
|
|
|
if(context->current_out_packet){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(context->current_out_packet);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context->current_out_packet);
|
2014-09-22 23:32:34 +00:00
|
|
|
context->current_out_packet = NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
while(context->out_packet){
|
2015-05-16 13:16:40 +00:00
|
|
|
packet__cleanup(context->out_packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
packet = context->out_packet;
|
|
|
|
context->out_packet = context->out_packet->next;
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2018-11-27 10:02:10 +00:00
|
|
|
if(do_free || context->clean_start){
|
2019-04-03 10:23:31 +00:00
|
|
|
db__messages_delete(db, context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2018-12-04 12:39:00 +00:00
|
|
|
#if defined(WITH_BROKER) && defined(__GLIBC__) && defined(WITH_ADNS)
|
|
|
|
if(context->adns){
|
|
|
|
gai_cancel(context->adns);
|
|
|
|
mosquitto__free((struct addrinfo *)context->adns->ar_request);
|
|
|
|
mosquitto__free(context->adns);
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
if(do_free){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:33:02 +00:00
|
|
|
|
2017-07-16 21:52:01 +00:00
|
|
|
void context__send_will(struct mosquitto_db *db, struct mosquitto *ctxt)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
if(ctxt->state != mosq_cs_disconnecting && ctxt->will){
|
2019-02-25 22:28:51 +00:00
|
|
|
if(ctxt->will_delay_interval > 0){
|
|
|
|
will_delay__add(ctxt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-24 13:07:09 +00:00
|
|
|
if(mosquitto_acl_check(db, ctxt,
|
|
|
|
ctxt->will->msg.topic,
|
|
|
|
ctxt->will->msg.payloadlen,
|
|
|
|
ctxt->will->msg.payload,
|
|
|
|
ctxt->will->msg.qos,
|
|
|
|
ctxt->will->msg.retain,
|
|
|
|
MOSQ_ACL_WRITE) == MOSQ_ERR_SUCCESS){
|
|
|
|
|
2015-04-18 21:16:57 +00:00
|
|
|
/* Unexpected disconnect, queue the client will. */
|
2018-10-24 13:07:09 +00:00
|
|
|
db__messages_easy_queue(db, ctxt,
|
|
|
|
ctxt->will->msg.topic,
|
|
|
|
ctxt->will->msg.qos,
|
|
|
|
ctxt->will->msg.payloadlen,
|
|
|
|
ctxt->will->msg.payload,
|
2018-10-25 09:24:19 +00:00
|
|
|
ctxt->will->msg.retain,
|
2019-01-22 17:51:57 +00:00
|
|
|
ctxt->will->expiry_interval,
|
2018-10-25 09:24:19 +00:00
|
|
|
&ctxt->will->properties);
|
2015-04-18 21:16:57 +00:00
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2019-05-21 13:35:43 +00:00
|
|
|
will__clear(ctxt);
|
2017-06-27 13:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-06 14:47:16 +00:00
|
|
|
void context__disconnect(struct mosquitto_db *db, struct mosquitto *context)
|
2017-06-27 13:33:02 +00:00
|
|
|
{
|
2019-03-12 22:47:48 +00:00
|
|
|
net__socket_close(db, context);
|
|
|
|
|
2019-05-21 13:35:43 +00:00
|
|
|
context__send_will(db, context);
|
2019-03-06 14:47:16 +00:00
|
|
|
if(context->session_expiry_interval == 0){
|
2017-06-27 13:33:02 +00:00
|
|
|
|
2019-03-06 14:47:16 +00:00
|
|
|
#ifdef WITH_BRIDGE
|
|
|
|
if(!context->bridge)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
|
|
|
|
if(context->will_delay_interval == 0){
|
2019-03-12 22:47:48 +00:00
|
|
|
/* This will be done later, after the will is published for delay>0. */
|
2019-03-06 14:47:16 +00:00
|
|
|
context__add_to_disused(db, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2019-03-20 00:02:38 +00:00
|
|
|
session_expiry__add(db, context);
|
2019-03-06 14:47:16 +00:00
|
|
|
}
|
2019-09-24 10:54:05 +00:00
|
|
|
mosquitto__set_state(context, mosq_cs_disconnected);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
void context__add_to_disused(struct mosquitto_db *db, struct mosquitto *context)
|
2014-09-22 22:35:09 +00:00
|
|
|
{
|
2019-03-12 22:47:48 +00:00
|
|
|
if(context->state == mosq_cs_disused) return;
|
|
|
|
|
2019-09-24 10:54:05 +00:00
|
|
|
mosquitto__set_state(context, mosq_cs_disused);
|
2019-03-12 22:47:48 +00:00
|
|
|
|
2019-03-16 10:24:17 +00:00
|
|
|
if(context->id){
|
|
|
|
context__remove_from_by_id(db, context);
|
|
|
|
mosquitto__free(context->id);
|
|
|
|
context->id = NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:35:09 +00:00
|
|
|
if(db->ll_for_free){
|
|
|
|
context->for_free_next = db->ll_for_free;
|
|
|
|
db->ll_for_free = context;
|
|
|
|
}else{
|
|
|
|
db->ll_for_free = context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
void context__free_disused(struct mosquitto_db *db)
|
2014-09-22 22:35:09 +00:00
|
|
|
{
|
2019-03-17 20:57:04 +00:00
|
|
|
struct mosquitto *context, *next;
|
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
struct mosquitto *last = NULL;
|
|
|
|
#endif
|
2014-10-24 20:57:33 +00:00
|
|
|
assert(db);
|
2014-09-22 22:35:09 +00:00
|
|
|
|
|
|
|
context = db->ll_for_free;
|
|
|
|
while(context){
|
2019-03-17 07:40:01 +00:00
|
|
|
#ifdef WITH_WEBSOCKETS
|
|
|
|
if(context->wsi){
|
|
|
|
/* Don't delete yet, lws hasn't finished with it */
|
|
|
|
if(last){
|
|
|
|
last->for_free_next = context;
|
|
|
|
}else{
|
|
|
|
db->ll_for_free = context;
|
|
|
|
}
|
|
|
|
next = context->for_free_next;
|
|
|
|
context->for_free_next = NULL;
|
|
|
|
last = context;
|
|
|
|
context = next;
|
|
|
|
}else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
next = context->for_free_next;
|
|
|
|
context__cleanup(db, context, true);
|
|
|
|
context = next;
|
|
|
|
}
|
2014-09-22 22:35:09 +00:00
|
|
|
}
|
2014-09-22 22:37:19 +00:00
|
|
|
db->ll_for_free = NULL;
|
2014-09-22 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 22:01:33 +00:00
|
|
|
|
|
|
|
void context__remove_from_by_id(struct mosquitto_db *db, struct mosquitto *context)
|
|
|
|
{
|
|
|
|
if(context->removed_from_by_id == false && context->id){
|
|
|
|
HASH_DELETE(hh_id, db->contexts_by_id, context);
|
|
|
|
context->removed_from_by_id = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 21:31:11 +00:00
|
|
|
|