I'm trying to get the ID and Username of the client who published a message via MQTT.
As a first step, I wanted to implement a simple printf-plugin based on https://github.com/eclipse/mosquitto/blob/master/plugins/payload-modification/mosquitto_payload_modification.c
This is my current sandbox repository: https://github.com/dersimn/mosquitto-plugin-sandbox
Basically a plugin that listens on new MQTT messages (pseudo-code):
static int callback_message(int event, void *event_data, void *userdata) {
struct mosquitto_evt_message *ed = (mosquitto_evt_message*)event_data;
printf("by-struct id: %s\n", ed->client->id); // prints IP address
printf("by-struct username: %s\n", ed->client->username); // prints client id
printf("by-struct password: %s\n", ed->client->password); // prints username
// workaround: Find base by tria & error:
char **base = &ed->client->id;
printf("by-base address: %s\n", base[0]);
printf("by-base id: %s\n", base[1]);
printf("by-base username: %s\n", base[2]);
printf("by-base password: %s\n", base[3]);
printf("payload: '%.*s'\n", ed->payloadlen, (char *)ed->payload);
}
mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE, callback_message, NULL, NULL);
The struct mosquitto_evt_message refers to another struct: struct mosquitto *client which is however defined in an internal header mosquitto_internal.h which is constructed by many preprocessor ifdef conditions.
Currently my plugin outputs data from this struct kinda wrong:
by-struct id: 192.168.161.1
by-struct username: someClient
by-struct password: someUser
by-base address: 192.168.161.1
by-base id: someClient
by-base username: someUser
by-base password: somePassword
payload: 'someMessage'
This is obviously shifted in a weird way.. Is there any "best practice" to obtain the client ID and Username via the MOSQ_EVT_MESSAGE API?