Make bridge connections non-blocking for TLS connections.

pull/211/merge
Roger A. Light 11 years ago
parent c92e2f5581
commit 041f60c03e

@ -268,7 +268,7 @@ static unsigned int psk_client_callback(SSL *ssl, const char *hint,
} }
#endif #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 hints;
struct addrinfo *ainfo, *rp; 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; *sock = INVALID_SOCKET;
memset(&hints, 0, sizeof(struct addrinfo)); 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_flags = AI_ADDRCONFIG;
hints.ai_socktype = SOCK_STREAM; 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; if(!mosq || !host || !port) return MOSQ_ERR_INVAL;
#ifdef WITH_TLS rc = _mosquitto_try_connect(mosq, host, port, &sock, bind_address, blocking);
if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){
blocking = true;
}
#endif
rc = _mosquitto_try_connect(host, port, &sock, bind_address, blocking);
if(rc > 0) return rc; if(rc > 0) return rc;
#ifdef WITH_TLS #ifdef WITH_TLS

@ -60,7 +60,7 @@ int _mosquitto_socket_close(struct mosquitto_db *db, struct mosquitto *mosq);
#else #else
int _mosquitto_socket_close(struct mosquitto *mosq); int _mosquitto_socket_close(struct mosquitto *mosq);
#endif #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_socket_nonblock(int sock);
int _mosquitto_socketpair(int *sp1, int *sp2); int _mosquitto_socketpair(int *sp1, int *sp2);

@ -146,8 +146,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock
&& context->bridge->cur_address != 0 && context->bridge->cur_address != 0
&& now > context->bridge->primary_retry){ && now > context->bridge->primary_retry){
/* FIXME - this should be non-blocking */ if(_mosquitto_try_connect(context, context->bridge->addresses[0].address, context->bridge->addresses[0].port, &bridge_sock, NULL, false) == MOSQ_ERR_SUCCESS){
if(_mosquitto_try_connect(context->bridge->addresses[0].address, context->bridge->addresses[0].port, &bridge_sock, NULL, false) == MOSQ_ERR_SUCCESS){
COMPAT_CLOSE(bridge_sock); COMPAT_CLOSE(bridge_sock);
_mosquitto_socket_close(db, context); _mosquitto_socket_close(db, context);
context->bridge->cur_address = context->bridge->address_count-1; 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) static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds)
{ {
struct mosquitto *context, *ctxt_tmp; 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){ HASH_ITER(hh_sock, db->contexts_by_sock, context, ctxt_tmp){
if(context->pollfd_index < 0){ if(context->pollfd_index < 0){

Loading…
Cancel
Save