mosquitto_broker.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. /*
  15. * File: mosquitto_broker.h
  16. *
  17. * This header contains functions for use by plugins.
  18. */
  19. #ifndef MOSQUITTO_BROKER_H
  20. #define MOSQUITTO_BROKER_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if defined(WIN32) && defined(mosquitto_EXPORTS)
  25. # define mosq_EXPORT __declspec(dllexport)
  26. #else
  27. # define mosq_EXPORT
  28. #endif
  29. #include <stdbool.h>
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #include <time.h>
  33. struct mosquitto;
  34. typedef struct mqtt5__property mosquitto_property;
  35. enum mosquitto_protocol {
  36. mp_mqtt,
  37. mp_mqttsn,
  38. mp_websockets
  39. };
  40. /* =========================================================================
  41. *
  42. * Section: Register callbacks.
  43. *
  44. * ========================================================================= */
  45. /* Callback events */
  46. enum mosquitto_plugin_event {
  47. MOSQ_EVT_RELOAD = 1,
  48. MOSQ_EVT_ACL_CHECK = 2,
  49. MOSQ_EVT_BASIC_AUTH = 3,
  50. MOSQ_EVT_EXT_AUTH_START = 4,
  51. MOSQ_EVT_EXT_AUTH_CONTINUE = 5,
  52. MOSQ_EVT_CONTROL = 6,
  53. MOSQ_EVT_MESSAGE = 7,
  54. MOSQ_EVT_PSK_KEY = 8,
  55. MOSQ_EVT_TICK = 9,
  56. MOSQ_EVT_DISCONNECT = 10,
  57. };
  58. /* Data for the MOSQ_EVT_RELOAD event */
  59. struct mosquitto_evt_reload {
  60. void *future;
  61. struct mosquitto_opt *options;
  62. int option_count;
  63. void *future2[4];
  64. };
  65. /* Data for the MOSQ_EVT_ACL_CHECK event */
  66. struct mosquitto_evt_acl_check {
  67. void *future;
  68. struct mosquitto *client;
  69. const char *topic;
  70. const void *payload;
  71. mosquitto_property *properties;
  72. int access;
  73. uint32_t payloadlen;
  74. uint8_t qos;
  75. bool retain;
  76. void *future2[4];
  77. };
  78. /* Data for the MOSQ_EVT_BASIC_AUTH event */
  79. struct mosquitto_evt_basic_auth {
  80. void *future;
  81. struct mosquitto *client;
  82. char *username;
  83. char *password;
  84. void *future2[4];
  85. };
  86. /* Data for the MOSQ_EVT_PSK_KEY event */
  87. struct mosquitto_evt_psk_key {
  88. void *future;
  89. struct mosquitto *client;
  90. const char *hint;
  91. const char *identity;
  92. char *key;
  93. int max_key_len;
  94. void *future2[4];
  95. };
  96. /* Data for the MOSQ_EVT_EXTENDED_AUTH event */
  97. struct mosquitto_evt_extended_auth {
  98. void *future;
  99. struct mosquitto *client;
  100. const void *data_in;
  101. void *data_out;
  102. uint16_t data_in_len;
  103. uint16_t data_out_len;
  104. const char *auth_method;
  105. void *future2[3];
  106. };
  107. /* Data for the MOSQ_EVT_CONTROL event */
  108. struct mosquitto_evt_control {
  109. void *future;
  110. struct mosquitto *client;
  111. const char *topic;
  112. const void *payload;
  113. const mosquitto_property *properties;
  114. char *reason_string;
  115. uint32_t payloadlen;
  116. uint8_t qos;
  117. uint8_t reason_code;
  118. bool retain;
  119. void *future2[4];
  120. };
  121. /* Data for the MOSQ_EVT_MESSAGE event */
  122. struct mosquitto_evt_message {
  123. void *future;
  124. struct mosquitto *client;
  125. char *topic;
  126. void *payload;
  127. mosquitto_property *properties;
  128. char *reason_string;
  129. uint32_t payloadlen;
  130. uint8_t qos;
  131. uint8_t reason_code;
  132. bool retain;
  133. void *future2[4];
  134. };
  135. /* Data for the MOSQ_EVT_TICK event */
  136. struct mosquitto_evt_tick {
  137. void *future;
  138. long now_ns;
  139. long next_ns;
  140. time_t now_s;
  141. time_t next_s;
  142. void *future2[4];
  143. };
  144. /* Data for the MOSQ_EVT_DISCONNECT event */
  145. struct mosquitto_evt_disconnect {
  146. void *future;
  147. struct mosquitto *client;
  148. int reason;
  149. void *future2[4];
  150. };
  151. /* Callback definition */
  152. typedef int (*MOSQ_FUNC_generic_callback)(int, void *, void *);
  153. typedef struct mosquitto_plugin_id_t mosquitto_plugin_id_t;
  154. /*
  155. * Function: mosquitto_callback_register
  156. *
  157. * Register a callback for an event.
  158. *
  159. * Parameters:
  160. * identifier - the plugin identifier, as provided by <mosquitto_plugin_init>.
  161. * event - the event to register a callback for. Can be one of:
  162. * * MOSQ_EVT_RELOAD
  163. * * MOSQ_EVT_ACL_CHECK
  164. * * MOSQ_EVT_BASIC_AUTH
  165. * * MOSQ_EVT_EXT_AUTH_START
  166. * * MOSQ_EVT_EXT_AUTH_CONTINUE
  167. * * MOSQ_EVT_CONTROL
  168. * * MOSQ_EVT_MESSAGE
  169. * * MOSQ_EVT_PSK_KEY
  170. * * MOSQ_EVT_TICK
  171. * * MOSQ_EVT_DISCONNECT
  172. * cb_func - the callback function
  173. * event_data - event specific data
  174. *
  175. * Returns:
  176. * MOSQ_ERR_SUCCESS - on success
  177. * MOSQ_ERR_INVAL - if cb_func is NULL
  178. * MOSQ_ERR_NOMEM - on out of memory
  179. * MOSQ_ERR_ALREADY_EXISTS - if cb_func has already been registered for this event
  180. * MOSQ_ERR_NOT_SUPPORTED - if the event is not supported
  181. */
  182. mosq_EXPORT int mosquitto_callback_register(
  183. mosquitto_plugin_id_t *identifier,
  184. int event,
  185. MOSQ_FUNC_generic_callback cb_func,
  186. const void *event_data,
  187. void *userdata);
  188. /*
  189. * Function: mosquitto_callback_unregister
  190. *
  191. * Unregister a previously registered callback function.
  192. *
  193. * Parameters:
  194. * identifier - the plugin identifier, as provided by <mosquitto_plugin_init>.
  195. * event - the event to register a callback for. Can be one of:
  196. * * MOSQ_EVT_RELOAD
  197. * * MOSQ_EVT_ACL_CHECK
  198. * * MOSQ_EVT_BASIC_AUTH
  199. * * MOSQ_EVT_EXT_AUTH_START
  200. * * MOSQ_EVT_EXT_AUTH_CONTINUE
  201. * * MOSQ_EVT_CONTROL
  202. * * MOSQ_EVT_MESSAGE
  203. * * MOSQ_EVT_PSK_KEY
  204. * * MOSQ_EVT_TICK
  205. * * MOSQ_EVT_DISCONNECT
  206. * cb_func - the callback function
  207. * event_data - event specific data
  208. *
  209. * Returns:
  210. * MOSQ_ERR_SUCCESS - on success
  211. * MOSQ_ERR_INVAL - if cb_func is NULL
  212. * MOSQ_ERR_NOT_FOUND - if cb_func was not registered for this event
  213. * MOSQ_ERR_NOT_SUPPORTED - if the event is not supported
  214. */
  215. mosq_EXPORT int mosquitto_callback_unregister(
  216. mosquitto_plugin_id_t *identifier,
  217. int event,
  218. MOSQ_FUNC_generic_callback cb_func,
  219. const void *event_data);
  220. /* =========================================================================
  221. *
  222. * Section: Memory allocation.
  223. *
  224. * Use these functions when allocating or freeing memory to have your memory
  225. * included in the memory tracking on the broker.
  226. *
  227. * ========================================================================= */
  228. /*
  229. * Function: mosquitto_calloc
  230. */
  231. mosq_EXPORT void *mosquitto_calloc(size_t nmemb, size_t size);
  232. /*
  233. * Function: mosquitto_free
  234. */
  235. mosq_EXPORT void mosquitto_free(void *mem);
  236. /*
  237. * Function: mosquitto_malloc
  238. */
  239. mosq_EXPORT void *mosquitto_malloc(size_t size);
  240. /*
  241. * Function: mosquitto_realloc
  242. */
  243. mosq_EXPORT void *mosquitto_realloc(void *ptr, size_t size);
  244. /*
  245. * Function: mosquitto_strdup
  246. */
  247. mosq_EXPORT char *mosquitto_strdup(const char *s);
  248. /* =========================================================================
  249. *
  250. * Section: Utility Functions
  251. *
  252. * Use these functions from within your plugin.
  253. *
  254. * ========================================================================= */
  255. /*
  256. * Function: mosquitto_log_printf
  257. *
  258. * Write a log message using the broker configured logging.
  259. *
  260. * Parameters:
  261. * level - Log message priority. Can currently be one of:
  262. *
  263. * * MOSQ_LOG_INFO
  264. * * MOSQ_LOG_NOTICE
  265. * * MOSQ_LOG_WARNING
  266. * * MOSQ_LOG_ERR
  267. * * MOSQ_LOG_DEBUG
  268. * * MOSQ_LOG_SUBSCRIBE (not recommended for use by plugins)
  269. * * MOSQ_LOG_UNSUBSCRIBE (not recommended for use by plugins)
  270. *
  271. * These values are defined in mosquitto.h.
  272. *
  273. * fmt, ... - printf style format and arguments.
  274. */
  275. mosq_EXPORT void mosquitto_log_printf(int level, const char *fmt, ...);
  276. /* =========================================================================
  277. *
  278. * Client Functions
  279. *
  280. * Use these functions to access client information.
  281. *
  282. * ========================================================================= */
  283. /*
  284. * Function: mosquitto_client_address
  285. *
  286. * Retrieve the IP address of the client as a string.
  287. */
  288. mosq_EXPORT const char *mosquitto_client_address(const struct mosquitto *client);
  289. /*
  290. * Function: mosquitto_client_clean_session
  291. *
  292. * Retrieve the clean session flag value for a client.
  293. */
  294. mosq_EXPORT bool mosquitto_client_clean_session(const struct mosquitto *client);
  295. /*
  296. * Function: mosquitto_client_id
  297. *
  298. * Retrieve the client id associated with a client.
  299. */
  300. mosq_EXPORT const char *mosquitto_client_id(const struct mosquitto *client);
  301. /*
  302. * Function: mosquitto_client_keepalive
  303. *
  304. * Retrieve the keepalive value for a client.
  305. */
  306. mosq_EXPORT int mosquitto_client_keepalive(const struct mosquitto *client);
  307. /*
  308. * Function: mosquitto_client_certificate
  309. *
  310. * If TLS support is enabled, return the certificate provided by a client as an
  311. * X509 pointer from openssl. If the client did not provide a certificate, then
  312. * NULL will be returned. This function will only ever return a non-NULL value
  313. * if the `require_certificate` option is set to true.
  314. *
  315. * When you have finished with the x509 pointer, it must be freed using
  316. * X509_free().
  317. *
  318. * If TLS is not supported, this function will always return NULL.
  319. */
  320. mosq_EXPORT void *mosquitto_client_certificate(const struct mosquitto *client);
  321. /*
  322. * Function: mosquitto_client_protocol
  323. *
  324. * Retrieve the protocol with which the client has connected. Can be one of:
  325. *
  326. * mp_mqtt (MQTT over TCP)
  327. * mp_mqttsn (MQTT-SN)
  328. * mp_websockets (MQTT over Websockets)
  329. */
  330. mosq_EXPORT int mosquitto_client_protocol(const struct mosquitto *client);
  331. /*
  332. * Function: mosquitto_client_protocol_version
  333. *
  334. * Retrieve the MQTT protocol version with which the client has connected. Can be one of:
  335. *
  336. * Returns:
  337. * 3 - for MQTT v3 / v3.1
  338. * 4 - for MQTT v3.1.1
  339. * 5 - for MQTT v5
  340. */
  341. mosq_EXPORT int mosquitto_client_protocol_version(const struct mosquitto *client);
  342. /*
  343. * Function: mosquitto_client_sub_count
  344. *
  345. * Retrieve the number of subscriptions that have been made by a client.
  346. */
  347. mosq_EXPORT int mosquitto_client_sub_count(const struct mosquitto *client);
  348. /*
  349. * Function: mosquitto_client_username
  350. *
  351. * Retrieve the username associated with a client.
  352. */
  353. mosq_EXPORT const char *mosquitto_client_username(const struct mosquitto *client);
  354. /* Function: mosquitto_set_username
  355. *
  356. * Set the username for a client.
  357. *
  358. * This removes and replaces the current username for a client and hence
  359. * updates its access.
  360. *
  361. * username can be NULL, in which case the client will become anonymous, but
  362. * must not be zero length.
  363. *
  364. * In the case of error, the client will be left with its original username.
  365. *
  366. * Returns:
  367. * MOSQ_ERR_SUCCESS - on success
  368. * MOSQ_ERR_INVAL - if client is NULL, or if username is zero length
  369. * MOSQ_ERR_NOMEM - on out of memory
  370. */
  371. mosq_EXPORT int mosquitto_set_username(struct mosquitto *client, const char *username);
  372. /* =========================================================================
  373. *
  374. * Section: Client control
  375. *
  376. * ========================================================================= */
  377. /* Function: mosquitto_kick_client_by_clientid
  378. *
  379. * Forcefully disconnect a client from the broker.
  380. *
  381. * If clientid != NULL, then the client with the matching client id is
  382. * disconnected from the broker.
  383. * If clientid == NULL, then all clients are disconnected from the broker.
  384. *
  385. * If with_will == true, then if the client has a Last Will and Testament
  386. * defined then this will be sent. If false, the LWT will not be sent.
  387. */
  388. mosq_EXPORT int mosquitto_kick_client_by_clientid(const char *clientid, bool with_will);
  389. /* Function: mosquitto_kick_client_by_username
  390. *
  391. * Forcefully disconnect a client from the broker.
  392. *
  393. * If username != NULL, then all clients with a matching username are kicked
  394. * from the broker.
  395. * If username == NULL, then all clients that do not have a username are
  396. * kicked.
  397. *
  398. * If with_will == true, then if the client has a Last Will and Testament
  399. * defined then this will be sent. If false, the LWT will not be sent.
  400. */
  401. mosq_EXPORT int mosquitto_kick_client_by_username(const char *username, bool with_will);
  402. /* =========================================================================
  403. *
  404. * Section: Publishing functions
  405. *
  406. * ========================================================================= */
  407. /* Function: mosquitto_broker_publish
  408. *
  409. * Publish a message from within a plugin.
  410. *
  411. * This function allows a plugin to publish a message. Messages published in
  412. * this way are treated as coming from the broker and so will not be passed to
  413. * `mosquitto_auth_acl_check(, MOSQ_ACL_WRITE, , )` for checking. Read access
  414. * will be enforced as normal for individual clients when they are due to
  415. * receive the message.
  416. *
  417. * It can be used to send messages to all clients that have a matching
  418. * subscription, or to a single client whether or not it has a matching
  419. * subscription.
  420. *
  421. * Parameters:
  422. * clientid - optional string. If set to NULL, the message is delivered to all
  423. * clients. If non-NULL, the message is delivered only to the
  424. * client with the corresponding client id. If the client id
  425. * specified is not connected, the message will be dropped.
  426. * topic - message topic
  427. * payloadlen - payload length in bytes. Can be 0 for an empty payload.
  428. * payload - payload bytes. If payloadlen > 0 this must not be NULL. Must
  429. * be allocated on the heap. Will be freed by mosquitto after use if the
  430. * function returns success.
  431. * qos - message QoS to use.
  432. * retain - should retain be set on the message. This does not apply if
  433. * clientid is non-NULL.
  434. * properties - MQTT v5 properties to attach to the message. If the function
  435. * returns success, then properties is owned by the broker and
  436. * will be freed at a later point.
  437. *
  438. * Returns:
  439. * MOSQ_ERR_SUCCESS - on success
  440. * MOSQ_ERR_INVAL - if topic is NULL, if payloadlen < 0, if payloadlen > 0
  441. * and payload is NULL, if qos is not 0, 1, or 2.
  442. * MOSQ_ERR_NOMEM - on out of memory
  443. */
  444. mosq_EXPORT int mosquitto_broker_publish(
  445. const char *clientid,
  446. const char *topic,
  447. int payloadlen,
  448. void *payload,
  449. int qos,
  450. bool retain,
  451. mosquitto_property *properties);
  452. /* Function: mosquitto_broker_publish_copy
  453. *
  454. * Publish a message from within a plugin.
  455. *
  456. * This function is identical to mosquitto_broker_publish, except that a copy
  457. * of `payload` is taken.
  458. *
  459. * Parameters:
  460. * clientid - optional string. If set to NULL, the message is delivered to all
  461. * clients. If non-NULL, the message is delivered only to the
  462. * client with the corresponding client id. If the client id
  463. * specified is not connected, the message will be dropped.
  464. * topic - message topic
  465. * payloadlen - payload length in bytes. Can be 0 for an empty payload.
  466. * payload - payload bytes. If payloadlen > 0 this must not be NULL.
  467. * Memory remains the property of the calling function.
  468. * qos - message QoS to use.
  469. * retain - should retain be set on the message. This does not apply if
  470. * clientid is non-NULL.
  471. * properties - MQTT v5 properties to attach to the message. If the function
  472. * returns success, then properties is owned by the broker and
  473. * will be freed at a later point.
  474. *
  475. * Returns:
  476. * MOSQ_ERR_SUCCESS - on success
  477. * MOSQ_ERR_INVAL - if topic is NULL, if payloadlen < 0, if payloadlen > 0
  478. * and payload is NULL, if qos is not 0, 1, or 2.
  479. * MOSQ_ERR_NOMEM - on out of memory
  480. */
  481. mosq_EXPORT int mosquitto_broker_publish_copy(
  482. const char *clientid,
  483. const char *topic,
  484. int payloadlen,
  485. const void *payload,
  486. int qos,
  487. bool retain,
  488. mosquitto_property *properties);
  489. #ifdef __cplusplus
  490. }
  491. #endif
  492. #endif