Commit 2d105afce7d for php.net
commit 2d105afce7d66cb42cbfd3226a35879548eabbd8
Author: Jakub Zelenka <bukka@php.net>
Date: Mon Nov 3 19:12:09 2025 +0100
socket: properly expose TCP keepalive constants (#20376)
This fixes missing constants on MacOS where TCP_KEEPALIVE,
TCP_KEEPINTVL and TCP_KEEPCNT are available. Currently only
TCP_KEEPALIVE is defined there.
diff --git a/ext/sockets/sockets.stub.php b/ext/sockets/sockets.stub.php
index 6050a894145..3df9b598a1e 100644
--- a/ext/sockets/sockets.stub.php
+++ b/ext/sockets/sockets.stub.php
@@ -643,11 +643,15 @@
* @cvalue TCP_KEEPIDLE
*/
const TCP_KEEPIDLE = UNKNOWN;
+#endif
+#ifdef TCP_KEEPINTVL
/**
* @var int
* @cvalue TCP_KEEPINTVL
*/
const TCP_KEEPINTVL = UNKNOWN;
+#endif
+#ifdef TCP_KEEPCNT
/**
* @var int
* @cvalue TCP_KEEPCNT
diff --git a/ext/sockets/sockets_arginfo.h b/ext/sockets/sockets_arginfo.h
index b7af6eb6b89..edfc344ff8c 100644
Binary files a/ext/sockets/sockets_arginfo.h and b/ext/sockets/sockets_arginfo.h differ
diff --git a/ext/sockets/tests/socket_tcp_keepalive.phpt b/ext/sockets/tests/socket_tcp_keepalive.phpt
new file mode 100644
index 00000000000..cef570dc62b
--- /dev/null
+++ b/ext/sockets/tests/socket_tcp_keepalive.phpt
@@ -0,0 +1,52 @@
+--TEST--
+Test SO_KEEPALIVE and TCP keepalive constants
+--EXTENSIONS--
+sockets
+--SKIPIF--
+<?php
+if (!defined('TCP_KEEPIDLE') && !defined('TCP_KEEPALIVE')) {
+ die('skip TCP_KEEPIDLE/TCP_KEEPALIVE not available');
+}
+if (!defined('TCP_KEEPINTVL')) {
+ die('skip TCP_KEEPINTVL not available');
+}
+if (!defined('TCP_KEEPCNT')) {
+ die('skip TCP_KEEPCNT not available');
+}
+?>
+--FILE--
+<?php
+$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
+if (!$socket) {
+ die("socket failed");
+}
+
+socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
+$keepalive = socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);
+echo "SO_KEEPALIVE: " . ($keepalive ? "enabled" : "disabled") . "\n";
+
+if (defined('TCP_KEEPIDLE')) {
+ socket_set_option($socket, SOL_TCP, TCP_KEEPIDLE, 60);
+ $idle = socket_get_option($socket, SOL_TCP, TCP_KEEPIDLE);
+ echo "TCP_KEEPIDLE: $idle\n";
+} else {
+ socket_set_option($socket, SOL_TCP, TCP_KEEPALIVE, 60);
+ $idle = socket_get_option($socket, SOL_TCP, TCP_KEEPALIVE);
+ echo "TCP_KEEPIDLE: $idle\n";
+}
+
+socket_set_option($socket, SOL_TCP, TCP_KEEPINTVL, 10);
+$intvl = socket_get_option($socket, SOL_TCP, TCP_KEEPINTVL);
+echo "TCP_KEEPINTVL: $intvl\n";
+
+socket_set_option($socket, SOL_TCP, TCP_KEEPCNT, 5);
+$cnt = socket_get_option($socket, SOL_TCP, TCP_KEEPCNT);
+echo "TCP_KEEPCNT: $cnt\n";
+
+socket_close($socket);
+?>
+--EXPECT--
+SO_KEEPALIVE: enabled
+TCP_KEEPIDLE: 60
+TCP_KEEPINTVL: 10
+TCP_KEEPCNT: 5