mirror of
https://github.com/eclipse-mosquitto/mosquitto.git
synced 2026-02-06 02:52:07 +08:00
Add on_pre_connect() callback.
This allows clients to update usernames/passwords/TLS parameters prior to reconnecting.
This commit is contained in:
@@ -11,6 +11,8 @@ Client library:
|
||||
feature that allows the network thread to be woken from select() by another
|
||||
thread when e.g. mosquitto_publish() is called. This reduces the number of
|
||||
sockets used by each client by two.
|
||||
- Add `on_pre_connect()` callback to allow clients to update
|
||||
username/password/TLS parameters before an automatic reconnection.
|
||||
|
||||
|
||||
2.0.6 - 2021-01-xx
|
||||
|
||||
@@ -1935,6 +1935,9 @@ libmosq_EXPORT void *mosquitto_ssl_get(struct mosquitto *mosq);
|
||||
* the MQTT protocol version in use.
|
||||
* For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
|
||||
* For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
|
||||
*
|
||||
* See Also:
|
||||
* <mosquitto_pre_connect_callback_set>
|
||||
*/
|
||||
libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int));
|
||||
|
||||
@@ -1957,6 +1960,9 @@ libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void
|
||||
* For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
|
||||
* For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
|
||||
* flags - the connect flags.
|
||||
*
|
||||
* See Also:
|
||||
* <mosquitto_pre_connect_callback_set>
|
||||
*/
|
||||
libmosq_EXPORT void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int));
|
||||
|
||||
@@ -1985,9 +1991,30 @@ libmosq_EXPORT void mosquitto_connect_with_flags_callback_set(struct mosquitto *
|
||||
* flags - the connect flags.
|
||||
* props - list of MQTT 5 properties, or NULL
|
||||
*
|
||||
* See Also:
|
||||
* <mosquitto_pre_connect_callback_set>
|
||||
*/
|
||||
libmosq_EXPORT void mosquitto_connect_v5_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int, const mosquitto_property *props));
|
||||
|
||||
/*
|
||||
* Function: mosquitto_pre_connect_callback_set
|
||||
*
|
||||
* Set the pre-connect callback. The pre-connect callback is called just before an attempt is made to connect to the broker. This may be useful if you are using <mosquitto_loop_start>, or
|
||||
* <mosquitto_loop_forever>, because when your client disconnects the library
|
||||
* will by default automatically reconnect. Using the pre-connect callback
|
||||
* allows you to set usernames, passwords, and TLS related parameters.
|
||||
*
|
||||
* Parameters:
|
||||
* mosq - a valid mosquitto instance.
|
||||
* on_pre_connect - a callback function in the following form:
|
||||
* void callback(struct mosquitto *mosq, void *obj)
|
||||
*
|
||||
* Callback Parameters:
|
||||
* mosq - the mosquitto instance making the callback.
|
||||
* obj - the user data provided in <mosquitto_new>
|
||||
*/
|
||||
libmosq_EXPORT void mosquitto_pre_connect_callback_set(struct mosquitto *mosq, void (*on_pre_connect)(struct mosquitto *, void *));
|
||||
|
||||
/*
|
||||
* Function: mosquitto_disconnect_callback_set
|
||||
*
|
||||
|
||||
@@ -43,6 +43,13 @@ void mosquitto_connect_v5_callback_set(struct mosquitto *mosq, void (*on_connect
|
||||
pthread_mutex_unlock(&mosq->callback_mutex);
|
||||
}
|
||||
|
||||
void mosquitto_pre_connect_callback_set(struct mosquitto *mosq, void (*on_pre_connect)(struct mosquitto *, void *))
|
||||
{
|
||||
pthread_mutex_lock(&mosq->callback_mutex);
|
||||
mosq->on_pre_connect = on_pre_connect;
|
||||
pthread_mutex_unlock(&mosq->callback_mutex);
|
||||
}
|
||||
|
||||
void mosquitto_disconnect_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int))
|
||||
{
|
||||
pthread_mutex_lock(&mosq->callback_mutex);
|
||||
|
||||
@@ -195,6 +195,14 @@ static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking)
|
||||
net__socket_close(mosq); //close socket
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mosq->callback_mutex);
|
||||
if(mosq->on_pre_connect){
|
||||
mosq->in_callback = true;
|
||||
mosq->on_pre_connect(mosq, mosq->userdata);
|
||||
mosq->in_callback = false;
|
||||
}
|
||||
pthread_mutex_unlock(&mosq->callback_mutex);
|
||||
|
||||
#ifdef WITH_SOCKS
|
||||
if(mosq->socks5_host){
|
||||
rc = net__socket_connect(mosq, mosq->socks5_host, mosq->socks5_port, mosq->bind_address, blocking);
|
||||
|
||||
@@ -141,3 +141,8 @@ MOSQ_1.7 {
|
||||
mosquitto_property_next;
|
||||
mosquitto_ssl_get;
|
||||
} MOSQ_1.6;
|
||||
|
||||
MOSQ_2.1 {
|
||||
global:
|
||||
mosquitto_pre_connect_callback_set;
|
||||
} MOSQ_1.7;
|
||||
|
||||
@@ -310,6 +310,7 @@ struct mosquitto {
|
||||
bool in_callback;
|
||||
struct mosquitto_msg_data msgs_in;
|
||||
struct mosquitto_msg_data msgs_out;
|
||||
void (*on_pre_connect)(struct mosquitto *, void *userdata);
|
||||
void (*on_connect)(struct mosquitto *, void *userdata, int rc);
|
||||
void (*on_connect_with_flags)(struct mosquitto *, void *userdata, int rc, int flags);
|
||||
void (*on_connect_v5)(struct mosquitto *, void *userdata, int rc, int flags, const mosquitto_property *props);
|
||||
|
||||
48
test/lib/01-pre-connect-callback.py
Executable file
48
test/lib/01-pre-connect-callback.py
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Test whether the pre-connect callback is triggered and allows us to set a username and password.
|
||||
|
||||
# The client should connect to port 1888 with keepalive=60, clean session set,
|
||||
# client id 01-pre-connect, username set to uname and password set to ;'[08gn=#
|
||||
|
||||
from mosq_test_helper import *
|
||||
|
||||
port = mosq_test.get_lib_port()
|
||||
|
||||
rc = 1
|
||||
keepalive = 60
|
||||
connect_packet = mosq_test.gen_connect("01-pre-connect", keepalive=keepalive, username="uname", password=";'[08gn=#")
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.settimeout(10)
|
||||
sock.bind(('', port))
|
||||
sock.listen(5)
|
||||
|
||||
client_args = sys.argv[1:]
|
||||
env = dict(os.environ)
|
||||
env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp'
|
||||
try:
|
||||
pp = env['PYTHONPATH']
|
||||
except KeyError:
|
||||
pp = ''
|
||||
env['PYTHONPATH'] = '../../lib/python:'+pp
|
||||
client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port)
|
||||
|
||||
try:
|
||||
(conn, address) = sock.accept()
|
||||
conn.settimeout(10)
|
||||
|
||||
mosq_test.expect_packet(conn, "connect", connect_packet)
|
||||
rc = 0
|
||||
|
||||
conn.close()
|
||||
except mosq_test.TestError:
|
||||
pass
|
||||
finally:
|
||||
client.terminate()
|
||||
client.wait()
|
||||
sock.close()
|
||||
|
||||
exit(rc)
|
||||
|
||||
@@ -27,6 +27,7 @@ c : test-compile
|
||||
./01-keepalive-pingreq.py $@/01-keepalive-pingreq.test
|
||||
./01-no-clean-session.py $@/01-no-clean-session.test
|
||||
./01-server-keepalive-pingreq.py $@/01-server-keepalive-pingreq.test
|
||||
./01-pre-connect-callback.py $@/01-pre-connect-callback.test
|
||||
./01-unpwd-set.py $@/01-unpwd-set.test
|
||||
./01-will-set.py $@/01-will-set.test
|
||||
./01-will-unpwd-set.py $@/01-will-unpwd-set.test
|
||||
|
||||
37
test/lib/c/01-pre-connect-callback.c
Normal file
37
test/lib/c/01-pre-connect-callback.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <mosquitto.h>
|
||||
|
||||
void on_pre_connect(struct mosquitto *mosq, void *userdata)
|
||||
{
|
||||
mosquitto_username_pw_set(mosq, "uname", ";'[08gn=#");
|
||||
}
|
||||
|
||||
static int run = -1;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int rc;
|
||||
struct mosquitto *mosq;
|
||||
|
||||
int port = atoi(argv[1]);
|
||||
|
||||
mosquitto_lib_init();
|
||||
|
||||
mosq = mosquitto_new("01-pre-connect", true, NULL);
|
||||
if(mosq == NULL){
|
||||
return 1;
|
||||
}
|
||||
mosquitto_pre_connect_callback_set(mosq, on_pre_connect);
|
||||
|
||||
rc = mosquitto_connect(mosq, "localhost", port, 60);
|
||||
|
||||
while(run == -1){
|
||||
mosquitto_loop(mosq, -1, 1);
|
||||
}
|
||||
mosquitto_destroy(mosq);
|
||||
|
||||
mosquitto_lib_cleanup();
|
||||
return run;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ SRC = \
|
||||
01-con-discon-success.c \
|
||||
01-keepalive-pingreq.c \
|
||||
01-no-clean-session.c \
|
||||
01-pre-connect-callback.c \
|
||||
01-server-keepalive-pingreq.c \
|
||||
01-unpwd-set.c \
|
||||
01-will-set.c \
|
||||
|
||||
Reference in New Issue
Block a user