Skip to content

Commit

Permalink
Add MOSQ_OPT_BIND_ADDRESS.
Browse files Browse the repository at this point in the history
This allows setting of a bind address independently of the
`mosquitto_connect*()` call.
  • Loading branch information
ralight committed Oct 14, 2020
1 parent c6b94f6 commit 9724710
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions include/mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};


Expand Down Expand Up @@ -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);

Expand Down
22 changes: 12 additions & 10 deletions lib/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ and the Eclipse Distribution License is available at
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions lib/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9724710

Please sign in to comment.