From 041f60c03e1771d6d085419f282c10c37ef94219 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 24 Oct 2014 22:39:09 +0100 Subject: [PATCH] Make bridge connections non-blocking for TLS connections. --- lib/net_mosq.c | 19 ++++++++++--------- lib/net_mosq.h | 2 +- src/loop.c | 10 +++++++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/net_mosq.c b/lib/net_mosq.c index a4c51872..1861d3af 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -268,7 +268,7 @@ static unsigned int psk_client_callback(SSL *ssl, const char *hint, } #endif -int _mosquitto_try_connect(const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking) +int _mosquitto_try_connect(struct mosquitto *mosq, const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking) { struct addrinfo hints; struct addrinfo *ainfo, *rp; @@ -281,7 +281,14 @@ int _mosquitto_try_connect(const char *host, uint16_t port, int *sock, const cha *sock = INVALID_SOCKET; memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = PF_UNSPEC; +#ifdef WITH_TLS + if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){ + hints.ai_family = PF_INET; + }else +#endif + { + hints.ai_family = PF_UNSPEC; + } hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; @@ -380,13 +387,7 @@ int _mosquitto_socket_connect(struct mosquitto *mosq, const char *host, uint16_t if(!mosq || !host || !port) return MOSQ_ERR_INVAL; -#ifdef WITH_TLS - if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){ - blocking = true; - } -#endif - - rc = _mosquitto_try_connect(host, port, &sock, bind_address, blocking); + rc = _mosquitto_try_connect(mosq, host, port, &sock, bind_address, blocking); if(rc > 0) return rc; #ifdef WITH_TLS diff --git a/lib/net_mosq.h b/lib/net_mosq.h index 4d7da907..af207dfe 100644 --- a/lib/net_mosq.h +++ b/lib/net_mosq.h @@ -60,7 +60,7 @@ int _mosquitto_socket_close(struct mosquitto_db *db, struct mosquitto *mosq); #else int _mosquitto_socket_close(struct mosquitto *mosq); #endif -int _mosquitto_try_connect(const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking); +int _mosquitto_try_connect(struct mosquitto *mosq, const char *host, uint16_t port, int *sock, const char *bind_address, bool blocking); int _mosquitto_socket_nonblock(int sock); int _mosquitto_socketpair(int *sp1, int *sp2); diff --git a/src/loop.c b/src/loop.c index 60fea216..79c39afd 100644 --- a/src/loop.c +++ b/src/loop.c @@ -146,8 +146,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock && context->bridge->cur_address != 0 && now > context->bridge->primary_retry){ - /* FIXME - this should be non-blocking */ - if(_mosquitto_try_connect(context->bridge->addresses[0].address, context->bridge->addresses[0].port, &bridge_sock, NULL, false) == MOSQ_ERR_SUCCESS){ + if(_mosquitto_try_connect(context, context->bridge->addresses[0].address, context->bridge->addresses[0].port, &bridge_sock, NULL, false) == MOSQ_ERR_SUCCESS){ COMPAT_CLOSE(bridge_sock); _mosquitto_socket_close(db, context); context->bridge->cur_address = context->bridge->address_count-1; @@ -400,7 +399,12 @@ static void loop_handle_errors(struct mosquitto_db *db, struct pollfd *pollfds) static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds) { struct mosquitto *context, *ctxt_tmp; - int err, len; +#ifdef WIN32 + char err; +#else + int err; +#endif + socklen_t len; HASH_ITER(hh_sock, db->contexts_by_sock, context, ctxt_tmp){ if(context->pollfd_index < 0){