From e0bcf8cda4e40e986547d03855f0fe7e61644c43 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 8 Jun 2021 23:50:03 +0100 Subject: [PATCH] Simplify SSL accept/connect. --- lib/loop.c | 26 +++----------------------- lib/mosquitto.c | 2 -- lib/net_mosq.c | 27 +-------------------------- lib/packet_mosq.c | 4 ---- src/net.c | 25 +------------------------ 5 files changed, 5 insertions(+), 79 deletions(-) diff --git a/lib/loop.c b/lib/loop.c index 6da8aeb5..d544f1b8 100644 --- a/lib/loop.c +++ b/lib/loop.c @@ -72,12 +72,6 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) if(mosq->ssl){ if(mosq->want_write){ FD_SET(mosq->sock, &writefds); - }else if(mosq->want_connect){ - /* Remove possible FD_SET from above, we don't want to check - * for writing if we are still connecting, unless want_write is - * definitely set. The presence of outgoing packets does not - * matter yet. */ - FD_CLR(mosq->sock, &writefds); } } #endif @@ -168,17 +162,9 @@ int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets) } if(net__is_connected(mosq) && FD_ISSET(mosq->sock, &writefds)){ -#ifdef WITH_TLS - if(mosq->want_connect){ - rc = net__socket_connect_tls(mosq); - if(rc) return rc; - }else -#endif - { - rc = mosquitto_loop_write(mosq, max_packets); - if(rc || !net__is_connected(mosq)){ - return rc; - } + rc = mosquitto_loop_write(mosq, max_packets); + if(rc || !net__is_connected(mosq)){ + return rc; } } } @@ -361,12 +347,6 @@ int mosquitto_loop_read(struct mosquitto *mosq, int max_packets) int i; if(max_packets < 1) return MOSQ_ERR_INVAL; -#ifdef WITH_TLS - if(mosq->want_connect){ - return net__socket_connect_tls(mosq); - } -#endif - pthread_mutex_lock(&mosq->msgs_out.mutex); max_packets = mosq->msgs_out.queue_len; pthread_mutex_unlock(&mosq->msgs_out.mutex); diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 0210aee3..73553eb1 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -334,8 +334,6 @@ bool mosquitto_want_write(struct mosquitto *mosq) if(mosq->ssl){ if (mosq->want_write) { result = true; - }else if(mosq->want_connect){ - result = false; } } #endif diff --git a/lib/net_mosq.c b/lib/net_mosq.c index 3896dd13..e84ef068 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -559,7 +559,6 @@ void net__print_ssl_error(struct mosquitto *mosq) int net__socket_connect_tls(struct mosquitto *mosq) { - int ret, err; long res; ERR_clear_error(); @@ -578,31 +577,7 @@ int net__socket_connect_tls(struct mosquitto *mosq) return MOSQ_ERR_OCSP; } } - - ret = SSL_connect(mosq->ssl); - if(ret != 1) { - err = SSL_get_error(mosq->ssl, ret); - if (err == SSL_ERROR_SYSCALL) { - mosq->want_connect = true; - return MOSQ_ERR_SUCCESS; - } - if(err == SSL_ERROR_WANT_READ){ - mosq->want_connect = true; - /* We always try to read anyway */ - }else if(err == SSL_ERROR_WANT_WRITE){ - mosq->want_write = true; - mosq->want_connect = true; - }else{ - net__print_ssl_error(mosq); - - COMPAT_CLOSE(mosq->sock); - mosq->sock = INVALID_SOCKET; - net__print_ssl_error(mosq); - return MOSQ_ERR_TLS; - } - }else{ - mosq->want_connect = false; - } + SSL_set_connect_state(mosq->ssl); return MOSQ_ERR_SUCCESS; } #endif diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index 5bad2e01..dacf705e 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -239,11 +239,7 @@ int packet__write(struct mosquitto *mosq) #endif state = mosquitto__get_state(mosq); -#if defined(WITH_TLS) && !defined(WITH_BROKER) - if(state == mosq_cs_connect_pending || mosq->want_connect){ -#else if(state == mosq_cs_connect_pending){ -#endif return MOSQ_ERR_SUCCESS; } diff --git a/src/net.c b/src/net.c index 942a5eff..dc16c94f 100644 --- a/src/net.c +++ b/src/net.c @@ -113,9 +113,6 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock struct mosquitto *new_context; #ifdef WITH_TLS BIO *bio; - int rc; - char ebuf[256]; - unsigned long e; #endif #ifdef WITH_WRAP struct request_info wrap_req; @@ -222,27 +219,7 @@ struct mosquitto *net__socket_accept(struct mosquitto__listener_sock *listensock bio = BIO_new_socket(new_sock, BIO_NOCLOSE); SSL_set_bio(new_context->ssl, bio, bio); ERR_clear_error(); - rc = SSL_accept(new_context->ssl); - if(rc != 1){ - rc = SSL_get_error(new_context->ssl, rc); - if(rc == SSL_ERROR_WANT_READ){ - /* We always want to read. */ - }else if(rc == SSL_ERROR_WANT_WRITE){ - new_context->want_write = true; - }else{ - if(db.config->connection_messages == true){ - e = ERR_get_error(); - while(e){ - log__printf(NULL, MOSQ_LOG_NOTICE, - "Client connection from %s failed: %s.", - new_context->address, ERR_error_string(e, ebuf)); - e = ERR_get_error(); - } - } - context__cleanup(new_context, true); - return NULL; - } - } + SSL_set_accept_state(new_context->ssl); } #endif