From 8a16af2bd8659a93b995d86a352d9b57f89c9daf Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 18 Nov 2011 11:13:30 -0600 Subject: [PATCH] keyfile: add support for Infiniband connections --- src/settings/plugins/keyfile/reader.c | 59 ++++---- .../keyfile/tests/keyfiles/Makefile.am | 3 +- .../tests/keyfiles/Test_Infiniband_Connection | 12 ++ .../plugins/keyfile/tests/test-keyfile.c | 132 ++++++++++++++++++ src/settings/plugins/keyfile/writer.c | 12 +- 5 files changed, 188 insertions(+), 30 deletions(-) create mode 100644 src/settings/plugins/keyfile/tests/keyfiles/Test_Infiniband_Connection diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index f973ce8b4c..67e3794266 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -655,7 +656,7 @@ mac_address_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, cons gint *tmp_list; GByteArray *array = NULL; gsize length; - int i; + int i, type; p = tmp_string = g_key_file_get_string (keyfile, setting_name, key, NULL); if (tmp_string) { @@ -666,41 +667,45 @@ mac_address_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, cons i++; p++; } - if (i == 5) { - /* parse as a MAC address */ - array = nm_utils_hwaddr_atoba (tmp_string, ARPHRD_ETHER); - if (array) { - g_free (tmp_string); - goto done; - } - } + + /* If we found enough it's probably a string-format MAC address */ + type = nm_utils_hwaddr_type (i + 1); + if (type > 0) + array = nm_utils_hwaddr_atoba (tmp_string, type); } g_free (tmp_string); - /* Old format; list of ints */ - tmp_list = g_key_file_get_integer_list (keyfile, setting_name, key, &length, NULL); - array = g_byte_array_sized_new (length); - for (i = 0; i < length; i++) { - int val = tmp_list[i]; - unsigned char v = (unsigned char) (val & 0xFF); + if (array == NULL) { + /* Old format; list of ints */ + tmp_list = g_key_file_get_integer_list (keyfile, setting_name, key, &length, NULL); + type = nm_utils_hwaddr_type (length); + if (type < 0) { + array = g_byte_array_sized_new (length); + for (i = 0; i < length; i++) { + int val = tmp_list[i]; + const guint8 v = (guint8) (val & 0xFF); - if (val < 0 || val > 255) { - g_warning ("%s: %s / %s ignoring invalid byte element '%d' (not " - " between 0 and 255 inclusive)", __func__, setting_name, - key, val); - } else - g_byte_array_append (array, (const unsigned char *) &v, sizeof (v)); + if (val < 0 || val > 255) { + g_warning ("%s: %s / %s ignoring invalid byte element '%d' (not " + " between 0 and 255 inclusive)", __func__, setting_name, + key, val); + g_byte_array_free (array, TRUE); + array = NULL; + break; + } + g_byte_array_append (array, &v, 1); + } + } + g_free (tmp_list); } - g_free (tmp_list); -done: - if (array->len == ETH_ALEN) { + if (array) { g_object_set (setting, key, array, NULL); + g_byte_array_free (array, TRUE); } else { g_warning ("%s: ignoring invalid MAC address for %s / %s", __func__, setting_name, key); } - g_byte_array_free (array, TRUE); } static void @@ -1033,6 +1038,10 @@ static KeyParser key_parsers[] = { NM_SETTING_BLUETOOTH_BDADDR, TRUE, mac_address_parser }, + { NM_SETTING_INFINIBAND_SETTING_NAME, + NM_SETTING_INFINIBAND_MAC_ADDRESS, + TRUE, + mac_address_parser }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, TRUE, diff --git a/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am b/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am index 55dda7ee06..e95ec86967 100644 --- a/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am +++ b/src/settings/plugins/keyfile/tests/keyfiles/Makefile.am @@ -13,7 +13,8 @@ KEYFILES = \ Test_Wired_TLS_Old \ Test_Wired_TLS_New \ Test_Wired_TLS_Blob \ - Test_Wired_TLS_Path_Missing + Test_Wired_TLS_Path_Missing \ + Test_Infiniband_Connection CERTS = \ test-ca-cert.pem \ diff --git a/src/settings/plugins/keyfile/tests/keyfiles/Test_Infiniband_Connection b/src/settings/plugins/keyfile/tests/keyfiles/Test_Infiniband_Connection new file mode 100644 index 0000000000..620df3c031 --- /dev/null +++ b/src/settings/plugins/keyfile/tests/keyfiles/Test_Infiniband_Connection @@ -0,0 +1,12 @@ +[connection] +id=Test Infiniband Connection +uuid=4e80a56d-c99f-4aad-a6dd-b449bc398c57 +type=infiniband + +[infiniband] +mac-address=00:11:22:33:44:55:66:77:88:99:01:12:23:34:45:56:67:78:89:90 +mtu=1400 + +[ipv4] +method=auto + diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 2859cb3e48..5452f4f736 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -38,6 +39,7 @@ #include #include #include +#include #include "nm-test-helpers.h" @@ -2828,6 +2830,133 @@ test_write_wired_8021x_tls_connection_blob (void) g_object_unref (connection); } +#define TEST_INFINIBAND_FILE TEST_KEYFILES_DIR"/Test_Infiniband_Connection" + +static void +test_read_infiniband_connection (void) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingInfiniband *s_ib; + GError *error = NULL; + const GByteArray *array; + guint8 expected_mac[INFINIBAND_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, + 0x77, 0x88, 0x99, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, + 0x90 }; + const char *expected_id = "Test Infiniband Connection"; + const char *expected_uuid = "4e80a56d-c99f-4aad-a6dd-b449bc398c57"; + gboolean success; + + connection = nm_keyfile_plugin_connection_from_file (TEST_INFINIBAND_FILE, &error); + g_assert_no_error (error); + g_assert (connection); + success = nm_connection_verify (connection, &error); + g_assert_no_error (error); + g_assert (success); + + /* Connection setting */ + s_con = nm_connection_get_setting_connection (connection); + g_assert (s_con); + g_assert_cmpstr (nm_setting_connection_get_id (s_con), ==, expected_id); + g_assert_cmpstr (nm_setting_connection_get_uuid (s_con), ==, expected_uuid); + + /* Infiniband setting */ + s_ib = nm_connection_get_setting_infiniband (connection); + g_assert (s_ib); + + array = nm_setting_infiniband_get_mac_address (s_ib); + g_assert (array); + g_assert_cmpint (array->len, ==, INFINIBAND_ALEN); + g_assert_cmpint (memcmp (array->data, expected_mac, sizeof (expected_mac)), ==, 0); + + g_object_unref (connection); +} + +static void +test_write_infiniband_connection (void) +{ + NMConnection *connection; + NMSettingConnection *s_con; + NMSettingInfiniband *s_ib; + NMSettingIP4Config *s_ip4; + NMSettingIP6Config *s_ip6; + char *uuid; + GByteArray *mac; + guint8 tmpmac[] = { 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0xab, 0xbc, + 0xcd, 0xde, 0xef, 0xf0, 0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f, 0x6f, 0xba + }; + gboolean success; + NMConnection *reread; + char *testfile = NULL; + GError *error = NULL; + pid_t owner_grp; + uid_t owner_uid; + + connection = nm_connection_new (); + g_assert (connection); + + /* Connection setting */ + + s_con = (NMSettingConnection *) nm_setting_connection_new (); + g_assert (s_con); + nm_connection_add_setting (connection, NM_SETTING (s_con)); + + uuid = nm_utils_uuid_generate (); + g_object_set (s_con, + NM_SETTING_CONNECTION_ID, "Work Infiniband", + NM_SETTING_CONNECTION_UUID, uuid, + NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, + NM_SETTING_CONNECTION_TYPE, NM_SETTING_INFINIBAND_SETTING_NAME, + NULL); + g_free (uuid); + + /* Infiniband setting */ + s_ib = (NMSettingInfiniband *) nm_setting_infiniband_new (); + g_assert (s_ib); + nm_connection_add_setting (connection, NM_SETTING (s_ib)); + + mac = g_byte_array_sized_new (sizeof (tmpmac)); + g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); + g_object_set (s_ib, + NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, + NM_SETTING_INFINIBAND_MTU, 900, + NULL); + g_byte_array_free (mac, TRUE); + + /* IP4 setting */ + s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + g_assert (s_ip4); + nm_connection_add_setting (connection, NM_SETTING (s_ip4)); + g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + + /* IP6 setting */ + s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + g_assert (s_ip6); + nm_connection_add_setting (connection, NM_SETTING (s_ip6)); + g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + + /* Write out the connection */ + owner_uid = geteuid (); + owner_grp = getegid (); + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); + g_assert_no_error (error); + g_assert (success); + g_assert (testfile); + + /* Read the connection back in and compare it to the one we just wrote out */ + reread = nm_keyfile_plugin_connection_from_file (testfile, &error); + g_assert_no_error (error); + g_assert (reread); + + g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + + unlink (testfile); + g_free (testfile); + + g_object_unref (reread); + g_object_unref (connection); +} + int main (int argc, char **argv) { GError *error = NULL; @@ -2876,6 +3005,9 @@ int main (int argc, char **argv) test_write_wired_8021x_tls_connection_path (); test_write_wired_8021x_tls_connection_blob (); + test_read_infiniband_connection (); + test_write_infiniband_connection (); + base = g_path_get_basename (argv[0]); fprintf (stdout, "%s: SUCCESS\n", base); g_free (base); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 3bc8260907..bbd6a7c3b7 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -416,7 +416,7 @@ mac_address_writer (GKeyFile *file, GByteArray *array; const char *setting_name = nm_setting_get_name (setting); char *mac; - struct ether_addr tmp; + int type; g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UCHAR_ARRAY)); @@ -424,15 +424,16 @@ mac_address_writer (GKeyFile *file, if (!array) return; - if (array->len != ETH_ALEN) { + type = nm_utils_hwaddr_type (array->len); + if (type < 0) { g_warning ("%s: invalid %s / %s MAC address length %d", __func__, setting_name, key, array->len); return; } - memcpy (tmp.ether_addr_octet, array->data, ETH_ALEN); - mac = ether_ntoa (&tmp); + mac = nm_utils_hwaddr_ntoa (array->data, type); g_key_file_set_string (file, setting_name, key, mac); + g_free (mac); } static void @@ -812,6 +813,9 @@ static KeyWriter key_writers[] = { { NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR, mac_address_writer }, + { NM_SETTING_INFINIBAND_SETTING_NAME, + NM_SETTING_INFINIBAND_MAC_ADDRESS, + mac_address_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, ssid_writer },