diff --git a/plugins/examples/CMakeLists.txt b/plugins/examples/CMakeLists.txt index 7ad697b6..10f8a678 100644 --- a/plugins/examples/CMakeLists.txt +++ b/plugins/examples/CMakeLists.txt @@ -10,6 +10,7 @@ add_subdirectory(connection-state) add_subdirectory(delayed-auth) add_subdirectory(deny-protocol-version) add_subdirectory(force-retain) +add_subdirectory(limit-subscription-qos) add_subdirectory(payload-ban) add_subdirectory(payload-modification) add_subdirectory(payload-size-stats) diff --git a/plugins/examples/Makefile b/plugins/examples/Makefile index 25031227..dbc1aca0 100644 --- a/plugins/examples/Makefile +++ b/plugins/examples/Makefile @@ -8,6 +8,7 @@ DIRS= \ delayed-auth \ deny-protocol-version \ force-retain \ + limit-subscription-qos \ message-timestamp \ payload-ban \ payload-modification \ diff --git a/plugins/examples/limit-subscription-qos/CMakeLists.txt b/plugins/examples/limit-subscription-qos/CMakeLists.txt new file mode 100644 index 00000000..68abbb0c --- /dev/null +++ b/plugins/examples/limit-subscription-qos/CMakeLists.txt @@ -0,0 +1,26 @@ +set (PLUGIN_NAME mosquitto_limit_subscription_qos) + +add_library(${PLUGIN_NAME} MODULE + ${PLUGIN_NAME}.c +) + +target_include_directories(${PLUGIN_NAME} PRIVATE + "${OPENSSL_INCLUDE_DIR}" + "${STDBOOL_H_PATH}" + "${STDINT_H_PATH}" + "${mosquitto_SOURCE_DIR}" + "${mosquitto_SOURCE_DIR}/include" +) + +link_directories(${mosquitto_SOURCE_DIR}) + +set_target_properties(${PLUGIN_NAME} PROPERTIES + PREFIX "" + POSITION_INDEPENDENT_CODE 1 +) +if(WIN32) + target_link_libraries(${PLUGIN_NAME} mosquitto) +endif() + +# Don't install, these are example plugins only. +#install(TARGETS ${PLUGIN_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") diff --git a/plugins/examples/limit-subscription-qos/Makefile b/plugins/examples/limit-subscription-qos/Makefile new file mode 100644 index 00000000..06acef57 --- /dev/null +++ b/plugins/examples/limit-subscription-qos/Makefile @@ -0,0 +1,29 @@ +R=../../.. +include ${R}/config.mk + +.PHONY : all binary check clean reallyclean test install uninstall + +PLUGIN_NAME=mosquitto_limit_subscription_qos +PLUGIN_CFLAGS+=-I${R}/include -I${R}/ + +all : binary + +binary : ${PLUGIN_NAME}.so + +${PLUGIN_NAME}.so : ${PLUGIN_NAME}.c + $(CROSS_COMPILE)$(CC) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) $(PLUGIN_LDFLAGS) -fPIC -shared $< -o $@ + +reallyclean : clean +clean: + -rm -f *.o ${PLUGIN_NAME}.so *.gcda *.gcno + +check: test +test: + +install: ${PLUGIN_NAME}.so + # Don't install, these are examples only. + #$(INSTALL) -d "${DESTDIR}$(libdir)" + #$(INSTALL) ${STRIP_OPTS} ${PLUGIN_NAME}.so "${DESTDIR}${libdir}/${PLUGIN_NAME}.so" + +uninstall : + -rm -f "${DESTDIR}${libdir}/${PLUGIN_NAME}.so" diff --git a/plugins/examples/limit-subscription-qos/mosquitto_limit_subscription_qos.c b/plugins/examples/limit-subscription-qos/mosquitto_limit_subscription_qos.c new file mode 100644 index 00000000..ac5626b6 --- /dev/null +++ b/plugins/examples/limit-subscription-qos/mosquitto_limit_subscription_qos.c @@ -0,0 +1,71 @@ +/* +Copyright (c) 2021 Roger Light + +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: + Abilio Marques - initial implementation and documentation. +*/ + +/* + * This is an *example* plugin which limits all the subscriptions' QoS to 1. + * + * Compile with: + * gcc -I -fPIC -shared mosquitto_limit_subscription.c -o mosquitto_limit_subscription_qos.so + * + * Use in config with: + * + * plugin /path/to/mosquitto_limit_subscription_qos.so + * + * Note that this only works on Mosquitto 2.0 or later. + */ +#include +#include + +#include "mosquitto_broker.h" +#include "mosquitto_plugin.h" +#include "mosquitto.h" +#include "mqtt_protocol.h" + +#define PLUGIN_NAME "limit-subscription-qos" +#define PLUGIN_VERSION "1.0" + +#define UNUSED(A) (void)(A) + +MOSQUITTO_PLUGIN_DECLARE_VERSION(5); + +static mosquitto_plugin_id_t *mosq_pid = NULL; + +static int callback_subscribe(int event, void *event_data, void *userdata) +{ + struct mosquitto_evt_subscribe *ed = event_data; + + UNUSED(event); + UNUSED(userdata); + + if(ed->qos > 1){ + ed->qos = 1; + } + + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count) +{ + UNUSED(user_data); + UNUSED(opts); + UNUSED(opt_count); + + mosq_pid = identifier; + mosquitto_plugin_set_info(identifier, PLUGIN_NAME, PLUGIN_VERSION); + return mosquitto_callback_register(mosq_pid, MOSQ_EVT_SUBSCRIBE, callback_subscribe, NULL, NULL); +} diff --git a/plugins/examples/limit-subscription-qos/test.conf b/plugins/examples/limit-subscription-qos/test.conf new file mode 100644 index 00000000..d14f5aec --- /dev/null +++ b/plugins/examples/limit-subscription-qos/test.conf @@ -0,0 +1 @@ +plugin ./mosquitto_limit_subscription_qos.so diff --git a/plugins/examples/limit-subscription-qos/test.sh b/plugins/examples/limit-subscription-qos/test.sh new file mode 100755 index 00000000..587459f7 --- /dev/null +++ b/plugins/examples/limit-subscription-qos/test.sh @@ -0,0 +1 @@ +../../../src/mosquitto -c test.conf -v