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. More...
 
int mqtt_init_auth (const char *, const char *, const char *)
 Init MQTT communication. More...
 
gboolean mqtt_is_initialized (void)
 Get the global init status. More...
 
void mqtt_reset (void)
 Destroy MQTTClient handle and free mqtt_t. More...
 
int mqtt_publish (const char *, const char *)
 Publish a message on topic using the global client. More...
 
int mqtt_publish_single_message_auth (const char *, const char *, const char *, const char *, const char *)
 Send a single message with credentials. More...
 
int mqtt_publish_single_message (const char *, const char *, const char *)
 Send a single message. More...
 
int mqtt_subscribe (const char *)
 subscribes to a single topic. More...
 
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 More...
 
int mqtt_unsubscribe (const char *)
 unsubscribe a single topic. More...
 

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 }

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 
422  mqtt_set_global_client (mqtt);
424 
425  g_debug ("%s: end", __func__);
426  return 0;
427 }

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 }

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 }

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 }

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);
615  mqtt_client_data_destroy (&mqtt);
616 
617  return ret;
618 }

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__);
223  mqtt_t *mqtt = mqtt_get_global_client ();
224 
225  if (mqtt == NULL)
226  return;
227 
228  mqtt_client_destroy (mqtt);
229  mqtt_client_data_destroy (&mqtt);
230 
231  mqtt_set_global_client (NULL);
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 }

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 }

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 {
717  return mqtt_unsubscribe_r (mqtt_get_global_client (), topic);
718 }

References mqtt_get_global_client(), and mqtt_unsubscribe_r().

Here is the call graph for this function:
mqtt_initialized
static gboolean mqtt_initialized
Definition: mqtt.c:50
mqtt_set_client_id
static char * mqtt_set_client_id(mqtt_t *mqtt)
Set a random client ID.
Definition: mqtt.c:277
mqtt_get_global_username
static const char * mqtt_get_global_username()
Get global username.
Definition: mqtt.c:111
mqtt_client_destroy
static void mqtt_client_destroy(mqtt_t *mqtt)
Destroy the MQTTClient client of the mqtt_t.
Definition: mqtt.c:186
mqtt_client_publish
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
mqtt_set_global_client
static void mqtt_set_global_client(mqtt_t *mqtt)
Set global client.
Definition: mqtt.c:151
mqtt_client_data_destroy
static void mqtt_client_data_destroy(mqtt_t **mqtt)
Destroy the mqtt_t data.
Definition: mqtt.c:209
mqtt_connect
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
mqtt_init_auth
int mqtt_init_auth(const char *server_uri, const char *username, const char *password)
Init MQTT communication.
Definition: mqtt.c:382
mqtt_set_global_server_uri
static void mqtt_set_global_server_uri(const char *server_uri_in)
Set the global mqtt server URI.
Definition: mqtt.c:80
mqtt_disconnect
static int mqtt_disconnect(mqtt_t *mqtt)
Disconnect from the Broker.
Definition: mqtt.c:164
mqtt_set_initialized_status
static void mqtt_set_initialized_status(gboolean status)
Set the global init status.
Definition: mqtt.c:58
mqtt_set_global_username
static void mqtt_set_global_username(const char *username)
Set the global mqtt username.
Definition: mqtt.c:102
mqtt_reinit
static void mqtt_reinit()
Reinitializes communication after mqtt_reset was used.
Definition: mqtt.c:434
mqtt_retrieve_message_r
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
mqtt_unsubscribe_r
static int mqtt_unsubscribe_r(mqtt_t *mqtt, const char *topic)
unsubscribe a single topic.
Definition: mqtt.c:689
mqtt_set_global_password
static void mqtt_set_global_password(const char *password)
Set the global mqtt password.
Definition: mqtt.c:122
mqtt_get_global_client
static mqtt_t * mqtt_get_global_client()
Definition: mqtt.c:142
mqtt_get_global_password
static const char * mqtt_get_global_password()
Get global password.
Definition: mqtt.c:131
mqtt_t
Definition: mqtt.c:41
mqtt_subscribe_r
static int mqtt_subscribe_r(mqtt_t *mqtt, int qos, const char *topic)
subscribes to a single topic.
Definition: mqtt.c:637
mqtt_get_global_server_uri
static const char * mqtt_get_global_server_uri()
Get global server URI.
Definition: mqtt.c:91
QOS
#define QOS
Definition: mqtt.c:37
mqtt_t::client_id
char * client_id
Definition: mqtt.c:43
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.
Definition: mqtt.c:562