Add MOSQ_OPT_BIND_ADDRESS.

This allows setting of a bind address independently of the
`mosquitto_connect*()` call.
This commit is contained in:
Roger A. Light 2020-10-14 10:31:46 +01:00
parent c6b94f6266
commit 9724710cd6
4 changed files with 32 additions and 10 deletions

View File

@ -75,6 +75,8 @@ Client library:
- Fix send quota being incorrecly reset on reconnect. Closes #1822.
- Don't use logging until log mutex is initialised. Closes #1819.
- Fix missing mach/mach_time.h header on OS X. Closes #1831.
- Add MOSQ_OPT_BIND_ADDRESS to allow setting of a bind address independently
of the `mosquitto_connect*()` call.
Clients:
- Add timeout return code (27) for `mosquitto_sub -W <secs>` and

View File

@ -118,6 +118,7 @@ enum mosq_opt_t {
MOSQ_OPT_TLS_OCSP_REQUIRED = 9,
MOSQ_OPT_TLS_ALPN = 10,
MOSQ_OPT_TCP_NODELAY = 11,
MOSQ_OPT_BIND_ADDRESS = 12,
};
@ -1485,6 +1486,9 @@ libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t
* services available on a single TLS port, such as both MQTT
* and WebSockets, use this option to configure the ALPN
* option for the connection.
*
* MOSQ_OPT_BIND_ADDRESS - Set the hostname or ip address of the local network
* interface to bind to when connecting.
*/
libmosq_EXPORT int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, const char *value);

View File

@ -33,10 +33,10 @@ Contributors:
static char alphanum[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static int mosquitto__reconnect(struct mosquitto *mosq, bool blocking, const mosquitto_property *properties);
static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address);
static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive);
static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address)
static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int port, int keepalive)
{
int i;
int rc;
@ -70,12 +70,6 @@ static int mosquitto__connect_init(struct mosquitto *mosq, const char *host, int
if(!mosq->host) return MOSQ_ERR_NOMEM;
mosq->port = port;
mosquitto__free(mosq->bind_address);
if(bind_address){
mosq->bind_address = mosquitto__strdup(bind_address);
if(!mosq->bind_address) return MOSQ_ERR_NOMEM;
}
mosq->keepalive = keepalive;
mosq->msgs_in.inflight_quota = mosq->msgs_in.inflight_maximum;
mosq->msgs_out.inflight_quota = mosq->msgs_out.inflight_maximum;
@ -100,12 +94,15 @@ int mosquitto_connect_bind_v5(struct mosquitto *mosq, const char *host, int port
{
int rc;
rc = mosquitto_string_option(mosq, MOSQ_OPT_BIND_ADDRESS, bind_address);
if(rc) return rc;
if(properties){
rc = mosquitto_property_check_all(CMD_CONNECT, properties);
if(rc) return rc;
}
rc = mosquitto__connect_init(mosq, host, port, keepalive, bind_address);
rc = mosquitto__connect_init(mosq, host, port, keepalive);
if(rc) return rc;
mosquitto__set_state(mosq, mosq_cs_new);
@ -122,7 +119,12 @@ int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port,
int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address)
{
int rc = mosquitto__connect_init(mosq, host, port, keepalive, bind_address);
int rc;
rc = mosquitto_string_option(mosq, MOSQ_OPT_BIND_ADDRESS, bind_address);
if(rc) return rc;
rc = mosquitto__connect_init(mosq, host, port, keepalive);
if(rc) return rc;
return mosquitto__reconnect(mosq, false, NULL);

View File

@ -321,6 +321,20 @@ int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, cons
#endif
break;
case MOSQ_OPT_BIND_ADDRESS:
mosquitto__free(mosq->bind_address);
if(value){
mosq->bind_address = mosquitto__strdup(value);
if(mosq->bind_address){
return MOSQ_ERR_SUCCESS;
}else{
return MOSQ_ERR_NOMEM;
}
}else{
return MOSQ_ERR_SUCCESS;
}
default:
return MOSQ_ERR_INVAL;
}