1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
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 |
|
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 |
|
55 | typedef uint64_t dbid_t;
|
56 |
|
57 | struct _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 |
|
81 | struct 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 |
|
127 | struct _mosquitto_subleaf {
|
128 | struct _mosquitto_subleaf *prev;
|
129 | struct _mosquitto_subleaf *next;
|
130 | struct mosquitto *context;
|
131 | int qos;
|
132 | };
|
133 |
|
134 | struct _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 |
|
142 | struct 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 |
|
153 | struct 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 |
|
165 | struct _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 |
|
176 | struct _mosquitto_acl{
|
177 | struct _mosquitto_acl *next;
|
178 | char *topic;
|
179 | int access;
|
180 | };
|
181 |
|
182 | struct _mosquitto_acl_user{
|
183 | struct _mosquitto_acl_user *next;
|
184 | char *username;
|
185 | struct _mosquitto_acl *acl;
|
186 | };
|
187 |
|
188 | struct _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 |
|
201 | struct _clientid_index_hash{
|
202 |
|
203 | char *id;
|
204 |
|
205 | int db_context_index;
|
206 | UT_hash_handle hh;
|
207 | };
|
208 |
|
209 | struct 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 |
|
228 | enum mqtt3_bridge_direction{
|
229 | bd_out = 0,
|
230 | bd_in = 1,
|
231 | bd_both = 2
|
232 | };
|
233 |
|
234 | enum mosquitto_bridge_start_type{
|
235 | bst_automatic = 0,
|
236 | bst_lazy = 1,
|
237 | bst_manual = 2,
|
238 | bst_once = 3
|
239 | };
|
240 |
|
241 | struct _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;
|
248 | char *remote_topic;
|
249 | };
|
250 |
|
251 | struct bridge_address{
|
252 | char *address;
|
253 | int port;
|
254 | };
|
255 |
|
256 | struct _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 |
|
299 |
|
300 | int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock_count, int listener_max);
|
301 | struct mosquitto_db *_mosquitto_get_db(void);
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | void mqtt3_config_init(struct mqtt3_config *config);
|
308 |
|
309 | int mqtt3_config_parse_args(struct mqtt3_config *config, int argc, char *argv[]);
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | int mqtt3_config_read(struct mqtt3_config *config, bool reload);
|
315 |
|
316 | void mqtt3_config_cleanup(struct mqtt3_config *config);
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | int _mosquitto_send_connack(struct mosquitto *context, int result);
|
322 | int _mosquitto_send_suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, const void *payload);
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | int mqtt3_socket_accept(struct mosquitto_db *db, int listensock);
|
328 | int mqtt3_socket_listen(struct _mqtt3_listener *listener);
|
329 | int _mosquitto_socket_get_address(int sock, char *buf, int len);
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | int mqtt3_packet_handle(struct mosquitto_db *db, struct mosquitto *context);
|
335 | int mqtt3_handle_connack(struct mosquitto_db *db, struct mosquitto *context);
|
336 | int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context);
|
337 | int mqtt3_handle_disconnect(struct mosquitto_db *db, struct mosquitto *context);
|
338 | int mqtt3_handle_publish(struct mosquitto_db *db, struct mosquitto *context);
|
339 | int mqtt3_handle_subscribe(struct mosquitto_db *db, struct mosquitto *context);
|
340 | int mqtt3_handle_unsubscribe(struct mosquitto_db *db, struct mosquitto *context);
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | int mqtt3_db_open(struct mqtt3_config *config, struct mosquitto_db *db);
|
346 | int mqtt3_db_close(struct mosquitto_db *db);
|
347 | #ifdef WITH_PERSISTENCE
|
348 | int mqtt3_db_backup(struct mosquitto_db *db, bool cleanup, bool shutdown);
|
349 | int mqtt3_db_restore(struct mosquitto_db *db);
|
350 | #endif
|
351 | int mqtt3_db_client_count(struct mosquitto_db *db, unsigned int *count, unsigned int *inactive_count);
|
352 | void mqtt3_db_limits_set(int inflight, int queued);
|
353 |
|
354 | int mqtt3_db_message_count(int *count);
|
355 | int mqtt3_db_message_delete(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir);
|
356 | int 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);
|
357 | int mqtt3_db_message_release(struct mosquitto_db *db, struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir);
|
358 | int mqtt3_db_message_update(struct mosquitto *context, uint16_t mid, enum mosquitto_msg_direction dir, enum mosquitto_msg_state state);
|
359 | int mqtt3_db_message_write(struct mosquitto *context);
|
360 | int mqtt3_db_messages_delete(struct mosquitto *context);
|
361 | int 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);
|
362 | int mqtt3_db_messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store *stored);
|
363 | int 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);
|
364 | int mqtt3_db_message_store_find(struct mosquitto *context, uint16_t mid, struct mosquitto_msg_store **stored);
|
365 |
|
366 | int mqtt3_db_message_timeout_check(struct mosquitto_db *db, unsigned int timeout);
|
367 | int mqtt3_db_message_reconnect_reset(struct mosquitto *context);
|
368 | int mqtt3_retain_queue(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int sub_qos);
|
369 | void mqtt3_db_store_clean(struct mosquitto_db *db);
|
370 | void mqtt3_db_sys_update(struct mosquitto_db *db, int interval, time_t start_time);
|
371 | void mqtt3_db_vacuum(void);
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | int mqtt3_sub_add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, struct _mosquitto_subhier *root);
|
377 | int mqtt3_sub_remove(struct mosquitto_db *db, struct mosquitto *context, const char *sub, struct _mosquitto_subhier *root);
|
378 | int 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);
|
379 | void mqtt3_sub_tree_print(struct _mosquitto_subhier *root, int level);
|
380 | int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context, struct _mosquitto_subhier *root);
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | struct mosquitto *mqtt3_context_init(int sock);
|
386 | void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free);
|
387 | void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context);
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | int mqtt3_log_init(int level, int destinations);
|
393 | int mqtt3_log_close(void);
|
394 | int _mosquitto_log_printf(struct mosquitto *mosq, int level, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | #ifdef WITH_BRIDGE
|
400 | int mqtt3_bridge_new(struct mosquitto_db *db, struct _mqtt3_bridge *bridge);
|
401 | int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context);
|
402 | void mqtt3_bridge_packet_cleanup(struct mosquitto *context);
|
403 | #endif
|
404 |
|
405 |
|
406 |
|
407 |
|
408 | int mosquitto_security_module_init(struct mosquitto_db *db);
|
409 | int mosquitto_security_module_cleanup(struct mosquitto_db *db);
|
410 |
|
411 | int mosquitto_security_init(struct mosquitto_db *db, bool reload);
|
412 | int mosquitto_security_apply(struct mosquitto_db *db);
|
413 | int mosquitto_security_cleanup(struct mosquitto_db *db, bool reload);
|
414 | int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access);
|
415 | int mosquitto_unpwd_check(struct mosquitto_db *db, const char *username, const char *password);
|
416 | int mosquitto_psk_key_get(struct mosquitto_db *db, const char *hint, const char *identity, char *key, int max_key_len);
|
417 |
|
418 | int mosquitto_security_init_default(struct mosquitto_db *db, bool reload);
|
419 | int mosquitto_security_apply_default(struct mosquitto_db *db);
|
420 | int mosquitto_security_cleanup_default(struct mosquitto_db *db, bool reload);
|
421 | int mosquitto_acl_check_default(struct mosquitto_db *db, struct mosquitto *context, const char *topic, int access);
|
422 | int mosquitto_unpwd_check_default(struct mosquitto_db *db, const char *username, const char *password);
|
423 | int mosquitto_psk_key_get_default(struct mosquitto_db *db, const char *hint, const char *identity, char *key, int max_key_len);
|
424 |
|
425 |
|
426 |
|
427 |
|
428 | #if defined(WIN32) || defined(__CYGWIN__)
|
429 | void service_install(void);
|
430 | void service_uninstall(void);
|
431 | void service_run(void);
|
432 | #endif
|
433 |
|
434 | #endif
|