diff --git a/plugins/examples/CMakeLists.txt b/plugins/examples/CMakeLists.txt index b75b228b..751e5edb 100644 --- a/plugins/examples/CMakeLists.txt +++ b/plugins/examples/CMakeLists.txt @@ -6,4 +6,5 @@ add_subdirectory(auth-by-ip) add_subdirectory(client-properties) add_subdirectory(connection-state) add_subdirectory(delayed-auth) +add_subdirectory(force-retain) add_subdirectory(payload-modification) diff --git a/plugins/examples/Makefile b/plugins/examples/Makefile index 0a007958..bdb43214 100644 --- a/plugins/examples/Makefile +++ b/plugins/examples/Makefile @@ -4,6 +4,7 @@ DIRS= \ client-properties \ connection-state \ delayed-auth \ + force-retain \ message-timestamp \ payload-modification diff --git a/plugins/examples/force-retain/CMakeLists.txt b/plugins/examples/force-retain/CMakeLists.txt new file mode 100644 index 00000000..b11c3063 --- /dev/null +++ b/plugins/examples/force-retain/CMakeLists.txt @@ -0,0 +1,24 @@ +add_library(mosquitto_force_retain MODULE + mosquitto_force_retain.c +) + +target_include_directories(mosquitto_force_retain 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(mosquitto_force_retain PROPERTIES + PREFIX "" + POSITION_INDEPENDENT_CODE 1 +) +if(WIN32) + target_link_libraries(mosquitto_force_retain mosquitto) +endif() + +# Don't install, these are example plugins only. +#install(TARGETS mosquitto_force_retain RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}") diff --git a/plugins/examples/force-retain/Makefile b/plugins/examples/force-retain/Makefile new file mode 100644 index 00000000..ee130d23 --- /dev/null +++ b/plugins/examples/force-retain/Makefile @@ -0,0 +1,28 @@ +include ../../../config.mk + +.PHONY : all binary check clean reallyclean test install uninstall + +PLUGIN_NAME=mosquitto_force_retain +PLUGIN_CFLAGS+=-I../../../include -I../../../ + +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/force-retain/mosquitto_force_retain.c b/plugins/examples/force-retain/mosquitto_force_retain.c new file mode 100644 index 00000000..8ae01b8f --- /dev/null +++ b/plugins/examples/force-retain/mosquitto_force_retain.c @@ -0,0 +1,85 @@ +/* +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: + Roger Light - initial implementation and documentation. +*/ + +/* + * This is an *example* plugin which forces all messages processed by the + * broker to be treated as though they had the retained bit set. + * + * Compile with: + * gcc -I -fPIC -shared mosquitto_force_retain.c -o mosquitto_force_retain.so + * + * Use in config with: + * + * plugin /path/to/mosquitto_force_retain.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 UNUSED(A) (void)(A) + +static mosquitto_plugin_id_t *mosq_pid = NULL; + +static int callback_message(int event, void *event_data, void *userdata) +{ + struct mosquitto_evt_message *ed = event_data; + + UNUSED(event); + UNUSED(userdata); + + ed->retain = true; + + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) +{ + int i; + + for(i=0; i