Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] Tracking down the publisher of a message

Hi Andrew,

If you're running version 2.0.x and are comfortable compiling code
then you could compile the attached C file to use as a plugin. It
prints a log message whenever a client sends a message with zero
length payload. The message includes the client id and IP address and
goes wherever your logs go now. There are some hints on compiling and
configuring the plugin in the file. If you would like to give this a
try but would like actual useful instructions then please don't
hesitate to ask. Without knowing what system you are on I wouldn't
want to give you the steps here that could be wrong and confusing.

Regards,

Roger

On Thu, 11 Nov 2021 at 09:45, Andrew Holt <andrewtholt60@xxxxxxxxx> wrote:
>
> Hi,
>
> It's my own fault.
>
> On my (dev) network, somewhere, I have a client that is publishing a message with a NULL topic.
>
> What I would like to do is to find the IP address of the publisher.
>
> If there is some way to get the broker to log the source of a message that would be ideal, but I'm open to other suggestions.
>
> Thanks,
> Andrew
>
> P.S. It's my own fault.
> _______________________________________________
> mosquitto-dev mailing list
> mosquitto-dev@xxxxxxxxxxx
> To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev
/*
Copyright (c) 2020 Roger Light <roger@xxxxxxxxxx>

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.
*/

/*
 * Add an MQTT v5 user-property with key "timestamp" and value of timestamp in ISO-8601 format to all messages.
 *
 * Compile with:
 *   gcc -I<path to mosquitto-repo/include> -fPIC -shared print_null_ip.c -o print_null_ip.so
 *
 * Use in config with:
 *
 *   plugin /path/to/print_null_ip.so
 *
 * Note that this only works on Mosquitto 2.0 or later.
 */
#include "config.h"

#include <stdio.h>
#include <time.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 int callback_message(int event, void *event_data, void *userdata)
{
	struct mosquitto_evt_message *ed = event_data;

	UNUSED(event);
	UNUSED(userdata);

	if(ed->payloadlen == 0){
		mosquitto_log_printf(MOSQ_LOG_INFO, "NULL MESSAGE FROM %s : %s",
				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, callback_message, 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, callback_message, NULL);
}

Back to the top