connect: Support AddressFamily option

* allow parsing of AddressFamily in config and cli
  * supports options "any", "inet" and "inet6"
* introduce SSH_OPTIONS_ADDRESS_FAMILY

Signed-off-by: Samir Benmendil <me@rmz.io>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Samir Benmendil
2025-12-15 19:16:15 +00:00
parent f52be27114
commit c4f1a70a89
10 changed files with 247 additions and 12 deletions

View File

@@ -20,6 +20,7 @@
*/
#include "config.h"
#include "torture_cmocka.h"
#define LIBSSH_STATIC
@@ -143,6 +144,48 @@ static void torture_connect_ipv6(void **state) {
assert_ssh_return_code(session, rc);
}
static void torture_connect_addrfamily(void **state)
{
struct torture_state *s = *state;
ssh_session session = s->ssh.session;
int rc;
struct aftest {
enum ssh_address_family_options_e family;
char const *host;
int return_code;
};
static struct aftest aftests[] = {
{SSH_ADDRESS_FAMILY_ANY, "afboth", SSH_OK},
{SSH_ADDRESS_FAMILY_INET, "afboth", SSH_OK},
{SSH_ADDRESS_FAMILY_INET6, "afboth", SSH_OK},
{SSH_ADDRESS_FAMILY_ANY, "afinet", SSH_OK},
{SSH_ADDRESS_FAMILY_INET, "afinet", SSH_OK},
{SSH_ADDRESS_FAMILY_INET6, "afinet", SSH_ERROR},
{SSH_ADDRESS_FAMILY_ANY, "afinet6", SSH_OK},
{SSH_ADDRESS_FAMILY_INET, "afinet6", SSH_ERROR},
{SSH_ADDRESS_FAMILY_INET6, "afinet6", SSH_OK},
};
int aftest_count = sizeof(aftests) / sizeof(aftests[0]);
for (int i = 0; i < aftest_count; ++i) {
struct aftest const *t = &aftests[i];
rc = ssh_options_set(session, SSH_OPTIONS_ADDRESS_FAMILY, &t->family);
assert_ssh_return_code(session, rc);
rc = ssh_options_set(session, SSH_OPTIONS_HOST, t->host);
assert_ssh_return_code(session, rc);
do {
rc = ssh_connect(session);
} while (rc == SSH_AGAIN);
assert_ssh_return_code_equal(session, rc, t->return_code);
ssh_disconnect(session);
}
}
#if 0 /* This does not work with socket_wrapper */
static void torture_connect_timeout(void **state) {
struct torture_state *s = *state;
@@ -329,6 +372,9 @@ int torture_run_tests(void) {
cmocka_unit_test_setup_teardown(torture_connect_ipv6,
session_setup,
session_teardown),
cmocka_unit_test_setup_teardown(torture_connect_addrfamily,
session_setup,
session_teardown),
cmocka_unit_test_setup_teardown(torture_connect_double,
session_setup,
session_teardown),

View File

@@ -5,3 +5,8 @@
123.0.0.11 testing
fd00::5357:5f0a testing
127.0.0.10 afboth
fd00::5357:5f0a afboth
127.0.0.10 afinet
fd00::5357:5f0a afinet6

View File

@@ -46,6 +46,7 @@ extern LIBSSH_THREAD int ssh_log_level;
#define LIBSSH_TESTCONFIG15 "libssh_testconfig15.tmp"
#define LIBSSH_TESTCONFIG16 "libssh_testconfig16.tmp"
#define LIBSSH_TESTCONFIG17 "libssh_testconfig17.tmp"
#define LIBSSH_TESTCONFIG18 "libssh_testconfig18.tmp"
#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp"
#define LIBSSH_TEST_PUBKEYTYPES "libssh_test_PubkeyAcceptedKeyTypes.tmp"
#define LIBSSH_TEST_PUBKEYALGORITHMS "libssh_test_PubkeyAcceptedAlgorithms.tmp"
@@ -222,6 +223,15 @@ extern LIBSSH_THREAD int ssh_log_level;
"\tControlMaster yes\n" \
"\tControlPath none\n"
#define LIBSSH_TESTCONFIG_STRING18 \
"Host simple\n" \
"Host af\n" \
"\tAddressFamily any\n" \
"Host af4\n" \
"\tAddressFamily inet\n" \
"Host af6\n" \
"\tAddressFamily inet6\n"
#define LIBSSH_TEST_PUBKEYTYPES_STRING \
"PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES"\n"
@@ -292,6 +302,7 @@ static int setup_config_files(void **state)
unlink(LIBSSH_TESTCONFIG15);
unlink(LIBSSH_TESTCONFIG16);
unlink(LIBSSH_TESTCONFIG17);
unlink(LIBSSH_TESTCONFIG18);
unlink(LIBSSH_TEST_PUBKEYTYPES);
unlink(LIBSSH_TEST_PUBKEYALGORITHMS);
unlink(LIBSSH_TEST_NONEWLINEEND);
@@ -350,6 +361,8 @@ static int setup_config_files(void **state)
LIBSSH_TESTCONFIG_STRING16);
torture_write_file(LIBSSH_TESTCONFIG17,
LIBSSH_TESTCONFIG_STRING17);
torture_write_file(LIBSSH_TESTCONFIG18,
LIBSSH_TESTCONFIG_STRING18);
torture_write_file(LIBSSH_TEST_PUBKEYTYPES,
LIBSSH_TEST_PUBKEYTYPES_STRING);
@@ -392,6 +405,7 @@ static int teardown_config_files(void **state)
unlink(LIBSSH_TESTCONFIG15);
unlink(LIBSSH_TESTCONFIG16);
unlink(LIBSSH_TESTCONFIG17);
unlink(LIBSSH_TESTCONFIG18);
unlink(LIBSSH_TEST_PUBKEYTYPES);
unlink(LIBSSH_TEST_PUBKEYALGORITHMS);
unlink(LIBSSH_TEST_NONEWLINEEND);
@@ -1520,6 +1534,79 @@ static void torture_config_control_master_file(void **state)
torture_config_control_master(state, LIBSSH_TESTCONFIG17, NULL);
}
/**
* @brief Verify we can parse AdressFamily configuration option
*/
static void torture_config_address_family(void **state,
const char *file,
const char *string)
{
ssh_session session = *state;
const char *config;
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "simple");
_parse_config(session, file, string, SSH_OK);
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_ANY);
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "af");
_parse_config(session, file, string, SSH_OK);
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_ANY);
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "af4");
_parse_config(session, file, string, SSH_OK);
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_INET);
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "af6");
_parse_config(session, file, string, SSH_OK);
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_INET6);
/* test for parsing failures */
config = "Host afmissing\n"
"\tAddressFamily\n";
if (file != NULL) {
torture_write_file(file, config);
} else {
string = config;
}
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "afmissing");
_parse_config(session, file, string, SSH_ERROR);
config = "Host afinvalid\n"
"\tAddressFamily wurstkäse\n";
if (file != NULL) {
torture_write_file(file, config);
} else {
string = config;
}
torture_reset_config(session);
ssh_options_set(session, SSH_OPTIONS_HOST, "afinvalid");
_parse_config(session, file, string, SSH_ERROR);
}
/**
* @brief Verify we can parse AdressFamily configuration option from string
*/
static void torture_config_address_family_string(void **state)
{
torture_config_address_family(state, NULL, LIBSSH_TESTCONFIG_STRING18);
}
/**
* @brief Verify we can parse AdressFamily configuration option from file
*/
static void torture_config_address_family_file(void **state)
{
torture_config_address_family(state, LIBSSH_TESTCONFIG18, NULL);
}
/**
* @brief Verify the configuration parser handles all the possible
* versions of RekeyLimit configuration option.
@@ -2707,6 +2794,12 @@ int torture_run_tests(void)
cmocka_unit_test_setup_teardown(torture_config_control_master_string,
setup,
teardown),
cmocka_unit_test_setup_teardown(torture_config_address_family_file,
setup,
teardown),
cmocka_unit_test_setup_teardown(torture_config_address_family_string,
setup,
teardown),
cmocka_unit_test_setup_teardown(torture_config_rekey_file,
setup,
teardown),

View File

@@ -1352,6 +1352,7 @@ static void torture_options_copy(void **state)
"GSSAPIDelegateCredentials yes\n"
"PubkeyAuthentication yes\n" /* sets flags */
"GSSAPIAuthentication no\n" /* sets flags */
"AddressFamily inet6\n"
"",
config);
fclose(config);
@@ -1428,6 +1429,7 @@ static void torture_options_copy(void **state)
assert_true(session->opts.config_processed == new->opts.config_processed);
assert_memory_equal(session->opts.options_seen, new->opts.options_seen,
sizeof(session->opts.options_seen));
assert_int_equal(session->opts.address_family, new->opts.address_family);
ssh_free(new);