UNPKG

16 kBtext/x-cView Raw
1/*
2Copyright (c) 2009-2013 Roger Light <roger@atchoo.org>
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
133. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28*/
29
30#ifndef MQTT3_H
31#define MQTT3_H
32
33#include <config.h>
34#include <stdio.h>
35
36#include <mosquitto_internal.h>
37#include <mosquitto_plugin.h>
38#include <mosquitto.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_ALL 0xFF
54
55typedef uint64_t dbid_t;
56
57struct _mqtt3_listener {
58 int fd;
59 char *host;
60 uint16_t port;
61 int max_connections;
62 char *mount_point;
63 int *socks;
64 int sock_count;
65 int client_count;
66#ifdef WITH_TLS
67 char *cafile;
68 char *capath;
69 char *certfile;
70 char *keyfile;
71 char *ciphers;
72 char *psk_hint;
73 bool require_certificate;
74 SSL_CTX *ssl_ctx;
75 char *crlfile;
76 bool use_identity_as_username;
77 char *tls_version;
78#endif
79};
80
81struct mqtt3_config {
82 char *config_file;
83 char *acl_file;
84 bool allow_anonymous;
85 bool allow_duplicate_messages;
86 bool allow_zero_length_clientid;
87 char *auto_id_prefix;
88 int auto_id_prefix_len;
89 int autosave_interval;
90 bool autosave_on_changes;
91 char *clientid_prefixes;
92 bool connection_messages;
93 bool daemon;
94 struct _mqtt3_listener default_listener;
95 struct _mqtt3_listener *listeners;
96 int listener_count;
97 int log_dest;
98 int log_type;
99 bool log_timestamp;
100 char *log_file;
101 FILE *log_fptr;
102 int message_size_limit;
103 char *password_file;
104 bool persistence;
105 char *persistence_location;
106 char *persistence_file;
107 char *persistence_filepath;
108 time_t persistent_client_expiration;
109 char *pid_file;
110 char *psk_file;
111 bool queue_qos0_messages;
112 int retry_interval;
113 int store_clean_interval;
114 int sys_interval;
115 bool upgrade_outgoing_qos;
116 char *user;
117 bool verbose;
118#ifdef WITH_BRIDGE
119 struct _mqtt3_bridge *bridges;
120 int bridge_count;
121#endif
122 char *auth_plugin;
123 struct mosquitto_auth_opt *auth_options;
124 int auth_option_count;
125};
126
127struct _mosquitto_subleaf {
128 struct _mosquitto_subleaf *prev;
129 struct _mosquitto_subleaf *next;
130 struct mosquitto *context;
131 int qos;
132};
133
134struct _mosquitto_subhier {
135 struct _mosquitto_subhier *children;
136 struct _mosquitto_subhier *next;
137 struct _mosquitto_subleaf *subs;
138 char *topic;
139 struct mosquitto_msg_store *retained;
140};
141
142struct mosquitto_msg_store{
143 struct mosquitto_msg_store *next;
144 dbid_t db_id;
145 int ref_count;
146 char *source_id;
147 char **dest_ids;
148 int dest_id_count;
149 uint16_t source_mid;
150 struct mosquitto_message msg;
151};
152
153struct mosquitto_client_msg{
154 struct mosquitto_client_msg *next;
155 struct mosquitto_msg_store *store;
156 uint16_t mid;
157 int qos;
158 bool retain;
159 time_t timestamp;
160 enum mosquitto_msg_direction direction;
161 enum mosquitto_msg_state state;
162 bool dup;
163};
164
165struct _mosquitto_unpwd{
166 char *username;
167 char *password;
168#ifdef WITH_TLS
169 unsigned int password_len;
170 unsigned char *salt;
171 unsigned int salt_len;
172#endif
173 UT_hash_handle hh;
174};
175
176struct _mosquitto_acl{
177 struct _mosquitto_acl *next;
178 char *topic;
179 int access;
180};
181
182struct _mosquitto_acl_user{
183 struct _mosquitto_acl_user *next;
184 char *username;
185 struct _mosquitto_acl *acl;
186};
187
188struct _mosquitto_auth_plugin{
189 void *lib;
190 void *user_data;
191 int (*plugin_version)(void);
192 int (*plugin_init)(void **user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count);
193 int (*plugin_cleanup)(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count);
194 int (*security_init)(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload);
195 int (*security_cleanup)(void *user_data, struct mosquitto_auth_opt *auth_opts, int auth_opt_count, bool reload);
196 int (*acl_check)(void *user_data, const char *clientid, const char *username, const char *topic, int access);
197 int (*unpwd_check)(void *user_data, const char *username, const char *password);
198 int (*psk_key_get)(void *user_data, const char *hint, const char *identity, char *key, int max_key_len);
199};
200
201struct _clientid_index_hash{
202 /* this is the key */
203 char *id;
204 /* this is the index where the client ID exists in the db->contexts array */
205 int db_context_index;
206 UT_hash_handle hh;
207};
208
209struct mosquitto_db{
210 dbid_t last_db_id;
211 struct _mosquitto_subhier subs;
212 struct _mosquitto_unpwd *unpwd;
213 struct _mosquitto_acl_user *acl_list;
214 struct _mosquitto_acl *acl_patterns;
215 struct _mosquitto_unpwd *psk_id;
216 struct mosquitto **contexts;
217 struct _clientid_index_hash *clientid_index_hash;
218 int context_count;
219 struct mosquitto_msg_store *msg_store;
220 int msg_store_count;
221 struct mqtt3_config *config;
222 int persistence_changes;
223 struct _mosquitto_auth_plugin auth_plugin;
224 int subscription_count;
225 int retained_count;
226};
227
228enum mqtt3_bridge_direction{
229 bd_out = 0,
230 bd_in = 1,
231 bd_both = 2
232};
233
234enum mosquitto_bridge_start_type{
235 bst_automatic = 0,
236 bst_lazy = 1,
237 bst_manual = 2,
238 bst_once = 3
239};
240
241struct _mqtt3_bridge_topic{
242 char *topic;
243 int qos;
244 enum mqtt3_bridge_direction direction;
245 char *local_prefix;
246 char *remote_prefix;
247 char *local_topic; /* topic prefixed with local_prefix */
248 char *remote_topic; /* topic prefixed with remote_prefix */
249};
250
251struct bridge_address{
252 char *address;
253 int port;
254};
255
256struct _mqtt3_bridge{
257 char *name;
258 struct bridge_address *addresses;
259 int cur_address;
260 int address_count;
261 time_t primary_retry;
262 bool round_robin;
263 char *clientid;
264 int keepalive;
265 bool clean_session;
266 struct _mqtt3_bridge_topic *topics;
267 int topic_count;
268 bool topic_remapping;
269 time_t restart_t;
270 char *username;
271 char *password;
272 bool notifications;
273 char *notification_topic;
274 enum mosquitto_bridge_start_type start_type;
275 int idle_timeout;
276 int restart_timeout;
277 int threshold;
278 bool lazy_reconnect;
279 bool try_private;
280 bool try_private_accepted;
281#ifdef WITH_TLS
282 char *tls_cafile;
283 char *tls_capath;
284 char *tls_certfile;
285 char *tls_keyfile;
286 bool tls_insecure;
287 char *tls_version;
288# ifdef REAL_WITH_TLS_PSK
289 char *tls_psk_identity;
290 char *tls_psk;
291# endif
292#endif
293};
294
295#include <net_mosq.h>
296
297/* ============================================================
298 * Main functions
299 * ============================================================ */
300int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock_count, int listener_max);
301struct mosquitto_db *_mosquitto_get_db(void);
302
303/* ============================================================
304 * Config functions
305 * ============================================================ */
306/* Initialise config struct to default values. */
307void mqtt3_config_init(struct mqtt3_config *config);
308/* Parse command line options into config. */
309int mqtt3_config_parse_args(struct mqtt3_config *config, int argc, char *argv[]);
310/* Read configuration data from config->config_file into config.
311 * If reload is true, don't process config options that shouldn't be reloaded (listeners etc)
312 * Returns 0 on success, 1 if there is a configuration error or if a file cannot be opened.
313 */
314int mqtt3_config_read(struct mqtt3_config *config, bool reload);
315/* Free all config data. */
316void mqtt3_config_cleanup(struct mqtt3_config *config);
317
318/* ============================================================
319 * Server send functions
320 * ============================================================ */
321int _mosquitto_send_connack(struct mosquitto *context, int result);
322int _mosquitto_send_suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, const void *payload);
323
324/* ============================================================
325 * Network functions
326 * ============================================================ */
327int mqtt3_socket_accept(struct mosquitto_db *db, int listensock);
328int mqtt3_socket_listen(struct _mqtt3_listener *listener);
329int _mosquitto_socket_get_address(int sock, char *buf, int len);
330
331/* ============================================================
332 * Read handling functions
333 * ============================================================ */
334int mqtt3_packet_handle(struct mosquitto_db *db, struct mosquitto *context);
335int mqtt3_handle_connack(struct mosquitto_db *db, struct mosquitto *context);
336int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context);
337int mqtt3_handle_disconnect(struct mosquitto_db *db, struct mosquitto *context);
338int mqtt3_handle_publish(struct mosquitto_db *db, struct mosquitto *context);
339int mqtt3_handle_subscribe(struct mosquitto_db *db, struct mosquitto *context);
340int mqtt3_handle_unsubscribe(struct mosquitto_db *db, struct mosquitto *context);
341
342/* ============================================================
343 * Database handling
344 * ============================================================ */
345int mqtt3_db_open(struct mqtt3_config *config, struct mosquitto_db *db);
346int mqtt3_db_close(struct mosquitto_db *db);
347#ifdef WITH_PERSISTENCE
348int mqtt3_db_backup(struct mosquitto_db *db, bool cleanup, bool shutdown);
349int mqtt3_db_restore(struct mosquitto_db *db);
350#endif
351int mqtt3_db_client_count(struct mosquitto_db *db, unsigned int *count, unsigned int *inactive_count);
352void mqtt3_db_limits_set(int inflight, int queued);
353/* Return the number of in-flight messages in count. */
354int mqtt3_db_message_count(int *count);
355int mqtt3_db_message_delete(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir);
356int mqtt3_db_message_insert(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, int qos, bool retain, struct mosquitto_msg_store *stored);
357int mqtt3_db_message_release(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir);
358int mqtt3_db_message_update(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, enum mosquitto_msg_state state);
359int mqtt3_db_message_write(struct mosquitto *context);
360int mqtt3_db_messages_delete(struct mosquitto *context);
361int mqtt3_db_messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int qos, uint32_t payloadlen, const void *payload, int retain);
362int mqtt3_db_messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored);
363int mqtt3_db_message_store(struct mosquitto_db *db, const char *source, uint16_t source_mid, const char *topic, int qos, uint32_t payloadlen, const void *payload, int retain, struct mosquitto_msg_store **stored, dbid_t store_id);
364int mqtt3_db_message_store_find(struct mosquitto *context, uint16_t mid, struct mosquitto_msg_store **stored);
365/* Check all messages waiting on a client reply and resend if timeout has been exceeded. */
366int mqtt3_db_message_timeout_check(struct mosquitto_db *db, unsigned int timeout);
367int mqtt3_db_message_reconnect_reset(struct mosquitto *context);
368int mqtt3_retain_queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos);
369void mqtt3_db_store_clean(struct mosquitto_db *db);
370void mqtt3_db_sys_update(struct mosquitto_db *db, int interval, time_t start_time);
371void mqtt3_db_vacuum(void);
372
373/* ============================================================
374 * Subscription functions
375 * ============================================================ */
376int mqtt3_sub_add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, struct _mosquitto_subhier *root);
377int mqtt3_sub_remove(struct mosquitto_db *db, struct mosquitto *context, const char *sub, struct _mosquitto_subhier *root);
378int mqtt3_sub_search(struct mosquitto_db *db, struct _mosquitto_subhier *root, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored);
379void mqtt3_sub_tree_print(struct _mosquitto_subhier *root, int level);
380int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context, struct _mosquitto_subhier *root);
381
382/* ============================================================
383 * Context functions
384 * ============================================================ */
385struct mosquitto *mqtt3_context_init(int sock);
386void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free);
387void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context);
388
389/* ============================================================
390 * Logging functions
391 * ============================================================ */
392int mqtt3_log_init(int level, int destinations);
393int mqtt3_log_close(void);
394int _mosquitto_log_printf(struct mosquitto *mosq, int level, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
395
396/* ============================================================
397 * Bridge functions
398 * ============================================================ */
399#ifdef WITH_BRIDGE
400int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge);
401int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context);
402void mqtt3_bridge_packet_cleanup(struct mosquitto *context);
403#endif
404
405/* ============================================================
406 * Security related functions
407 * ============================================================ */
408int mosquitto_security_module_init(struct mosquitto_db *db);
409int mosquitto_security_module_cleanup(struct mosquitto_db *db);
410
411int mosquitto_security_init(struct mosquitto_db *db, bool reload);
412int mosquitto_security_apply(struct mosquitto_db *db);
413int mosquitto_security_cleanup(struct mosquitto_db *db, bool reload);
414int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access);
415int mosquitto_unpwd_check(struct mosquitto_db *db, const char *username, const char *password);
416int mosquitto_psk_key_get(struct mosquitto_db *db, const char *hint, const char *identity, char *key, int max_key_len);
417
418int mosquitto_security_init_default(struct mosquitto_db *db, bool reload);
419int mosquitto_security_apply_default(struct mosquitto_db *db);
420int mosquitto_security_cleanup_default(struct mosquitto_db *db, bool reload);
421int mosquitto_acl_check_default(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access);
422int mosquitto_unpwd_check_default(struct mosquitto_db *db, const char *username, const char *password);
423int mosquitto_psk_key_get_default(struct mosquitto_db *db, const char *hint, const char *identity, char *key, int max_key_len);
424
425/* ============================================================
426 * Window service related functions
427 * ============================================================ */
428#if defined(WIN32) || defined(__CYGWIN__)
429void service_install(void);
430void service_uninstall(void);
431void service_run(void);
432#endif
433
434#endif