Skip to content

Commit 2d105af

Browse files
authored
socket: properly expose TCP keepalive constants (php#20376)
This fixes missing constants on MacOS where TCP_KEEPALIVE, TCP_KEEPINTVL and TCP_KEEPCNT are available. Currently only TCP_KEEPALIVE is defined there.
1 parent 540fd6e commit 2d105af

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

ext/sockets/sockets.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,15 @@
643643
* @cvalue TCP_KEEPIDLE
644644
*/
645645
const TCP_KEEPIDLE = UNKNOWN;
646+
#endif
647+
#ifdef TCP_KEEPINTVL
646648
/**
647649
* @var int
648650
* @cvalue TCP_KEEPINTVL
649651
*/
650652
const TCP_KEEPINTVL = UNKNOWN;
653+
#endif
654+
#ifdef TCP_KEEPCNT
651655
/**
652656
* @var int
653657
* @cvalue TCP_KEEPCNT

ext/sockets/sockets_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
Test SO_KEEPALIVE and TCP keepalive constants
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+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
20+
if (!$socket) {
21+
die("socket failed");
22+
}
23+
24+
socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
25+
$keepalive = socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);
26+
echo "SO_KEEPALIVE: " . ($keepalive ? "enabled" : "disabled") . "\n";
27+
28+
if (defined('TCP_KEEPIDLE')) {
29+
socket_set_option($socket, SOL_TCP, TCP_KEEPIDLE, 60);
30+
$idle = socket_get_option($socket, SOL_TCP, TCP_KEEPIDLE);
31+
echo "TCP_KEEPIDLE: $idle\n";
32+
} else {
33+
socket_set_option($socket, SOL_TCP, TCP_KEEPALIVE, 60);
34+
$idle = socket_get_option($socket, SOL_TCP, TCP_KEEPALIVE);
35+
echo "TCP_KEEPIDLE: $idle\n";
36+
}
37+
38+
socket_set_option($socket, SOL_TCP, TCP_KEEPINTVL, 10);
39+
$intvl = socket_get_option($socket, SOL_TCP, TCP_KEEPINTVL);
40+
echo "TCP_KEEPINTVL: $intvl\n";
41+
42+
socket_set_option($socket, SOL_TCP, TCP_KEEPCNT, 5);
43+
$cnt = socket_get_option($socket, SOL_TCP, TCP_KEEPCNT);
44+
echo "TCP_KEEPCNT: $cnt\n";
45+
46+
socket_close($socket);
47+
?>
48+
--EXPECT--
49+
SO_KEEPALIVE: enabled
50+
TCP_KEEPIDLE: 60
51+
TCP_KEEPINTVL: 10
52+
TCP_KEEPCNT: 5

0 commit comments

Comments
 (0)