Greenbone Vulnerability Management Libraries 22.8.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 368 of file mqtt.c.

369{
370 return mqtt_init_auth (server_uri, NULL, NULL);
371}
int mqtt_init_auth(const char *server_uri, const char *username, const char *password)
Init MQTT communication.
Definition: mqtt.c:382

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 382 of file mqtt.c.

384{
385 mqtt_t *mqtt = NULL;
386 const char *g_server_uri;
387 const char *g_username;
388 const char *g_password;
389
390 g_debug ("%s: start", __func__);
391
392 mqtt = g_malloc0 (sizeof (mqtt_t));
393 // Set random uuid as client id
394 if (mqtt_set_client_id (mqtt) == NULL)
395 {
396 g_warning ("%s: Could not set client id.", __func__);
397 g_free (mqtt);
398 mqtt = NULL;
399 return -1;
400 }
401 g_debug ("%s: client id set: %s", __func__, mqtt->client_id);
402 g_server_uri = mqtt_get_global_server_uri ();
403 if (g_server_uri == NULL)
404 mqtt_set_global_server_uri (server_uri);
405
406 g_username = mqtt_get_global_username ();
407 if (g_username == NULL)
408 mqtt_set_global_username (username);
409
410 g_password = mqtt_get_global_password ();
411 if (g_password == NULL)
412 mqtt_set_global_password (password);
413
414 if (mqtt_connect (mqtt, server_uri, username, password))
415 {
416 g_warning ("%s: Unable to connect to MQTT broker.", __func__);
417 g_free (mqtt);
418 mqtt = NULL;
419 return -1;
420 }
421
424
425 g_debug ("%s: end", __func__);
426 return 0;
427}
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:317
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:277
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 510 of file mqtt.c.

511{
512 mqtt_t *mqtt = NULL;
513 int rc = 0;
514
515 if ((mqtt_get_global_client ()) == NULL)
516 mqtt_reinit ();
517 mqtt = mqtt_get_global_client ();
518
519 rc = mqtt_client_publish (mqtt, topic, msg);
520
521 return rc;
522}
static void mqtt_reinit()
Reinitializes communication after mqtt_reset was used.
Definition: mqtt.c:434
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:462

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 539 of file mqtt.c.

541{
542 return mqtt_publish_single_message_auth (server_uri_in, NULL, NULL, topic,
543 msg);
544}
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:562

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 562 of file mqtt.c.

566{
567 const char *server_uri;
568 const char *username = NULL;
569 const char *password = NULL;
570 mqtt_t *mqtt = NULL;
571 int ret = 0;
572
573 // If server_uri is NULL try to get global
574 if (server_uri_in == NULL)
575 {
576 server_uri = mqtt_get_global_server_uri ();
577 if (server_uri == NULL)
578 {
579 g_warning (
580 "%s: No server URI provided and no global server URI available.",
581 __func__);
582 return -1;
583 }
584 }
585 else
586 {
587 server_uri = server_uri_in;
588 }
589
590 if (username_in == NULL || passwd_in == NULL)
591 {
592 username = mqtt_get_global_username ();
593 password = mqtt_get_global_password ();
594 }
595 else
596 {
597 username = username_in;
598 password = passwd_in;
599 }
600
601 mqtt = g_malloc0 (sizeof (mqtt_t));
602 // Set random uuid as client id
603 if (mqtt_set_client_id (mqtt) == NULL)
604 {
605 g_warning ("%s: Could not set client id.", __func__);
606 g_free (mqtt);
607 return -2;
608 }
609
610 mqtt_connect (mqtt, server_uri, username, password);
611 mqtt_client_publish (mqtt, topic, msg);
612
613 mqtt_disconnect (mqtt);
614 mqtt_client_destroy (mqtt);
616
617 return ret;
618}
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:209

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 220 of file mqtt.c.

221{
222 g_debug ("%s: start", __func__);
224
225 if (mqtt == NULL)
226 return;
227
228 mqtt_client_destroy (mqtt);
230
232
233 g_debug ("%s: end", __func__);
234 return;
235}

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 838 of file mqtt.c.

840{
841 return mqtt_retrieve_message_r (mqtt_get_global_client (), topic, topic_len,
842 payload, payload_len, timeout);
843}
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:745

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 670 of file mqtt.c.

671{
672 if ((mqtt_get_global_client ()) == NULL)
673 mqtt_reinit ();
674 return mqtt_subscribe_r (mqtt_get_global_client (), QOS, topic);
675}
#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:637

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 715 of file mqtt.c.

716{
718}
static int mqtt_unsubscribe_r(mqtt_t *mqtt, const char *topic)
unsubscribe a single topic.
Definition: mqtt.c:689

References mqtt_get_global_client(), and mqtt_unsubscribe_r().

Here is the call graph for this function: