Greenbone Vulnerability Management Libraries 22.32.0
mqtt.c File Reference

Implementation of API to handle MQTT communication. More...

#include "mqtt.h"
#include "uuidutils.h"
#include <stdlib.h>
#include <string.h>
Include dependency graph for mqtt.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mqtt_t

Macros

#define G_LOG_DOMAIN   "libgvm util"
#define QOS   1
#define TIMEOUT   10000L

Functions

static void mqtt_set_initialized_status (gboolean status)
 Set the global init status.
gboolean mqtt_is_initialized ()
 Get the global init status.
static void mqtt_set_global_server_uri (const char *server_uri_in)
 Set the global mqtt server URI.
static const char * mqtt_get_global_server_uri ()
 Get global server URI.
static void mqtt_set_global_username (const char *username)
 Set the global mqtt username.
static const char * mqtt_get_global_username ()
 Get global username.
static void mqtt_set_global_password (const char *password)
 Set the global mqtt password.
static const char * mqtt_get_global_password ()
 Get global password.
static mqtt_tmqtt_get_global_client ()
static void mqtt_set_global_client (mqtt_t *mqtt)
 Set global client.
static int mqtt_disconnect (mqtt_t *mqtt)
 Disconnect from the Broker.
static void mqtt_client_destroy (mqtt_t *mqtt)
 Destroy the MQTTClient client of the mqtt_t.
static void mqtt_client_data_destroy (mqtt_t **mqtt)
 Destroy the mqtt_t data.
void mqtt_reset ()
 Destroy MQTTClient handle and free mqtt_t.
static MQTTClient mqtt_create (mqtt_t *mqtt, const char *address)
 Create a new mqtt client.
static char * mqtt_set_client_id (mqtt_t *mqtt)
 Set a random client ID.
static int mqtt_set_client (mqtt_t *mqtt, MQTTClient client)
 Set MQTTClient of mqtt_t.
static int mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username, const char *password)
 Make new client and connect to mqtt broker.
int mqtt_init (const char *server_uri)
 Init MQTT communication.
int mqtt_init_auth (const char *server_uri, const char *username, const char *password)
 Init MQTT communication.
static void mqtt_reinit ()
 Reinitializes communication after mqtt_reset was used.
static int mqtt_client_publish (mqtt_t *mqtt, const char *topic, const char *msg)
 Use the provided client to publish message on a topic.
int mqtt_publish (const char *topic, const char *msg)
 Publish a message on topic using the global client.
int mqtt_publish_single_message (const char *server_uri_in, const char *topic, const char *msg)
 Send a single message.
int mqtt_publish_single_message_auth (const char *server_uri_in, const char *username_in, const char *passwd_in, const char *topic, const char *msg)
 Send a single message with credentials.
static int mqtt_subscribe_r (mqtt_t *mqtt, int qos, const char *topic)
 subscribes to a single topic.
int mqtt_subscribe (const char *topic)
 subscribes to a single topic.
static int mqtt_unsubscribe_r (mqtt_t *mqtt, const char *topic)
 unsubscribe a single topic.
int mqtt_unsubscribe (const char *topic)
 unsubscribe a single topic.
static int mqtt_retrieve_message_r (mqtt_t *mqtt, char **topic, int *topic_len, char **payload, int *payload_len, const unsigned int timeout)
 wait for a given timeout in ms to retrieve any message of subscribed topics
int mqtt_retrieve_message (char **topic, int *topic_len, char **payload, int *payload_len, const unsigned int timeout)
 wait for a given timeout in ms to retrieve any message of subscribed topics

Variables

static const char * global_server_uri = NULL
static const char * global_username = NULL
static const char * global_password = NULL
static mqtt_tglobal_mqtt_client = NULL
static gboolean mqtt_initialized = FALSE

Detailed Description

Implementation of API to handle MQTT communication.

This file contains all methods to handle MQTT communication.

Before communicating via MQTT a handle has to be created and a connection established. This is done by calling mqtt_init(). Mmessages can be published via mqtt_publish() afterwards.

mqtt_init() should be called only once at program init. After forking mqtt_reset() has to be called in the child. mqtt_publish() can be used after mqtt_reset(). No additional mqtt_init() is needed. A new connection will be established on first call to publish for the current process.

mqtt_publish_single_message() is a convenience function for sending single messages. Do not send repeated messages via this function as a new connection is established every call.

Definition in file mqtt.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm util"

Definition at line 35 of file mqtt.c.

◆ QOS

#define QOS   1

Definition at line 37 of file mqtt.c.

Referenced by mqtt_client_publish(), and mqtt_subscribe().

◆ TIMEOUT

#define TIMEOUT   10000L

Definition at line 38 of file mqtt.c.

Referenced by mqtt_client_publish().

Function Documentation

◆ mqtt_client_data_destroy()

void mqtt_client_data_destroy ( mqtt_t ** mqtt)
static

Destroy the mqtt_t data.

Parameters
mqttmqtt_t

Definition at line 201 of file mqtt.c.

202{
203 g_free ((*mqtt)->client_id);
204 g_free (*mqtt);
205 *mqtt = NULL;
206}

Referenced by Ensure(), mqtt_publish_single_message_auth(), and mqtt_reset().

Here is the caller graph for this function:

◆ mqtt_client_destroy()

void mqtt_client_destroy ( mqtt_t * mqtt)
static

Destroy the MQTTClient client of the mqtt_t.

Parameters
[in]mqttmqtt_t handle.

Definition at line 186 of file mqtt.c.

187{
188 if (mqtt && mqtt->client)
189 {
190 MQTTClient_destroy (&mqtt->client);
191 mqtt->client = NULL;
192 }
193}
void MQTTClient_destroy(MQTTClient *client)
Definition mqtt_tests.c:15
void * client
Definition mqtt.c:42

References mqtt_t::client, and MQTTClient_destroy().

Referenced by Ensure(), mqtt_publish_single_message_auth(), and mqtt_reset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_client_publish()

int mqtt_client_publish ( mqtt_t * mqtt,
const char * topic,
const char * msg )
static

Use the provided client to publish message on a topic.

Parameters
mqttmqtt_t
topicTopic to publish on.
msgMessage to publish on queue.
Returns
0 on success, <0 on failure.

Definition at line 449 of file mqtt.c.

450{
451 MQTTClient client;
452 MQTTClient_message pubmsg = MQTTClient_message_initializer;
453 MQTTClient_deliveryToken token;
454 MQTTResponse resp;
455 int rc;
456
457 client = mqtt->client;
458 if (client == NULL)
459 {
460 return -1;
461 }
462
463 pubmsg.payload = (char *) msg;
464 pubmsg.payloadlen = (int) strlen (msg);
465 pubmsg.qos = QOS;
466 pubmsg.retained = 0;
467
468 resp = MQTTClient_publishMessage5 (client, topic, &pubmsg, &token);
469 rc = resp.reasonCode;
470 if (rc != MQTTCLIENT_SUCCESS)
471 {
472 g_warning ("Failed to connect: %s", MQTTClient_strerror (rc));
473 MQTTResponse_free (resp);
474 return -2;
475 }
476
477 rc = MQTTClient_waitForCompletion (client, token, TIMEOUT);
478 if (rc != MQTTCLIENT_SUCCESS)
479 {
480 g_debug ("Message '%s' with delivery token %d could not be "
481 "published on topic %s",
482 msg, token, topic);
483 }
484
485 MQTTResponse_free (resp);
486 return rc;
487}
#define QOS
Definition mqtt.c:37
#define TIMEOUT
Definition mqtt.c:38

References mqtt_t::client, QOS, and TIMEOUT.

Referenced by mqtt_publish(), and mqtt_publish_single_message_auth().

Here is the caller graph for this function:

◆ mqtt_connect()

int mqtt_connect ( mqtt_t * mqtt,
const char * server_uri,
const char * username,
const char * password )
static

Make new client and connect to mqtt broker.

Parameters
mqttInitialized mqtt_t
server_uriServer URI
usernameUsername
passwordPassword
Returns
0 on success, <0 on error.

Definition at line 309 of file mqtt.c.

311{
312 int rc;
313 MQTTClient client;
314 MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer5;
315 MQTTProperties connect_properties = MQTTProperties_initializer;
316 MQTTResponse resp;
317
318 if (mqtt == NULL)
319 return -1;
320
321 client = mqtt_create (mqtt, server_uri);
322 if (!client)
323 return -2;
324
325 conn_opts.keepAliveInterval = 0;
326 conn_opts.cleanstart = 1;
327 conn_opts.MQTTVersion = MQTTVERSION_5;
328
329 if (username != NULL && password != NULL)
330 {
331 conn_opts.username = username;
332 conn_opts.password = password;
333 }
334
335 resp = MQTTClient_connect5 (client, &conn_opts, &connect_properties, NULL);
336 rc = resp.reasonCode;
337 MQTTProperties_free (&connect_properties);
338 if (rc != MQTTCLIENT_SUCCESS)
339 {
340 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
341 "%s: mqtt connection error to %s: %s", __func__, server_uri,
342 MQTTClient_strerror (rc));
343 MQTTResponse_free (resp);
344 return -3;
345 }
346
347 mqtt_set_client (mqtt, client);
348
349 MQTTResponse_free (resp);
350 return 0;
351}
#define G_LOG_DOMAIN
GLib log domain.
Definition array.c:17
static int mqtt_set_client(mqtt_t *mqtt, MQTTClient client)
Set MQTTClient of mqtt_t.
Definition mqtt.c:288
static MQTTClient mqtt_create(mqtt_t *mqtt, const char *address)
Create a new mqtt client.
Definition mqtt.c:238

References G_LOG_DOMAIN, mqtt_create(), and mqtt_set_client().

Referenced by mqtt_init_auth(), and mqtt_publish_single_message_auth().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_create()

MQTTClient mqtt_create ( mqtt_t * mqtt,
const char * address )
static

Create a new mqtt client.

Parameters
mqttmqtt_t
addressAddress of the broker
Returns
MQTTClient or NULL on error.

Definition at line 238 of file mqtt.c.

239{
240 MQTTClient client;
241 MQTTClient_createOptions create_opts = MQTTClient_createOptions_initializer;
242 create_opts.MQTTVersion = MQTTVERSION_5;
243
244 if (mqtt == NULL || mqtt->client_id == NULL)
245 return NULL;
246
247 int rc = MQTTClient_createWithOptions (&client, address, mqtt->client_id,
248 MQTTCLIENT_PERSISTENCE_NONE, NULL,
249 &create_opts);
250
251 if (rc != MQTTCLIENT_SUCCESS)
252 {
253 g_warning ("%s: Error creating MQTTClient: %s", __func__,
254 MQTTClient_strerror (rc));
255 MQTTClient_destroy (&client);
256 return NULL;
257 }
258 return client;
259}
char * client_id
Definition mqtt.c:43

References mqtt_t::client_id, and MQTTClient_destroy().

Referenced by Ensure(), and mqtt_connect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_disconnect()

int mqtt_disconnect ( mqtt_t * mqtt)
static

Disconnect from the Broker.

Parameters
mqttmqtt_t
Returns
0 on success, -1 on error.

Definition at line 164 of file mqtt.c.

165{
166 int rc;
167
168 rc = MQTTClient_disconnect5 (mqtt->client, 200,
169 MQTTREASONCODE_NORMAL_DISCONNECTION, NULL);
170 if (rc != MQTTCLIENT_SUCCESS)
171 {
172 g_warning ("Failed to disconnect: %s", MQTTClient_strerror (rc));
173 return -1;
174 }
175
176 return 0;
177}

References mqtt_t::client.

Referenced by mqtt_publish_single_message_auth().

Here is the caller graph for this function:

◆ mqtt_get_global_client()

mqtt_t * mqtt_get_global_client ( )
static
Returns
Get global client.

Definition at line 142 of file mqtt.c.

143{
144 return global_mqtt_client;
145}
static mqtt_t * global_mqtt_client
Definition mqtt.c:49

References global_mqtt_client.

Referenced by mqtt_publish(), mqtt_reset(), mqtt_retrieve_message(), mqtt_subscribe(), and mqtt_unsubscribe().

Here is the caller graph for this function:

◆ mqtt_get_global_password()

const char * mqtt_get_global_password ( )
static

Get global password.

Definition at line 131 of file mqtt.c.

132{
133 return global_password;
134}
static const char * global_password
Definition mqtt.c:48

References global_password.

Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

Here is the caller graph for this function:

◆ mqtt_get_global_server_uri()

const char * mqtt_get_global_server_uri ( )
static

Get global server URI.

Returns
Server URI, NULL if not found.

Definition at line 91 of file mqtt.c.

92{
93 return global_server_uri;
94}
static const char * global_server_uri
Definition mqtt.c:46

References global_server_uri.

Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

Here is the caller graph for this function:

◆ mqtt_get_global_username()

const char * mqtt_get_global_username ( )
static

Get global username.

Definition at line 111 of file mqtt.c.

112{
113 return global_username;
114}
static const char * global_username
Definition mqtt.c:47

References global_username.

Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

Here is the caller graph for this function:

◆ mqtt_init()

int mqtt_init ( const char * server_uri)

Init MQTT communication.

Parameters
server_uriServer URI
Returns
0 on success, <0 on error.

Definition at line 361 of file mqtt.c.

362{
363 return mqtt_init_auth (server_uri, NULL, NULL);
364}
int mqtt_init_auth(const char *server_uri, const char *username, const char *password)
Init MQTT communication.
Definition mqtt.c:375

References mqtt_init_auth().

Here is the call graph for this function:

◆ mqtt_init_auth()

int mqtt_init_auth ( const char * server_uri,
const char * username,
const char * password )

Init MQTT communication.

Parameters
server_uriServer URI
usernameUsername
passwordPassword
Returns
0 on success, <0 on error.

Definition at line 375 of file mqtt.c.

377{
378 mqtt_t *mqtt = NULL;
379
380 g_debug ("%s: start", __func__);
381
382 mqtt = g_malloc0 (sizeof (mqtt_t));
383 // Set random uuid as client id
384 if (mqtt_set_client_id (mqtt) == NULL)
385 {
386 g_warning ("%s: Could not set client id.", __func__);
387 g_free (mqtt);
388 mqtt = NULL;
389 return -1;
390 }
391 g_debug ("%s: client id set: %s", __func__, mqtt->client_id);
392 if (mqtt_get_global_server_uri () == NULL)
393 mqtt_set_global_server_uri (server_uri);
394
395 if (mqtt_get_global_username () == NULL)
396 mqtt_set_global_username (username);
397
398 if (mqtt_get_global_password () == NULL)
399 mqtt_set_global_password (password);
400
401 if (mqtt_connect (mqtt, server_uri, username, password))
402 {
403 g_warning ("%s: Unable to connect to MQTT broker.", __func__);
404 g_free (mqtt);
405 mqtt = NULL;
406 return -1;
407 }
408
411
412 g_debug ("%s: end", __func__);
413 return 0;
414}
static int mqtt_connect(mqtt_t *mqtt, const char *server_uri, const char *username, const char *password)
Make new client and connect to mqtt broker.
Definition mqtt.c:309
static const char * mqtt_get_global_server_uri()
Get global server URI.
Definition mqtt.c:91
static void mqtt_set_global_password(const char *password)
Set the global mqtt password.
Definition mqtt.c:122
static void mqtt_set_global_client(mqtt_t *mqtt)
Set global client.
Definition mqtt.c:151
static void mqtt_set_initialized_status(gboolean status)
Set the global init status.
Definition mqtt.c:58
static const char * mqtt_get_global_password()
Get global password.
Definition mqtt.c:131
static void mqtt_set_global_username(const char *username)
Set the global mqtt username.
Definition mqtt.c:102
static char * mqtt_set_client_id(mqtt_t *mqtt)
Set a random client ID.
Definition mqtt.c:269
static void mqtt_set_global_server_uri(const char *server_uri_in)
Set the global mqtt server URI.
Definition mqtt.c:80
static const char * mqtt_get_global_username()
Get global username.
Definition mqtt.c:111
Definition mqtt.c:41

References mqtt_t::client_id, mqtt_connect(), mqtt_get_global_password(), mqtt_get_global_server_uri(), mqtt_get_global_username(), mqtt_set_client_id(), mqtt_set_global_client(), mqtt_set_global_password(), mqtt_set_global_server_uri(), mqtt_set_global_username(), and mqtt_set_initialized_status().

Referenced by mqtt_init(), and mqtt_reinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_is_initialized()

gboolean mqtt_is_initialized ( void )

Get the global init status.

Returns
Initialization status of mqtt handling.

Definition at line 69 of file mqtt.c.

70{
71 return mqtt_initialized;
72}
static gboolean mqtt_initialized
Definition mqtt.c:50

References mqtt_initialized.

◆ mqtt_publish()

int mqtt_publish ( const char * topic,
const char * msg )

Publish a message on topic using the global client.

Parameters
topictopic
msgmessage
Returns
0 on success, <0 on error.

Definition at line 498 of file mqtt.c.

499{
500 mqtt_t *mqtt = NULL;
501 int rc = 0;
502
503 if ((mqtt_get_global_client ()) == NULL)
504 mqtt_reinit ();
505 mqtt = mqtt_get_global_client ();
506
507 rc = mqtt_client_publish (mqtt, topic, msg);
508
509 return rc;
510}
static void mqtt_reinit()
Reinitializes communication after mqtt_reset was used.
Definition mqtt.c:421
static mqtt_t * mqtt_get_global_client()
Definition mqtt.c:142
static int mqtt_client_publish(mqtt_t *mqtt, const char *topic, const char *msg)
Use the provided client to publish message on a topic.
Definition mqtt.c:449

References mqtt_client_publish(), mqtt_get_global_client(), and mqtt_reinit().

Here is the call graph for this function:

◆ mqtt_publish_single_message()

int mqtt_publish_single_message ( const char * server_uri_in,
const char * topic,
const char * msg )

Send a single message.

This functions creates a mqtt handle, connects, sends the message, closes the connection and destroys the handler. This function should not be chosen for repeated and frequent messaging. Its meant for error messages and the likes emitted by openvas.

Parameters
server_uri_inServer URI
topicTopic to publish to
msgMessage to publish
Returns
0 on success, <0 on failure.

Definition at line 527 of file mqtt.c.

529{
530 return mqtt_publish_single_message_auth (server_uri_in, NULL, NULL, topic,
531 msg);
532}
int mqtt_publish_single_message_auth(const char *server_uri_in, const char *username_in, const char *passwd_in, const char *topic, const char *msg)
Send a single message with credentials.
Definition mqtt.c:550

References mqtt_publish_single_message_auth().

Here is the call graph for this function:

◆ mqtt_publish_single_message_auth()

int mqtt_publish_single_message_auth ( const char * server_uri_in,
const char * username_in,
const char * passwd_in,
const char * topic,
const char * msg )

Send a single message with credentials.

This functions creates a mqtt handle, connects, sends the message, closes the connection and destroys the handler. This function should not be chosen for repeated and frequent messaging. Its meant for error messages and the likes emitted by openvas.

Parameters
server_uri_inServer URI
username_inUsername
passwd_inPassword
topicTopic to publish to
msgMessage to publish
Returns
0 on success, <0 on failure.

Definition at line 550 of file mqtt.c.

554{
555 const char *server_uri;
556 const char *username = NULL;
557 const char *password = NULL;
558 mqtt_t *mqtt = NULL;
559 int ret = 0;
560
561 // If server_uri is NULL try to get global
562 if (server_uri_in == NULL)
563 {
564 server_uri = mqtt_get_global_server_uri ();
565 if (server_uri == NULL)
566 {
567 g_warning (
568 "%s: No server URI provided and no global server URI available.",
569 __func__);
570 return -1;
571 }
572 }
573 else
574 {
575 server_uri = server_uri_in;
576 }
577
578 if (username_in == NULL || passwd_in == NULL)
579 {
580 username = mqtt_get_global_username ();
581 password = mqtt_get_global_password ();
582 }
583 else
584 {
585 username = username_in;
586 password = passwd_in;
587 }
588
589 mqtt = g_malloc0 (sizeof (mqtt_t));
590 // Set random uuid as client id
591 if (mqtt_set_client_id (mqtt) == NULL)
592 {
593 g_warning ("%s: Could not set client id.", __func__);
594 g_free (mqtt);
595 return -2;
596 }
597
598 mqtt_connect (mqtt, server_uri, username, password);
599 mqtt_client_publish (mqtt, topic, msg);
600
601 mqtt_disconnect (mqtt);
602 mqtt_client_destroy (mqtt);
604
605 return ret;
606}
static int mqtt_disconnect(mqtt_t *mqtt)
Disconnect from the Broker.
Definition mqtt.c:164
static void mqtt_client_destroy(mqtt_t *mqtt)
Destroy the MQTTClient client of the mqtt_t.
Definition mqtt.c:186
static void mqtt_client_data_destroy(mqtt_t **mqtt)
Destroy the mqtt_t data.
Definition mqtt.c:201

References mqtt_client_data_destroy(), mqtt_client_destroy(), mqtt_client_publish(), mqtt_connect(), mqtt_disconnect(), mqtt_get_global_password(), mqtt_get_global_server_uri(), mqtt_get_global_username(), and mqtt_set_client_id().

Referenced by mqtt_publish_single_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_reinit()

void mqtt_reinit ( )
static

Reinitializes communication after mqtt_reset was used.

Definition at line 421 of file mqtt.c.

422{
423 const char *server_uri;
424 const char *username;
425 const char *password;
426
427 server_uri = mqtt_get_global_server_uri ();
428 if (server_uri == NULL)
429 {
430 g_warning ("%s: mqtt_init() has to be called once at program start "
431 "else the server URI is not set. ",
432 __func__);
433 }
434 username = mqtt_get_global_username ();
435 password = mqtt_get_global_password ();
436 mqtt_init_auth (server_uri, username, password);
437}

References mqtt_get_global_password(), mqtt_get_global_server_uri(), mqtt_get_global_username(), and mqtt_init_auth().

Referenced by mqtt_publish(), and mqtt_subscribe().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_reset()

void mqtt_reset ( void )

Destroy MQTTClient handle and free mqtt_t.

Definition at line 212 of file mqtt.c.

213{
214 g_debug ("%s: start", __func__);
216
217 if (mqtt == NULL)
218 return;
219
220 mqtt_client_destroy (mqtt);
222
224
225 g_debug ("%s: end", __func__);
226 return;
227}

References mqtt_client_data_destroy(), mqtt_client_destroy(), mqtt_get_global_client(), and mqtt_set_global_client().

Here is the call graph for this function:

◆ mqtt_retrieve_message()

int mqtt_retrieve_message ( char ** topic,
int * topic_len,
char ** payload,
int * payload_len,
const unsigned int timeout )

wait for a given timeout in ms to retrieve any message of subscribed topics

This function performs a synchronous receive of incoming messages. Using this function allows a single-threaded client subscriber application to be written. When called, this function blocks until the next message arrives or the specified timeout expires.

Important note: The application must free() the memory allocated to the topic and payload when processing is complete.

Parameters
[out]topicThe address of a pointer to a topic. This function allocates the memory for the topic and returns it to the application by setting topic to point to the topic.
[out]topic_lenThe length of the topic.
[out]payloadThe address of a pointer to the received message. This function allocates the memory for the payload and returns it to the application by setting payload to point to the received message. The pointer is set to NULL if the timeout expires.
[out]payload_lenThe length of the payload.
timeoutThe length of time to wait for a message in milliseconds.
Returns
0 on message retrieved, 1 on timeout and -1 on an error.

Definition at line 837 of file mqtt.c.

839{
840 return mqtt_retrieve_message_r (mqtt_get_global_client (), topic, topic_len,
841 payload, payload_len, timeout);
842}
static int mqtt_retrieve_message_r(mqtt_t *mqtt, char **topic, int *topic_len, char **payload, int *payload_len, const unsigned int timeout)
wait for a given timeout in ms to retrieve any message of subscribed topics
Definition mqtt.c:739

References mqtt_get_global_client(), and mqtt_retrieve_message_r().

Here is the call graph for this function:

◆ mqtt_retrieve_message_r()

int mqtt_retrieve_message_r ( mqtt_t * mqtt,
char ** topic,
int * topic_len,
char ** payload,
int * payload_len,
const unsigned int timeout )
static

wait for a given timeout in ms to retrieve any message of subscribed topics

This function performs a synchronous receive of incoming messages. Using this function allows a single-threaded client subscriber application to be written. When called, this function blocks until the next message arrives or the specified timeout expires.

Important note: The application must free() the memory allocated to the topic and payload when processing is complete.

Parameters
mqttAn already created and connected mqtt client.
[out]topicThe address of a pointer to a topic. This function allocates the memory for the topic and returns it to the application by setting topic to point to the topic.
[out]topic_lenThe length of the topic.
[out]payloadThe address of a pointer to the received message. This function allocates the memory for the payload and returns it to the application by setting payload to point to the received message. The pointer is set to NULL if the timeout expires.
[out]payload_lenThe length of the payload.
timeoutThe length of time to wait for a message in milliseconds.
Returns
0 on message retrieved, 1 on no message retrieved and -1 on an error.

Definition at line 739 of file mqtt.c.

742{
743 int rc = -1;
744 char *tmp = NULL;
745 MQTTClient_message *message = NULL;
746 if (mqtt == NULL || mqtt->client == NULL)
747 {
748 g_warning ("mqtt is not initialized.");
749 goto exit;
750 }
751 // copy from tmp into topic to make free work as usual and don't force the
752 // user to double check topic_len and topic
753 rc = MQTTClient_receive (mqtt->client, &tmp, topic_len, &message, timeout);
754 if (rc == MQTTCLIENT_SUCCESS || rc == MQTTCLIENT_TOPICNAME_TRUNCATED)
755 {
756 if (message)
757 {
758 g_debug ("%s: got message %s (%d) on topic %s (%d) \n", __func__,
759 (char *) message->payload, message->payloadlen, tmp,
760 *topic_len);
761
762 *topic = calloc (1, *topic_len);
763 if (*topic == NULL)
764 {
765 goto exit;
766 }
767 rc = 0;
768 if ((memcpy (*topic, tmp, *topic_len)) == NULL)
769 {
770 g_warning ("unable to copy topic");
771 rc = -1;
772 goto exit;
773 }
774
775 *payload_len = message->payloadlen;
776 *payload = calloc (1, message->payloadlen);
777 if ((memcpy (*payload, (char *) message->payload,
778 message->payloadlen))
779 == NULL)
780 {
781 g_warning ("unable to copy payload");
782 rc = -1;
783 goto exit;
784 }
785 }
786 else
787 {
788 rc = 1;
789 *payload = NULL;
790 *payload_len = 0;
791 *topic = NULL;
792 *topic_len = 0;
793 }
794 }
795 else
796 {
797 rc = -1;
798 }
799
800exit:
801 if (message != NULL)
802 MQTTClient_freeMessage (&message);
803 if (tmp != NULL)
804 MQTTClient_free (tmp);
805
806 return rc;
807}

References mqtt_t::client.

Referenced by mqtt_retrieve_message().

Here is the caller graph for this function:

◆ mqtt_set_client()

int mqtt_set_client ( mqtt_t * mqtt,
MQTTClient client )
static

Set MQTTClient of mqtt_t.

Returns
0 on success, -1 on failure.

Definition at line 288 of file mqtt.c.

289{
290 if (mqtt == NULL)
291 {
292 return -1;
293 }
294 mqtt->client = client;
295 return 0;
296}

References mqtt_t::client.

Referenced by Ensure(), and mqtt_connect().

Here is the caller graph for this function:

◆ mqtt_set_client_id()

char * mqtt_set_client_id ( mqtt_t * mqtt)
static

Set a random client ID.

Parameters
mqttmqtt_t
Returns
Client ID which was set, NULL on failure.

Definition at line 269 of file mqtt.c.

270{
271 if (mqtt == NULL)
272 return NULL;
273
274 char *uuid;
275
276 uuid = gvm_uuid_make ();
277 mqtt->client_id = uuid;
278
279 return uuid;
280}
char * gvm_uuid_make(void)
Make a new universal identifier.
Definition uuidutils.c:30

References mqtt_t::client_id, and gvm_uuid_make().

Referenced by Ensure(), mqtt_init_auth(), and mqtt_publish_single_message_auth().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mqtt_set_global_client()

void mqtt_set_global_client ( mqtt_t * mqtt)
static

Set global client.

Definition at line 151 of file mqtt.c.

152{
153 global_mqtt_client = mqtt;
154}

References global_mqtt_client.

Referenced by mqtt_init_auth(), and mqtt_reset().

Here is the caller graph for this function:

◆ mqtt_set_global_password()

void mqtt_set_global_password ( const char * password)
static

Set the global mqtt password.

Parameters
passwordto set.

Definition at line 122 of file mqtt.c.

123{
124 global_password = password;
125}

References global_password.

Referenced by mqtt_init_auth().

Here is the caller graph for this function:

◆ mqtt_set_global_server_uri()

void mqtt_set_global_server_uri ( const char * server_uri_in)
static

Set the global mqtt server URI.

Parameters
server_uri_inServer uri to set.

Definition at line 80 of file mqtt.c.

81{
82 global_server_uri = server_uri_in;
83}

References global_server_uri.

Referenced by mqtt_init_auth().

Here is the caller graph for this function:

◆ mqtt_set_global_username()

void mqtt_set_global_username ( const char * username)
static

Set the global mqtt username.

Parameters
usernameto set.

Definition at line 102 of file mqtt.c.

103{
104 global_username = username;
105}

References global_username.

Referenced by mqtt_init_auth().

Here is the caller graph for this function:

◆ mqtt_set_initialized_status()

void mqtt_set_initialized_status ( gboolean status)
static

Set the global init status.

Parameters
statusStatus of initialization.

Definition at line 58 of file mqtt.c.

59{
60 mqtt_initialized = status;
61}

References mqtt_initialized.

Referenced by mqtt_init_auth().

Here is the caller graph for this function:

◆ mqtt_subscribe()

int mqtt_subscribe ( const char * topic)

subscribes to a single topic.

mqtt_subscribe uses global mqtt_t to subscribe with global qos to given topic.

To be able to subscribe to a topic the client needs to be connected to a broker. To do that call mqtt_init before mqtt_subscribe.

Parameters
topicTopic to subscribe to
Returns
0 on success, -1 when mqtt is not initialized, -2 when subscription failed.

Definition at line 660 of file mqtt.c.

661{
662 if ((mqtt_get_global_client ()) == NULL)
663 mqtt_reinit ();
664 return mqtt_subscribe_r (mqtt_get_global_client (), QOS, topic);
665}
static int mqtt_subscribe_r(mqtt_t *mqtt, int qos, const char *topic)
subscribes to a single topic.
Definition mqtt.c:625

References mqtt_get_global_client(), mqtt_reinit(), mqtt_subscribe_r(), and QOS.

Here is the call graph for this function:

◆ mqtt_subscribe_r()

int mqtt_subscribe_r ( mqtt_t * mqtt,
int qos,
const char * topic )
static

subscribes to a single topic.

mqtt_subscribe_r uses given mqtt_t to subscribe with given qos to given topic.

To be able to subscribe to a topic the client needs to be connected to a broker.

Parameters
mqttContains the mqtt client
qosQuality of service of messages within topic
topicTopic to subscribe to
Returns
0 on success, -1 when given mqtt is not useable, -2 when subscription failed.

Definition at line 625 of file mqtt.c.

626{
627 if (mqtt == NULL || mqtt->client == NULL)
628 {
629 return -1;
630 }
631 MQTTSubscribe_options opts = MQTTSubscribe_options_initializer;
632 MQTTProperties props = MQTTProperties_initializer;
633 MQTTResponse resp =
634 MQTTClient_subscribe5 (mqtt->client, topic, qos, &opts, &props);
635 if (resp.reasonCode != MQTTREASONCODE_GRANTED_QOS_1)
636 {
637 MQTTResponse_free (resp);
638 return -2;
639 }
640 MQTTResponse_free (resp);
641 return 0;
642}

References mqtt_t::client.

Referenced by mqtt_subscribe().

Here is the caller graph for this function:

◆ mqtt_unsubscribe()

int mqtt_unsubscribe ( const char * topic)

unsubscribe a single topic.

This function unsubscribes global client from a given topic.

Parameters
topicTopic to unsubscribe from
Returns
0 on success, -1 when given mqtt is not useable, -2 when unsubscribe failed.

Definition at line 705 of file mqtt.c.

706{
708}
static int mqtt_unsubscribe_r(mqtt_t *mqtt, const char *topic)
unsubscribe a single topic.
Definition mqtt.c:679

References mqtt_get_global_client(), and mqtt_unsubscribe_r().

Here is the call graph for this function:

◆ mqtt_unsubscribe_r()

int mqtt_unsubscribe_r ( mqtt_t * mqtt,
const char * topic )
static

unsubscribe a single topic.

This function unsubscribes given client from a given topic.

Parameters
mqttContains the mqtt client
topicTopic to unsubscribe from
Returns
0 on success, -1 when given mqtt is not useable, -2 when unsubscribe failed.

Definition at line 679 of file mqtt.c.

680{
681 if (mqtt == NULL || mqtt->client == NULL)
682 {
683 return -1;
684 }
685
686 if (MQTTClient_unsubscribe (mqtt->client, topic) != MQTTCLIENT_SUCCESS)
687 {
688 return -2;
689 }
690
691 return 0;
692}

References mqtt_t::client.

Referenced by mqtt_unsubscribe().

Here is the caller graph for this function:

Variable Documentation

◆ global_mqtt_client

mqtt_t* global_mqtt_client = NULL
static

Definition at line 49 of file mqtt.c.

Referenced by mqtt_get_global_client(), and mqtt_set_global_client().

◆ global_password

const char* global_password = NULL
static

Definition at line 48 of file mqtt.c.

Referenced by mqtt_get_global_password(), and mqtt_set_global_password().

◆ global_server_uri

const char* global_server_uri = NULL
static

Definition at line 46 of file mqtt.c.

Referenced by mqtt_get_global_server_uri(), and mqtt_set_global_server_uri().

◆ global_username

const char* global_username = NULL
static

Definition at line 47 of file mqtt.c.

Referenced by mqtt_get_global_username(), and mqtt_set_global_username().

◆ mqtt_initialized

gboolean mqtt_initialized = FALSE
static

Definition at line 50 of file mqtt.c.

Referenced by mqtt_is_initialized(), and mqtt_set_initialized_status().