mqtt.c File Reference

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

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

## 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 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)
| Make new client and connect to mqtt broker.
int | mqtt_init (const char *server_uri)
| 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.
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 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()

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

static

Destroy the mqtt_t data.

Parameters

  mqtt | mqtt_t

## mqtt_client_destroy()

static 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()

static 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()

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

static

Make new client and connect to mqtt broker.

Parameters

  mqtt | Initialized mqtt_t

Returns
  0 on success, <0 on error.

## mqtt_create()

static 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()

static 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()

static mqtt_t * mqtt_get_global_client | ( |  | ) |

static

Returns
  Get global client.

## mqtt_get_global_server_uri()

static const char * mqtt_get_global_server_uri | ( |  | ) |

static

Get global server URI.

Returns
  Server URI, NULL if not found.

## 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_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_reinit()

static 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()

static 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()

static 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()

static 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()

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

static

Set global client.

## mqtt_set_global_server_uri()

static 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_initialized_status()

static 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()

static 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_t | 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()

static 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_t | 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_server_uri

const char* global_server_uri = NULL

static

## mqtt_initialized

gboolean mqtt_initialized = FALSE

static