2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2015-04-19 21:10:59 +00:00
|
|
|
Copyright (c) 2010-2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* A note on matching topic subscriptions.
|
|
|
|
*
|
|
|
|
* Topics can be up to 32767 characters in length. The / character is used as a
|
|
|
|
* hierarchy delimiter. Messages are published to a particular topic.
|
|
|
|
* Clients may subscribe to particular topics directly, but may also use
|
|
|
|
* wildcards in subscriptions. The + and # characters are used as wildcards.
|
|
|
|
* The # wildcard can be used at the end of a subscription only, and is a
|
|
|
|
* wildcard for the level of hierarchy at which it is placed and all subsequent
|
|
|
|
* levels.
|
|
|
|
* The + wildcard may be used at any point within the subscription and is a
|
|
|
|
* wildcard for only the level of hierarchy at which it is placed.
|
|
|
|
* Neither wildcard may be used as part of a substring.
|
|
|
|
* Valid:
|
|
|
|
* a/b/+
|
|
|
|
* a/+/c
|
|
|
|
* a/#
|
|
|
|
* a/b/#
|
|
|
|
* #
|
|
|
|
* +/b/c
|
|
|
|
* +/+/+
|
|
|
|
* Invalid:
|
|
|
|
* a/#/c
|
|
|
|
* a+/b/c
|
|
|
|
* Valid but non-matching:
|
|
|
|
* a/b
|
|
|
|
* a/+
|
|
|
|
* +/b
|
|
|
|
* b/c/a
|
|
|
|
* a/b/d
|
|
|
|
*/
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "config.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mosquitto_broker.h"
|
|
|
|
#include "memory_mosq.h"
|
|
|
|
#include "util_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
struct sub__token {
|
|
|
|
struct sub__token *next;
|
2015-04-11 20:17:16 +00:00
|
|
|
mosquitto__topic_element_uhpa topic;
|
2015-04-04 20:15:27 +00:00
|
|
|
uint16_t topic_len;
|
2014-05-07 22:27:00 +00:00
|
|
|
};
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hier, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored, bool set_retain)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int rc2;
|
|
|
|
int client_qos, msg_qos;
|
|
|
|
uint16_t mid;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subleaf *leaf;
|
2014-05-07 22:27:00 +00:00
|
|
|
bool client_retain;
|
|
|
|
|
|
|
|
leaf = hier->subs;
|
|
|
|
|
|
|
|
if(retain && set_retain){
|
|
|
|
#ifdef WITH_PERSISTENCE
|
|
|
|
if(strncmp(topic, "$SYS", 4)){
|
|
|
|
/* Retained messages count as a persistence change, but only if
|
|
|
|
* they aren't for $SYS. */
|
|
|
|
db->persistence_changes++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(hier->retained){
|
2014-11-19 20:59:10 +00:00
|
|
|
mosquitto__db_msg_store_deref(db, &hier->retained);
|
2014-09-16 22:48:26 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-05-07 22:27:00 +00:00
|
|
|
db->retained_count--;
|
2014-09-16 22:48:26 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-11-18 07:42:49 +00:00
|
|
|
if(stored->payloadlen){
|
2014-05-07 22:27:00 +00:00
|
|
|
hier->retained = stored;
|
|
|
|
hier->retained->ref_count++;
|
2014-09-16 22:48:26 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-05-07 22:27:00 +00:00
|
|
|
db->retained_count++;
|
2014-09-16 22:48:26 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
|
|
|
hier->retained = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(source_id && leaf){
|
2014-09-17 20:53:39 +00:00
|
|
|
if(!leaf->context->id || (leaf->context->is_bridge && !strcmp(leaf->context->id, source_id))){
|
2014-05-07 22:27:00 +00:00
|
|
|
leaf = leaf->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Check for ACL topic access. */
|
|
|
|
rc2 = mosquitto_acl_check(db, leaf->context, topic, MOSQ_ACL_READ);
|
|
|
|
if(rc2 == MOSQ_ERR_ACL_DENIED){
|
|
|
|
leaf = leaf->next;
|
|
|
|
continue;
|
|
|
|
}else if(rc2 == MOSQ_ERR_SUCCESS){
|
|
|
|
client_qos = leaf->qos;
|
|
|
|
|
|
|
|
if(db->config->upgrade_outgoing_qos){
|
|
|
|
msg_qos = client_qos;
|
|
|
|
}else{
|
|
|
|
if(qos > client_qos){
|
|
|
|
msg_qos = client_qos;
|
|
|
|
}else{
|
|
|
|
msg_qos = qos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(msg_qos){
|
2015-04-19 21:10:59 +00:00
|
|
|
mid = mosquitto__mid_generate(leaf->context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
|
|
|
mid = 0;
|
|
|
|
}
|
|
|
|
if(leaf->context->is_bridge){
|
|
|
|
/* If we know the client is a bridge then we should set retain
|
|
|
|
* even if the message is fresh. If we don't do this, retained
|
|
|
|
* messages won't be propagated. */
|
|
|
|
client_retain = retain;
|
|
|
|
}else{
|
|
|
|
/* Client is not a bridge and this isn't a stale message so
|
|
|
|
* retain should be false. */
|
|
|
|
client_retain = false;
|
|
|
|
}
|
|
|
|
if(mqtt3_db_message_insert(db, leaf->context, mid, mosq_md_out, msg_qos, client_retain, stored) == 1) rc = 1;
|
|
|
|
}else{
|
2014-07-13 20:20:18 +00:00
|
|
|
return 1; /* Application error */
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
leaf = leaf->next;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static struct sub__token *sub__topic_append(struct sub__token **tail, struct sub__token **topics, char *topic)
|
2014-09-22 23:42:25 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct sub__token *new_topic;
|
2014-09-22 23:42:25 +00:00
|
|
|
|
|
|
|
if(!topic){
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
new_topic = mosquitto__malloc(sizeof(struct sub__token));
|
2014-09-22 23:42:25 +00:00
|
|
|
if(!new_topic){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
new_topic->next = NULL;
|
2015-04-04 20:15:27 +00:00
|
|
|
new_topic->topic_len = strlen(topic);
|
|
|
|
if(UHPA_ALLOC_TOPIC(new_topic) == 0){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(new_topic);
|
2015-04-04 20:15:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strncpy(UHPA_ACCESS_TOPIC(new_topic), topic, new_topic->topic_len+1);
|
2014-09-22 23:42:25 +00:00
|
|
|
|
|
|
|
if(*tail){
|
|
|
|
(*tail)->next = new_topic;
|
|
|
|
*tail = (*tail)->next;
|
|
|
|
}else{
|
|
|
|
*topics = new_topic;
|
|
|
|
*tail = new_topic;
|
|
|
|
}
|
|
|
|
return new_topic;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int sub__topic_tokenise(const char *subtopic, struct sub__token **topics)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct sub__token *new_topic, *tail = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
int len;
|
|
|
|
int start, stop, tlen;
|
|
|
|
int i;
|
2015-04-11 20:17:16 +00:00
|
|
|
mosquitto__topic_element_uhpa topic;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(subtopic);
|
|
|
|
assert(topics);
|
|
|
|
|
|
|
|
if(subtopic[0] != '$'){
|
2015-04-19 21:10:59 +00:00
|
|
|
new_topic = sub__topic_append(&tail, topics, "");
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!new_topic) goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(subtopic);
|
|
|
|
|
|
|
|
if(subtopic[0] == '/'){
|
2015-04-19 21:10:59 +00:00
|
|
|
new_topic = sub__topic_append(&tail, topics, "");
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!new_topic) goto cleanup;
|
|
|
|
|
|
|
|
start = 1;
|
|
|
|
}else{
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop = 0;
|
|
|
|
for(i=start; i<len+1; i++){
|
|
|
|
if(subtopic[i] == '/' || subtopic[i] == '\0'){
|
|
|
|
stop = i;
|
|
|
|
|
|
|
|
if(start != stop){
|
2014-11-17 21:58:53 +00:00
|
|
|
tlen = stop-start;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-04-04 20:15:27 +00:00
|
|
|
if(UHPA_ALLOC(topic, tlen+1) == 0) goto cleanup;
|
|
|
|
memcpy(UHPA_ACCESS(topic, tlen+1), &subtopic[start], tlen);
|
|
|
|
UHPA_ACCESS(topic, tlen+1)[tlen] = '\0';
|
2015-04-19 21:10:59 +00:00
|
|
|
new_topic = sub__topic_append(&tail, topics, UHPA_ACCESS(topic, tlen+1));
|
2015-04-04 20:15:27 +00:00
|
|
|
UHPA_FREE(topic, tlen+1);
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
2015-04-19 21:10:59 +00:00
|
|
|
new_topic = sub__topic_append(&tail, topics, "");
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2014-09-22 23:42:25 +00:00
|
|
|
if(!new_topic) goto cleanup;
|
2014-05-07 22:27:00 +00:00
|
|
|
start = i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
tail = *topics;
|
|
|
|
*topics = NULL;
|
|
|
|
while(tail){
|
2015-04-04 20:15:27 +00:00
|
|
|
UHPA_FREE_TOPIC(tail);
|
2014-05-07 22:27:00 +00:00
|
|
|
new_topic = tail->next;
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(tail);
|
2014-05-07 22:27:00 +00:00
|
|
|
tail = new_topic;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static void sub__topic_tokens_free(struct sub__token *tokens)
|
2014-09-06 20:51:59 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct sub__token *tail;
|
2014-09-06 20:51:59 +00:00
|
|
|
|
|
|
|
while(tokens){
|
|
|
|
tail = tokens->next;
|
2015-04-04 20:15:27 +00:00
|
|
|
UHPA_FREE_TOPIC(tokens);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(tokens);
|
2014-09-06 20:51:59 +00:00
|
|
|
tokens = tail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int sub__add(struct mosquitto_db *db, struct mosquitto *context, int qos, struct mosquitto__subhier *subhier, struct sub__token *tokens)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *branch, *last = NULL;
|
|
|
|
struct mosquitto__subleaf *leaf, *last_leaf;
|
|
|
|
struct mosquitto__subhier **subs;
|
2014-10-09 22:28:16 +00:00
|
|
|
int i;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(!tokens){
|
2014-10-16 22:06:47 +00:00
|
|
|
if(context && context->id){
|
2014-05-07 22:27:00 +00:00
|
|
|
leaf = subhier->subs;
|
|
|
|
last_leaf = NULL;
|
|
|
|
while(leaf){
|
2014-05-31 22:26:46 +00:00
|
|
|
if(leaf->context && leaf->context->id && !strcmp(leaf->context->id, context->id)){
|
|
|
|
/* Client making a second subscription to same topic. Only
|
|
|
|
* need to update QoS. Return -1 to indicate this to the
|
|
|
|
* calling function. */
|
|
|
|
leaf->qos = qos;
|
|
|
|
if(context->protocol == mosq_p_mqtt31){
|
|
|
|
return -1;
|
|
|
|
}else{
|
|
|
|
/* mqttv311 requires retained messages are resent on
|
|
|
|
* resubscribe. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
last_leaf = leaf;
|
|
|
|
leaf = leaf->next;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
leaf = mosquitto__malloc(sizeof(struct mosquitto__subleaf));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!leaf) return MOSQ_ERR_NOMEM;
|
|
|
|
leaf->next = NULL;
|
|
|
|
leaf->context = context;
|
|
|
|
leaf->qos = qos;
|
2014-10-09 22:28:16 +00:00
|
|
|
if(context->subs){
|
|
|
|
for(i=0; i<context->sub_count; i++){
|
|
|
|
if(!context->subs[i]){
|
|
|
|
context->subs[i] = subhier;
|
2014-10-16 21:35:05 +00:00
|
|
|
break;
|
2014-10-09 22:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i == context->sub_count){
|
|
|
|
context->sub_count++;
|
2015-04-19 21:10:59 +00:00
|
|
|
subs = mosquitto__realloc(context->subs, sizeof(struct mosquitto__subhier *)*context->sub_count);
|
2014-10-09 22:28:16 +00:00
|
|
|
if(!subs){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(leaf);
|
2014-10-09 22:28:16 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
context->subs = subs;
|
|
|
|
context->subs[context->sub_count-1] = subhier;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
context->sub_count = 1;
|
2015-04-19 21:10:59 +00:00
|
|
|
context->subs = mosquitto__malloc(sizeof(struct mosquitto__subhier *)*context->sub_count);
|
2014-10-09 22:28:16 +00:00
|
|
|
if(!context->subs){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(leaf);
|
2014-10-09 22:28:16 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
context->subs[0] = subhier;
|
|
|
|
}
|
2014-10-16 22:18:12 +00:00
|
|
|
if(last_leaf){
|
|
|
|
last_leaf->next = leaf;
|
|
|
|
leaf->prev = last_leaf;
|
|
|
|
}else{
|
|
|
|
subhier->subs = leaf;
|
|
|
|
leaf->prev = NULL;
|
|
|
|
}
|
2014-09-16 22:48:26 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-05-07 22:27:00 +00:00
|
|
|
db->subscription_count++;
|
2014-09-16 22:48:26 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
branch = subhier->children;
|
|
|
|
while(branch){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(branch), UHPA_ACCESS_TOPIC(tokens))){
|
2015-04-19 21:10:59 +00:00
|
|
|
return sub__add(db, context, qos, branch, tokens->next);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
last = branch;
|
|
|
|
branch = branch->next;
|
|
|
|
}
|
|
|
|
/* Not found */
|
2015-04-19 21:10:59 +00:00
|
|
|
branch = mosquitto__calloc(1, sizeof(struct mosquitto__subhier));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!branch) return MOSQ_ERR_NOMEM;
|
2015-04-04 20:15:27 +00:00
|
|
|
branch->topic_len = tokens->topic_len;
|
|
|
|
if(UHPA_ALLOC_TOPIC(branch) == 0){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(branch);
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
strncpy(UHPA_ACCESS_TOPIC(branch), UHPA_ACCESS_TOPIC(tokens), branch->topic_len+1);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!last){
|
|
|
|
subhier->children = branch;
|
|
|
|
}else{
|
|
|
|
last->next = branch;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
return sub__add(db, context, qos, branch, tokens->next);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int sub__remove(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto__subhier *subhier, struct sub__token *tokens)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *branch, *last = NULL;
|
|
|
|
struct mosquitto__subleaf *leaf;
|
2014-10-09 22:28:16 +00:00
|
|
|
int i;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(!tokens){
|
|
|
|
leaf = subhier->subs;
|
|
|
|
while(leaf){
|
|
|
|
if(leaf->context==context){
|
2014-09-16 22:48:26 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-05-07 22:27:00 +00:00
|
|
|
db->subscription_count--;
|
2014-09-16 22:48:26 +00:00
|
|
|
#endif
|
2014-05-07 22:27:00 +00:00
|
|
|
if(leaf->prev){
|
|
|
|
leaf->prev->next = leaf->next;
|
|
|
|
}else{
|
|
|
|
subhier->subs = leaf->next;
|
|
|
|
}
|
|
|
|
if(leaf->next){
|
|
|
|
leaf->next->prev = leaf->prev;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(leaf);
|
2014-10-09 22:28:16 +00:00
|
|
|
|
|
|
|
/* Remove the reference to the sub that the client is keeping.
|
|
|
|
* It would be nice to be able to use the reference directly,
|
|
|
|
* but that would involve keeping a copy of the topic string in
|
|
|
|
* each subleaf. Might be worth considering though. */
|
|
|
|
for(i=0; i<context->sub_count; i++){
|
|
|
|
if(context->subs[i] == subhier){
|
|
|
|
context->subs[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
leaf = leaf->next;
|
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
branch = subhier->children;
|
|
|
|
while(branch){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(branch), UHPA_ACCESS_TOPIC(tokens))){
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__remove(db, context, branch, tokens->next);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!branch->children && !branch->subs && !branch->retained){
|
|
|
|
if(last){
|
|
|
|
last->next = branch->next;
|
|
|
|
}else{
|
|
|
|
subhier->children = branch->next;
|
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
UHPA_FREE_TOPIC(branch);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(branch);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
last = branch;
|
|
|
|
branch = branch->next;
|
|
|
|
}
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static void sub__search(struct mosquitto_db *db, struct mosquitto__subhier *subhier, struct sub__token *tokens, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored, bool set_retain)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
/* FIXME - need to take into account source_id if the client is a bridge */
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *branch;
|
2014-05-07 22:27:00 +00:00
|
|
|
bool sr;
|
|
|
|
|
|
|
|
branch = subhier->children;
|
|
|
|
while(branch){
|
|
|
|
sr = set_retain;
|
|
|
|
|
2015-04-04 20:15:27 +00:00
|
|
|
if(tokens && UHPA_ACCESS_TOPIC(tokens)
|
|
|
|
&& (!strcmp(UHPA_ACCESS_TOPIC(branch), UHPA_ACCESS_TOPIC(tokens))
|
|
|
|
|| !strcmp(UHPA_ACCESS_TOPIC(branch), "+"))){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* The topic matches this subscription.
|
|
|
|
* Doesn't include # wildcards */
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(branch), "+")){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* Don't set a retained message where + is in the hierarchy. */
|
|
|
|
sr = false;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__search(db, branch, tokens->next, source_id, topic, qos, retain, stored, sr);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!tokens->next){
|
2015-04-19 21:10:59 +00:00
|
|
|
subs__process(db, branch, source_id, topic, qos, retain, stored, sr);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
}else if(!strcmp(UHPA_ACCESS_TOPIC(branch), "#") && !branch->children){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* The topic matches due to a # wildcard - process the
|
|
|
|
* subscriptions but *don't* return. Although this branch has ended
|
|
|
|
* there may still be other subscriptions to deal with.
|
|
|
|
*/
|
2015-04-19 21:10:59 +00:00
|
|
|
subs__process(db, branch, source_id, topic, qos, retain, stored, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
branch = branch->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
int mqtt3_sub_add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, struct mosquitto__subhier *root)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *subhier, *child;
|
|
|
|
struct sub__token *tokens = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(root);
|
|
|
|
assert(sub);
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
if(sub__topic_tokenise(sub, &tokens)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
subhier = root->children;
|
|
|
|
while(subhier){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(subhier), UHPA_ACCESS_TOPIC(tokens))){
|
2015-04-19 21:10:59 +00:00
|
|
|
rc = sub__add(db, context, qos, subhier, tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
subhier = subhier->next;
|
|
|
|
}
|
|
|
|
if(!subhier){
|
2015-04-19 21:10:59 +00:00
|
|
|
child = mosquitto__malloc(sizeof(struct mosquitto__subhier));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!child){
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__topic_tokens_free(tokens);
|
|
|
|
mosquitto__log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
child->topic_len = tokens->topic_len;
|
|
|
|
if(UHPA_ALLOC_TOPIC(child) == 0){
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__topic_tokens_free(tokens);
|
|
|
|
mosquitto__log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
2014-05-07 22:27:00 +00:00
|
|
|
return MOSQ_ERR_NOMEM;
|
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
strncpy(UHPA_ACCESS_TOPIC(child), UHPA_ACCESS_TOPIC(tokens), child->topic_len+1);
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
child->subs = NULL;
|
|
|
|
child->children = NULL;
|
|
|
|
child->retained = NULL;
|
2014-05-08 22:05:34 +00:00
|
|
|
if(db->subs.children){
|
|
|
|
child->next = db->subs.children;
|
|
|
|
}else{
|
|
|
|
child->next = NULL;
|
|
|
|
}
|
2014-05-07 22:27:00 +00:00
|
|
|
db->subs.children = child;
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
rc = sub__add(db, context, qos, child, tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__topic_tokens_free(tokens);
|
2014-09-06 20:51:59 +00:00
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
/* We aren't worried about -1 (already subscribed) return codes. */
|
|
|
|
if(rc == -1) rc = MOSQ_ERR_SUCCESS;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
int mqtt3_sub_remove(struct mosquitto_db *db, struct mosquitto *context, const char *sub, struct mosquitto__subhier *root)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *subhier;
|
|
|
|
struct sub__token *tokens = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(root);
|
|
|
|
assert(sub);
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
if(sub__topic_tokenise(sub, &tokens)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
subhier = root->children;
|
|
|
|
while(subhier){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(subhier), UHPA_ACCESS_TOPIC(tokens))){
|
2015-04-19 21:10:59 +00:00
|
|
|
rc = sub__remove(db, context, subhier, tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
subhier = subhier->next;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__topic_tokens_free(tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-11-20 21:13:21 +00:00
|
|
|
int mqtt3_db_messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store **stored)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *subhier;
|
|
|
|
struct sub__token *tokens = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(db);
|
|
|
|
assert(topic);
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
if(sub__topic_tokenise(topic, &tokens)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-11-20 21:13:21 +00:00
|
|
|
/* Protect this message until we have sent it to all
|
|
|
|
clients - this is required because websockets client calls
|
|
|
|
mqtt3_db_message_write(), which could remove the message if ref_count==0.
|
|
|
|
*/
|
|
|
|
(*stored)->ref_count++;
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
subhier = db->subs.children;
|
|
|
|
while(subhier){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(subhier), UHPA_ACCESS_TOPIC(tokens))){
|
2014-05-07 22:27:00 +00:00
|
|
|
if(retain){
|
|
|
|
/* We have a message that needs to be retained, so ensure that the subscription
|
|
|
|
* tree for its topic exists.
|
|
|
|
*/
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__add(db, NULL, 0, subhier, tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__search(db, subhier, tokens, source_id, topic, qos, retain, *stored, true);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
subhier = subhier->next;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
sub__topic_tokens_free(tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-11-20 21:13:21 +00:00
|
|
|
/* Remove our reference and free if needed. */
|
|
|
|
mosquitto__db_msg_store_deref(db, stored);
|
|
|
|
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-10-09 22:28:16 +00:00
|
|
|
/* Remove all subscriptions for a client.
|
|
|
|
*/
|
|
|
|
int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2014-10-09 22:28:16 +00:00
|
|
|
int i;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subleaf *leaf;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2014-10-09 22:28:16 +00:00
|
|
|
for(i=0; i<context->sub_count; i++){
|
2014-10-16 22:04:26 +00:00
|
|
|
if(context->subs[i] == NULL){
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-09 22:28:16 +00:00
|
|
|
leaf = context->subs[i]->subs;
|
|
|
|
while(leaf){
|
|
|
|
if(leaf->context==context){
|
2014-09-16 22:48:26 +00:00
|
|
|
#ifdef WITH_SYS_TREE
|
2014-10-09 22:28:16 +00:00
|
|
|
db->subscription_count--;
|
2014-09-16 22:48:26 +00:00
|
|
|
#endif
|
2014-10-09 22:28:16 +00:00
|
|
|
if(leaf->prev){
|
|
|
|
leaf->prev->next = leaf->next;
|
|
|
|
}else{
|
|
|
|
context->subs[i]->subs = leaf->next;
|
|
|
|
}
|
|
|
|
if(leaf->next){
|
|
|
|
leaf->next->prev = leaf->prev;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(leaf);
|
2014-10-09 22:28:16 +00:00
|
|
|
break;
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
leaf = leaf->next;
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(context->subs);
|
2014-10-09 22:28:16 +00:00
|
|
|
context->subs = NULL;
|
|
|
|
context->sub_count = 0;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
void mqtt3_sub_tree_print(struct mosquitto__subhier *root, int level)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int i;
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *branch;
|
|
|
|
struct mosquitto__subleaf *leaf;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
for(i=0; i<level*2; i++){
|
|
|
|
printf(" ");
|
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
printf("%s", UHPA_ACCESS_TOPIC(root));
|
2014-05-07 22:27:00 +00:00
|
|
|
leaf = root->subs;
|
|
|
|
while(leaf){
|
|
|
|
if(leaf->context){
|
|
|
|
printf(" (%s, %d)", leaf->context->id, leaf->qos);
|
|
|
|
}else{
|
|
|
|
printf(" (%s, %d)", "", leaf->qos);
|
|
|
|
}
|
|
|
|
leaf = leaf->next;
|
|
|
|
}
|
|
|
|
if(root->retained){
|
|
|
|
printf(" (r)");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
branch = root->children;
|
|
|
|
while(branch){
|
|
|
|
mqtt3_sub_tree_print(branch, level+1);
|
|
|
|
branch = branch->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int retain__process(struct mosquitto_db *db, struct mosquitto_msg_store *retained, struct mosquitto *context, const char *sub, int sub_qos)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int qos;
|
|
|
|
uint16_t mid;
|
|
|
|
|
2015-04-11 20:17:16 +00:00
|
|
|
rc = mosquitto_acl_check(db, context, retained->topic, MOSQ_ACL_READ);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc == MOSQ_ERR_ACL_DENIED){
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}else if(rc != MOSQ_ERR_SUCCESS){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-11-18 07:42:49 +00:00
|
|
|
qos = retained->qos;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(qos > sub_qos) qos = sub_qos;
|
|
|
|
if(qos > 0){
|
2015-04-19 21:10:59 +00:00
|
|
|
mid = mosquitto__mid_generate(context);
|
2014-05-07 22:27:00 +00:00
|
|
|
}else{
|
|
|
|
mid = 0;
|
|
|
|
}
|
|
|
|
return mqtt3_db_message_insert(db, context, mid, mosq_md_out, qos, true, retained);
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
static int retain__search(struct mosquitto_db *db, struct mosquitto__subhier *subhier, struct sub__token *tokens, struct mosquitto *context, const char *sub, int sub_qos, int level)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *branch;
|
2014-05-07 22:27:00 +00:00
|
|
|
int flag = 0;
|
|
|
|
|
|
|
|
branch = subhier->children;
|
|
|
|
while(branch){
|
|
|
|
/* Subscriptions with wildcards in aren't really valid topics to publish to
|
|
|
|
* so they can't have retained messages.
|
|
|
|
*/
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(tokens), "#") && !tokens->next){
|
2014-05-07 22:27:00 +00:00
|
|
|
/* Set flag to indicate that we should check for retained messages
|
|
|
|
* on "foo" when we are subscribing to e.g. "foo/#" and then exit
|
2015-04-19 21:10:59 +00:00
|
|
|
* this function and return to an earlier retain__search().
|
2014-05-07 22:27:00 +00:00
|
|
|
*/
|
|
|
|
flag = -1;
|
|
|
|
if(branch->retained){
|
2015-04-19 21:10:59 +00:00
|
|
|
retain__process(db, branch->retained, context, sub, sub_qos);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
if(branch->children){
|
2015-04-19 21:10:59 +00:00
|
|
|
retain__search(db, branch, tokens, context, sub, sub_qos, level+1);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-04-04 20:15:27 +00:00
|
|
|
}else if(strcmp(UHPA_ACCESS_TOPIC(branch), "+")
|
|
|
|
&& (!strcmp(UHPA_ACCESS_TOPIC(branch), UHPA_ACCESS_TOPIC(tokens))
|
|
|
|
|| !strcmp(UHPA_ACCESS_TOPIC(tokens), "+"))){
|
2014-05-07 22:27:00 +00:00
|
|
|
if(tokens->next){
|
2015-04-19 21:10:59 +00:00
|
|
|
if(retain__search(db, branch, tokens->next, context, sub, sub_qos, level+1) == -1
|
2015-04-04 20:15:27 +00:00
|
|
|
|| (!branch->next && tokens->next && !strcmp(UHPA_ACCESS_TOPIC(tokens->next), "#") && level>0)){
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(branch->retained){
|
2015-04-19 21:10:59 +00:00
|
|
|
retain__process(db, branch->retained, context, sub, sub_qos);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if(branch->retained){
|
2015-04-19 21:10:59 +00:00
|
|
|
retain__process(db, branch->retained, context, sub, sub_qos);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
branch = branch->next;
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mqtt3_retain_queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos)
|
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__subhier *subhier;
|
|
|
|
struct sub__token *tokens = NULL, *tail;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
assert(db);
|
|
|
|
assert(context);
|
|
|
|
assert(sub);
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
if(sub__topic_tokenise(sub, &tokens)) return 1;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
subhier = db->subs.children;
|
|
|
|
while(subhier){
|
2015-04-04 20:15:27 +00:00
|
|
|
if(!strcmp(UHPA_ACCESS_TOPIC(subhier), UHPA_ACCESS_TOPIC(tokens))){
|
2015-04-19 21:10:59 +00:00
|
|
|
retain__search(db, subhier, tokens, context, sub, sub_qos, 0);
|
2014-05-07 22:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
subhier = subhier->next;
|
|
|
|
}
|
|
|
|
while(tokens){
|
|
|
|
tail = tokens->next;
|
2015-04-04 20:15:27 +00:00
|
|
|
UHPA_FREE_TOPIC(tokens);
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(tokens);
|
2014-05-07 22:27:00 +00:00
|
|
|
tokens = tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|