Greenbone Vulnerability Management Libraries: util/mqtt.c File Reference

Greenbone Vulnerability Management Libraries 23.1.2

Data Structures | Macros | Functions | Variables
mqtt.c File Reference

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

`#include "mqtt.h"`
`#include "uuidutils.h"`
`#include <stdlib.h>`
`#include <string.h>`

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_t * | mqtt_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_t * | global_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.

## Macro Definition Documentation

## ◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN "libgvm util"

## ◆ QOS

#define QOS 1

## ◆ TIMEOUT

#define TIMEOUT 10000L

## Function Documentation

## ◆ mqtt_client_data_destroy()

void mqtt_client_data_destroy | ( | mqtt_t ** | mqtt | ) |
| static

Destroy the mqtt_t data.

Parameters

  mqtt | mqtt_t

## ◆ mqtt_client_destroy()

void mqtt_client_destroy | ( | mqtt_t * | mqtt | ) |
| static

Destroy the MQTTClient client of the mqtt_t.

Parameters

  [in] | mqtt | mqtt_t handle.

## ◆ 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

  mqtt | mqtt_t
  topic | Topic to publish on.
  msg | Message to publish on queue.

Returns
  0 on success, <0 on failure.

## ◆ 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

  mqtt | Initialized mqtt_t
  server_uri | Server URI
  username | Username
  password | Password

Returns
  0 on success, <0 on error.

## ◆ mqtt_create()

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

Create a new mqtt client.

Parameters

  mqtt | mqtt_t
  address | Address of the broker

Returns
  MQTTClient or NULL on error.

## ◆ mqtt_disconnect()

int mqtt_disconnect | ( | mqtt_t * | mqtt | ) |
| static

Disconnect from the Broker.

Parameters

  mqtt | mqtt_t

Returns
  0 on success, -1 on error.

## ◆ mqtt_get_global_client()

mqtt_t * mqtt_get_global_client | ( |  | ) |
| static

Returns
  Get global client.

## ◆ mqtt_get_global_password()

const char * mqtt_get_global_password | ( |  | ) |
| static

Get global password.

## ◆ mqtt_get_global_server_uri()

const char * mqtt_get_global_server_uri | ( |  | ) |
| static

Get global server URI.

Returns
  Server URI, NULL if not found.

## ◆ mqtt_get_global_username()

const char * mqtt_get_global_username | ( |  | ) |
| static

Get global username.

## ◆ mqtt_init()

int mqtt_init | ( | const char * | server_uri | ) |

Init MQTT communication.

Parameters

  server_uri | Server URI

Returns
  0 on success, <0 on error.

## ◆ mqtt_init_auth()

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

Init MQTT communication.

Parameters

  server_uri | Server URI
  username | Username
  password | Password

Returns
  0 on success, <0 on error.

## ◆ mqtt_is_initialized()

gboolean mqtt_is_initialized | ( | void |  | ) |

Get the global init status.

Returns
  Initialization status of mqtt handling.

## ◆ mqtt_publish()

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

Publish a message on topic using the global client.

Parameters

  topic | topic
  msg | message

Returns
  0 on success, <0 on error.

## ◆ 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_in | Server URI
  topic | Topic to publish to
  msg | Message to publish

Returns
  0 on success, <0 on failure.

## ◆ 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_in | Server URI
  username_in | Username
  passwd_in | Password
  topic | Topic to publish to
  msg | Message to publish

Returns
  0 on success, <0 on failure.

## ◆ mqtt_reinit()

void mqtt_reinit | ( |  | ) |
| static

Reinitializes communication after mqtt_reset was used.

## ◆ mqtt_reset()

void mqtt_reset | ( | void |  | ) |

Destroy MQTTClient handle and free mqtt_t.

## ◆ 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] | topic | The 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_len | The length of the topic.
  [out] | payload | The 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_len | The length of the payload.
  | timeout | The length of time to wait for a message in milliseconds.

Returns
  0 on message retrieved, 1 on timeout and -1 on an error.

## ◆ 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

  | mqtt | An already created and connected mqtt client.
  [out] | topic | The 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_len | The length of the topic.
  [out] | payload | The 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_len | The length of the payload.
  | timeout | The 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.

## ◆ 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.

## ◆ mqtt_set_client_id()

char * mqtt_set_client_id | ( | mqtt_t * | mqtt | ) |
| static

Set a random client ID.

Parameters

  mqtt | mqtt_t

Returns
  Client ID which was set, NULL on failure.

## ◆ mqtt_set_global_client()

void mqtt_set_global_client | ( | mqtt_t * | mqtt | ) |
| static

Set global client.

## ◆ mqtt_set_global_password()

void mqtt_set_global_password | ( | const char * | password | ) |
| static

Set the global mqtt password.

Parameters

  password | to set.

## ◆ 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_in | Server uri to set.

## ◆ mqtt_set_global_username()

void mqtt_set_global_username | ( | const char * | username | ) |
| static

Set the global mqtt username.

Parameters

  username | to set.

## ◆ mqtt_set_initialized_status()

void mqtt_set_initialized_status | ( | gboolean | status | ) |
| static

Set the global init status.

Parameters

  status | Status of initialization.

## ◆ 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

  topic | Topic to subscribe to

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

## ◆ 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

  mqtt | Contains the mqtt client
  qos | Quality of service of messages within topic
  topic | Topic to subscribe to

Returns
  0 on success, -1 when given mqtt is not useable, -2 when subscription failed.

## ◆ mqtt_unsubscribe()

int mqtt_unsubscribe | ( | const char * | topic | ) |

unsubscribe a single topic.

This function unsubscribes global client from a given topic.

Parameters

  topic | Topic to unsubscribe from

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

## ◆ 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

  mqtt | Contains the mqtt client
  topic | Topic to unsubscribe from

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

## Variable Documentation

## ◆ global_mqtt_client

mqtt_t* global_mqtt_client = NULL
| static

## ◆ global_password

const char* global_password = NULL
| static

## ◆ global_server_uri

const char* global_server_uri = NULL
| static

## ◆ global_username

const char* global_username = NULL
| static

## ◆ mqtt_initialized

gboolean mqtt_initialized = FALSE
| static