Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-01 08:54:12

0001 /*
0002 Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
0003 
0004 All rights reserved. This program and the accompanying materials
0005 are made available under the terms of the Eclipse Public License 2.0
0006 and Eclipse Distribution License v1.0 which accompany this distribution.
0007 
0008 The Eclipse Public License is available at
0009    https://www.eclipse.org/legal/epl-2.0/
0010 and the Eclipse Distribution License is available at
0011   http://www.eclipse.org/org/documents/edl-v10.php.
0012 
0013 SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
0014 
0015 Contributors:
0016    Roger Light - initial implementation and documentation.
0017 */
0018 
0019 /*
0020  * File: mosquitto_broker.h
0021  *
0022  * This header contains functions for use by plugins.
0023  */
0024 #ifndef MOSQUITTO_BROKER_H
0025 #define MOSQUITTO_BROKER_H
0026 
0027 #ifdef __cplusplus
0028 extern "C" {
0029 #endif
0030 
0031 #if defined(WIN32) && defined(mosquitto_EXPORTS)
0032 #   define mosq_EXPORT  __declspec(dllexport)
0033 #else
0034 #   define mosq_EXPORT
0035 #endif
0036 
0037 #include <stdbool.h>
0038 #include <stddef.h>
0039 #include <stdint.h>
0040 #include <time.h>
0041 
0042 struct mosquitto;
0043 typedef struct mqtt5__property mosquitto_property;
0044 
0045 enum mosquitto_protocol {
0046     mp_mqtt,
0047     mp_mqttsn,
0048     mp_websockets
0049 };
0050 
0051 /* =========================================================================
0052  *
0053  * Section: Register callbacks.
0054  *
0055  * ========================================================================= */
0056 
0057 /* Callback events */
0058 enum mosquitto_plugin_event {
0059     MOSQ_EVT_RELOAD = 1,
0060     MOSQ_EVT_ACL_CHECK = 2,
0061     MOSQ_EVT_BASIC_AUTH = 3,
0062     MOSQ_EVT_EXT_AUTH_START = 4,
0063     MOSQ_EVT_EXT_AUTH_CONTINUE = 5,
0064     MOSQ_EVT_CONTROL = 6,
0065     MOSQ_EVT_MESSAGE = 7,
0066     MOSQ_EVT_PSK_KEY = 8,
0067     MOSQ_EVT_TICK = 9,
0068     MOSQ_EVT_DISCONNECT = 10,
0069 };
0070 
0071 /* Data for the MOSQ_EVT_RELOAD event */
0072 struct mosquitto_evt_reload {
0073     void *future;
0074     struct mosquitto_opt *options;
0075     int option_count;
0076     void *future2[4];
0077 };
0078 
0079 /* Data for the MOSQ_EVT_ACL_CHECK event */
0080 struct mosquitto_evt_acl_check {
0081     void *future;
0082     struct mosquitto *client;
0083     const char *topic;
0084     const void *payload;
0085     mosquitto_property *properties;
0086     int access;
0087     uint32_t payloadlen;
0088     uint8_t qos;
0089     bool retain;
0090     void *future2[4];
0091 };
0092 
0093 /* Data for the MOSQ_EVT_BASIC_AUTH event */
0094 struct mosquitto_evt_basic_auth {
0095     void *future;
0096     struct mosquitto *client;
0097     char *username;
0098     char *password;
0099     void *future2[4];
0100 };
0101 
0102 /* Data for the MOSQ_EVT_PSK_KEY event */
0103 struct mosquitto_evt_psk_key {
0104     void *future;
0105     struct mosquitto *client;
0106     const char *hint;
0107     const char *identity;
0108     char *key;
0109     int max_key_len;
0110     void *future2[4];
0111 };
0112 
0113 /* Data for the MOSQ_EVT_EXTENDED_AUTH event */
0114 struct mosquitto_evt_extended_auth {
0115     void *future;
0116     struct mosquitto *client;
0117     const void *data_in;
0118     void *data_out;
0119     uint16_t data_in_len;
0120     uint16_t data_out_len;
0121     const char *auth_method;
0122     void *future2[3];
0123 };
0124 
0125 /* Data for the MOSQ_EVT_CONTROL event */
0126 struct mosquitto_evt_control {
0127     void *future;
0128     struct mosquitto *client;
0129     const char *topic;
0130     const void *payload;
0131     const mosquitto_property *properties;
0132     char *reason_string;
0133     uint32_t payloadlen;
0134     uint8_t qos;
0135     uint8_t reason_code;
0136     bool retain;
0137     void *future2[4];
0138 };
0139 
0140 /* Data for the MOSQ_EVT_MESSAGE event */
0141 struct mosquitto_evt_message {
0142     void *future;
0143     struct mosquitto *client;
0144     char *topic;
0145     void *payload;
0146     mosquitto_property *properties;
0147     char *reason_string;
0148     uint32_t payloadlen;
0149     uint8_t qos;
0150     uint8_t reason_code;
0151     bool retain;
0152     void *future2[4];
0153 };
0154 
0155 
0156 /* Data for the MOSQ_EVT_TICK event */
0157 struct mosquitto_evt_tick {
0158     void *future;
0159     long now_ns;
0160     long next_ns;
0161     time_t now_s;
0162     time_t next_s;
0163     void *future2[4];
0164 };
0165 
0166 /* Data for the MOSQ_EVT_DISCONNECT event */
0167 struct mosquitto_evt_disconnect {
0168     void *future;
0169     struct mosquitto *client;
0170     int reason;
0171     void *future2[4];
0172 };
0173 
0174 
0175 /* Callback definition */
0176 typedef int (*MOSQ_FUNC_generic_callback)(int, void *, void *);
0177 
0178 typedef struct mosquitto_plugin_id_t mosquitto_plugin_id_t;
0179 
0180 /*
0181  * Function: mosquitto_callback_register
0182  *
0183  * Register a callback for an event.
0184  *
0185  * Parameters:
0186  *  identifier - the plugin identifier, as provided by <mosquitto_plugin_init>.
0187  *  event - the event to register a callback for. Can be one of:
0188  *          * MOSQ_EVT_RELOAD
0189  *          * MOSQ_EVT_ACL_CHECK
0190  *          * MOSQ_EVT_BASIC_AUTH
0191  *          * MOSQ_EVT_EXT_AUTH_START
0192  *          * MOSQ_EVT_EXT_AUTH_CONTINUE
0193  *          * MOSQ_EVT_CONTROL
0194  *          * MOSQ_EVT_MESSAGE
0195  *          * MOSQ_EVT_PSK_KEY
0196  *          * MOSQ_EVT_TICK
0197  *          * MOSQ_EVT_DISCONNECT
0198  *  cb_func - the callback function
0199  *  event_data - event specific data
0200  *
0201  * Returns:
0202  *  MOSQ_ERR_SUCCESS - on success
0203  *  MOSQ_ERR_INVAL - if cb_func is NULL
0204  *  MOSQ_ERR_NOMEM - on out of memory
0205  *  MOSQ_ERR_ALREADY_EXISTS - if cb_func has already been registered for this event
0206  *  MOSQ_ERR_NOT_SUPPORTED - if the event is not supported
0207  */
0208 mosq_EXPORT int mosquitto_callback_register(
0209         mosquitto_plugin_id_t *identifier,
0210         int event,
0211         MOSQ_FUNC_generic_callback cb_func,
0212         const void *event_data,
0213         void *userdata);
0214 
0215 /*
0216  * Function: mosquitto_callback_unregister
0217  *
0218  * Unregister a previously registered callback function.
0219  *
0220  * Parameters:
0221  *  identifier - the plugin identifier, as provided by <mosquitto_plugin_init>.
0222  *  event - the event to register a callback for. Can be one of:
0223  *          * MOSQ_EVT_RELOAD
0224  *          * MOSQ_EVT_ACL_CHECK
0225  *          * MOSQ_EVT_BASIC_AUTH
0226  *          * MOSQ_EVT_EXT_AUTH_START
0227  *          * MOSQ_EVT_EXT_AUTH_CONTINUE
0228  *          * MOSQ_EVT_CONTROL
0229  *          * MOSQ_EVT_MESSAGE
0230  *          * MOSQ_EVT_PSK_KEY
0231  *          * MOSQ_EVT_TICK
0232  *          * MOSQ_EVT_DISCONNECT
0233  *  cb_func - the callback function
0234  *  event_data - event specific data
0235  *
0236  * Returns:
0237  *  MOSQ_ERR_SUCCESS - on success
0238  *  MOSQ_ERR_INVAL - if cb_func is NULL
0239  *  MOSQ_ERR_NOT_FOUND - if cb_func was not registered for this event
0240  *  MOSQ_ERR_NOT_SUPPORTED - if the event is not supported
0241  */
0242 mosq_EXPORT int mosquitto_callback_unregister(
0243         mosquitto_plugin_id_t *identifier,
0244         int event,
0245         MOSQ_FUNC_generic_callback cb_func,
0246         const void *event_data);
0247 
0248 
0249 /* =========================================================================
0250  *
0251  * Section: Memory allocation.
0252  *
0253  * Use these functions when allocating or freeing memory to have your memory
0254  * included in the memory tracking on the broker.
0255  *
0256  * ========================================================================= */
0257 
0258 /*
0259  * Function: mosquitto_calloc
0260  */
0261 mosq_EXPORT void *mosquitto_calloc(size_t nmemb, size_t size);
0262 
0263 /*
0264  * Function: mosquitto_free
0265  */
0266 mosq_EXPORT void mosquitto_free(void *mem);
0267 
0268 /*
0269  * Function: mosquitto_malloc
0270  */
0271 mosq_EXPORT void *mosquitto_malloc(size_t size);
0272 
0273 /*
0274  * Function: mosquitto_realloc
0275  */
0276 mosq_EXPORT void *mosquitto_realloc(void *ptr, size_t size);
0277 
0278 /*
0279  * Function: mosquitto_strdup
0280  */
0281 mosq_EXPORT char *mosquitto_strdup(const char *s);
0282 
0283 /* =========================================================================
0284  *
0285  * Section: Utility Functions
0286  *
0287  * Use these functions from within your plugin.
0288  *
0289  * ========================================================================= */
0290 
0291 
0292 /*
0293  * Function: mosquitto_log_printf
0294  *
0295  * Write a log message using the broker configured logging.
0296  *
0297  * Parameters:
0298  *  level -    Log message priority. Can currently be one of:
0299  *
0300  *             * MOSQ_LOG_INFO
0301  *             * MOSQ_LOG_NOTICE
0302  *             * MOSQ_LOG_WARNING
0303  *             * MOSQ_LOG_ERR
0304  *             * MOSQ_LOG_DEBUG
0305  *             * MOSQ_LOG_SUBSCRIBE (not recommended for use by plugins)
0306  *             * MOSQ_LOG_UNSUBSCRIBE (not recommended for use by plugins)
0307  *
0308  *             These values are defined in mosquitto.h.
0309  *
0310  *  fmt, ... - printf style format and arguments.
0311  */
0312 mosq_EXPORT void mosquitto_log_printf(int level, const char *fmt, ...);
0313 
0314 
0315 /* =========================================================================
0316  *
0317  * Client Functions
0318  *
0319  * Use these functions to access client information.
0320  *
0321  * ========================================================================= */
0322 
0323 /*
0324  * Function: mosquitto_client_address
0325  *
0326  * Retrieve the IP address of the client as a string.
0327  */
0328 mosq_EXPORT const char *mosquitto_client_address(const struct mosquitto *client);
0329 
0330 
0331 /*
0332  * Function: mosquitto_client_clean_session
0333  *
0334  * Retrieve the clean session flag value for a client.
0335  */
0336 mosq_EXPORT bool mosquitto_client_clean_session(const struct mosquitto *client);
0337 
0338 
0339 /*
0340  * Function: mosquitto_client_id
0341  *
0342  * Retrieve the client id associated with a client.
0343  */
0344 mosq_EXPORT const char *mosquitto_client_id(const struct mosquitto *client);
0345 
0346 
0347 /*
0348  * Function: mosquitto_client_keepalive
0349  *
0350  * Retrieve the keepalive value for a client.
0351  */
0352 mosq_EXPORT int mosquitto_client_keepalive(const struct mosquitto *client);
0353 
0354 
0355 /*
0356  * Function: mosquitto_client_certificate
0357  *
0358  * If TLS support is enabled, return the certificate provided by a client as an
0359  * X509 pointer from openssl. If the client did not provide a certificate, then
0360  * NULL will be returned. This function will only ever return a non-NULL value
0361  * if the `require_certificate` option is set to true.
0362  *
0363  * When you have finished with the x509 pointer, it must be freed using
0364  * X509_free().
0365  *
0366  * If TLS is not supported, this function will always return NULL.
0367  */
0368 mosq_EXPORT void *mosquitto_client_certificate(const struct mosquitto *client);
0369 
0370 
0371 /*
0372  * Function: mosquitto_client_protocol
0373  *
0374  * Retrieve the protocol with which the client has connected. Can be one of:
0375  *
0376  * mp_mqtt (MQTT over TCP)
0377  * mp_mqttsn (MQTT-SN)
0378  * mp_websockets (MQTT over Websockets)
0379  */
0380 mosq_EXPORT int mosquitto_client_protocol(const struct mosquitto *client);
0381 
0382 
0383 /*
0384  * Function: mosquitto_client_protocol_version
0385  *
0386  * Retrieve the MQTT protocol version with which the client has connected. Can be one of:
0387  *
0388  * Returns:
0389  *   3 - for MQTT v3 / v3.1
0390  *   4 - for MQTT v3.1.1
0391  *   5 - for MQTT v5
0392  */
0393 mosq_EXPORT int mosquitto_client_protocol_version(const struct mosquitto *client);
0394 
0395 
0396 /*
0397  * Function: mosquitto_client_sub_count
0398  *
0399  * Retrieve the number of subscriptions that have been made by a client.
0400  */
0401 mosq_EXPORT int mosquitto_client_sub_count(const struct mosquitto *client);
0402 
0403 
0404 /*
0405  * Function: mosquitto_client_username
0406  *
0407  * Retrieve the username associated with a client.
0408  */
0409 mosq_EXPORT const char *mosquitto_client_username(const struct mosquitto *client);
0410 
0411 
0412 /* Function: mosquitto_set_username
0413  *
0414  * Set the username for a client.
0415  *
0416  * This removes and replaces the current username for a client and hence
0417  * updates its access.
0418  *
0419  * username can be NULL, in which case the client will become anonymous, but
0420  * must not be zero length.
0421  *
0422  * In the case of error, the client will be left with its original username.
0423  *
0424  * Returns:
0425  *   MOSQ_ERR_SUCCESS - on success
0426  *   MOSQ_ERR_INVAL - if client is NULL, or if username is zero length
0427  *   MOSQ_ERR_NOMEM - on out of memory
0428  */
0429 mosq_EXPORT int mosquitto_set_username(struct mosquitto *client, const char *username);
0430 
0431 
0432 /* =========================================================================
0433  *
0434  * Section: Client control
0435  *
0436  * ========================================================================= */
0437 
0438 /* Function: mosquitto_kick_client_by_clientid
0439  *
0440  * Forcefully disconnect a client from the broker.
0441  *
0442  * If clientid != NULL, then the client with the matching client id is
0443  *   disconnected from the broker.
0444  * If clientid == NULL, then all clients are disconnected from the broker.
0445  *
0446  * If with_will == true, then if the client has a Last Will and Testament
0447  *   defined then this will be sent. If false, the LWT will not be sent.
0448  */
0449 mosq_EXPORT int mosquitto_kick_client_by_clientid(const char *clientid, bool with_will);
0450 
0451 /* Function: mosquitto_kick_client_by_username
0452  *
0453  * Forcefully disconnect a client from the broker.
0454  *
0455  * If username != NULL, then all clients with a matching username are kicked
0456  *   from the broker.
0457  * If username == NULL, then all clients that do not have a username are
0458  *   kicked.
0459  *
0460  * If with_will == true, then if the client has a Last Will and Testament
0461  *   defined then this will be sent. If false, the LWT will not be sent.
0462  */
0463 mosq_EXPORT int mosquitto_kick_client_by_username(const char *username, bool with_will);
0464 
0465 
0466 /* =========================================================================
0467  *
0468  * Section: Publishing functions
0469  *
0470  * ========================================================================= */
0471 
0472 /* Function: mosquitto_broker_publish
0473  *
0474  * Publish a message from within a plugin.
0475  *
0476  * This function allows a plugin to publish a message. Messages published in
0477  * this way are treated as coming from the broker and so will not be passed to
0478  * `mosquitto_auth_acl_check(, MOSQ_ACL_WRITE, , )` for checking. Read access
0479  * will be enforced as normal for individual clients when they are due to
0480  * receive the message.
0481  *
0482  * It can be used to send messages to all clients that have a matching
0483  * subscription, or to a single client whether or not it has a matching
0484  * subscription.
0485  *
0486  * Parameters:
0487  *  clientid -   optional string. If set to NULL, the message is delivered to all
0488  *               clients. If non-NULL, the message is delivered only to the
0489  *               client with the corresponding client id. If the client id
0490  *               specified is not connected, the message will be dropped.
0491  *  topic -      message topic
0492  *  payloadlen - payload length in bytes. Can be 0 for an empty payload.
0493  *  payload -    payload bytes. If payloadlen > 0 this must not be NULL. Must
0494  *               be allocated on the heap. Will be freed by mosquitto after use if the
0495  *               function returns success.
0496  *  qos -        message QoS to use.
0497  *  retain -     should retain be set on the message. This does not apply if
0498  *               clientid is non-NULL.
0499  *  properties - MQTT v5 properties to attach to the message. If the function
0500  *               returns success, then properties is owned by the broker and
0501  *               will be freed at a later point.
0502  *
0503  * Returns:
0504  *   MOSQ_ERR_SUCCESS - on success
0505  *   MOSQ_ERR_INVAL - if topic is NULL, if payloadlen < 0, if payloadlen > 0
0506  *                    and payload is NULL, if qos is not 0, 1, or 2.
0507  *   MOSQ_ERR_NOMEM - on out of memory
0508  */
0509 mosq_EXPORT int mosquitto_broker_publish(
0510         const char *clientid,
0511         const char *topic,
0512         int payloadlen,
0513         void *payload,
0514         int qos,
0515         bool retain,
0516         mosquitto_property *properties);
0517 
0518 
0519 /* Function: mosquitto_broker_publish_copy
0520  *
0521  * Publish a message from within a plugin.
0522  *
0523  * This function is identical to mosquitto_broker_publish, except that a copy
0524  * of `payload` is taken.
0525  *
0526  * Parameters:
0527  *  clientid -   optional string. If set to NULL, the message is delivered to all
0528  *               clients. If non-NULL, the message is delivered only to the
0529  *               client with the corresponding client id. If the client id
0530  *               specified is not connected, the message will be dropped.
0531  *  topic -      message topic
0532  *  payloadlen - payload length in bytes. Can be 0 for an empty payload.
0533  *  payload -    payload bytes. If payloadlen > 0 this must not be NULL.
0534  *               Memory remains the property of the calling function.
0535  *  qos -        message QoS to use.
0536  *  retain -     should retain be set on the message. This does not apply if
0537  *               clientid is non-NULL.
0538  *  properties - MQTT v5 properties to attach to the message. If the function
0539  *               returns success, then properties is owned by the broker and
0540  *               will be freed at a later point.
0541  *
0542  * Returns:
0543  *   MOSQ_ERR_SUCCESS - on success
0544  *   MOSQ_ERR_INVAL - if topic is NULL, if payloadlen < 0, if payloadlen > 0
0545  *                    and payload is NULL, if qos is not 0, 1, or 2.
0546  *   MOSQ_ERR_NOMEM - on out of memory
0547  */
0548 mosq_EXPORT int mosquitto_broker_publish_copy(
0549         const char *clientid,
0550         const char *topic,
0551         int payloadlen,
0552         const void *payload,
0553         int qos,
0554         bool retain,
0555         mosquitto_property *properties);
0556 
0557 #ifdef __cplusplus
0558 }
0559 #endif
0560 
0561 #endif