mosquitto_broker.h

This header contains functions for use by plugins.

Summary
mosquitto_broker.hThis header contains functions for use by plugins.
Register callbacks.
Functions
mosquitto_callback_registerRegister a callback for an event.
mosquitto_callback_unregisterUnregister a previously registered callback function.
Memory allocation.Use these functions when allocating or freeing memory to have your memory included in the memory tracking on the broker.
Functions
mosquitto_calloc
mosquitto_free
mosquitto_malloc
mosquitto_realloc
mosquitto_strdup
Utility FunctionsUse these functions from within your plugin.
Functions
mosquitto_log_printfWrite a log message using the broker configured logging.
mosquitto_client_addressRetrieve the IP address of the client as a string.
mosquitto_client_clean_sessionRetrieve the clean session flag value for a client.
mosquitto_client_idRetrieve the client id associated with a client.
mosquitto_client_keepaliveRetrieve the keepalive value for a client.
mosquitto_client_certificateIf TLS support is enabled, return the certificate provided by a client as an X509 pointer from openssl.
mosquitto_client_protocolmp_mqtt (MQTT over TCP) mp_mqttsn (MQTT-SN) mp_websockets (MQTT over Websockets)
mosquitto_client_protocol_version
mosquitto_client_sub_countRetrieve the number of subscriptions that have been made by a client.
mosquitto_client_usernameRetrieve the username associated with a client.
mosquitto_set_usernameSet the username for a client.
Client control
Functions
mosquitto_kick_client_by_clientidForcefully disconnect a client from the broker.
mosquitto_kick_client_by_usernameForcefully disconnect a client from the broker.
Publishing functions
Functions
mosquitto_broker_publishPublish a message from within a plugin.
mosquitto_broker_publish_copyPublish a message from within a plugin.

Register callbacks.

Summary
Functions
mosquitto_callback_registerRegister a callback for an event.
mosquitto_callback_unregisterUnregister a previously registered callback function.

Functions

mosquitto_callback_register

mosq_EXPORT int mosquitto_callback_register(mosquitto_plugin_id_t *identifier,
int event,
MOSQ_FUNC_generic_callback cb_func,
const void *event_data,
void *userdata)

Register a callback for an event.

Parameters

identifierthe plugin identifier, as provided by mosquitto_plugin_init.
eventthe event to register a callback for.  Can be one of:
  • MOSQ_EVT_RELOAD
  • MOSQ_EVT_ACL_CHECK
  • MOSQ_EVT_BASIC_AUTH
  • MOSQ_EVT_EXT_AUTH_START
  • MOSQ_EVT_EXT_AUTH_CONTINUE
  • MOSQ_EVT_CONTROL
  • MOSQ_EVT_MESSAGE
  • MOSQ_EVT_PSK_KEY
  • MOSQ_EVT_TICK
  • MOSQ_EVT_DISCONNECT
cb_functhe callback function
event_dataevent specific data

Returns

MOSQ_ERR_SUCCESSon success
MOSQ_ERR_INVALif cb_func is NULL
MOSQ_ERR_NOMEMon out of memory
MOSQ_ERR_ALREADY_EXISTSif cb_func has already been registered for this event
MOSQ_ERR_NOT_SUPPORTEDif the event is not supported

mosquitto_callback_unregister

mosq_EXPORT int mosquitto_callback_unregister(
   mosquitto_plugin_id_t *identifier,
   int event,
   MOSQ_FUNC_generic_callback cb_func,
   const void *event_data
)

Unregister a previously registered callback function.

Parameters

identifierthe plugin identifier, as provided by mosquitto_plugin_init.
eventthe event to register a callback for.  Can be one of:
  • MOSQ_EVT_RELOAD
  • MOSQ_EVT_ACL_CHECK
  • MOSQ_EVT_BASIC_AUTH
  • MOSQ_EVT_EXT_AUTH_START
  • MOSQ_EVT_EXT_AUTH_CONTINUE
  • MOSQ_EVT_CONTROL
  • MOSQ_EVT_MESSAGE
  • MOSQ_EVT_PSK_KEY
  • MOSQ_EVT_TICK
  • MOSQ_EVT_DISCONNECT
cb_functhe callback function
event_dataevent specific data

Returns

MOSQ_ERR_SUCCESSon success
MOSQ_ERR_INVALif cb_func is NULL
MOSQ_ERR_NOT_FOUNDif cb_func was not registered for this event
MOSQ_ERR_NOT_SUPPORTEDif the event is not supported

Memory allocation.

Use these functions when allocating or freeing memory to have your memory included in the memory tracking on the broker.

Functions

mosquitto_calloc

mosq_EXPORT void *mosquitto_calloc(size_t nmemb,
size_t size)

mosquitto_free

mosq_EXPORT void mosquitto_free(void *mem)

mosquitto_malloc

mosq_EXPORT void *mosquitto_malloc(size_t size)

mosquitto_realloc

mosq_EXPORT void *mosquitto_realloc(void *ptr,
size_t size)

mosquitto_strdup

mosq_EXPORT char *mosquitto_strdup(const char *s)

Utility Functions

Use these functions from within your plugin.

Summary
Functions
mosquitto_log_printfWrite a log message using the broker configured logging.
mosquitto_client_addressRetrieve the IP address of the client as a string.
mosquitto_client_clean_sessionRetrieve the clean session flag value for a client.
mosquitto_client_idRetrieve the client id associated with a client.
mosquitto_client_keepaliveRetrieve the keepalive value for a client.
mosquitto_client_certificateIf TLS support is enabled, return the certificate provided by a client as an X509 pointer from openssl.
mosquitto_client_protocolmp_mqtt (MQTT over TCP) mp_mqttsn (MQTT-SN) mp_websockets (MQTT over Websockets)
mosquitto_client_protocol_version
mosquitto_client_sub_countRetrieve the number of subscriptions that have been made by a client.
mosquitto_client_usernameRetrieve the username associated with a client.
mosquitto_set_usernameSet the username for a client.

Functions

mosquitto_log_printf

mosq_EXPORT void mosquitto_log_printf(int level,
const char *fmt,
 ...)

Write a log message using the broker configured logging.

Parameters

levelLog message priority.  Can currently be one of:
  • MOSQ_LOG_INFO
  • MOSQ_LOG_NOTICE
  • MOSQ_LOG_WARNING
  • MOSQ_LOG_ERR
  • MOSQ_LOG_DEBUG
  • MOSQ_LOG_SUBSCRIBE (not recommended for use by plugins)
  • MOSQ_LOG_UNSUBSCRIBE (not recommended for use by plugins)

These values are defined in mosquitto.h.

fmt, ...printf style format and arguments.

mosquitto_client_address

mosq_EXPORT const char *mosquitto_client_address(
   const struct mosquitto *client
)

Retrieve the IP address of the client as a string.

mosquitto_client_clean_session

mosq_EXPORT bool mosquitto_client_clean_session(const struct mosquitto *client)

Retrieve the clean session flag value for a client.

mosquitto_client_id

mosq_EXPORT const char *mosquitto_client_id(const struct mosquitto *client)

Retrieve the client id associated with a client.

mosquitto_client_keepalive

mosq_EXPORT int mosquitto_client_keepalive(const struct mosquitto *client)

Retrieve the keepalive value for a client.

mosquitto_client_certificate

mosq_EXPORT void *mosquitto_client_certificate(const struct mosquitto *client)

If TLS support is enabled, return the certificate provided by a client as an X509 pointer from openssl.  If the client did not provide a certificate, then NULL will be returned.  This function will only ever return a non-NULL value if the `require_certificate` option is set to true.

When you have finished with the x509 pointer, it must be freed using X509_free().

If TLS is not supported, this function will always return NULL.

mosquitto_client_protocol

mosq_EXPORT int mosquitto_client_protocol(const struct mosquitto *client)

Retrieve the protocol with which the client has connected.  Can be one of

mp_mqtt (MQTT over TCP) mp_mqttsn (MQTT-SN) mp_websockets (MQTT over Websockets)

mosquitto_client_protocol_version

mosq_EXPORT int mosquitto_client_protocol_version(
   const struct mosquitto *client
)

Retrieve the MQTT protocol version with which the client has connected.  Can be one of

Returns

3for MQTT v3 / v3.1
4for MQTT v3.1.1
5for MQTT v5

mosquitto_client_sub_count

mosq_EXPORT int mosquitto_client_sub_count(const struct mosquitto *client)

Retrieve the number of subscriptions that have been made by a client.

mosquitto_client_username

mosq_EXPORT const char *mosquitto_client_username(
   const struct mosquitto *client
)

Retrieve the username associated with a client.

mosquitto_set_username

mosq_EXPORT int mosquitto_set_username(struct mosquitto *client,
const char *username)

Set the username for a client.

This removes and replaces the current username for a client and hence updates its access.

username can be NULL, in which case the client will become anonymous, but must not be zero length.

In the case of error, the client will be left with its original username.

Returns

MOSQ_ERR_SUCCESSon success
MOSQ_ERR_INVALif client is NULL, or if username is zero length
MOSQ_ERR_NOMEMon out of memory

Client control

Summary
Functions
mosquitto_kick_client_by_clientidForcefully disconnect a client from the broker.
mosquitto_kick_client_by_usernameForcefully disconnect a client from the broker.

Functions

mosquitto_kick_client_by_clientid

mosq_EXPORT int mosquitto_kick_client_by_clientid(const char *clientid,
bool with_will)

Forcefully disconnect a client from the broker.

If clientid != NULL, then the client with the matching client id is disconnected from the broker.  If clientid == NULL, then all clients are disconnected from the broker.

If with_will == true, then if the client has a Last Will and Testament defined then this will be sent.  If false, the LWT will not be sent.

mosquitto_kick_client_by_username

mosq_EXPORT int mosquitto_kick_client_by_username(const char *username,
bool with_will)

Forcefully disconnect a client from the broker.

If username != NULL, then all clients with a matching username are kicked from the broker.  If username == NULL, then all clients that do not have a username are kicked.

If with_will == true, then if the client has a Last Will and Testament defined then this will be sent.  If false, the LWT will not be sent.

Publishing functions

Summary
Functions
mosquitto_broker_publishPublish a message from within a plugin.
mosquitto_broker_publish_copyPublish a message from within a plugin.

Functions

mosquitto_broker_publish

mosq_EXPORT int mosquitto_broker_publish(const char *clientid,
const char *topic,
int payloadlen,
void *payload,
int qos,
bool retain,
mosquitto_property *properties)

Publish a message from within a plugin.

This function allows a plugin to publish a message.  Messages published in this way are treated as coming from the broker and so will not be passed to `mosquitto_auth_acl_check(, MOSQ_ACL_WRITE, , )` for checking.  Read access will be enforced as normal for individual clients when they are due to receive the message.

It can be used to send messages to all clients that have a matching subscription, or to a single client whether or not it has a matching subscription.

Parameters

clientidoptional string.  If set to NULL, the message is delivered to all clients.  If non-NULL, the message is delivered only to the client with the corresponding client id.  If the client id specified is not connected, the message will be dropped.
topicmessage topic
payloadlenpayload length in bytes.  Can be 0 for an empty payload.
payloadpayload bytes.  If payloadlen > 0 this must not be NULL.  Must be allocated on the heap.  Will be freed by mosquitto after use if the function returns success.
qosmessage QoS to use.
retainshould retain be set on the message.  This does not apply if clientid is non-NULL.
propertiesMQTT v5 properties to attach to the message.  If the function returns success, then properties is owned by the broker and will be freed at a later point.

Returns

MOSQ_ERR_SUCCESSon success
MOSQ_ERR_INVALif topic is NULL, if payloadlen < 0, if payloadlen > 0 and payload is NULL, if qos is not 0, 1, or 2.
MOSQ_ERR_NOMEMon out of memory

mosquitto_broker_publish_copy

mosq_EXPORT int mosquitto_broker_publish_copy(const char *clientid,
const char *topic,
int payloadlen,
const void *payload,
int qos,
bool retain,
mosquitto_property *properties)

Publish a message from within a plugin.

This function is identical to mosquitto_broker_publish, except that a copy of `payload` is taken.

Parameters

clientidoptional string.  If set to NULL, the message is delivered to all clients.  If non-NULL, the message is delivered only to the client with the corresponding client id.  If the client id specified is not connected, the message will be dropped.
topicmessage topic
payloadlenpayload length in bytes.  Can be 0 for an empty payload.
payloadpayload bytes.  If payloadlen > 0 this must not be NULL.  Memory remains the property of the calling function.
qosmessage QoS to use.
retainshould retain be set on the message.  This does not apply if clientid is non-NULL.
propertiesMQTT v5 properties to attach to the message.  If the function returns success, then properties is owned by the broker and will be freed at a later point.

Returns

MOSQ_ERR_SUCCESSon success
MOSQ_ERR_INVALif topic is NULL, if payloadlen < 0, if payloadlen > 0 and payload is NULL, if qos is not 0, 1, or 2.
MOSQ_ERR_NOMEMon out of memory
mosq_EXPORT int mosquitto_callback_register(mosquitto_plugin_id_t *identifier,
int event,
MOSQ_FUNC_generic_callback cb_func,
const void *event_data,
void *userdata)
Register a callback for an event.
mosq_EXPORT int mosquitto_callback_unregister(
   mosquitto_plugin_id_t *identifier,
   int event,
   MOSQ_FUNC_generic_callback cb_func,
   const void *event_data
)
Unregister a previously registered callback function.
mosq_EXPORT void *mosquitto_calloc(size_t nmemb,
size_t size)
mosq_EXPORT void mosquitto_free(void *mem)
mosq_EXPORT void *mosquitto_malloc(size_t size)
mosq_EXPORT void *mosquitto_realloc(void *ptr,
size_t size)
mosq_EXPORT char *mosquitto_strdup(const char *s)
mosq_EXPORT void mosquitto_log_printf(int level,
const char *fmt,
 ...)
Write a log message using the broker configured logging.
mosq_EXPORT const char *mosquitto_client_address(
   const struct mosquitto *client
)
Retrieve the IP address of the client as a string.
mosq_EXPORT bool mosquitto_client_clean_session(const struct mosquitto *client)
Retrieve the clean session flag value for a client.
mosq_EXPORT const char *mosquitto_client_id(const struct mosquitto *client)
Retrieve the client id associated with a client.
mosq_EXPORT int mosquitto_client_keepalive(const struct mosquitto *client)
Retrieve the keepalive value for a client.
mosq_EXPORT void *mosquitto_client_certificate(const struct mosquitto *client)
If TLS support is enabled, return the certificate provided by a client as an X509 pointer from openssl.
mosq_EXPORT int mosquitto_client_protocol(const struct mosquitto *client)
mp_mqtt (MQTT over TCP) mp_mqttsn (MQTT-SN) mp_websockets (MQTT over Websockets)
mosq_EXPORT int mosquitto_client_protocol_version(
   const struct mosquitto *client
)
mosq_EXPORT int mosquitto_client_sub_count(const struct mosquitto *client)
Retrieve the number of subscriptions that have been made by a client.
mosq_EXPORT const char *mosquitto_client_username(
   const struct mosquitto *client
)
Retrieve the username associated with a client.
mosq_EXPORT int mosquitto_set_username(struct mosquitto *client,
const char *username)
Set the username for a client.
mosq_EXPORT int mosquitto_kick_client_by_clientid(const char *clientid,
bool with_will)
Forcefully disconnect a client from the broker.
mosq_EXPORT int mosquitto_kick_client_by_username(const char *username,
bool with_will)
Forcefully disconnect a client from the broker.
mosq_EXPORT int mosquitto_broker_publish(const char *clientid,
const char *topic,
int payloadlen,
void *payload,
int qos,
bool retain,
mosquitto_property *properties)
Publish a message from within a plugin.
mosq_EXPORT int mosquitto_broker_publish_copy(const char *clientid,
const char *topic,
int payloadlen,
const void *payload,
int qos,
bool retain,
mosquitto_property *properties)
Publish a message from within a plugin.
mosq_plugin_EXPORT int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier,
void **userdata,
struct mosquitto_opt *options,
int option_count)
Called after the plugin has been loaded and mosquitto_plugin_version has been called.
Close