aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLee Hambley <lee.hambley@gmail.com>2011-10-28 10:26:32 +0200
committerAndreas Schneider <asn@cryptomilk.org>2011-10-28 12:15:12 +0200
commite797781bb54685a69735d8b1e84dd26a69227c47 (patch)
tree0b31b317214513c05508140cdbed811b39342c6f
parent6bd95b50f535d07594f3659c67a1f5e501887641 (diff)
downloadlibssh-e797781bb54685a69735d8b1e84dd26a69227c47.tar.gz
libssh-e797781bb54685a69735d8b1e84dd26a69227c47.tar.xz
libssh-e797781bb54685a69735d8b1e84dd26a69227c47.zip
Implement ssh_options_get_port(ssh_session, unsigned int*).
-rw-r--r--include/libssh/libssh.h1
-rw-r--r--src/options.c26
-rw-r--r--tests/unittests/torture_options.c14
3 files changed, 41 insertions, 0 deletions
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 0104c8a..961dfc0 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -429,6 +429,7 @@ LIBSSH_API int ssh_options_set(ssh_session session, enum ssh_options_e type,
const void *value);
LIBSSH_API int ssh_options_get(ssh_session session, enum ssh_options_e type,
char **value);
+LIBSSH_API int ssh_options_get_port(ssh_session session, unsigned int * port_target);
LIBSSH_API int ssh_pcap_file_close(ssh_pcap_file pcap);
LIBSSH_API void ssh_pcap_file_free(ssh_pcap_file pcap);
LIBSSH_API ssh_pcap_file ssh_pcap_file_new(void);
diff --git a/src/options.c b/src/options.c
index f6b1ae9..4dbcb67 100644
--- a/src/options.c
+++ b/src/options.c
@@ -779,6 +779,32 @@ int ssh_options_set(ssh_session session, enum ssh_options_e type,
}
/**
+ * @brief This function can get ssh the ssh port. It must only be used on
+ * a valid ssh session. This function is useful when the session
+ * options have been automatically inferred from the environment
+ * or configuration files and one
+ *
+ * @param session An allocated SSH session structure.
+ *
+ * @param unsigned int An unsigned integer into which the
+ * port will be set from the ssh session.
+ *
+ * @return 0 on success, < 0 on error.
+ *
+ */
+int ssh_options_get_port(ssh_session session, unsigned int* port_target) {
+ if (session == NULL) {
+ return -1;
+ }
+ if (!session->port) {
+ ssh_set_error_invalid(session);
+ return -1;
+ }
+ *port_target = session->port;
+ return 0;
+}
+
+/**
* @brief This function can get ssh options, it does not support all options provided for
* ssh options set, but mostly those which a user-space program may care about having
* trusted the ssh driver to infer these values from underlaying configuration files.
diff --git a/tests/unittests/torture_options.c b/tests/unittests/torture_options.c
index 059095a..99dacef 100644
--- a/tests/unittests/torture_options.c
+++ b/tests/unittests/torture_options.c
@@ -66,6 +66,19 @@ static void torture_options_set_port(void **state) {
assert_true(rc == -1);
}
+static void torture_options_get_port(void **state) {
+ ssh_session session = *state;
+ ssh_session uninitialized_session = NULL;
+ unsigned int given_port = 1234;
+ unsigned int port_container;
+ int rc;
+ rc = ssh_options_set(session, SSH_OPTIONS_PORT, &given_port);
+ assert_true(rc == 0);
+ rc = ssh_options_get_port(session, &port_container);
+ assert_true(rc == 0);
+ assert_int_equal(port_container, 1234);
+}
+
static void torture_options_get_user(void **state) {
ssh_session session = *state;
char* user = NULL;
@@ -166,6 +179,7 @@ int torture_run_tests(void) {
unit_test_setup_teardown(torture_options_set_host, setup, teardown),
unit_test_setup_teardown(torture_options_get_host, setup, teardown),
unit_test_setup_teardown(torture_options_set_port, setup, teardown),
+ unit_test_setup_teardown(torture_options_get_port, setup, teardown),
unit_test_setup_teardown(torture_options_set_fd, setup, teardown),
unit_test_setup_teardown(torture_options_set_user, setup, teardown),
unit_test_setup_teardown(torture_options_get_user, setup, teardown),