From b40cedaf1d5dc3d1aa3c9a7471719844f941a2c3 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 8 Jul 2016 22:04:39 +0100 Subject: [PATCH] More client accessors. --- src/linker-macosx.syms | 7 ++++ src/linker.syms | 7 ++++ src/mosquitto_broker.h | 72 +++++++++++++++++++++++++++++++++ src/mosquitto_broker_internal.h | 7 +--- src/plugin.c | 55 +++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 6 deletions(-) diff --git a/src/linker-macosx.syms b/src/linker-macosx.syms index b2692598..cf9d4fc3 100644 --- a/src/linker-macosx.syms +++ b/src/linker-macosx.syms @@ -1,2 +1,9 @@ _mosquitto_log_printf +_mosquitto_client_address +_mosquitto_client_clean_session +_mosquitto_client_id +_mosquitto_client_keepalive +_mosquitto_client_certificate +_mosquitto_client_protocol +_mosquitto_client_sub_count _mosquitto_client_username diff --git a/src/linker.syms b/src/linker.syms index 9d4663eb..69324205 100644 --- a/src/linker.syms +++ b/src/linker.syms @@ -1,4 +1,11 @@ { mosquitto_log_printf; + mosquitto_client_address; + mosquitto_client_clean_session; + mosquitto_client_id; + mosquitto_client_keepalive; + mosquitto_client_certificate; + mosquitto_client_protocol; + mosquitto_client_sub_count; mosquitto_client_username; }; diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index dc82c52c..5171e417 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -24,6 +24,12 @@ struct mosquitto_opt { char *value; }; +enum mosquitto_protocol { + mp_mqtt, + mp_mqttsn, + mp_websockets +}; + /* ========================================================================= * * Utility Functions @@ -65,6 +71,72 @@ void mosquitto_log_printf(int level, const char *fmt, ...); * * ========================================================================= */ +/* + * Function: mosquitto_client_address + * + * Retrieve the username associated with a client, or NULL if the client + * provided no username. + */ +const char *mosquitto_client_address(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_clean_session + * + * Retrieve the clean session flag value for a client. + */ +bool mosquitto_client_clean_session(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_id + * + * Retrieve the client id associated with a client. + */ +const char *mosquitto_client_id(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_keepalive + * + * Retrieve the keepalive value for a client. + */ +int mosquitto_client_keepalive(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_certificate + * + * If TLS support is enabled, return the certificate provided by a client as an + * X509 pointer from openssl. If the client did not provide a certificate, then + * NULL will be returned. This function will only ever return a non-NULL value + * if the `require_certificate` option is set to true. + * + * If TLS is not supported, this function will always return NULL. + */ +void *mosquitto_client_certificate(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_protocol + * + * Retrieve the protocol with which the client has connected. Can be one of: + * + * mp_mqtt (MQTT over TCP) + * mp_mqttsn (MQTT-SN) + * mp_websockets (MQTT over Websockets) + */ +int mosquitto_client_protocol(const struct mosquitto *client); + + +/* + * Function: mosquitto_client_sub_count + * + * Retrieve the number of subscriptions that have been made by a client. + */ +int mosquitto_client_sub_count(const struct mosquitto *client); + + /* * Function: mosquitto_client_username * diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index bb16b490..9b91dda8 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -40,6 +40,7 @@ Contributors: #endif #include "mosquitto_internal.h" +#include "mosquitto_broker.h" #include "mosquitto_plugin.h" #include "mosquitto.h" #include "tls_mosq.h" @@ -133,12 +134,6 @@ typedef union { * End UHPA data types * ======================================== */ -enum mosquitto_protocol { - mp_mqtt, - mp_mqttsn, - mp_websockets -}; - typedef uint64_t dbid_t; struct mosquitto__listener { diff --git a/src/plugin.c b/src/plugin.c index 31c2b6b9..f384af8f 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -14,9 +14,64 @@ Contributors: Roger Light - initial implementation and documentation. */ +#ifdef WITH_TLS +# include +#endif + #include "mosquitto_internal.h" +#include "mosquitto_broker.h" #include "mosquitto_broker_internal.h" +const char *mosquitto_client_address(const struct mosquitto *client) +{ + return client->address; +} + + +bool mosquitto_client_clean_session(const struct mosquitto *client) +{ + return client->clean_session; +} + + +const char *mosquitto_client_id(const struct mosquitto *client) +{ + return client->id; +} + + +int mosquitto_client_keepalive(const struct mosquitto *client) +{ + return client->keepalive; +} + + +void *mosquitto_client_certificate(const struct mosquitto *client) +{ +#ifdef WITH_TLS + if(client->ssl){ + return SSL_get_peer_certificate(client->ssl); + }else{ + return NULL; + } +#else + return NULL; +#endif +} + + +int mosquitto_client_protocol(const struct mosquitto *client) +{ + return client->protocol; +} + + +int mosquitto_client_sub_count(const struct mosquitto *client) +{ + return client->sub_count; +} + + const char *mosquitto_client_username(const struct mosquitto *context) { #ifdef WITH_BRIDGE