Debian Codegraph @

src/mqtt.c

raw
1 /**
2 * collectd - src/mqtt.c
3 * Copyright (C) 2014 Marc Falzon
4 * Copyright (C) 2014,2015 Florian octo Forster
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Marc Falzon <marc at baha dot mu>
26 * Florian octo Forster <octo at collectd.org>
27 * Jan-Piet Mens <jpmens at gmail.com>
28 **/
29
30 // Reference: http://mosquitto.org/api/files/mosquitto-h.html
31
32 #include "collectd.h"
33
34 #include "plugin.h"
35 #include "utils/common/common.h"
36 #include "utils_complain.h"
37
38 #include <mosquitto.h>
39
40 #define MQTT_MAX_TOPIC_SIZE 1024
41 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
42 #define MQTT_DEFAULT_HOST "localhost"
43 #define MQTT_DEFAULT_PORT 1883
44 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
45 #define MQTT_DEFAULT_TOPIC "collectd/#"
46 #ifndef MQTT_KEEPALIVE
47 #define MQTT_KEEPALIVE 60
48 #endif
49 #ifndef SSL_VERIFY_PEER
50 #define SSL_VERIFY_PEER 1
51 #endif
52
53 /*
54 * Data types
55 */
56 struct mqtt_client_conf {
57 bool publish;
58 char *name;
59
60 struct mosquitto *mosq;
61 bool connected;
62
63 char *host;
64 int port;
65 char *client_id;
66 char *username;
67 char *password;
68 int qos;
69 char *cacertificatefile;
70 char *certificatefile;
71 char *certificatekeyfile;
72 char *tlsprotocol;
73 char *ciphersuite;
74
75 /* For publishing */
76 char *topic_prefix;
77 bool store_rates;
78 bool retain;
79
80 /* For subscribing */
81 pthread_t thread;
82 bool loop;
83 char *topic;
84 bool clean_session;
85
86 c_complain_t complaint_cantpublish;
87 pthread_mutex_t lock;
88 };
89 typedef struct mqtt_client_conf mqtt_client_conf_t;
90
91 static mqtt_client_conf_t **subscribers;
92 static size_t subscribers_num;
93
94 /*
95 * Functions
96 */
97 #if LIBMOSQUITTO_MAJOR == 0
98 static char const *mosquitto_strerror(int code) {
99 switch (code) {
100 case MOSQ_ERR_SUCCESS:
101 return "MOSQ_ERR_SUCCESS";
102 case MOSQ_ERR_NOMEM:
103 return "MOSQ_ERR_NOMEM";
104 case MOSQ_ERR_PROTOCOL:
105 return "MOSQ_ERR_PROTOCOL";
106 case MOSQ_ERR_INVAL:
107 return "MOSQ_ERR_INVAL";
108 case MOSQ_ERR_NO_CONN:
109 return "MOSQ_ERR_NO_CONN";
110 case MOSQ_ERR_CONN_REFUSED:
111 return "MOSQ_ERR_CONN_REFUSED";
112 case MOSQ_ERR_NOT_FOUND:
113 return "MOSQ_ERR_NOT_FOUND";
114 case MOSQ_ERR_CONN_LOST:
115 return "MOSQ_ERR_CONN_LOST";
116 case MOSQ_ERR_SSL:
117 return "MOSQ_ERR_SSL";
118 case MOSQ_ERR_PAYLOAD_SIZE:
119 return "MOSQ_ERR_PAYLOAD_SIZE";
120 case MOSQ_ERR_NOT_SUPPORTED:
121 return "MOSQ_ERR_NOT_SUPPORTED";
122 case MOSQ_ERR_AUTH:
123 return "MOSQ_ERR_AUTH";
124 case MOSQ_ERR_ACL_DENIED:
125 return "MOSQ_ERR_ACL_DENIED";
126 case MOSQ_ERR_UNKNOWN:
127 return "MOSQ_ERR_UNKNOWN";
128 case MOSQ_ERR_ERRNO:
129 return "MOSQ_ERR_ERRNO";
130 }
131
132 return "UNKNOWN ERROR CODE";
133 }
134 #else
135 /* provided by libmosquitto */
136 #endif
137
138 static void mqtt_free(mqtt_client_conf_t *conf) {
139 if (conf == NULL)
140 return;
141
142 if (conf->connected)
143 (void)mosquitto_disconnect(conf->mosq);
144 conf->connected = false;
145 (void)mosquitto_destroy(conf->mosq);
146
147 sfree(conf->host);
148 sfree(conf->username);
149 sfree(conf->password);
150 sfree(conf->client_id);
151 sfree(conf->topic_prefix);
152 sfree(conf);
153 }
154
155 static char *strip_prefix(char *topic) {
156 size_t num = 0;
157
158 for (size_t i = 0; topic[i] != 0; i++)
159 if (topic[i] == '/')
160 num++;
161
162 if (num < 2)
163 return NULL;
164
165 while (num > 2) {
166 char *tmp = strchr(topic, '/');
167 if (tmp == NULL)
168 return NULL;
169 topic = tmp + 1;
170 num--;
171 }
172
173 return topic;
174 }
175
176 static void on_message(
177 #if LIBMOSQUITTO_MAJOR == 0
178 #else
179 __attribute__((unused)) struct mosquitto *m,
180 #endif
181 __attribute__((unused)) void *arg, const struct mosquitto_message *msg) {
182 value_list_t vl = VALUE_LIST_INIT;
183 data_set_t const *ds;
184 char *topic;
185 char *name;
186 char *payload;
187 int status;
188
189 if (msg->payloadlen <= 0) {
190 DEBUG("mqtt plugin: message has empty payload");
191 return;
192 }
193
194 topic = strdup(msg->topic);
195 name = strip_prefix(topic);
196
197 status = parse_identifier_vl(name, &vl);
198 if (status != 0) {
199 ERROR("mqtt plugin: Unable to parse topic \"%s\".", topic);
200 sfree(topic);
201 return;
202 }
203 sfree(topic);
204
205 ds = plugin_get_ds(vl.type);
206 if (ds == NULL) {
207 ERROR("mqtt plugin: Unknown type: \"%s\".", vl.type);
208 return;
209 }
210
211 vl.values = calloc(ds->ds_num, sizeof(*vl.values));
212 if (vl.values == NULL) {
213 ERROR("mqtt plugin: calloc failed.");
214 return;
215 }
216 vl.values_len = ds->ds_num;
217
218 payload = malloc(msg->payloadlen + 1);
219 if (payload == NULL) {
220 ERROR("mqtt plugin: malloc for payload buffer failed.");
221 sfree(vl.values);
222 return;
223 }
224 memmove(payload, msg->payload, msg->payloadlen);
225 payload[msg->payloadlen] = 0;
226
227 DEBUG("mqtt plugin: payload = \"%s\"", payload);
228 status = parse_values(payload, &vl, ds);
229 if (status != 0) {
230 ERROR("mqtt plugin: Unable to parse payload \"%s\".", payload);
231 sfree(payload);
232 sfree(vl.values);
233 return;
234 }
235 sfree(payload);
236
237 plugin_dispatch_values(&vl);
238 sfree(vl.values);
239 } /* void on_message */
240
241 static int mqtt_subscribe(mqtt_client_conf_t *conf) {
242 int status = mosquitto_subscribe(conf->mosq, /* message_id = */ NULL,
243 conf->topic, conf->qos);
244 if (status != MOSQ_ERR_SUCCESS) {
245 ERROR("mqtt plugin: Subscribing to \"%s\" failed: %s", conf->topic,
246 mosquitto_strerror(status));
247 mosquitto_disconnect(conf->mosq);
248 return -1;
249 }
250
251 return 0;
252 }
253
254 /* must hold conf->lock when calling. */
255 static int mqtt_reconnect(mqtt_client_conf_t *conf) {
256 int status;
257
258 if (conf->connected)
259 return 0;
260
261 status = mosquitto_reconnect(conf->mosq);
262 if (status != MOSQ_ERR_SUCCESS) {
263 ERROR("mqtt_connect_broker: mosquitto_connect failed: %s",
264 (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
265 return -1;
266 }
267
268 if (!conf->publish) {
269 status = mqtt_subscribe(conf);
270 if (status != 0)
271 return status;
272 }
273
274 conf->connected = true;
275
276 c_release(LOG_INFO, &conf->complaint_cantpublish,
277 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
278 conf->host, conf->port);
279
280 return 0;
281 } /* mqtt_reconnect */
282
283 /* must hold conf->lock when calling. */
284 static int mqtt_connect(mqtt_client_conf_t *conf) {
285 char const *client_id;
286 int status;
287
288 if (conf->mosq != NULL)
289 return mqtt_reconnect(conf);
290
291 if (conf->client_id)
292 client_id = conf->client_id;
293 else
294 client_id = hostname_g;
295
296 #if LIBMOSQUITTO_MAJOR == 0
297 conf->mosq = mosquitto_new(client_id, /* user data = */ conf);
298 #else
299 conf->mosq =
300 mosquitto_new(client_id, conf->clean_session, /* user data = */ conf);
301 #endif
302 if (conf->mosq == NULL) {
303 ERROR("mqtt plugin: mosquitto_new failed");
304 return -1;
305 }
306
307 #if LIBMOSQUITTO_MAJOR != 0
308 if (conf->cacertificatefile) {
309 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
310 conf->certificatefile, conf->certificatekeyfile,
311 /* pw_callback */ NULL);
312 if (status != MOSQ_ERR_SUCCESS) {
313 ERROR("mqtt plugin: cannot mosquitto_tls_set: %s",
314 mosquitto_strerror(status));
315 mosquitto_destroy(conf->mosq);
316 conf->mosq = NULL;
317 return -1;
318 }
319
320 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER,
321 conf->tlsprotocol, conf->ciphersuite);
322 if (status != MOSQ_ERR_SUCCESS) {
323 ERROR("mqtt plugin: cannot mosquitto_tls_opts_set: %s",
324 mosquitto_strerror(status));
325 mosquitto_destroy(conf->mosq);
326 conf->mosq = NULL;
327 return -1;
328 }
329
330 status = mosquitto_tls_insecure_set(conf->mosq, false);
331 if (status != MOSQ_ERR_SUCCESS) {
332 ERROR("mqtt plugin: cannot mosquitto_tls_insecure_set: %s",
333 mosquitto_strerror(status));
334 mosquitto_destroy(conf->mosq);
335 conf->mosq = NULL;
336 return -1;
337 }
338 }
339 #endif
340
341 if (conf->username && conf->password) {
342 status =
343 mosquitto_username_pw_set(conf->mosq, conf->username, conf->password);
344 if (status != MOSQ_ERR_SUCCESS) {
345 ERROR("mqtt plugin: mosquitto_username_pw_set failed: %s",
346 (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
347
348 mosquitto_destroy(conf->mosq);
349 conf->mosq = NULL;
350 return -1;
351 }
352 }
353
354 #if LIBMOSQUITTO_MAJOR == 0
355 status = mosquitto_connect(conf->mosq, conf->host, conf->port,
356 /* keepalive = */ MQTT_KEEPALIVE,
357 /* clean session = */ conf->clean_session);
358 #else
359 status =
360 mosquitto_connect(conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
361 #endif
362 if (status != MOSQ_ERR_SUCCESS) {
363 ERROR("mqtt plugin: mosquitto_connect failed: %s",
364 (status == MOSQ_ERR_ERRNO) ? STRERRNO : mosquitto_strerror(status));
365
366 mosquitto_destroy(conf->mosq);
367 conf->mosq = NULL;
368 return -1;
369 }
370
371 if (!conf->publish) {
372 mosquitto_message_callback_set(conf->mosq, on_message);
373
374 status = mqtt_subscribe(conf);
375 if (status != 0) {
376 mosquitto_destroy(conf->mosq);
377 conf->mosq = NULL;
378 return status;
379 }
380 }
381
382 conf->connected = true;
383 return 0;
384 } /* mqtt_connect */
385
386 static void *subscribers_thread(void *arg) {
387 mqtt_client_conf_t *conf = arg;
388 int status;
389
390 conf->loop = 1;
391
392 while (conf->loop) {
393 status = mqtt_connect(conf);
394 if (status != 0) {
395 sleep(1);
396 continue;
397 }
398
399 /* The documentation says "0" would map to the default (1000ms), but
400 * that does not work on some versions. */
401 #if LIBMOSQUITTO_MAJOR == 0
402 status = mosquitto_loop(conf->mosq, /* timeout = */ 1000 /* ms */);
403 #else
404 status = mosquitto_loop(conf->mosq,
405 /* timeout[ms] = */ 1000,
406 /* max_packets = */ 100);
407 #endif
408 if (status == MOSQ_ERR_CONN_LOST) {
409 conf->connected = false;
410 continue;
411 } else if (status != MOSQ_ERR_SUCCESS) {
412 ERROR("mqtt plugin: mosquitto_loop failed: %s",
413 mosquitto_strerror(status));
414 mosquitto_destroy(conf->mosq);
415 conf->mosq = NULL;
416 conf->connected = false;
417 continue;
418 }
419
420 DEBUG("mqtt plugin: mosquitto_loop succeeded.");
421 } /* while (conf->loop) */
422
423 pthread_exit(0);
424 } /* void *subscribers_thread */
425
426 static int publish(mqtt_client_conf_t *conf, char const *topic,
427 void const *payload, size_t payload_len) {
428 int status;
429
430 pthread_mutex_lock(&conf->lock);
431
432 status = mqtt_connect(conf);
433 if (status != 0) {
434 pthread_mutex_unlock(&conf->lock);
435 ERROR("mqtt plugin: unable to reconnect to broker");
436 return status;
437 }
438
439 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
440 #if LIBMOSQUITTO_MAJOR == 0
441 (uint32_t)payload_len, payload,
442 #else
443 (int)payload_len, payload,
444 #endif
445 conf->qos, conf->retain);
446 if (status != MOSQ_ERR_SUCCESS) {
447 c_complain(LOG_ERR, &conf->complaint_cantpublish,
448 "mqtt plugin: mosquitto_publish failed: %s",
449 (status == MOSQ_ERR_ERRNO) ? STRERRNO
450 : mosquitto_strerror(status));
451 /* Mark our connection "down" regardless of the error as a safety
452 * measure; we will try to reconnect the next time we have to publish a
453 * message */
454 conf->connected = false;
455 mosquitto_disconnect(conf->mosq);
456
457 pthread_mutex_unlock(&conf->lock);
458 return -1;
459 }
460
461 #if LIBMOSQUITTO_MAJOR == 0
462 status = mosquitto_loop(conf->mosq, /* timeout = */ 1000 /* ms */);
463 #else
464 status = mosquitto_loop(conf->mosq,
465 /* timeout[ms] = */ 1000,
466 /* max_packets = */ 1);
467 #endif
468
469 if (status != MOSQ_ERR_SUCCESS) {
470 c_complain(LOG_ERR, &conf->complaint_cantpublish,
471 "mqtt plugin: mosquitto_loop failed: %s",
472 (status == MOSQ_ERR_ERRNO) ? STRERRNO
473 : mosquitto_strerror(status));
474 /* Mark our connection "down" regardless of the error as a safety
475 * measure; we will try to reconnect the next time we have to publish a
476 * message */
477 conf->connected = 0;
478 mosquitto_disconnect(conf->mosq);
479
480 pthread_mutex_unlock(&conf->lock);
481 return -1;
482 }
483
484 pthread_mutex_unlock(&conf->lock);
485 return 0;
486 } /* int publish */
487
488 static int format_topic(char *buf, size_t buf_len, data_set_t const *ds,
489 value_list_t const *vl, mqtt_client_conf_t *conf) {
490 char name[MQTT_MAX_TOPIC_SIZE];
491 int status;
492 char *c;
493
494 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
495 return FORMAT_VL(buf, buf_len, vl);
496
497 status = FORMAT_VL(name, sizeof(name), vl);
498 if (status != 0)
499 return status;
500
501 status = ssnprintf(buf, buf_len, "%s/%s", conf->topic_prefix, name);
502 if ((status < 0) || (((size_t)status) >= buf_len))
503 return ENOMEM;
504
505 while ((c = strchr(buf, '#')) || (c = strchr(buf, '+'))) {
506 *c = '_';
507 }
508
509 return 0;
510 } /* int format_topic */
511
512 static int mqtt_write(const data_set_t *ds, const value_list_t *vl,
513 user_data_t *user_data) {
514 mqtt_client_conf_t *conf;
515 char topic[MQTT_MAX_TOPIC_SIZE];
516 char payload[MQTT_MAX_MESSAGE_SIZE];
517 int status = 0;
518
519 if ((user_data == NULL) || (user_data->data == NULL))
520 return EINVAL;
521 conf = user_data->data;
522
523 status = format_topic(topic, sizeof(topic), ds, vl, conf);
524 if (status != 0) {
525 ERROR("mqtt plugin: format_topic failed with status %d.", status);
526 return status;
527 }
528
529 status = format_values(payload, sizeof(payload), ds, vl, conf->store_rates);
530 if (status != 0) {
531 ERROR("mqtt plugin: format_values failed with status %d.", status);
532 return status;
533 }
534
535 status = publish(conf, topic, payload, strlen(payload) + 1);
536 if (status != 0) {
537 ERROR("mqtt plugin: publish failed: %s", mosquitto_strerror(status));
538 return status;
539 }
540
541 return status;
542 } /* mqtt_write */
543
544 /*
545 * <Publish "name">
546 * Host "example.com"
547 * Port 1883
548 * ClientId "collectd"
549 * User "guest"
550 * Password "secret"
551 * Prefix "collectd"
552 * StoreRates true
553 * Retain false
554 * QoS 0
555 * CACert "ca.pem" Enables TLS if set
556 * CertificateFile "client-cert.pem" optional
557 * CertificateKeyFile "client-key.pem" optional
558 * TLSProtocol "tlsv1.2" optional
559 * </Publish>
560 */
561 static int mqtt_config_publisher(oconfig_item_t *ci) {
562 mqtt_client_conf_t *conf;
563 char cb_name[1024];
564 int status;
565
566 conf = calloc(1, sizeof(*conf));
567 if (conf == NULL) {
568 ERROR("mqtt plugin: calloc failed.");
569 return -1;
570 }
571 conf->publish = true;
572
573 conf->name = NULL;
574 status = cf_util_get_string(ci, &conf->name);
575 if (status != 0) {
576 mqtt_free(conf);
577 return status;
578 }
579
580 conf->host = strdup(MQTT_DEFAULT_HOST);
581 conf->port = MQTT_DEFAULT_PORT;
582 conf->client_id = NULL;
583 conf->qos = 0;
584 conf->topic_prefix = strdup(MQTT_DEFAULT_TOPIC_PREFIX);
585 conf->store_rates = true;
586
587 status = pthread_mutex_init(&conf->lock, NULL);
588 if (status != 0) {
589 mqtt_free(conf);
590 return status;
591 }
592
593 C_COMPLAIN_INIT(&conf->complaint_cantpublish);
594
595 for (int i = 0; i < ci->children_num; i++) {
596 oconfig_item_t *child = ci->children + i;
597 if (strcasecmp("Host", child->key) == 0)
598 cf_util_get_string(child, &conf->host);
599 else if (strcasecmp("Port", child->key) == 0) {
600 int tmp = cf_util_get_port_number(child);
601 if (tmp < 0)
602 ERROR("mqtt plugin: Invalid port number.");
603 else
604 conf->port = tmp;
605 } else if (strcasecmp("ClientId", child->key) == 0)
606 cf_util_get_string(child, &conf->client_id);
607 else if (strcasecmp("User", child->key) == 0)
608 cf_util_get_string(child, &conf->username);
609 else if (strcasecmp("Password", child->key) == 0)
610 cf_util_get_string(child, &conf->password);
611 else if (strcasecmp("QoS", child->key) == 0) {
612 int tmp = -1;
613 status = cf_util_get_int(child, &tmp);
614 if ((status != 0) || (tmp < 0) || (tmp > 2))
615 ERROR("mqtt plugin: Not a valid QoS setting.");
616 else
617 conf->qos = tmp;
618 } else if (strcasecmp("Prefix", child->key) == 0)
619 cf_util_get_string(child, &conf->topic_prefix);
620 else if (strcasecmp("StoreRates", child->key) == 0)
621 cf_util_get_boolean(child, &conf->store_rates);
622 else if (strcasecmp("Retain", child->key) == 0)
623 cf_util_get_boolean(child, &conf->retain);
624 else if (strcasecmp("CACert", child->key) == 0)
625 cf_util_get_string(child, &conf->cacertificatefile);
626 else if (strcasecmp("CertificateFile", child->key) == 0)
627 cf_util_get_string(child, &conf->certificatefile);
628 else if (strcasecmp("CertificateKeyFile", child->key) == 0)
629 cf_util_get_string(child, &conf->certificatekeyfile);
630 else if (strcasecmp("TLSProtocol", child->key) == 0)
631 cf_util_get_string(child, &conf->tlsprotocol);
632 else if (strcasecmp("CipherSuite", child->key) == 0)
633 cf_util_get_string(child, &conf->ciphersuite);
634 else
635 ERROR("mqtt plugin: Unknown config option: %s", child->key);
636 }
637
638 ssnprintf(cb_name, sizeof(cb_name), "mqtt/%s", conf->name);
639 plugin_register_write(cb_name, mqtt_write,
640 &(user_data_t){
641 .data = conf,
642 });
643 return 0;
644 } /* mqtt_config_publisher */
645
646 /*
647 * <Subscribe "name">
648 * Host "example.com"
649 * Port 1883
650 * ClientId "collectd"
651 * User "guest"
652 * Password "secret"
653 * Topic "collectd/#"
654 * CACert "ca.pem" Enables TLS if set
655 * CertificateFile "client-cert.pem" optional
656 * CertificateKeyFile "client-key.pem" optional
657 * TLSProtocol "tlsv1.2" optional
658 * </Subscribe>
659 */
660 static int mqtt_config_subscriber(oconfig_item_t *ci) {
661 mqtt_client_conf_t **tmp;
662 mqtt_client_conf_t *conf;
663 int status;
664
665 conf = calloc(1, sizeof(*conf));
666 if (conf == NULL) {
667 ERROR("mqtt plugin: calloc failed.");
668 return -1;
669 }
670 conf->publish = false;
671
672 conf->name = NULL;
673 status = cf_util_get_string(ci, &conf->name);
674 if (status != 0) {
675 mqtt_free(conf);
676 return status;
677 }
678
679 conf->host = strdup(MQTT_DEFAULT_HOST);
680 conf->port = MQTT_DEFAULT_PORT;
681 conf->client_id = NULL;
682 conf->qos = 2;
683 conf->topic = strdup(MQTT_DEFAULT_TOPIC);
684 conf->clean_session = true;
685
686 status = pthread_mutex_init(&conf->lock, NULL);
687 if (status != 0) {
688 mqtt_free(conf);
689 return status;
690 }
691
692 C_COMPLAIN_INIT(&conf->complaint_cantpublish);
693
694 for (int i = 0; i < ci->children_num; i++) {
695 oconfig_item_t *child = ci->children + i;
696 if (strcasecmp("Host", child->key) == 0)
697 cf_util_get_string(child, &conf->host);
698 else if (strcasecmp("Port", child->key) == 0) {
699 status = cf_util_get_port_number(child);
700 if (status < 0)
701 ERROR("mqtt plugin: Invalid port number.");
702 else
703 conf->port = status;
704 } else if (strcasecmp("ClientId", child->key) == 0)
705 cf_util_get_string(child, &conf->client_id);
706 else if (strcasecmp("User", child->key) == 0)
707 cf_util_get_string(child, &conf->username);
708 else if (strcasecmp("Password", child->key) == 0)
709 cf_util_get_string(child, &conf->password);
710 else if (strcasecmp("QoS", child->key) == 0) {
711 int qos = -1;
712 status = cf_util_get_int(child, &qos);
713 if ((status != 0) || (qos < 0) || (qos > 2))
714 ERROR("mqtt plugin: Not a valid QoS setting.");
715 else
716 conf->qos = qos;
717 } else if (strcasecmp("Topic", child->key) == 0)
718 cf_util_get_string(child, &conf->topic);
719 else if (strcasecmp("CleanSession", child->key) == 0)
720 cf_util_get_boolean(child, &conf->clean_session);
721 else if (strcasecmp("CACert", child->key) == 0)
722 cf_util_get_string(child, &conf->cacertificatefile);
723 else if (strcasecmp("CertificateFile", child->key) == 0)
724 cf_util_get_string(child, &conf->certificatefile);
725 else if (strcasecmp("CertificateKeyFile", child->key) == 0)
726 cf_util_get_string(child, &conf->certificatekeyfile);
727 else if (strcasecmp("TLSProtocol", child->key) == 0)
728 cf_util_get_string(child, &conf->tlsprotocol);
729 else if (strcasecmp("CipherSuite", child->key) == 0)
730 cf_util_get_string(child, &conf->ciphersuite);
731 else
732 ERROR("mqtt plugin: Unknown config option: %s", child->key);
733 }
734
735 tmp = realloc(subscribers, sizeof(*subscribers) * (subscribers_num + 1));
736 if (tmp == NULL) {
737 ERROR("mqtt plugin: realloc failed.");
738 mqtt_free(conf);
739 return -1;
740 }
741 subscribers = tmp;
742 subscribers[subscribers_num] = conf;
743 subscribers_num++;
744
745 return 0;
746 } /* mqtt_config_subscriber */
747
748 /*
749 * <Plugin mqtt>
750 * <Publish "name">
751 * # ...
752 * </Publish>
753 * <Subscribe "name">
754 * # ...
755 * </Subscribe>
756 * </Plugin>
757 */
758 static int mqtt_config(oconfig_item_t *ci) {
759 for (int i = 0; i < ci->children_num; i++) {
760 oconfig_item_t *child = ci->children + i;
761
762 if (strcasecmp("Publish", child->key) == 0)
763 mqtt_config_publisher(child);
764 else if (strcasecmp("Subscribe", child->key) == 0)
765 mqtt_config_subscriber(child);
766 else
767 ERROR("mqtt plugin: Unknown config option: %s", child->key);
768 }
769
770 return 0;
771 } /* int mqtt_config */
772
773 static int mqtt_init(void) {
774 mosquitto_lib_init();
775
776 for (size_t i = 0; i < subscribers_num; i++) {
777 int status;
778
779 if (subscribers[i]->loop)
780 continue;
781
782 status = plugin_thread_create(&subscribers[i]->thread,
783 /* func = */ subscribers_thread,
784 /* args = */ subscribers[i],
785 /* name = */ "mqtt");
786 if (status != 0) {
787 ERROR("mqtt plugin: pthread_create failed: %s", STRERRNO);
788 continue;
789 }
790 }
791
792 return 0;
793 } /* mqtt_init */
794
795 void module_register(void) {
796 plugin_register_complex_config("mqtt", mqtt_config);
797 plugin_register_init("mqtt", mqtt_init);
798 } /* void module_register */