[PECL-CVS] [pecl-networking-ssh2] master: Expose libssh2_set_timeout() (#70)

[email protected] (Jille Timmermans via GitHub) Sat, 4 Apr 2026 10:22:03 +0000
Newsgroups php.pecl.cvs
Message-ID <[email protected]>
Author: Jille Timmermans (Jille)
Committer: GitHub (web-flow)
Pusher: rlerdorf
Date: 2026-04-04T06:22:01-04:00

Commit: https://github.com/php/pecl-networking-ssh2/commit/c497cc257b662dcd605a151e30f58938b1decd16
Raw diff: https://github.com/php/pecl-networking-ssh2/commit/c497cc257b662dcd605a151e30f58938b1decd16.diff

Expose libssh2_set_timeout() (#70)

Changed paths:
  M  ssh2.c


Diff:

diff --git a/ssh2.c b/ssh2.c
index 70093b4..305071a 100644
--- a/ssh2.c
+++ b/ssh2.c
@@ -451,6 +451,30 @@ PHP_FUNCTION(ssh2_disconnect)
 }
 /* }}} */
 
+/* {{{ proto void ssh2_set_timeout(resource session, long seconds [, long microseconds])
+ * Set the timeout in seconds + microseconds for how long a blocking the libssh2 function calls may wait until they consider the situation an error and return LIBSSH2_ERROR_TIMEOUT.
+ * This method takes microseconds to align with stream_set_timeout, but the underlying library call allows only millisecond precision so this function rounds up to millisecond.
+ *
+ * By default or if you set the timeout to zero, libssh2 has no timeout for blocking functions.
+ */
+PHP_FUNCTION(ssh2_set_timeout)
+{
+	LIBSSH2_SESSION *session;
+	zval *zsession;
+	zend_long seconds, microseconds;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|l", &zsession, &seconds, &microseconds) == FAILURE) {
+		return;
+	}
+
+	if ((session = (LIBSSH2_SESSION *)zend_fetch_resource(Z_RES_P(zsession), PHP_SSH2_SESSION_RES_NAME, le_ssh2_session)) == NULL) {
+		return;
+	}
+
+	libssh2_session_set_timeout(session, seconds * 1000 + (microseconds + 1000-1) / 1000);
+}
+/* }}} */
+
 /* {{{ proto array ssh2_methods_negotiated(resource session)
  * Return list of negotiaed methods
  */
@@ -1400,6 +1424,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_ssh2_disconnect, 1)
  	ZEND_ARG_INFO(0, session)
 ZEND_END_ARG_INFO()
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_ssh2_set_timeout, 0, 0, 2)
+ 	ZEND_ARG_INFO(0, session)
+ 	ZEND_ARG_INFO(0, seconds)
+ 	ZEND_ARG_INFO(0, microseconds)
+ZEND_END_ARG_INFO()
+
 ZEND_BEGIN_ARG_INFO(arginfo_ssh2_methods_negotiated, 1)
  	ZEND_ARG_INFO(0, session)
 ZEND_END_ARG_INFO()
@@ -1609,6 +1639,7 @@ ZEND_END_ARG_INFO()
 zend_function_entry ssh2_functions[] = {
 	PHP_FE(ssh2_connect,						arginfo_ssh2_connect)
 	PHP_FE(ssh2_disconnect,						arginfo_ssh2_disconnect)
+	PHP_FE(ssh2_set_timeout,					arginfo_ssh2_set_timeout)
 	PHP_FE(ssh2_methods_negotiated,				arginfo_ssh2_methods_negotiated)
 	PHP_FE(ssh2_fingerprint,					arginfo_ssh2_fingerprint)