Add print-ip-on-publish example plugin.

pull/2386/head
Roger A. Light 4 years ago
parent c862ffec8b
commit 869016b831

@ -55,3 +55,7 @@ verified the payload is the correct format before modifying it.
This plugin adds the text string "hello " to the beginning of each payload, so
with anything other than simple plain text messages it will corrupt the payload
contents.
## Examples / Print IP on publish
This is an **example** plugin that prints out client ID and IP address of any
client that publishes on a particular topic.

@ -8,3 +8,4 @@ add_subdirectory(connection-state)
add_subdirectory(delayed-auth)
add_subdirectory(force-retain)
add_subdirectory(payload-modification)
add_subdirectory(print-ip-on-publish)

@ -6,7 +6,8 @@ DIRS= \
delayed-auth \
force-retain \
message-timestamp \
payload-modification
payload-modification \
print-ip-on-publish
.PHONY : all binary check clean reallyclean test install uninstall

@ -0,0 +1,24 @@
add_library(mosquitto_print_ip_on_publish MODULE
mosquitto_print_ip_on_publish.c
)
target_include_directories(mosquitto_print_ip_on_publish 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_print_ip_on_publish PROPERTIES
PREFIX ""
POSITION_INDEPENDENT_CODE 1
)
if(WIN32)
target_link_libraries(mosquitto_print_ip_on_publish mosquitto)
endif()
# Don't install, these are example plugins only.
#install(TARGETS mosquitto_print_ip_on_publish RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")

@ -0,0 +1,28 @@
include ../../../config.mk
.PHONY : all binary check clean reallyclean test install uninstall
PLUGIN_NAME=mosquitto_print_ip_on_publish
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"

@ -0,0 +1,59 @@
/* Example plugin that prints out the client id and IP address of any clients
* that publish to a particular topic, defined in "my_topic". */
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker.h"
#include "mosquitto_plugin.h"
#include "mosquitto.h"
#include "mqtt_protocol.h"
static mosquitto_plugin_id_t *mosq_pid = NULL;
static char my_topic[] = "troublesome/topic";
static int message_callback(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_message *ed = event_data;
UNUSED(event);
UNUSED(userdata);
if(!strcmp(ed->topic, my_topic)){
printf("PUBLISH FROM %s on IP %s\n", mosquitto_client_id(ed->client), mosquitto_client_address(ed->client));
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_plugin_version(int supported_version_count, const int *supported_versions)
{
int i;
for(i=0; i<supported_version_count; i++){
if(supported_versions[i] == 5){
return 5;
}
}
return -1;
}
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;
return mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE, message_callback, NULL, NULL);
}
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count)
{
UNUSED(user_data);
UNUSED(opts);
UNUSED(opt_count);
return mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_MESSAGE, message_callback, NULL);
}

@ -0,0 +1,4 @@
listener 1883
allow_anonymous true
plugin ./mosquitto_print_ip_on_publish.so

@ -0,0 +1,3 @@
#!/bin/sh
../../../src/mosquitto -c test.conf -v
Loading…
Cancel
Save