common: fix #248 TLS on FreeBSD

According to document[1][2][3], retry when SSL_get_error returns
SSL_ERROR_WANT_READ/SSL_ERROR_WANT_WRITE.

[1] https://www.openssl.org/docs/ssl/SSL_read.html
[2] https://www.openssl.org/docs/ssl/SSL_write.html
[3] https://www.openssl.org/docs/ssl/SSL_accept.html
ulab-next-nosound
Koichiro IWAO 10 years ago
parent 9fb02e381d
commit 2a2b8bcd59

@ -669,6 +669,7 @@ ssl_tls_accept(struct ssl_tls *self)
return 1; return 1;
} }
while(1) {
connection_status = SSL_accept(self->ssl); connection_status = SSL_accept(self->ssl);
if (connection_status <= 0) if (connection_status <= 0)
@ -677,6 +678,16 @@ ssl_tls_accept(struct ssl_tls *self)
{ {
return 1; return 1;
} }
/**
* retry when SSL_get_error returns:
* SSL_ERROR_WANT_READ
* SSL_ERROR_WANT_WRITE
*/
}
else
{
break;
}
} }
g_writeln("ssl_tls_accept: TLS connection accepted"); g_writeln("ssl_tls_accept: TLS connection accepted");
@ -709,6 +720,11 @@ ssl_tls_disconnect(struct ssl_tls *self)
{ {
return 1; return 1;
} }
/**
* retry when SSL_get_error returns:
* SSL_ERROR_WANT_READ
* SSL_ERROR_WANT_WRITE
*/
} }
} }
return 0; return 0;
@ -737,25 +753,39 @@ int APP_CC
ssl_tls_read(struct ssl_tls *tls, char *data, int length) ssl_tls_read(struct ssl_tls *tls, char *data, int length)
{ {
int status; int status;
int break_flag;
while(1) {
status = SSL_read(tls->ssl, data, length); status = SSL_read(tls->ssl, data, length);
switch (SSL_get_error(tls->ssl, status)) switch (SSL_get_error(tls->ssl, status))
{ {
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
break_flag = 1;
break; break;
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
status = 0; /**
break; * retry when SSL_get_error returns:
* SSL_ERROR_WANT_READ
* SSL_ERROR_WANT_WRITE
*/
continue;
default: default:
ssl_tls_print_error("SSL_read", tls->ssl, status); ssl_tls_print_error("SSL_read", tls->ssl, status);
status = -1; status = -1;
break_flag = 1;
break; break;
} }
if (break_flag)
{
break;
}
}
if (SSL_pending(tls->ssl) > 0) if (SSL_pending(tls->ssl) > 0)
{ {
g_set_wait_obj(tls->rwo); g_set_wait_obj(tls->rwo);
@ -769,24 +799,38 @@ int APP_CC
ssl_tls_write(struct ssl_tls *tls, const char *data, int length) ssl_tls_write(struct ssl_tls *tls, const char *data, int length)
{ {
int status; int status;
int break_flag;
while(1) {
status = SSL_write(tls->ssl, data, length); status = SSL_write(tls->ssl, data, length);
switch (SSL_get_error(tls->ssl, status)) switch (SSL_get_error(tls->ssl, status))
{ {
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
break_flag = 1;
break; break;
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
status = 0; /**
break; * retry when SSL_get_error returns:
* SSL_ERROR_WANT_READ
* SSL_ERROR_WANT_WRITE
*/
continue;
default: default:
ssl_tls_print_error("SSL_write", tls->ssl, status); ssl_tls_print_error("SSL_write", tls->ssl, status);
status = -1; status = -1;
break_flag = 1;
break;
}
if (break_flag)
{
break; break;
} }
}
return status; return status;
} }

Loading…
Cancel
Save