1 /* 2 Copyright (c) 2009-2020 Roger Light <roger@atchoo.org> 3 4 All rights reserved. This program and the accompanying materials 5 are made available under the terms of the Eclipse Public License 2.0 6 and Eclipse Distribution License v1.0 which accompany this distribution. 7 8 The Eclipse Public License is available at 9 https://www.eclipse.org/legal/epl-2.0/ 10 and the Eclipse Distribution License is available at 11 http://www.eclipse.org/org/documents/edl-v10.php. 12 13 SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause 14 15 Contributors: 16 Roger Light - initial implementation and documentation. 17 Tatsuzo Osawa - Add epoll. 18 */ 19 20 #ifndef MOSQUITTO_BROKER_INTERNAL_H 21 #define MOSQUITTO_BROKER_INTERNAL_H 22 23 #include "config.h" 24 #include <stdio.h> 25 26 #ifdef WITH_WEBSOCKETS 27 # include <libwebsockets.h> 28 # if LWS_LIBRARY_VERSION_NUMBER >= 3002000 && !defined(LWS_WITH_EXTERNAL_POLL) 29 # warning "libwebsockets is not compiled with LWS_WITH_EXTERNAL_POLL support. Websocket performance will be unusable." 30 # endif 31 #endif 32 33 #include "mosquitto_internal.h" 34 #include "mosquitto_broker.h" 35 #include "mosquitto_plugin.h" 36 #include "mosquitto.h" 37 #include "logging_mosq.h" 38 #include "password_mosq.h" 39 #include "tls_mosq.h" 40 #include "uthash.h" 41 42 #ifndef __GNUC__ 43 #define __attribute__(attrib) 44 #endif 45 46 /* Log destinations */ 47 #define MQTT3_LOG_NONE 0x00 48 #define MQTT3_LOG_SYSLOG 0x01 49 #define MQTT3_LOG_FILE 0x02 50 #define MQTT3_LOG_STDOUT 0x04 51 #define MQTT3_LOG_STDERR 0x08 52 #define MQTT3_LOG_TOPIC 0x10 53 #define MQTT3_LOG_DLT 0x20 54 #define MQTT3_LOG_ALL 0xFF 55 56 #define WEBSOCKET_CLIENT -2 57 58 #define CMD_PORT_LIMIT 10 59 #define TOPIC_HIERARCHY_LIMIT 200 60 61 typedef uint64_t dbid_t; 62 63 typedef int (*FUNC_plugin_init_v5)(mosquitto_plugin_id_t *, void **, struct mosquitto_opt *, int); 64 typedef int (*FUNC_plugin_cleanup_v5)(void *, struct mosquitto_opt *, int); 65 66 typedef int (*FUNC_auth_plugin_init_v4)(void **, struct mosquitto_opt *, int); 67 typedef int (*FUNC_auth_plugin_cleanup_v4)(void *, struct mosquitto_opt *, int); 68 typedef int (*FUNC_auth_plugin_security_init_v4)(void *, struct mosquitto_opt *, int, bool); 69 typedef int (*FUNC_auth_plugin_security_cleanup_v4)(void *, struct mosquitto_opt *, int, bool); 70 typedef int (*FUNC_auth_plugin_acl_check_v4)(void *, int, struct mosquitto *, struct mosquitto_acl_msg *); 71 typedef int (*FUNC_auth_plugin_unpwd_check_v4)(void *, struct mosquitto *, const char *, const char *); 72 typedef int (*FUNC_auth_plugin_psk_key_get_v4)(void *, struct mosquitto *, const char *, const char *, char *, int); 73 typedef int (*FUNC_auth_plugin_auth_start_v4)(void *, struct mosquitto *, const char *, bool, const void *, uint16_t, void **, uint16_t *); 74 typedef int (*FUNC_auth_plugin_auth_continue_v4)(void *, struct mosquitto *, const char *, const void *, uint16_t, void **, uint16_t *); 75 76 typedef int (*FUNC_auth_plugin_init_v3)(void **, struct mosquitto_opt *, int); 77 typedef int (*FUNC_auth_plugin_cleanup_v3)(void *, struct mosquitto_opt *, int); 78 typedef int (*FUNC_auth_plugin_security_init_v3)(void *, struct mosquitto_opt *, int, bool); 79 typedef int (*FUNC_auth_plugin_security_cleanup_v3)(void *, struct mosquitto_opt *, int, bool); 80 typedef int (*FUNC_auth_plugin_acl_check_v3)(void *, int, const struct mosquitto *, struct mosquitto_acl_msg *); 81 typedef int (*FUNC_auth_plugin_unpwd_check_v3)(void *, const struct mosquitto *, const char *, const char *); 82 typedef int (*FUNC_auth_plugin_psk_key_get_v3)(void *, const struct mosquitto *, const char *, const char *, char *, int); 83 84 typedef int (*FUNC_auth_plugin_init_v2)(void **, struct mosquitto_auth_opt *, int); 85 typedef int (*FUNC_auth_plugin_cleanup_v2)(void *, struct mosquitto_auth_opt *, int); 86 typedef int (*FUNC_auth_plugin_security_init_v2)(void *, struct mosquitto_auth_opt *, int, bool); 87 typedef int (*FUNC_auth_plugin_security_cleanup_v2)(void *, struct mosquitto_auth_opt *, int, bool); 88 typedef int (*FUNC_auth_plugin_acl_check_v2)(void *, const char *, const char *, const char *, int); 89 typedef int (*FUNC_auth_plugin_unpwd_check_v2)(void *, const char *, const char *); 90 typedef int (*FUNC_auth_plugin_psk_key_get_v2)(void *, const char *, const char *, char *, int); 91 92 93 enum mosquitto_msg_origin{ 94 mosq_mo_client = 0, 95 mosq_mo_broker = 1 96 }; 97 98 struct mosquitto__auth_plugin{ 99 void *lib; 100 void *user_data; 101 int (*plugin_version)(void); 102 struct mosquitto_plugin_id_t *identifier; 103 104 FUNC_plugin_init_v5 plugin_init_v5; 105 FUNC_plugin_cleanup_v5 plugin_cleanup_v5; 106 107 FUNC_auth_plugin_init_v4 plugin_init_v4; 108 FUNC_auth_plugin_cleanup_v4 plugin_cleanup_v4; 109 FUNC_auth_plugin_security_init_v4 security_init_v4; 110 FUNC_auth_plugin_security_cleanup_v4 security_cleanup_v4; 111 FUNC_auth_plugin_acl_check_v4 acl_check_v4; 112 FUNC_auth_plugin_unpwd_check_v4 unpwd_check_v4; 113 FUNC_auth_plugin_psk_key_get_v4 psk_key_get_v4; 114 FUNC_auth_plugin_auth_start_v4 auth_start_v4; 115 FUNC_auth_plugin_auth_continue_v4 auth_continue_v4; 116 117 FUNC_auth_plugin_init_v3 plugin_init_v3; 118 FUNC_auth_plugin_cleanup_v3 plugin_cleanup_v3; 119 FUNC_auth_plugin_security_init_v3 security_init_v3; 120 FUNC_auth_plugin_security_cleanup_v3 security_cleanup_v3; 121 FUNC_auth_plugin_acl_check_v3 acl_check_v3; 122 FUNC_auth_plugin_unpwd_check_v3 unpwd_check_v3; 123 FUNC_auth_plugin_psk_key_get_v3 psk_key_get_v3; 124 125 FUNC_auth_plugin_init_v2 plugin_init_v2; 126 FUNC_auth_plugin_cleanup_v2 plugin_cleanup_v2; 127 FUNC_auth_plugin_security_init_v2 security_init_v2; 128 FUNC_auth_plugin_security_cleanup_v2 security_cleanup_v2; 129 FUNC_auth_plugin_acl_check_v2 acl_check_v2; 130 FUNC_auth_plugin_unpwd_check_v2 unpwd_check_v2; 131 FUNC_auth_plugin_psk_key_get_v2 psk_key_get_v2; 132 int version; 133 }; 134 135 struct mosquitto__auth_plugin_config 136 { 137 char *path; 138 struct mosquitto_opt *options; 139 int option_count; 140 bool deny_special_chars; 141 142 struct mosquitto__auth_plugin plugin; 143 }; 144 145 struct mosquitto__callback{ 146 UT_hash_handle hh; /* For callbacks that register for e.g. a specific topic */ 147 struct mosquitto__callback *next, *prev; /* For typical callbacks */ 148 MOSQ_FUNC_generic_callback cb; 149 void *userdata; 150 char *data; /* e.g. topic for control event */ 151 }; 152 153 struct plugin__callbacks{ 154 struct mosquitto__callback *tick; 155 struct mosquitto__callback *acl_check; 156 struct mosquitto__callback *basic_auth; 157 struct mosquitto__callback *control; 158 struct mosquitto__callback *disconnect; 159 struct mosquitto__callback *ext_auth_continue; 160 struct mosquitto__callback *ext_auth_start; 161 struct mosquitto__callback *message; 162 struct mosquitto__callback *psk_key; 163 struct mosquitto__callback *reload; 164 }; 165 166 struct mosquitto__security_options { 167 /* Any options that get added here also need considering 168 * in config__read() with regards whether allow_anonymous 169 * should be disabled when these options are set. 170 */ 171 struct mosquitto__unpwd *unpwd; 172 struct mosquitto__unpwd *psk_id; 173 struct mosquitto__acl_user *acl_list; 174 struct mosquitto__acl *acl_patterns; 175 char *password_file; 176 char *psk_file; 177 char *acl_file; 178 struct mosquitto__auth_plugin_config *auth_plugin_configs; 179 int auth_plugin_config_count; 180 int8_t allow_anonymous; 181 bool allow_zero_length_clientid; 182 char *auto_id_prefix; 183 uint16_t auto_id_prefix_len; 184 struct plugin__callbacks plugin_callbacks; 185 mosquitto_plugin_id_t *pid; /* For registering as a "plugin" */ 186 }; 187 188 #ifdef WITH_EPOLL 189 enum struct_ident{ 190 id_invalid = 0, 191 id_listener = 1, 192 id_client = 2, 193 id_listener_ws = 3, 194 }; 195 #endif 196 197 struct mosquitto__listener { 198 uint16_t port; 199 char *host; 200 char *bind_interface; 201 int max_connections; 202 char *mount_point; 203 mosq_sock_t *socks; 204 int sock_count; 205 int client_count; 206 enum mosquitto_protocol protocol; 207 int socket_domain; 208 bool use_username_as_clientid; 209 uint8_t max_qos; 210 uint16_t max_topic_alias; 211 #ifdef WITH_TLS 212 char *cafile; 213 char *capath; 214 char *certfile; 215 char *keyfile; 216 char *tls_engine; 217 char *tls_engine_kpass_sha1; 218 char *ciphers; 219 char *ciphers_tls13; 220 char *psk_hint; 221 SSL_CTX *ssl_ctx; 222 char *crlfile; 223 char *tls_version; 224 char *dhparamfile; 225 bool use_identity_as_username; 226 bool use_subject_as_username; 227 bool require_certificate; 228 enum mosquitto__keyform tls_keyform; 229 #endif 230 #ifdef WITH_WEBSOCKETS 231 struct lws_context *ws_context; 232 bool ws_in_init; 233 char *http_dir; 234 struct lws_protocols *ws_protocol; 235 #endif 236 struct mosquitto__security_options security_options; 237 #ifdef WITH_UNIX_SOCKETS 238 char *unix_socket_path; 239 #endif 240 }; 241 242 243 struct mosquitto__listener_sock{ 244 #ifdef WITH_EPOLL 245 /* This *must* be the first element in the struct. */ 246 int ident; 247 #endif 248 mosq_sock_t sock; 249 struct mosquitto__listener *listener; 250 }; 251 252 typedef struct mosquitto_plugin_id_t{ 253 struct mosquitto__listener *listener; 254 } mosquitto_plugin_id_t; 255 256 struct mosquitto__config { 257 bool allow_duplicate_messages; 258 int autosave_interval; 259 bool autosave_on_changes; 260 bool check_retain_source; 261 char *clientid_prefixes; 262 bool connection_messages; 263 uint16_t cmd_port[CMD_PORT_LIMIT]; 264 int cmd_port_count; 265 bool daemon; 266 struct mosquitto__listener default_listener; 267 struct mosquitto__listener *listeners; 268 int listener_count; 269 bool local_only; 270 unsigned int log_dest; 271 int log_facility; 272 unsigned int log_type; 273 bool log_timestamp; 274 char *log_timestamp_format; 275 char *log_file; 276 FILE *log_fptr; 277 size_t max_inflight_bytes; 278 size_t max_queued_bytes; 279 int max_queued_messages; 280 uint32_t max_packet_size; 281 uint32_t message_size_limit; 282 uint16_t max_inflight_messages; 283 uint16_t max_keepalive; 284 uint8_t max_qos; 285 bool persistence; 286 char *persistence_location; 287 char *persistence_file; 288 char *persistence_filepath; 289 time_t persistent_client_expiration; 290 char *pid_file; 291 bool queue_qos0_messages; 292 bool per_listener_settings; 293 bool retain_available; 294 bool set_tcp_nodelay; 295 int sys_interval; 296 bool upgrade_outgoing_qos; 297 char *user; 298 #ifdef WITH_WEBSOCKETS 299 int websockets_log_level; 300 uint16_t websockets_headers_size; 301 #endif 302 #ifdef WITH_BRIDGE 303 struct mosquitto__bridge *bridges; 304 int bridge_count; 305 #endif 306 struct mosquitto__security_options security_options; 307 }; 308 309 310 struct mosquitto__subleaf { 311 struct mosquitto__subleaf *prev; 312 struct mosquitto__subleaf *next; 313 struct mosquitto *context; 314 uint32_t identifier; 315 uint8_t qos; 316 bool no_local; 317 bool retain_as_published; 318 }; 319 320 321 struct mosquitto__subshared { 322 UT_hash_handle hh; 323 char *name; 324 struct mosquitto__subleaf *subs; 325 }; 326 327 struct mosquitto__subhier { 328 UT_hash_handle hh; 329 struct mosquitto__subhier *parent; 330 struct mosquitto__subhier *children; 331 struct mosquitto__subleaf *subs; 332 struct mosquitto__subshared *shared; 333 char *topic; 334 uint16_t topic_len; 335 }; 336 337 struct mosquitto__client_sub { 338 struct mosquitto__subhier *hier; 339 struct mosquitto__subshared *shared; 340 char topic_filter[]; 341 }; 342 343 struct sub__token { 344 struct sub__token *next; 345 char *topic; 346 uint16_t topic_len; 347 }; 348 349 struct mosquitto__retainhier { 350 UT_hash_handle hh; 351 struct mosquitto__retainhier *parent; 352 struct mosquitto__retainhier *children; 353 struct mosquitto_msg_store *retained; 354 char *topic; 355 uint16_t topic_len; 356 }; 357 358 struct mosquitto_msg_store_load{ 359 UT_hash_handle hh; 360 dbid_t db_id; 361 struct mosquitto_msg_store *store; 362 }; 363 364 struct mosquitto_msg_store{ 365 struct mosquitto_msg_store *next; 366 struct mosquitto_msg_store *prev; 367 dbid_t db_id; 368 char *source_id; 369 char *source_username; 370 struct mosquitto__listener *source_listener; 371 char **dest_ids; 372 int dest_id_count; 373 int ref_count; 374 char* topic; 375 mosquitto_property *properties; 376 void *payload; 377 time_t message_expiry_time; 378 uint32_t payloadlen; 379 enum mosquitto_msg_origin origin; 380 uint16_t source_mid; 381 uint16_t mid; 382 uint8_t qos; 383 bool retain; 384 }; 385 386 struct mosquitto_client_msg{ 387 struct mosquitto_client_msg *prev; 388 struct mosquitto_client_msg *next; 389 struct mosquitto_msg_store *store; 390 mosquitto_property *properties; 391 time_t timestamp; 392 uint16_t mid; 393 uint8_t qos; 394 bool retain; 395 enum mosquitto_msg_direction direction; 396 enum mosquitto_msg_state state; 397 bool dup; 398 }; 399 400 401 struct mosquitto__unpwd{ 402 UT_hash_handle hh; 403 char *username; 404 char *password; 405 char *clientid; 406 #ifdef WITH_TLS 407 unsigned char *salt; 408 unsigned int password_len; 409 unsigned int salt_len; 410 int iterations; 411 #endif 412 enum mosquitto_pwhash_type hashtype; 413 }; 414 415 struct mosquitto__acl{ 416 struct mosquitto__acl *next; 417 char *topic; 418 int access; 419 int ucount; 420 int ccount; 421 }; 422 423 struct mosquitto__acl_user{ 424 struct mosquitto__acl_user *next; 425 char *username; 426 struct mosquitto__acl *acl; 427 }; 428 429 430 struct mosquitto_message_v5{ 431 struct mosquitto_message_v5 *next, *prev; 432 char *topic; 433 void *payload; 434 mosquitto_property *properties; 435 char *clientid; /* Used only by mosquitto_broker_publish*() to indicate 436 this message is for a specific client. */ 437 int payloadlen; 438 int qos; 439 bool retain; 440 }; 441 442 443 struct mosquitto_db{ 444 dbid_t last_db_id; 445 struct mosquitto__subhier *subs; 446 struct mosquitto__retainhier *retains; 447 struct mosquitto *contexts_by_id; 448 struct mosquitto *contexts_by_sock; 449 struct mosquitto *contexts_for_free; 450 #ifdef WITH_BRIDGE 451 struct mosquitto **bridges; 452 #endif 453 struct clientid__index_hash *clientid_index_hash; 454 struct mosquitto_msg_store *msg_store; 455 struct mosquitto_msg_store_load *msg_store_load; 456 time_t now_s; /* Monotonic clock, where possible */ 457 time_t now_real_s; /* Read clock, for measuring session/message expiry */ 458 #ifdef WITH_BRIDGE 459 int bridge_count; 460 #endif 461 int msg_store_count; 462 unsigned long msg_store_bytes; 463 char *config_file; 464 struct mosquitto__config *config; 465 int auth_plugin_count; 466 bool verbose; 467 #ifdef WITH_SYS_TREE 468 int subscription_count; 469 int shared_subscription_count; 470 int retained_count; 471 #endif 472 int persistence_changes; 473 struct mosquitto *ll_for_free; 474 #ifdef WITH_EPOLL 475 int epollfd; 476 #endif 477 struct mosquitto_message_v5 *plugin_msgs; 478 }; 479 480 enum mosquitto__bridge_direction{ 481 bd_out = 0, 482 bd_in = 1, 483 bd_both = 2 484 }; 485 486 enum mosquitto_bridge_start_type{ 487 bst_automatic = 0, 488 bst_lazy = 1, 489 bst_manual = 2, 490 bst_once = 3 491 }; 492 493 struct mosquitto__bridge_topic{ 494 char *topic; 495 char *local_prefix; 496 char *remote_prefix; 497 char *local_topic; /* topic prefixed with local_prefix */ 498 char *remote_topic; /* topic prefixed with remote_prefix */ 499 enum mosquitto__bridge_direction direction; 500 uint8_t qos; 501 }; 502 503 struct bridge_address{ 504 char *address; 505 uint16_t port; 506 }; 507 508 struct mosquitto__bridge{ 509 char *name; 510 struct bridge_address *addresses; 511 int cur_address; 512 int address_count; 513 time_t primary_retry; 514 mosq_sock_t primary_retry_sock; 515 bool round_robin; 516 bool try_private; 517 bool try_private_accepted; 518 bool clean_start; 519 int8_t clean_start_local; 520 uint16_t keepalive; 521 struct mosquitto__bridge_topic *topics; 522 int topic_count; 523 bool topic_remapping; 524 enum mosquitto__protocol protocol_version; 525 time_t restart_t; 526 char *remote_clientid; 527 char *remote_username; 528 char *remote_password; 529 char *local_clientid; 530 char *local_username; 531 char *local_password; 532 char *notification_topic; 533 char *bind_address; 534 bool notifications; 535 bool notifications_local_only; 536 enum mosquitto_bridge_start_type start_type; 537 int idle_timeout; 538 int restart_timeout; 539 int backoff_base; 540 int backoff_cap; 541 int threshold; 542 uint32_t maximum_packet_size; 543 bool lazy_reconnect; 544 bool attempt_unsubscribe; 545 bool initial_notification_done; 546 bool outgoing_retain; 547 #ifdef WITH_TLS 548 bool tls_insecure; 549 bool tls_ocsp_required; 550 char *tls_cafile; 551 char *tls_capath; 552 char *tls_certfile; 553 char *tls_keyfile; 554 char *tls_version; 555 char *tls_alpn; 556 # ifdef FINAL_WITH_TLS_PSK 557 char *tls_psk_identity; 558 char *tls_psk; 559 # endif 560 #endif 561 }; 562 563 #ifdef WITH_WEBSOCKETS 564 struct libws_mqtt_hack { 565 char *http_dir; 566 struct mosquitto__listener *listener; 567 }; 568 569 struct libws_mqtt_data { 570 struct mosquitto *mosq; 571 }; 572 #endif 573 574 #include <net_mosq.h> 575 576 577 extern struct mosquitto_db db; 578 579 /* ============================================================ 580 * Main functions 581 * ============================================================ */ 582 int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listensock_count); 583 584 /* ============================================================ 585 * Config functions 586 * ============================================================ */ 587 /* Initialise config struct to default values. */ 588 void config__init(struct mosquitto__config *config); 589 /* Parse command line options into config. */ 590 int config__parse_args(struct mosquitto__config *config, int argc, char *argv[]); 591 /* Read configuration data from config->config_file into config. 592 * If reload is true, don't process config options that shouldn't be reloaded (listeners etc) 593 * Returns 0 on success, 1 if there is a configuration error or if a file cannot be opened. 594 */ 595 int config__read(struct mosquitto__config *config, bool reload); 596 /* Free all config data. */ 597 void config__cleanup(struct mosquitto__config *config); 598 int config__get_dir_files(const char *include_dir, char ***files, int *file_count); 599 600 int drop_privileges(struct mosquitto__config *config); 601 602 /* ============================================================ 603 * Server send functions 604 * ============================================================ */ 605 int send__connack(struct mosquitto *context, uint8_t ack, uint8_t reason_code, const mosquitto_property *properties); 606 int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, const void *payload); 607 int send__unsuback(struct mosquitto *context, uint16_t mid, int reason_code_count, uint8_t *reason_codes, const mosquitto_property *properties); 608 int send__auth(struct mosquitto *context, uint8_t reason_code, const void *auth_data, uint16_t auth_data_len); 609 610 /* ============================================================ 611 * Network functions 612 * ============================================================ */ 613 void net__broker_init(void); 614 void net__broker_cleanup(void); 615 struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock); 616 int net__socket_listen(struct mosquitto__listener *listener); 617 int net__socket_get_address(mosq_sock_t sock, char *buf, size_t len, uint16_t *remote_address); 618 int net__tls_load_verify(struct mosquitto__listener *listener); 619 int net__tls_server_ctx(struct mosquitto__listener *listener); 620 int net__load_certificates(struct mosquitto__listener *listener); 621 622 /* ============================================================ 623 * Read handling functions 624 * ============================================================ */ 625 int handle__packet(struct mosquitto *context); 626 int handle__connack(struct mosquitto *context); 627 int handle__connect(struct mosquitto *context); 628 int handle__disconnect(struct mosquitto *context); 629 int handle__publish(struct mosquitto *context); 630 int handle__subscribe(struct mosquitto *context); 631 int handle__unsubscribe(struct mosquitto *context); 632 int handle__auth(struct mosquitto *context); 633 634 /* ============================================================ 635 * Database handling 636 * ============================================================ */ 637 int db__open(struct mosquitto__config *config); 638 int db__close(void); 639 #ifdef WITH_PERSISTENCE 640 int persist__backup(bool shutdown); 641 int persist__restore(void); 642 #endif 643 /* Return the number of in-flight messages in count. */ 644 int db__message_count(int *count); 645 int db__message_delete_outgoing(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_state expect_state, int qos); 646 int db__message_insert(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, uint8_t qos, bool retain, struct mosquitto_msg_store *stored, mosquitto_property *properties, bool update); 647 int db__message_remove_incoming(struct mosquitto* context, uint16_t mid); 648 int db__message_release_incoming(struct mosquitto *context, uint16_t mid); 649 int db__message_update_outgoing(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_state state, int qos); 650 void db__message_dequeue_first(struct mosquitto *context, struct mosquitto_msg_data *msg_data); 651 int db__messages_delete(struct mosquitto *context, bool force_free); 652 int db__messages_easy_queue(struct mosquitto *context, const char *topic, uint8_t qos, uint32_t payloadlen, const void *payload, int retain, uint32_t message_expiry_interval, mosquitto_property **properties); 653 int db__message_store(const struct mosquitto *source, struct mosquitto_msg_store *stored, uint32_t message_expiry_interval, dbid_t store_id, enum mosquitto_msg_origin origin); 654 int db__message_store_find(struct mosquitto *context, uint16_t mid, struct mosquitto_msg_store **stored); 655 void db__msg_store_add(struct mosquitto_msg_store *store); 656 void db__msg_store_remove(struct mosquitto_msg_store *store); 657 void db__msg_store_ref_inc(struct mosquitto_msg_store *store); 658 void db__msg_store_ref_dec(struct mosquitto_msg_store **store); 659 void db__msg_store_clean(void); 660 void db__msg_store_compact(void); 661 void db__msg_store_free(struct mosquitto_msg_store *store); 662 int db__message_reconnect_reset(struct mosquitto *context); 663 bool db__ready_for_flight(struct mosquitto *context, enum mosquitto_msg_direction dir, int qos); 664 bool db__ready_for_queue(struct mosquitto *context, int qos, struct mosquitto_msg_data *msg_data); 665 void sys_tree__init(void); 666 void sys_tree__update(int interval, time_t start_time); 667 int db__message_write_inflight_out_all(struct mosquitto *context); 668 int db__message_write_inflight_out_latest(struct mosquitto *context); 669 int db__message_write_queued_out(struct mosquitto *context); 670 int db__message_write_queued_in(struct mosquitto *context); 671 void db__msg_add_to_inflight_stats(struct mosquitto_msg_data *msg_data, struct mosquitto_client_msg *msg); 672 void db__msg_add_to_queued_stats(struct mosquitto_msg_data *msg_data, struct mosquitto_client_msg *msg); 673 674 /* ============================================================ 675 * Subscription functions 676 * ============================================================ */ 677 int sub__add(struct mosquitto *context, const char *sub, uint8_t qos, uint32_t identifier, int options, struct mosquitto__subhier **root); 678 struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len); 679 int sub__remove(struct mosquitto *context, const char *sub, struct mosquitto__subhier *root, uint8_t *reason); 680 void sub__tree_print(struct mosquitto__subhier *root, int level); 681 int sub__clean_session(struct mosquitto *context); 682 int sub__messages_queue(const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto_msg_store **stored); 683 int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, const char **sharename); 684 void sub__topic_tokens_free(struct sub__token *tokens); 685 686 /* ============================================================ 687 * Context functions 688 * ============================================================ */ 689 struct mosquitto *context__init(mosq_sock_t sock); 690 void context__cleanup(struct mosquitto *context, bool force_free); 691 void context__disconnect(struct mosquitto *context); 692 void context__add_to_disused(struct mosquitto *context); 693 void context__free_disused(void); 694 void context__send_will(struct mosquitto *context); 695 void context__add_to_by_id(struct mosquitto *context); 696 void context__remove_from_by_id(struct mosquitto *context); 697 698 int connect__on_authorised(struct mosquitto *context, void *auth_data_out, uint16_t auth_data_out_len); 699 700 701 /* ============================================================ 702 * Control functions 703 * ============================================================ */ 704 #ifdef WITH_CONTROL 705 int control__process(struct mosquitto *context, struct mosquitto_msg_store *stored); 706 void control__cleanup(void); 707 #endif 708 int control__register_callback(struct mosquitto__security_options *opts, MOSQ_FUNC_generic_callback cb_func, const char *topic, void *userdata); 709 int control__unregister_callback(struct mosquitto__security_options *opts, MOSQ_FUNC_generic_callback cb_func, const char *topic); 710 711 712 /* ============================================================ 713 * Logging functions 714 * ============================================================ */ 715 int log__init(struct mosquitto__config *config); 716 int log__close(struct mosquitto__config *config); 717 void log__internal(const char *fmt, ...) __attribute__((format(printf, 1, 2))); 718 719 /* ============================================================ 720 * Bridge functions 721 * ============================================================ */ 722 #ifdef WITH_BRIDGE 723 void bridge__start_all(void); 724 int bridge__new(struct mosquitto__bridge *bridge); 725 void bridge__cleanup(struct mosquitto *context); 726 int bridge__connect(struct mosquitto *context); 727 int bridge__connect_step1(struct mosquitto *context); 728 int bridge__connect_step2(struct mosquitto *context); 729 int bridge__connect_step3(struct mosquitto *context); 730 int bridge__on_connect(struct mosquitto *context); 731 void bridge__packet_cleanup(struct mosquitto *context); 732 void bridge_check(void); 733 int bridge__register_local_connections(void); 734 int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix); 735 int bridge__remap_topic_in(struct mosquitto *context, char **topic); 736 #endif 737 738 /* ============================================================ 739 * IO multiplex related functions 740 * ============================================================ */ 741 int mux__init(struct mosquitto__listener_sock *listensock, int listensock_count); 742 int mux__loop_prepare(void); 743 int mux__add_out(struct mosquitto *context); 744 int mux__remove_out(struct mosquitto *context); 745 int mux__add_in(struct mosquitto *context); 746 int mux__delete(struct mosquitto *context); 747 int mux__wait(void); 748 int mux__handle(struct mosquitto__listener_sock *listensock, int listensock_count); 749 int mux__cleanup(void); 750 751 /* ============================================================ 752 * Listener related functions 753 * ============================================================ */ 754 void listener__set_defaults(struct mosquitto__listener *listener); 755 void listeners__reload_all_certificates(void); 756 #ifdef WITH_WEBSOCKETS 757 void listeners__add_websockets(struct lws_context *ws_context, mosq_sock_t fd); 758 #endif 759 760 /* ============================================================ 761 * Plugin related functions 762 * ============================================================ */ 763 int plugin__load_v5(struct mosquitto__listener *listener, struct mosquitto__auth_plugin *plugin, struct mosquitto_opt *auth_options, int auth_option_count, void *lib); 764 void plugin__handle_disconnect(struct mosquitto *context, int reason); 765 int plugin__handle_message(struct mosquitto *context, struct mosquitto_msg_store *stored); 766 void LIB_ERROR(void); 767 void plugin__handle_tick(void); 768 769 /* ============================================================ 770 * Property related functions 771 * ============================================================ */ 772 int keepalive__add(struct mosquitto *context); 773 void keepalive__check(void); 774 int keepalive__remove(struct mosquitto *context); 775 void keepalive__remove_all(void); 776 int keepalive__update(struct mosquitto *context); 777 778 /* ============================================================ 779 * Property related functions 780 * ============================================================ */ 781 int property__process_connect(struct mosquitto *context, mosquitto_property **props); 782 int property__process_will(struct mosquitto *context, struct mosquitto_message_all *msg, mosquitto_property **props); 783 int property__process_disconnect(struct mosquitto *context, mosquitto_property **props); 784 785 /* ============================================================ 786 * Retain tree related functions 787 * ============================================================ */ 788 int retain__init(void); 789 void retain__clean(struct mosquitto__retainhier **retainhier); 790 int retain__queue(struct mosquitto *context, const char *sub, uint8_t sub_qos, uint32_t subscription_identifier); 791 int retain__store(const char *topic, struct mosquitto_msg_store *stored, char **split_topics); 792 793 /* ============================================================ 794 * Security related functions 795 * ============================================================ */ 796 int acl__find_acls(struct mosquitto *context); 797 int mosquitto_security_module_init(void); 798 int mosquitto_security_module_cleanup(void); 799 800 int mosquitto_security_init(bool reload); 801 int mosquitto_security_apply(void); 802 int mosquitto_security_cleanup(bool reload); 803 int mosquitto_acl_check(struct mosquitto *context, const char *topic, uint32_t payloadlen, void* payload, uint8_t qos, bool retain, int access); 804 int mosquitto_unpwd_check(struct mosquitto *context); 805 int mosquitto_psk_key_get(struct mosquitto *context, const char *hint, const char *identity, char *key, int max_key_len); 806 807 int mosquitto_security_init_default(bool reload); 808 int mosquitto_security_apply_default(void); 809 int mosquitto_security_cleanup_default(bool reload); 810 int mosquitto_psk_key_get_default(struct mosquitto *context, const char *hint, const char *identity, char *key, int max_key_len); 811 812 int mosquitto_security_auth_start(struct mosquitto *context, bool reauth, const void *data_in, uint16_t data_in_len, void **data_out, uint16_t *data_out_len); 813 int mosquitto_security_auth_continue(struct mosquitto *context, const void *data_in, uint16_t data_len, void **data_out, uint16_t *data_out_len); 814 815 void unpwd__free_item(struct mosquitto__unpwd **unpwd, struct mosquitto__unpwd *item); 816 817 /* ============================================================ 818 * Session expiry 819 * ============================================================ */ 820 int session_expiry__add(struct mosquitto *context); 821 void session_expiry__remove(struct mosquitto *context); 822 void session_expiry__remove_all(void); 823 void session_expiry__check(void); 824 void session_expiry__send_all(void); 825 826 /* ============================================================ 827 * Signals 828 * ============================================================ */ 829 void handle_sigint(int signal); 830 void handle_sigusr1(int signal); 831 void handle_sigusr2(int signal); 832 #ifdef SIGHUP 833 void handle_sighup(int signal); 834 #endif 835 836 /* ============================================================ 837 * Window service and signal related functions 838 * ============================================================ */ 839 #if defined(WIN32) || defined(__CYGWIN__) 840 void service_install(void); 841 void service_uninstall(void); 842 void service_run(void); 843 844 DWORD WINAPI SigThreadProc(void* data); 845 #endif 846 847 /* ============================================================ 848 * Websockets related functions 849 * ============================================================ */ 850 #ifdef WITH_WEBSOCKETS 851 void mosq_websockets_init(struct mosquitto__listener *listener, const struct mosquitto__config *conf); 852 #endif 853 void do_disconnect(struct mosquitto *context, int reason); 854 855 /* ============================================================ 856 * Will delay 857 * ============================================================ */ 858 int will_delay__add(struct mosquitto *context); 859 void will_delay__check(void); 860 void will_delay__send_all(void); 861 void will_delay__remove(struct mosquitto *mosq); 862 863 864 /* ============================================================ 865 * Other 866 * ============================================================ */ 867 #ifdef WITH_XTREPORT 868 void xtreport(void); 869 #endif 870 871 #endif 872