diff --git a/ChangeLog.txt b/ChangeLog.txt index 5cd2adc8..c2910d09 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -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 diff --git a/include/mosquitto.h b/include/mosquitto.h index 17f9a9c7..afdb59ca 100644 --- a/include/mosquitto.h +++ b/include/mosquitto.h @@ -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: + * */ 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: + * */ 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: + * */ 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 , or + * , 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 + */ +libmosq_EXPORT void mosquitto_pre_connect_callback_set(struct mosquitto *mosq, void (*on_pre_connect)(struct mosquitto *, void *)); + /* * Function: mosquitto_disconnect_callback_set * diff --git a/lib/callbacks.c b/lib/callbacks.c index ea770eee..a29f5fde 100644 --- a/lib/callbacks.c +++ b/lib/callbacks.c @@ -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); diff --git a/lib/connect.c b/lib/connect.c index f5669924..8d4b1710 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -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); diff --git a/lib/linker.version b/lib/linker.version index a846505f..6fe20feb 100644 --- a/lib/linker.version +++ b/lib/linker.version @@ -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; diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 38230d23..699d0f3e 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -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); diff --git a/test/lib/01-pre-connect-callback.py b/test/lib/01-pre-connect-callback.py new file mode 100755 index 00000000..221e797c --- /dev/null +++ b/test/lib/01-pre-connect-callback.py @@ -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) + diff --git a/test/lib/Makefile b/test/lib/Makefile index 93603f5b..79770f67 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -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 diff --git a/test/lib/c/01-pre-connect-callback.c b/test/lib/c/01-pre-connect-callback.c new file mode 100644 index 00000000..998b088f --- /dev/null +++ b/test/lib/c/01-pre-connect-callback.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +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; +} diff --git a/test/lib/c/Makefile b/test/lib/c/Makefile index 6c09e806..084f0d7a 100644 --- a/test/lib/c/Makefile +++ b/test/lib/c/Makefile @@ -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 \