Split plugin code to separate files.

pull/2386/head
Roger A. Light 4 years ago
parent 06dc2f41f8
commit a199878fb3

@ -40,7 +40,7 @@ set (MOSQ_SRCS
persist_read_v234.c persist_read_v5.c persist_read.c
persist_write_v5.c persist_write.c
persist.h
plugin.c plugin_public.c
plugin.c plugin_connect.c plugin_disconnect.c plugin_message.c plugin_public.c plugin_tick.c
property_broker.c
../lib/property_mosq.c ../lib/property_mosq.h
read_handle.c

@ -53,7 +53,11 @@ OBJS= mosquitto.o \
persist_write.o \
persist_write_v5.o \
plugin.o \
plugin_connect.o \
plugin_disconnect.o \
plugin_message.o \
plugin_public.o \
plugin_tick.o \
read_handle.o \
retain.o \
security.o \
@ -235,9 +239,21 @@ property_mosq.o : ../lib/property_mosq.c ../lib/property_mosq.h
plugin.o : plugin.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
plugin_connect.o : plugin_connect.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
plugin_disconnect.o : plugin_disconnect.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
plugin_message.o : plugin_message.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
plugin_public.o : plugin_public.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
plugin_tick.o : plugin_tick.c ../include/mosquitto_plugin.h mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@
read_handle.o : read_handle.c mosquitto_broker_internal.h
${CROSS_COMPILE}${CC} $(BROKER_CPPFLAGS) $(BROKER_CFLAGS) -c $< -o $@

@ -1,5 +1,5 @@
/*
Copyright (c) 2016-2020 Roger Light <roger@atchoo.org>
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
@ -19,15 +19,9 @@ Contributors:
#include "config.h"
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "mosquitto_broker.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "send_mosq.h"
#include "util_mosq.h"
#include "utlist.h"
#include "lib_load.h"
#include "will_mosq.h"
static bool check_callback_exists(struct mosquitto__callback *cb_base, MOSQ_FUNC_generic_callback cb_func)
@ -114,147 +108,6 @@ int plugin__load_v5(struct mosquitto__listener *listener, struct mosquitto__auth
}
static void plugin__handle_connect_single(struct mosquitto__security_options *opts, struct mosquitto *context)
{
struct mosquitto_evt_connect event_data;
struct mosquitto__callback *cb_base;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
DL_FOREACH(opts->plugin_callbacks.connect, cb_base){
cb_base->cb(MOSQ_EVT_CONNECT, &event_data, cb_base->userdata);
}
}
void plugin__handle_connect(struct mosquitto *context)
{
/* Global plugins */
plugin__handle_connect_single(&db.config->security_options, context);
/* Per listener plugins */
if(db.config->per_listener_settings && context->listener){
plugin__handle_connect_single(&context->listener->security_options, context);
}
}
static void plugin__handle_disconnect_single(struct mosquitto__security_options *opts, struct mosquitto *context, int reason)
{
struct mosquitto_evt_disconnect event_data;
struct mosquitto__callback *cb_base;
if(context->id == NULL) return;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
event_data.reason = reason;
DL_FOREACH(opts->plugin_callbacks.disconnect, cb_base){
cb_base->cb(MOSQ_EVT_DISCONNECT, &event_data, cb_base->userdata);
}
}
void plugin__handle_disconnect(struct mosquitto *context, int reason)
{
/* Global plugins */
plugin__handle_disconnect_single(&db.config->security_options, context, reason);
/* Per listener plugins */
if(db.config->per_listener_settings && context->listener){
plugin__handle_disconnect_single(&context->listener->security_options, context, reason);
}
}
static int plugin__handle_message_single(struct mosquitto__security_options *opts, struct mosquitto *context, struct mosquitto_msg_store *stored)
{
struct mosquitto_evt_message event_data;
struct mosquitto__callback *cb_base;
int rc = MOSQ_ERR_SUCCESS;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
event_data.topic = stored->topic;
event_data.payloadlen = stored->payloadlen;
event_data.payload = stored->payload;
event_data.qos = stored->qos;
event_data.retain = stored->retain;
event_data.properties = stored->properties;
DL_FOREACH(opts->plugin_callbacks.message, cb_base){
rc = cb_base->cb(MOSQ_EVT_MESSAGE, &event_data, cb_base->userdata);
if(rc != MOSQ_ERR_SUCCESS){
break;
}
}
stored->topic = event_data.topic;
if(stored->payload != event_data.payload){
mosquitto__free(stored->payload);
stored->payload = event_data.payload;
stored->payloadlen = event_data.payloadlen;
}
stored->retain = event_data.retain;
stored->properties = event_data.properties;
return rc;
}
int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored)
{
int rc = MOSQ_ERR_SUCCESS;
/* Global plugins */
rc = plugin__handle_message_single(&db.config->security_options,
context, stored);
if(rc) return rc;
if(db.config->per_listener_settings && context->listener){
rc = plugin__handle_message_single(&context->listener->security_options,
context, stored);
}
return rc;
}
static void plugin__handle_tick_single(struct mosquitto__security_options *opts)
{
struct mosquitto_evt_tick event_data;
struct mosquitto__callback *cb_base;
memset(&event_data, 0, sizeof(event_data));
DL_FOREACH(opts->plugin_callbacks.tick, cb_base){
mosquitto_time_ns(&event_data.now_s, &event_data.now_ns);
event_data.next_s = 0;
event_data.next_ms = 0;
cb_base->cb(MOSQ_EVT_TICK, &event_data, cb_base->userdata);
loop__update_next_event(event_data.next_s * 1000 + event_data.next_ms);
}
}
void plugin__handle_tick(void)
{
struct mosquitto__security_options *opts;
int i;
/* FIXME - set now_s and now_ns to avoid need for multiple time lookups */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
opts = &db.config->listeners[i].security_options;
if(opts && opts->plugin_callbacks.tick){
plugin__handle_tick_single(opts);
}
}
}else{
plugin__handle_tick_single(&db.config->security_options);
}
}
int mosquitto_callback_register(
mosquitto_plugin_id_t *identifier,
int event,
@ -478,28 +331,3 @@ int mosquitto_callback_unregister(
return remove_callback(identifier, event, cb_base, cb_func);
}
void mosquitto_complete_basic_auth(const char *client_id, int result)
{
struct mosquitto *context;
if(client_id == NULL) return;
HASH_FIND(hh_id, db.contexts_by_id_delayed_auth, client_id, strlen(client_id), context);
if(context){
HASH_DELETE(hh_id, db.contexts_by_id_delayed_auth, context);
if(result == MOSQ_ERR_SUCCESS){
connect__on_authorised(context, NULL, 0);
}else{
if(context->protocol == mosq_p_mqtt5){
send__connack(context, 0, MQTT_RC_NOT_AUTHORIZED, NULL);
}else{
send__connack(context, 0, CONNACK_REFUSED_NOT_AUTHORIZED, NULL);
}
context->clean_start = true;
context->session_expiry_interval = 0;
will__clear(context);
do_disconnect(context, MOSQ_ERR_AUTH);
}
}
}

@ -0,0 +1,48 @@
/*
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "utlist.h"
static void plugin__handle_connect_single(struct mosquitto__security_options *opts, struct mosquitto *context)
{
struct mosquitto_evt_connect event_data;
struct mosquitto__callback *cb_base;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
DL_FOREACH(opts->plugin_callbacks.connect, cb_base){
cb_base->cb(MOSQ_EVT_CONNECT, &event_data, cb_base->userdata);
}
}
void plugin__handle_connect(struct mosquitto *context)
{
/* Global plugins */
plugin__handle_connect_single(&db.config->security_options, context);
/* Per listener plugins */
if(db.config->per_listener_settings && context->listener){
plugin__handle_connect_single(&context->listener->security_options, context);
}
}

@ -0,0 +1,50 @@
/*
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include "mosquitto_broker_internal.h"
#include "utlist.h"
static void plugin__handle_disconnect_single(struct mosquitto__security_options *opts, struct mosquitto *context, int reason)
{
struct mosquitto_evt_disconnect event_data;
struct mosquitto__callback *cb_base;
if(context->id == NULL) return;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
event_data.reason = reason;
DL_FOREACH(opts->plugin_callbacks.disconnect, cb_base){
cb_base->cb(MOSQ_EVT_DISCONNECT, &event_data, cb_base->userdata);
}
}
void plugin__handle_disconnect(struct mosquitto *context, int reason)
{
/* Global plugins */
plugin__handle_disconnect_single(&db.config->security_options, context, reason);
/* Per listener plugins */
if(db.config->per_listener_settings && context->listener){
plugin__handle_disconnect_single(&context->listener->security_options, context, reason);
}
}

@ -0,0 +1,75 @@
/*
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "utlist.h"
static int plugin__handle_message_single(struct mosquitto__security_options *opts, struct mosquitto *context, struct mosquitto_msg_store *stored)
{
struct mosquitto_evt_message event_data;
struct mosquitto__callback *cb_base;
int rc = MOSQ_ERR_SUCCESS;
memset(&event_data, 0, sizeof(event_data));
event_data.client = context;
event_data.topic = stored->topic;
event_data.payloadlen = stored->payloadlen;
event_data.payload = stored->payload;
event_data.qos = stored->qos;
event_data.retain = stored->retain;
event_data.properties = stored->properties;
DL_FOREACH(opts->plugin_callbacks.message, cb_base){
rc = cb_base->cb(MOSQ_EVT_MESSAGE, &event_data, cb_base->userdata);
if(rc != MOSQ_ERR_SUCCESS){
break;
}
}
stored->topic = event_data.topic;
if(stored->payload != event_data.payload){
mosquitto__free(stored->payload);
stored->payload = event_data.payload;
stored->payloadlen = event_data.payloadlen;
}
stored->retain = event_data.retain;
stored->properties = event_data.properties;
return rc;
}
int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored)
{
int rc = MOSQ_ERR_SUCCESS;
/* Global plugins */
rc = plugin__handle_message_single(&db.config->security_options,
context, stored);
if(rc) return rc;
if(db.config->per_listener_settings && context->listener){
rc = plugin__handle_message_single(&context->listener->security_options,
context, stored);
}
return rc;
}

@ -1,5 +1,5 @@
/*
Copyright (c) 2016-2020 Roger Light <roger@atchoo.org>
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
@ -19,13 +19,12 @@ Contributors:
#include "config.h"
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "mosquitto_broker.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "send_mosq.h"
#include "util_mosq.h"
#include "utlist.h"
#include "will_mosq.h"
#ifdef WITH_TLS
# include <openssl/ssl.h>
@ -380,3 +379,29 @@ int mosquitto_kick_client_by_username(const char *username, bool with_will)
}
return MOSQ_ERR_SUCCESS;
}
void mosquitto_complete_basic_auth(const char *client_id, int result)
{
struct mosquitto *context;
if(client_id == NULL) return;
HASH_FIND(hh_id, db.contexts_by_id_delayed_auth, client_id, strlen(client_id), context);
if(context){
HASH_DELETE(hh_id, db.contexts_by_id_delayed_auth, context);
if(result == MOSQ_ERR_SUCCESS){
connect__on_authorised(context, NULL, 0);
}else{
if(context->protocol == mosq_p_mqtt5){
send__connack(context, 0, MQTT_RC_NOT_AUTHORIZED, NULL);
}else{
send__connack(context, 0, CONNACK_REFUSED_NOT_AUTHORIZED, NULL);
}
context->clean_start = true;
context->session_expiry_interval = 0;
will__clear(context);
do_disconnect(context, MOSQ_ERR_AUTH);
}
}
}

@ -0,0 +1,62 @@
/*
Copyright (c) 2016-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "mosquitto_broker.h"
#include "time_mosq.h"
#include "utlist.h"
static void plugin__handle_tick_single(struct mosquitto__security_options *opts)
{
struct mosquitto_evt_tick event_data;
struct mosquitto__callback *cb_base;
memset(&event_data, 0, sizeof(event_data));
DL_FOREACH(opts->plugin_callbacks.tick, cb_base){
mosquitto_time_ns(&event_data.now_s, &event_data.now_ns);
event_data.next_s = 0;
event_data.next_ms = 0;
cb_base->cb(MOSQ_EVT_TICK, &event_data, cb_base->userdata);
loop__update_next_event(event_data.next_s * 1000 + event_data.next_ms);
}
}
void plugin__handle_tick(void)
{
struct mosquitto__security_options *opts;
int i;
/* FIXME - set now_s and now_ns to avoid need for multiple time lookups */
if(db.config->per_listener_settings){
for(i=0; i<db.config->listener_count; i++){
opts = &db.config->listeners[i].security_options;
if(opts && opts->plugin_callbacks.tick){
plugin__handle_tick_single(opts);
}
}
}else{
plugin__handle_tick_single(&db.config->security_options);
}
}
Loading…
Cancel
Save