Skip to content

Commit 36ec92c

Browse files
committed
ext/openssl/xp_ssl: eliminate poll() when MSG_DONTWAIT is available
If there is a zero timeout and MSG_DONTWAIT is available (or the socket is non-blocking), the poll() call is not necessary, and we can just call recv() right away.
1 parent 42817cf commit 36ec92c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ext/openssl/xp_ssl.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#undef X509_EXTENSIONS
4747
#endif
4848

49+
#ifndef MSG_DONTWAIT
50+
# define MSG_DONTWAIT 0
51+
#endif
52+
4953
/* Flags for determining allowed stream crypto methods */
5054
#define STREAM_CRYPTO_IS_CLIENT (1<<0)
5155
#define STREAM_CRYPTO_METHOD_SSLv2 (1<<1)
@@ -2395,7 +2399,10 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
23952399

23962400
if (sslsock->s.socket == -1) {
23972401
alive = 0;
2398-
} else if (php_pollfd_for(sslsock->s.socket, PHP_POLLREADABLE|POLLPRI, &tv) > 0) {
2402+
} else if ((!sslsock->ssl_active && value == 0 && (MSG_DONTWAIT || !sslsock->s.is_blocked)) ||
2403+
php_pollfd_for(sslsock->s.socket, PHP_POLLREADABLE|POLLPRI, &tv) > 0) {
2404+
/* the poll() call was skipped if the socket is non-blocking (or MSG_DONTWAIT is available) and if the timeout is zero */
2405+
/* additionally, we don't use this optimization if SSL is active because in that case, we're not using MSG_DONTWAIT */
23992406
if (sslsock->ssl_active) {
24002407
int n = SSL_peek(sslsock->ssl_handle, &buf, sizeof(buf));
24012408
if (n <= 0) {
@@ -2413,7 +2420,7 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
24132420
alive = 0;
24142421
}
24152422
}
2416-
} else if (0 == recv(sslsock->s.socket, &buf, sizeof(buf), MSG_PEEK) && php_socket_errno() != EAGAIN) {
2423+
} else if (0 == recv(sslsock->s.socket, &buf, sizeof(buf), MSG_PEEK|MSG_DONTWAIT) && php_socket_errno() != EAGAIN) {
24172424
alive = 0;
24182425
}
24192426
}

0 commit comments

Comments
 (0)