diff --git a/include/mosquitto_broker.h b/include/mosquitto_broker.h index 6f818a96..03bd24e1 100644 --- a/include/mosquitto_broker.h +++ b/include/mosquitto_broker.h @@ -462,6 +462,21 @@ mosq_EXPORT const char *mosquitto_client_username(const struct mosquitto *client */ mosq_EXPORT int mosquitto_set_username(struct mosquitto *client, const char *username); +/* Function: mosquitto_set_clientid + * + * Set the client id for a client. + * + * This effectively forces the client onto another message queue. + * Can be used to scope client ids by prefixing the client id with some user-specific data. + * eg. "client1" could become "user1-client1". + * + * Returns: + * MOSQ_ERR_SUCCESS - on success + * MOSQ_ERR_INVAL - if client is NULL, or if clientid is not a valid utf-8 string + * MOSQ_ERR_NOMEM - on out of memory + */ +mosq_EXPORT int mosquitto_set_clientid(struct mosquitto *client, const char *clientid); + /* ========================================================================= * diff --git a/src/linker-macosx.syms b/src/linker-macosx.syms index 40481bca..3d0fdab7 100644 --- a/src/linker-macosx.syms +++ b/src/linker-macosx.syms @@ -29,6 +29,7 @@ _mosquitto_property_free_all _mosquitto_pub_topic_check _mosquitto_realloc _mosquitto_set_username +_mosquitto_set_clientid _mosquitto_strdup _mosquitto_sub_matches_acl _mosquitto_sub_matches_acl_with_pattern diff --git a/src/linker.syms b/src/linker.syms index f1b8deb1..2c4061cd 100644 --- a/src/linker.syms +++ b/src/linker.syms @@ -30,6 +30,7 @@ mosquitto_pub_topic_check; mosquitto_realloc; mosquitto_set_username; + mosquitto_set_clientid; mosquitto_strdup; mosquitto_sub_matches_acl; mosquitto_sub_matches_acl_with_pattern; diff --git a/src/plugin_public.c b/src/plugin_public.c index 5e5489d9..f678ddf0 100644 --- a/src/plugin_public.c +++ b/src/plugin_public.c @@ -268,6 +268,28 @@ int mosquitto_set_username(struct mosquitto *client, const char *username) } } +int mosquitto_set_clientid(struct mosquitto *client, const char *clientid) +{ + char *u_dup; + char *old; + + if(!client) return MOSQ_ERR_INVAL; + + int clientid_len = (int)strlen(clientid); + if(mosquitto_validate_utf8(clientid, clientid_len)){ + return MOSQ_ERR_INVAL; + } + + u_dup = mosquitto__strdup(clientid); + if(!u_dup) return MOSQ_ERR_NOMEM; + + + old = client->id; + client->id = u_dup; + + mosquitto__free(old); + return MOSQ_ERR_SUCCESS; +} static void disconnect_client(struct mosquitto *context, bool with_will) {