Greenbone Vulnerability Management Libraries 22.32.0
mqtt.h File Reference

Protos for MQTT handling. More...

#include <MQTTClient.h>
#include <glib.h>
Include dependency graph for mqtt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define AUTH_MQTT   1

Functions

int mqtt_init (const char *)
 Init MQTT communication.
int mqtt_init_auth (const char *, const char *, const char *)
 Init MQTT communication.
gboolean mqtt_is_initialized (void)
 Get the global init status.
void mqtt_reset (void)
 Destroy MQTTClient handle and free mqtt_t.
int mqtt_publish (const char *, const char *)
 Publish a message on topic using the global client.
int mqtt_publish_single_message_auth (const char *, const char *, const char *, const char *, const char *)
 Send a single message with credentials.
int mqtt_publish_single_message (const char *, const char *, const char *)
 Send a single message.
int mqtt_subscribe (const char *)
 subscribes to a single topic.
int mqtt_retrieve_message (char **, int *, char **, int *, const unsigned int)
 wait for a given timeout in ms to retrieve any message of subscribed topics
int mqtt_unsubscribe (const char *)
 unsubscribe a single topic.

Detailed Description

Protos for MQTT handling.

Definition in file mqtt.h.

Macro Definition Documentation

◆ AUTH_MQTT

#define AUTH_MQTT   1

Definition at line 17 of file mqtt.h.

Function Documentation

◆ 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
char * client_id
Definition mqtt.c:43

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_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_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}
#define QOS
Definition mqtt.c:37
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_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: