Skip to content

Commit

Permalink
Change: make authenticated connection to MQTT broker backward comaptible
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola authored and nichtsfrei committed Dec 20, 2023
1 parent 89cd6a9 commit 3752550
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
45 changes: 40 additions & 5 deletions util/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username,
return 0;
}

/**
* @brief Init MQTT communication
*
* @param server_uri Server URI
*
* @return 0 on success, <0 on error.
*/
int
mqtt_init (const char *server_uri)
{
return mqtt_init_auth (server_uri, NULL, NULL);
}
/**
* @brief Init MQTT communication
*
Expand All @@ -367,7 +379,8 @@ mqtt_connect (mqtt_t *mqtt, const char *server_uri, const char *username,
* @return 0 on success, <0 on error.
*/
int
mqtt_init (const char *server_uri, const char *username, const char *password)
mqtt_init_auth (const char *server_uri, const char *username,
const char *password)
{
mqtt_t *mqtt = NULL;
const char *g_server_uri;
Expand Down Expand Up @@ -433,7 +446,7 @@ mqtt_reinit ()
}
username = mqtt_get_global_username ();
password = mqtt_get_global_password ();
mqtt_init (server_uri, username, password);
mqtt_init_auth (server_uri, username, password);
}

/**
Expand Down Expand Up @@ -517,6 +530,27 @@ mqtt_publish (const char *topic, const char *msg)
* meant for error messages and the likes emitted by openvas.
*
* @param server_uri_in Server URI
* @param topic Topic to publish to
* @param msg Message to publish
*
* @return 0 on success, <0 on failure.
*/
int
mqtt_publish_single_message (const char *server_uri_in, const char *topic,
const char *msg)
{
return mqtt_publish_single_message_auth (server_uri_in, NULL, NULL, topic,
msg);
}
/**
* @brief 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.
*
* @param server_uri_in Server URI
* @param username_in Username
* @param password_in Password
* @param topic Topic to publish to
Expand All @@ -525,9 +559,10 @@ mqtt_publish (const char *topic, const char *msg)
* @return 0 on success, <0 on failure.
*/
int
mqtt_publish_single_message (const char *server_uri_in, const char *username_in,
const char *passwd_in, const char *topic,
const char *msg)
mqtt_publish_single_message_auth (const char *server_uri_in,
const char *username_in,
const char *passwd_in, const char *topic,
const char *msg)
{
const char *server_uri;
const char *username = NULL;
Expand Down
14 changes: 11 additions & 3 deletions util/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
#include <MQTTClient.h>
#include <glib.h>

#define AUTH_MQTT 1

int
mqtt_init (const char *);

int
mqtt_init (const char *, const char *, const char *);
mqtt_init_auth (const char *, const char *, const char *);

gboolean
mqtt_is_initialized (void);
Expand All @@ -27,8 +32,11 @@ int
mqtt_publish (const char *, const char *);

int
mqtt_publish_single_message (const char *, const char *, const char *,
const char *, const char *);
mqtt_publish_single_message_auth (const char *, const char *, const char *,
const char *, const char *);

int
mqtt_publish_single_message (const char *, const char *, const char *);

int
mqtt_subscribe (const char *);
Expand Down

0 comments on commit 3752550

Please sign in to comment.