when use SSL_IO mode to encrypt channel, SSL_write return SSL_ERROR_WANT_WRITE will return 0 bytes and connection will be closed

This commit is contained in:
496148326@qq.com
2023-09-20 15:51:58 +00:00
committed by ob-robot
parent 4e7afbb24f
commit 72b55c00fe

View File

@ -813,9 +813,10 @@ ssize_t writev_regard_ssl(int fildes, const struct iovec *iov, int iovcnt)
wbytes = 0; wbytes = 0;
for (i = 0; (i < iovcnt) && (0 == ret); i++) { for (i = 0; (i < iovcnt) && (0 == ret); i++) {
ERR_clear_error(); ERR_clear_error();
int ret = 0;
n = SSL_write(ssl, iov[i].iov_base, iov[i].iov_len); n = SSL_write(ssl, iov[i].iov_base, iov[i].iov_len);
if (n <= 0) { if (n > 0) {
wbytes += n;
} else {
int ssl_error = SSL_get_error(ssl, n); int ssl_error = SSL_get_error(ssl, n);
if (SSL_ERROR_WANT_WRITE == ssl_error) { if (SSL_ERROR_WANT_WRITE == ssl_error) {
errno = EAGAIN; errno = EAGAIN;
@ -827,12 +828,23 @@ ssize_t writev_regard_ssl(int fildes, const struct iovec *iov, int iovcnt)
ussl_log_error("SSL_write failed, fd:%d, reason:%s", fildes, ussl_log_error("SSL_write failed, fd:%d, reason:%s", fildes,
ERR_error_string(ERR_get_error(), NULL)); ERR_error_string(ERR_get_error(), NULL));
} }
ret = ENODATA; // errno: EIO, need destroy connection
continue; // errno: EAGAIN:
} // (1) wbytes larger than 0 (means already send some data successfully), just return wbytes
// (2) wbytes equal to zero (means send the first iov), wbytes equals to n (-1 means socket buffer
// temporarily unwritable, 0 means need destroy connection)
if (EIO == errno) {
wbytes = -1;
} else if (EAGAIN == errno) {
if (wbytes > 0) {
} else {
wbytes += n; wbytes += n;
} }
} }
ret = ENODATA;
}
}
}
} }
return wbytes; return wbytes;
} }