Skip to content

Commit 71d41da

Browse files
committed
Use long conversion for stream context keepalive int values
This is consistent with http and ssl wrappers where int values are converted in this way.
1 parent c0be847 commit 71d41da

3 files changed

Lines changed: 161 additions & 16 deletions

File tree

ext/openssl/xp_ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,9 +2231,9 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
22312231
sockvals.tcp_nodelay = 1;
22322232
}
22332233
tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepidle");
2234-
if (tmpzval != NULL && Z_TYPE_P(tmpzval) == IS_LONG) {
2234+
if (tmpzval != NULL) {
22352235
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPIDLE;
2236-
sockvals.keepalive.keepidle = Z_LVAL_P(tmpzval);
2236+
sockvals.keepalive.keepidle = (int)zval_get_long(tmpzval);
22372237
}
22382238
}
22392239

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
--TEST--
2+
stream_socket_server() and stream_socket_client() keepalive option test with string values
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
if (!defined('TCP_KEEPIDLE') && !defined('TCP_KEEPALIVE')) {
8+
die('skip TCP_KEEPIDLE/TCP_KEEPALIVE not available');
9+
}
10+
if (!defined('TCP_KEEPINTVL')) {
11+
die('skip TCP_KEEPINTVL not available');
12+
}
13+
if (!defined('TCP_KEEPCNT')) {
14+
die('skip TCP_KEEPCNT not available');
15+
}
16+
?>
17+
--FILE--
18+
<?php
19+
// Test server with SO_KEEPALIVE enabled
20+
$server_context = stream_context_create([
21+
'socket' => [
22+
'so_keepalive' => true,
23+
'tcp_keepidle' => '80',
24+
'tcp_keepintvl' => '12',
25+
'tcp_keepcnt' => '6',
26+
]
27+
]);
28+
29+
$server = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr,
30+
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $server_context);
31+
32+
if (!$server) {
33+
die('Unable to create server');
34+
}
35+
36+
$addr = stream_socket_get_name($server, false);
37+
$port = (int)substr(strrchr($addr, ':'), 1);
38+
39+
// Test client with SO_KEEPALIVE enabled
40+
$client_context = stream_context_create([
41+
'socket' => [
42+
'so_keepalive' => true,
43+
'tcp_keepidle' => '31',
44+
'tcp_keepintvl' => '6',
45+
'tcp_keepcnt' => '4',
46+
]
47+
]);
48+
49+
$client = stream_socket_client("tcp://127.0.0.1:$port", $errno, $errstr, 30,
50+
STREAM_CLIENT_CONNECT, $client_context);
51+
52+
if (!$client) {
53+
die('Unable to create client');
54+
}
55+
56+
$accepted = stream_socket_accept($server, 1);
57+
58+
if (!$accepted) {
59+
die('Unable to accept connection');
60+
}
61+
62+
// Verify server side (accepted connection)
63+
$server_sock = socket_import_stream($accepted);
64+
$server_keepalive = socket_get_option($server_sock, SOL_SOCKET, SO_KEEPALIVE);
65+
echo "Server SO_KEEPALIVE: " . ($server_keepalive ? "enabled" : "disabled") . "\n";
66+
67+
if (defined('TCP_KEEPIDLE')) {
68+
$server_idle = socket_get_option($server_sock, SOL_TCP, TCP_KEEPIDLE);
69+
echo "Server TCP_KEEPIDLE: $server_idle\n";
70+
} else {
71+
$server_idle = socket_get_option($server_sock, SOL_TCP, TCP_KEEPALIVE);
72+
echo "Server TCP_KEEPIDLE: $server_idle\n";
73+
}
74+
75+
$server_intvl = socket_get_option($server_sock, SOL_TCP, TCP_KEEPINTVL);
76+
echo "Server TCP_KEEPINTVL: $server_intvl\n";
77+
78+
$server_cnt = socket_get_option($server_sock, SOL_TCP, TCP_KEEPCNT);
79+
echo "Server TCP_KEEPCNT: $server_cnt\n";
80+
81+
// Verify client side
82+
$client_sock = socket_import_stream($client);
83+
$client_keepalive = socket_get_option($client_sock, SOL_SOCKET, SO_KEEPALIVE);
84+
echo "Client SO_KEEPALIVE: " . ($client_keepalive ? "enabled" : "disabled") . "\n";
85+
86+
if (defined('TCP_KEEPIDLE')) {
87+
$client_idle = socket_get_option($client_sock, SOL_TCP, TCP_KEEPIDLE);
88+
echo "Client TCP_KEEPIDLE: $client_idle\n";
89+
} else {
90+
$client_idle = socket_get_option($client_sock, SOL_TCP, TCP_KEEPALIVE);
91+
echo "Client TCP_KEEPIDLE: $client_idle\n";
92+
}
93+
94+
$client_intvl = socket_get_option($client_sock, SOL_TCP, TCP_KEEPINTVL);
95+
echo "Client TCP_KEEPINTVL: $client_intvl\n";
96+
97+
$client_cnt = socket_get_option($client_sock, SOL_TCP, TCP_KEEPCNT);
98+
echo "Client TCP_KEEPCNT: $client_cnt\n";
99+
100+
fclose($accepted);
101+
fclose($client);
102+
fclose($server);
103+
104+
// Test server with SO_KEEPALIVE disabled
105+
$server2_context = stream_context_create(['socket' => ['so_keepalive' => false]]);
106+
$server2 = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr,
107+
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $server2_context);
108+
109+
$addr2 = stream_socket_get_name($server2, false);
110+
$port2 = (int)substr(strrchr($addr2, ':'), 1);
111+
112+
$client2 = stream_socket_client("tcp://127.0.0.1:$port2");
113+
$accepted2 = stream_socket_accept($server2, 1);
114+
115+
$server2_sock = socket_import_stream($accepted2);
116+
$server2_keepalive = socket_get_option($server2_sock, SOL_SOCKET, SO_KEEPALIVE);
117+
echo "Server disabled SO_KEEPALIVE: " . ($server2_keepalive ? "enabled" : "disabled") . "\n";
118+
119+
fclose($accepted2);
120+
fclose($client2);
121+
fclose($server2);
122+
123+
// Test client with SO_KEEPALIVE disabled
124+
$server3 = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr,
125+
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
126+
127+
$addr3 = stream_socket_get_name($server3, false);
128+
$port3 = (int)substr(strrchr($addr3, ':'), 1);
129+
130+
$client3_context = stream_context_create(['socket' => ['so_keepalive' => false]]);
131+
$client3 = stream_socket_client("tcp://127.0.0.1:$port3", $errno, $errstr, 30,
132+
STREAM_CLIENT_CONNECT, $client3_context);
133+
134+
$client3_sock = socket_import_stream($client3);
135+
$client3_keepalive = socket_get_option($client3_sock, SOL_SOCKET, SO_KEEPALIVE);
136+
echo "Client disabled SO_KEEPALIVE: " . ($client3_keepalive ? "enabled" : "disabled") . "\n";
137+
138+
fclose($client3);
139+
fclose($server3);
140+
?>
141+
--EXPECT--
142+
Server SO_KEEPALIVE: enabled
143+
Server TCP_KEEPIDLE: 80
144+
Server TCP_KEEPINTVL: 12
145+
Server TCP_KEEPCNT: 6
146+
Client SO_KEEPALIVE: enabled
147+
Client TCP_KEEPIDLE: 31
148+
Client TCP_KEEPINTVL: 6
149+
Client TCP_KEEPCNT: 4
150+
Server disabled SO_KEEPALIVE: disabled
151+
Client disabled SO_KEEPALIVE: disabled

main/streams/xp_socket.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -764,30 +764,27 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
764764
#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
765765
if (PHP_STREAM_CONTEXT(stream)
766766
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepidle")) != NULL
767-
&& Z_TYPE_P(tmpzval) == IS_LONG
768767
) {
769768
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPIDLE;
770-
sockvals.keepalive.keepidle = (int)Z_LVAL_P(tmpzval);
769+
sockvals.keepalive.keepidle = (int)zval_get_long(tmpzval);
771770
}
772771
#endif
773772

774773
#ifdef TCP_KEEPINTVL
775774
if (PHP_STREAM_CONTEXT(stream)
776775
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepintvl")) != NULL
777-
&& Z_TYPE_P(tmpzval) == IS_LONG
778776
) {
779777
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPINTVL;
780-
sockvals.keepalive.keepintvl = (int)Z_LVAL_P(tmpzval);
778+
sockvals.keepalive.keepintvl = (int)zval_get_long(tmpzval);
781779
}
782780
#endif
783781

784782
#ifdef TCP_KEEPCNT
785783
if (PHP_STREAM_CONTEXT(stream)
786784
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepcnt")) != NULL
787-
&& Z_TYPE_P(tmpzval) == IS_LONG
788785
) {
789786
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPCNT;
790-
sockvals.keepalive.keepcnt = (int)Z_LVAL_P(tmpzval);
787+
sockvals.keepalive.keepcnt = (int)zval_get_long(tmpzval);
791788
}
792789
#endif
793790
}
@@ -895,30 +892,27 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
895892
#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
896893
if (PHP_STREAM_CONTEXT(stream)
897894
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepidle")) != NULL
898-
&& Z_TYPE_P(tmpzval) == IS_LONG
899895
) {
900896
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPIDLE;
901-
sockvals.keepalive.keepidle = (int)Z_LVAL_P(tmpzval);
897+
sockvals.keepalive.keepidle = (int)zval_get_long(tmpzval);
902898
}
903899
#endif
904900

905901
#ifdef TCP_KEEPINTVL
906902
if (PHP_STREAM_CONTEXT(stream)
907903
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepintvl")) != NULL
908-
&& Z_TYPE_P(tmpzval) == IS_LONG
909904
) {
910905
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPINTVL;
911-
sockvals.keepalive.keepintvl = (int)Z_LVAL_P(tmpzval);
906+
sockvals.keepalive.keepintvl = (int)zval_get_long(tmpzval);
912907
}
913908
#endif
914909

915910
#ifdef TCP_KEEPCNT
916911
if (PHP_STREAM_CONTEXT(stream)
917912
&& (tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepcnt")) != NULL
918-
&& Z_TYPE_P(tmpzval) == IS_LONG
919913
) {
920914
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPCNT;
921-
sockvals.keepalive.keepcnt = (int)Z_LVAL_P(tmpzval);
915+
sockvals.keepalive.keepcnt = (int)zval_get_long(tmpzval);
922916
}
923917
#endif
924918
}
@@ -976,9 +970,9 @@ static inline int php_tcp_sockop_accept(php_stream *stream, php_netstream_data_t
976970
sockvals.tcp_nodelay = 1;
977971
}
978972
tmpzval = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), "socket", "tcp_keepidle");
979-
if (tmpzval != NULL && Z_TYPE_P(tmpzval) == IS_LONG) {
973+
if (tmpzval != NULL) {
980974
sockvals.mask |= PHP_SOCKVAL_TCP_KEEPIDLE;
981-
sockvals.keepalive.keepidle = Z_LVAL_P(tmpzval);
975+
sockvals.keepalive.keepidle = (int)zval_get_long(tmpzval);
982976
}
983977
}
984978

0 commit comments

Comments
 (0)