Simplify SSL accept/connect.

This commit is contained in:
Roger A. Light
2021-06-08 23:52:25 +01:00
parent 906a515704
commit e0bcf8cda4
5 changed files with 5 additions and 79 deletions
+3 -23
View File
@@ -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);
-2
View File
@@ -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
+1 -26
View File
@@ -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
-4
View File
@@ -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;
}
+1 -24
View File
@@ -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