Greenbone Vulnerability Management Libraries 22.8.0
|
Implementation of API to handle MQTT communication. More...
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.
#define G_LOG_DOMAIN "libgvm util" |
#define QOS 1 |
#define TIMEOUT 10000L |
|
static |
|
static |
|
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. |
|
static |
Make new client and connect to mqtt broker.
mqtt | Initialized mqtt_t |
server_uri | Server URI |
username | Username |
password | Password |
|
static |
Create a new mqtt client.
mqtt | mqtt_t |
address | address of the broker |
|
static |
|
static |
|
static |
Get global password.
|
static |
Get global server URI.
|
static |
Get global username.
int mqtt_init | ( | const char * | server_uri | ) |
Init MQTT communication.
server_uri | Server URI |
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 |
gboolean mqtt_is_initialized | ( | void | ) |
Get the global init status.
int mqtt_publish | ( | const char * | topic, |
const char * | msg ) |
Publish a message on topic using the global client.
topic | topic |
msg | 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.
server_uri_in | Server URI |
topic | Topic to publish to |
msg | Message to publish |
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 |
|
static |
Reinitializes communication after mqtt_reset was used.
void mqtt_reset | ( | void | ) |
Destroy MQTTClient handle and free mqtt_t.
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. |
|
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. |
|
static |
Set MQTTClient of mqtt_t.
|
static |
|
static |
Set global client.
|
static |
Set the global mqtt password.
password | to set. |
|
static |
Set the global mqtt server URI.
server_uri_in | Server uri to set. |
|
static |
Set the global mqtt username.
username | to set. |
|
static |
Set the global init status.
status | Status of initialization. |
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 |
|
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 |
int mqtt_unsubscribe | ( | const char * | topic | ) |
unsubscribe a single topic.
This function unsubscribes global client from a given topic.
topic | Topic to unsubscribe from |
|
static |
unsubscribe a single topic.
This function unsubscribes given client from a given topic.
mqtt | contains the mqtt client |
topic | Topic to unsubscribe from |
|
static |
|
static |
|
static |
|
static |
|
static |