Skip to content

Commit

Permalink
Add mosquitto_int_option and mosquitto_void_option
Browse files Browse the repository at this point in the history
This deprecates mosquitto_opts_set().
  • Loading branch information
ralight committed Jan 8, 2019
1 parent 2f54b16 commit 0546e7b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/client_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
{
int rc;

mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &(cfg->protocol_version));
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, cfg->protocol_version);

if(cfg->will_topic && mosquitto_will_set_v5(mosq, cfg->will_topic,
cfg->will_payloadlen, cfg->will_payload, cfg->will_qos,
Expand Down
2 changes: 2 additions & 0 deletions lib/linker.version
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ MOSQ_1.6 {
mosquitto_disconnect_v5_callback_set;
mosquitto_disconnect_v5;
mosquitto_message_v5_callback_set;
mosquitto_int_option;
mosquitto_property_add_binary;
mosquitto_property_add_byte;
mosquitto_property_add_int16;
Expand Down Expand Up @@ -126,5 +127,6 @@ MOSQ_1.6 {
mosquitto_subscribe_v5;
mosquitto_unsubscribe_v5_callback_set;
mosquitto_unsubscribe_v5;
mosquitto_void_option;
mosquitto_will_set_v5;
} MOSQ_1.5;
54 changes: 54 additions & 0 deletions lib/mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,9 @@ libmosq_EXPORT int mosquitto_threaded_set(struct mosquitto *mosq, bool threaded)
*
* Used to set options for the client.
*
* This function is deprecated, the replacement <mosquitto_int_option> and
* <mosquitto_void_option> functions should be used instead.
*
* Parameters:
* mosq - a valid mosquitto instance.
* option - the option to set.
Expand Down Expand Up @@ -1345,6 +1348,57 @@ libmosq_EXPORT int mosquitto_threaded_set(struct mosquitto *mosq, bool threaded)
*/
libmosq_EXPORT int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *value);

/*
* Function: mosquitto_int_option
*
* Used to set integer options for the client.
*
* Parameters:
* mosq - a valid mosquitto instance.
* option - the option to set.
* value - the option specific value.
*
* Options:
* MOSQ_OPT_PROTOCOL_VERSION
* Value must be set to either MQTT_PROTOCOL_V31,
* MQTT_PROTOCOL_V311, or MQTT_PROTOCOL_V5. Must be set before the
* client connects. Defaults to MQTT_PROTOCOL_V311.
*
* MOSQ_OPT_SSL_CTX_WITH_DEFAULTS
* If value is set to a non zero value, then the user specified
* SSL_CTX passed in using MOSQ_OPT_SSL_CTX will have the default
* options applied to it. This means that you only need to change
* the values that are relevant to you. If you use this option then
* you must configure the TLS options as normal, i.e. you should
* use <mosquitto_tls_set> to configure the cafile/capath as a
* minimum.
* This option is only available for openssl 1.1.0 and higher.
*/
libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value);

/*
* Function: mosquitto_void_option
*
* Used to set void* options for the client.
*
* Parameters:
* mosq - a valid mosquitto instance.
* option - the option to set.
* value - the option specific value.
*
* Options:
* MOSQ_OPT_SSL_CTX
* Pass an openssl SSL_CTX to be used when creating TLS connections
* rather than libmosquitto creating its own. This must be called
* before connecting to have any effect. If you use this option, the
* onus is on you to ensure that you are using secure settings.
* Setting to NULL means that libmosquitto will use its own SSL_CTX
* if TLS is to be used.
* This option is only available for openssl 1.1.0 and higher.
*/
libmosq_EXPORT int mosquitto_void_option(struct mosquitto *mosq, enum mosq_opt_t option, void *value);


/*
* Function: mosquitto_reconnect_delay_set
*
Expand Down
66 changes: 56 additions & 10 deletions lib/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,69 @@ int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *val
switch(option){
case MOSQ_OPT_PROTOCOL_VERSION:
ival = *((int *)value);
if(ival == MQTT_PROTOCOL_V31){
return mosquitto_int_option(mosq, option, ival);
case MOSQ_OPT_SSL_CTX:
#ifdef WITH_TLS
mosq->ssl_ctx = (SSL_CTX *)value;
if(mosq->ssl_ctx){
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
SSL_CTX_up_ref(mosq->ssl_ctx);
#else
CRYPTO_add(&(mosq->ssl_ctx)->references, 1, CRYPTO_LOCK_SSL_CTX);
#endif
}
break;
#else
return MOSQ_ERR_NOT_SUPPORTED;
#endif
default:
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}


int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value)
{
if(!mosq) return MOSQ_ERR_INVAL;

switch(option){
case MOSQ_OPT_PROTOCOL_VERSION:
if(value == MQTT_PROTOCOL_V31){
mosq->protocol = mosq_p_mqtt31;
}else if(ival == MQTT_PROTOCOL_V311){
}else if(value == MQTT_PROTOCOL_V311){
mosq->protocol = mosq_p_mqtt311;
}else if(ival == MQTT_PROTOCOL_V5){
}else if(value == MQTT_PROTOCOL_V5){
mosq->protocol = mosq_p_mqtt5;
}else{
return MOSQ_ERR_INVAL;
}
break;

case MOSQ_OPT_SSL_CTX_WITH_DEFAULTS:
#if defined(WITH_TLS) && OPENSSL_VERSION_NUMBER >= 0x10100000L
if(value){
mosq->ssl_ctx_defaults = true;
}else{
mosq->ssl_ctx_defaults = false;
}
break;
#else
return MOSQ_ERR_NOT_SUPPORTED;
#endif

default:
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}


int mosquitto_void_option(struct mosquitto *mosq, enum mosq_opt_t option, void *value)
{
if(!mosq || !value) return MOSQ_ERR_INVAL;

switch(option){
case MOSQ_OPT_SSL_CTX:
#ifdef WITH_TLS
mosq->ssl_ctx = (SSL_CTX *)value;
Expand All @@ -299,13 +352,6 @@ int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *val
break;
#else
return MOSQ_ERR_NOT_SUPPORTED;
#endif
case MOSQ_OPT_SSL_CTX_WITH_DEFAULTS:
#if defined(WITH_TLS) && OPENSSL_VERSION_NUMBER >= 0x10100000L
mosq->ssl_ctx_defaults = true;
break;
#else
return MOSQ_ERR_NOT_SUPPORTED;
#endif
default:
return MOSQ_ERR_INVAL;
Expand Down

0 comments on commit 0546e7b

Please sign in to comment.