Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tcp keepalive support #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ Bindings for the libssh2 library.

Provides bindings to the functions of [libssh2](http:/libssh2.org/) which implements the SSH2 protocol.

Version with tcp keepalive support ( setsockopt SO_KEEPALIVE flag ).
For enable tcp keepalive set ssh2.keepalive=1 in your php.ini .
To configure tcp keepalive timeouts must be set by kernel parameters:
```
net.ipv4.tcp_keepalive_time
net.ipv4.tcp_keepalive_intvl
net.ipv4.tcp_keepalive_probes
```

[The ssh2 extension at the PECL Repository website](http://pecl.php.net/package/ssh2)

Documentation
Expand Down
20 changes: 20 additions & 0 deletions ssh2.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#define MD5_DIGEST_LENGTH 16
#endif

PHP_INI_BEGIN()
PHP_INI_ENTRY("ssh2.keepalive", "0", PHP_INI_ALL, NULL)
PHP_INI_END()

/* True global resources - no need for thread safety here */
int le_ssh2_session;
int le_ssh2_listener;
Expand Down Expand Up @@ -314,6 +318,14 @@ LIBSSH2_SESSION *php_ssh2_session_connect(char *host, int port, zval *methods, z
return NULL;
}

// tcp keepalive
zend_bool flag_keepalive = INI_BOOL("ssh2.keepalive");
int flags = 1;
if (flag_keepalive) {
if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)) < 0)
php_error_docref(NULL, E_WARNING, "setsocketopt(), SO_KEEPALIVE error for %s on port %d", host, port);
}

data = ecalloc(1, sizeof(php_ssh2_session_data));
data->socket = socket;

Expand Down Expand Up @@ -1355,6 +1367,8 @@ PHP_MINIT_FUNCTION(ssh2)
REGISTER_LONG_CONSTANT("SSH2_POLL_CHANNEL_CLOSED", LIBSSH2_POLLFD_CHANNEL_CLOSED, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SSH2_POLL_LISTENER_CLOSED", LIBSSH2_POLLFD_LISTENER_CLOSED, CONST_CS | CONST_PERSISTENT);

REGISTER_INI_ENTRIES();

return (php_register_url_stream_wrapper("ssh2.shell", &php_ssh2_stream_wrapper_shell) == SUCCESS &&
php_register_url_stream_wrapper("ssh2.exec", &php_ssh2_stream_wrapper_exec) == SUCCESS &&
php_register_url_stream_wrapper("ssh2.tunnel", &php_ssh2_stream_wrapper_tunnel) == SUCCESS &&
Expand All @@ -1367,6 +1381,9 @@ PHP_MINIT_FUNCTION(ssh2)
*/
PHP_MSHUTDOWN_FUNCTION(ssh2)
{

UNREGISTER_INI_ENTRIES();

return (php_unregister_url_stream_wrapper("ssh2.shell") == SUCCESS &&
php_unregister_url_stream_wrapper("ssh2.exec") == SUCCESS &&
php_unregister_url_stream_wrapper("ssh2.tunnel") == SUCCESS &&
Expand All @@ -1385,6 +1402,9 @@ PHP_MINFO_FUNCTION(ssh2)
php_info_print_table_row(2, "libssh2 version", LIBSSH2_VERSION);
php_info_print_table_row(2, "banner", LIBSSH2_SSH_BANNER);
php_info_print_table_end();

DISPLAY_INI_ENTRIES();

}
/* }}} */

Expand Down