Commit e245231f authored by Victor Stinner's avatar Victor Stinner

Issue #22117: Fix ssl to use _PyTime_t API on sock_timeout

I didn't notice that the ssl module uses private attributes of socket objects.
parent b28e9163
...@@ -534,7 +534,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, ...@@ -534,7 +534,7 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
/* If the socket is in non-blocking mode or timeout mode, set the BIO /* If the socket is in non-blocking mode or timeout mode, set the BIO
* to non-blocking mode (blocking is the default) * to non-blocking mode (blocking is the default)
*/ */
if (sock && sock->sock_timeout >= 0.0) { if (sock && sock->sock_timeout >= 0) {
BIO_set_nbio(SSL_get_rbio(self->ssl), 1); BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
BIO_set_nbio(SSL_get_wbio(self->ssl), 1); BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
} }
...@@ -576,7 +576,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self) ...@@ -576,7 +576,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
Py_INCREF(sock); Py_INCREF(sock);
/* just in case the blocking state of the socket has been changed */ /* just in case the blocking state of the socket has been changed */
nonblocking = (sock->sock_timeout >= 0.0); nonblocking = (sock->sock_timeout >= 0);
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
} }
...@@ -1616,9 +1616,9 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) ...@@ -1616,9 +1616,9 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
int rc; int rc;
/* Nothing to do unless we're in timeout mode (not non-blocking) */ /* Nothing to do unless we're in timeout mode (not non-blocking) */
if ((s == NULL) || (s->sock_timeout == 0.0)) if ((s == NULL) || (s->sock_timeout == 0))
return SOCKET_IS_NONBLOCKING; return SOCKET_IS_NONBLOCKING;
else if (s->sock_timeout < 0.0) else if (s->sock_timeout < 0)
return SOCKET_IS_BLOCKING; return SOCKET_IS_BLOCKING;
/* Guard against closed socket */ /* Guard against closed socket */
...@@ -1636,7 +1636,9 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) ...@@ -1636,7 +1636,9 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
pollfd.events = writing ? POLLOUT : POLLIN; pollfd.events = writing ? POLLOUT : POLLIN;
/* s->sock_timeout is in seconds, timeout in ms */ /* s->sock_timeout is in seconds, timeout in ms */
timeout = (int)(s->sock_timeout * 1000 + 0.5); timeout = (int)_PyTime_AsMilliseconds(s->sock_timeout,
_PyTime_ROUND_UP);
PySSL_BEGIN_ALLOW_THREADS PySSL_BEGIN_ALLOW_THREADS
rc = poll(&pollfd, 1, timeout); rc = poll(&pollfd, 1, timeout);
PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS
...@@ -1649,9 +1651,10 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing) ...@@ -1649,9 +1651,10 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
if (!_PyIsSelectable_fd(s->sock_fd)) if (!_PyIsSelectable_fd(s->sock_fd))
return SOCKET_TOO_LARGE_FOR_SELECT; return SOCKET_TOO_LARGE_FOR_SELECT;
/* Construct the arguments to select */ /* conversion was already checked for overflow when
tv.tv_sec = (int)s->sock_timeout; the timeout was set */
tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); (void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(s->sock_fd, &fds); FD_SET(s->sock_fd, &fds);
...@@ -1704,7 +1707,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args) ...@@ -1704,7 +1707,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
if (sock != NULL) { if (sock != NULL) {
/* just in case the blocking state of the socket has been changed */ /* just in case the blocking state of the socket has been changed */
nonblocking = (sock->sock_timeout >= 0.0); nonblocking = (sock->sock_timeout >= 0);
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
} }
...@@ -1836,7 +1839,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args) ...@@ -1836,7 +1839,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
if (sock != NULL) { if (sock != NULL) {
/* just in case the blocking state of the socket has been changed */ /* just in case the blocking state of the socket has been changed */
nonblocking = (sock->sock_timeout >= 0.0); nonblocking = (sock->sock_timeout >= 0);
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
} }
...@@ -1915,7 +1918,7 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self) ...@@ -1915,7 +1918,7 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
Py_INCREF(sock); Py_INCREF(sock);
/* Just in case the blocking state of the socket has been changed */ /* Just in case the blocking state of the socket has been changed */
nonblocking = (sock->sock_timeout >= 0.0); nonblocking = (sock->sock_timeout >= 0);
BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment