|
Greenbone Vulnerability Management Libraries 22.32.0
|
Implementation of API to handle MQTT communication. More...


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 |
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.
Definition in file mqtt.c.
| #define QOS 1 |
Definition at line 37 of file mqtt.c.
Referenced by mqtt_client_publish(), and mqtt_subscribe().
| #define TIMEOUT 10000L |
Definition at line 38 of file mqtt.c.
Referenced by mqtt_client_publish().
|
static |
Destroy the mqtt_t data.
| mqtt | mqtt_t |
Definition at line 201 of file mqtt.c.
Referenced by Ensure(), mqtt_publish_single_message_auth(), and mqtt_reset().

|
static |
Destroy the MQTTClient client of the mqtt_t.
| [in] | mqtt | mqtt_t handle. |
Definition at line 186 of file mqtt.c.
References mqtt_t::client, and MQTTClient_destroy().
Referenced by Ensure(), mqtt_publish_single_message_auth(), and mqtt_reset().


|
static |
Use the provided client to publish message on a topic.
| mqtt | mqtt_t |
| topic | Topic to publish on. |
| msg | Message to publish on queue. |
Definition at line 449 of file mqtt.c.
References mqtt_t::client, QOS, and TIMEOUT.
Referenced by mqtt_publish(), and mqtt_publish_single_message_auth().

|
static |
Make new client and connect to mqtt broker.
| mqtt | Initialized mqtt_t |
| server_uri | Server URI |
| username | Username |
| password | Password |
Definition at line 309 of file mqtt.c.
References G_LOG_DOMAIN, mqtt_create(), and mqtt_set_client().
Referenced by mqtt_init_auth(), and mqtt_publish_single_message_auth().


|
static |
Create a new mqtt client.
| mqtt | mqtt_t |
| address | Address of the broker |
Definition at line 238 of file mqtt.c.
References mqtt_t::client_id, and MQTTClient_destroy().
Referenced by Ensure(), and mqtt_connect().


|
static |
Disconnect from the Broker.
| mqtt | mqtt_t |
Definition at line 164 of file mqtt.c.
References mqtt_t::client.
Referenced by mqtt_publish_single_message_auth().

|
static |
Definition at line 142 of file mqtt.c.
References global_mqtt_client.
Referenced by mqtt_publish(), mqtt_reset(), mqtt_retrieve_message(), mqtt_subscribe(), and mqtt_unsubscribe().

|
static |
Get global password.
Definition at line 131 of file mqtt.c.
References global_password.
Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

|
static |
Get global server URI.
Definition at line 91 of file mqtt.c.
References global_server_uri.
Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

|
static |
Get global username.
Definition at line 111 of file mqtt.c.
References global_username.
Referenced by mqtt_init_auth(), mqtt_publish_single_message_auth(), and mqtt_reinit().

| int mqtt_init | ( | const char * | server_uri | ) |
Init MQTT communication.
| server_uri | Server URI |
Definition at line 361 of file mqtt.c.
References mqtt_init_auth().

| int mqtt_init_auth | ( | const char * | server_uri, |
| const char * | username, | ||
| const char * | password ) |
Init MQTT communication.
| server_uri | Server URI |
| username | Username |
| password | Password |
Definition at line 375 of file mqtt.c.
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().


| gboolean mqtt_is_initialized | ( | void | ) |
Get the global init status.
Definition at line 69 of file mqtt.c.
References mqtt_initialized.
| int mqtt_publish | ( | const char * | topic, |
| const char * | msg ) |
Publish a message on topic using the global client.
| topic | topic |
| msg | message |
Definition at line 498 of file mqtt.c.
References mqtt_client_publish(), mqtt_get_global_client(), and mqtt_reinit().

| 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.
| server_uri_in | Server URI |
| topic | Topic to publish to |
| msg | Message to publish |
Definition at line 527 of file mqtt.c.
References 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.
| server_uri_in | Server URI |
| username_in | Username |
| passwd_in | Password |
| topic | Topic to publish to |
| msg | Message to publish |
Definition at line 550 of file mqtt.c.
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().


|
static |
Reinitializes communication after mqtt_reset was used.
Definition at line 421 of file mqtt.c.
References mqtt_get_global_password(), mqtt_get_global_server_uri(), mqtt_get_global_username(), and mqtt_init_auth().
Referenced by mqtt_publish(), and mqtt_subscribe().


| void mqtt_reset | ( | void | ) |
Destroy MQTTClient handle and free mqtt_t.
Definition at line 212 of file mqtt.c.
References mqtt_client_data_destroy(), mqtt_client_destroy(), mqtt_get_global_client(), and mqtt_set_global_client().

| 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.
| [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. |
Definition at line 837 of file mqtt.c.
References mqtt_get_global_client(), and mqtt_retrieve_message_r().

|
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.
| 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. |
Definition at line 739 of file mqtt.c.
References mqtt_t::client.
Referenced by mqtt_retrieve_message().

|
static |
Set MQTTClient of mqtt_t.
Definition at line 288 of file mqtt.c.
References mqtt_t::client.
Referenced by Ensure(), and mqtt_connect().

|
static |
Set a random client ID.
| mqtt | mqtt_t |
Definition at line 269 of file mqtt.c.
References mqtt_t::client_id, and gvm_uuid_make().
Referenced by Ensure(), mqtt_init_auth(), and mqtt_publish_single_message_auth().


|
static |
Set global client.
Definition at line 151 of file mqtt.c.
References global_mqtt_client.
Referenced by mqtt_init_auth(), and mqtt_reset().

|
static |
Set the global mqtt password.
| password | to set. |
Definition at line 122 of file mqtt.c.
References global_password.
Referenced by mqtt_init_auth().

|
static |
Set the global mqtt server URI.
| server_uri_in | Server uri to set. |
Definition at line 80 of file mqtt.c.
References global_server_uri.
Referenced by mqtt_init_auth().

|
static |
Set the global mqtt username.
| username | to set. |
Definition at line 102 of file mqtt.c.
References global_username.
Referenced by mqtt_init_auth().

|
static |
Set the global init status.
| status | Status of initialization. |
Definition at line 58 of file mqtt.c.
References mqtt_initialized.
Referenced by mqtt_init_auth().

| 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.
| topic | Topic to subscribe to |
Definition at line 660 of file mqtt.c.
References mqtt_get_global_client(), mqtt_reinit(), mqtt_subscribe_r(), and QOS.

|
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.
| mqtt | Contains the mqtt client |
| qos | Quality of service of messages within topic |
| topic | Topic to subscribe to |
Definition at line 625 of file mqtt.c.
References mqtt_t::client.
Referenced by mqtt_subscribe().

| int mqtt_unsubscribe | ( | const char * | topic | ) |
unsubscribe a single topic.
This function unsubscribes global client from a given topic.
| topic | Topic to unsubscribe from |
Definition at line 705 of file mqtt.c.
References mqtt_get_global_client(), and mqtt_unsubscribe_r().

|
static |
unsubscribe a single topic.
This function unsubscribes given client from a given topic.
| mqtt | Contains the mqtt client |
| topic | Topic to unsubscribe from |
Definition at line 679 of file mqtt.c.
References mqtt_t::client.
Referenced by mqtt_unsubscribe().

|
static |
Definition at line 49 of file mqtt.c.
Referenced by mqtt_get_global_client(), and mqtt_set_global_client().
|
static |
Definition at line 48 of file mqtt.c.
Referenced by mqtt_get_global_password(), and mqtt_set_global_password().
|
static |
Definition at line 46 of file mqtt.c.
Referenced by mqtt_get_global_server_uri(), and mqtt_set_global_server_uri().
|
static |
Definition at line 47 of file mqtt.c.
Referenced by mqtt_get_global_username(), and mqtt_set_global_username().
|
static |
Definition at line 50 of file mqtt.c.
Referenced by mqtt_is_initialized(), and mqtt_set_initialized_status().