diff --git a/.gitignore b/.gitignore index 29bb2b7d9f..bd177a1f35 100644 --- a/.gitignore +++ b/.gitignore @@ -137,6 +137,7 @@ valgrind-*.log /libgsystem/ /libnm-core/nm-version.h +/libnm-core/tests/test-compare /libnm-core/tests/test-crypto /libnm-core/tests/test-settings-defaults /libnm-core/tests/test-general diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 1a8bcd0d9e..74279bec1c 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2456,34 +2456,23 @@ is_setting_mandatory (NMConnection *connection, NMSetting *setting) /*----------------------------------------------------------------------------*/ static gboolean -check_and_convert_mac (const char *mac, - GByteArray **mac_array, - int type, - const char *keyword, - GError **error) +check_mac (const char *mac, + int type, + const char *keyword, + GError **error) { - GByteArray *local_mac_array = NULL; - - g_return_val_if_fail (mac_array == NULL || *mac_array == NULL, FALSE); g_return_val_if_fail (type == ARPHRD_ETHER || type == ARPHRD_INFINIBAND, FALSE); if (!mac) return TRUE; - local_mac_array = nm_utils_hwaddr_atoba (mac, nm_utils_hwaddr_len (type)); - if (!local_mac_array) { + if (!nm_utils_hwaddr_valid (mac, type)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("Error: '%s': '%s' is not a valid %s MAC address."), keyword, mac, type == ARPHRD_INFINIBAND ? _("InfiniBand") : _("Ethernet")); return FALSE; } - if (mac_array) - *mac_array = local_mac_array; - else - if (local_mac_array) - g_byte_array_free (local_mac_array, TRUE); - return TRUE; } @@ -2879,7 +2868,7 @@ do_questionnaire_ethernet (gboolean ethernet, char **mtu, char **mac, char **clo if (!*mac) { do { *mac = nmc_readline (_("MAC [none]: ")); - once_more = !check_and_convert_mac (*mac, NULL, ARPHRD_ETHER, "mac", &error); + once_more = !check_mac (*mac, ARPHRD_ETHER, "mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -2890,7 +2879,7 @@ do_questionnaire_ethernet (gboolean ethernet, char **mtu, char **mac, char **clo if (!*cloned_mac) { do { *cloned_mac = nmc_readline (_("Cloned MAC [none]: ")); - once_more = !check_and_convert_mac (*cloned_mac, NULL, ARPHRD_ETHER, "cloned-mac", &error); + once_more = !check_mac (*cloned_mac, ARPHRD_ETHER, "cloned-mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -2927,7 +2916,7 @@ do_questionnaire_infiniband (char **mtu, char **mac, char **mode, char **parent, if (!*mac) { do { *mac = nmc_readline (_("MAC [none]: ")); - once_more = !check_and_convert_mac (*mac, NULL, ARPHRD_INFINIBAND, "mac", &error); + once_more = !check_mac (*mac, ARPHRD_INFINIBAND, "mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -2997,7 +2986,7 @@ do_questionnaire_wimax (char **mac) if (!*mac) { do { *mac = nmc_readline (_("MAC [none]: ")); - once_more = !check_and_convert_mac (*mac, NULL, ARPHRD_ETHER, "mac", &error); + once_more = !check_mac (*mac, ARPHRD_ETHER, "mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -3036,7 +3025,7 @@ do_questionnaire_pppoe (char **password, char **service, char **mtu, char **mac) if (!*mac) { do { *mac = nmc_readline (_("MAC [none]: ")); - once_more = !check_and_convert_mac (*mac, NULL, ARPHRD_ETHER, "mac", &error); + once_more = !check_mac (*mac, ARPHRD_ETHER, "mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -3391,7 +3380,7 @@ do_questionnaire_bridge (char **stp, char **priority, char **fwd_delay, char **h if (!*mac) { do { *mac = nmc_get_user_input (_("MAC [none]: ")); - once_more = !check_and_convert_mac (*mac, NULL, ARPHRD_ETHER, "mac", &error); + once_more = !check_mac (*mac, ARPHRD_ETHER, "mac", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -3490,7 +3479,7 @@ do_questionnaire_olpc (char **channel, char **dhcp_anycast) if (!*dhcp_anycast) { do { *dhcp_anycast = nmc_readline (_("DHCP anycast MAC address [none]: ")); - once_more = !check_and_convert_mac (*dhcp_anycast, NULL, ARPHRD_ETHER, "dhcp-anycast", &error); + once_more = !check_mac (*dhcp_anycast, ARPHRD_ETHER, "dhcp-anycast", &error); if (once_more) { printf ("%s\n", error->message); g_clear_error (&error); @@ -3635,8 +3624,6 @@ complete_connection_by_type (NMConnection *connection, char *mac = NULL; const char *cloned_mac_c = NULL; char *cloned_mac = NULL; - GByteArray *array = NULL; - GByteArray *cloned_array = NULL; nmc_arg_t exp_args[] = { {"mtu", TRUE, &mtu_c, FALSE}, {"mac", TRUE, &mac_c, FALSE}, {"cloned-mac", TRUE, &cloned_mac_c, FALSE}, @@ -3654,9 +3641,9 @@ complete_connection_by_type (NMConnection *connection, if (!check_and_convert_mtu (mtu, &mtu_int, error)) goto cleanup_wired; - if (!check_and_convert_mac (mac, &array, ARPHRD_ETHER, "mac", error)) + if (!check_mac (mac, ARPHRD_ETHER, "mac", error)) goto cleanup_wired; - if (!check_and_convert_mac (cloned_mac, &cloned_array, ARPHRD_ETHER, "cloned-mac", error)) + if (!check_mac (cloned_mac, ARPHRD_ETHER, "cloned-mac", error)) goto cleanup_wired; /* Add ethernet setting */ @@ -3665,20 +3652,16 @@ complete_connection_by_type (NMConnection *connection, if (mtu) g_object_set (s_wired, NM_SETTING_WIRED_MTU, mtu_int, NULL); - if (array) - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, array, NULL); - if (cloned_array) - g_object_set (s_wired, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, cloned_array, NULL); + if (mac) + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); + if (cloned_mac) + g_object_set (s_wired, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, cloned_mac, NULL); success = TRUE; cleanup_wired: g_free (mtu); g_free (mac); g_free (cloned_mac); - if (array) - g_byte_array_free (array, TRUE); - if (cloned_array) - g_byte_array_free (cloned_array, TRUE); if (!success) return FALSE; @@ -3690,7 +3673,6 @@ cleanup_wired: guint32 mtu_int = 0; const char *mac_c = NULL; char *mac = NULL; - GByteArray *array = NULL; const char *mode_c = NULL; char *mode = NULL; const char *parent_c = NULL; @@ -3719,7 +3701,7 @@ cleanup_wired: if (!check_and_convert_mtu (mtu, &mtu_int, error)) goto cleanup_ib; - if (!check_and_convert_mac (mac, &array, ARPHRD_INFINIBAND, "mac", error)) + if (!check_mac (mac, ARPHRD_INFINIBAND, "mac", error)) goto cleanup_ib; if (!check_infiniband_mode (&mode, error)) goto cleanup_ib; @@ -3741,10 +3723,8 @@ cleanup_wired: g_object_set (s_infiniband, NM_SETTING_INFINIBAND_TRANSPORT_MODE, mode ? mode : "datagram", NULL); if (mtu) g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MTU, mtu_int, NULL); - if (array) { - g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, array, NULL); - g_byte_array_free (array, TRUE); - } + if (mac) + g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); if (p_key) g_object_set (s_infiniband, NM_SETTING_INFINIBAND_P_KEY, p_key_int, NULL); if (parent) @@ -3772,10 +3752,8 @@ cleanup_ib: guint32 mtu_int = 0; const char *mac_c = NULL; char *mac = NULL; - GByteArray *mac_array = NULL; const char *cloned_mac_c = NULL; char *cloned_mac = NULL; - GByteArray *cloned_mac_array = NULL; nmc_arg_t exp_args[] = { {"ssid", TRUE, &ssid, !ask}, {"mtu", TRUE, &mtu_c, FALSE}, {"mac", TRUE, &mac_c, FALSE}, @@ -3802,9 +3780,9 @@ cleanup_ib: if (!check_and_convert_mtu (mtu, &mtu_int, error)) goto cleanup_wifi; - if (!check_and_convert_mac (mac, &mac_array, ARPHRD_ETHER, "mac", error)) + if (!check_mac (mac, ARPHRD_ETHER, "mac", error)) goto cleanup_wifi; - if (!check_and_convert_mac (cloned_mac, &cloned_mac_array, ARPHRD_ETHER, "cloned-mac", error)) + if (!check_mac (cloned_mac, ARPHRD_ETHER, "cloned-mac", error)) goto cleanup_wifi; /* Add wifi setting */ @@ -3817,10 +3795,10 @@ cleanup_ib: if (mtu) g_object_set (s_wifi, NM_SETTING_WIRELESS_MTU, mtu_int, NULL); - if (mac_array) - g_object_set (s_wifi, NM_SETTING_WIRELESS_MAC_ADDRESS, mac_array, NULL); - if (cloned_mac_array) - g_object_set (s_wifi, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, cloned_mac_array, NULL); + if (mac) + g_object_set (s_wifi, NM_SETTING_WIRELESS_MAC_ADDRESS, mac, NULL); + if (cloned_mac) + g_object_set (s_wifi, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, cloned_mac, NULL); success = TRUE; cleanup_wifi: @@ -3830,10 +3808,6 @@ cleanup_wifi: g_free (cloned_mac); if (ssid_arr) g_byte_array_free (ssid_arr, TRUE); - if (mac_array) - g_byte_array_free (mac_array, TRUE); - if (cloned_mac_array) - g_byte_array_free (cloned_mac_array, TRUE); if (!success) return FALSE; @@ -3844,7 +3818,6 @@ cleanup_wifi: char *nsp_name_ask = NULL; const char *mac_c = NULL; char *mac = NULL; - GByteArray *mac_array = NULL; nmc_arg_t exp_args[] = { {"nsp", TRUE, &nsp_name, !ask}, {"mac", TRUE, &mac_c, FALSE}, {NULL} }; @@ -3865,7 +3838,7 @@ cleanup_wifi: if (ask) do_questionnaire_wimax (&mac); - if (!check_and_convert_mac (mac, &mac_array, ARPHRD_ETHER, "mac", error)) + if (!check_mac (mac, ARPHRD_ETHER, "mac", error)) goto cleanup_wimax; /* Add 'wimax' setting */ @@ -3873,10 +3846,8 @@ cleanup_wifi: nm_connection_add_setting (connection, NM_SETTING (s_wimax)); g_object_set (s_wimax, NM_SETTING_WIMAX_NETWORK_NAME, nsp_name, NULL); - if (mac_array) { - g_object_set (s_wimax, NM_SETTING_WIMAX_MAC_ADDRESS, mac_array, NULL); - g_byte_array_free (mac_array, TRUE); - } + if (mac) + g_object_set (s_wimax, NM_SETTING_WIMAX_MAC_ADDRESS, mac, NULL); success = TRUE; cleanup_wimax: @@ -3899,7 +3870,6 @@ cleanup_wimax: guint32 mtu_int = 0; const char *mac_c = NULL; char *mac = NULL; - GByteArray *mac_array = NULL; nmc_arg_t exp_args[] = { {"username", TRUE, &username, !ask}, {"password", TRUE, &password_c, FALSE}, {"service", TRUE, &service_c, FALSE}, @@ -3928,7 +3898,7 @@ cleanup_wimax: if (!check_and_convert_mtu (mtu, &mtu_int, error)) goto cleanup_pppoe; - if (!check_and_convert_mac (mac, &mac_array, ARPHRD_ETHER, "mac", error)) + if (!check_mac (mac, ARPHRD_ETHER, "mac", error)) goto cleanup_pppoe; /* Add 'pppoe' setting */ @@ -3943,8 +3913,8 @@ cleanup_wimax: nm_connection_add_setting (connection, NM_SETTING (s_wired)); if (mtu) g_object_set (s_wired, NM_SETTING_WIRED_MTU, mtu_int, NULL); - if (mac_array) - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac_array, NULL); + if (mac) + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); success = TRUE; cleanup_pppoe: @@ -3953,8 +3923,6 @@ cleanup_pppoe: g_free (service); g_free (mtu); g_free (mac); - if (mac_array) - g_byte_array_free (mac_array, TRUE); if (!success) return FALSE; @@ -4038,7 +4006,6 @@ cleanup_mobile: char *addr_ask = NULL; const char *bt_type_c = NULL; char *bt_type = NULL; - GByteArray *array = NULL; nmc_arg_t exp_args[] = { {"addr", TRUE, &addr, !ask}, {"bt-type", TRUE, &bt_type_c, FALSE}, {NULL} }; @@ -4053,7 +4020,7 @@ cleanup_mobile: _("Error: 'addr' is required.")); return FALSE; } - if (!check_and_convert_mac (addr, &array, ARPHRD_ETHER, "addr", error)) + if (!check_mac (addr, ARPHRD_ETHER, "addr", error)) goto cleanup_bt; /* Also ask for all optional arguments if '--ask' is specified. */ @@ -4069,10 +4036,8 @@ cleanup_mobile: s_bt = (NMSettingBluetooth *) nm_setting_bluetooth_new (); nm_connection_add_setting (connection, NM_SETTING (s_bt)); - if (array) { - g_object_set (s_bt, NM_SETTING_BLUETOOTH_BDADDR, array, NULL); - g_byte_array_free (array, TRUE); - } + if (addr) + g_object_set (s_bt, NM_SETTING_BLUETOOTH_BDADDR, addr, NULL); /* 'dun' type requires adding 'gsm' or 'cdma' setting */ if ( !strcmp (bt_type, NM_SETTING_BLUETOOTH_TYPE_DUN) @@ -4517,7 +4482,6 @@ cleanup_team_slave: max_age_int, ageing_time_int; const char *mac_c = NULL; char *mac = NULL; - GByteArray *mac_array = NULL; nmc_arg_t exp_args[] = { {"stp", TRUE, &stp_c, FALSE}, {"priority", TRUE, &priority_c, FALSE}, {"forward-delay", TRUE, &fwd_delay_c, FALSE}, @@ -4588,7 +4552,7 @@ cleanup_team_slave: if (!bridge_prop_string_to_uint (ageing_time, "ageing-time", NM_TYPE_SETTING_BRIDGE, NM_SETTING_BRIDGE_AGEING_TIME, &ageing_time_int, error)) goto cleanup_bridge; - if (!check_and_convert_mac (mac, &mac_array, ARPHRD_ETHER, "mac", error)) + if (!check_mac (mac, ARPHRD_ETHER, "mac", error)) goto cleanup_bridge; /* Set bridge options */ @@ -4604,8 +4568,8 @@ cleanup_team_slave: g_object_set (s_bridge, NM_SETTING_BRIDGE_MAX_AGE, max_age_int, NULL); if (ageing_time) g_object_set (s_bridge, NM_SETTING_BRIDGE_AGEING_TIME, ageing_time_int, NULL); - if (mac_array) - g_object_set (s_bridge, NM_SETTING_BRIDGE_MAC_ADDRESS, mac_array, NULL); + if (mac) + g_object_set (s_bridge, NM_SETTING_BRIDGE_MAC_ADDRESS, mac, NULL); success = TRUE; cleanup_bridge: @@ -4616,8 +4580,6 @@ cleanup_bridge: g_free (max_age); g_free (ageing_time); g_free (mac); - if (mac_array) - g_byte_array_free (mac_array, TRUE); if (!success) return FALSE; @@ -4785,7 +4747,6 @@ cleanup_vpn: unsigned long chan; const char *dhcp_anycast_c = NULL; char *dhcp_anycast = NULL; - GByteArray *array = NULL; nmc_arg_t exp_args[] = { {"ssid", TRUE, &ssid, !ask}, {"channel", TRUE, &channel_c, FALSE}, {"dhcp-anycast", TRUE, &dhcp_anycast_c, FALSE}, @@ -4816,7 +4777,7 @@ cleanup_vpn: goto cleanup_olpc; } } - if (!check_and_convert_mac (dhcp_anycast, &array, ARPHRD_ETHER, "dhcp-anycast", error)) + if (!check_mac (dhcp_anycast, ARPHRD_ETHER, "dhcp-anycast", error)) goto cleanup_olpc; /* Add OLPC mesh setting */ @@ -4830,10 +4791,8 @@ cleanup_vpn: g_object_set (s_olpc_mesh, NM_SETTING_OLPC_MESH_CHANNEL, chan, NULL); else g_object_set (s_olpc_mesh, NM_SETTING_OLPC_MESH_CHANNEL, 1, NULL); - if (array) { - g_object_set (s_olpc_mesh, NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, array, NULL); - g_byte_array_free (array, TRUE); - } + if (dhcp_anycast) + g_object_set (s_olpc_mesh, NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, dhcp_anycast, NULL); g_byte_array_free (ssid_arr, TRUE); success = TRUE; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 1237125cf5..11ed479706 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -528,7 +528,7 @@ fill_output_access_point (gpointer data, gpointer user_data) /* Convert to strings */ if (ssid) { - ssid_str = nm_utils_ssid_to_utf8 (ssid); + ssid_str = nm_utils_ssid_to_utf8 (ssid->data, ssid->len); ssid_hex_str = ssid_to_hex ((const char *) ssid->data, ssid->len); } channel_str = g_strdup_printf ("%u", nm_utils_wifi_freq_to_channel (freq)); @@ -2052,7 +2052,8 @@ find_ap_on_device (NMDevice *device, GByteArray *bssid, const char *ssid) candidate_ssid = nm_access_point_get_ssid (candidate_ap); if (candidate_ssid) { - char *ssid_tmp = nm_utils_ssid_to_utf8 (candidate_ssid); + char *ssid_tmp = nm_utils_ssid_to_utf8 (candidate_ssid->data, + candidate_ssid->len); /* Compare SSIDs */ if (strcmp (ssid, ssid_tmp) == 0) { diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index ab0455d473..99d0254b4c 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -388,6 +388,88 @@ setup_signals (void) return TRUE; } +static void +nmc_convert_strv_to_string (const GValue *src_value, GValue *dest_value) +{ + char **strings; + + strings = g_value_get_boxed (src_value); + if (strings) + g_value_take_string (dest_value, g_strjoinv (",", strings)); + else + g_value_set_string (dest_value, ""); +} + +static void +nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value) +{ + GHashTable *hash; + GHashTableIter iter; + const char *key, *value; + GString *string; + + hash = (GHashTable *) g_value_get_boxed (src_value); + + string = g_string_new (NULL); + if (hash) { + g_hash_table_iter_init (&iter, hash); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) { + if (string->len) + g_string_append_c (string, ','); + g_string_append_printf (string, "%s=%s", key, value); + } + } + + g_value_take_string (dest_value, g_string_free (string, FALSE)); +} + +static void +nmc_convert_bytes_to_string (const GValue *src_value, GValue *dest_value) +{ + GBytes *bytes; + const guint8 *array; + gsize length; + GString *printable; + guint i = 0; + + bytes = g_value_get_boxed (src_value); + + printable = g_string_new ("["); + + if (bytes) { + array = g_bytes_get_data (bytes, &length); + while (i < MIN (length, 35)) { + if (i > 0) + g_string_append_c (printable, ' '); + g_string_append_printf (printable, "0x%02X", array[i++]); + } + if (i < length) + g_string_append (printable, " ... "); + } + g_string_append_c (printable, ']'); + + g_value_take_string (dest_value, g_string_free (printable, FALSE)); +} + +static void +nmc_value_transforms_register (void) +{ + g_value_register_transform_func (G_TYPE_STRV, + G_TYPE_STRING, + nmc_convert_strv_to_string); + + /* This depends on the fact that all of the hash-table-valued properties + * in libnm-core are string->string. + */ + g_value_register_transform_func (G_TYPE_HASH_TABLE, + G_TYPE_STRING, + nmc_convert_string_hash_to_string); + + g_value_register_transform_func (G_TYPE_BYTES, + G_TYPE_STRING, + nmc_convert_bytes_to_string); +} + static NMClient * nmc_get_client (NmCli *nmc) { @@ -496,6 +578,8 @@ main (int argc, char *argv[]) */ rl_set_keyboard_input_timeout (10000); + nmc_value_transforms_register (); + nmc_init (&nm_cli); g_idle_add (start, &args_info); diff --git a/clients/cli/settings.c b/clients/cli/settings.c index f91fe9ba31..33d8aca118 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -663,19 +663,22 @@ wep_key_type_to_string (NMWepKeyType type) } static char * -byte_array_to_string (const GByteArray *array) +bytes_to_string (GBytes *bytes) { + const guint8 *data; + gsize len; GString *cert = NULL; int i; - if (array && array->len > 0) - cert = g_string_new (NULL); + if (!bytes) + return NULL; + data = g_bytes_get_data (bytes, &len); - for (i = 0; array && i < array->len; i++) { - g_string_append_printf (cert, "%02X", array->data[i]); - } + cert = g_string_new (NULL); + for (i = 0; i < len; i++) + g_string_append_printf (cert, "%02X", data[i]); - return cert ? g_string_free (cert, FALSE) : NULL; + return g_string_free (cert, FALSE); } static char * @@ -789,9 +792,6 @@ vpn_data_item (const char *key, const char *value, gpointer user_data) GValue val = G_VALUE_INIT; \ g_value_init (&val, G_TYPE_STRING); \ g_object_get_property (G_OBJECT (setting), property_name, &val); \ - /* Getters return allocated values, and returning the string \ - * the GValue copied from the object without unsetting the \ - * GValue fulfills that requirement. */ \ s = g_value_dup_string (&val); \ g_value_unset (&val); \ return s; \ @@ -810,22 +810,6 @@ vpn_data_item (const char *key, const char *value, gpointer user_data) return secret_flags_to_string (v); \ } -#define DEFINE_HWADDR_GETTER(func_name, property_name) \ - static char * \ - func_name (NMSetting *setting) \ - { \ - GValue val = G_VALUE_INIT; \ - GArray *array; \ - char *hwaddr = NULL; \ - g_value_init (&val, DBUS_TYPE_G_UCHAR_ARRAY); \ - g_object_get_property (G_OBJECT (setting), property_name, &val); \ - array = g_value_get_boxed (&val); \ - if (array && array->len) \ - hwaddr = nm_utils_hwaddr_ntoa (array->data, array->len); \ - g_value_unset (&val); \ - return hwaddr; \ - } - /* --- NM_SETTING_802_1X_SETTING_NAME property get functions --- */ DEFINE_GETTER (nmc_property_802_1X_get_eap, NM_SETTING_802_1X_EAP) DEFINE_GETTER (nmc_property_802_1X_get_identity, NM_SETTING_802_1X_IDENTITY) @@ -862,7 +846,7 @@ nmc_property_802_1X_get_ca_cert (NMSetting *setting) scheme = nm_setting_802_1x_get_ca_cert_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - ca_cert_str = byte_array_to_string (nm_setting_802_1x_get_ca_cert_blob (s_8021X)); + ca_cert_str = bytes_to_string (nm_setting_802_1x_get_ca_cert_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) ca_cert_str = g_strdup (nm_setting_802_1x_get_ca_cert_path (s_8021X)); @@ -878,7 +862,7 @@ nmc_property_802_1X_get_client_cert (NMSetting *setting) scheme = nm_setting_802_1x_get_client_cert_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - client_cert_str = byte_array_to_string (nm_setting_802_1x_get_client_cert_blob (s_8021X)); + client_cert_str = bytes_to_string (nm_setting_802_1x_get_client_cert_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) client_cert_str = g_strdup (nm_setting_802_1x_get_client_cert_path (s_8021X)); @@ -894,7 +878,7 @@ nmc_property_802_1X_get_phase2_ca_cert (NMSetting *setting) scheme = nm_setting_802_1x_get_phase2_ca_cert_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - phase2_ca_cert_str = byte_array_to_string (nm_setting_802_1x_get_phase2_ca_cert_blob (s_8021X)); + phase2_ca_cert_str = bytes_to_string (nm_setting_802_1x_get_phase2_ca_cert_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) phase2_ca_cert_str = g_strdup (nm_setting_802_1x_get_phase2_ca_cert_path (s_8021X)); @@ -910,7 +894,7 @@ nmc_property_802_1X_get_phase2_client_cert (NMSetting *setting) scheme = nm_setting_802_1x_get_phase2_client_cert_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - phase2_client_cert_str = byte_array_to_string (nm_setting_802_1x_get_phase2_client_cert_blob (s_8021X)); + phase2_client_cert_str = bytes_to_string (nm_setting_802_1x_get_phase2_client_cert_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) phase2_client_cert_str = g_strdup (nm_setting_802_1x_get_phase2_client_cert_path (s_8021X)); @@ -921,7 +905,7 @@ static char * nmc_property_802_1X_get_password_raw (NMSetting *setting) { NMSetting8021x *s_8021X = NM_SETTING_802_1X (setting); - return byte_array_to_string (nm_setting_802_1x_get_password_raw (s_8021X)); + return bytes_to_string (nm_setting_802_1x_get_password_raw (s_8021X)); } static char * @@ -933,7 +917,7 @@ nmc_property_802_1X_get_private_key (NMSetting *setting) scheme = nm_setting_802_1x_get_private_key_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - private_key_str = byte_array_to_string (nm_setting_802_1x_get_private_key_blob (s_8021X)); + private_key_str = bytes_to_string (nm_setting_802_1x_get_private_key_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) private_key_str = g_strdup (nm_setting_802_1x_get_private_key_path (s_8021X)); @@ -949,7 +933,7 @@ nmc_property_802_1X_get_phase2_private_key (NMSetting *setting) scheme = nm_setting_802_1x_get_phase2_private_key_scheme (s_8021X); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - phase2_private_key_str = byte_array_to_string (nm_setting_802_1x_get_phase2_private_key_blob (s_8021X)); + phase2_private_key_str = bytes_to_string (nm_setting_802_1x_get_phase2_private_key_blob (s_8021X)); if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) phase2_private_key_str = g_strdup (nm_setting_802_1x_get_phase2_private_key_path (s_8021X)); @@ -966,7 +950,7 @@ DEFINE_GETTER (nmc_property_adsl_get_vpi, NM_SETTING_ADSL_VPI) DEFINE_GETTER (nmc_property_adsl_get_vci, NM_SETTING_ADSL_VCI) /* --- NM_SETTING_BLUETOOTH_SETTING_NAME property get functions --- */ -DEFINE_HWADDR_GETTER (nmc_property_bluetooth_get_bdaddr, NM_SETTING_BLUETOOTH_BDADDR) +DEFINE_GETTER (nmc_property_bluetooth_get_bdaddr, NM_SETTING_BLUETOOTH_BDADDR) DEFINE_GETTER (nmc_property_bluetooth_get_type, NM_SETTING_BLUETOOTH_TYPE) static char * @@ -989,7 +973,7 @@ nmc_property_bond_get_options (NMSetting *setting) } /* --- NM_SETTING_BRIDGE_SETTING_NAME property get functions --- */ -DEFINE_HWADDR_GETTER (nmc_property_bridge_get_mac_address, NM_SETTING_BRIDGE_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_bridge_get_mac_address, NM_SETTING_BRIDGE_MAC_ADDRESS) DEFINE_GETTER (nmc_property_bridge_get_stp, NM_SETTING_BRIDGE_STP) DEFINE_GETTER (nmc_property_bridge_get_priority, NM_SETTING_BRIDGE_PRIORITY) DEFINE_GETTER (nmc_property_bridge_get_forward_delay, NM_SETTING_BRIDGE_FORWARD_DELAY) @@ -1181,7 +1165,7 @@ DEFINE_SECRET_FLAGS_GETTER (nmc_property_gsm_get_pin_flags, NM_SETTING_GSM_PIN_F DEFINE_GETTER (nmc_property_gsm_get_home_only, NM_SETTING_GSM_HOME_ONLY) /* --- NM_SETTING_INFINIBAND_SETTING_NAME property get functions --- */ -DEFINE_HWADDR_GETTER (nmc_property_ib_get_mac_address, NM_SETTING_INFINIBAND_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_ib_get_mac_address, NM_SETTING_INFINIBAND_MAC_ADDRESS) DEFINE_GETTER (nmc_property_ib_get_transport_mode, NM_SETTING_INFINIBAND_TRANSPORT_MODE) static char * @@ -1216,8 +1200,82 @@ DEFINE_GETTER (nmc_property_ib_get_parent, NM_SETTING_INFINIBAND_PARENT) DEFINE_GETTER (nmc_property_ipv4_get_method, NM_SETTING_IP4_CONFIG_METHOD) DEFINE_GETTER (nmc_property_ipv4_get_dns, NM_SETTING_IP4_CONFIG_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dns_search, NM_SETTING_IP4_CONFIG_DNS_SEARCH) -DEFINE_GETTER (nmc_property_ipv4_get_addresses, NM_SETTING_IP4_CONFIG_ADDRESSES) -DEFINE_GETTER (nmc_property_ipv4_get_routes, NM_SETTING_IP4_CONFIG_ROUTES) + +static char * +nmc_property_ipv4_get_addresses (NMSetting *setting) +{ + NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); + GString *printable; + guint32 num_addresses, i; + NMIP4Address *addr; + char buf[INET_ADDRSTRLEN]; + + printable = g_string_new (NULL); + + num_addresses = nm_setting_ip4_config_get_num_addresses (s_ip4); + for (i = 0; i < num_addresses; i++) { + addr = nm_setting_ip4_config_get_address (s_ip4, i); + + if (printable->len > 0) + g_string_append (printable, "; "); + + g_string_append (printable, "{ "); + + nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), buf); + g_string_append_printf (printable, "ip = %s", buf); + + g_string_append_printf (printable, "/%u", nm_ip4_address_get_prefix (addr)); + + if (nm_ip4_address_get_gateway (addr)) { + nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), buf); + g_string_append_printf (printable, ", gw = %s", buf); + } + + g_string_append (printable, " }"); + } + + return g_string_free (printable, FALSE); +} + +static char * +nmc_property_ipv4_get_routes (NMSetting *setting) +{ + NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); + GString *printable; + guint32 num_routes, i; + NMIP4Route *route; + char buf[INET_ADDRSTRLEN]; + + printable = g_string_new (NULL); + + num_routes = nm_setting_ip4_config_get_num_routes (s_ip4); + for (i = 0; i < num_routes; i++) { + route = nm_setting_ip4_config_get_route (s_ip4, i); + + if (printable->len > 0) + g_string_append (printable, "; "); + + g_string_append (printable, "{ "); + + nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), buf); + g_string_append_printf (printable, "ip = %s", buf); + + g_string_append_printf (printable, "/%u", nm_ip4_route_get_prefix (route)); + + if (nm_ip4_route_get_next_hop (route)) { + nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), buf); + g_string_append_printf (printable, ", nh = %s", buf); + } + + if (nm_ip4_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip4_route_get_metric (route)); + + g_string_append (printable, " }"); + } + + return g_string_free (printable, FALSE); +} + DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_routes, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_dns, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dhcp_client_id, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID) @@ -1230,8 +1288,82 @@ DEFINE_GETTER (nmc_property_ipv4_get_may_fail, NM_SETTING_IP4_CONFIG_MAY_FAIL) DEFINE_GETTER (nmc_property_ipv6_get_method, NM_SETTING_IP6_CONFIG_METHOD) DEFINE_GETTER (nmc_property_ipv6_get_dns, NM_SETTING_IP6_CONFIG_DNS) DEFINE_GETTER (nmc_property_ipv6_get_dns_search, NM_SETTING_IP6_CONFIG_DNS_SEARCH) -DEFINE_GETTER (nmc_property_ipv6_get_addresses, NM_SETTING_IP6_CONFIG_ADDRESSES) -DEFINE_GETTER (nmc_property_ipv6_get_routes, NM_SETTING_IP6_CONFIG_ROUTES) + +static char * +nmc_property_ipv6_get_addresses (NMSetting *setting) +{ + NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); + GString *printable; + guint32 num_addresses, i; + NMIP6Address *addr; + char buf[INET6_ADDRSTRLEN]; + + printable = g_string_new (NULL); + + num_addresses = nm_setting_ip6_config_get_num_addresses (s_ip6); + for (i = 0; i < num_addresses; i++) { + addr = nm_setting_ip6_config_get_address (s_ip6, i); + + if (printable->len > 0) + g_string_append (printable, "; "); + + g_string_append (printable, "{ "); + + nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), buf); + g_string_append_printf (printable, "ip = %s", buf); + + g_string_append_printf (printable, "/%u", nm_ip6_address_get_prefix (addr)); + + if (nm_ip6_address_get_gateway (addr)) { + nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), buf); + g_string_append_printf (printable, ", gw = %s", buf); + } + + g_string_append (printable, " }"); + } + + return g_string_free (printable, FALSE); +} + +static char * +nmc_property_ipv6_get_routes (NMSetting *setting) +{ + NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); + GString *printable; + guint32 num_routes, i; + NMIP6Route *route; + char buf[INET6_ADDRSTRLEN]; + + printable = g_string_new (NULL); + + num_routes = nm_setting_ip6_config_get_num_routes (s_ip6); + for (i = 0; i < num_routes; i++) { + route = nm_setting_ip6_config_get_route (s_ip6, i); + + if (printable->len > 0) + g_string_append (printable, "; "); + + g_string_append (printable, "{ "); + + nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), buf); + g_string_append_printf (printable, "ip = %s", buf); + + g_string_append_printf (printable, "/%u", nm_ip6_route_get_prefix (route)); + + if (nm_ip6_route_get_next_hop (route)) { + nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), buf); + g_string_append_printf (printable, ", nh = %s", buf); + } + + if (nm_ip6_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip6_route_get_metric (route)); + + g_string_append (printable, " }"); + } + + return g_string_free (printable, FALSE); +} + DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP6_CONFIG_NEVER_DEFAULT) @@ -1247,18 +1379,20 @@ nmc_property_ipv6_get_ip6_privacy (NMSetting *setting) /* --- NM_SETTING_OLPC_MESH_SETTING_NAME property get functions --- */ DEFINE_GETTER (nmc_property_olpc_get_channel, NM_SETTING_OLPC_MESH_CHANNEL) -DEFINE_HWADDR_GETTER (nmc_property_olpc_get_anycast_address, NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS) +DEFINE_GETTER (nmc_property_olpc_get_anycast_address, NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS) static char * nmc_property_olpc_get_ssid (NMSetting *setting) { NMSettingOlpcMesh *s_olpc_mesh = NM_SETTING_OLPC_MESH (setting); - const GByteArray *ssid; + GBytes *ssid; char *ssid_str = NULL; ssid = nm_setting_olpc_mesh_get_ssid (s_olpc_mesh); - if (ssid) - ssid_str = nm_utils_ssid_to_utf8 (ssid); + if (ssid) { + ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + } return ssid_str; } @@ -1352,15 +1486,15 @@ nmc_property_vpn_get_secrets (NMSetting *setting) /* --- NM_SETTING_WIMAX_SETTING_NAME property get functions --- */ DEFINE_GETTER (nmc_property_wimax_get_network_name, NM_SETTING_WIMAX_NETWORK_NAME) -DEFINE_HWADDR_GETTER (nmc_property_wimax_get_mac_address, NM_SETTING_WIMAX_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_wimax_get_mac_address, NM_SETTING_WIMAX_MAC_ADDRESS) /* --- NM_SETTING_WIRED_SETTING_NAME property get functions --- */ DEFINE_GETTER (nmc_property_wired_get_port, NM_SETTING_WIRED_PORT) DEFINE_GETTER (nmc_property_wired_get_speed, NM_SETTING_WIRED_SPEED) DEFINE_GETTER (nmc_property_wired_get_duplex, NM_SETTING_WIRED_DUPLEX) DEFINE_GETTER (nmc_property_wired_get_auto_negotiate, NM_SETTING_WIRED_AUTO_NEGOTIATE) -DEFINE_HWADDR_GETTER (nmc_property_wired_get_mac_address, NM_SETTING_WIRED_MAC_ADDRESS) -DEFINE_HWADDR_GETTER (nmc_property_wired_get_cloned_mac_address, NM_SETTING_WIRED_CLONED_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_wired_get_mac_address, NM_SETTING_WIRED_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_wired_get_cloned_mac_address, NM_SETTING_WIRED_CLONED_MAC_ADDRESS) DEFINE_GETTER (nmc_property_wired_get_mac_address_blacklist, NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST) DEFINE_GETTER (nmc_property_wired_get_s390_subchannels, NM_SETTING_WIRED_S390_SUBCHANNELS) DEFINE_GETTER (nmc_property_wired_get_s390_nettype, NM_SETTING_WIRED_S390_NETTYPE) @@ -1383,11 +1517,11 @@ nmc_property_wired_get_mtu (NMSetting *setting) DEFINE_GETTER (nmc_property_wireless_get_mode, NM_SETTING_WIRELESS_MODE) DEFINE_GETTER (nmc_property_wireless_get_band, NM_SETTING_WIRELESS_BAND) DEFINE_GETTER (nmc_property_wireless_get_channel, NM_SETTING_WIRELESS_CHANNEL) -DEFINE_HWADDR_GETTER (nmc_property_wireless_get_bssid, NM_SETTING_WIRELESS_BSSID) +DEFINE_GETTER (nmc_property_wireless_get_bssid, NM_SETTING_WIRELESS_BSSID) DEFINE_GETTER (nmc_property_wireless_get_rate, NM_SETTING_WIRELESS_RATE) DEFINE_GETTER (nmc_property_wireless_get_tx_power, NM_SETTING_WIRELESS_TX_POWER) -DEFINE_HWADDR_GETTER (nmc_property_wireless_get_mac_address, NM_SETTING_WIRELESS_MAC_ADDRESS) -DEFINE_HWADDR_GETTER (nmc_property_wireless_get_cloned_mac_address, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_wireless_get_mac_address, NM_SETTING_WIRELESS_MAC_ADDRESS) +DEFINE_GETTER (nmc_property_wireless_get_cloned_mac_address, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS) DEFINE_GETTER (nmc_property_wireless_get_mac_address_blacklist, NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST) DEFINE_GETTER (nmc_property_wireless_get_seen_bssids, NM_SETTING_WIRELESS_SEEN_BSSIDS) DEFINE_GETTER (nmc_property_wireless_get_hidden, NM_SETTING_WIRELESS_HIDDEN) @@ -1396,12 +1530,14 @@ static char * nmc_property_wireless_get_ssid (NMSetting *setting) { NMSettingWireless *s_wireless = NM_SETTING_WIRELESS (setting); - const GByteArray *ssid; + GBytes *ssid; char *ssid_str = NULL; ssid = nm_setting_wireless_get_ssid (s_wireless); - if (ssid) - ssid_str = nm_utils_ssid_to_utf8 (ssid); + if (ssid) { + ssid_str = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + } return ssid_str; } @@ -2061,18 +2197,14 @@ nmc_property_set_ssid (NMSetting *setting, const char *prop, const char *val, GE static gboolean nmc_property_set_mac (NMSetting *setting, const char *prop, const char *val, GError **error) { - GByteArray *array; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - array = nm_utils_hwaddr_atoba (val, ETH_ALEN); - if (!array) { + if (!nm_utils_hwaddr_valid (val, ETH_ALEN)) { g_set_error (error, 1, 0, _("'%s' is not a valid Ethernet MAC"), val); return FALSE; } - g_object_set (setting, prop, array, NULL); - g_byte_array_free (array, TRUE); + g_object_set (setting, prop, val, NULL); return TRUE; } @@ -2830,18 +2962,14 @@ nmc_property_bond_allowed_options (NMSetting *setting, const char *prop) static gboolean nmc_property_ib_set_mac (NMSetting *setting, const char *prop, const char *val, GError **error) { - GByteArray *array; - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - array = nm_utils_hwaddr_atoba (val, INFINIBAND_ALEN); - if (!array) { + if (!nm_utils_hwaddr_valid (val, INFINIBAND_ALEN)) { g_set_error (error, 1, 0, _("'%s' is not a valid InfiniBand MAC"), val); return FALSE; } - g_object_set (setting, prop, array, NULL); - g_byte_array_free (array, TRUE); + g_object_set (setting, prop, val, NULL); return TRUE; } @@ -2909,19 +3037,20 @@ DEFINE_ALLOWED_VAL_FUNC (nmc_property_ipv4_allowed_method, ipv4_valid_methods) static gboolean nmc_property_ipv4_set_dns (NMSetting *setting, const char *prop, const char *val, GError **error) { - char **strv = NULL, **iter; + char **strv = NULL, **iter, *addr; guint32 ip4_addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); strv = nmc_strsplit_set (val, " \t,", 0); for (iter = strv; iter && *iter; iter++) { - if (inet_pton (AF_INET, g_strstrip (*iter), &ip4_addr) < 1) { - g_set_error (error, 1, 0, _("invalid IPv4 address '%s'"), *iter); + addr = g_strstrip (*iter); + if (inet_pton (AF_INET, addr, &ip4_addr) < 1) { + g_set_error (error, 1, 0, _("invalid IPv4 address '%s'"), addr); g_strfreev (strv); return FALSE; } - nm_setting_ip4_config_add_dns (NM_SETTING_IP4_CONFIG (setting), ip4_addr); + nm_setting_ip4_config_add_dns (NM_SETTING_IP4_CONFIG (setting), addr); } g_strfreev (strv); return TRUE; @@ -2940,7 +3069,7 @@ _validate_and_remove_ipv4_dns (NMSettingIP4Config *setting, return FALSE; } - ret = nm_setting_ip4_config_remove_dns_by_value (setting, ip4_addr); + ret = nm_setting_ip4_config_remove_dns_by_value (setting, dns); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS server '%s'"), dns); return ret; @@ -3254,19 +3383,20 @@ DEFINE_ALLOWED_VAL_FUNC (nmc_property_ipv6_allowed_method, ipv6_valid_methods) static gboolean nmc_property_ipv6_set_dns (NMSetting *setting, const char *prop, const char *val, GError **error) { - char **strv = NULL, **iter; + char **strv = NULL, **iter, *addr; struct in6_addr ip6_addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); strv = nmc_strsplit_set (val, " \t,", 0); for (iter = strv; iter && *iter; iter++) { - if (inet_pton (AF_INET6, g_strstrip (*iter), &ip6_addr) < 1) { - g_set_error (error, 1, 0, _("invalid IPv6 address '%s'"), *iter); + addr = g_strstrip (*iter); + if (inet_pton (AF_INET6, addr, &ip6_addr) < 1) { + g_set_error (error, 1, 0, _("invalid IPv6 address '%s'"), addr); g_strfreev (strv); return FALSE; } - nm_setting_ip6_config_add_dns (NM_SETTING_IP6_CONFIG (setting), &ip6_addr); + nm_setting_ip6_config_add_dns (NM_SETTING_IP6_CONFIG (setting), addr); } g_strfreev (strv); return TRUE; @@ -3285,7 +3415,7 @@ _validate_and_remove_ipv6_dns (NMSettingIP6Config *setting, return FALSE; } - ret = nm_setting_ip6_config_remove_dns_by_value (setting, &ip6_addr); + ret = nm_setting_ip6_config_remove_dns_by_value (setting, dns); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS server '%s'"), dns); return ret; diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c index 0325c0a8b8..e3b210dec6 100644 --- a/clients/tui/nm-editor-bindings.c +++ b/clients/tui/nm-editor-bindings.c @@ -119,7 +119,7 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, gpointer user_data) { GPtrArray *addrs; - GArray *addr; + NMIP4Address *addr; guint32 addrbytes, prefix; char buf[INET_ADDRSTRLEN], **strings; int i; @@ -129,8 +129,8 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - addrbytes = g_array_index (addr, guint32, 0); - prefix = g_array_index (addr, guint32, 1); + addrbytes = nm_ip4_address_get_address (addr); + prefix = nm_ip4_address_get_prefix (addr); if (addrbytes) { strings[i] = g_strdup_printf ("%s/%d", @@ -152,8 +152,8 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, { char **strings; GPtrArray *addrs; - GArray *addr; - guint32 *addrvals; + NMIP4Address *addr; + guint32 addrbytes, prefix; int i; strings = g_value_get_boxed (source_value); @@ -164,24 +164,19 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, for (i = 0; strings[i]; i++) { if (i >= addrs->len) { - guint32 val; - - addr = g_array_sized_new (FALSE, FALSE, sizeof (guint32), 3); - val = 0; - g_array_append_val (addr, val); - val = 32; - g_array_append_val (addr, val); - val = 0; - g_array_append_val (addr, val); + addr = nm_ip4_address_new (); + nm_ip4_address_set_prefix (addr, 32); g_ptr_array_add (addrs, addr); } else addr = addrs->pdata[i]; - addrvals = (guint32 *)addr->data; - if (!ip_string_parse (strings[i], AF_INET, &addrvals[0], &addrvals[1])) { + if (!ip_string_parse (strings[i], AF_INET, &addrbytes, &prefix)) { g_ptr_array_unref (addrs); return FALSE; } + + nm_ip4_address_set_address (addr, addrbytes); + nm_ip4_address_set_prefix (addr, prefix); } g_ptr_array_set_size (addrs, i); @@ -199,16 +194,14 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT property - * @source_property on @source to the %G_TYPE_STRV property - * @target_property on @target. + * Binds the #GPtrArray-of-#NMIP4Address property @source_property on @source to + * the %G_TYPE_STRV property @target_property on @target. * - * Each address/prefix/gateway triplet in @source_property will be - * converted to a string of the form "ip.ad.dr.ess/prefix" in - * @target_property (and vice versa if %G_BINDING_BIDIRECTIONAL) is - * specified. The "gateway" fields in @source_property are ignored - * when converting to strings, and unmodified when converting from - * strings. + * Each #NMIP4Address in @source_property will be converted to a string of the + * form "ip.ad.dr.ess/prefix" in @target_property (and vice versa if + * %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in + * @source_property are ignored when converting to strings, and unmodified when + * converting from strings. */ void nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, @@ -226,55 +219,23 @@ nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, } static gboolean -ip4_addresses_to_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GArray *addrs; - guint32 addrbytes; - char buf[INET_ADDRSTRLEN], **strings; - int i; - - addrs = g_value_get_boxed (source_value); - strings = g_new0 (char *, addrs->len + 1); - - for (i = 0; i < addrs->len; i++) { - addrbytes = g_array_index (addrs, guint32, i); - if (addrbytes) - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)); - else - buf[0] = '\0'; - strings[i] = g_strdup (buf); - } - - g_value_take_boxed (target_value, strings); - return TRUE; -} - -static gboolean -ip4_addresses_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip4_addresses_check_and_copy (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { char **strings; - GArray *addrs; guint32 addr; int i; strings = g_value_get_boxed (source_value); - addrs = g_array_new (FALSE, FALSE, sizeof (guint32)); for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET, &addr, NULL)) { - g_array_unref (addrs); + if (!ip_string_parse (strings[i], AF_INET, &addr, NULL)) return FALSE; - } - g_array_append_val (addrs, addr); } - g_value_take_boxed (target_value, addrs); + g_value_set_boxed (target_value, strings); return TRUE; } @@ -288,12 +249,9 @@ ip4_addresses_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_UINT_ARRAY property @source_property on - * @source to the %G_TYPE_STRV property @target_property on @target. - * - * Each address in @source_property will be converted to a string of - * the form "ip.ad.dr.ess" in @target_property (and vice versa if - * %G_BINDING_BIDIRECTIONAL) is specified. + * Binds the %G_TYPE_STRV property @source_property on @source to the + * %G_TYPE_STRV property @target_property on @target, verifying that + * each string is a valid IPv4 address when copying. */ void nm_editor_bind_ip4_addresses_to_strv (gpointer source, @@ -305,8 +263,8 @@ nm_editor_bind_ip4_addresses_to_strv (gpointer source, g_object_bind_property_full (source, source_property, target, target_property, flags, - ip4_addresses_to_strv, - ip4_addresses_from_strv, + ip4_addresses_check_and_copy, + ip4_addresses_check_and_copy, NULL, NULL); } @@ -317,7 +275,7 @@ ip4_gateway_to_string (GBinding *binding, gpointer user_data) { GPtrArray *addrs; - GArray *addr; + NMIP4Address *addr; guint32 gateway = 0; const char *str; char buf[INET_ADDRSTRLEN]; @@ -326,7 +284,7 @@ ip4_gateway_to_string (GBinding *binding, addrs = g_value_get_boxed (source_value); for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - gateway = g_array_index (addr, guint32, 2); + gateway = nm_ip4_address_get_gateway (addr); if (gateway) break; } @@ -347,8 +305,8 @@ ip4_gateway_from_string (GBinding *binding, { const char *text; GPtrArray *addrs; - GArray *addr; - guint32 addrbytes, *addrvals; + NMIP4Address *addr; + guint32 addrbytes; int i; text = g_value_get_string (source_value); @@ -364,17 +322,15 @@ ip4_gateway_from_string (GBinding *binding, return FALSE; } addr = addrs->pdata[0]; - addrvals = (guint32 *)addr->data; - if (addrbytes == addrvals[2]) { + if (addrbytes == nm_ip4_address_get_gateway (addr)) { g_ptr_array_unref (addrs); return FALSE; } - addrvals[2] = addrbytes; + nm_ip4_address_set_gateway (addr, addrbytes); for (i = 1; i < addrs->len; i++) { addr = addrs->pdata[i]; - addrvals = (guint32 *)addr->data; - addrvals[2] = 0; + nm_ip4_address_set_gateway (addr, 0); } g_value_take_boxed (target_value, addrs); @@ -391,13 +347,12 @@ ip4_gateway_from_string (GBinding *binding, * (eg, "text") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT property - * @source_property on @source to the %G_TYPE_STRING property - * @target_property on @target. + * Binds the #GPtrArray-of-#NMIP4Route property @source_property on @source to + * the %G_TYPE_STRING property @target_property on @target. * - * Specifically, this binds the "gateway" field of the first address - * in @source_property; all other addresses in @source_property are - * ignored, and its "address" and "prefix" fields are unmodified. + * Specifically, this binds the "gateway" field of the first address in + * @source_property; all other addresses in @source_property are ignored, and + * its "address" and "prefix" fields are unmodified. */ void nm_editor_bind_ip4_gateway_to_string (gpointer source, @@ -572,13 +527,13 @@ ip4_route_transform_from_metric_string (GBinding *binding, * @metric_target_property: the property on @metric_target * @flags: %GBindingFlags * - * Binds the #NMIP4Route-valued property @source_property on @source - * to the three indicated string-valued target properties (and vice - * versa if %G_BINDING_BIDIRECTIONAL is specified). + * Binds the #NMIP4Route-valued property @source_property on @source to the + * three indicated string-valued target properties (and vice versa if + * %G_BINDING_BIDIRECTIONAL is specified). * * @dest_target_property should be an "address/prefix" string, as with - * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target - * is a plain IP address, and @metric_target is a number. + * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target_property + * is a plain IP address, and @metric_target_property is a number. */ void nm_editor_bind_ip4_route_to_strings (gpointer source, @@ -612,8 +567,7 @@ nm_editor_bind_ip4_route_to_strings (gpointer source, } #define IP6_ADDRESS_SET(addr) ( addr \ - && addr->len == sizeof (struct in6_addr) \ - && memcmp (addr->data, &in6addr_any, addr->len) != 0) + && memcmp (addr, &in6addr_any, sizeof (struct in6_addr)) != 0) static gboolean ip6_addresses_with_prefix_to_strv (GBinding *binding, @@ -622,9 +576,8 @@ ip6_addresses_with_prefix_to_strv (GBinding *binding, gpointer user_data) { GPtrArray *addrs; - GValueArray *addr; - GValue *val; - GByteArray *addrbytes; + NMIP6Address *addr; + const struct in6_addr *addrbytes; guint prefix; char **strings, buf[INET6_ADDRSTRLEN]; int i; @@ -634,14 +587,12 @@ ip6_addresses_with_prefix_to_strv (GBinding *binding, for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - val = g_value_array_get_nth (addr, 0); - addrbytes = g_value_get_boxed (val); - val = g_value_array_get_nth (addr, 1); - prefix = g_value_get_uint (val); + addrbytes = nm_ip6_address_get_address (addr); + prefix = nm_ip6_address_get_prefix (addr); if (IP6_ADDRESS_SET (addrbytes)) { strings[i] = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET6, addrbytes->data, buf, sizeof (buf)), + inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), prefix); } else strings[i] = g_strdup (""); @@ -659,10 +610,9 @@ ip6_addresses_with_prefix_from_strv (GBinding *binding, { char **strings; GPtrArray *addrs; - GValueArray *addr; + NMIP6Address *addr; + struct in6_addr addrbytes; guint32 prefix; - GValue val = G_VALUE_INIT, *valp; - GByteArray *ba; int i; strings = g_value_get_boxed (source_value); @@ -674,42 +624,19 @@ ip6_addresses_with_prefix_from_strv (GBinding *binding, for (i = 0; strings[i]; i++) { if (i >= addrs->len) { - addr = g_value_array_new (3); - - g_value_init (&val, DBUS_TYPE_G_UCHAR_ARRAY); - ba = g_byte_array_sized_new (sizeof (struct in6_addr)); - g_byte_array_append (ba, (guint8 *) &in6addr_any, sizeof (struct in6_addr)); - g_value_take_boxed (&val, ba); - g_value_array_append (addr, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, 128); - g_value_array_append (addr, &val); - g_value_unset (&val); - - g_value_init (&val, DBUS_TYPE_G_UCHAR_ARRAY); - ba = g_byte_array_sized_new (sizeof (struct in6_addr)); - g_byte_array_append (ba, (guint8 *) &in6addr_any, sizeof (struct in6_addr)); - g_value_take_boxed (&val, ba); - g_value_array_append (addr, &val); - g_value_unset (&val); - + addr = nm_ip6_address_new (); + nm_ip6_address_set_prefix (addr, 128); g_ptr_array_add (addrs, addr); } else addr = addrs->pdata[i]; - valp = g_value_array_get_nth (addr, 0); - ba = g_value_get_boxed (valp); - g_assert (ba->len == sizeof (struct in6_addr)); - - if (!ip_string_parse (strings[i], AF_INET6, ba->data, &prefix)) { + if (!ip_string_parse (strings[i], AF_INET6, &addrbytes, &prefix)) { g_ptr_array_unref (addrs); return FALSE; } - valp = g_value_array_get_nth (addr, 1); - g_value_set_uint (valp, prefix); + nm_ip6_address_set_address (addr, &addrbytes); + nm_ip6_address_set_prefix (addr, prefix); } g_ptr_array_set_size (addrs, i); @@ -727,16 +654,14 @@ ip6_addresses_with_prefix_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS property - * @source_property on @source to the %G_TYPE_STRV property - * @target_property on @target. + * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to + * the %G_TYPE_STRV property @target_property on @target. * - * Each address/prefix/gateway triplet in @source_property will be - * converted to a string of the form "ip::ad:dr:ess/prefix" in - * @target_property (and vice versa if %G_BINDING_BIDIRECTIONAL) is - * specified. The "gateway" fields in @source_property are ignored - * when converting to strings, and unmodified when converting from - * strings. + * Each #NMIP6Address in triplet in @source_property will be converted to a + * string of the form "ip::ad:dr:ess/prefix" in @target_property (and vice versa + * if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in + * @source_property are ignored when converting to strings, and unmodified when + * converting from strings. */ void nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, @@ -754,61 +679,23 @@ nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, } static gboolean -ip6_addresses_to_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - GByteArray *addrbytes; - char buf[INET6_ADDRSTRLEN], **strings; - int i; - - addrs = g_value_get_boxed (source_value); - strings = g_new0 (char *, addrs->len + 1); - - for (i = 0; i < addrs->len; i++) { - addrbytes = addrs->pdata[i]; - if (IP6_ADDRESS_SET (addrbytes)) - inet_ntop (AF_INET6, addrbytes->data, buf, sizeof (buf)); - else - buf[0] = '\0'; - strings[i] = g_strdup (buf); - } - - g_value_take_boxed (target_value, strings); - return TRUE; -} - -static gboolean -ip6_addresses_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip6_addresses_check_and_copy (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { char **strings; - GPtrArray *addrs; - GByteArray *addr; - struct in6_addr addrbytes; + struct in6_addr addr; int i; strings = g_value_get_boxed (source_value); - addrs = g_ptr_array_new (); for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET6, &addrbytes, NULL)) { - while (i--) - g_byte_array_unref (addrs->pdata[i]); - g_ptr_array_unref (addrs); + if (!ip_string_parse (strings[i], AF_INET6, &addr, NULL)) return FALSE; - } - - addr = g_byte_array_sized_new (sizeof (addrbytes)); - g_byte_array_append (addr, (guint8 *)&addrbytes, sizeof (addrbytes)); - g_ptr_array_add (addrs, addr); } - g_value_take_boxed (target_value, addrs); + g_value_set_boxed (target_value, strings); return TRUE; } @@ -822,13 +709,9 @@ ip6_addresses_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR property - * @source_property on @source to the %G_TYPE_STRV property - * @target_property on @target. - * - * Each address in @source_property will be converted to a string of - * the form "ip::ad:dr:ess" in @target_property (and vice versa if - * %G_BINDING_BIDIRECTIONAL) is specified. + * Binds the %G_TYPE_STRV property @source_property on @source to the + * %G_TYPE_STRV property @target_property on @target, verifying that + * each string is a valid IPv6 address when copying. */ void nm_editor_bind_ip6_addresses_to_strv (gpointer source, @@ -840,8 +723,8 @@ nm_editor_bind_ip6_addresses_to_strv (gpointer source, g_object_bind_property_full (source, source_property, target, target_property, flags, - ip6_addresses_to_strv, - ip6_addresses_from_strv, + ip6_addresses_check_and_copy, + ip6_addresses_check_and_copy, NULL, NULL); } @@ -852,9 +735,8 @@ ip6_gateway_to_string (GBinding *binding, gpointer user_data) { GPtrArray *addrs; - GValueArray *addr; - GValue *val; - GByteArray *gateway; + NMIP6Address *addr; + const struct in6_addr *gateway; char buf[INET6_ADDRSTRLEN]; const char *str; @@ -863,11 +745,10 @@ ip6_gateway_to_string (GBinding *binding, return FALSE; addr = addrs->pdata[0]; - val = g_value_array_get_nth (addr, 2); - gateway = g_value_get_boxed (val); + gateway = nm_ip6_address_get_gateway (addr); if (IP6_ADDRESS_SET (gateway)) - str = inet_ntop (AF_INET6, gateway->data, buf, sizeof (buf)); + str = inet_ntop (AF_INET6, gateway, buf, sizeof (buf)); else str = ""; g_value_set_string (target_value, str); @@ -882,10 +763,8 @@ ip6_gateway_from_string (GBinding *binding, { GPtrArray *addrs; const char *text; - GValueArray *addr; + NMIP6Address *addr; struct in6_addr gateway; - GValue *val; - GByteArray *ba; int i; text = g_value_get_string (source_value); @@ -902,20 +781,11 @@ ip6_gateway_from_string (GBinding *binding, } addr = addrs->pdata[0]; - - ba = g_byte_array_sized_new (sizeof (gateway)); - g_byte_array_append (ba, (guint8 *) &gateway, sizeof (gateway)); - - val = g_value_array_get_nth (addr, 2); - g_value_take_boxed (val, ba); + nm_ip6_address_set_gateway (addr, &gateway); for (i = 1; i < addrs->len; i++) { addr = addrs->pdata[i]; - val = g_value_array_get_nth (addr, 2); - ba = g_value_get_boxed (val); - - if (ba) - memset (ba->data, 0, ba->len); + nm_ip6_address_set_gateway (addr, &in6addr_any); } g_value_take_boxed (target_value, addrs); @@ -932,13 +802,12 @@ ip6_gateway_from_string (GBinding *binding, * (eg, "text") * @flags: %GBindingFlags * - * Binds the %DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS property - * @source_property on @source to the %G_TYPE_STRING property - * @target_property on @target. + * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to + * the %G_TYPE_STRING property @target_property on @target. * - * Specifically, this binds the "gateway" field of the first address - * in @source_property; all other addresses in @source_property are - * ignored, and its "address" and "prefix" fields are unmodified. + * Specifically, this binds the "gateway" field of the first address in + * @source_property; all other addresses in @source_property are ignored, and + * its "address" and "prefix" fields are unmodified. */ void nm_editor_bind_ip6_gateway_to_string (gpointer source, @@ -955,8 +824,6 @@ nm_editor_bind_ip6_gateway_to_string (gpointer source, NULL, NULL); } -#define IN6_ADDR_SET(bytes) (memcmp (bytes, &in6addr_any, sizeof (struct in6_addr)) != 0) - static gboolean ip6_route_transform_to_dest_string (GBinding *binding, const GValue *source_value, @@ -973,7 +840,7 @@ ip6_route_transform_to_dest_string (GBinding *binding, else addrbytes = &in6addr_any; - if (IN6_ADDR_SET (addrbytes)) { + if (IP6_ADDRESS_SET (addrbytes)) { string = g_strdup_printf ("%s/%d", inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), (int) nm_ip6_route_get_prefix (route)); @@ -999,7 +866,7 @@ ip6_route_transform_to_next_hop_string (GBinding *binding, else addrbytes = &in6addr_any; - if (IN6_ADDR_SET (addrbytes)) + if (IP6_ADDRESS_SET (addrbytes)) inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)); else buf[0] = '\0'; @@ -1017,7 +884,7 @@ ip6_route_transform_to_metric_string (GBinding *binding, char *string; route = g_value_get_boxed (source_value); - if (route && IN6_ADDR_SET (nm_ip6_route_get_dest (route))) { + if (route && IP6_ADDRESS_SET (nm_ip6_route_get_dest (route))) { string = g_strdup_printf ("%lu", (gulong) nm_ip6_route_get_metric (route)); g_value_take_string (target_value, string); } else diff --git a/clients/tui/nmt-connect-connection-list.c b/clients/tui/nmt-connect-connection-list.c index aadd74b395..d7592b80b2 100644 --- a/clients/tui/nmt-connect-connection-list.c +++ b/clients/tui/nmt-connect-connection-list.c @@ -266,6 +266,7 @@ add_connections_for_aps (NmtConnectDevice *nmtdev, NMAccessPoint *ap; const GPtrArray *aps; GHashTable *seen_ssids; + const GByteArray *ssid; char *ap_hash; GSList *iter; int i; @@ -292,7 +293,8 @@ add_connections_for_aps (NmtConnectDevice *nmtdev, nmtconn = g_slice_new0 (NmtConnectConnection); nmtconn->device = nmtdev->device; nmtconn->ap = g_object_ref (ap); - nmtconn->ssid = nm_utils_ssid_to_utf8 (nm_access_point_get_ssid (ap)); + ssid = nm_access_point_get_ssid (ap); + nmtconn->ssid = nm_utils_ssid_to_utf8 (ssid->data, ssid->len); for (iter = connections; iter; iter = iter->next) { conn = iter->data; diff --git a/clients/tui/nmt-device-entry.c b/clients/tui/nmt-device-entry.c index 4c5b88d119..b8db86869f 100644 --- a/clients/tui/nmt-device-entry.c +++ b/clients/tui/nmt-device-entry.c @@ -57,7 +57,7 @@ typedef struct { int arptype; char *interface_name; - GByteArray *mac_address; + char *mac_address; char *label; NmtNewtEntry *entry; @@ -259,8 +259,8 @@ update_entry (NmtDeviceEntry *deventry) } if (priv->mac_address) { - mac = nm_utils_hwaddr_ntoa (priv->mac_address->data, priv->mac_address->len); - mac_device = find_device_by_mac_address (deventry, mac); + mac = g_strdup (priv->mac_address); + mac_device = find_device_by_mac_address (deventry, priv->mac_address); } else { mac = NULL; mac_device = NULL; @@ -312,25 +312,21 @@ nmt_device_entry_set_interface_name (NmtDeviceEntry *deventry, static gboolean nmt_device_entry_set_mac_address (NmtDeviceEntry *deventry, - GByteArray *mac_address) + const char *mac_address) { NmtDeviceEntryPrivate *priv = NMT_DEVICE_ENTRY_GET_PRIVATE (deventry); gboolean changed; - if (mac_address) - g_return_val_if_fail (mac_address->len == nm_utils_hwaddr_len (priv->arptype), FALSE); - if (mac_address && !priv->mac_address) { - priv->mac_address = g_boxed_copy (DBUS_TYPE_G_UCHAR_ARRAY, mac_address); + priv->mac_address = g_strdup (mac_address); changed = TRUE; } else if (!mac_address && priv->mac_address) { - g_clear_pointer (&priv->mac_address, g_byte_array_unref); + g_clear_pointer (&priv->mac_address, g_free); changed = TRUE; } else if ( mac_address && priv->mac_address - && !nm_utils_hwaddr_matches (mac_address->data, mac_address->len, - priv->mac_address->data, priv->mac_address->len)) { - g_byte_array_unref (priv->mac_address); - priv->mac_address = g_boxed_copy (DBUS_TYPE_G_UCHAR_ARRAY, mac_address); + && !nm_utils_hwaddr_matches (mac_address, -1, priv->mac_address, -1)) { + g_free (priv->mac_address); + priv->mac_address = g_strdup (mac_address); changed = TRUE; } else changed = FALSE; @@ -356,21 +352,11 @@ entry_text_changed (GObject *object, if (!device_entry_parse (deventry, text, &ifname, &mac)) return; - if (ifname) { - nmt_device_entry_set_interface_name (deventry, ifname); - g_free (ifname); - } else - nmt_device_entry_set_interface_name (deventry, NULL); + nmt_device_entry_set_interface_name (deventry, ifname); + g_free (ifname); - if (mac) { - GByteArray *mac_address; - - mac_address = nm_utils_hwaddr_atoba (mac, nm_utils_hwaddr_len (priv->arptype)); - nmt_device_entry_set_mac_address (deventry, mac_address); - g_byte_array_unref (mac_address); - g_free (mac); - } else - nmt_device_entry_set_mac_address (deventry, NULL); + nmt_device_entry_set_mac_address (deventry, mac); + g_free (mac); } static void @@ -410,8 +396,7 @@ nmt_device_entry_finalize (GObject *object) NmtDeviceEntryPrivate *priv = NMT_DEVICE_ENTRY_GET_PRIVATE (object); g_free (priv->interface_name); - if (priv->mac_address) - g_byte_array_unref (priv->mac_address); + g_free (priv->mac_address); G_OBJECT_CLASS (nmt_device_entry_parent_class)->finalize (object); } @@ -461,7 +446,7 @@ nmt_device_entry_set_property (GObject *object, NmtDeviceEntry *deventry = NMT_DEVICE_ENTRY (object); NmtDeviceEntryPrivate *priv = NMT_DEVICE_ENTRY_GET_PRIVATE (deventry); const char *interface_name; - GByteArray *mac_address; + const char *mac_address; switch (prop_id) { case PROP_LABEL: @@ -480,7 +465,7 @@ nmt_device_entry_set_property (GObject *object, update_entry (deventry); break; case PROP_MAC_ADDRESS: - mac_address = g_value_get_boxed (value); + mac_address = g_value_get_string (value); if (nmt_device_entry_set_mac_address (deventry, mac_address)) update_entry (deventry); break; @@ -512,7 +497,7 @@ nmt_device_entry_get_property (GObject *object, g_value_set_string (value, priv->interface_name); break; case PROP_MAC_ADDRESS: - g_value_set_boxed (value, priv->mac_address); + g_value_set_string (value, priv->mac_address); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -587,8 +572,8 @@ nmt_device_entry_class_init (NmtDeviceEntryClass *deventry_class) */ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - g_param_spec_boxed ("mac-address", "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string ("mac-address", "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); } diff --git a/clients/tui/nmt-mac-entry.c b/clients/tui/nmt-mac-entry.c index 8feb382288..013ab22a1a 100644 --- a/clients/tui/nmt-mac-entry.c +++ b/clients/tui/nmt-mac-entry.c @@ -152,8 +152,6 @@ nmt_mac_entry_set_property (GObject *object, GParamSpec *pspec) { NmtMacEntryPrivate *priv = NMT_MAC_ENTRY_GET_PRIVATE (object); - GByteArray *addr; - char *addr_str; switch (prop_id) { case PROP_MAC_LENGTH: @@ -161,13 +159,7 @@ nmt_mac_entry_set_property (GObject *object, priv->mac_str_length = priv->mac_length * 3 - 1; break; case PROP_MAC_ADDRESS: - addr = g_value_get_boxed (value); - if (addr) { - addr_str = nm_utils_hwaddr_ntoa (addr->data, addr->len); - nmt_newt_entry_set_text (NMT_NEWT_ENTRY (object), addr_str); - g_free (addr_str); - } else - nmt_newt_entry_set_text (NMT_NEWT_ENTRY (object), ""); + nmt_newt_entry_set_text (NMT_NEWT_ENTRY (object), g_value_get_string (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -182,15 +174,13 @@ nmt_mac_entry_get_property (GObject *object, GParamSpec *pspec) { NmtMacEntryPrivate *priv = NMT_MAC_ENTRY_GET_PRIVATE (object); - GByteArray *addr; switch (prop_id) { case PROP_MAC_LENGTH: g_value_set_int (value, priv->mac_length); break; case PROP_MAC_ADDRESS: - addr = nm_utils_hwaddr_atoba (nmt_newt_entry_get_text (NMT_NEWT_ENTRY (object)), ETH_ALEN); - g_value_take_boxed (value, addr); + g_value_set_boxed (value, nmt_newt_entry_get_text (NMT_NEWT_ENTRY (object))); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -225,13 +215,13 @@ nmt_mac_entry_class_init (NmtMacEntryClass *entry_class) /** * NmtMacEntry:mac-address: * - * The MAC address, in binary (in the same format used by the various - * #NMSetting "mac-address" properties). + * The MAC address, as a string (as with the various #NMSetting + * "mac-address" properties). */ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - g_param_spec_boxed ("mac-address", "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string ("mac-address", "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); } diff --git a/clients/tui/nmt-page-main.c b/clients/tui/nmt-page-main.c index 2e1510f375..ada5c29a25 100644 --- a/clients/tui/nmt-page-main.c +++ b/clients/tui/nmt-page-main.c @@ -96,9 +96,9 @@ permissions_transform_to_allusers (GBinding *binding, GValue *target_value, gpointer user_data) { - GSList *perms = g_value_get_boxed (source_value); + char **perms = g_value_get_boxed (source_value); - g_value_set_boolean (target_value, perms == NULL); + g_value_set_boolean (target_value, g_strv_length (perms) == 0); return TRUE; } @@ -109,12 +109,13 @@ permissions_transform_from_allusers (GBinding *binding, gpointer user_data) { gboolean allusers = g_value_get_boolean (source_value); - GSList *perms = NULL; + char **perms = NULL; if (allusers) { - char *perm = g_strdup_printf ("user:%s:", g_get_user_name ()); + perms = g_new (char *, 2); - perms = g_slist_prepend (perms, perm); + perms[0] = g_strdup_printf ("user:%s:", g_get_user_name ()); + perms[1] = NULL; } g_value_take_boxed (target_value, perms); return TRUE; diff --git a/clients/tui/nmt-page-wifi.c b/clients/tui/nmt-page-wifi.c index 12b2f4e59d..3ead72daa2 100644 --- a/clients/tui/nmt-page-wifi.c +++ b/clients/tui/nmt-page-wifi.c @@ -134,11 +134,12 @@ ssid_transform_to_entry (GBinding *binding, GValue *target_value, gpointer user_data) { - GByteArray *ssid; + GBytes *ssid; char *utf8; ssid = g_value_get_boxed (source_value); - utf8 = nm_utils_ssid_to_utf8 (ssid); + utf8 = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); g_value_take_string (target_value, utf8); return TRUE; } @@ -151,14 +152,14 @@ ssid_transform_from_entry (GBinding *binding, { NMSettingWireless *s_wireless = user_data; const char *text; - const GByteArray *old_ssid; - GByteArray *ssid; + GBytes *old_ssid, *ssid; char *utf8; text = g_value_get_string (source_value); old_ssid = nm_setting_wireless_get_ssid (s_wireless); - utf8 = nm_utils_ssid_to_utf8 (old_ssid); + utf8 = nm_utils_ssid_to_utf8 (g_bytes_get_data (old_ssid, NULL), + g_bytes_get_size (old_ssid)); if (!g_strcmp0 (text, utf8)) { g_free (utf8); @@ -166,8 +167,7 @@ ssid_transform_from_entry (GBinding *binding, } g_free (utf8); - ssid = g_byte_array_new (); - g_byte_array_append (ssid, (guint8 *)text, strlen (text)); + ssid = g_bytes_new (text, strlen (text)); g_value_take_boxed (target_value, ssid); return TRUE; } diff --git a/clients/tui/nmt-route-table.c b/clients/tui/nmt-route-table.c index 83765da04d..f8b5e89725 100644 --- a/clients/tui/nmt-route-table.c +++ b/clients/tui/nmt-route-table.c @@ -47,7 +47,7 @@ typedef struct { int ip_entry_width; int metric_entry_width; - GSList *routes; + GPtrArray *routes; NmtNewtWidget *list; } NmtRouteTablePrivate; @@ -87,7 +87,7 @@ route_list_transform_to_route (GBinding *binding, int n = GPOINTER_TO_INT (user_data); gpointer route; - route = g_slist_nth_data (priv->routes, n); + route = priv->routes->pdata[n]; if (route) g_value_set_boxed (target_value, route); return route != NULL; @@ -102,31 +102,25 @@ route_list_transform_from_route (GBinding *binding, NmtRouteTable *table = NMT_ROUTE_TABLE (g_binding_get_source (binding)); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); int n = GPOINTER_TO_INT (user_data); - GSList *routes, *nth; + GPtrArray *routes; + gpointer route; - nth = g_slist_nth (priv->routes, n); - if (!nth) + if (n >= priv->routes->len) return FALSE; + route = priv->routes->pdata[n]; routes = priv->routes; priv->routes = NULL; - if (nth->data) { + if (route) { if (priv->family == AF_INET) - nm_ip4_route_unref (nth->data); + nm_ip4_route_unref (route); else if (priv->family == AF_INET6) - nm_ip6_route_unref (nth->data); - } - nth->data = g_value_dup_boxed (source_value); - - if (priv->family == AF_INET) { - nm_utils_ip4_routes_to_gvalue (routes, target_value); - g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); - } else if (priv->family == AF_INET6) { - nm_utils_ip6_routes_to_gvalue (routes, target_value); - g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); + nm_ip6_route_unref (route); } + routes->pdata[n] = g_value_dup_boxed (source_value); + g_value_take_boxed (target_value, routes); return TRUE; } @@ -171,16 +165,16 @@ add_route (NmtWidgetList *list, route = nm_ip4_route_new (); nm_ip4_route_set_prefix (route, 32); - priv->routes = g_slist_append (priv->routes, route); - nmt_widget_list_set_length (list, g_slist_length (priv->routes)); + g_ptr_array_add (priv->routes, route); + nmt_widget_list_set_length (list, priv->routes->len); g_object_notify (table, "ip4-routes"); } else { NMIP6Route *route; route = nm_ip6_route_new (); nm_ip6_route_set_prefix (route, 128); - priv->routes = g_slist_append (priv->routes, route); - nmt_widget_list_set_length (list, g_slist_length (priv->routes)); + g_ptr_array_add (priv->routes, route); + nmt_widget_list_set_length (list, priv->routes->len); g_object_notify (table, "ip6-routes"); } } @@ -191,16 +185,14 @@ remove_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); - GSList *nth; gpointer route; - nth = g_slist_nth (priv->routes, num); - if (!nth) + if (num >= priv->routes->len) return; - route = nth->data; - priv->routes = g_slist_delete_link (priv->routes, nth); - nmt_widget_list_set_length (list, g_slist_length (priv->routes)); + route = priv->routes->pdata[num]; + g_ptr_array_remove_index (priv->routes, num); + nmt_widget_list_set_length (list, priv->routes->len); if (priv->family == AF_INET) { nm_ip4_route_unref (route); @@ -271,10 +263,7 @@ nmt_route_table_finalize (GObject *object) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (object); - if (priv->family == AF_INET) - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); - else if (priv->family == AF_INET6) - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + g_ptr_array_unref (priv->routes); G_OBJECT_CLASS (nmt_route_table_parent_class)->finalize (object); } @@ -286,24 +275,36 @@ nmt_route_table_set_property (GObject *object, GParamSpec *pspec) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (object); + GPtrArray *array; + int i; switch (prop_id) { case PROP_FAMILY: priv->family = g_value_get_int (value); + if (priv->family == AF_INET) + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + else + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); break; case PROP_IP4_ROUTES: g_return_if_fail (priv->family == AF_INET); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); - priv->routes = nm_utils_ip4_routes_from_gvalue (value); - nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), - g_slist_length (priv->routes)); + array = g_value_get_boxed (value); + g_ptr_array_set_size (priv->routes, 0); + for (i = 0; i < array->len; i++) { + nm_ip4_route_ref (array->pdata[i]); + g_ptr_array_add (priv->routes, array->pdata[i]); + } + nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); break; case PROP_IP6_ROUTES: g_return_if_fail (priv->family == AF_INET6); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); - priv->routes = nm_utils_ip6_routes_from_gvalue (value); - nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), - g_slist_length (priv->routes)); + array = g_value_get_boxed (value); + g_ptr_array_set_size (priv->routes, 0); + for (i = 0; i < array->len; i++) { + nm_ip6_route_ref (array->pdata[i]); + g_ptr_array_add (priv->routes, array->pdata[i]); + } + nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -325,11 +326,11 @@ nmt_route_table_get_property (GObject *object, break; case PROP_IP4_ROUTES: g_return_if_fail (priv->family == AF_INET); - nm_utils_ip4_routes_to_gvalue (priv->routes, value); + g_value_set_boxed (value, priv->routes); break; case PROP_IP6_ROUTES: g_return_if_fail (priv->family == AF_INET6); - nm_utils_ip6_routes_to_gvalue (priv->routes, value); + g_value_set_boxed (value, priv->routes); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -337,11 +338,6 @@ nmt_route_table_get_property (GObject *object, } } -#define DBUS_TYPE_G_ARRAY_OF_UINT (dbus_g_type_get_collection ("GArray", G_TYPE_UINT)) -#define DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_ARRAY_OF_UINT)) -#define DBUS_TYPE_G_IP6_ROUTE (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID)) -#define DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_IP6_ROUTE)) - static void nmt_route_table_class_init (NmtRouteTableClass *table_class) { @@ -373,11 +369,13 @@ nmt_route_table_class_init (NmtRouteTableClass *table_class) * #NMSettingIP4Config:routes. * * Only valid if #NmtRouteTable:family is %AF_INET + * + * Element-type: NMIP4Route */ g_object_class_install_property (object_class, PROP_IP4_ROUTES, g_param_spec_boxed ("ip4-routes", "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + G_TYPE_PTR_ARRAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); /** @@ -387,11 +385,13 @@ nmt_route_table_class_init (NmtRouteTableClass *table_class) * #NMSettingIP6Config:routes. * * Only valid if #NmtRouteTable:family is %AF_INET6 + * + * Element-type: NMIP6Route */ g_object_class_install_property (object_class, PROP_IP6_ROUTES, g_param_spec_boxed ("ip6-routes", "", "", - DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE, + G_TYPE_PTR_ARRAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); } diff --git a/clients/tui/nmt-secret-agent.c b/clients/tui/nmt-secret-agent.c index 658740e2f9..cabe5b4a87 100644 --- a/clients/tui/nmt-secret-agent.c +++ b/clients/tui/nmt-secret-agent.c @@ -329,13 +329,16 @@ request_secrets_from_ui (NmtSecretAgentRequest *request) if (nm_connection_is_type (request->connection, NM_SETTING_WIRELESS_SETTING_NAME)) { NMSettingWireless *s_wireless; - char *ssid; + GBytes *ssid; + char *ssid_utf8; s_wireless = nm_connection_get_setting_wireless (request->connection); - ssid = nm_utils_ssid_to_utf8 (nm_setting_wireless_get_ssid (s_wireless)); + ssid = nm_setting_wireless_get_ssid (s_wireless); + ssid_utf8 = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); title = _("Authentication required by wireless network"); - msg = g_strdup_printf (_("Passwords or encryption keys are required to access the wireless network '%s'."), ssid); + msg = g_strdup_printf (_("Passwords or encryption keys are required to access the wireless network '%s'."), ssid_utf8); ok = add_wireless_secrets (request, secrets); } else if (nm_connection_is_type (request->connection, NM_SETTING_WIRED_SETTING_NAME)) { diff --git a/examples/C/glib/get-ap-info-libnm.c b/examples/C/glib/get-ap-info-libnm.c index aba22c6dd9..acd93f9ba7 100644 --- a/examples/C/glib/get-ap-info-libnm.c +++ b/examples/C/glib/get-ap-info-libnm.c @@ -97,7 +97,7 @@ show_access_point_info (NMAccessPoint *ap) strength = nm_access_point_get_strength (ap); /* Convert to strings */ - ssid_str = nm_utils_ssid_to_utf8 (ssid); + ssid_str = nm_utils_ssid_to_utf8 (ssid->data, ssid->len); freq_str = g_strdup_printf ("%u MHz", freq); bitrate_str = g_strdup_printf ("%u Mbit/s", bitrate/1000); strength_str = g_strdup_printf ("%u", strength); @@ -163,7 +163,7 @@ show_wifi_device_info (NMDevice *device) if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) { if ((active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (device)))) { active_ssid = nm_access_point_get_ssid (active_ap); - active_ssid_str = nm_utils_ssid_to_utf8 (active_ssid); + active_ssid_str = nm_utils_ssid_to_utf8 (active_ssid->data, active_ssid->len); } } diff --git a/libnm-core/Makefile.libnm-core b/libnm-core/Makefile.libnm-core index b7924847e3..c68a720f7e 100644 --- a/libnm-core/Makefile.libnm-core +++ b/libnm-core/Makefile.libnm-core @@ -43,8 +43,8 @@ libnm_core_headers = \ libnm_core_private_headers = \ $(core)/crypto.h \ - $(core)/nm-core-internal.h \ - $(core)/nm-param-spec-specialized.h \ + $(core)/nm-core-internal.h \ + $(core)/nm-property-compare.h \ $(core)/nm-setting-private.h \ $(core)/nm-utils-private.h @@ -52,7 +52,7 @@ libnm_core_sources = \ $(core_build)/nm-core-enum-types.c \ $(core)/crypto.c \ $(core)/nm-connection.c \ - $(core)/nm-param-spec-specialized.c \ + $(core)/nm-property-compare.c \ $(core)/nm-setting-8021x.c \ $(core)/nm-setting-adsl.c \ $(core)/nm-setting-bluetooth.c \ diff --git a/libnm-core/crypto.c b/libnm-core/crypto.c index d5f7608d6e..2cc671c7df 100644 --- a/libnm-core/crypto.c +++ b/libnm-core/crypto.c @@ -60,19 +60,20 @@ _nm_crypto_error_quark (void) static gboolean find_tag (const char *tag, - const GByteArray *array, + const guint8 *data, + gsize data_len, gsize start_at, gsize *out_pos) { gsize i, taglen; - gsize len = array->len - start_at; + gsize len = data_len - start_at; g_return_val_if_fail (out_pos != NULL, FALSE); taglen = strlen (tag); if (len >= taglen) { for (i = 0; i < len - taglen + 1; i++) { - if (memcmp (array->data + start_at + i, tag, taglen) == 0) { + if (memcmp (data + start_at + i, tag, taglen) == 0) { *out_pos = start_at + i; return TRUE; } @@ -85,7 +86,8 @@ find_tag (const char *tag, #define PROC_TYPE_TAG "Proc-Type: " static GByteArray * -parse_old_openssl_key_file (const GByteArray *contents, +parse_old_openssl_key_file (const guint8 *data, + gsize data_len, int key_type, char **out_cipher, char **out_iv, @@ -123,11 +125,11 @@ parse_old_openssl_key_file (const GByteArray *contents, return NULL; } - if (!find_tag (start_tag, contents, 0, &start)) + if (!find_tag (start_tag, data, data_len, 0, &start)) goto parse_error; start += strlen (start_tag); - if (!find_tag (end_tag, contents, start, &end)) { + if (!find_tag (end_tag, data, data_len, start, &end)) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, _("PEM key file had no end tag '%s'."), @@ -135,10 +137,10 @@ parse_old_openssl_key_file (const GByteArray *contents, goto parse_error; } - save_end = contents->data[end]; - contents->data[end] = '\0'; - lines = g_strsplit ((const char *) (contents->data + start), "\n", 0); - contents->data[end] = save_end; + save_end = data[end]; + ((guint8 *)data)[end] = '\0'; + lines = g_strsplit ((const char *) (data + start), "\n", 0); + ((guint8 *)data)[end] = save_end; if (!lines || g_strv_length (lines) <= 1) { g_set_error (error, NM_CRYPTO_ERROR, @@ -258,7 +260,8 @@ parse_error: } static GByteArray * -parse_pkcs8_key_file (const GByteArray *contents, +parse_pkcs8_key_file (const guint8 *data, + gsize data_len, gboolean *out_encrypted, GError **error) { @@ -271,11 +274,11 @@ parse_pkcs8_key_file (const GByteArray *contents, gboolean encrypted = FALSE; /* Try encrypted first, decrypted next */ - if (find_tag (PEM_PKCS8_ENC_KEY_BEGIN, contents, 0, &start)) { + if (find_tag (PEM_PKCS8_ENC_KEY_BEGIN, data, data_len, 0, &start)) { start_tag = PEM_PKCS8_ENC_KEY_BEGIN; end_tag = PEM_PKCS8_ENC_KEY_END; encrypted = TRUE; - } else if (find_tag (PEM_PKCS8_DEC_KEY_BEGIN, contents, 0, &start)) { + } else if (find_tag (PEM_PKCS8_DEC_KEY_BEGIN, data, data_len, 0, &start)) { start_tag = PEM_PKCS8_DEC_KEY_BEGIN; end_tag = PEM_PKCS8_DEC_KEY_END; encrypted = FALSE; @@ -287,7 +290,7 @@ parse_pkcs8_key_file (const GByteArray *contents, } start += strlen (start_tag); - if (!find_tag (end_tag, contents, start, &end)) { + if (!find_tag (end_tag, data, data_len, start, &end)) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, _("Failed to find expected PKCS#8 end tag '%s'."), @@ -296,10 +299,10 @@ parse_pkcs8_key_file (const GByteArray *contents, } /* g_base64_decode() wants a NULL-terminated string */ - save_end = contents->data[end]; - contents->data[end] = '\0'; - der = g_base64_decode ((const char *) (contents->data + start), &length); - contents->data[end] = save_end; + save_end = data[end]; + ((guint8 *)data)[end] = '\0'; + der = g_base64_decode ((const char *) (data + start), &length); + ((guint8 *)data)[end] = save_end; if (der && length) { key = g_byte_array_sized_new (length); @@ -439,7 +442,8 @@ error: static GByteArray * decrypt_key (const char *cipher, int key_type, - GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, const char *password, GError **error) @@ -464,7 +468,7 @@ decrypt_key (const char *cipher, goto out; output = crypto_decrypt (cipher, key_type, - data, + data, data_len, bin_iv, bin_iv_len, key, key_len, &decrypted_len, @@ -486,32 +490,33 @@ out: } GByteArray * -crypto_decrypt_private_key_data (const GByteArray *contents, +crypto_decrypt_private_key_data (const guint8 *data, + gsize data_len, const char *password, NMCryptoKeyType *out_key_type, GError **error) { GByteArray *decrypted = NULL; NMCryptoKeyType key_type = NM_CRYPTO_KEY_TYPE_RSA; - GByteArray *data; + GByteArray *parsed; char *iv = NULL; char *cipher = NULL; - g_return_val_if_fail (contents != NULL, NULL); + g_return_val_if_fail (data != NULL, NULL); if (out_key_type) g_return_val_if_fail (*out_key_type == NM_CRYPTO_KEY_TYPE_UNKNOWN, NULL); /* OpenSSL non-standard legacy PEM files */ /* Try RSA keys first */ - data = parse_old_openssl_key_file (contents, key_type, &cipher, &iv, error); - if (!data) { + parsed = parse_old_openssl_key_file (data, data_len, key_type, &cipher, &iv, error); + if (!parsed) { g_clear_error (error); /* DSA next */ key_type = NM_CRYPTO_KEY_TYPE_DSA; - data = parse_old_openssl_key_file (contents, key_type, &cipher, &iv, error); - if (!data) { + parsed = parse_old_openssl_key_file (data, data_len, key_type, &cipher, &iv, error); + if (!parsed) { g_clear_error (error); g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, @@ -519,7 +524,7 @@ crypto_decrypt_private_key_data (const GByteArray *contents, } } - if (data) { + if (parsed) { /* return the key type even if decryption failed */ if (out_key_type) *out_key_type = key_type; @@ -527,12 +532,13 @@ crypto_decrypt_private_key_data (const GByteArray *contents, if (password) { decrypted = decrypt_key (cipher, key_type, - data, + parsed->data, + parsed->len, iv, password, error); } - g_byte_array_free (data, TRUE); + g_byte_array_free (parsed, TRUE); } g_free (cipher); @@ -552,7 +558,8 @@ crypto_decrypt_private_key (const char *file, contents = file_to_g_byte_array (file, error); if (contents) { - key = crypto_decrypt_private_key_data (contents, password, out_key_type, error); + key = crypto_decrypt_private_key_data (contents->data, contents->len, + password, out_key_type, error); g_byte_array_free (contents, TRUE); } return key; @@ -567,7 +574,7 @@ extract_pem_cert_data (GByteArray *contents, GError **error) guint8 save_end; gsize length = 0; - if (!find_tag (PEM_CERT_BEGIN, contents, 0, &start)) { + if (!find_tag (PEM_CERT_BEGIN, contents->data, contents->len, 0, &start)) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, _("PEM certificate had no start tag '%s'."), @@ -576,7 +583,7 @@ extract_pem_cert_data (GByteArray *contents, GError **error) } start += strlen (PEM_CERT_BEGIN); - if (!find_tag (PEM_CERT_END, contents, start, &end)) { + if (!find_tag (PEM_CERT_END, contents->data, contents->len, start, &end)) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, _("PEM certificate had no end tag '%s'."), @@ -621,7 +628,7 @@ crypto_load_and_verify_certificate (const char *file, return NULL; /* Check for PKCS#12 */ - if (crypto_is_pkcs12_data (contents)) { + if (crypto_is_pkcs12_data (contents->data, contents->len)) { *out_file_format = NM_CRYPTO_FILE_FORMAT_PKCS12; return contents; } @@ -649,14 +656,15 @@ crypto_load_and_verify_certificate (const char *file, } gboolean -crypto_is_pkcs12_data (const GByteArray *data) +crypto_is_pkcs12_data (const guint8 *data, + gsize data_len) { GError *error = NULL; gboolean success; g_return_val_if_fail (data != NULL, FALSE); - success = crypto_verify_pkcs12 (data, NULL, &error); + success = crypto_verify_pkcs12 (data, data_len, NULL, &error); if (success == FALSE) { /* If the error was just a decryption error, then it's pkcs#12 */ if (error) { @@ -678,7 +686,7 @@ crypto_is_pkcs12_file (const char *file, GError **error) contents = file_to_g_byte_array (file, error); if (contents) { - success = crypto_is_pkcs12_data (contents); + success = crypto_is_pkcs12_data (contents->data, contents->len); g_byte_array_free (contents, TRUE); } return success; @@ -688,7 +696,8 @@ crypto_is_pkcs12_file (const char *file, GError **error) * the private key can be decrypted with that password. */ NMCryptoFileFormat -crypto_verify_private_key_data (const GByteArray *contents, +crypto_verify_private_key_data (const guint8 *data, + gsize data_len, const char *password, GError **error) { @@ -697,23 +706,23 @@ crypto_verify_private_key_data (const GByteArray *contents, NMCryptoKeyType ktype = NM_CRYPTO_KEY_TYPE_UNKNOWN; gboolean is_encrypted = FALSE; - g_return_val_if_fail (contents != NULL, FALSE); + g_return_val_if_fail (data != NULL, FALSE); /* Check for PKCS#12 first */ - if (crypto_is_pkcs12_data (contents)) { - if (!password || crypto_verify_pkcs12 (contents, password, error)) + if (crypto_is_pkcs12_data (data, data_len)) { + if (!password || crypto_verify_pkcs12 (data, data_len, password, error)) format = NM_CRYPTO_FILE_FORMAT_PKCS12; } else { /* Maybe it's PKCS#8 */ - tmp = parse_pkcs8_key_file (contents, &is_encrypted, error); + tmp = parse_pkcs8_key_file (data, data_len, &is_encrypted, error); if (tmp) { - if (crypto_verify_pkcs8 (tmp, is_encrypted, password, error)) + if (crypto_verify_pkcs8 (tmp->data, tmp->len, is_encrypted, password, error)) format = NM_CRYPTO_FILE_FORMAT_RAW_KEY; } else { g_clear_error (error); /* Or it's old-style OpenSSL */ - tmp = crypto_decrypt_private_key_data (contents, password, &ktype, error); + tmp = crypto_decrypt_private_key_data (data, data_len, password, &ktype, error); if (tmp) format = NM_CRYPTO_FILE_FORMAT_RAW_KEY; else if (!password && (ktype != NM_CRYPTO_KEY_TYPE_UNKNOWN)) @@ -742,7 +751,7 @@ crypto_verify_private_key (const char *filename, contents = file_to_g_byte_array (filename, error); if (contents) { - format = crypto_verify_private_key_data (contents, password, error); + format = crypto_verify_private_key_data (contents->data, contents->len, password, error); g_byte_array_free (contents, TRUE); } return format; diff --git a/libnm-core/crypto.h b/libnm-core/crypto.h index edb79007f1..a530080ec6 100644 --- a/libnm-core/crypto.h +++ b/libnm-core/crypto.h @@ -72,7 +72,8 @@ gboolean crypto_init (GError **error); void crypto_deinit (void); -GByteArray *crypto_decrypt_private_key_data (const GByteArray *contents, +GByteArray *crypto_decrypt_private_key_data (const guint8 *data, + gsize data_len, const char *password, NMCryptoKeyType *out_key_type, GError **error); @@ -88,9 +89,10 @@ GByteArray *crypto_load_and_verify_certificate (const char *file, gboolean crypto_is_pkcs12_file (const char *file, GError **error); -gboolean crypto_is_pkcs12_data (const GByteArray *data); +gboolean crypto_is_pkcs12_data (const guint8 *data, gsize len); -NMCryptoFileFormat crypto_verify_private_key_data (const GByteArray *contents, +NMCryptoFileFormat crypto_verify_private_key_data (const guint8 *data, + gsize data_len, const char *password, GError **error); @@ -110,7 +112,8 @@ gboolean crypto_md5_hash (const char *salt, char * crypto_decrypt (const char *cipher, int key_type, - GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, const gsize iv_len, const char *key, @@ -119,7 +122,8 @@ char * crypto_decrypt (const char *cipher, GError **error); char * crypto_encrypt (const char *cipher, - const GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, gsize iv_len, const char *key, @@ -129,15 +133,17 @@ char * crypto_encrypt (const char *cipher, gboolean crypto_randomize (void *buffer, gsize buffer_len, GError **error); -NMCryptoFileFormat crypto_verify_cert (const unsigned char *data, +NMCryptoFileFormat crypto_verify_cert (const guint8 *data, gsize len, GError **error); -gboolean crypto_verify_pkcs12 (const GByteArray *data, +gboolean crypto_verify_pkcs12 (const guint8 *data, + gsize data_len, const char *password, GError **error); -gboolean crypto_verify_pkcs8 (const GByteArray *data, +gboolean crypto_verify_pkcs8 (const guint8 *data, + gsize data_len, gboolean is_encrypted, const char *password, GError **error); diff --git a/libnm-core/crypto_gnutls.c b/libnm-core/crypto_gnutls.c index be16d135af..e79d4fd3e2 100644 --- a/libnm-core/crypto_gnutls.c +++ b/libnm-core/crypto_gnutls.c @@ -120,7 +120,8 @@ crypto_md5_hash (const char *salt, char * crypto_decrypt (const char *cipher, int key_type, - GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, const gsize iv_len, const char *key, @@ -160,7 +161,7 @@ crypto_decrypt (const char *cipher, return NULL; } - output = g_malloc0 (data->len); + output = g_malloc0 (data_len); err = gcry_cipher_open (&ctx, cipher_mech, GCRY_CIPHER_MODE_CBC, 0); if (err) { @@ -189,7 +190,7 @@ crypto_decrypt (const char *cipher, goto out; } - err = gcry_cipher_decrypt (ctx, output, data->len, data->data, data->len); + err = gcry_cipher_decrypt (ctx, output, data_len, data, data_len); if (err) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, @@ -197,7 +198,7 @@ crypto_decrypt (const char *cipher, gcry_strsource (err), gcry_strerror (err)); goto out; } - pad_len = output[data->len - 1]; + pad_len = output[data_len - 1]; /* Check if the padding at the end of the decrypted data is valid */ if (pad_len == 0 || pad_len > real_iv_len) { @@ -211,7 +212,7 @@ crypto_decrypt (const char *cipher, * should contain the padding size. */ for (i = 1; i <= pad_len; ++i) { - if (output[data->len - i] != pad_len) { + if (output[data_len - i] != pad_len) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, _("Failed to decrypt the private key.")); @@ -219,14 +220,14 @@ crypto_decrypt (const char *cipher, } } - *out_len = data->len - pad_len; + *out_len = data_len - pad_len; success = TRUE; out: if (!success) { if (output) { /* Don't expose key material */ - memset (output, 0, data->len); + memset (output, 0, data_len); g_free (output); output = NULL; } @@ -237,7 +238,8 @@ out: char * crypto_encrypt (const char *cipher, - const GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, const gsize iv_len, const char *key, @@ -269,16 +271,16 @@ crypto_encrypt (const char *cipher, return NULL; } - /* If data->len % ivlen == 0, then we add another complete block + /* If data_len % ivlen == 0, then we add another complete block * onto the end so that the decrypter knows there's padding. */ - pad_len = iv_len - (data->len % iv_len); - output_len = padded_buf_len = data->len + pad_len; + pad_len = iv_len - (data_len % iv_len); + output_len = padded_buf_len = data_len + pad_len; padded_buf = g_malloc0 (padded_buf_len); - memcpy (padded_buf, data->data, data->len); + memcpy (padded_buf, data, data_len); for (i = 0; i < pad_len; i++) - padded_buf[data->len + i] = (guint8) (pad_len & 0xFF); + padded_buf[data_len + i] = (guint8) (pad_len & 0xFF); output = g_malloc0 (output_len); @@ -382,7 +384,8 @@ crypto_verify_cert (const unsigned char *data, } gboolean -crypto_verify_pkcs12 (const GByteArray *data, +crypto_verify_pkcs12 (const guint8 *data, + gsize data_len, const char *password, GError **error) { @@ -393,8 +396,8 @@ crypto_verify_pkcs12 (const GByteArray *data, g_return_val_if_fail (data != NULL, FALSE); - dt.data = (unsigned char *) data->data; - dt.size = data->len; + dt.data = (unsigned char *) data; + dt.size = data_len; err = gnutls_pkcs12_init (&p12); if (err < 0) { @@ -435,7 +438,8 @@ out: } gboolean -crypto_verify_pkcs8 (const GByteArray *data, +crypto_verify_pkcs8 (const guint8 *data, + gsize data_len, gboolean is_encrypted, const char *password, GError **error) @@ -446,8 +450,8 @@ crypto_verify_pkcs8 (const GByteArray *data, g_return_val_if_fail (data != NULL, FALSE); - dt.data = (unsigned char *) data->data; - dt.size = data->len; + dt.data = (unsigned char *) data; + dt.size = data_len; err = gnutls_x509_privkey_init (&p8); if (err < 0) { diff --git a/libnm-core/crypto_nss.c b/libnm-core/crypto_nss.c index 1e589ea709..cc6418f452 100644 --- a/libnm-core/crypto_nss.c +++ b/libnm-core/crypto_nss.c @@ -134,7 +134,8 @@ crypto_md5_hash (const char *salt, char * crypto_decrypt (const char *cipher, int key_type, - GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, const gsize iv_len, const char *key, @@ -180,7 +181,7 @@ crypto_decrypt (const char *cipher, return NULL; } - output = g_malloc0 (data->len); + output = g_malloc0 (data_len); slot = PK11_GetBestSlot (cipher_mech, NULL); if (!slot) { @@ -221,9 +222,9 @@ crypto_decrypt (const char *cipher, s = PK11_CipherOp (ctx, (unsigned char *) output, &decrypted_len, - data->len, - data->data, - data->len); + data_len, + data, + data_len); if (s != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, @@ -232,7 +233,7 @@ crypto_decrypt (const char *cipher, goto out; } - if (decrypted_len > data->len) { + if (decrypted_len > data_len) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, _("Failed to decrypt the private key: decrypted data too large.")); @@ -242,7 +243,7 @@ crypto_decrypt (const char *cipher, s = PK11_DigestFinal (ctx, (unsigned char *) (output + decrypted_len), &extra, - data->len - decrypted_len); + data_len - decrypted_len); if (s != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, @@ -251,7 +252,7 @@ crypto_decrypt (const char *cipher, goto out; } decrypted_len += extra; - pad_len = data->len - decrypted_len; + pad_len = data_len - decrypted_len; /* Check if the padding at the end of the decrypted data is valid */ if (pad_len == 0 || pad_len > real_iv_len) { @@ -265,7 +266,7 @@ crypto_decrypt (const char *cipher, * should contain the padding size. */ for (i = pad_len; i > 0; i--) { - if (output[data->len - i] != pad_len) { + if (output[data_len - i] != pad_len) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_CIPHER_DECRYPT_FAILED, _("Failed to decrypt the private key.")); @@ -289,7 +290,7 @@ out: if (!success) { if (output) { /* Don't expose key material */ - memset (output, 0, data->len); + memset (output, 0, data_len); g_free (output); output = NULL; } @@ -299,7 +300,8 @@ out: char * crypto_encrypt (const char *cipher, - const GByteArray *data, + const guint8 *data, + gsize data_len, const char *iv, gsize iv_len, const char *key, @@ -336,13 +338,13 @@ crypto_encrypt (const char *cipher, /* If data->len % ivlen == 0, then we add another complete block * onto the end so that the decrypter knows there's padding. */ - pad_len = iv_len - (data->len % iv_len); - output_len = padded_buf_len = data->len + pad_len; + pad_len = iv_len - (data_len % iv_len); + output_len = padded_buf_len = data_len + pad_len; padded_buf = g_malloc0 (padded_buf_len); - memcpy (padded_buf, data->data, data->len); + memcpy (padded_buf, data, data_len); for (i = 0; i < pad_len; i++) - padded_buf[data->len + i] = (guint8) (pad_len & 0xFF); + padded_buf[data_len + i] = (guint8) (pad_len & 0xFF); output = g_malloc0 (output_len); @@ -440,7 +442,8 @@ crypto_verify_cert (const unsigned char *data, } gboolean -crypto_verify_pkcs12 (const GByteArray *data, +crypto_verify_pkcs12 (const guint8 *data, + gsize data_len, const char *password, GError **error) { @@ -498,7 +501,7 @@ crypto_verify_pkcs12 (const GByteArray *data, goto error; } - s = SEC_PKCS12DecoderUpdate (p12ctx, data->data, data->len); + s = SEC_PKCS12DecoderUpdate (p12ctx, (guint8 *)data, data_len); if (s != SECSuccess) { g_set_error (error, NM_CRYPTO_ERROR, NM_CRYPTO_ERR_FILE_FORMAT_INVALID, @@ -532,7 +535,8 @@ error: } gboolean -crypto_verify_pkcs8 (const GByteArray *data, +crypto_verify_pkcs8 (const guint8 *data, + gsize data_len, gboolean is_encrypted, const char *password, GError **error) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index a167117b8e..e02ccf98ff 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -256,12 +256,11 @@ validate_permissions_type (GHashTable *hash, GError **error) if (s_con) { permissions = g_hash_table_lookup (s_con, NM_SETTING_CONNECTION_PERMISSIONS); if (permissions) { - if ( !G_VALUE_HOLDS (permissions, G_TYPE_STRV) - && !G_VALUE_HOLDS (permissions, DBUS_TYPE_G_LIST_OF_STRING)) { + if (!G_VALUE_HOLDS (permissions, G_TYPE_STRV)) { g_set_error_literal (error, NM_SETTING_ERROR, NM_SETTING_ERROR_PROPERTY_TYPE_MISMATCH, - "Wrong permissions property type; should be a list of strings."); + "Wrong permissions property type; should be an array of strings."); return FALSE; } } diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 1e6dd63892..d5e85e1475 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -63,5 +63,14 @@ gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *s GSList * _nm_utils_hash_values_to_slist (GHashTable *hash); +GHashTable *_nm_utils_copy_strdict (GHashTable *strdict); + +typedef gpointer (*NMUtilsCopyFunc) (gpointer); + +GPtrArray *_nm_utils_copy_slist_to_array (const GSList *list, + NMUtilsCopyFunc copy_func, + GDestroyNotify unref_func); +GSList *_nm_utils_copy_array_to_slist (const GPtrArray *array, + NMUtilsCopyFunc copy_func); #endif diff --git a/libnm-core/nm-param-spec-specialized.h b/libnm-core/nm-param-spec-specialized.h deleted file mode 100644 index 100e070524..0000000000 --- a/libnm-core/nm-param-spec-specialized.h +++ /dev/null @@ -1,43 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - -/* - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2007 - 2008 Red Hat, Inc. - * Copyright 2007 - 2008 Novell, Inc. - */ - -#ifndef __NM_PARAM_SPEC_SPECIALIZED_H__ -#define __NM_PARAM_SPEC_SPECIALIZED_H__ - -#include - -typedef struct _NMParamSpecSpecialized NMParamSpecSpecialized; - -#define NM_TYPE_PARAM_SPEC_SPECIALIZED (_nm_param_spec_specialized_get_type ()) - -#define NM_IS_PARAM_SPEC_SPECIALIZED(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), NM_TYPE_PARAM_SPEC_SPECIALIZED)) -#define NM_PARAM_SPEC_SPECIALIZED(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), NM_TYPE_PARAM_SPEC_SPECIALIZED, NMParamSpecSpecialized)) - -GType _nm_param_spec_specialized_get_type (void); - -GParamSpec *_nm_param_spec_specialized (const char *name, - const char *nick, - const char *blurb, - GType specialized_type, - GParamFlags flags); - -#endif /* __NM_PARAM_SPEC_SPECIALIZED_H__ */ diff --git a/libnm-core/nm-param-spec-specialized.c b/libnm-core/nm-property-compare.c similarity index 51% rename from libnm-core/nm-param-spec-specialized.c rename to libnm-core/nm-property-compare.c index 2b4ca230a0..4f6fae8bb8 100644 --- a/libnm-core/nm-param-spec-specialized.c +++ b/libnm-core/nm-property-compare.c @@ -16,17 +16,13 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2007 - 2011 Red Hat, Inc. + * Copyright 2007 - 2014 Red Hat, Inc. * Copyright 2007 - 2008 Novell, Inc. */ -#include "nm-param-spec-specialized.h" +#include "nm-property-compare.h" #include "nm-glib-compat.h" -struct _NMParamSpecSpecialized { - GParamSpec parent; -}; - #include #include #include @@ -34,11 +30,6 @@ struct _NMParamSpecSpecialized { #include "nm-dbus-glib-types.h" -/***********************************************************/ -/* _gvalues_compare */ - -static gint _gvalues_compare (const GValue *value1, const GValue *value2); - static gboolean type_is_fixed_size (GType type, gsize *tsize) { @@ -84,7 +75,7 @@ type_is_fixed_size (GType type, gsize *tsize) #define FLOAT_FACTOR 0.00000001 static gint -_gvalues_compare_fixed (const GValue *value1, const GValue *value2) +nm_property_compare_fixed (const GValue *value1, const GValue *value2) { int ret = 0; @@ -175,7 +166,7 @@ _gvalues_compare_fixed (const GValue *value1, const GValue *value2) } static gint -_gvalues_compare_string (const GValue *value1, const GValue *value2) +nm_property_compare_string (const GValue *value1, const GValue *value2) { const char *str1 = g_value_get_string (value1); const char *str2 = g_value_get_string (value2); @@ -192,7 +183,7 @@ _gvalues_compare_string (const GValue *value1, const GValue *value2) } static gint -_gvalues_compare_strv (const GValue *value1, const GValue *value2) +nm_property_compare_strv (const GValue *value1, const GValue *value2) { char **strv1; char **strv2; @@ -248,7 +239,7 @@ iterate_collection (const GValue *value, gpointer user_data) } static gint -_gvalues_compare_collection (const GValue *value1, const GValue *value2) +nm_property_compare_collection (const GValue *value1, const GValue *value2) { gint ret; guint len1; @@ -285,7 +276,7 @@ _gvalues_compare_collection (const GValue *value1, const GValue *value2) for (iter1 = list1, iter2 = list2, ret = 0; ret == 0 && iter1 && iter2; iter1 = iter1->next, iter2 = iter2->next) - ret = _gvalues_compare ((GValue *) iter1->data, (GValue *) iter2->data); + ret = nm_property_compare ((GValue *) iter1->data, (GValue *) iter2->data); } g_slist_free_full (list1, _gvalue_destroy); @@ -321,13 +312,13 @@ compare_one_map_item (gpointer key, gpointer val, gpointer user_data) value2 = (GValue *) g_hash_table_lookup (info->hash2, key); if (value2) - info->ret = _gvalues_compare ((GValue *) val, value2); + info->ret = nm_property_compare ((GValue *) val, value2); else info->ret = 1; } static gint -_gvalues_compare_map (const GValue *value1, const GValue *value2) +nm_property_compare_map (const GValue *value1, const GValue *value2) { GHashTable *hash1 = NULL; GHashTable *hash2 = NULL; @@ -487,10 +478,10 @@ _gvalue_ip6_route_compare (const GValue *value1, const GValue *value2) } static gint -_gvalues_compare_struct (const GValue *value1, const GValue *value2) +nm_property_compare_struct (const GValue *value1, const GValue *value2) { /* value1 and value2 must contain the same type since - * _gvalues_compare() enforced that already. + * nm_property_compare() enforced that already. */ if (G_VALUE_HOLDS (value1, DBUS_TYPE_G_IP6_ADDRESS)) { @@ -503,8 +494,8 @@ _gvalues_compare_struct (const GValue *value1, const GValue *value2) } } -gint -_gvalues_compare (const GValue *value1, const GValue *value2) +int +nm_property_compare (const GValue *value1, const GValue *value2) { GType type1; GType type2; @@ -524,9 +515,9 @@ _gvalues_compare (const GValue *value1, const GValue *value2) return type1 < type2 ? -1 : type1 > type2; if (type_is_fixed_size (type1, NULL)) - ret = _gvalues_compare_fixed (value1, value2); + ret = nm_property_compare_fixed (value1, value2); else if (type1 == G_TYPE_STRING) - ret = _gvalues_compare_string (value1, value2); + ret = nm_property_compare_string (value1, value2); else if (G_VALUE_HOLDS_BOXED (value1)) { gpointer p1 = g_value_get_boxed (value1); gpointer p2 = g_value_get_boxed (value2); @@ -538,15 +529,15 @@ _gvalues_compare (const GValue *value1, const GValue *value2) else if (!p2) ret = -1; /* The comparision functions below don't handle NULLs */ else if (type1 == G_TYPE_STRV) - ret = _gvalues_compare_strv (value1, value2); + ret = nm_property_compare_strv (value1, value2); else if (dbus_g_type_is_collection (type1)) - ret = _gvalues_compare_collection (value1, value2); + ret = nm_property_compare_collection (value1, value2); else if (dbus_g_type_is_map (type1)) - ret = _gvalues_compare_map (value1, value2); + ret = nm_property_compare_map (value1, value2); else if (dbus_g_type_is_struct (type1)) - ret = _gvalues_compare_struct (value1, value2); + ret = nm_property_compare_struct (value1, value2); else if (type1 == G_TYPE_VALUE) - ret = _gvalues_compare ((GValue *) g_value_get_boxed (value1), (GValue *) g_value_get_boxed (value2)); + ret = nm_property_compare ((GValue *) g_value_get_boxed (value1), (GValue *) g_value_get_boxed (value2)); else { g_warning ("Don't know how to compare boxed types '%s'", g_type_name (type1)); ret = value1 == value2; @@ -558,415 +549,3 @@ _gvalues_compare (const GValue *value1, const GValue *value2) return ret; } - -/***********************************************************/ - -static void -param_specialized_init (GParamSpec *pspec) -{ -} - -static void -param_specialized_set_default (GParamSpec *pspec, GValue *value) -{ - value->data[0].v_pointer = NULL; -} - -static gboolean -param_specialized_validate (GParamSpec *pspec, GValue *value) -{ - NMParamSpecSpecialized *sspec = NM_PARAM_SPEC_SPECIALIZED (pspec); - GType value_type = G_VALUE_TYPE (value); - gboolean changed = FALSE; - - if (!g_value_type_compatible (value_type, G_PARAM_SPEC_VALUE_TYPE (sspec))) { - g_value_reset (value); - changed = TRUE; - } - - return changed; -} - -static gint -param_specialized_values_cmp (GParamSpec *pspec, - const GValue *value1, - const GValue *value2) -{ - return _gvalues_compare (value1, value2); -} - -GType -_nm_param_spec_specialized_get_type (void) -{ - static GType type; - - if (G_UNLIKELY (type) == 0) { - static const GParamSpecTypeInfo pspec_info = { - sizeof (NMParamSpecSpecialized), - 0, - param_specialized_init, - G_TYPE_OBJECT, /* value_type */ - NULL, /* finalize */ - param_specialized_set_default, - param_specialized_validate, - param_specialized_values_cmp, - }; - type = g_param_type_register_static ("NMParamSpecSpecialized", &pspec_info); - } - - return type; -} - -GParamSpec * -_nm_param_spec_specialized (const char *name, - const char *nick, - const char *blurb, - GType specialized_type, - GParamFlags flags) -{ - NMParamSpecSpecialized *pspec; - - g_return_val_if_fail (g_type_is_a (specialized_type, G_TYPE_BOXED), NULL); - - pspec = g_param_spec_internal (NM_TYPE_PARAM_SPEC_SPECIALIZED, - name, nick, blurb, flags); - - G_PARAM_SPEC (pspec)->value_type = specialized_type; - - return G_PARAM_SPEC (pspec); -} - -/***********************************************************/ -/* Tests */ - -#if 0 - -static void -compare_ints (void) -{ - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - - g_value_init (&value1, G_TYPE_INT); - g_value_init (&value2, G_TYPE_INT); - - g_value_set_int (&value1, 5); - g_value_set_int (&value2, 5); - g_print ("Comparing ints 5 and 5: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_int (&value2, 10); - g_print ("Comparing ints 5 and 10: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_int (&value2, 1); - g_print ("Comparing ints 5 and 1: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_strings (void) -{ - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - const char *str1 = "hello"; - const char *str2 = "world"; - - g_value_init (&value1, G_TYPE_STRING); - g_value_init (&value2, G_TYPE_STRING); - - g_value_set_string (&value1, str1); - g_value_set_string (&value2, str1); - g_print ("Comparing identical strings: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_string (&value2, str2); - g_print ("Comparing different strings: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_strv (void) -{ - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - char *strv1[] = { "foo", "bar", "baz", NULL }; - char *strv2[] = { "foo", "bar", "bar", NULL }; - char *strv3[] = { "foo", "bar", NULL }; - char *strv4[] = { "foo", "bar", "baz", "bam", NULL }; - - g_value_init (&value1, G_TYPE_STRV); - g_value_init (&value2, G_TYPE_STRV); - - g_value_set_boxed (&value1, strv1); - g_value_set_boxed (&value2, strv1); - g_print ("Comparing identical strv's: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_boxed (&value2, strv2); - g_print ("Comparing different strv's: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_boxed (&value2, strv3); - g_print ("Comparing different len (smaller) strv's: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_boxed (&value2, strv4); - g_print ("Comparing different len (longer) strv's: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_garrays (void) -{ - GArray *array1; - GArray *array2; - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - int i; - - g_value_init (&value1, DBUS_TYPE_G_UINT_ARRAY); - array1 = g_array_new (FALSE, FALSE, sizeof (guint32)); - - g_value_init (&value2, DBUS_TYPE_G_UINT_ARRAY); - array2 = g_array_new (FALSE, FALSE, sizeof (guint32)); - - for (i = 0; i < 5; i++) { - g_array_append_val (array1, i); - g_array_append_val (array2, i); - } - - g_value_set_boxed (&value1, array1); - g_value_set_boxed (&value2, array2); - - g_print ("Comparing identical arrays's: %d\n", _gvalues_compare (&value1, &value2)); - - g_array_remove_index (array2, 0); - g_value_set_boxed (&value2, array2); - g_print ("Comparing different length arrays's: %d\n", _gvalues_compare (&value1, &value2)); - - i = 7; - g_array_prepend_val (array2, i); - g_value_set_boxed (&value2, array2); - g_print ("Comparing different arrays's: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_ptrarrays (void) -{ - GPtrArray *array1; - GPtrArray *array2; - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - - g_value_init (&value1, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING)); - array1 = g_ptr_array_new (); - - g_value_init (&value2, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING)); - array2 = g_ptr_array_new (); - - g_ptr_array_add (array1, "hello"); - g_ptr_array_add (array1, "world"); - g_value_set_boxed (&value1, array1); - - g_ptr_array_add (array2, "hello"); - g_ptr_array_add (array2, "world"); - g_value_set_boxed (&value2, array2); - - g_print ("Comparing identical ptr arrays's: %d\n", _gvalues_compare (&value1, &value2)); - - g_ptr_array_add (array2, "boo"); - g_value_set_boxed (&value2, array2); - g_print ("Comparing different len ptr arrays's: %d\n", _gvalues_compare (&value1, &value2)); - - g_ptr_array_add (array1, "booz"); - g_value_set_boxed (&value1, array1); - g_print ("Comparing different ptr arrays's: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_str_hash (void) -{ - GHashTable *hash1; - GHashTable *hash2; - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - - g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)); - g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)); - - hash1 = g_hash_table_new (g_str_hash, g_str_equal); - hash2 = g_hash_table_new (g_str_hash, g_str_equal); - - g_hash_table_insert (hash1, "key1", "hello"); - g_hash_table_insert (hash1, "key2", "world"); - - g_hash_table_insert (hash2, "key1", "hello"); - g_hash_table_insert (hash2, "key2", "world"); - - g_value_set_boxed (&value1, hash1); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing identical str hashes: %d\n", _gvalues_compare (&value1, &value2)); - - g_hash_table_remove (hash2, "key2"); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing different length str hashes: %d\n", _gvalues_compare (&value1, &value2)); - - g_hash_table_insert (hash2, "key2", "moon"); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing different str hashes: %d\n", _gvalues_compare (&value1, &value2)); -} - -static GValue * -str_to_gvalue (const char *str) -{ - GValue *value; - - value = g_slice_new0 (GValue); - g_value_init (value, G_TYPE_STRING); - g_value_set_string (value, str); - - return value; -} - -static GValue * -int_to_gvalue (int i) -{ - GValue *value; - - value = g_slice_new0 (GValue); - g_value_init (value, G_TYPE_INT); - g_value_set_int (value, i); - - return value; -} - -static void -compare_gvalue_hash (void) -{ - GHashTable *hash1; - GHashTable *hash2; - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - - g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)); - g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)); - - hash1 = g_hash_table_new (g_str_hash, g_str_equal); - hash2 = g_hash_table_new (g_str_hash, g_str_equal); - - g_hash_table_insert (hash1, "key1", str_to_gvalue ("hello")); - g_hash_table_insert (hash1, "key2", int_to_gvalue (5)); - - g_hash_table_insert (hash2, "key1", str_to_gvalue ("hello")); - g_hash_table_insert (hash2, "key2", int_to_gvalue (5)); - - g_value_set_boxed (&value1, hash1); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing identical gvalue hashes: %d\n", _gvalues_compare (&value1, &value2)); - - g_hash_table_remove (hash2, "key2"); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing different length str hashes: %d\n", _gvalues_compare (&value1, &value2)); - - g_hash_table_insert (hash2, "key2", str_to_gvalue ("moon")); - g_value_set_boxed (&value2, hash2); - g_print ("Comparing different str hashes: %d\n", _gvalues_compare (&value1, &value2)); -} - -static void -compare_ip6_addresses (void) -{ - GValueArray *array1; - GValueArray *array2; - GValueArray *array3; - GByteArray *ba1; - GByteArray *ba2; - GByteArray *ba3; - GValue element = G_VALUE_INIT; - GValue value1 = G_VALUE_INIT; - GValue value2 = G_VALUE_INIT; - struct in6_addr addr1; - struct in6_addr addr2; - struct in6_addr addr3; - guint32 prefix1 = 64; - guint32 prefix2 = 64; - guint32 prefix3 = 0; - - inet_pton (AF_INET6, "1:2:3:4:5:6:7:8", &addr1, sizeof (struct in6_addr)); - inet_pton (AF_INET6, "ffff:2:3:4:5:6:7:8", &addr2, sizeof (struct in6_addr)); - inet_pton (AF_INET6, "::", &addr3, sizeof (struct in6_addr)); - - /* address 1 */ - ba1 = g_byte_array_new (); - array1 = g_value_array_new (2); - g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); - g_byte_array_append (ba1, (guint8 *) addr1.s6_addr, 16); - g_value_take_boxed (&element, ba1); - g_value_array_append (array1, &element); - g_value_unset (&element); - - g_value_init (&element, G_TYPE_UINT); - g_value_set_uint (&element, prefix1); - g_value_array_append (array1, &element); - g_value_unset (&element); - - /* address 2 */ - ba2 = g_byte_array_new (); - array2 = g_value_array_new (2); - g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); - g_byte_array_append (ba2, (guint8 *) addr2.s6_addr, 16); - g_value_take_boxed (&element, ba2); - g_value_array_append (array2, &element); - g_value_unset (&element); - - g_value_init (&element, G_TYPE_UINT); - g_value_set_uint (&element, prefix2); - g_value_array_append (array2, &element); - g_value_unset (&element); - - /* address 3 */ - ba3 = g_byte_array_new (); - array3 = g_value_array_new (2); - g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); - g_byte_array_append (ba3, (guint8 *) addr3.s6_addr, 16); - g_value_take_boxed (&element, ba3); - g_value_array_append (array3, &element); - g_value_unset (&element); - - g_value_init (&element, G_TYPE_UINT); - g_value_set_uint (&element, prefix3); - g_value_array_append (array3, &element); - g_value_unset (&element); - - g_value_init (&value1, DBUS_TYPE_G_IP6_ADDRESS); - g_value_init (&value2, DBUS_TYPE_G_IP6_ADDRESS); - - g_value_set_boxed (&value1, array1); - g_value_set_boxed (&value2, array1); - g_print ("Comparing identical IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_boxed (&value1, array1); - g_value_set_boxed (&value2, array2); - g_print ("Comparing different IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2)); - - g_value_set_boxed (&value1, array1); - g_value_set_boxed (&value2, array3); - g_print ("Comparing different IPv6 address structures: %d\n", _gvalues_compare (&value1, &value2)); -} - -int -main (int argc, char *argv[]) -{ - DBusGConnection *bus; - -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - bus = dbus_g_bus_get (DBUS_BUS_SESSION, NULL); - - compare_ints (); - compare_strings (); - compare_strv (); - compare_garrays (); - compare_ptrarrays (); - compare_str_hash (); - compare_gvalue_hash (); - compare_ip6_addresses (); - - return 0; -} - -#endif diff --git a/libnm-core/nm-property-compare.h b/libnm-core/nm-property-compare.h new file mode 100644 index 0000000000..72fc76baf7 --- /dev/null +++ b/libnm-core/nm-property-compare.h @@ -0,0 +1,30 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#ifndef __NM_PROPERTY_COMPARE_H__ +#define __NM_PROPERTY_COMPARE_H__ + +#include + +int nm_property_compare (const GValue *value1, const GValue *value2); + +#endif /* __NM_PROPERTY_COMPARE_H__ */ diff --git a/libnm-core/nm-setting-8021x.c b/libnm-core/nm-setting-8021x.c index 0fbbca6ec1..0a63b74043 100644 --- a/libnm-core/nm-setting-8021x.c +++ b/libnm-core/nm-setting-8021x.c @@ -25,7 +25,6 @@ #include #include "nm-setting-8021x.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-dbus-glib-types.h" #include "crypto.h" @@ -95,31 +94,31 @@ typedef struct { char *identity; char *anonymous_identity; char *pac_file; - GByteArray *ca_cert; + GBytes *ca_cert; char *ca_path; char *subject_match; GSList *altsubject_matches; - GByteArray *client_cert; + GBytes *client_cert; char *phase1_peapver; char *phase1_peaplabel; char *phase1_fast_provisioning; char *phase2_auth; char *phase2_autheap; - GByteArray *phase2_ca_cert; + GBytes *phase2_ca_cert; char *phase2_ca_path; char *phase2_subject_match; GSList *phase2_altsubject_matches; - GByteArray *phase2_client_cert; + GBytes *phase2_client_cert; char *password; NMSettingSecretFlags password_flags; - GByteArray *password_raw; + GBytes *password_raw; NMSettingSecretFlags password_raw_flags; char *pin; NMSettingSecretFlags pin_flags; - GByteArray *private_key; + GBytes *private_key; char *private_key_password; NMSettingSecretFlags private_key_password_flags; - GByteArray *phase2_private_key; + GBytes *phase2_private_key; char *phase2_private_key_password; NMSettingSecretFlags phase2_private_key_password_flags; gboolean system_ca_certs; @@ -417,13 +416,20 @@ nm_setting_802_1x_get_system_ca_certs (NMSetting8021x *setting) } static NMSetting8021xCKScheme -get_cert_scheme (GByteArray *array) +get_cert_scheme (GBytes *bytes) { - if (!array || !array->len) + gconstpointer data; + gsize length; + + if (!bytes) return NM_SETTING_802_1X_CK_SCHEME_UNKNOWN; - if ( (array->len > strlen (SCHEME_PATH)) - && !memcmp (array->data, SCHEME_PATH, strlen (SCHEME_PATH))) + data = g_bytes_get_data (bytes, &length); + if (!length) + return NM_SETTING_802_1X_CK_SCHEME_UNKNOWN; + + if ( (length > strlen (SCHEME_PATH)) + && !memcmp (data, SCHEME_PATH, strlen (SCHEME_PATH))) return NM_SETTING_802_1X_CK_SCHEME_PATH; return NM_SETTING_802_1X_CK_SCHEME_BLOB; @@ -460,7 +466,7 @@ nm_setting_802_1x_get_ca_cert_scheme (NMSetting8021x *setting) * * Returns: the CA certificate data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_ca_cert_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -490,16 +496,18 @@ const char * nm_setting_802_1x_get_ca_cert_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_ca_cert_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->ca_cert->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->ca_cert, NULL); + return (const char *)data + strlen (SCHEME_PATH); } -static GByteArray * +static GBytes * path_to_scheme_value (const char *path) { GByteArray *array; @@ -508,11 +516,11 @@ path_to_scheme_value (const char *path) /* Add the path scheme tag to the front, then the fielname */ array = g_byte_array_sized_new (strlen (path) + strlen (SCHEME_PATH) + 1); - g_assert (array); g_byte_array_append (array, (const guint8 *) SCHEME_PATH, strlen (SCHEME_PATH)); g_byte_array_append (array, (const guint8 *) path, strlen (path)); g_byte_array_append (array, (const guint8 *) "\0", 1); - return array; + + return g_byte_array_free_to_bytes (array); } /** @@ -559,11 +567,7 @@ nm_setting_802_1x_set_ca_cert (NMSetting8021x *setting, priv = NM_SETTING_802_1X_GET_PRIVATE (setting); - /* Clear out any previous ca_cert blob */ - if (priv->ca_cert) { - g_byte_array_free (priv->ca_cert, TRUE); - priv->ca_cert = NULL; - } + g_clear_pointer (&priv->ca_cert, g_bytes_unref); if (!cert_path) { g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_CA_CERT); @@ -577,9 +581,10 @@ nm_setting_802_1x_set_ca_cert (NMSetting8021x *setting, if (out_format) *out_format = NM_SETTING_802_1X_CK_FORMAT_X509; - if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - priv->ca_cert = g_byte_array_ref (data); - else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) + if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { + priv->ca_cert = g_byte_array_free_to_bytes (data); + data = NULL; + } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->ca_cert = path_to_scheme_value (cert_path); else g_assert_not_reached (); @@ -590,7 +595,8 @@ nm_setting_802_1x_set_ca_cert (NMSetting8021x *setting, _("CA certificate must be in X.509 format")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_CA_CERT); } - g_byte_array_unref (data); + if (data) + g_byte_array_unref (data); } g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_CA_CERT); @@ -789,7 +795,7 @@ nm_setting_802_1x_get_client_cert_scheme (NMSetting8021x *setting) * * Returns: the client certificate data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_client_cert_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -816,13 +822,15 @@ const char * nm_setting_802_1x_get_client_cert_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_client_cert_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->client_cert->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->client_cert, NULL); + return (const char *)data + strlen (SCHEME_PATH); } /** @@ -873,11 +881,7 @@ nm_setting_802_1x_set_client_cert (NMSetting8021x *setting, priv = NM_SETTING_802_1X_GET_PRIVATE (setting); - /* Clear out any previous ca_cert blob */ - if (priv->client_cert) { - g_byte_array_free (priv->client_cert, TRUE); - priv->client_cert = NULL; - } + g_clear_pointer (&priv->client_cert, g_bytes_unref); if (!cert_path) { g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_CLIENT_CERT); @@ -909,14 +913,16 @@ nm_setting_802_1x_set_client_cert (NMSetting8021x *setting, } if (valid) { - if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - priv->client_cert = g_byte_array_ref (data); - else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) + if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { + priv->client_cert = g_byte_array_free_to_bytes (data); + data = NULL; + } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->client_cert = path_to_scheme_value (cert_path); else g_assert_not_reached (); } - g_byte_array_unref (data); + if (data) + g_byte_array_unref (data); } g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_CLIENT_CERT); @@ -1055,7 +1061,7 @@ nm_setting_802_1x_get_phase2_ca_cert_scheme (NMSetting8021x *setting) * * Returns: the "phase 2" CA certificate data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_phase2_ca_cert_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -1085,13 +1091,15 @@ const char * nm_setting_802_1x_get_phase2_ca_cert_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_phase2_ca_cert_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_ca_cert->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_ca_cert, NULL); + return (const char *)data + strlen (SCHEME_PATH); } /** @@ -1138,11 +1146,7 @@ nm_setting_802_1x_set_phase2_ca_cert (NMSetting8021x *setting, priv = NM_SETTING_802_1X_GET_PRIVATE (setting); - /* Clear out any previous ca_cert blob */ - if (priv->phase2_ca_cert) { - g_byte_array_free (priv->phase2_ca_cert, TRUE); - priv->phase2_ca_cert = NULL; - } + g_clear_pointer (&priv->phase2_ca_cert, g_bytes_unref); if (!cert_path) { g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_PHASE2_CA_CERT); @@ -1156,9 +1160,10 @@ nm_setting_802_1x_set_phase2_ca_cert (NMSetting8021x *setting, if (out_format) *out_format = NM_SETTING_802_1X_CK_FORMAT_X509; - if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - priv->phase2_ca_cert = g_byte_array_ref (data); - else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) + if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { + priv->phase2_ca_cert = g_byte_array_free_to_bytes (data); + data = NULL; + } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->phase2_ca_cert = path_to_scheme_value (cert_path); else g_assert_not_reached (); @@ -1169,7 +1174,8 @@ nm_setting_802_1x_set_phase2_ca_cert (NMSetting8021x *setting, _("invalid certificate format")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_PHASE2_CA_CERT); } - g_byte_array_unref (data); + if (data) + g_byte_array_unref (data); } g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_PHASE2_CA_CERT); @@ -1372,7 +1378,7 @@ nm_setting_802_1x_get_phase2_client_cert_scheme (NMSetting8021x *setting) * * Returns: the "phase 2" client certificate data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_phase2_client_cert_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -1399,13 +1405,15 @@ const char * nm_setting_802_1x_get_phase2_client_cert_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_phase2_client_cert_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_client_cert->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_client_cert, NULL); + return (const char *)data + strlen (SCHEME_PATH); } /** @@ -1456,11 +1464,7 @@ nm_setting_802_1x_set_phase2_client_cert (NMSetting8021x *setting, priv = NM_SETTING_802_1X_GET_PRIVATE (setting); - /* Clear out any previous ca_cert blob */ - if (priv->phase2_client_cert) { - g_byte_array_free (priv->phase2_client_cert, TRUE); - priv->phase2_client_cert = NULL; - } + g_clear_pointer (&priv->phase2_client_cert, g_bytes_unref); if (!cert_path) { g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_PHASE2_CLIENT_CERT); @@ -1493,14 +1497,16 @@ nm_setting_802_1x_set_phase2_client_cert (NMSetting8021x *setting, } if (valid) { - if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) - priv->phase2_client_cert = g_byte_array_ref (data); - else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) + if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { + priv->phase2_client_cert = g_byte_array_free_to_bytes (data); + data = NULL; + } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->phase2_client_cert = path_to_scheme_value (cert_path); else g_assert_not_reached (); } - g_byte_array_unref (data); + if (data) + g_byte_array_unref (data); } g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_PHASE2_CLIENT_CERT); @@ -1544,7 +1550,7 @@ nm_setting_802_1x_get_password_flags (NMSetting8021x *setting) * UTF-8-encoded array of bytes, as specified by the * #NMSetting8021x:password-raw property **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_password_raw (NMSetting8021x *setting) { g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); @@ -1631,7 +1637,7 @@ nm_setting_802_1x_get_private_key_scheme (NMSetting8021x *setting) * * Returns: the private key data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_private_key_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -1658,17 +1664,28 @@ const char * nm_setting_802_1x_get_private_key_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_private_key_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->private_key->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->private_key, NULL); + return (const char *)data + strlen (SCHEME_PATH); } -static GByteArray * -file_to_byte_array (const char *filename) +static void +free_secure_bytes (gpointer data) +{ + GByteArray *array = data; + + memset (array->data, 0, array->len); + g_byte_array_unref (array); +} + +static GBytes * +file_to_secure_bytes (const char *filename) { char *contents; GByteArray *array = NULL; @@ -1680,7 +1697,7 @@ file_to_byte_array (const char *filename) g_assert (array->len == length); g_free (contents); } - return array; + return g_bytes_new_with_free_func (array->data, array->len, free_secure_bytes, array); } /** @@ -1767,9 +1784,7 @@ nm_setting_802_1x_set_private_key (NMSetting8021x *setting, /* Clear out any previous private key data */ if (priv->private_key) { - /* Try not to leave the private key around in memory */ - memset (priv->private_key->data, 0, priv->private_key->len); - g_byte_array_free (priv->private_key, TRUE); + g_bytes_unref (priv->private_key); priv->private_key = NULL; key_cleared = TRUE; } @@ -1791,7 +1806,7 @@ nm_setting_802_1x_set_private_key (NMSetting8021x *setting, priv->private_key_password = g_strdup (password); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { /* Shouldn't fail this since we just verified the private key above */ - priv->private_key = file_to_byte_array (key_path); + priv->private_key = file_to_secure_bytes (key_path); g_assert (priv->private_key); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->private_key = path_to_scheme_value (key_path); @@ -1804,10 +1819,8 @@ nm_setting_802_1x_set_private_key (NMSetting8021x *setting, g_assert (format != NM_CRYPTO_FILE_FORMAT_UNKNOWN); if (format == NM_CRYPTO_FILE_FORMAT_PKCS12) { if (priv->client_cert) - g_byte_array_free (priv->client_cert, TRUE); - - priv->client_cert = g_byte_array_sized_new (priv->private_key->len); - g_byte_array_append (priv->client_cert, priv->private_key->data, priv->private_key->len); + g_bytes_unref (priv->client_cert); + priv->client_cert = g_bytes_ref (priv->private_key); g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_CLIENT_CERT); } @@ -1873,7 +1886,8 @@ nm_setting_802_1x_get_private_key_format (NMSetting8021x *setting) switch (nm_setting_802_1x_get_private_key_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - if (crypto_is_pkcs12_data (priv->private_key)) + if (crypto_is_pkcs12_data (g_bytes_get_data (priv->private_key, NULL), + g_bytes_get_size (priv->private_key))) return NM_SETTING_802_1X_CK_FORMAT_PKCS12; return NM_SETTING_802_1X_CK_FORMAT_RAW_KEY; case NM_SETTING_802_1X_CK_SCHEME_PATH: @@ -1958,7 +1972,7 @@ nm_setting_802_1x_get_phase2_private_key_scheme (NMSetting8021x *setting) * * Returns: the "phase 2" private key data **/ -const GByteArray * +GBytes * nm_setting_802_1x_get_phase2_private_key_blob (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; @@ -1985,13 +1999,15 @@ const char * nm_setting_802_1x_get_phase2_private_key_path (NMSetting8021x *setting) { NMSetting8021xCKScheme scheme; + gconstpointer data; g_return_val_if_fail (NM_IS_SETTING_802_1X (setting), NULL); scheme = nm_setting_802_1x_get_phase2_private_key_scheme (setting); g_return_val_if_fail (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH, NULL); - return (const char *) (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_private_key->data + strlen (SCHEME_PATH)); + data = g_bytes_get_data (NM_SETTING_802_1X_GET_PRIVATE (setting)->phase2_private_key, NULL); + return (const char *)data + strlen (SCHEME_PATH); } /** @@ -2078,9 +2094,7 @@ nm_setting_802_1x_set_phase2_private_key (NMSetting8021x *setting, /* Clear out any previous private key data */ if (priv->phase2_private_key) { - /* Try not to leave the private key around in memory */ - memset (priv->phase2_private_key->data, 0, priv->phase2_private_key->len); - g_byte_array_free (priv->phase2_private_key, TRUE); + g_bytes_unref (priv->phase2_private_key); priv->phase2_private_key = NULL; key_cleared = TRUE; } @@ -2102,7 +2116,7 @@ nm_setting_802_1x_set_phase2_private_key (NMSetting8021x *setting, priv->phase2_private_key_password = g_strdup (password); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { /* Shouldn't fail this since we just verified the private key above */ - priv->phase2_private_key = file_to_byte_array (key_path); + priv->phase2_private_key = file_to_secure_bytes (key_path); g_assert (priv->phase2_private_key); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) priv->phase2_private_key = path_to_scheme_value (key_path); @@ -2115,10 +2129,9 @@ nm_setting_802_1x_set_phase2_private_key (NMSetting8021x *setting, g_assert (format != NM_CRYPTO_FILE_FORMAT_UNKNOWN); if (format == NM_CRYPTO_FILE_FORMAT_PKCS12) { if (priv->phase2_client_cert) - g_byte_array_free (priv->phase2_client_cert, TRUE); + g_bytes_unref (priv->phase2_client_cert); - priv->phase2_client_cert = g_byte_array_sized_new (priv->phase2_private_key->len); - g_byte_array_append (priv->phase2_client_cert, priv->phase2_private_key->data, priv->phase2_private_key->len); + priv->phase2_client_cert = g_bytes_ref (priv->phase2_private_key); g_object_notify (G_OBJECT (setting), NM_SETTING_802_1X_PHASE2_CLIENT_CERT); } @@ -2153,7 +2166,8 @@ nm_setting_802_1x_get_phase2_private_key_format (NMSetting8021x *setting) switch (nm_setting_802_1x_get_phase2_private_key_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - if (crypto_is_pkcs12_data (priv->phase2_private_key)) + if (crypto_is_pkcs12_data (g_bytes_get_data (priv->phase2_private_key, NULL), + g_bytes_get_size (priv->phase2_private_key))) return NM_SETTING_802_1X_CK_FORMAT_PKCS12; return NM_SETTING_802_1X_CK_FORMAT_RAW_KEY; case NM_SETTING_802_1X_CK_SCHEME_PATH: @@ -2181,7 +2195,7 @@ need_secrets_password (NMSetting8021x *self, NMSetting8021xPrivate *priv = NM_SETTING_802_1X_GET_PRIVATE (self); if ( (!priv->password || !strlen (priv->password)) - && (!priv->password_raw || !priv->password_raw->len)) { + && (!priv->password_raw || !g_bytes_get_size (priv->password_raw))) { g_ptr_array_add (secrets, NM_SETTING_802_1X_PASSWORD); g_ptr_array_add (secrets, NM_SETTING_802_1X_PASSWORD_RAW); } @@ -2199,7 +2213,7 @@ need_secrets_sim (NMSetting8021x *self, } static gboolean -need_private_key_password (const GByteArray *blob, +need_private_key_password (GBytes *blob, const char *path, const char *password) { @@ -2210,7 +2224,9 @@ need_private_key_password (const GByteArray *blob, if (path) format = crypto_verify_private_key (path, password, NULL); else if (blob) - format = crypto_verify_private_key_data (blob, password, NULL); + format = crypto_verify_private_key_data (g_bytes_get_data (blob, NULL), + g_bytes_get_size (blob), + password, NULL); else g_warning ("%s: unknown private key password scheme", __func__); } @@ -2225,7 +2241,7 @@ need_secrets_tls (NMSetting8021x *self, { NMSetting8021xPrivate *priv = NM_SETTING_802_1X_GET_PRIVATE (self); NMSetting8021xCKScheme scheme; - const GByteArray *blob = NULL; + GBytes *blob = NULL; const char *path = NULL; if (phase2) { @@ -2272,7 +2288,7 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) _("property is missing")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_PHASE2_CLIENT_CERT); return FALSE; - } else if (!priv->phase2_client_cert->len) { + } else if (!g_bytes_get_size (priv->phase2_client_cert)) { g_set_error_literal (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2289,7 +2305,7 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) _("property is missing")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_PHASE2_PRIVATE_KEY); return FALSE; - } else if (!priv->phase2_private_key->len) { + } else if (!g_bytes_get_size (priv->phase2_private_key)) { g_set_error_literal (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2299,20 +2315,9 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) } /* If the private key is PKCS#12, check that it matches the client cert */ - if (crypto_is_pkcs12_data (priv->phase2_private_key)) { - if (priv->phase2_private_key->len != priv->phase2_client_cert->len) { - g_set_error (error, - NM_SETTING_802_1X_ERROR, - NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, - _("has to match '%s' property for PKCS#12"), - NM_SETTING_802_1X_PHASE2_PRIVATE_KEY); - g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_PHASE2_CLIENT_CERT); - return FALSE; - } - - if (memcmp (priv->phase2_private_key->data, - priv->phase2_client_cert->data, - priv->phase2_private_key->len)) { + if (crypto_is_pkcs12_data (g_bytes_get_data (priv->phase2_private_key, NULL), + g_bytes_get_size (priv->phase2_private_key))) { + if (!g_bytes_equal (priv->phase2_private_key, priv->phase2_client_cert)) { g_set_error (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2330,7 +2335,7 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) _("property is missing")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_CLIENT_CERT); return FALSE; - } else if (!priv->client_cert->len) { + } else if (!g_bytes_get_size (priv->client_cert)) { g_set_error_literal (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2347,7 +2352,7 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) _("property is missing")); g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_PRIVATE_KEY); return FALSE; - } else if (!priv->private_key->len) { + } else if (!g_bytes_get_size (priv->private_key)) { g_set_error_literal (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2357,20 +2362,9 @@ verify_tls (NMSetting8021x *self, gboolean phase2, GError **error) } /* If the private key is PKCS#12, check that it matches the client cert */ - if (crypto_is_pkcs12_data (priv->private_key)) { - if (priv->private_key->len != priv->client_cert->len) { - g_set_error (error, - NM_SETTING_802_1X_ERROR, - NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, - _("has to match '%s' property for PKCS#12"), - NM_SETTING_802_1X_PRIVATE_KEY); - g_prefix_error (error, "%s.%s: ", NM_SETTING_802_1X_SETTING_NAME, NM_SETTING_802_1X_CLIENT_CERT); - return FALSE; - } - - if (memcmp (priv->private_key->data, - priv->client_cert->data, - priv->private_key->len)) { + if (crypto_is_pkcs12_data (g_bytes_get_data (priv->private_key, NULL), + g_bytes_get_size (priv->private_key))) { + if (!g_bytes_equal (priv->private_key, priv->client_cert)) { g_set_error (error, NM_SETTING_802_1X_ERROR, NM_SETTING_802_1X_ERROR_INVALID_PROPERTY, @@ -2590,21 +2584,25 @@ need_secrets (NMSetting *setting) } static gboolean -verify_cert (GByteArray *array, const char *prop_name, GError **error) +verify_cert (GBytes *bytes, const char *prop_name, GError **error) { - if (!array) + gconstpointer data; + gsize length; + + if (!bytes) return TRUE; - switch (get_cert_scheme (array)) { + switch (get_cert_scheme (bytes)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: return TRUE; case NM_SETTING_802_1X_CK_SCHEME_PATH: /* For path-based schemes, verify that the path is zero-terminated */ - if (array->data[array->len - 1] == '\0') { + data = g_bytes_get_data (bytes, &length); + if (((const guchar *)data)[length - 1] == '\0') { /* And ensure it's UTF-8 valid too so we can pass it through * D-Bus and stuff like that. */ - if (g_utf8_validate ((const char *) (array->data + strlen (SCHEME_PATH)), -1, NULL)) + if (g_utf8_validate ((const char *)data + strlen (SCHEME_PATH), -1, NULL)) return TRUE; } break; @@ -2766,7 +2764,7 @@ finalize (GObject *object) g_free (priv->phase2_subject_match); g_free (priv->password); if (priv->password_raw) - g_byte_array_free (priv->password_raw, TRUE); + g_bytes_unref (priv->password_raw); g_free (priv->pin); g_slist_free_full (priv->eap, g_free); @@ -2774,39 +2772,37 @@ finalize (GObject *object) g_slist_free_full (priv->phase2_altsubject_matches, g_free); if (priv->ca_cert) - g_byte_array_free (priv->ca_cert, TRUE); + g_bytes_unref (priv->ca_cert); if (priv->client_cert) - g_byte_array_free (priv->client_cert, TRUE); + g_bytes_unref (priv->client_cert); if (priv->private_key) - g_byte_array_free (priv->private_key, TRUE); + g_bytes_unref (priv->private_key); g_free (priv->private_key_password); if (priv->phase2_ca_cert) - g_byte_array_free (priv->phase2_ca_cert, TRUE); + g_bytes_unref (priv->phase2_ca_cert); if (priv->phase2_client_cert) - g_byte_array_free (priv->phase2_client_cert, TRUE); + g_bytes_unref (priv->phase2_client_cert); if (priv->phase2_private_key) - g_byte_array_free (priv->phase2_private_key, TRUE); + g_bytes_unref (priv->phase2_private_key); g_free (priv->phase2_private_key_password); G_OBJECT_CLASS (nm_setting_802_1x_parent_class)->finalize (object); } -static GByteArray * +static GBytes * set_cert_prop_helper (const GValue *value, const char *prop_name, GError **error) { gboolean valid; - GByteArray *data = NULL; + GBytes *bytes = NULL; - data = g_value_dup_boxed (value); + bytes = g_value_dup_boxed (value); /* Verify the new data */ - if (data) { - valid = verify_cert (data, prop_name, error); - if (!valid) { - g_byte_array_free (data, TRUE); - data = NULL; - } + if (bytes) { + valid = verify_cert (bytes, prop_name, error); + if (!valid) + g_clear_pointer (&bytes, g_bytes_unref); } - return data; + return bytes; } static void @@ -2820,7 +2816,7 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_EAP: g_slist_free_full (priv->eap, g_free); - priv->eap = g_value_dup_boxed (value); + priv->eap = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_IDENTITY: g_free (priv->identity); @@ -2835,10 +2831,8 @@ set_property (GObject *object, guint prop_id, priv->pac_file = g_value_dup_string (value); break; case PROP_CA_CERT: - if (priv->ca_cert) { - g_byte_array_free (priv->ca_cert, TRUE); - priv->ca_cert = NULL; - } + if (priv->ca_cert) + g_bytes_unref (priv->ca_cert); priv->ca_cert = set_cert_prop_helper (value, NM_SETTING_802_1X_CA_CERT, &error); if (error) { g_warning ("Error setting certificate (invalid data): (%d) %s", @@ -2856,13 +2850,11 @@ set_property (GObject *object, guint prop_id, break; case PROP_ALTSUBJECT_MATCHES: g_slist_free_full (priv->altsubject_matches, g_free); - priv->altsubject_matches = g_value_dup_boxed (value); + priv->altsubject_matches = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_CLIENT_CERT: - if (priv->client_cert) { - g_byte_array_free (priv->client_cert, TRUE); - priv->client_cert = NULL; - } + if (priv->client_cert) + g_bytes_unref (priv->client_cert); priv->client_cert = set_cert_prop_helper (value, NM_SETTING_802_1X_CLIENT_CERT, &error); if (error) { g_warning ("Error setting certificate (invalid data): (%d) %s", @@ -2891,10 +2883,8 @@ set_property (GObject *object, guint prop_id, priv->phase2_autheap = g_value_dup_string (value); break; case PROP_PHASE2_CA_CERT: - if (priv->phase2_ca_cert) { - g_byte_array_free (priv->phase2_ca_cert, TRUE); - priv->phase2_ca_cert = NULL; - } + if (priv->phase2_ca_cert) + g_bytes_unref (priv->phase2_ca_cert); priv->phase2_ca_cert = set_cert_prop_helper (value, NM_SETTING_802_1X_PHASE2_CA_CERT, &error); if (error) { g_warning ("Error setting certificate (invalid data): (%d) %s", @@ -2912,13 +2902,11 @@ set_property (GObject *object, guint prop_id, break; case PROP_PHASE2_ALTSUBJECT_MATCHES: g_slist_free_full (priv->phase2_altsubject_matches, g_free); - priv->phase2_altsubject_matches = g_value_dup_boxed (value); + priv->phase2_altsubject_matches = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_PHASE2_CLIENT_CERT: - if (priv->phase2_client_cert) { - g_byte_array_free (priv->phase2_client_cert, TRUE); - priv->phase2_client_cert = NULL; - } + if (priv->phase2_client_cert) + g_bytes_unref (priv->phase2_client_cert); priv->phase2_client_cert = set_cert_prop_helper (value, NM_SETTING_802_1X_PHASE2_CLIENT_CERT, &error); if (error) { g_warning ("Error setting certificate (invalid data): (%d) %s", @@ -2935,17 +2923,15 @@ set_property (GObject *object, guint prop_id, break; case PROP_PASSWORD_RAW: if (priv->password_raw) - g_byte_array_free (priv->password_raw, TRUE); + g_bytes_unref (priv->password_raw); priv->password_raw = g_value_dup_boxed (value); break; case PROP_PASSWORD_RAW_FLAGS: priv->password_raw_flags = g_value_get_uint (value); break; case PROP_PRIVATE_KEY: - if (priv->private_key) { - g_byte_array_free (priv->private_key, TRUE); - priv->private_key = NULL; - } + if (priv->private_key) + g_bytes_unref (priv->private_key); priv->private_key = set_cert_prop_helper (value, NM_SETTING_802_1X_PRIVATE_KEY, &error); if (error) { g_warning ("Error setting private key (invalid data): (%d) %s", @@ -2961,10 +2947,8 @@ set_property (GObject *object, guint prop_id, priv->private_key_password_flags = g_value_get_uint (value); break; case PROP_PHASE2_PRIVATE_KEY: - if (priv->phase2_private_key) { - g_byte_array_free (priv->phase2_private_key, TRUE); - priv->phase2_private_key = NULL; - } + if (priv->phase2_private_key) + g_bytes_unref (priv->phase2_private_key); priv->phase2_private_key = set_cert_prop_helper (value, NM_SETTING_802_1X_PHASE2_PRIVATE_KEY, &error); if (error) { g_warning ("Error setting private key (invalid data): (%d) %s", @@ -3004,7 +2988,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_EAP: - g_value_set_boxed (value, priv->eap); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->eap)); break; case PROP_IDENTITY: g_value_set_string (value, priv->identity); @@ -3025,7 +3009,7 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, priv->subject_match); break; case PROP_ALTSUBJECT_MATCHES: - g_value_set_boxed (value, priv->altsubject_matches); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->altsubject_matches)); break; case PROP_CLIENT_CERT: g_value_set_boxed (value, priv->client_cert); @@ -3055,7 +3039,7 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, priv->phase2_subject_match); break; case PROP_PHASE2_ALTSUBJECT_MATCHES: - g_value_set_boxed (value, priv->phase2_altsubject_matches); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->phase2_altsubject_matches)); break; case PROP_PHASE2_CLIENT_CERT: g_value_set_boxed (value, priv->phase2_client_cert); @@ -3135,10 +3119,10 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_EAP, - _nm_param_spec_specialized (NM_SETTING_802_1X_EAP, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_EAP, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSetting8021x:identity: @@ -3199,10 +3183,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_CA_CERT, - _nm_param_spec_specialized (NM_SETTING_802_1X_CA_CERT, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_CA_CERT, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_CA_CERT, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:ca-path: @@ -3241,10 +3229,10 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_ALTSUBJECT_MATCHES, - _nm_param_spec_specialized (NM_SETTING_802_1X_ALTSUBJECT_MATCHES, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_ALTSUBJECT_MATCHES, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSetting8021x:client-cert: @@ -3264,10 +3252,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_CLIENT_CERT, - _nm_param_spec_specialized (NM_SETTING_802_1X_CLIENT_CERT, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_CLIENT_CERT, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_CLIENT_CERT, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:phase1-peapver: @@ -3373,10 +3365,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PHASE2_CA_CERT, - _nm_param_spec_specialized (NM_SETTING_802_1X_PHASE2_CA_CERT, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PHASE2_CA_CERT, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_PHASE2_CA_CERT, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:phase2-ca-path: @@ -3417,10 +3413,10 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PHASE2_ALTSUBJECT_MATCHES, - _nm_param_spec_specialized (NM_SETTING_802_1X_PHASE2_ALTSUBJECT_MATCHES, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PHASE2_ALTSUBJECT_MATCHES, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSetting8021x:phase2-client-cert: @@ -3443,10 +3439,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PHASE2_CLIENT_CERT, - _nm_param_spec_specialized (NM_SETTING_802_1X_PHASE2_CLIENT_CERT, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PHASE2_CLIENT_CERT, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_PHASE2_CLIENT_CERT, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:password: @@ -3487,11 +3487,15 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PASSWORD_RAW, - _nm_param_spec_specialized (NM_SETTING_802_1X_PASSWORD_RAW, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_SECRET | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PASSWORD_RAW, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + NM_SETTING_PARAM_SECRET | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_PASSWORD_RAW, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:password-raw-flags: @@ -3539,10 +3543,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PRIVATE_KEY, - _nm_param_spec_specialized (NM_SETTING_802_1X_PRIVATE_KEY, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PRIVATE_KEY, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_PRIVATE_KEY, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:private-key-password: @@ -3604,10 +3612,14 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) **/ g_object_class_install_property (object_class, PROP_PHASE2_PRIVATE_KEY, - _nm_param_spec_specialized (NM_SETTING_802_1X_PHASE2_PRIVATE_KEY, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_802_1X_PHASE2_PRIVATE_KEY, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_802_1X_PHASE2_PRIVATE_KEY, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSetting8021x:phase2-private-key-password: diff --git a/libnm-core/nm-setting-8021x.h b/libnm-core/nm-setting-8021x.h index 6901b79d69..d855df8584 100644 --- a/libnm-core/nm-setting-8021x.h +++ b/libnm-core/nm-setting-8021x.h @@ -182,7 +182,7 @@ const char * nm_setting_802_1x_get_ca_path (NMSetting8 const char * nm_setting_802_1x_get_phase2_ca_path (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_ca_cert_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_ca_cert_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_ca_cert_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_ca_cert_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_ca_cert (NMSetting8021x *setting, const char *cert_path, @@ -204,7 +204,7 @@ gboolean nm_setting_802_1x_remove_altsubject_match_by_value (NMSetting8 void nm_setting_802_1x_clear_altsubject_matches (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_client_cert_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_client_cert_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_client_cert_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_client_cert_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_client_cert (NMSetting8021x *setting, const char *cert_path, @@ -223,7 +223,7 @@ const char * nm_setting_802_1x_get_phase2_auth (NMSetting8 const char * nm_setting_802_1x_get_phase2_autheap (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_phase2_ca_cert_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_phase2_ca_cert_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_phase2_ca_cert_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_phase2_ca_cert_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_phase2_ca_cert (NMSetting8021x *setting, const char *cert_path, @@ -245,7 +245,7 @@ gboolean nm_setting_802_1x_remove_phase2_altsubject_match_by_value (NMS void nm_setting_802_1x_clear_phase2_altsubject_matches (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_phase2_client_cert_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_phase2_client_cert_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_phase2_client_cert_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_phase2_client_cert_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_phase2_client_cert (NMSetting8021x *setting, const char *cert_path, @@ -255,14 +255,14 @@ gboolean nm_setting_802_1x_set_phase2_client_cert (NMSett const char * nm_setting_802_1x_get_password (NMSetting8021x *setting); NMSettingSecretFlags nm_setting_802_1x_get_password_flags (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_password_raw (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_password_raw (NMSetting8021x *setting); NMSettingSecretFlags nm_setting_802_1x_get_password_raw_flags (NMSetting8021x *setting); const char * nm_setting_802_1x_get_pin (NMSetting8021x *setting); NMSettingSecretFlags nm_setting_802_1x_get_pin_flags (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_private_key_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_private_key_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_private_key_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_private_key_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_private_key (NMSetting8021x *setting, const char *key_path, @@ -276,7 +276,7 @@ NMSettingSecretFlags nm_setting_802_1x_get_private_key_password_flags (NMSett NMSetting8021xCKFormat nm_setting_802_1x_get_private_key_format (NMSetting8021x *setting); NMSetting8021xCKScheme nm_setting_802_1x_get_phase2_private_key_scheme (NMSetting8021x *setting); -const GByteArray * nm_setting_802_1x_get_phase2_private_key_blob (NMSetting8021x *setting); +GBytes * nm_setting_802_1x_get_phase2_private_key_blob (NMSetting8021x *setting); const char * nm_setting_802_1x_get_phase2_private_key_path (NMSetting8021x *setting); gboolean nm_setting_802_1x_set_phase2_private_key (NMSetting8021x *setting, const char *key_path, diff --git a/libnm-core/nm-setting-bluetooth.c b/libnm-core/nm-setting-bluetooth.c index 19d533d2b9..44e71e06da 100644 --- a/libnm-core/nm-setting-bluetooth.c +++ b/libnm-core/nm-setting-bluetooth.c @@ -24,12 +24,13 @@ #include #include -#include "nm-param-spec-specialized.h" #include "nm-dbus-glib-types.h" #include "nm-setting-bluetooth.h" #include "nm-setting-cdma.h" #include "nm-setting-gsm.h" #include "nm-setting-private.h" +#include "nm-utils.h" +#include "nm-utils-private.h" /** * SECTION:nm-setting-bluetooth @@ -66,7 +67,7 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BLUETOOTH) #define NM_SETTING_BLUETOOTH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BLUETOOTH, NMSettingBluetoothPrivate)) typedef struct { - GByteArray *bdaddr; + char *bdaddr; char *type; } NMSettingBluetoothPrivate; @@ -116,7 +117,7 @@ nm_setting_bluetooth_get_connection_type (NMSettingBluetooth *setting) * * Returns: the Bluetooth address **/ -const GByteArray * +const char * nm_setting_bluetooth_get_bdaddr (NMSettingBluetooth *setting) { g_return_val_if_fail (NM_IS_SETTING_BLUETOOTH (setting), NULL); @@ -138,7 +139,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->bdaddr && priv->bdaddr->len != ETH_ALEN) { + if (!nm_utils_hwaddr_valid (priv->bdaddr, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_BLUETOOTH_ERROR, NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY, @@ -200,8 +201,7 @@ finalize (GObject *object) { NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (object); - if (priv->bdaddr) - g_byte_array_free (priv->bdaddr, TRUE); + g_free (priv->bdaddr); g_free (priv->type); G_OBJECT_CLASS (nm_setting_bluetooth_parent_class)->finalize (object); @@ -215,9 +215,8 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_BDADDR: - if (priv->bdaddr) - g_byte_array_free (priv->bdaddr, TRUE); - priv->bdaddr = g_value_dup_boxed (value); + g_free (priv->bdaddr); + priv->bdaddr = g_value_dup_string (value); break; case PROP_TYPE: g_free (priv->type); @@ -237,7 +236,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_BDADDR: - g_value_set_boxed (value, nm_setting_bluetooth_get_bdaddr (setting)); + g_value_set_string (value, nm_setting_bluetooth_get_bdaddr (setting)); break; case PROP_TYPE: g_value_set_string (value, nm_setting_bluetooth_get_connection_type (setting)); @@ -271,11 +270,15 @@ nm_setting_bluetooth_class_init (NMSettingBluetoothClass *setting_class) **/ g_object_class_install_property (object_class, PROP_BDADDR, - _nm_param_spec_specialized (NM_SETTING_BLUETOOTH_BDADDR, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_BLUETOOTH_BDADDR, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_BLUETOOTH_BDADDR, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingBluetooth:type: diff --git a/libnm-core/nm-setting-bluetooth.h b/libnm-core/nm-setting-bluetooth.h index eaf97f0fef..d030f9760c 100644 --- a/libnm-core/nm-setting-bluetooth.h +++ b/libnm-core/nm-setting-bluetooth.h @@ -93,7 +93,7 @@ typedef struct { GType nm_setting_bluetooth_get_type (void); NMSetting * nm_setting_bluetooth_new (void); -const GByteArray *nm_setting_bluetooth_get_bdaddr (NMSettingBluetooth *setting); +const char * nm_setting_bluetooth_get_bdaddr (NMSettingBluetooth *setting); const char * nm_setting_bluetooth_get_connection_type (NMSettingBluetooth *setting); G_END_DECLS diff --git a/libnm-core/nm-setting-bond.c b/libnm-core/nm-setting-bond.c index 999b9b0d7b..22fa19f3e4 100644 --- a/libnm-core/nm-setting-bond.c +++ b/libnm-core/nm-setting-bond.c @@ -28,7 +28,6 @@ #include #include "nm-setting-bond.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-utils-private.h" #include "nm-dbus-glib-types.h" @@ -670,26 +669,16 @@ finalize (GObject *object) G_OBJECT_CLASS (nm_setting_bond_parent_class)->finalize (object); } -static void -copy_hash (gpointer key, gpointer value, gpointer user_data) -{ - g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value)); -} - static void set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object); - GHashTable *new_hash; switch (prop_id) { case PROP_OPTIONS: - /* Must make a deep copy of the hash table here... */ - g_hash_table_remove_all (priv->options); - new_hash = g_value_get_boxed (value); - if (new_hash) - g_hash_table_foreach (new_hash, copy_hash, priv->options); + g_hash_table_unref (priv->options); + priv->options = _nm_utils_copy_strdict (g_value_get_boxed (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -705,7 +694,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_OPTIONS: - g_value_set_boxed (value, priv->options); + g_value_take_boxed (value, _nm_utils_copy_strdict (priv->options)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -734,14 +723,20 @@ nm_setting_bond_class_init (NMSettingBondClass *setting_class) * Dictionary of key/value pairs of bonding options. Both keys and values * must be strings. Option names must contain only alphanumeric characters * (ie, [a-zA-Z0-9]). + * + * Type: GHashTable(utf8,utf8) **/ g_object_class_install_property (object_class, PROP_OPTIONS, - _nm_param_spec_specialized (NM_SETTING_BOND_OPTIONS, "", "", - DBUS_TYPE_G_MAP_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_BOND_OPTIONS, "", "", + G_TYPE_HASH_TABLE, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_BOND_OPTIONS, + DBUS_TYPE_G_MAP_OF_STRING, + _nm_utils_strdict_to_dbus, + _nm_utils_strdict_from_dbus); _nm_setting_class_add_dbus_only_property (parent_class, "interface-name", G_TYPE_STRING, _nm_setting_get_deprecated_virtual_interface_name, diff --git a/libnm-core/nm-setting-bridge.c b/libnm-core/nm-setting-bridge.c index 60d6f516c3..cd682a49d8 100644 --- a/libnm-core/nm-setting-bridge.c +++ b/libnm-core/nm-setting-bridge.c @@ -26,11 +26,9 @@ #include #include "nm-setting-bridge.h" -#include "nm-param-spec-specialized.h" #include "nm-setting-private.h" #include "nm-utils.h" #include "nm-utils-private.h" -#include "nm-dbus-glib-types.h" /** * SECTION:nm-setting-bridge @@ -65,7 +63,7 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BRIDGE) #define NM_SETTING_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE, NMSettingBridgePrivate)) typedef struct { - GByteArray *mac_address; + char * mac_address; gboolean stp; guint16 priority; guint16 forward_delay; @@ -105,7 +103,7 @@ nm_setting_bridge_new (void) * * Returns: the #NMSettingBridge:mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_bridge_get_mac_address (NMSettingBridge *setting) { g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), NULL); @@ -235,7 +233,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) { NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting); - if (priv->mac_address && priv->mac_address->len != ETH_ALEN) { + if (priv->mac_address && !nm_utils_hwaddr_valid (priv->mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_BRIDGE_ERROR, NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY, @@ -285,8 +283,7 @@ finalize (GObject *object) { NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object); - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); + g_free (priv->mac_address); G_OBJECT_CLASS (nm_setting_bridge_parent_class)->finalize (object); } @@ -299,9 +296,8 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_MAC_ADDRESS: - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); - priv->mac_address = g_value_dup_boxed (value); + g_free (priv->mac_address); + priv->mac_address = g_value_dup_string (value); break; case PROP_STP: priv->stp = g_value_get_boolean (value); @@ -336,7 +332,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_bridge_get_mac_address (setting)); + g_value_set_string (value, nm_setting_bridge_get_mac_address (setting)); break; case PROP_STP: g_value_set_boolean (value, priv->stp); @@ -386,11 +382,15 @@ nm_setting_bridge_class_init (NMSettingBridgeClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_BRIDGE_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_BRIDGE_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_BRIDGE_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingBridge:stp: diff --git a/libnm-core/nm-setting-bridge.h b/libnm-core/nm-setting-bridge.h index 80cfe995c5..6fca58a3a2 100644 --- a/libnm-core/nm-setting-bridge.h +++ b/libnm-core/nm-setting-bridge.h @@ -78,7 +78,7 @@ GType nm_setting_bridge_get_type (void); NMSetting * nm_setting_bridge_new (void); -const GByteArray *nm_setting_bridge_get_mac_address (NMSettingBridge *setting); +const char * nm_setting_bridge_get_mac_address (NMSettingBridge *setting); gboolean nm_setting_bridge_get_stp (NMSettingBridge *setting); diff --git a/libnm-core/nm-setting-connection.c b/libnm-core/nm-setting-connection.c index d3a80f2425..9fea63b783 100644 --- a/libnm-core/nm-setting-connection.c +++ b/libnm-core/nm-setting-connection.c @@ -24,8 +24,7 @@ #include #include "nm-utils.h" -#include "nm-dbus-glib-types.h" -#include "nm-param-spec-specialized.h" +#include "nm-utils-private.h" #include "nm-setting-connection.h" #include "nm-setting-private.h" @@ -1034,14 +1033,15 @@ finalize (GObject *object) } static GSList * -perm_stringlist_to_permlist (GSList *strlist) +perm_strv_to_permlist (char **strv) { - GSList *list = NULL, *iter; + GSList *list = NULL; + int i; - for (iter = strlist; iter; iter = g_slist_next (iter)) { + for (i = 0; strv[i]; i++) { Permission *p; - p = permission_new_from_str ((const char *) iter->data); + p = permission_new_from_str (strv[i]); if (p) list = g_slist_append (list, p); } @@ -1074,7 +1074,7 @@ set_property (GObject *object, guint prop_id, break; case PROP_PERMISSIONS: g_slist_free_full (priv->permissions, (GDestroyNotify) permission_free); - priv->permissions = perm_stringlist_to_permlist (g_value_get_boxed (value)); + priv->permissions = perm_strv_to_permlist (g_value_get_boxed (value)); break; case PROP_AUTOCONNECT: priv->autoconnect = g_value_get_boolean (value); @@ -1099,7 +1099,7 @@ set_property (GObject *object, guint prop_id, break; case PROP_SECONDARIES: g_slist_free_full (priv->secondaries, g_free); - priv->secondaries = g_value_dup_boxed (value); + priv->secondaries = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_GATEWAY_PING_TIMEOUT: priv->gateway_ping_timeout = g_value_get_uint (value); @@ -1110,14 +1110,18 @@ set_property (GObject *object, guint prop_id, } } -static GSList * -perm_permlist_to_stringlist (GSList *permlist) +static char ** +perm_permlist_to_strv (GSList *permlist) { - GSList *list = NULL, *iter; + GPtrArray *strings; + GSList *iter; + strings = g_ptr_array_new (); for (iter = permlist; iter; iter = g_slist_next (iter)) - list = g_slist_append (list, permission_to_string ((Permission *) iter->data)); - return list; + g_ptr_array_add (strings, permission_to_string ((Permission *) iter->data)); + g_ptr_array_add (strings, NULL); + + return (char **) g_ptr_array_free (strings, FALSE); } static void @@ -1141,7 +1145,7 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, nm_setting_connection_get_connection_type (setting)); break; case PROP_PERMISSIONS: - g_value_take_boxed (value, perm_permlist_to_stringlist (priv->permissions)); + g_value_take_boxed (value, perm_permlist_to_strv (priv->permissions)); break; case PROP_AUTOCONNECT: g_value_set_boolean (value, nm_setting_connection_get_autoconnect (setting)); @@ -1162,7 +1166,7 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, nm_setting_connection_get_slave_type (setting)); break; case PROP_SECONDARIES: - g_value_set_boxed (value, priv->secondaries); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->secondaries)); break; case PROP_GATEWAY_PING_TIMEOUT: g_value_set_uint (value, priv->gateway_ping_timeout); @@ -1289,10 +1293,10 @@ nm_setting_connection_class_init (NMSettingConnectionClass *setting_class) */ g_object_class_install_property (object_class, PROP_PERMISSIONS, - _nm_param_spec_specialized (NM_SETTING_CONNECTION_PERMISSIONS, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_CONNECTION_PERMISSIONS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingConnection:autoconnect: @@ -1403,11 +1407,11 @@ nm_setting_connection_class_init (NMSettingConnectionClass *setting_class) **/ g_object_class_install_property (object_class, PROP_SECONDARIES, - _nm_param_spec_specialized (NM_SETTING_CONNECTION_SECONDARIES, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_FUZZY_IGNORE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_CONNECTION_SECONDARIES, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_FUZZY_IGNORE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingConnection:gateway-ping-timeout: diff --git a/libnm-core/nm-setting-dcb.c b/libnm-core/nm-setting-dcb.c index 0861d53996..1c8bf03a22 100644 --- a/libnm-core/nm-setting-dcb.c +++ b/libnm-core/nm-setting-dcb.c @@ -24,7 +24,6 @@ #include #include "nm-setting-dcb.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-utils-private.h" #include "nm-dbus-glib-types.h" @@ -750,6 +749,8 @@ nm_setting_dcb_init (NMSettingDcb *setting) { } +G_STATIC_ASSERT (sizeof (guint) == sizeof (gboolean)); + static inline void set_uint_array (const GValue *v, uint *a, size_t len) { @@ -765,6 +766,35 @@ set_uint_array (const GValue *v, uint *a, size_t len) } #define SET_UINT_ARRAY(v, a) set_uint_array (v, a, G_N_ELEMENTS (a)) +static inline void +take_uint_array (GValue *v, uint *a, size_t len) +{ + GArray *dst = g_array_sized_new (FALSE, TRUE, sizeof (guint), len); + + g_array_append_vals (dst, a, len); + g_value_take_boxed (v, dst); +} + +#define TAKE_UINT_ARRAY(v, a) take_uint_array (v, a, G_N_ELEMENTS (a)) + +static void +_nm_setting_dcb_uint_array_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GArray *src = g_value_get_boxed (prop_value); + + take_uint_array (dbus_value, (guint *) src->data, src->len); +} + +static void +_nm_setting_dcb_uint_array_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GArray *src = g_value_get_boxed (dbus_value); + + set_uint_array (prop_value, (guint *) src->data, src->len); +} + static void set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) @@ -823,14 +853,6 @@ set_property (GObject *object, guint prop_id, } } -#define TAKE_UINT_ARRAY(v, a) \ -{ \ - guint len = G_N_ELEMENTS (a); \ - GArray *dst = g_array_sized_new (FALSE, TRUE, sizeof (guint), len); \ - g_array_append_vals (dst, (a), len); \ - g_value_take_boxed (v, dst); \ -} - static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) @@ -1022,17 +1044,22 @@ nm_setting_dcb_class_init (NMSettingDcbClass *setting_class) /** * NMSettingDcb:priority-flow-control: * - * An array of 8 uint values, where the array index corresponds to the User + * An array of 8 boolean values, where the array index corresponds to the User * Priority (0 - 7) and the value indicates whether or not the corresponding - * priority should transmit priority pause. Allowed values are 0 (do not - * transmit pause) and 1 (transmit pause). + * priority should transmit priority pause. + * + * Element-type: gboolean **/ g_object_class_install_property (object_class, PROP_PFC, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_FLOW_CONTROL, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_FLOW_CONTROL, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_FLOW_CONTROL, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); /** * NMSettingDcb:priority-group-flags: @@ -1054,13 +1081,19 @@ nm_setting_dcb_class_init (NMSettingDcbClass *setting_class) * An array of 8 uint values, where the array index corresponds to the User * Priority (0 - 7) and the value indicates the Priority Group ID. Allowed * Priority Group ID values are 0 - 7 or 15 for the unrestricted group. + * + * Element-type: guint **/ g_object_class_install_property (object_class, PROP_PRIORITY_GROUP_ID, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_GROUP_ID, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_GROUP_ID, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_GROUP_ID, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); /** * NMSettingDcb:priority-group-bandwidth: @@ -1069,13 +1102,19 @@ nm_setting_dcb_class_init (NMSettingDcbClass *setting_class) * Priority Group ID (0 - 7) and the value indicates the percentage of link * bandwidth allocated to that group. Allowed values are 0 - 100, and the * sum of all values must total 100 percent. + * + * Element-type: guint **/ g_object_class_install_property (object_class, PROP_PRIORITY_GROUP_BANDWIDTH, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_GROUP_BANDWIDTH, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_GROUP_BANDWIDTH, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_GROUP_BANDWIDTH, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); /** * NMSettingDcb:priority-bandwidth: @@ -1085,29 +1124,39 @@ nm_setting_dcb_class_init (NMSettingDcbClass *setting_class) * the priority's assigned group that the priority may use. The sum of all * percentages for priorities which belong to the same group must total 100 * percent. + * + * Element-type: guint **/ g_object_class_install_property (object_class, PROP_PRIORITY_BANDWIDTH, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_BANDWIDTH, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_BANDWIDTH, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_BANDWIDTH, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); /** * NMSettingDcb:priority-strict-bandwidth: * - * An array of 8 uint values, where the array index corresponds to the User + * An array of 8 boolean values, where the array index corresponds to the User * Priority (0 - 7) and the value indicates whether or not the priority may - * use all of the bandwidth allocated to its assigned group. Allowed values - * are 0 (the priority may not utilize all bandwidth) or 1 (the priority may - * utilize all bandwidth). + * use all of the bandwidth allocated to its assigned group. + * + * Element-type: gboolean **/ g_object_class_install_property (object_class, PROP_PRIORITY_STRICT, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_STRICT_BANDWIDTH, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_STRICT_BANDWIDTH, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_STRICT_BANDWIDTH, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); /** * NMSettingDcb:priority-traffic-class: @@ -1115,11 +1164,17 @@ nm_setting_dcb_class_init (NMSettingDcbClass *setting_class) * An array of 8 uint values, where the array index corresponds to the User * Priority (0 - 7) and the value indicates the traffic class (0 - 7) to * which the priority is mapped. + * + * Element-type: guint **/ g_object_class_install_property (object_class, PROP_PRIORITY_TRAFFIC_CLASS, - _nm_param_spec_specialized (NM_SETTING_DCB_PRIORITY_TRAFFIC_CLASS, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_DCB_PRIORITY_TRAFFIC_CLASS, "", "", + G_TYPE_ARRAY, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_DCB_PRIORITY_TRAFFIC_CLASS, + DBUS_TYPE_G_UINT_ARRAY, + _nm_setting_dcb_uint_array_to_dbus, + _nm_setting_dcb_uint_array_from_dbus); } diff --git a/libnm-core/nm-setting-infiniband.c b/libnm-core/nm-setting-infiniband.c index e143fdaa58..a5bbf92664 100644 --- a/libnm-core/nm-setting-infiniband.c +++ b/libnm-core/nm-setting-infiniband.c @@ -24,7 +24,6 @@ #include #include "nm-setting-infiniband.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-utils-private.h" #include "nm-setting-private.h" @@ -62,7 +61,7 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_INFINIBAND) #define NM_SETTING_INFINIBAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_INFINIBAND, NMSettingInfinibandPrivate)) typedef struct { - GByteArray *mac_address; + char *mac_address; char *transport_mode; guint32 mtu; int p_key; @@ -99,7 +98,7 @@ nm_setting_infiniband_new (void) * * Returns: the #NMSettingInfiniband:mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_infiniband_get_mac_address (NMSettingInfiniband *setting) { g_return_val_if_fail (NM_IS_SETTING_INFINIBAND (setting), NULL); @@ -203,7 +202,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (setting); guint32 normerr_max_mtu = 0; - if (priv->mac_address && priv->mac_address->len != INFINIBAND_ALEN) { + if (priv->mac_address && !nm_utils_hwaddr_valid (priv->mac_address, INFINIBAND_ALEN)) { g_set_error_literal (error, NM_SETTING_INFINIBAND_ERROR, NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY, @@ -321,8 +320,7 @@ finalize (GObject *object) NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE (object); g_free (priv->transport_mode); - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); + g_free (priv->mac_address); g_free (priv->parent); g_free (priv->virtual_iface_name); @@ -337,9 +335,8 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_MAC_ADDRESS: - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); - priv->mac_address = g_value_dup_boxed (value); + g_free (priv->mac_address); + priv->mac_address = g_value_dup_string (value); break; case PROP_MTU: priv->mtu = g_value_get_uint (value); @@ -371,7 +368,7 @@ get_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_infiniband_get_mac_address (setting)); + g_value_set_string (value, nm_setting_infiniband_get_mac_address (setting)); break; case PROP_MTU: g_value_set_uint (value, nm_setting_infiniband_get_mtu (setting)); @@ -416,11 +413,15 @@ nm_setting_infiniband_class_init (NMSettingInfinibandClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_INFINIBAND_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_INFINIBAND_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_INFINIBAND_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingInfiniband:mtu: diff --git a/libnm-core/nm-setting-infiniband.h b/libnm-core/nm-setting-infiniband.h index 10899de257..2b8aabd106 100644 --- a/libnm-core/nm-setting-infiniband.h +++ b/libnm-core/nm-setting-infiniband.h @@ -75,7 +75,7 @@ typedef struct { GType nm_setting_infiniband_get_type (void); NMSetting * nm_setting_infiniband_new (void); -const GByteArray *nm_setting_infiniband_get_mac_address (NMSettingInfiniband *setting); +const char * nm_setting_infiniband_get_mac_address (NMSettingInfiniband *setting); guint32 nm_setting_infiniband_get_mtu (NMSettingInfiniband *setting); const char * nm_setting_infiniband_get_transport_mode (NMSettingInfiniband *setting); int nm_setting_infiniband_get_p_key (NMSettingInfiniband *setting); diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 4aac500090..ef31832dc2 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -25,13 +25,12 @@ #include #include "nm-setting-ip4-config.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" #include "nm-setting-private.h" #include "nm-core-internal.h" - +#include "nm-utils-private.h" /** * SECTION:nm-setting-ip4-config @@ -69,7 +68,7 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG) typedef struct { char *method; - GArray *dns; /* array of guint32; elements in network byte order */ + GSList *dns; /* list of IP address strings */ GSList *dns_search; /* list of strings */ GSList *addresses; /* array of NMIP4Address */ GSList *address_labels; /* list of strings */ @@ -140,7 +139,7 @@ nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting) { g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns->len; + return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns); } /** @@ -148,26 +147,36 @@ nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting) * @setting: the #NMSettingIP4Config * @i: index number of the DNS server to return * - * Returns: the IPv4 address (network byte order) of the DNS server at index - * @i + * Returns: the IPv4 address of the DNS server at index @i **/ -guint32 +const char * nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); + g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= priv->dns->len, 0); + g_return_val_if_fail (i < g_slist_length (priv->dns), NULL); - return g_array_index (priv->dns, guint32, i); + return (const char *) g_slist_nth_data (priv->dns, i); +} + +static const char * +canonicalize_ip (const char *ip) +{ + in_addr_t addr; + int ret; + + ret = inet_pton (AF_INET, ip, &addr); + g_return_val_if_fail (ret == 1, NULL); + return nm_utils_inet4_ntop (addr, NULL); } /** * nm_setting_ip4_config_add_dns: * @setting: the #NMSettingIP4Config - * @dns: the IPv4 address (network byte order) of the DNS server to add + * @dns: the IPv4 address of the DNS server to add * * Adds a new DNS server to the setting. * @@ -175,20 +184,27 @@ nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i) * known **/ gboolean -nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, guint32 dns) +nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, const char *dns) { NMSettingIP4ConfigPrivate *priv; - int i; + const char *dns_canonical; + GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (dns[0] != '\0', FALSE); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (i = 0; i < priv->dns->len; i++) { - if (dns == g_array_index (priv->dns, guint32, i)) + + dns_canonical = canonicalize_ip (dns); + g_return_val_if_fail (dns_canonical != NULL, FALSE); + + for (iter = priv->dns; iter; iter = g_slist_next (iter)) { + if (!strcmp (dns_canonical, (char *) iter->data)) return FALSE; } - g_array_append_val (priv->dns, dns); + priv->dns = g_slist_append (priv->dns, g_strdup (dns_canonical)); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); return TRUE; } @@ -204,13 +220,16 @@ void nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; + GSList *elt; g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_if_fail (i <= priv->dns->len); + elt = g_slist_nth (priv->dns, i); + g_return_if_fail (elt != NULL); - g_array_remove_index (priv->dns, i); + g_free (elt->data); + priv->dns = g_slist_delete_link (priv->dns, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); } @@ -222,20 +241,26 @@ nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i) * Removes the DNS server @dns. * * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not. - * domain was already known **/ gboolean -nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, guint32 dns) +nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, const char *dns) { NMSettingIP4ConfigPrivate *priv; - int i; + const char *dns_canonical; + GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (dns[0] != '\0', FALSE); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (i = 0; i < priv->dns->len; i++) { - if (dns == g_array_index (priv->dns, guint32, i)) { - g_array_remove_index (priv->dns, i); + + dns_canonical = canonicalize_ip (dns); + g_return_val_if_fail (dns_canonical != NULL, FALSE); + + for (iter = priv->dns; iter; iter = g_slist_next (iter)) { + if (!strcmp (dns_canonical, (char *) iter->data)) { + priv->dns = g_slist_delete_link (priv->dns, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); return TRUE; } @@ -252,12 +277,10 @@ nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, guint32 void nm_setting_ip4_config_clear_dns (NMSettingIP4Config *setting) { - NMSettingIP4ConfigPrivate *priv; - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_array_remove_range (priv->dns, 0, priv->dns->len); + g_slist_free_full (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns, g_free); + NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); } @@ -290,7 +313,7 @@ nm_setting_ip4_config_get_dns_search (NMSettingIP4Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->dns_search), NULL); + g_return_val_if_fail (i < g_slist_length (priv->dns_search), NULL); return (const char *) g_slist_nth_data (priv->dns_search, i); } @@ -427,7 +450,7 @@ nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->addresses), NULL); + g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); return (NMIP4Address *) g_slist_nth_data (priv->addresses, i); } @@ -440,7 +463,7 @@ _nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->address_labels), NULL); + g_return_val_if_fail (i < g_slist_length (priv->address_labels), NULL); return (const char *) g_slist_nth_data (priv->address_labels, i); } @@ -460,7 +483,7 @@ gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIP4Address *address) { - return _nm_setting_ip4_config_add_address_with_label (setting, address, NULL); + return _nm_setting_ip4_config_add_address_with_label (setting, address, ""); } gboolean @@ -474,6 +497,7 @@ _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); g_return_val_if_fail (address != NULL, FALSE); + g_return_val_if_fail (label != NULL, FALSE); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { @@ -511,8 +535,7 @@ nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) nm_ip4_address_unref ((NMIP4Address *) addr->data); priv->addresses = g_slist_delete_link (priv->addresses, addr); - if (label->data) - g_free (label->data); + g_free (label->data); priv->address_labels = g_slist_delete_link (priv->address_labels, label); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); @@ -598,7 +621,7 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->routes), NULL); + g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); return (NMIP4Route *) g_slist_nth_data (priv->routes, i); } @@ -841,6 +864,9 @@ verify_label (const char *label) const char *p; char *iface; + if (!*label) + return TRUE; + p = strchr (label, ':'); if (!p) return FALSE; @@ -888,7 +914,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } else if ( !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) { - if (priv->dns && priv->dns->len) { + if (priv->dns) { g_set_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -898,7 +924,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (g_slist_length (priv->dns_search)) { + if (priv->dns_search) { g_set_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -910,7 +936,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) /* Shared allows IP addresses; link-local and disabled do not */ if (strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0) { - if (g_slist_length (priv->addresses)) { + if (priv->addresses) { g_set_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -977,7 +1003,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (label && !verify_label (label)) { + if (!verify_label (label)) { g_set_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY, @@ -1025,6 +1051,22 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } } + /* Validate DNS */ + for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { + const char *dns = (const char *) iter->data; + in_addr_t addr; + + if (inet_pton (AF_INET, dns, &addr) != 1) { + g_set_error (error, + NM_SETTING_IP4_CONFIG_ERROR, + NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY, + _("%d. DNS server address is invalid"), + i+1); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); + return FALSE; + } + } + return TRUE; } @@ -1032,10 +1074,6 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) static void nm_setting_ip4_config_init (NMSettingIP4Config *setting) { - NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - - - priv->dns = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3); } static void @@ -1048,8 +1086,7 @@ finalize (GObject *object) g_free (priv->dhcp_hostname); g_free (priv->dhcp_client_id); - g_array_free (priv->dns, TRUE); - + g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); g_slist_free_full (priv->address_labels, g_free); @@ -1072,40 +1109,33 @@ set_property (GObject *object, guint prop_id, priv->method = g_value_dup_string (value); break; case PROP_DNS: - g_array_free (priv->dns, TRUE); - priv->dns = g_value_dup_boxed (value); - if (!priv->dns) - priv->dns = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3); + g_slist_free_full (priv->dns, g_free); + priv->dns = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_DNS_SEARCH: g_slist_free_full (priv->dns_search, g_free); - priv->dns_search = g_value_dup_boxed (value); + priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); - priv->addresses = nm_utils_ip4_addresses_from_gvalue (value); + priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip4_address_dup); if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { g_slist_free_full (priv->address_labels, g_free); priv->address_labels = NULL; for (iter = priv->addresses; iter; iter = iter->next) - priv->address_labels = g_slist_prepend (priv->address_labels, NULL); + priv->address_labels = g_slist_prepend (priv->address_labels, g_strdup ("")); } break; case PROP_ADDRESS_LABELS: g_slist_free_full (priv->address_labels, g_free); - priv->address_labels = g_value_dup_boxed (value); - /* NULLs get converted to "" when this is sent over D-Bus. */ - for (iter = priv->address_labels; iter; iter = iter->next) { - if (!g_strcmp0 (iter->data, "")) { - g_free (iter->data); - iter->data = NULL; - } - } + priv->address_labels = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ROUTES: g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); - priv->routes = nm_utils_ip4_routes_from_gvalue (value); + priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip4_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -1148,19 +1178,19 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, nm_setting_ip4_config_get_method (setting)); break; case PROP_DNS: - g_value_set_boxed (value, priv->dns); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns)); break; case PROP_DNS_SEARCH: - g_value_set_boxed (value, priv->dns_search); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - nm_utils_ip4_addresses_to_gvalue (priv->addresses, value); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip4_address_dup, (GDestroyNotify) nm_ip4_address_unref)); break; case PROP_ADDRESS_LABELS: - g_value_set_boxed (value, priv->address_labels); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels)); break; case PROP_ROUTES: - nm_utils_ip4_routes_to_gvalue (priv->routes, value); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip4_route_dup, (GDestroyNotify) nm_ip4_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); @@ -1201,7 +1231,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) object_class->set_property = set_property; object_class->get_property = get_property; object_class->finalize = finalize; - parent_class->verify = verify; + parent_class->verify = verify; /* Properties */ /** @@ -1231,7 +1261,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) /** * NMSettingIP4Config:dns: * - * List of DNS servers (network byte order). For the "auto" method, these + * Array of IPv4 addresses of DNS servers. For the 'auto' method, these * DNS servers are appended to those (if any) returned by automatic * configuration. DNS servers cannot be used with the "shared", * "link-local", or "disabled" methods as there is no upstream network. In @@ -1240,10 +1270,14 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) **/ g_object_class_install_property (object_class, PROP_DNS, - _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_DNS, "", "", - DBUS_TYPE_G_UINT_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP4_CONFIG_DNS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_DNS, + DBUS_TYPE_G_UINT_ARRAY, + _nm_utils_ip4_dns_to_dbus, + _nm_utils_ip4_dns_from_dbus); /** * NMSettingIP4Config:dns-search: @@ -1256,30 +1290,33 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) **/ g_object_class_install_property (object_class, PROP_DNS_SEARCH, - _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_DNS_SEARCH, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP4_CONFIG_DNS_SEARCH, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingIP4Config:addresses: * - * Array of IPv4 address structures. Each IPv4 address structure is - * composed of 3 32-bit values; the first being the IPv4 address (network - * byte order), the second the prefix (1 - 32), and last the IPv4 gateway - * (network byte order). The gateway may be left as 0 if no gateway exists - * for that subnet. For the "auto" method, given IP addresses are appended + * Array of IPv4 addresses. The gateway may be left as 0 if no gateway exists + * for that subnet. For the 'auto' method, given IP addresses are appended * to those returned by automatic configuration. Addresses cannot be used * with the "shared", "link-local", or "disabled" methods as addressing is * either automatic or disabled with these methods. + * + * Element-Type: NMIP4Address **/ g_object_class_install_property (object_class, PROP_ADDRESSES, - _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_ADDRESSES, "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP4_CONFIG_ADDRESSES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_ADDRESSES, + DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + _nm_utils_ip4_addresses_to_dbus, + _nm_utils_ip4_addresses_from_dbus); /** * NMSettingIP4Config:address-labels: @@ -1288,31 +1325,33 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) **/ g_object_class_install_property (object_class, PROP_ADDRESS_LABELS, - _nm_param_spec_specialized ("address-labels", "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed ("address-labels", "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingIP4Config:routes: * - * Array of IPv4 route structures. Each IPv4 route structure is composed of - * 4 32-bit values; the first being the destination IPv4 network or address - * (network byte order), the second the destination network or address - * prefix (1 - 32), the third being the next-hop (network byte order) if - * any, and the fourth being the route metric. For the "auto" method, given - * IP routes are appended to those returned by automatic configuration. - * Routes cannot be used with the "shared", "link-local", or "disabled" - * methods because there is no upstream network. + * Array of IPv4 routes. For the 'auto' method, given IP routes are appended + * to those returned by automatic configuration. Routes cannot be used with + * the 'shared', 'link-local', or 'disabled' methods because there is no + * upstream network. + * + * Element-Type: NMIP4Route **/ g_object_class_install_property (object_class, PROP_ROUTES, - _nm_param_spec_specialized (NM_SETTING_IP4_CONFIG_ROUTES, "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP4_CONFIG_ROUTES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_ROUTES, + DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + _nm_utils_ip4_routes_to_dbus, + _nm_utils_ip4_routes_from_dbus); /** * NMSettingIP4Config:ignore-auto-routes: diff --git a/libnm-core/nm-setting-ip4-config.h b/libnm-core/nm-setting-ip4-config.h index 1caa563ddb..4891571131 100644 --- a/libnm-core/nm-setting-ip4-config.h +++ b/libnm-core/nm-setting-ip4-config.h @@ -184,10 +184,10 @@ NMSetting * nm_setting_ip4_config_new (void); const char * nm_setting_ip4_config_get_method (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting); -guint32 nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, guint32 dns); +const char * nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i); +gboolean nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, const char *dns); void nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, guint32 dns); +gboolean nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, const char *dns); void nm_setting_ip4_config_clear_dns (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_dns_searches (NMSettingIP4Config *setting); diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index 90ebd4e182..6c085404d0 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -24,8 +24,8 @@ #include #include "nm-setting-ip6-config.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" +#include "nm-utils-private.h" #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" #include "nm-setting-private.h" @@ -161,18 +161,28 @@ nm_setting_ip6_config_get_num_dns (NMSettingIP6Config *setting) * * Returns: (transfer none): the IPv6 address of the DNS server at index @i **/ -const struct in6_addr * +const char * nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i) { NMSettingIP6ConfigPrivate *priv; - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->dns), NULL); + g_return_val_if_fail (i < g_slist_length (priv->dns), NULL); - return (const struct in6_addr *) g_slist_nth_data (priv->dns, i); + return (const char *) g_slist_nth_data (priv->dns, i); +} + +static const char * +canonicalize_ip (const char *ip) +{ + struct in6_addr addr; + int ret; + + ret = inet_pton (AF_INET6, ip, &addr); + g_return_val_if_fail (ret == 1, NULL); + return nm_utils_inet6_ntop (&addr, NULL); } /** @@ -186,25 +196,28 @@ nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i) * known **/ gboolean -nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const struct in6_addr *addr) +nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const char *dns) { NMSettingIP6ConfigPrivate *priv; - struct in6_addr *copy; + const char *dns_canonical; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (dns[0] != '\0', FALSE); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); + + dns_canonical = canonicalize_ip (dns); + g_return_val_if_fail (dns_canonical != NULL, FALSE); + for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!memcmp (addr, (struct in6_addr *) iter->data, sizeof (struct in6_addr))) + if (!strcmp (dns_canonical, (char *) iter->data)) return FALSE; } - copy = g_malloc0 (sizeof (struct in6_addr)); - memcpy (copy, addr, sizeof (struct in6_addr)); - priv->dns = g_slist_append (priv->dns, copy); + priv->dns = g_slist_append (priv->dns, g_strdup (dns_canonical)); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); - return TRUE; } @@ -243,16 +256,23 @@ nm_setting_ip6_config_remove_dns (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting, - const struct in6_addr *addr) + const char *dns) { NMSettingIP6ConfigPrivate *priv; + const char *dns_canonical; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (dns[0] != '\0', FALSE); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); + + dns_canonical = canonicalize_ip (dns); + g_return_val_if_fail (dns_canonical != NULL, FALSE); + for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!memcmp (addr, (struct in6_addr *) iter->data, sizeof (struct in6_addr))) { + if (!strcmp (dns_canonical, (char *) iter->data)) { priv->dns = g_slist_delete_link (priv->dns, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); return TRUE; @@ -306,7 +326,7 @@ nm_setting_ip6_config_get_dns_search (NMSettingIP6Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->dns_search), NULL); + g_return_val_if_fail (i < g_slist_length (priv->dns_search), NULL); return (const char *) g_slist_nth_data (priv->dns_search, i); } @@ -443,7 +463,7 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->addresses), NULL); + g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); return (NMIP6Address *) g_slist_nth_data (priv->addresses, i); } @@ -583,7 +603,7 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i <= g_slist_length (priv->routes), NULL); + g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); return (NMIP6Route *) g_slist_nth_data (priv->routes, i); } @@ -787,6 +807,8 @@ static gboolean verify (NMSetting *setting, GSList *all_settings, GError **error) { NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); + GSList *iter; + int i; if (!priv->method) { g_set_error_literal (error, @@ -810,7 +832,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } else if ( !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) { - if (g_slist_length (priv->dns)) { + if (priv->dns) { g_set_error (error, NM_SETTING_IP6_CONFIG_ERROR, NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -821,7 +843,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (g_slist_length (priv->dns_search)) { + if (priv->dns_search) { g_set_error (error, NM_SETTING_IP6_CONFIG_ERROR, NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -831,7 +853,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (g_slist_length (priv->addresses)) { + if (priv->addresses) { g_set_error (error, NM_SETTING_IP6_CONFIG_ERROR, NM_SETTING_IP6_CONFIG_ERROR_NOT_ALLOWED_FOR_METHOD, @@ -861,6 +883,21 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } + for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { + const char *dns = (const char *) iter->data; + struct in6_addr addr; + + if (inet_pton (AF_INET6, dns, &addr) != 1) { + g_set_error (error, + NM_SETTING_IP6_CONFIG_ERROR, + NM_SETTING_IP6_CONFIG_ERROR_INVALID_PROPERTY, + _("%d. DNS server address is invalid"), + i+1); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); + return FALSE; + } + } + return TRUE; } @@ -899,19 +936,21 @@ set_property (GObject *object, guint prop_id, break; case PROP_DNS: g_slist_free_full (priv->dns, g_free); - priv->dns = nm_utils_ip6_dns_from_gvalue (value); + priv->dns = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_DNS_SEARCH: g_slist_free_full (priv->dns_search, g_free); - priv->dns_search = g_value_dup_boxed (value); + priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: - g_slist_free_full (priv->addresses, g_free); - priv->addresses = nm_utils_ip6_addresses_from_gvalue (value); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_address_unref); + priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip6_address_dup); break; case PROP_ROUTES: - g_slist_free_full (priv->routes, g_free); - priv->routes = nm_utils_ip6_routes_from_gvalue (value); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip6_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -949,16 +988,16 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, priv->method); break; case PROP_DNS: - nm_utils_ip6_dns_to_gvalue (priv->dns, value); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns)); break; case PROP_DNS_SEARCH: - g_value_set_boxed (value, priv->dns_search); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - nm_utils_ip6_addresses_to_gvalue (priv->addresses, value); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip6_address_dup, (GDestroyNotify) nm_ip6_address_unref)); break; case PROP_ROUTES: - nm_utils_ip6_routes_to_gvalue (priv->routes, value); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip6_route_dup, (GDestroyNotify) nm_ip6_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, priv->ignore_auto_routes); @@ -996,7 +1035,7 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) object_class->set_property = set_property; object_class->get_property = get_property; object_class->finalize = finalize; - parent_class->verify = verify; + parent_class->verify = verify; /* Properties */ /** @@ -1037,20 +1076,23 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) /** * NMSettingIP6Config:dns: * - * Array of DNS servers, where each member of the array is a byte array - * containing the IPv6 address of the DNS server (in network byte order). - * For the "auto" method, these DNS servers are appended to those (if any) - * returned by automatic configuration. DNS servers cannot be used with the - * "shared" or "link-local" methods as there is no usptream network. In all - * other methods, these DNS servers are used as the only DNS servers for - * this connection. + * Array of IPv6 addresses of DNS servers. For the "auto" method, these DNS + * servers are appended to those (if any) returned by automatic + * configuration. DNS servers cannot be used with the "shared" or + * "link-local" methods as there is no usptream network. In all other + * methods, these DNS servers are used as the only DNS servers for this + * connection. **/ g_object_class_install_property (object_class, PROP_DNS, - _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_DNS, "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP6_CONFIG_DNS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_DNS, + DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR, + _nm_utils_ip6_dns_to_dbus, + _nm_utils_ip6_dns_from_dbus); /** * NMSettingIP6Config:dns-search: @@ -1063,52 +1105,53 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) **/ g_object_class_install_property (object_class, PROP_DNS_SEARCH, - _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_DNS_SEARCH, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP6_CONFIG_DNS_SEARCH, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingIP6Config:addresses: * - * Array of IPv6 address structures. Each IPv6 address structure is - * composed of 3 members, the first being a byte array containing the IPv6 - * address (network byte order), the second a 32-bit integer containing the - * IPv6 address prefix, and the third a byte array containing the IPv6 - * address (network byte order) of the gateway associated with this address, - * if any. If no gateway is given, the third element should be given as all - * zeros. For the "auto" method, given IP addresses are appended to those - * returned by automatic configuration. Addresses cannot be used with the - * "shared" or "link-local" methods as the interface is automatically - * assigned an address with these methods. + * Array of IPv6 addresses. For the 'auto' method, given IP addresses are + * appended to those returned by automatic configuration. Addresses cannot + * be used with the 'shared' or 'link-local' methods as the interface is + * automatically assigned an address with these methods. + * + * Element-Type: NMIP6Address **/ g_object_class_install_property (object_class, PROP_ADDRESSES, - _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_ADDRESSES, "", "", - DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP6_CONFIG_ADDRESSES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_ADDRESSES, + DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, + _nm_utils_ip6_addresses_to_dbus, + _nm_utils_ip6_addresses_from_dbus); /** * NMSettingIP6Config:routes: * - * Array of IPv6 route structures. Each IPv6 route structure is composed of - * 4 members; the first being the destination IPv6 network or address - * (network byte order) as a byte array, the second the destination network - * or address IPv6 prefix, the third being the next-hop IPv6 address - * (network byte order) if any, and the fourth being the route metric. For - * the "auto" method, given IP routes are appended to those returned by - * automatic configuration. Routes cannot be used with the "shared" or - * "link-local" methods because there is no upstream network. + * Array of IPv6 routes. For the 'auto' method, given IP routes are appended + * to those returned by automatic configuration. Routes cannot be used with + * the 'shared' or 'link-local' methods because there is no upstream network. + * + * Element-Type: NMIP6Route **/ g_object_class_install_property (object_class, PROP_ROUTES, - _nm_param_spec_specialized (NM_SETTING_IP6_CONFIG_ROUTES, "", "", - DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_IP6_CONFIG_ROUTES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_ROUTES, + DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE, + _nm_utils_ip6_routes_to_dbus, + _nm_utils_ip6_routes_from_dbus); /** * NMSettingIP6Config:ignore-auto-routes: diff --git a/libnm-core/nm-setting-ip6-config.h b/libnm-core/nm-setting-ip6-config.h index 2dc9c8dde2..349cc36a48 100644 --- a/libnm-core/nm-setting-ip6-config.h +++ b/libnm-core/nm-setting-ip6-config.h @@ -214,10 +214,10 @@ NMSetting * nm_setting_ip6_config_new (void); const char * nm_setting_ip6_config_get_method (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_dns (NMSettingIP6Config *setting); -const struct in6_addr *nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const struct in6_addr *dns); +const char * nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i); +gboolean nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const char *dns); void nm_setting_ip6_config_remove_dns (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting, const struct in6_addr *dns); +gboolean nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting, const char *dns); void nm_setting_ip6_config_clear_dns (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_dns_searches (NMSettingIP6Config *setting); diff --git a/libnm-core/nm-setting-olpc-mesh.c b/libnm-core/nm-setting-olpc-mesh.c index fab45b2de7..378aa044da 100644 --- a/libnm-core/nm-setting-olpc-mesh.c +++ b/libnm-core/nm-setting-olpc-mesh.c @@ -26,7 +26,6 @@ #include "nm-setting-olpc-mesh.h" #include "nm-dbus-interface.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-dbus-glib-types.h" #include "nm-utils-private.h" @@ -51,9 +50,9 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_OLPC_MESH) #define NM_SETTING_OLPC_MESH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_OLPC_MESH, NMSettingOlpcMeshPrivate)) typedef struct { - GByteArray *ssid; + GBytes *ssid; guint32 channel; - GByteArray *dhcp_anycast_addr; + char *dhcp_anycast_addr; } NMSettingOlpcMeshPrivate; enum { @@ -82,7 +81,7 @@ nm_setting_olpc_mesh_init (NMSettingOlpcMesh *setting) { } -const GByteArray * +GBytes * nm_setting_olpc_mesh_get_ssid (NMSettingOlpcMesh *setting) { g_return_val_if_fail (NM_IS_SETTING_OLPC_MESH (setting), NULL); @@ -98,7 +97,7 @@ nm_setting_olpc_mesh_get_channel (NMSettingOlpcMesh *setting) return NM_SETTING_OLPC_MESH_GET_PRIVATE (setting)->channel; } -const GByteArray * +const char * nm_setting_olpc_mesh_get_dhcp_anycast_address (NMSettingOlpcMesh *setting) { g_return_val_if_fail (NM_IS_SETTING_OLPC_MESH (setting), NULL); @@ -110,6 +109,7 @@ static gboolean verify (NMSetting *setting, GSList *all_settings, GError **error) { NMSettingOlpcMeshPrivate *priv = NM_SETTING_OLPC_MESH_GET_PRIVATE (setting); + gsize length; if (!priv->ssid) { g_set_error_literal (error, @@ -120,7 +120,8 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (!priv->ssid->len || priv->ssid->len > 32) { + length = g_bytes_get_size (priv->ssid); + if (length == 0 || length > 32) { g_set_error_literal (error, NM_SETTING_OLPC_MESH_ERROR, NM_SETTING_OLPC_MESH_ERROR_INVALID_PROPERTY, @@ -139,7 +140,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->dhcp_anycast_addr && priv->dhcp_anycast_addr->len != ETH_ALEN) { + if (priv->dhcp_anycast_addr && !nm_utils_hwaddr_valid (priv->dhcp_anycast_addr, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_OLPC_MESH_ERROR, NM_SETTING_OLPC_MESH_ERROR_INVALID_PROPERTY, @@ -157,9 +158,8 @@ finalize (GObject *object) NMSettingOlpcMeshPrivate *priv = NM_SETTING_OLPC_MESH_GET_PRIVATE (object); if (priv->ssid) - g_byte_array_free (priv->ssid, TRUE); - if (priv->dhcp_anycast_addr) - g_byte_array_free (priv->dhcp_anycast_addr, TRUE); + g_bytes_unref (priv->ssid); + g_free (priv->dhcp_anycast_addr); G_OBJECT_CLASS (nm_setting_olpc_mesh_parent_class)->finalize (object); } @@ -173,16 +173,15 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_SSID: if (priv->ssid) - g_byte_array_free (priv->ssid, TRUE); + g_bytes_unref (priv->ssid); priv->ssid = g_value_dup_boxed (value); break; case PROP_CHANNEL: priv->channel = g_value_get_uint (value); break; case PROP_DHCP_ANYCAST_ADDRESS: - if (priv->dhcp_anycast_addr) - g_byte_array_free (priv->dhcp_anycast_addr, TRUE); - priv->dhcp_anycast_addr = g_value_dup_boxed (value); + g_free (priv->dhcp_anycast_addr); + priv->dhcp_anycast_addr = g_value_dup_string (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -204,7 +203,7 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, nm_setting_olpc_mesh_get_channel (setting)); break; case PROP_DHCP_ANYCAST_ADDRESS: - g_value_set_boxed (value, nm_setting_olpc_mesh_get_dhcp_anycast_address (setting)); + g_value_set_string (value, nm_setting_olpc_mesh_get_dhcp_anycast_address (setting)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -234,11 +233,15 @@ nm_setting_olpc_mesh_class_init (NMSettingOlpcMeshClass *setting_class) **/ g_object_class_install_property (object_class, PROP_SSID, - _nm_param_spec_specialized (NM_SETTING_OLPC_MESH_SSID, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_OLPC_MESH_SSID, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_OLPC_MESH_SSID, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSettingOlpcMesh:channel: @@ -263,8 +266,12 @@ nm_setting_olpc_mesh_class_init (NMSettingOlpcMeshClass *setting_class) **/ g_object_class_install_property (object_class, PROP_DHCP_ANYCAST_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); } diff --git a/libnm-core/nm-setting-olpc-mesh.h b/libnm-core/nm-setting-olpc-mesh.h index 67c0981fc9..c363894540 100644 --- a/libnm-core/nm-setting-olpc-mesh.h +++ b/libnm-core/nm-setting-olpc-mesh.h @@ -74,9 +74,9 @@ typedef struct { GType nm_setting_olpc_mesh_get_type (void); NMSetting * nm_setting_olpc_mesh_new (void); -const GByteArray *nm_setting_olpc_mesh_get_ssid (NMSettingOlpcMesh *setting); +GBytes * nm_setting_olpc_mesh_get_ssid (NMSettingOlpcMesh *setting); guint32 nm_setting_olpc_mesh_get_channel (NMSettingOlpcMesh *setting); -const GByteArray *nm_setting_olpc_mesh_get_dhcp_anycast_address (NMSettingOlpcMesh *setting); +const char * nm_setting_olpc_mesh_get_dhcp_anycast_address (NMSettingOlpcMesh *setting); G_END_DECLS diff --git a/libnm-core/nm-setting-private.h b/libnm-core/nm-setting-private.h index 9d9a4caa4f..aad7e51697 100644 --- a/libnm-core/nm-setting-private.h +++ b/libnm-core/nm-setting-private.h @@ -158,4 +158,13 @@ void _nm_setting_class_override_property (NMSettingClass *setting_class, NMSettingPropertySetFunc set_func, NMSettingPropertyNotSetFunc not_set_func); +typedef void (*NMSettingPropertyTransformFunc) (const GValue *from, + GValue *to); + +void _nm_setting_class_transform_property (NMSettingClass *setting_class, + const char *property_name, + GType dbus_type, + NMSettingPropertyTransformFunc to_dbus, + NMSettingPropertyTransformFunc from_dbus); + #endif /* NM_SETTING_PRIVATE_H */ diff --git a/libnm-core/nm-setting-team.c b/libnm-core/nm-setting-team.c index 37a274abaf..5946ec75a8 100644 --- a/libnm-core/nm-setting-team.c +++ b/libnm-core/nm-setting-team.c @@ -24,7 +24,6 @@ #include #include "nm-setting-team.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-utils-private.h" #include "nm-dbus-glib-types.h" diff --git a/libnm-core/nm-setting-vlan.c b/libnm-core/nm-setting-vlan.c index 45db1cc5e8..f1781027d0 100644 --- a/libnm-core/nm-setting-vlan.c +++ b/libnm-core/nm-setting-vlan.c @@ -21,13 +21,10 @@ #include #include -#include #include #include "nm-setting-vlan.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" -#include "nm-dbus-glib-types.h" #include "nm-setting-connection.h" #include "nm-setting-private.h" @@ -575,14 +572,15 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } static GSList * -priority_stringlist_to_maplist (NMVlanPriorityMap map, GSList *strlist) +priority_strv_to_maplist (NMVlanPriorityMap map, char **strv) { - GSList *list = NULL, *iter; + GSList *list = NULL; + int i; - for (iter = strlist; iter; iter = g_slist_next (iter)) { + for (i = 0; strv[i]; i++) { PriorityMap *item; - item = priority_map_new_from_str (map, (const char *) iter->data); + item = priority_map_new_from_str (map, strv[i]); if (item) list = g_slist_prepend (list, item); } @@ -610,12 +608,12 @@ set_property (GObject *object, guint prop_id, case PROP_INGRESS_PRIORITY_MAP: g_slist_free_full (priv->ingress_priority_map, g_free); priv->ingress_priority_map = - priority_stringlist_to_maplist (NM_VLAN_INGRESS_MAP, g_value_get_boxed (value)); + priority_strv_to_maplist (NM_VLAN_INGRESS_MAP, g_value_get_boxed (value)); break; case PROP_EGRESS_PRIORITY_MAP: g_slist_free_full (priv->egress_priority_map, g_free); priv->egress_priority_map = - priority_stringlist_to_maplist (NM_VLAN_EGRESS_MAP, g_value_get_boxed (value)); + priority_strv_to_maplist (NM_VLAN_EGRESS_MAP, g_value_get_boxed (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -623,17 +621,22 @@ set_property (GObject *object, guint prop_id, } } -static GSList * -priority_maplist_to_stringlist (GSList *list) +static char ** +priority_maplist_to_strv (GSList *list) { - GSList *strlist = NULL, *iter; + GSList *iter; + GPtrArray *strv; + + strv = g_ptr_array_new (); for (iter = list; iter; iter = g_slist_next (iter)) { PriorityMap *item = iter->data; - strlist = g_slist_prepend (strlist, g_strdup_printf ("%d:%d", item->from, item->to)); + g_ptr_array_add (strv, g_strdup_printf ("%d:%d", item->from, item->to)); } - return g_slist_reverse (strlist); + g_ptr_array_add (strv, NULL); + + return (char **) g_ptr_array_free (strv, FALSE); } static void @@ -654,10 +657,10 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, priv->flags); break; case PROP_INGRESS_PRIORITY_MAP: - g_value_take_boxed (value, priority_maplist_to_stringlist (priv->ingress_priority_map)); + g_value_take_boxed (value, priority_maplist_to_strv (priv->ingress_priority_map)); break; case PROP_EGRESS_PRIORITY_MAP: - g_value_take_boxed (value, priority_maplist_to_stringlist (priv->egress_priority_map)); + g_value_take_boxed (value, priority_maplist_to_strv (priv->egress_priority_map)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -753,11 +756,11 @@ nm_setting_vlan_class_init (NMSettingVlanClass *setting_class) **/ g_object_class_install_property (object_class, PROP_INGRESS_PRIORITY_MAP, - _nm_param_spec_specialized (NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingVlan:egress-priority-map: @@ -768,11 +771,11 @@ nm_setting_vlan_class_init (NMSettingVlanClass *setting_class) **/ g_object_class_install_property (object_class, PROP_EGRESS_PRIORITY_MAP, - _nm_param_spec_specialized (NM_SETTING_VLAN_EGRESS_PRIORITY_MAP, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_VLAN_EGRESS_PRIORITY_MAP, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); _nm_setting_class_add_dbus_only_property (parent_class, "interface-name", G_TYPE_STRING, _nm_setting_get_deprecated_virtual_interface_name, diff --git a/libnm-core/nm-setting-vpn.c b/libnm-core/nm-setting-vpn.c index a45aaeba32..30a4893d31 100644 --- a/libnm-core/nm-setting-vpn.c +++ b/libnm-core/nm-setting-vpn.c @@ -26,8 +26,8 @@ #include #include "nm-setting-vpn.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" +#include "nm-utils-private.h" #include "nm-dbus-glib-types.h" #include "nm-setting-private.h" @@ -714,20 +714,11 @@ finalize (GObject *object) G_OBJECT_CLASS (nm_setting_vpn_parent_class)->finalize (object); } -static void -copy_hash (gpointer key, gpointer value, gpointer user_data) -{ - g_return_if_fail (value != NULL); - g_return_if_fail (strlen (value)); - g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value)); -} - static void set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object); - GHashTable *new_hash; switch (prop_id) { case PROP_SERVICE_TYPE: @@ -739,18 +730,12 @@ set_property (GObject *object, guint prop_id, priv->user_name = g_value_dup_string (value); break; case PROP_DATA: - /* Must make a deep copy of the hash table here... */ - g_hash_table_remove_all (priv->data); - new_hash = g_value_get_boxed (value); - if (new_hash) - g_hash_table_foreach (new_hash, copy_hash, priv->data); + g_hash_table_unref (priv->data); + priv->data = _nm_utils_copy_strdict (g_value_get_boxed (value)); break; case PROP_SECRETS: - /* Must make a deep copy of the hash table here... */ - g_hash_table_remove_all (priv->secrets); - new_hash = g_value_get_boxed (value); - if (new_hash) - g_hash_table_foreach (new_hash, copy_hash, priv->secrets); + g_hash_table_unref (priv->secrets); + priv->secrets = _nm_utils_copy_strdict (g_value_get_boxed (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -773,10 +758,10 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, nm_setting_vpn_get_user_name (setting)); break; case PROP_DATA: - g_value_set_boxed (value, priv->data); + g_value_take_boxed (value, _nm_utils_copy_strdict (priv->data)); break; case PROP_SECRETS: - g_value_set_boxed (value, priv->secrets); + g_value_take_boxed (value, _nm_utils_copy_strdict (priv->secrets)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -842,25 +827,37 @@ nm_setting_vpn_class_init (NMSettingVpnClass *setting_class) * * Dictionary of key/value pairs of VPN plugin specific data. Both keys and * values must be strings. + * + * Type: GHashTable(utf8,utf8) **/ g_object_class_install_property (object_class, PROP_DATA, - _nm_param_spec_specialized (NM_SETTING_VPN_DATA, "", "", - DBUS_TYPE_G_MAP_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_VPN_DATA, "", "", + G_TYPE_HASH_TABLE, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_VPN_DATA, + DBUS_TYPE_G_MAP_OF_STRING, + _nm_utils_strdict_to_dbus, + _nm_utils_strdict_from_dbus); /** * NMSettingVpn:secrets: * * Dictionary of key/value pairs of VPN plugin specific secrets like * passwords or private keys. Both keys and values must be strings. + * + * Type: GHashTable(utf8,utf8) **/ g_object_class_install_property (object_class, PROP_SECRETS, - _nm_param_spec_specialized (NM_SETTING_VPN_SECRETS, "", "", - DBUS_TYPE_G_MAP_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_SECRET | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_VPN_SECRETS, "", "", + G_TYPE_HASH_TABLE, + G_PARAM_READWRITE | + NM_SETTING_PARAM_SECRET | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_VPN_SECRETS, + DBUS_TYPE_G_MAP_OF_STRING, + _nm_utils_strdict_to_dbus, + _nm_utils_strdict_from_dbus); } diff --git a/libnm-core/nm-setting-wimax.c b/libnm-core/nm-setting-wimax.c index 3d0599c85e..467e1f617c 100644 --- a/libnm-core/nm-setting-wimax.c +++ b/libnm-core/nm-setting-wimax.c @@ -26,8 +26,9 @@ #include #include "nm-setting-wimax.h" -#include "nm-param-spec-specialized.h" #include "nm-setting-private.h" +#include "nm-utils.h" +#include "nm-utils-private.h" /** * SECTION:nm-setting-wimax @@ -63,7 +64,7 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_WIMAX) typedef struct { char *network_name; - GByteArray *mac_address; + char *mac_address; } NMSettingWimaxPrivate; enum { @@ -113,7 +114,7 @@ nm_setting_wimax_get_network_name (NMSettingWimax *setting) * * Returns: the MAC address **/ -const GByteArray * +const char * nm_setting_wimax_get_mac_address (NMSettingWimax *setting) { g_return_val_if_fail (NM_IS_SETTING_WIMAX (setting), NULL); @@ -144,7 +145,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->mac_address && priv->mac_address->len != ETH_ALEN) { + if (priv->mac_address && !nm_utils_hwaddr_valid (priv->mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIMAX_ERROR, NM_SETTING_WIMAX_ERROR_INVALID_PROPERTY, @@ -167,8 +168,7 @@ finalize (GObject *object) NMSettingWimaxPrivate *priv = NM_SETTING_WIMAX_GET_PRIVATE (object); g_free (priv->network_name); - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); + g_free (priv->mac_address); G_OBJECT_CLASS (nm_setting_wimax_parent_class)->finalize (object); } @@ -185,9 +185,8 @@ set_property (GObject *object, guint prop_id, priv->network_name = g_value_dup_string (value); break; case PROP_MAC_ADDRESS: - if (priv->mac_address) - g_byte_array_free (priv->mac_address, TRUE); - priv->mac_address = g_value_dup_boxed (value); + g_free (priv->mac_address); + priv->mac_address = g_value_dup_string (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -206,7 +205,7 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, nm_setting_wimax_get_network_name (setting)); break; case PROP_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_wimax_get_mac_address (setting)); + g_value_set_string (value, nm_setting_wimax_get_mac_address (setting)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -251,8 +250,12 @@ nm_setting_wimax_class_init (NMSettingWimaxClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_WIMAX_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_WIMAX_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIMAX_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); } diff --git a/libnm-core/nm-setting-wimax.h b/libnm-core/nm-setting-wimax.h index e70c135ae8..68a7df93a1 100644 --- a/libnm-core/nm-setting-wimax.h +++ b/libnm-core/nm-setting-wimax.h @@ -73,7 +73,7 @@ GType nm_setting_wimax_get_type (void); NMSetting *nm_setting_wimax_new (void); const char *nm_setting_wimax_get_network_name (NMSettingWimax *setting); -const GByteArray *nm_setting_wimax_get_mac_address (NMSettingWimax *setting); +const char *nm_setting_wimax_get_mac_address (NMSettingWimax *setting); G_END_DECLS diff --git a/libnm-core/nm-setting-wired.c b/libnm-core/nm-setting-wired.c index a302cb81f4..f641e8a813 100644 --- a/libnm-core/nm-setting-wired.c +++ b/libnm-core/nm-setting-wired.c @@ -26,7 +26,6 @@ #include #include "nm-setting-wired.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-utils-private.h" #include "nm-dbus-glib-types.h" @@ -69,11 +68,11 @@ typedef struct { guint32 speed; char *duplex; gboolean auto_negotiate; - GByteArray *device_mac_address; - GByteArray *cloned_mac_address; + char *device_mac_address; + char *cloned_mac_address; GSList *mac_address_blacklist; guint32 mtu; - GPtrArray *s390_subchannels; + char **s390_subchannels; char *s390_nettype; GHashTable *s390_options; } NMSettingWiredPrivate; @@ -180,7 +179,7 @@ nm_setting_wired_get_auto_negotiate (NMSettingWired *setting) * * Returns: the #NMSettingWired:mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_wired_get_mac_address (NMSettingWired *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), NULL); @@ -194,7 +193,7 @@ nm_setting_wired_get_mac_address (NMSettingWired *setting) * * Returns: the #NMSettingWired:cloned-mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_wired_get_cloned_mac_address (NMSettingWired *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), NULL); @@ -206,7 +205,7 @@ nm_setting_wired_get_cloned_mac_address (NMSettingWired *setting) * nm_setting_wired_get_mac_address_blacklist: * @setting: the #NMSettingWired * - * Returns: (element-type GLib.ByteArray): the #NMSettingWired:mac-address-blacklist + * Returns: (element-type utf8): the #NMSettingWired:mac-address-blacklist * property of the setting **/ const GSList * @@ -381,15 +380,15 @@ nm_setting_wired_get_mtu (NMSettingWired *setting) * connection is applicable to. The connection should only be used in * conjunction with that device. * - * Returns: (element-type utf8): #GPtrArray of strings, each specifying one - * subchannel the s390 device uses to communicate to the host. + * Returns: (transfer none) (element-type utf8): array of strings, each specifying + * one subchannel the s390 device uses to communicate to the host. **/ -const GPtrArray * +const char * const * nm_setting_wired_get_s390_subchannels (NMSettingWired *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRED (setting), NULL); - return NM_SETTING_WIRED_GET_PRIVATE (setting)->s390_subchannels; + return (const char * const *) NM_SETTING_WIRED_GET_PRIVATE (setting)->s390_subchannels; } /** @@ -607,7 +606,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->device_mac_address && priv->device_mac_address->len != ETH_ALEN) { + if (priv->device_mac_address && !nm_utils_hwaddr_valid (priv->device_mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIRED_ERROR, NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, @@ -629,14 +628,17 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } } - if ( priv->s390_subchannels - && !(priv->s390_subchannels->len == 3 || priv->s390_subchannels->len == 2)) { - g_set_error_literal (error, - NM_SETTING_WIRED_ERROR, - NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, - _("property is invalid")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_SUBCHANNELS); - return FALSE; + if (priv->s390_subchannels) { + int len = g_strv_length (priv->s390_subchannels); + + if (len != 2 && len != 3) { + g_set_error_literal (error, + NM_SETTING_WIRED_ERROR, + NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, + _("property is invalid")); + g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_SUBCHANNELS); + return FALSE; + } } if (priv->s390_nettype && !_nm_utils_string_in_list (priv->s390_nettype, valid_nettype)) { @@ -663,7 +665,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } } - if (priv->cloned_mac_address && priv->cloned_mac_address->len != ETH_ALEN) { + if (priv->cloned_mac_address && !nm_utils_hwaddr_valid (priv->cloned_mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIRED_ERROR, NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, @@ -694,34 +696,21 @@ finalize (GObject *object) g_hash_table_destroy (priv->s390_options); - if (priv->device_mac_address) - g_byte_array_free (priv->device_mac_address, TRUE); - - if (priv->cloned_mac_address) - g_byte_array_free (priv->cloned_mac_address, TRUE); - + g_free (priv->device_mac_address); + g_free (priv->cloned_mac_address); g_slist_free_full (priv->mac_address_blacklist, g_free); - if (priv->s390_subchannels) { - g_ptr_array_set_free_func (priv->s390_subchannels, g_free); - g_ptr_array_free (priv->s390_subchannels, TRUE); - } + if (priv->s390_subchannels) + g_strfreev (priv->s390_subchannels); G_OBJECT_CLASS (nm_setting_wired_parent_class)->finalize (object); } -static void -copy_hash (gpointer key, gpointer value, gpointer user_data) -{ - g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value)); -} - static void set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { NMSettingWiredPrivate *priv = NM_SETTING_WIRED_GET_PRIVATE (object); - GHashTable *new_hash; switch (prop_id) { case PROP_PORT: @@ -739,27 +728,23 @@ set_property (GObject *object, guint prop_id, priv->auto_negotiate = g_value_get_boolean (value); break; case PROP_MAC_ADDRESS: - if (priv->device_mac_address) - g_byte_array_free (priv->device_mac_address, TRUE); - priv->device_mac_address = g_value_dup_boxed (value); + g_free (priv->device_mac_address); + priv->device_mac_address = g_value_dup_string (value); break; case PROP_CLONED_MAC_ADDRESS: - if (priv->cloned_mac_address) - g_byte_array_free (priv->cloned_mac_address, TRUE); - priv->cloned_mac_address = g_value_dup_boxed (value); + g_free (priv->cloned_mac_address); + priv->cloned_mac_address = g_value_dup_string (value); break; case PROP_MAC_ADDRESS_BLACKLIST: g_slist_free_full (priv->mac_address_blacklist, g_free); - priv->mac_address_blacklist = g_value_dup_boxed (value); + priv->mac_address_blacklist = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_MTU: priv->mtu = g_value_get_uint (value); break; case PROP_S390_SUBCHANNELS: - if (priv->s390_subchannels) { - g_ptr_array_set_free_func (priv->s390_subchannels, g_free); - g_ptr_array_free (priv->s390_subchannels, TRUE); - } + if (priv->s390_subchannels) + g_strfreev (priv->s390_subchannels); priv->s390_subchannels = g_value_dup_boxed (value); break; case PROP_S390_NETTYPE: @@ -767,11 +752,8 @@ set_property (GObject *object, guint prop_id, priv->s390_nettype = g_value_dup_string (value); break; case PROP_S390_OPTIONS: - /* Must make a deep copy of the hash table here... */ - g_hash_table_remove_all (priv->s390_options); - new_hash = g_value_get_boxed (value); - if (new_hash) - g_hash_table_foreach (new_hash, copy_hash, priv->s390_options); + g_hash_table_unref (priv->s390_options); + priv->s390_options = _nm_utils_copy_strdict (g_value_get_boxed (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -800,25 +782,25 @@ get_property (GObject *object, guint prop_id, g_value_set_boolean (value, nm_setting_wired_get_auto_negotiate (setting)); break; case PROP_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_wired_get_mac_address (setting)); + g_value_set_string (value, nm_setting_wired_get_mac_address (setting)); break; case PROP_CLONED_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_wired_get_cloned_mac_address (setting)); + g_value_set_string (value, nm_setting_wired_get_cloned_mac_address (setting)); break; case PROP_MAC_ADDRESS_BLACKLIST: - g_value_set_boxed (value, nm_setting_wired_get_mac_address_blacklist (setting)); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->mac_address_blacklist)); break; case PROP_MTU: g_value_set_uint (value, nm_setting_wired_get_mtu (setting)); break; case PROP_S390_SUBCHANNELS: - g_value_set_boxed (value, nm_setting_wired_get_s390_subchannels (setting)); + g_value_set_boxed (value, priv->s390_subchannels); break; case PROP_S390_NETTYPE: g_value_set_string (value, nm_setting_wired_get_s390_nettype (setting)); break; case PROP_S390_OPTIONS: - g_value_set_boxed (value, priv->s390_options); + g_value_take_boxed (value, _nm_utils_copy_strdict (priv->s390_options)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -907,11 +889,15 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_WIRED_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_WIRED_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRED_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingWired:cloned-mac-address: @@ -921,12 +907,16 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class) **/ g_object_class_install_property (object_class, PROP_CLONED_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - + g_param_spec_string (NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); + /** * NMSettingWired:mac-address-blacklist: * @@ -937,11 +927,11 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS_BLACKLIST, - _nm_param_spec_specialized (NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_FUZZY_IGNORE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_FUZZY_IGNORE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWired:mtu: @@ -971,11 +961,11 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class) **/ g_object_class_install_property (object_class, PROP_S390_SUBCHANNELS, - _nm_param_spec_specialized (NM_SETTING_WIRED_S390_SUBCHANNELS, "", "", - DBUS_TYPE_G_ARRAY_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRED_S390_SUBCHANNELS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWired:s390-nettype: @@ -998,12 +988,18 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class) * and values must be strings. Allowed keys include "portno", "layer2", * "portname", "protocol", among others. Key names must contain only * alphanumeric characters (ie, [a-zA-Z0-9]). + * + * Type: GHashTable(utf8,utf8) **/ g_object_class_install_property (object_class, PROP_S390_OPTIONS, - _nm_param_spec_specialized (NM_SETTING_WIRED_S390_OPTIONS, "", "", - DBUS_TYPE_G_MAP_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRED_S390_OPTIONS, "", "", + G_TYPE_HASH_TABLE, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRED_S390_OPTIONS, + DBUS_TYPE_G_MAP_OF_STRING, + _nm_utils_strdict_to_dbus, + _nm_utils_strdict_from_dbus); } diff --git a/libnm-core/nm-setting-wired.h b/libnm-core/nm-setting-wired.h index c635d6008c..73a25230c1 100644 --- a/libnm-core/nm-setting-wired.h +++ b/libnm-core/nm-setting-wired.h @@ -86,8 +86,8 @@ const char * nm_setting_wired_get_port (NMSettingWired *setting guint32 nm_setting_wired_get_speed (NMSettingWired *setting); const char * nm_setting_wired_get_duplex (NMSettingWired *setting); gboolean nm_setting_wired_get_auto_negotiate (NMSettingWired *setting); -const GByteArray *nm_setting_wired_get_mac_address (NMSettingWired *setting); -const GByteArray *nm_setting_wired_get_cloned_mac_address (NMSettingWired *setting); +const char * nm_setting_wired_get_mac_address (NMSettingWired *setting); +const char * nm_setting_wired_get_cloned_mac_address (NMSettingWired *setting); const GSList *nm_setting_wired_get_mac_address_blacklist (NMSettingWired *setting); guint32 nm_setting_wired_get_num_mac_blacklist_items (NMSettingWired *setting); @@ -103,7 +103,7 @@ void nm_setting_wired_clear_mac_blacklist_items (NMSettingWired * guint32 nm_setting_wired_get_mtu (NMSettingWired *setting); -const GPtrArray * nm_setting_wired_get_s390_subchannels (NMSettingWired *setting); +const char * const *nm_setting_wired_get_s390_subchannels (NMSettingWired *setting); const char * nm_setting_wired_get_s390_nettype (NMSettingWired *setting); guint32 nm_setting_wired_get_num_s390_options (NMSettingWired *setting); diff --git a/libnm-core/nm-setting-wireless-security.c b/libnm-core/nm-setting-wireless-security.c index 129d321f8e..c84d123b2d 100644 --- a/libnm-core/nm-setting-wireless-security.c +++ b/libnm-core/nm-setting-wireless-security.c @@ -22,14 +22,11 @@ #include #include -#include #include #include "nm-setting-wireless-security.h" #include "nm-setting-8021x.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" -#include "nm-dbus-glib-types.h" #include "nm-utils-private.h" #include "nm-setting-private.h" @@ -1192,15 +1189,15 @@ set_property (GObject *object, guint prop_id, break; case PROP_PROTO: g_slist_free_full (priv->proto, g_free); - priv->proto = g_value_dup_boxed (value); + priv->proto = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_PAIRWISE: g_slist_free_full (priv->pairwise, g_free); - priv->pairwise = g_value_dup_boxed (value); + priv->pairwise = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_GROUP: g_slist_free_full (priv->group, g_free); - priv->group = g_value_dup_boxed (value); + priv->group = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_LEAP_USERNAME: g_free (priv->leap_username); @@ -1266,13 +1263,13 @@ get_property (GObject *object, guint prop_id, g_value_set_string (value, priv->auth_alg); break; case PROP_PROTO: - g_value_set_boxed (value, priv->proto); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->proto)); break; case PROP_PAIRWISE: - g_value_set_boxed (value, priv->pairwise); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->pairwise)); break; case PROP_GROUP: - g_value_set_boxed (value, priv->group); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->group)); break; case PROP_LEAP_USERNAME: g_value_set_string (value, priv->leap_username); @@ -1389,10 +1386,10 @@ nm_setting_wireless_security_class_init (NMSettingWirelessSecurityClass *setting **/ g_object_class_install_property (object_class, PROP_PROTO, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_SECURITY_PROTO, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_SECURITY_PROTO, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWirelessSecurity:pairwise: @@ -1404,10 +1401,10 @@ nm_setting_wireless_security_class_init (NMSettingWirelessSecurityClass *setting **/ g_object_class_install_property (object_class, PROP_PAIRWISE, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_SECURITY_PAIRWISE, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_SECURITY_PAIRWISE, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWirelessSecurity:group: @@ -1419,10 +1416,10 @@ nm_setting_wireless_security_class_init (NMSettingWirelessSecurityClass *setting **/ g_object_class_install_property (object_class, PROP_GROUP, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_SECURITY_GROUP, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_SECURITY_GROUP, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWirelessSecurity:leap-username: diff --git a/libnm-core/nm-setting-wireless.c b/libnm-core/nm-setting-wireless.c index 5c20f39328..600e54c0eb 100644 --- a/libnm-core/nm-setting-wireless.c +++ b/libnm-core/nm-setting-wireless.c @@ -27,7 +27,6 @@ #include "nm-setting-wireless.h" #include "nm-dbus-interface.h" -#include "nm-param-spec-specialized.h" #include "nm-utils.h" #include "nm-dbus-glib-types.h" #include "nm-utils-private.h" @@ -66,15 +65,15 @@ NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_WIRELESS) #define NM_SETTING_WIRELESS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_WIRELESS, NMSettingWirelessPrivate)) typedef struct { - GByteArray *ssid; + GBytes *ssid; char *mode; char *band; guint32 channel; - GByteArray *bssid; + char *bssid; guint32 rate; guint32 tx_power; - GByteArray *device_mac_address; - GByteArray *cloned_mac_address; + char *device_mac_address; + char *cloned_mac_address; GSList *mac_address_blacklist; guint32 mtu; GSList *seen_bssids; @@ -316,7 +315,7 @@ nm_setting_wireless_new (void) * * Returns: the #NMSettingWireless:ssid property of the setting **/ -const GByteArray * +GBytes * nm_setting_wireless_get_ssid (NMSettingWireless *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL); @@ -372,7 +371,7 @@ nm_setting_wireless_get_channel (NMSettingWireless *setting) * * Returns: the #NMSettingWireless:bssid property of the setting **/ -const GByteArray * +const char * nm_setting_wireless_get_bssid (NMSettingWireless *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL); @@ -414,7 +413,7 @@ nm_setting_wireless_get_tx_power (NMSettingWireless *setting) * * Returns: the #NMSettingWireless:mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_wireless_get_mac_address (NMSettingWireless *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL); @@ -428,7 +427,7 @@ nm_setting_wireless_get_mac_address (NMSettingWireless *setting) * * Returns: the #NMSettingWireless:cloned-mac-address property of the setting **/ -const GByteArray * +const char * nm_setting_wireless_get_cloned_mac_address (NMSettingWireless *setting) { g_return_val_if_fail (NM_IS_SETTING_WIRELESS (setting), NULL); @@ -440,7 +439,7 @@ nm_setting_wireless_get_cloned_mac_address (NMSettingWireless *setting) * nm_setting_wireless_get_mac_address_blacklist: * @setting: the #NMSettingWireless * - * Returns: (element-type GLib.ByteArray): the + * Returns: (element-type utf8): the * #NMSettingWireless:mac-address-blacklist property of the setting **/ const GSList * @@ -704,6 +703,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) const char *valid_modes[] = { NM_SETTING_WIRELESS_MODE_INFRA, NM_SETTING_WIRELESS_MODE_ADHOC, NM_SETTING_WIRELESS_MODE_AP, NULL }; const char *valid_bands[] = { "a", "bg", NULL }; GSList *iter; + gsize length; if (!priv->ssid) { g_set_error_literal (error, @@ -714,7 +714,8 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (!priv->ssid->len || priv->ssid->len > 32) { + length = g_bytes_get_size (priv->ssid); + if (length == 0 || length > 32) { g_set_error_literal (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -765,7 +766,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) } } - if (priv->bssid && priv->bssid->len != ETH_ALEN) { + if (priv->bssid && !nm_utils_hwaddr_valid (priv->bssid, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -774,7 +775,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->device_mac_address && priv->device_mac_address->len != ETH_ALEN) { + if (priv->device_mac_address && !nm_utils_hwaddr_valid (priv->device_mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -783,7 +784,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) return FALSE; } - if (priv->cloned_mac_address && priv->cloned_mac_address->len != ETH_ALEN) { + if (priv->cloned_mac_address && !nm_utils_hwaddr_valid (priv->cloned_mac_address, ETH_ALEN)) { g_set_error_literal (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -846,13 +847,10 @@ finalize (GObject *object) g_free (priv->band); if (priv->ssid) - g_byte_array_free (priv->ssid, TRUE); - if (priv->bssid) - g_byte_array_free (priv->bssid, TRUE); - if (priv->device_mac_address) - g_byte_array_free (priv->device_mac_address, TRUE); - if (priv->cloned_mac_address) - g_byte_array_free (priv->cloned_mac_address, TRUE); + g_bytes_unref (priv->ssid); + g_free (priv->bssid); + g_free (priv->device_mac_address); + g_free (priv->cloned_mac_address); g_slist_free_full (priv->mac_address_blacklist, g_free); g_slist_free_full (priv->seen_bssids, g_free); @@ -868,7 +866,7 @@ set_property (GObject *object, guint prop_id, switch (prop_id) { case PROP_SSID: if (priv->ssid) - g_byte_array_free (priv->ssid, TRUE); + g_bytes_unref (priv->ssid); priv->ssid = g_value_dup_boxed (value); break; case PROP_MODE: @@ -883,9 +881,8 @@ set_property (GObject *object, guint prop_id, priv->channel = g_value_get_uint (value); break; case PROP_BSSID: - if (priv->bssid) - g_byte_array_free (priv->bssid, TRUE); - priv->bssid = g_value_dup_boxed (value); + g_free (priv->bssid); + priv->bssid = g_value_dup_string (value); break; case PROP_RATE: priv->rate = g_value_get_uint (value); @@ -894,25 +891,23 @@ set_property (GObject *object, guint prop_id, priv->tx_power = g_value_get_uint (value); break; case PROP_MAC_ADDRESS: - if (priv->device_mac_address) - g_byte_array_free (priv->device_mac_address, TRUE); - priv->device_mac_address = g_value_dup_boxed (value); + g_free (priv->device_mac_address); + priv->device_mac_address = g_value_dup_string (value); break; case PROP_CLONED_MAC_ADDRESS: - if (priv->cloned_mac_address) - g_byte_array_free (priv->cloned_mac_address, TRUE); - priv->cloned_mac_address = g_value_dup_boxed (value); + g_free (priv->cloned_mac_address); + priv->cloned_mac_address = g_value_dup_string (value); break; case PROP_MAC_ADDRESS_BLACKLIST: g_slist_free_full (priv->mac_address_blacklist, g_free); - priv->mac_address_blacklist = g_value_dup_boxed (value); + priv->mac_address_blacklist = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_MTU: priv->mtu = g_value_get_uint (value); break; case PROP_SEEN_BSSIDS: g_slist_free_full (priv->seen_bssids, g_free); - priv->seen_bssids = g_value_dup_boxed (value); + priv->seen_bssids = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_HIDDEN: priv->hidden = g_value_get_boolean (value); @@ -928,6 +923,7 @@ get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { NMSettingWireless *setting = NM_SETTING_WIRELESS (object); + NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (object); switch (prop_id) { case PROP_SSID: @@ -943,7 +939,7 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, nm_setting_wireless_get_channel (setting)); break; case PROP_BSSID: - g_value_set_boxed (value, nm_setting_wireless_get_bssid (setting)); + g_value_set_string (value, nm_setting_wireless_get_bssid (setting)); break; case PROP_RATE: g_value_set_uint (value, nm_setting_wireless_get_rate (setting)); @@ -952,19 +948,19 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, nm_setting_wireless_get_tx_power (setting)); break; case PROP_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_wireless_get_mac_address (setting)); + g_value_set_string (value, nm_setting_wireless_get_mac_address (setting)); break; case PROP_CLONED_MAC_ADDRESS: - g_value_set_boxed (value, nm_setting_wireless_get_cloned_mac_address (setting)); + g_value_set_string (value, nm_setting_wireless_get_cloned_mac_address (setting)); break; case PROP_MAC_ADDRESS_BLACKLIST: - g_value_set_boxed (value, nm_setting_wireless_get_mac_address_blacklist (setting)); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->mac_address_blacklist)); break; case PROP_MTU: g_value_set_uint (value, nm_setting_wireless_get_mtu (setting)); break; case PROP_SEEN_BSSIDS: - g_value_set_boxed (value, NM_SETTING_WIRELESS_GET_PRIVATE (setting)->seen_bssids); + g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->seen_bssids)); break; case PROP_HIDDEN: g_value_set_boolean (value, nm_setting_wireless_get_hidden (setting)); @@ -997,10 +993,14 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_SSID, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_SSID, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_SSID, "", "", + G_TYPE_BYTES, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRELESS_SSID, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_bytes_to_dbus, + _nm_utils_bytes_from_dbus); /** * NMSettingWireless:mode: @@ -1058,10 +1058,14 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_BSSID, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_BSSID, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_WIRELESS_BSSID, "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRELESS_BSSID, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingWireless:rate: @@ -1105,10 +1109,14 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_WIRELESS_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRELESS_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingWireless:cloned-mac-address: @@ -1118,10 +1126,14 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_CLONED_MAC_ADDRESS, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, "", "", - DBUS_TYPE_G_UCHAR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_string (NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, "", "", + NULL, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, + DBUS_TYPE_G_UCHAR_ARRAY, + _nm_utils_hwaddr_to_dbus, + _nm_utils_hwaddr_from_dbus); /** * NMSettingWireless:mac-address-blacklist: @@ -1132,11 +1144,11 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_MAC_ADDRESS_BLACKLIST, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_FUZZY_IGNORE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_FUZZY_IGNORE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWireless:seen-bssids: @@ -1150,11 +1162,11 @@ nm_setting_wireless_class_init (NMSettingWirelessClass *setting_class) **/ g_object_class_install_property (object_class, PROP_SEEN_BSSIDS, - _nm_param_spec_specialized (NM_SETTING_WIRELESS_SEEN_BSSIDS, "", "", - DBUS_TYPE_G_LIST_OF_STRING, - G_PARAM_READWRITE | - NM_SETTING_PARAM_FUZZY_IGNORE | - G_PARAM_STATIC_STRINGS)); + g_param_spec_boxed (NM_SETTING_WIRELESS_SEEN_BSSIDS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + NM_SETTING_PARAM_FUZZY_IGNORE | + G_PARAM_STATIC_STRINGS)); /** * NMSettingWireless:mtu: diff --git a/libnm-core/nm-setting-wireless.h b/libnm-core/nm-setting-wireless.h index 54dc6af3a8..50826efe6b 100644 --- a/libnm-core/nm-setting-wireless.h +++ b/libnm-core/nm-setting-wireless.h @@ -116,15 +116,15 @@ GType nm_setting_wireless_get_type (void); NMSetting *nm_setting_wireless_new (void); -const GByteArray *nm_setting_wireless_get_ssid (NMSettingWireless *setting); +GBytes *nm_setting_wireless_get_ssid (NMSettingWireless *setting); const char *nm_setting_wireless_get_mode (NMSettingWireless *setting); const char *nm_setting_wireless_get_band (NMSettingWireless *setting); guint32 nm_setting_wireless_get_channel (NMSettingWireless *setting); -const GByteArray *nm_setting_wireless_get_bssid (NMSettingWireless *setting); +const char *nm_setting_wireless_get_bssid (NMSettingWireless *setting); guint32 nm_setting_wireless_get_rate (NMSettingWireless *setting); guint32 nm_setting_wireless_get_tx_power (NMSettingWireless *setting); -const GByteArray *nm_setting_wireless_get_mac_address (NMSettingWireless *setting); -const GByteArray *nm_setting_wireless_get_cloned_mac_address (NMSettingWireless *setting); +const char *nm_setting_wireless_get_mac_address (NMSettingWireless *setting); +const char *nm_setting_wireless_get_cloned_mac_address (NMSettingWireless *setting); const GSList *nm_setting_wireless_get_mac_address_blacklist (NMSettingWireless *setting); guint32 nm_setting_wireless_get_num_mac_blacklist_items (NMSettingWireless *setting); diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c index b9885a9c84..f65171f97e 100644 --- a/libnm-core/nm-setting.c +++ b/libnm-core/nm-setting.c @@ -28,6 +28,7 @@ #include "nm-setting-connection.h" #include "nm-utils.h" #include "nm-utils-private.h" +#include "nm-property-compare.h" /** * SECTION:nm-setting @@ -389,9 +390,13 @@ typedef struct { const char *name; GParamSpec *param_spec; GType dbus_type; + NMSettingPropertyGetFunc get_func; NMSettingPropertySetFunc set_func; NMSettingPropertyNotSetFunc not_set_func; + + NMSettingPropertyTransformFunc to_dbus; + NMSettingPropertyTransformFunc from_dbus; } NMSettingProperty; static GQuark setting_property_overrides_quark; @@ -422,7 +427,9 @@ add_property_override (NMSettingClass *setting_class, GType dbus_type, NMSettingPropertyGetFunc get_func, NMSettingPropertySetFunc set_func, - NMSettingPropertyNotSetFunc not_set_func) + NMSettingPropertyNotSetFunc not_set_func, + NMSettingPropertyTransformFunc to_dbus, + NMSettingPropertyTransformFunc from_dbus) { GType setting_type = G_TYPE_FROM_CLASS (setting_class); GArray *overrides; @@ -437,6 +444,8 @@ add_property_override (NMSettingClass *setting_class, override.get_func = get_func; override.set_func = set_func; override.not_set_func = not_set_func; + override.to_dbus = to_dbus; + override.from_dbus = from_dbus; overrides = g_type_get_qdata (setting_type, setting_property_overrides_quark); if (!overrides) { @@ -487,7 +496,8 @@ _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, add_property_override (setting_class, property_name, NULL, dbus_type, - get_func, set_func, NULL); + get_func, set_func, NULL, + NULL, NULL); } /** @@ -535,7 +545,42 @@ _nm_setting_class_override_property (NMSettingClass *setting_class, add_property_override (setting_class, property_name, param_spec, dbus_type, - get_func, set_func, not_set_func); + get_func, set_func, not_set_func, + NULL, NULL); +} + +/** + * _nm_setting_class_transform_property: + * @setting_class: the setting class + * @property: the name of the property to transform + * @dbus_type: the type of the property (in its D-Bus representation) + * @to_dbus: function to convert from object to D-Bus format + * @from_dbus: function to convert from D-Bus to object format + * + * Indicates that @property on @setting_class does not have the same format as + * its corresponding D-Bus representation, and so must be transformed when + * serializing/deserializing. + * + * The transformation will also be used by nm_setting_compare(), meaning that + * the underlying object property does not need to be of a type that + * nm_property_compare() recognizes, as long as it recognizes @dbus_type. + */ +void +_nm_setting_class_transform_property (NMSettingClass *setting_class, + const char *property, + GType dbus_type, + NMSettingPropertyTransformFunc to_dbus, + NMSettingPropertyTransformFunc from_dbus) +{ + GParamSpec *param_spec; + + param_spec = g_object_class_find_property (G_OBJECT_CLASS (setting_class), property); + g_return_if_fail (param_spec != NULL); + + add_property_override (setting_class, + property, param_spec, dbus_type, + NULL, NULL, NULL, + to_dbus, from_dbus); } static GArray * @@ -680,6 +725,16 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS /* Don't serialize values with default values */ set = !g_param_value_defaults (prop_spec, value); + + /* Convert the property value if necessary */ + if (set && property->to_dbus) { + GValue *dbus_value = g_slice_new0 (GValue); + + g_value_init (dbus_value, property->dbus_type); + property->to_dbus (value, dbus_value); + destroy_gvalue (value); + value = dbus_value; + } } else g_assert_not_reached (); @@ -780,7 +835,15 @@ _nm_setting_new_from_dbus (GType setting_type, if (!(property->param_spec->flags & G_PARAM_WRITABLE)) continue; - g_object_set_property (G_OBJECT (setting), property->param_spec->name, value); + if (property->from_dbus) { + GValue object_value = G_VALUE_INIT; + + g_value_init (&object_value, property->param_spec->value_type); + property->from_dbus (value, &object_value); + g_object_set_property (G_OBJECT (setting), property->param_spec->name, &object_value); + g_value_unset (&object_value); + } else + g_object_set_property (G_OBJECT (setting), property->param_spec->name, value); } } @@ -910,9 +973,10 @@ compare_property (NMSetting *setting, const GParamSpec *prop_spec, NMSettingCompareFlags flags) { + const NMSettingProperty *property; GValue value1 = G_VALUE_INIT; GValue value2 = G_VALUE_INIT; - gboolean different; + int cmp; /* Handle compare flags */ if (prop_spec->flags & NM_SETTING_PARAM_SECRET) { @@ -938,18 +1002,34 @@ compare_property (NMSetting *setting, return TRUE; } + property = nm_setting_class_find_property (NM_SETTING_GET_CLASS (setting), prop_spec->name); + g_return_val_if_fail (property != NULL, FALSE); + g_value_init (&value1, prop_spec->value_type); g_object_get_property (G_OBJECT (setting), prop_spec->name, &value1); g_value_init (&value2, prop_spec->value_type); g_object_get_property (G_OBJECT (other), prop_spec->name, &value2); - different = g_param_values_cmp ((GParamSpec *) prop_spec, &value1, &value2); + if (property->to_dbus) { + GValue dbus_value1 = G_VALUE_INIT, dbus_value2 = G_VALUE_INIT; + + g_value_init (&dbus_value1, property->dbus_type); + property->to_dbus (&value1, &dbus_value1); + g_value_init (&dbus_value2, property->dbus_type); + property->to_dbus (&value2, &dbus_value2); + + cmp = nm_property_compare (&dbus_value1, &dbus_value2); + + g_value_unset (&dbus_value1); + g_value_unset (&dbus_value2); + } else + cmp = nm_property_compare (&value1, &value2); g_value_unset (&value1); g_value_unset (&value2); - return different == 0 ? TRUE : FALSE; + return cmp == 0; } /** diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index 0e7be21e28..ae78d96276 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -34,4 +34,48 @@ gboolean _nm_utils_gvalue_array_validate (GValueArray *elements, void _nm_value_transforms_register (void); +void _nm_utils_hwaddr_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_hwaddr_from_dbus (const GValue *dbus_value, + GValue *prop_value); + +void _nm_utils_strdict_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_strdict_from_dbus (const GValue *dbus_value, + GValue *prop_value); + +void _nm_utils_bytes_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_bytes_from_dbus (const GValue *dbus_value, + GValue *prop_value); + +void _nm_utils_ip4_dns_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip4_dns_from_dbus (const GValue *dbus_value, + GValue *prop_value); +void _nm_utils_ip4_addresses_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip4_addresses_from_dbus (const GValue *dbus_value, + GValue *prop_value); +void _nm_utils_ip4_routes_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip4_routes_from_dbus (const GValue *dbus_value, + GValue *prop_value); + +void _nm_utils_ip6_dns_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip6_dns_from_dbus (const GValue *dbus_value, + GValue *prop_value); +void _nm_utils_ip6_addresses_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip6_addresses_from_dbus (const GValue *dbus_value, + GValue *prop_value); +void _nm_utils_ip6_routes_to_dbus (const GValue *prop_value, + GValue *dbus_value); +void _nm_utils_ip6_routes_from_dbus (const GValue *dbus_value, + GValue *prop_value); + +GSList * _nm_utils_strv_to_slist (char **strv); +char ** _nm_utils_slist_to_strv (GSList *slist); + #endif diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index fd99bfd206..06261b0d25 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -261,7 +261,8 @@ nm_utils_deinit (void) /** * nm_utils_ssid_to_utf8: - * @ssid: a byte array containing the SSID data + * @ssid: pointer to a buffer containing the SSID data + * @len: length of the SSID data in @ssid * * Wi-Fi SSIDs are byte arrays, they are _not_ strings. Thus, an SSID may * contain embedded NULLs and other unprintable characters. Often it is @@ -291,15 +292,15 @@ nm_utils_deinit (void) * Returns %NULL on errors. **/ char * -nm_utils_ssid_to_utf8 (const GByteArray *ssid) +nm_utils_ssid_to_utf8 (const guint8 *ssid, gsize len) { char *converted = NULL; char *lang, *e1 = NULL, *e2 = NULL, *e3 = NULL; g_return_val_if_fail (ssid != NULL, NULL); - if (g_utf8_validate ((const gchar *) ssid->data, ssid->len, NULL)) - return g_strndup ((const gchar *) ssid->data, ssid->len); + if (g_utf8_validate ((const gchar *) ssid, len, NULL)) + return g_strndup ((const gchar *) ssid, len); /* LANG may be a good encoding hint */ g_get_charset ((const char **)(&e1)); @@ -314,15 +315,15 @@ nm_utils_ssid_to_utf8 (const GByteArray *ssid) g_free (lang); } - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e1, NULL, NULL, NULL); + converted = g_convert ((const gchar *) ssid, len, "UTF-8", e1, NULL, NULL, NULL); if (!converted && e2) - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e2, NULL, NULL, NULL); + converted = g_convert ((const gchar *) ssid, len, "UTF-8", e2, NULL, NULL, NULL); if (!converted && e3) - converted = g_convert ((const gchar *) ssid->data, ssid->len, "UTF-8", e3, NULL, NULL, NULL); + converted = g_convert ((const gchar *) ssid, len, "UTF-8", e3, NULL, NULL, NULL); if (!converted) { - converted = g_convert_with_fallback ((const gchar *) ssid->data, ssid->len, + converted = g_convert_with_fallback ((const gchar *) ssid, len, "UTF-8", e1, "?", NULL, NULL, NULL); } @@ -342,7 +343,7 @@ nm_utils_ssid_to_utf8 (const GByteArray *ssid) * Returns: %TRUE if the SSID is "empty", %FALSE if it is not **/ gboolean -nm_utils_is_empty_ssid (const guint8 * ssid, int len) +nm_utils_is_empty_ssid (const guint8 *ssid, gsize len) { /* Single white space is for Linksys APs */ if (len == 1 && ssid[0] == ' ') @@ -372,7 +373,7 @@ nm_utils_is_empty_ssid (const guint8 * ssid, int len) * and will be overwritten by subsequent calls to this function **/ const char * -nm_utils_escape_ssid (const guint8 * ssid, guint32 len) +nm_utils_escape_ssid (const guint8 *ssid, gsize len) { static char escaped[ESSID_MAX_SIZE * 2 + 1]; const guint8 *s = ssid; @@ -399,8 +400,10 @@ nm_utils_escape_ssid (const guint8 * ssid, guint32 len) /** * nm_utils_same_ssid: - * @ssid1: first SSID data to compare - * @ssid2: second SSID data to compare + * @ssid1: the first SSID to compare + * @len1: length of the SSID data in @ssid1 + * @ssid2: the second SSID to compare + * @len2: length of the SSID data in @ssid2 * @ignore_trailing_null: %TRUE to ignore one trailing NULL byte * * Earlier versions of the Linux kernel added a NULL byte to the end of the @@ -413,30 +416,29 @@ nm_utils_escape_ssid (const guint8 * ssid, guint32 len) * Returns: %TRUE if the SSIDs are the same, %FALSE if they are not **/ gboolean -nm_utils_same_ssid (const GByteArray * ssid1, - const GByteArray * ssid2, +nm_utils_same_ssid (const guint8 *ssid1, gsize len1, + const guint8 *ssid2, gsize len2, gboolean ignore_trailing_null) { - guint32 ssid1_len, ssid2_len; + g_return_val_if_fail (ssid1 != NULL || len1 == 0, FALSE); + g_return_val_if_fail (ssid2 != NULL || len2 == 0, FALSE); - if (ssid1 == ssid2) + if (ssid1 == ssid2 && len1 == len2) return TRUE; if (!ssid1 || !ssid2) return FALSE; - ssid1_len = ssid1->len; - ssid2_len = ssid2->len; - if (ssid1_len && ssid2_len && ignore_trailing_null) { - if (ssid1->data[ssid1_len - 1] == '\0') - ssid1_len--; - if (ssid2->data[ssid2_len - 1] == '\0') - ssid2_len--; + if (ignore_trailing_null) { + if (len1 && ssid1[len1 - 1] == '\0') + len1--; + if (len2 && ssid2[len2 - 1] == '\0') + len2--; } - if (ssid1_len != ssid2_len) + if (len1 != len2) return FALSE; - return memcmp (ssid1->data, ssid2->data, ssid1_len) == 0 ? TRUE : FALSE; + return memcmp (ssid1, ssid2, len1) == 0 ? TRUE : FALSE; } static void @@ -564,6 +566,126 @@ _nm_utils_hash_values_to_slist (GHashTable *hash) return list; } +void +_nm_utils_strdict_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + g_value_set_boxed (dbus_value, g_value_get_boxed (prop_value)); +} + +void +_nm_utils_strdict_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + g_value_set_boxed (prop_value, g_value_get_boxed (dbus_value)); +} + +GHashTable * +_nm_utils_copy_strdict (GHashTable *strdict) +{ + GHashTable *copy; + GHashTableIter iter; + gpointer key, value; + + copy = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + if (strdict) { + g_hash_table_iter_init (&iter, strdict); + while (g_hash_table_iter_next (&iter, &key, &value)) + g_hash_table_insert (copy, g_strdup (key), g_strdup (value)); + } + return copy; +} + +GPtrArray * +_nm_utils_copy_slist_to_array (const GSList *list, + NMUtilsCopyFunc copy_func, + GDestroyNotify unref_func) +{ + const GSList *iter; + GPtrArray *array; + + array = g_ptr_array_new_with_free_func (unref_func); + for (iter = list; iter; iter = iter->next) + g_ptr_array_add (array, copy_func (iter->data)); + return array; +} + +GSList * +_nm_utils_copy_array_to_slist (const GPtrArray *array, + NMUtilsCopyFunc copy_func) +{ + GSList *slist = NULL; + gpointer item; + int i; + + for (i = 0; i < array->len; i++) { + item = array->pdata[i]; + slist = g_slist_prepend (slist, copy_func (item)); + } + + return g_slist_reverse (slist); +} + +void +_nm_utils_bytes_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GBytes *bytes = g_value_get_boxed (prop_value); + GByteArray *ba = NULL; + + if (bytes) { + ba = g_byte_array_new (); + g_byte_array_append (ba, + g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes)); + } + + g_value_take_boxed (dbus_value, ba); +} + +void +_nm_utils_bytes_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GByteArray *ba = g_value_dup_boxed (dbus_value); + GBytes *bytes = NULL; + + if (ba) + bytes = g_byte_array_free_to_bytes (ba); + g_value_take_boxed (prop_value, bytes); +} + +GSList * +_nm_utils_strv_to_slist (char **strv) +{ + int i; + GSList *list = NULL; + + g_return_val_if_fail (strv != NULL, NULL); + + for (i = 0; strv[i]; i++) + list = g_slist_prepend (list, g_strdup (strv[i])); + + return g_slist_reverse (list); +} + +char ** +_nm_utils_slist_to_strv (GSList *slist) +{ + GSList *iter; + char **strv; + int len, i = 0; + + len = g_slist_length (slist); + strv = g_new (char *, len + 1); + + for (i = 0, iter = slist; iter; iter = iter->next, i++) + strv[i] = g_strdup (iter->data); + strv[i] = NULL; + + return strv; +} + static gboolean device_supports_ap_ciphers (guint32 dev_caps, guint32 ap_flags, @@ -895,6 +1017,191 @@ nm_utils_wpa_psk_valid (const char *psk) return TRUE; } +void +_nm_utils_ip4_dns_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + char **dns; + int i; + GArray *array; + + dns = g_value_get_boxed (prop_value); + array = g_array_new (FALSE, FALSE, sizeof (guint32)); + + if (dns) { + for (i = 0; dns[i]; i++) { + guint32 ip = 0; + + inet_pton (AF_INET, dns[i], &ip); + g_array_append_val (array, ip); + } + } + + g_value_take_boxed (dbus_value, array); +} + +void +_nm_utils_ip4_dns_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GArray *array; + GPtrArray *dns; + int i; + + array = g_value_get_boxed (dbus_value); + dns = g_ptr_array_new (); + + if (array) { + for (i = 0; i < array->len; i++) { + guint32 ip = g_array_index (array, guint32, i); + const char *str; + + str = nm_utils_inet4_ntop (ip, NULL); + g_ptr_array_add (dns, g_strdup (str)); + } + } + + g_ptr_array_add (dns, NULL); + g_value_take_boxed (prop_value, g_ptr_array_free (dns, FALSE)); +} + +void +_nm_utils_ip4_addresses_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GPtrArray *addresses, *dbus_addresses; + int i; + + addresses = g_value_get_boxed (prop_value); + dbus_addresses = g_ptr_array_new (); + + if (addresses) { + for (i = 0; i < addresses->len; i++) { + NMIP4Address *addr = addresses->pdata[i]; + GArray *array; + guint32 tmp; + + array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3); + + tmp = nm_ip4_address_get_address (addr); + g_array_append_val (array, tmp); + + tmp = nm_ip4_address_get_prefix (addr); + g_array_append_val (array, tmp); + + tmp = nm_ip4_address_get_gateway (addr); + g_array_append_val (array, tmp); + + g_ptr_array_add (dbus_addresses, array); + } + } + + g_value_take_boxed (dbus_value, dbus_addresses); +} + +void +_nm_utils_ip4_addresses_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GPtrArray *dbus_addresses; + GPtrArray *addresses; + int i; + + dbus_addresses = g_value_get_boxed (dbus_value); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_address_unref); + + if (dbus_addresses) { + for (i = 0; i < dbus_addresses->len; i++) { + GArray *array = dbus_addresses->pdata[i]; + NMIP4Address *addr; + + if (array->len < 3) { + g_warning ("Ignoring invalid IP4 address"); + continue; + } + + addr = nm_ip4_address_new (); + nm_ip4_address_set_address (addr, g_array_index (array, guint32, 0)); + nm_ip4_address_set_prefix (addr, g_array_index (array, guint32, 1)); + nm_ip4_address_set_gateway (addr, g_array_index (array, guint32, 2)); + + g_ptr_array_add (addresses, addr); + } + } + + g_value_take_boxed (prop_value, addresses); +} + +void +_nm_utils_ip4_routes_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GPtrArray *routes, *dbus_routes; + int i; + + routes = g_value_get_boxed (prop_value); + dbus_routes = g_ptr_array_new (); + + if (routes) { + for (i = 0; i < routes->len; i++) { + NMIP4Route *route = routes->pdata[i]; + GArray *array; + guint32 tmp; + + array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 4); + + tmp = nm_ip4_route_get_dest (route); + g_array_append_val (array, tmp); + + tmp = nm_ip4_route_get_prefix (route); + g_array_append_val (array, tmp); + + tmp = nm_ip4_route_get_next_hop (route); + g_array_append_val (array, tmp); + + tmp = nm_ip4_route_get_metric (route); + g_array_append_val (array, tmp); + + g_ptr_array_add (dbus_routes, array); + } + } + + g_value_take_boxed (dbus_value, dbus_routes); +} + +void +_nm_utils_ip4_routes_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GPtrArray *dbus_routes, *routes; + int i; + + dbus_routes = g_value_get_boxed (dbus_value); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + + if (dbus_routes) { + for (i = 0; i < dbus_routes->len; i++) { + GArray *array = dbus_routes->pdata[i]; + NMIP4Route *route; + + if (array->len < 4) { + g_warning ("Ignoring invalid IP4 route"); + continue; + } + + route = nm_ip4_route_new (); + nm_ip4_route_set_dest (route, g_array_index (array, guint32, 0)); + nm_ip4_route_set_prefix (route, g_array_index (array, guint32, 1)); + nm_ip4_route_set_next_hop (route, g_array_index (array, guint32, 2)); + nm_ip4_route_set_metric (route, g_array_index (array, guint32, 3)); + + g_ptr_array_add (routes, route); + } + } + + g_value_take_boxed (prop_value, routes); +} + /** * nm_utils_ip4_addresses_from_gvalue: * @value: #GValue containing a #GPtrArray of #GArrays of #guint32s @@ -1134,6 +1441,289 @@ nm_utils_ip4_get_default_prefix (guint32 ip) return 24; /* Class C - 255.255.255.0 */ } +void +_nm_utils_ip6_dns_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + char **dns; + GPtrArray *dbus_dns; + int i; + + dns = g_value_get_boxed (prop_value); + dbus_dns = g_ptr_array_new (); + + if (dns) { + for (i = 0; dns[i]; i++) { + GByteArray *bytearray; + + bytearray = g_byte_array_new (); + g_byte_array_set_size (bytearray, 16); + inet_pton (AF_INET6, dns[i], bytearray->data); + g_ptr_array_add (dbus_dns, bytearray); + } + } + + g_value_take_boxed (dbus_value, dbus_dns); +} + +void +_nm_utils_ip6_dns_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GPtrArray *dbus_dns, *dns; + int i; + + dbus_dns = g_value_get_boxed (dbus_value); + dns = g_ptr_array_new (); + + if (dbus_dns) { + for (i = 0; i < dbus_dns->len; i++) { + GByteArray *bytearray = dbus_dns->pdata[i]; + const char *str; + + if (bytearray->len != 16) { + g_warning ("%s: ignoring invalid IP6 address of length %d", + __func__, bytearray->len); + continue; + } + + str = nm_utils_inet6_ntop ((struct in6_addr *) bytearray->data, NULL); + g_ptr_array_add (dns, g_strdup (str)); + } + } + + g_ptr_array_add (dns, NULL); + g_value_take_boxed (prop_value, g_ptr_array_free (dns, FALSE)); +} + +void +_nm_utils_ip6_addresses_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GPtrArray *addresses, *dbus_addresses; + int i; + + addresses = g_value_get_boxed (prop_value); + dbus_addresses = g_ptr_array_new (); + + if (addresses) { + for (i = 0; i < addresses->len; i++) { + NMIP6Address *addr = addresses->pdata[i]; + GValueArray *array; + GValue element = G_VALUE_INIT; + GByteArray *ba; + + array = g_value_array_new (3); + + /* IP address */ + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + ba = g_byte_array_new (); + g_byte_array_append (ba, (guint8 *) nm_ip6_address_get_address (addr), 16); + g_value_take_boxed (&element, ba); + g_value_array_append (array, &element); + g_value_unset (&element); + + /* Prefix */ + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, nm_ip6_address_get_prefix (addr)); + g_value_array_append (array, &element); + g_value_unset (&element); + + /* Gateway */ + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + ba = g_byte_array_new (); + g_byte_array_append (ba, (guint8 *) nm_ip6_address_get_gateway (addr), 16); + g_value_take_boxed (&element, ba); + g_value_array_append (array, &element); + g_value_unset (&element); + + g_ptr_array_add (dbus_addresses, array); + } + } + + g_value_take_boxed (dbus_value, dbus_addresses); +} + +void +_nm_utils_ip6_addresses_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GPtrArray *addresses, *dbus_addresses; + int i; + + dbus_addresses = g_value_get_boxed (dbus_value); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_address_unref); + + if (dbus_addresses) { + for (i = 0; i < dbus_addresses->len; i++) { + GValueArray *elements = dbus_addresses->pdata[i]; + GValue *tmp; + GByteArray *ba_addr; + GByteArray *ba_gw = NULL; + NMIP6Address *addr; + guint32 prefix; + + if (elements->n_values < 2 || elements->n_values > 3) { + g_warning ("%s: ignoring invalid IP6 address structure", __func__); + continue; + } + + /* Third element (gateway) is optional */ + if ( !_nm_utils_gvalue_array_validate (elements, 2, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT) + && !_nm_utils_gvalue_array_validate (elements, 3, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, DBUS_TYPE_G_UCHAR_ARRAY)) { + g_warning ("%s: ignoring invalid IP6 address structure", __func__); + continue; + } + + tmp = g_value_array_get_nth (elements, 0); + ba_addr = g_value_get_boxed (tmp); + if (ba_addr->len != 16) { + g_warning ("%s: ignoring invalid IP6 address of length %d", + __func__, ba_addr->len); + continue; + } + + tmp = g_value_array_get_nth (elements, 1); + prefix = g_value_get_uint (tmp); + if (prefix > 128) { + g_warning ("%s: ignoring invalid IP6 prefix %d", + __func__, prefix); + continue; + } + + if (elements->n_values == 3) { + tmp = g_value_array_get_nth (elements, 2); + ba_gw = g_value_get_boxed (tmp); + if (ba_gw->len != 16) { + g_warning ("%s: ignoring invalid IP6 gateway address of length %d", + __func__, ba_gw->len); + continue; + } + } + + addr = nm_ip6_address_new (); + nm_ip6_address_set_prefix (addr, prefix); + nm_ip6_address_set_address (addr, (const struct in6_addr *) ba_addr->data); + if (ba_gw) + nm_ip6_address_set_gateway (addr, (const struct in6_addr *) ba_gw->data); + + g_ptr_array_add (addresses, addr); + } + } + + g_value_take_boxed (prop_value, addresses); +} + +void +_nm_utils_ip6_routes_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + GPtrArray *routes, *dbus_routes; + int i; + + routes = g_value_get_boxed (prop_value); + dbus_routes = g_ptr_array_new (); + + if (routes) { + for (i = 0; i < routes->len; i++) { + NMIP6Route *route = routes->pdata[i]; + GValueArray *array; + const struct in6_addr *addr; + GByteArray *ba; + GValue element = G_VALUE_INIT; + + array = g_value_array_new (4); + + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + addr = nm_ip6_route_get_dest (route); + ba = g_byte_array_new (); + g_byte_array_append (ba, (guchar *)addr, sizeof (*addr)); + g_value_take_boxed (&element, ba); + g_value_array_append (array, &element); + g_value_unset (&element); + + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, nm_ip6_route_get_prefix (route)); + g_value_array_append (array, &element); + g_value_unset (&element); + + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + addr = nm_ip6_route_get_next_hop (route); + ba = g_byte_array_new (); + g_byte_array_append (ba, (guchar *)addr, sizeof (*addr)); + g_value_take_boxed (&element, ba); + g_value_array_append (array, &element); + g_value_unset (&element); + + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, nm_ip6_route_get_metric (route)); + g_value_array_append (array, &element); + g_value_unset (&element); + + g_ptr_array_add (dbus_routes, array); + } + } + + g_value_take_boxed (dbus_value, dbus_routes); +} + +void +_nm_utils_ip6_routes_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GPtrArray *routes, *dbus_routes; + int i; + + dbus_routes = g_value_get_boxed (dbus_value); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); + + if (dbus_routes) { + for (i = 0; i < dbus_routes->len; i++) { + GValueArray *route_values = dbus_routes->pdata[i]; + GByteArray *dest, *next_hop; + guint prefix, metric; + NMIP6Route *route; + + if (!_nm_utils_gvalue_array_validate (route_values, 4, + DBUS_TYPE_G_UCHAR_ARRAY, + G_TYPE_UINT, + DBUS_TYPE_G_UCHAR_ARRAY, + G_TYPE_UINT)) { + g_warning ("Ignoring invalid IP6 route"); + continue; + } + + dest = g_value_get_boxed (g_value_array_get_nth (route_values, 0)); + if (dest->len != 16) { + g_warning ("%s: ignoring invalid IP6 dest address of length %d", + __func__, dest->len); + continue; + } + + prefix = g_value_get_uint (g_value_array_get_nth (route_values, 1)); + + next_hop = g_value_get_boxed (g_value_array_get_nth (route_values, 2)); + if (next_hop->len != 16) { + g_warning ("%s: ignoring invalid IP6 next_hop address of length %d", + __func__, next_hop->len); + continue; + } + + metric = g_value_get_uint (g_value_array_get_nth (route_values, 3)); + + route = nm_ip6_route_new (); + nm_ip6_route_set_dest (route, (struct in6_addr *)dest->data); + nm_ip6_route_set_prefix (route, prefix); + nm_ip6_route_set_next_hop (route, (struct in6_addr *)next_hop->data); + nm_ip6_route_set_metric (route, metric); + + g_ptr_array_add (routes, route); + } + } + + g_value_take_boxed (prop_value, routes); +} + /** * nm_utils_ip6_addresses_from_gvalue: * @value: gvalue containing a GPtrArray of GValueArrays of (GArray of guchars) and #guint32 @@ -1402,7 +1992,7 @@ nm_utils_ip6_routes_to_gvalue (GSList *list, GValue *value) * @value: a #GValue * * Converts a #GValue containing a #GPtrArray of IP6 DNS, represented as - * #GByteArrays into a #GSList of struct in6_addrs. + * #GByteArrays into a #GSList of IP address strings. * * Returns: a #GSList of IP6 addresses. */ @@ -1416,7 +2006,7 @@ nm_utils_ip6_dns_from_gvalue (const GValue *value) dns = (GPtrArray *) g_value_get_boxed (value); for (i = 0; dns && (i < dns->len); i++) { GByteArray *bytearray = (GByteArray *) g_ptr_array_index (dns, i); - struct in6_addr *addr; + const char *str; if (bytearray->len != 16) { g_warning ("%s: ignoring invalid IP6 address of length %d", @@ -1424,9 +2014,8 @@ nm_utils_ip6_dns_from_gvalue (const GValue *value) continue; } - addr = g_malloc0 (sizeof (struct in6_addr)); - memcpy (addr->s6_addr, bytearray->data, bytearray->len); - list = g_slist_prepend (list, addr); + str = nm_utils_inet6_ntop ((struct in6_addr *) bytearray->data, NULL); + list = g_slist_prepend (list, g_strdup (str)); } return g_slist_reverse (list); @@ -1454,11 +2043,12 @@ nm_utils_ip6_dns_to_gvalue (GSList *list, GValue *value) dns = g_ptr_array_new (); for (iter = list; iter; iter = iter->next) { - struct in6_addr *addr = (struct in6_addr *) iter->data; + const char *str = iter->data; GByteArray *bytearray; - bytearray = g_byte_array_sized_new (16); - g_byte_array_append (bytearray, (guint8 *) addr->s6_addr, 16); + bytearray = g_byte_array_new (); + g_byte_array_set_size (bytearray, 16); + inet_pton (AF_INET6, str, bytearray->data); g_ptr_array_add (dns, bytearray); } @@ -1564,6 +2154,7 @@ make_key (const char *cipher, * nm_utils_rsa_key_encrypt_helper: * @cipher: cipher to use for encryption ("DES-EDE3-CBC" or "AES-128-CBC") * @data: RSA private key data to be encrypted + * @len: length of @data * @in_password: (allow-none): existing password to use, if any * @out_password: (out) (allow-none): if @in_password was %NULL, a random password will be generated * and returned in this argument @@ -1578,7 +2169,8 @@ make_key (const char *cipher, **/ static GByteArray * nm_utils_rsa_key_encrypt_helper (const char *cipher, - const GByteArray *data, + const guint8 *data, + gsize len, const char *in_password, char **out_password, GError **error) @@ -1595,7 +2187,7 @@ nm_utils_rsa_key_encrypt_helper (const char *cipher, g_return_val_if_fail (!g_strcmp0 (cipher, CIPHER_DES_EDE3_CBC) || !g_strcmp0 (cipher, CIPHER_AES_CBC), NULL); g_return_val_if_fail (data != NULL, NULL); - g_return_val_if_fail (data->len > 0, NULL); + g_return_val_if_fail (len > 0, NULL); if (out_password) g_return_val_if_fail (*out_password == NULL, NULL); @@ -1618,7 +2210,7 @@ nm_utils_rsa_key_encrypt_helper (const char *cipher, if (!key) goto out; - enc = crypto_encrypt (cipher, data, salt, salt_len, key, key_len, &enc_len, error); + enc = crypto_encrypt (cipher, data, len, salt, salt_len, key, key_len, &enc_len, error); if (!enc) goto out; @@ -1672,6 +2264,7 @@ out: /** * nm_utils_rsa_key_encrypt: * @data: RSA private key data to be encrypted + * @len: length of @data * @in_password: (allow-none): existing password to use, if any * @out_password: (out) (allow-none): if @in_password was %NULL, a random password will be generated * and returned in this argument @@ -1685,7 +2278,8 @@ out: * certificate/private key file. **/ GByteArray * -nm_utils_rsa_key_encrypt (const GByteArray *data, +nm_utils_rsa_key_encrypt (const guint8 *data, + gsize len, const char *in_password, char **out_password, GError **error) @@ -1693,7 +2287,7 @@ nm_utils_rsa_key_encrypt (const GByteArray *data, return nm_utils_rsa_key_encrypt_helper (CIPHER_DES_EDE3_CBC, - data, + data, len, in_password, out_password, error); @@ -1702,6 +2296,7 @@ nm_utils_rsa_key_encrypt (const GByteArray *data, /** * nm_utils_rsa_key_encrypt_aes: * @data: RSA private key data to be encrypted + * @len: length of @data * @in_password: (allow-none): existing password to use, if any * @out_password: (out) (allow-none): if @in_password was %NULL, a random password will be generated * and returned in this argument @@ -1715,14 +2310,15 @@ nm_utils_rsa_key_encrypt (const GByteArray *data, * certificate/private key file. **/ GByteArray * -nm_utils_rsa_key_encrypt_aes (const GByteArray *data, +nm_utils_rsa_key_encrypt_aes (const guint8 *data, + gsize len, const char *in_password, char **out_password, GError **error) { return nm_utils_rsa_key_encrypt_helper (CIPHER_AES_CBC, - data, + data, len, in_password, out_password, error); @@ -2230,6 +2826,28 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1, return !memcmp (hwaddr1, hwaddr2, hwaddr1_len); } +void +_nm_utils_hwaddr_to_dbus (const GValue *prop_value, + GValue *dbus_value) +{ + const char *str = g_value_get_string (prop_value); + GByteArray *array; + + array = str ? nm_utils_hwaddr_atoba (str, hwaddr_binary_len (str)) : NULL; + g_value_take_boxed (dbus_value, array); +} + +void +_nm_utils_hwaddr_from_dbus (const GValue *dbus_value, + GValue *prop_value) +{ + GByteArray *array = g_value_get_boxed (dbus_value); + char *str; + + str = array ? nm_utils_hwaddr_ntoa (array->data, array->len) : NULL; + g_value_take_string (prop_value, str); +} + /** * nm_utils_bin2hexstr: * @bytes: an array of bytes diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 038559d1fd..b6b9db583b 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -42,12 +42,12 @@ gboolean nm_utils_init (GError **error); void nm_utils_deinit (void); /* SSID helpers */ -gboolean nm_utils_is_empty_ssid (const guint8 *ssid, int len); -const char *nm_utils_escape_ssid (const guint8 *ssid, guint32 len); -gboolean nm_utils_same_ssid (const GByteArray *ssid1, - const GByteArray *ssid2, +gboolean nm_utils_is_empty_ssid (const guint8 *ssid, gsize len); +const char *nm_utils_escape_ssid (const guint8 *ssid, gsize len); +gboolean nm_utils_same_ssid (const guint8 *ssid1, gsize len1, + const guint8 *ssid2, gsize len2, gboolean ignore_trailing_null); -char * nm_utils_ssid_to_utf8 (const GByteArray *ssid); +char * nm_utils_ssid_to_utf8 (const guint8 *ssid, gsize len); GHashTable *nm_utils_gvalue_hash_dup (GHashTable *hash); @@ -117,11 +117,13 @@ void nm_utils_ip6_dns_to_gvalue (GSList *list, GValue *value); char *nm_utils_uuid_generate (void); char *nm_utils_uuid_generate_from_string (const char *s); -GByteArray *nm_utils_rsa_key_encrypt (const GByteArray *data, +GByteArray *nm_utils_rsa_key_encrypt (const guint8 *data, + gsize len, const char *in_password, char **out_password, GError **error); -GByteArray *nm_utils_rsa_key_encrypt_aes (const GByteArray *data, +GByteArray *nm_utils_rsa_key_encrypt_aes (const guint8 *data, + gsize len, const char *in_password, char **out_password, GError **error); diff --git a/libnm-core/nm-value-transforms.c b/libnm-core/nm-value-transforms.c index 00e7e77579..77d2609741 100644 --- a/libnm-core/nm-value-transforms.c +++ b/libnm-core/nm-value-transforms.c @@ -36,80 +36,6 @@ _nm_utils_convert_op_to_string (const GValue *src_value, GValue *dest_value) g_value_set_string (dest_value, (const char *) g_value_get_boxed (src_value)); } -static void -_nm_utils_convert_strv_to_slist (const GValue *src_value, GValue *dest_value) -{ - char **str; - GSList *list = NULL; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), G_TYPE_STRV)); - - str = (char **) g_value_get_boxed (src_value); - - while (str && str[i]) - list = g_slist_prepend (list, g_strdup (str[i++])); - - g_value_take_boxed (dest_value, g_slist_reverse (list)); -} - -static void -_nm_utils_convert_slist_to_strv (const GValue *src_value, GValue *dest_value) -{ - GSList *slist; - char **strv; - int len, i = 0; - - slist = g_value_get_boxed (src_value); - len = g_slist_length (slist); - - strv = g_new (char *, len + 1); - for (i = 0; slist; slist = slist->next, i++) - strv[i] = g_strdup (slist->data); - strv[i] = NULL; - - g_value_take_boxed (dest_value, strv); -} - -static void -_nm_utils_convert_strv_to_ptrarray (const GValue *src_value, GValue *dest_value) -{ - char **str; - GPtrArray *array = NULL; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), G_TYPE_STRV)); - - str = (char **) g_value_get_boxed (src_value); - - array = g_ptr_array_sized_new (3); - while (str && str[i]) - g_ptr_array_add (array, g_strdup (str[i++])); - - g_value_take_boxed (dest_value, array); -} - -static void -_nm_utils_convert_string_list_to_string (const GValue *src_value, GValue *dest_value) -{ - GSList *strings; - GString *printable; - GSList *iter; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_LIST_OF_STRING)); - - strings = (GSList *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - for (iter = strings; iter; iter = iter->next) { - if (iter != strings) - g_string_append_c (printable, ','); - g_string_append (printable, iter->data ? iter->data : "(null)"); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - static void _string_array_to_string (const GPtrArray *strings, GValue *dest_value) { @@ -126,17 +52,6 @@ _string_array_to_string (const GPtrArray *strings, GValue *dest_value) g_value_take_string (dest_value, g_string_free (printable, FALSE)); } -static void -_nm_utils_convert_string_array_to_string (const GValue *src_value, GValue *dest_value) -{ - const GPtrArray *strings; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_ARRAY_OF_STRING)); - - strings = (const GPtrArray *) g_value_get_boxed (src_value); - _string_array_to_string (strings, dest_value); -} - static void _nm_utils_convert_op_array_to_string (const GValue *src_value, GValue *dest_value) { @@ -148,87 +63,6 @@ _nm_utils_convert_op_array_to_string (const GValue *src_value, GValue *dest_valu _string_array_to_string (strings, dest_value); } -static void -_nm_utils_convert_uint_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GArray *array; - GString *printable; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_UINT_ARRAY)); - - array = (GArray *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - while (array && (i < array->len)) { - guint32 addr; - - if (i > 0) - g_string_append (printable, ", "); - - addr = g_array_index (array, guint32, i++); - g_string_append (printable, nm_utils_inet4_ntop (addr, NULL)); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void -_nm_utils_convert_ip4_addr_route_struct_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GPtrArray *ptr_array; - GString *printable; - guint i = 0; - char buf[INET_ADDRSTRLEN]; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT)); - - ptr_array = (GPtrArray *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - while (ptr_array && (i < ptr_array->len)) { - GArray *array; - gboolean is_addr; /* array contains address x route */ - - if (i > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); - array = (GArray *) g_ptr_array_index (ptr_array, i++); - if (array->len < 2) { - g_string_append (printable, "invalid"); - continue; - } - is_addr = (array->len < 4); - - nm_utils_inet4_ntop (g_array_index (array, guint32, 0), buf); - if (is_addr) - g_string_append_printf (printable, "ip = %s", buf); - else - g_string_append_printf (printable, "dst = %s", buf); - - g_string_append_printf (printable, "/%u", - g_array_index (array, guint32, 1)); - - if (array->len > 2) { - nm_utils_inet4_ntop (g_array_index (array, guint32, 2), buf); - if (is_addr) - g_string_append_printf (printable, ", gw = %s", buf); - else - g_string_append_printf (printable, ", nh = %s", buf); - } - - if (array->len > 3) { - g_string_append_printf (printable, ", mt = %u", - g_array_index (array, guint32, 3)); - } - - g_string_append (printable, " }"); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - static void convert_one_gvalue_hash_entry (gpointer key, gpointer value, gpointer user_data) { @@ -258,282 +92,6 @@ _nm_utils_convert_gvalue_hash_to_string (const GValue *src_value, GValue *dest_v g_string_free (printable, FALSE); } -static void -convert_one_string_hash_entry (gpointer key, gpointer value, gpointer user_data) -{ - GString *printable = (GString *) user_data; - - if (printable->len) - g_string_append_c (printable, ','); - g_string_append_printf (printable, "%s=%s", (const char *) key, (const char *) value); -} - -static void -_nm_utils_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value) -{ - GHashTable *hash; - GString *printable; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_MAP_OF_STRING)); - - hash = (GHashTable *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - if (hash) - g_hash_table_foreach (hash, convert_one_string_hash_entry, printable); - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void -_nm_utils_convert_byte_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GArray *array; - GString *printable; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_UCHAR_ARRAY)); - - array = (GArray *) g_value_get_boxed (src_value); - - printable = g_string_new ("["); - if (array) { - while (i < MIN (array->len, 35)) { - if (i > 0) - g_string_append_c (printable, ' '); - g_string_append_printf (printable, "0x%02X", - g_array_index (array, unsigned char, i++)); - } - if (i < array->len) - g_string_append (printable, " ... "); - } - g_string_append_c (printable, ']'); - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void -_nm_utils_convert_ip6_dns_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GPtrArray *ptr_array; - GString *printable; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR)); - - ptr_array = (GPtrArray *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - while (ptr_array && (i < ptr_array->len)) { - GByteArray *bytearray; - struct in6_addr *addr; - - if (i > 0) - g_string_append (printable, ", "); - - bytearray = (GByteArray *) g_ptr_array_index (ptr_array, i++); - if (bytearray->len != 16) { - g_string_append (printable, "invalid"); - continue; - } - addr = (struct in6_addr *) bytearray->data; - g_string_append (printable, nm_utils_inet6_ntop (addr, NULL)); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void -_nm_utils_convert_ip6_addr_struct_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GPtrArray *ptr_array; - GString *printable; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS)); - - ptr_array = (GPtrArray *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - while (ptr_array && (i < ptr_array->len)) { - GValueArray *elements; - GValue *tmp; - GByteArray *ba_addr; - struct in6_addr *addr; - guint32 prefix; - - if (i > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); - elements = (GValueArray *) g_ptr_array_index (ptr_array, i++); - if (!_nm_utils_gvalue_array_validate (elements, 3, - DBUS_TYPE_G_UCHAR_ARRAY, - G_TYPE_UINT, - DBUS_TYPE_G_UCHAR_ARRAY)) { - g_string_append (printable, "invalid }"); - continue; - } - - /* IPv6 address */ - tmp = g_value_array_get_nth (elements, 0); - ba_addr = g_value_get_boxed (tmp); - if (ba_addr->len != 16) { - g_string_append (printable, "invalid }"); - continue; - } - addr = (struct in6_addr *) ba_addr->data; - g_string_append_printf (printable, "ip = %s", nm_utils_inet6_ntop (addr, NULL)); - - /* Prefix */ - tmp = g_value_array_get_nth (elements, 1); - prefix = g_value_get_uint (tmp); - if (prefix > 128) { - g_string_append (printable, "/invalid }"); - continue; - } - g_string_append_printf (printable, "/%u", prefix); - g_string_append (printable, ", "); - - /* IPv6 Gateway */ - tmp = g_value_array_get_nth (elements, 2); - ba_addr = g_value_get_boxed (tmp); - if (ba_addr->len != 16) { - g_string_append (printable, "invalid }"); - continue; - } - addr = (struct in6_addr *) ba_addr->data; - g_string_append_printf (printable, "gw = %s", nm_utils_inet6_ntop (addr, NULL)); - g_string_append (printable, " }"); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -static void -_nm_utils_convert_ip6_route_struct_array_to_string (const GValue *src_value, GValue *dest_value) -{ - GPtrArray *ptr_array; - GString *printable; - guint i = 0; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE)); - - ptr_array = (GPtrArray *) g_value_get_boxed (src_value); - - printable = g_string_new (NULL); - while (ptr_array && (i < ptr_array->len)) { - GValueArray *elements; - GValue *tmp; - GByteArray *ba_addr; - struct in6_addr *addr; - guint32 prefix, metric; - - if (i > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); - elements = (GValueArray *) g_ptr_array_index (ptr_array, i++); - if (!_nm_utils_gvalue_array_validate (elements, 4, - DBUS_TYPE_G_UCHAR_ARRAY, - G_TYPE_UINT, - DBUS_TYPE_G_UCHAR_ARRAY, - G_TYPE_UINT)) { - g_string_append (printable, "invalid"); - continue; - } - - /* Destination address */ - tmp = g_value_array_get_nth (elements, 0); - ba_addr = g_value_get_boxed (tmp); - if (ba_addr->len != 16) { - g_string_append (printable, "invalid"); - continue; - } - addr = (struct in6_addr *) ba_addr->data; - g_string_append_printf (printable, "dst = %s", nm_utils_inet6_ntop (addr, NULL)); - - /* Prefix */ - tmp = g_value_array_get_nth (elements, 1); - prefix = g_value_get_uint (tmp); - if (prefix > 128) { - g_string_append (printable, "/invalid"); - continue; - } - g_string_append_printf (printable, "/%u", prefix); - g_string_append (printable, ", "); - - /* Next hop addresses */ - tmp = g_value_array_get_nth (elements, 2); - ba_addr = g_value_get_boxed (tmp); - if (ba_addr->len != 16) { - g_string_append (printable, "invalid"); - continue; - } - addr = (struct in6_addr *) ba_addr->data; - g_string_append_printf (printable, "nh = %s", nm_utils_inet6_ntop (addr, NULL)); - g_string_append (printable, ", "); - - /* Metric */ - tmp = g_value_array_get_nth (elements, 3); - metric = g_value_get_uint (tmp); - g_string_append_printf (printable, "mt = %u", metric); - - g_string_append (printable, " }"); - } - - g_value_take_string (dest_value, g_string_free (printable, FALSE)); -} - -#define OLD_DBUS_TYPE_G_IP6_ADDRESS (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID)) -#define OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS (dbus_g_type_get_collection ("GPtrArray", OLD_DBUS_TYPE_G_IP6_ADDRESS)) - -static void -_nm_utils_convert_old_ip6_addr_array (const GValue *src_value, GValue *dst_value) -{ - GPtrArray *src_outer_array; - GPtrArray *dst_outer_array; - guint i; - - g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS)); - - src_outer_array = (GPtrArray *) g_value_get_boxed (src_value); - dst_outer_array = g_ptr_array_new (); - - for (i = 0; src_outer_array && (i < src_outer_array->len); i++) { - GValueArray *src_addr_array; - GValueArray *dst_addr_array; - GValue element = G_VALUE_INIT; - GValue *src_addr, *src_prefix; - GByteArray *ba; - - src_addr_array = (GValueArray *) g_ptr_array_index (src_outer_array, i); - if (!_nm_utils_gvalue_array_validate (src_addr_array, 2, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT)) { - g_warning ("%s: invalid old IPv6 address type", __func__); - return; - } - - dst_addr_array = g_value_array_new (3); - - src_addr = g_value_array_get_nth (src_addr_array, 0); - g_value_array_append (dst_addr_array, src_addr); - src_prefix = g_value_array_get_nth (src_addr_array, 1); - g_value_array_append (dst_addr_array, src_prefix); - - /* Blank Gateway */ - g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); - ba = g_byte_array_new (); - g_byte_array_append (ba, (guint8 *) "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16); - g_value_take_boxed (&element, ba); - g_value_array_append (dst_addr_array, &element); - g_value_unset (&element); - - g_ptr_array_add (dst_outer_array, dst_addr_array); - } - - g_value_take_boxed (dst_value, dst_outer_array); -} - void _nm_value_transforms_register (void) { @@ -543,51 +101,12 @@ _nm_value_transforms_register (void) g_value_register_transform_func (DBUS_TYPE_G_OBJECT_PATH, G_TYPE_STRING, _nm_utils_convert_op_to_string); - g_value_register_transform_func (G_TYPE_STRV, - DBUS_TYPE_G_LIST_OF_STRING, - _nm_utils_convert_strv_to_slist); - g_value_register_transform_func (DBUS_TYPE_G_LIST_OF_STRING, - G_TYPE_STRV, - _nm_utils_convert_slist_to_strv); - g_value_register_transform_func (G_TYPE_STRV, - DBUS_TYPE_G_ARRAY_OF_STRING, - _nm_utils_convert_strv_to_ptrarray); - g_value_register_transform_func (DBUS_TYPE_G_LIST_OF_STRING, - G_TYPE_STRING, - _nm_utils_convert_string_list_to_string); - g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_STRING, - G_TYPE_STRING, - _nm_utils_convert_string_array_to_string); g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_OBJECT_PATH, G_TYPE_STRING, _nm_utils_convert_op_array_to_string); - g_value_register_transform_func (DBUS_TYPE_G_UINT_ARRAY, - G_TYPE_STRING, - _nm_utils_convert_uint_array_to_string); - g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, - G_TYPE_STRING, - _nm_utils_convert_ip4_addr_route_struct_array_to_string); g_value_register_transform_func (DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_STRING, _nm_utils_convert_gvalue_hash_to_string); - g_value_register_transform_func (DBUS_TYPE_G_MAP_OF_STRING, - G_TYPE_STRING, - _nm_utils_convert_string_hash_to_string); - g_value_register_transform_func (DBUS_TYPE_G_UCHAR_ARRAY, - G_TYPE_STRING, - _nm_utils_convert_byte_array_to_string); - g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR, - G_TYPE_STRING, - _nm_utils_convert_ip6_dns_array_to_string); - g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, - G_TYPE_STRING, - _nm_utils_convert_ip6_addr_struct_array_to_string); - g_value_register_transform_func (DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE, - G_TYPE_STRING, - _nm_utils_convert_ip6_route_struct_array_to_string); - g_value_register_transform_func (OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, - DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, - _nm_utils_convert_old_ip6_addr_array); registered = TRUE; } } diff --git a/libnm-core/tests/Makefile.am b/libnm-core/tests/Makefile.am index 631f958ce7..9966688689 100644 --- a/libnm-core/tests/Makefile.am +++ b/libnm-core/tests/Makefile.am @@ -13,6 +13,7 @@ AM_CPPFLAGS = \ -DTEST_CERT_DIR=\"$(certsdir)\" noinst_PROGRAMS = \ + test-compare \ test-crypto \ test-general \ test-secrets \ diff --git a/libnm-core/tests/test-compare.c b/libnm-core/tests/test-compare.c new file mode 100644 index 0000000000..5091403f02 --- /dev/null +++ b/libnm-core/tests/test-compare.c @@ -0,0 +1,396 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#include +#include +#include + +#include "nm-dbus-glib-types.h" +#include "nm-glib-compat.h" + +#include "nm-property-compare.h" + +#include "nm-test-utils.h" + +static void +compare_ints (void) +{ + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + + g_value_init (&value1, G_TYPE_INT); + g_value_init (&value2, G_TYPE_INT); + + g_value_set_int (&value1, 5); + g_value_set_int (&value2, 5); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_value_set_int (&value2, 10); + g_assert (nm_property_compare (&value1, &value2) < 0); + + g_value_set_int (&value2, 1); + g_assert (nm_property_compare (&value1, &value2) > 0); +} + +static void +compare_strings (void) +{ + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + const char *str1 = "hello"; + const char *str2 = "world"; + + g_value_init (&value1, G_TYPE_STRING); + g_value_init (&value2, G_TYPE_STRING); + + g_value_set_string (&value1, str1); + g_value_set_string (&value2, str1); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_value_set_string (&value2, str2); + g_assert (nm_property_compare (&value1, &value2) < 0); + + g_assert (nm_property_compare (&value2, &value1) > 0); +} + +static void +compare_strv (void) +{ + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + char *strv1[] = { "foo", "bar", "baz", NULL }; + char *strv2[] = { "foo", "bar", "bar", NULL }; + char *strv3[] = { "foo", "bar", NULL }; + char *strv4[] = { "foo", "bar", "baz", "bam", NULL }; + + g_value_init (&value1, G_TYPE_STRV); + g_value_init (&value2, G_TYPE_STRV); + + g_value_set_boxed (&value1, strv1); + g_value_set_boxed (&value2, strv1); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_value_set_boxed (&value2, strv2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_value_set_boxed (&value2, strv3); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_value_set_boxed (&value2, strv4); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +static void +compare_garrays (void) +{ + GArray *array1; + GArray *array2; + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + int i; + + g_value_init (&value1, DBUS_TYPE_G_UINT_ARRAY); + array1 = g_array_new (FALSE, FALSE, sizeof (guint32)); + + g_value_init (&value2, DBUS_TYPE_G_UINT_ARRAY); + array2 = g_array_new (FALSE, FALSE, sizeof (guint32)); + + for (i = 0; i < 5; i++) { + g_array_append_val (array1, i); + g_array_append_val (array2, i); + } + + g_value_set_boxed (&value1, array1); + g_value_set_boxed (&value2, array2); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_array_remove_index (array2, 0); + g_value_set_boxed (&value2, array2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + i = 7; + g_array_prepend_val (array2, i); + g_value_set_boxed (&value2, array2); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +static void +compare_ptrarrays (void) +{ + GPtrArray *array1; + GPtrArray *array2; + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + + g_value_init (&value1, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING)); + array1 = g_ptr_array_new (); + + g_value_init (&value2, dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING)); + array2 = g_ptr_array_new (); + + g_ptr_array_add (array1, "hello"); + g_ptr_array_add (array1, "world"); + g_value_set_boxed (&value1, array1); + + g_ptr_array_add (array2, "hello"); + g_ptr_array_add (array2, "world"); + g_value_set_boxed (&value2, array2); + + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_ptr_array_add (array2, "boo"); + g_value_set_boxed (&value2, array2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_ptr_array_add (array1, "booz"); + g_value_set_boxed (&value1, array1); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +static void +compare_str_hash (void) +{ + GHashTable *hash1; + GHashTable *hash2; + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + + g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)); + g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING)); + + hash1 = g_hash_table_new (g_str_hash, g_str_equal); + hash2 = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (hash1, "key1", "hello"); + g_hash_table_insert (hash1, "key2", "world"); + g_hash_table_insert (hash1, "key3", "!"); + + g_hash_table_insert (hash2, "key3", "!"); + g_hash_table_insert (hash2, "key2", "world"); + g_hash_table_insert (hash2, "key1", "hello"); + + g_value_set_boxed (&value1, hash1); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_hash_table_remove (hash2, "key2"); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_hash_table_insert (hash2, "key2", "moon"); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +static GValue * +str_to_gvalue (const char *str) +{ + GValue *value; + + value = g_slice_new0 (GValue); + g_value_init (value, G_TYPE_STRING); + g_value_set_string (value, str); + + return value; +} + +static GValue * +uint_to_gvalue (guint i) +{ + GValue *value; + + value = g_slice_new0 (GValue); + g_value_init (value, G_TYPE_UINT); + g_value_set_uint (value, i); + + return value; +} + +static GValue * +double_to_gvalue (double d) +{ + GValue *value; + + value = g_slice_new0 (GValue); + g_value_init (value, G_TYPE_DOUBLE); + g_value_set_double (value, d); + + return value; +} + +static void +compare_gvalue_hash (void) +{ + GHashTable *hash1; + GHashTable *hash2; + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + + g_value_init (&value1, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)); + g_value_init (&value2, dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)); + + hash1 = g_hash_table_new (g_str_hash, g_str_equal); + hash2 = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (hash1, "key1", str_to_gvalue ("hello")); + g_hash_table_insert (hash1, "key2", uint_to_gvalue (5)); + g_hash_table_insert (hash1, "key3", double_to_gvalue (123.456)); + + g_hash_table_insert (hash2, "key3", double_to_gvalue (123.456)); + g_hash_table_insert (hash2, "key2", uint_to_gvalue (5)); + g_hash_table_insert (hash2, "key1", str_to_gvalue ("hello")); + + g_value_set_boxed (&value1, hash1); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_hash_table_remove (hash2, "key2"); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_hash_table_insert (hash2, "key2", str_to_gvalue ("moon")); + g_value_set_boxed (&value2, hash2); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +static void +compare_ip6_addresses (void) +{ + GValueArray *array1; + GValueArray *array2; + GValueArray *array3; + GByteArray *ba1; + GByteArray *ba2; + GByteArray *ba3; + GValue element = G_VALUE_INIT; + GValue value1 = G_VALUE_INIT; + GValue value2 = G_VALUE_INIT; + struct in6_addr addr1; + struct in6_addr addr2; + struct in6_addr addr3; + guint32 prefix1 = 64; + guint32 prefix2 = 64; + guint32 prefix3 = 0; + + inet_pton (AF_INET6, "1:2:3:4:5:6:7:8", &addr1); + inet_pton (AF_INET6, "ffff:2:3:4:5:6:7:8", &addr2); + inet_pton (AF_INET6, "::", &addr3); + + /* address 1 */ + array1 = g_value_array_new (2); + + ba1 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba1, (guint8 *) addr1.s6_addr, 16); + g_value_take_boxed (&element, ba1); + g_value_array_append (array1, &element); + g_value_unset (&element); + + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, prefix1); + g_value_array_append (array1, &element); + g_value_unset (&element); + + ba1 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba1, (guint8 *) addr3.s6_addr, 16); + g_value_take_boxed (&element, ba1); + g_value_array_append (array1, &element); + g_value_unset (&element); + + /* address 2 */ + array2 = g_value_array_new (2); + + ba2 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba2, (guint8 *) addr2.s6_addr, 16); + g_value_take_boxed (&element, ba2); + g_value_array_append (array2, &element); + g_value_unset (&element); + + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, prefix2); + g_value_array_append (array2, &element); + g_value_unset (&element); + + ba2 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba2, (guint8 *) addr3.s6_addr, 16); + g_value_take_boxed (&element, ba2); + g_value_array_append (array2, &element); + g_value_unset (&element); + + /* address 3 */ + array3 = g_value_array_new (2); + + ba3 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba3, (guint8 *) addr3.s6_addr, 16); + g_value_take_boxed (&element, ba3); + g_value_array_append (array3, &element); + g_value_unset (&element); + + g_value_init (&element, G_TYPE_UINT); + g_value_set_uint (&element, prefix3); + g_value_array_append (array3, &element); + g_value_unset (&element); + + ba3 = g_byte_array_new (); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); + g_byte_array_append (ba3, (guint8 *) addr3.s6_addr, 16); + g_value_take_boxed (&element, ba3); + g_value_array_append (array3, &element); + g_value_unset (&element); + + g_value_init (&value1, DBUS_TYPE_G_IP6_ADDRESS); + g_value_init (&value2, DBUS_TYPE_G_IP6_ADDRESS); + + g_value_set_boxed (&value1, array1); + g_value_set_boxed (&value2, array1); + g_assert (nm_property_compare (&value1, &value2) == 0); + + g_value_set_boxed (&value1, array1); + g_value_set_boxed (&value2, array2); + g_assert (nm_property_compare (&value1, &value2) != 0); + + g_value_set_boxed (&value1, array1); + g_value_set_boxed (&value2, array3); + g_assert (nm_property_compare (&value1, &value2) != 0); +} + +NMTST_DEFINE (); + +int +main (int argc, char *argv[]) +{ + nmtst_init (&argc, &argv, TRUE); + + g_test_add_func ("/libnm/compare/ints", compare_ints); + g_test_add_func ("/libnm/compare/strings", compare_strings); + g_test_add_func ("/libnm/compare/strv", compare_strv); + g_test_add_func ("/libnm/compare/garrays", compare_garrays); + g_test_add_func ("/libnm/compare/ptrarrays", compare_ptrarrays); + g_test_add_func ("/libnm/compare/str_hash", compare_str_hash); + g_test_add_func ("/libnm/compare/gvalue_hash", compare_gvalue_hash); + g_test_add_func ("/libnm/compare/ip6_addresses", compare_ip6_addresses); + + return g_test_run (); +} diff --git a/libnm-core/tests/test-crypto.c b/libnm-core/tests/test-crypto.c index 677f116209..b1dd3f4e09 100644 --- a/libnm-core/tests/test-crypto.c +++ b/libnm-core/tests/test-crypto.c @@ -301,16 +301,17 @@ test_encrypt_private_key (const char *path, /* Now re-encrypt the private key */ if (is_cipher_aes (path)) - encrypted = nm_utils_rsa_key_encrypt_aes (array, password, NULL, &error); + encrypted = nm_utils_rsa_key_encrypt_aes (array->data, array->len, password, NULL, &error); else - encrypted = nm_utils_rsa_key_encrypt (array, password, NULL, &error); + encrypted = nm_utils_rsa_key_encrypt (array->data, array->len, password, NULL, &error); ASSERT (encrypted != NULL, desc, "couldn't re-encrypt private key file '%s': %d %s", path, error->code, error->message); /* Then re-decrypt the private key */ key_type = NM_CRYPTO_KEY_TYPE_UNKNOWN; - re_decrypted = crypto_decrypt_private_key_data (encrypted, password, &key_type, &error); + re_decrypted = crypto_decrypt_private_key_data (encrypted->data, encrypted->len, + password, &key_type, &error); ASSERT (re_decrypted != NULL, desc, "couldn't read private key file '%s': %d %s", path, error->code, error->message); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 44812d1cb1..f0fe4ae0a1 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -312,7 +312,7 @@ test_setting_ip4_config_labels (void) NMIP4Address *addr; const char *label; GPtrArray *addrs; - GSList *labels; + char **labels; GError *error = NULL; s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -331,7 +331,7 @@ test_setting_ip4_config_labels (void) g_assert_no_error (error); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); /* addr 2 */ addr = nm_ip4_address_new (); @@ -351,13 +351,13 @@ test_setting_ip4_config_labels (void) nm_ip4_address_set_address (addr, 0x03030303); nm_ip4_address_set_prefix (addr, 24); - _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, NULL); + _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, ""); nm_ip4_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); label = _nm_setting_ip4_config_get_address_label (s_ip4, 2); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); /* Remove addr 1 and re-verify remaining addresses */ nm_setting_ip4_config_remove_address (s_ip4, 0); @@ -372,7 +372,7 @@ test_setting_ip4_config_labels (void) addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); /* Test explicit property assignment */ @@ -388,7 +388,7 @@ test_setting_ip4_config_labels (void) g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_ADDRESSES, addrs, NULL); - g_boxed_free (DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, addrs); + g_ptr_array_unref (addrs); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); @@ -396,18 +396,18 @@ test_setting_ip4_config_labels (void) addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); /* Setting labels now will leave addresses untouched */ g_object_set (G_OBJECT (s_ip4), "address-labels", labels, NULL); - g_boxed_free (DBUS_TYPE_G_LIST_OF_STRING, labels); + g_strfreev (labels); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); @@ -420,134 +420,43 @@ test_setting_ip4_config_labels (void) addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, NULL); + g_assert_cmpstr (label, ==, ""); /* Setting labels to a value that's too short or too long will result in * the setting not verifying. */ - labels = g_slist_append (NULL, "eth0:2"); + labels = g_strsplit ("eth0:2", ",", -1); g_object_set (G_OBJECT (s_ip4), "address-labels", labels, NULL); + g_strfreev (labels); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY); g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:")); g_clear_error (&error); - labels = g_slist_append (labels, "eth0:3"); + labels = g_strsplit ("eth0:2,eth0:3", ",", -1); g_object_set (G_OBJECT (s_ip4), "address-labels", labels, NULL); + g_strfreev (labels); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - labels = g_slist_append (labels, "eth0:4"); + labels = g_strsplit ("eth0:2,eth0:3,eth0:4", ",", -1); g_object_set (G_OBJECT (s_ip4), "address-labels", labels, NULL); + g_strfreev (labels); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_error (error, NM_SETTING_IP4_CONFIG_ERROR, NM_SETTING_IP4_CONFIG_ERROR_INVALID_PROPERTY); g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:")); g_clear_error (&error); - g_object_unref (s_ip4); } -#define OLD_DBUS_TYPE_G_IP6_ADDRESS (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID)) -#define OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS (dbus_g_type_get_collection ("GPtrArray", OLD_DBUS_TYPE_G_IP6_ADDRESS)) - -/* Test that setting the IPv6 setting's 'addresses' property using the old - * IPv6 address format still works, i.e. that the GValue transformation function - * from old->new is working correctly. - */ -static void -test_setting_ip6_config_old_address_array (void) -{ - NMSettingIP6Config *s_ip6; - GPtrArray *addresses, *read_addresses; - GValueArray *array, *read_array; - GValue element = G_VALUE_INIT, written_value = G_VALUE_INIT, read_value = G_VALUE_INIT; - GByteArray *ba; - const guint8 addr[16] = { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, - 0x11, 0x22, 0x33, 0x44, 0x66, 0x77, 0x88, 0x99 }; - const guint8 gw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - guint32 prefix = 56; - GValue *read_addr, *read_prefix, *read_gw; - - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); - ASSERT (s_ip6 != NULL, - "ip6-old-addr", "error creating IP6 setting"); - - g_value_init (&written_value, OLD_DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS); - - addresses = g_ptr_array_new (); - array = g_value_array_new (3); - - /* IP address */ - g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); - ba = g_byte_array_new (); - g_byte_array_append (ba, &addr[0], sizeof (addr)); - g_value_take_boxed (&element, ba); - g_value_array_append (array, &element); - g_value_unset (&element); - - /* Prefix */ - g_value_init (&element, G_TYPE_UINT); - g_value_set_uint (&element, prefix); - g_value_array_append (array, &element); - g_value_unset (&element); - - g_ptr_array_add (addresses, array); - g_value_set_boxed (&written_value, addresses); - - /* Set the address array on the object */ - g_object_set_property (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_ADDRESSES, &written_value); - - /* Get it back so we can compare it */ - g_value_init (&read_value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS); - g_object_get_property (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_ADDRESSES, &read_value); - - ASSERT (G_VALUE_HOLDS (&read_value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS), - "ip6-old-addr", "wrong addresses property value type '%s'", - G_VALUE_TYPE_NAME (&read_value)); - - read_addresses = (GPtrArray *) g_value_get_boxed (&read_value); - ASSERT (read_addresses != NULL, - "ip6-old-addr", "missing addresses on readback"); - ASSERT (read_addresses->len == 1, - "ip6-old-addr", "expected one address on readback"); - - read_array = (GValueArray *) g_ptr_array_index (read_addresses, 0); - - read_addr = g_value_array_get_nth (read_array, 0); - ba = g_value_get_boxed (read_addr); - ASSERT (ba->len == sizeof (addr), - "ip6-old-addr", "unexpected address item length %d", ba->len); - ASSERT (memcmp (ba->data, &addr[0], sizeof (addr)) == 0, - "ip6-old-addr", "unexpected failure comparing addresses"); - - read_prefix = g_value_array_get_nth (read_array, 1); - ASSERT (g_value_get_uint (read_prefix) == prefix, - "ip6-old-addr", "unexpected failure comparing prefix"); - - /* Ensure the gateway is all zeros, which is how the 2-item to 3-item - * conversion happens. - */ - read_gw = g_value_array_get_nth (read_array, 2); - ba = g_value_get_boxed (read_gw); - ASSERT (ba->len == sizeof (gw), - "ip6-old-addr", "unexpected gateway item length %d", ba->len); - ASSERT (memcmp (ba->data, &gw[0], sizeof (gw)) == 0, - "ip6-old-addr", "unexpected failure comparing gateways"); - - g_value_unset (&written_value); - g_value_unset (&read_value); - g_object_unref (s_ip6); -} - static void test_setting_gsm_apn_spaces (void) { @@ -748,6 +657,42 @@ test_setting_to_dbus_only_secrets (void) g_object_unref (s_wsec); } +static void +test_setting_to_dbus_transform (void) +{ + NMSetting *s_wired; + GHashTable *hash; + GValue *val; + const char *test_mac_address = "11:22:33:44:55:66"; + GByteArray *dbus_mac_address; + GByteArray *cmp_mac_address; + + s_wired = nm_setting_wired_new (); + g_object_set (s_wired, + NM_SETTING_WIRED_MAC_ADDRESS, test_mac_address, + NULL); + + g_assert_cmpstr (nm_setting_wired_get_mac_address (NM_SETTING_WIRED (s_wired)), ==, test_mac_address); + + hash = _nm_setting_to_dbus (s_wired, NULL, NM_CONNECTION_SERIALIZE_ALL); + g_assert (hash != NULL); + + val = g_hash_table_lookup (hash, NM_SETTING_WIRED_MAC_ADDRESS); + g_assert (val != NULL); + g_assert (G_VALUE_HOLDS (val, DBUS_TYPE_G_UCHAR_ARRAY)); + + dbus_mac_address = g_value_get_boxed (val); + + cmp_mac_address = nm_utils_hwaddr_atoba (test_mac_address, ETH_ALEN); + + g_assert_cmpint (dbus_mac_address->len, ==, cmp_mac_address->len); + g_assert (memcmp (dbus_mac_address->data, cmp_mac_address->data, ETH_ALEN) == 0); + + g_byte_array_unref (cmp_mac_address); + g_hash_table_unref (hash); + g_object_unref (s_wired); +} + static void test_connection_to_dbus_setting_name (void) { @@ -776,7 +721,7 @@ test_connection_to_dbus_deprecated_props (void) { NMConnection *connection; NMSetting *s_wireless; - GByteArray *ssid; + GBytes *ssid; NMSettingWirelessSecurity *s_wsec; GHashTable *hash, *wireless_hash; GValue *sec_val; @@ -787,12 +732,11 @@ test_connection_to_dbus_deprecated_props (void) NULL); s_wireless = nm_setting_wireless_new (); - ssid = g_byte_array_new (); - g_byte_array_append (ssid, (const guint8 *) "1234567", 7); + ssid = g_bytes_new ("1234567", 7); g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_unref (ssid); + g_bytes_unref (ssid); nm_connection_add_setting (connection, s_wireless); /* Hash should not have an 802-11-wireless.security property */ @@ -845,6 +789,32 @@ test_setting_new_from_dbus (void) g_object_unref (s_wsec); } +static void +test_setting_new_from_dbus_transform (void) +{ + NMSetting *s_wired; + GHashTable *hash; + GValue val = { 0, }; + const char *test_mac_address = "11:22:33:44:55:66"; + GByteArray *dbus_mac_address; + GError *error = NULL; + + hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_value_unset); + + dbus_mac_address = nm_utils_hwaddr_atoba (test_mac_address, ETH_ALEN); + g_value_init (&val, DBUS_TYPE_G_UCHAR_ARRAY); + g_value_take_boxed (&val, dbus_mac_address); + g_hash_table_insert (hash, NM_SETTING_WIRED_MAC_ADDRESS, &val); + + s_wired = _nm_setting_new_from_dbus (NM_TYPE_SETTING_WIRED, hash, NULL, &error); + g_assert_no_error (error); + + g_assert_cmpstr (nm_setting_wired_get_mac_address (NM_SETTING_WIRED (s_wired)), ==, test_mac_address); + + g_hash_table_unref (hash); + g_object_unref (s_wired); +} + static NMConnection * new_test_connection (void) { @@ -991,7 +961,7 @@ test_connection_replace_settings_from_connection () gboolean success; NMSettingConnection *s_con; NMSetting *setting; - GByteArray *ssid; + GBytes *ssid; char *uuid = NULL; const char *expected_id = "Awesome connection"; @@ -1017,13 +987,12 @@ test_connection_replace_settings_from_connection () setting = nm_setting_wireless_new (); g_assert (setting); - ssid = g_byte_array_new (); - g_byte_array_append (ssid, (const guint8 *) "1234567", 7); + ssid = g_bytes_new ("1234567", 7); g_object_set (setting, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); nm_connection_add_setting (replacement, setting); /* Replace settings and test */ @@ -1164,7 +1133,7 @@ test_setting_connection_permissions_helpers (void) NMSettingConnection *s_con; gboolean success; char buf[9] = { 0x61, 0x62, 0x63, 0xff, 0xfe, 0xfd, 0x23, 0x01, 0x00 }; - GSList *list = NULL; + char **perms; const char *expected_perm = "user:" TEST_UNAME ":"; s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); @@ -1233,14 +1202,14 @@ test_setting_connection_permissions_helpers (void) check_permission (s_con, 0, TEST_UNAME, "setting-connection-permissions-helpers"); /* Check the actual GObject property just to be paranoid */ - g_object_get (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, &list, NULL); - ASSERT (list != NULL, - "setting-connection-permissions-helpers", "unexpected failure getting permissions list"); - ASSERT (g_slist_length (list) == 1, - "setting-connection-permissions-helpers", "unexpected failure getting number of permissions in list"); - ASSERT (strcmp (list->data, expected_perm) == 0, + g_object_get (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, &perms, NULL); + ASSERT (perms != NULL, + "setting-connection-permissions-helpers", "unexpected failure getting permissions"); + ASSERT (g_strv_length (perms) == 1, + "setting-connection-permissions-helpers", "unexpected failure getting number of permissions"); + ASSERT (strcmp (perms[0], expected_perm) == 0, "setting-connection-permissions-helpers", "unexpected permission property data"); - g_slist_free_full (list, g_free); + g_strfreev (perms); /* Now remove that permission and ensure we have 0 permissions */ nm_setting_connection_remove_permission (s_con, 0); @@ -1258,7 +1227,7 @@ add_permission_property (NMSettingConnection *s_con, const char *detail) { GString *str; - GSList *list = NULL; + char *perms[2]; str = g_string_sized_new (50); if (ptype) @@ -1277,11 +1246,11 @@ add_permission_property (NMSettingConnection *s_con, if (detail) g_string_append (str, detail); - list = g_slist_append (list, str->str); - g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, list, NULL); + perms[0] = str->str; + perms[1] = NULL; + g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_PERMISSIONS, perms, NULL); g_string_free (str, TRUE); - g_slist_free (list); } static void @@ -1738,8 +1707,8 @@ test_connection_good_base_types (void) NMSetting *setting; gboolean success; GError *error = NULL; - GByteArray *array; - const guint8 bdaddr[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; + GBytes *ssid; + const char *bdaddr = "11:22:33:44:55:66"; /* Try a basic wired connection */ connection = nm_simple_connection_new (); @@ -1769,13 +1738,12 @@ test_connection_good_base_types (void) add_generic_settings (connection, NM_SETTING_WIRELESS_SETTING_NAME); setting = nm_setting_wireless_new (); - array = g_byte_array_new (); - g_byte_array_append (array, (const guint8 *) "1234567", 7); + ssid = g_bytes_new ("1234567", 7); g_object_set (setting, - NM_SETTING_WIRELESS_SSID, array, + NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (array, TRUE); + g_bytes_unref (ssid); nm_connection_add_setting (connection, setting); success = nm_connection_verify (connection, &error); @@ -1788,13 +1756,10 @@ test_connection_good_base_types (void) add_generic_settings (connection, NM_SETTING_BLUETOOTH_SETTING_NAME); setting = nm_setting_bluetooth_new (); - array = g_byte_array_new (); - g_byte_array_append (array, bdaddr, sizeof (bdaddr)); g_object_set (setting, - NM_SETTING_BLUETOOTH_BDADDR, array, + NM_SETTING_BLUETOOTH_BDADDR, bdaddr, NM_SETTING_CONNECTION_TYPE, NM_SETTING_BLUETOOTH_TYPE_PANU, NULL); - g_byte_array_free (array, TRUE); nm_connection_add_setting (connection, setting); success = nm_connection_verify (connection, &error); @@ -2311,14 +2276,14 @@ test_setting_ip4_changed_signal (void) s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - ASSERT_CHANGED (nm_setting_ip4_config_add_dns (s_ip4, 0x1122)); + ASSERT_CHANGED (nm_setting_ip4_config_add_dns (s_ip4, "11.22.0.0")); ASSERT_CHANGED (nm_setting_ip4_config_remove_dns (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i <= priv->dns->len*"); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); ASSERT_UNCHANGED (nm_setting_ip4_config_remove_dns (s_ip4, 1)); g_test_assert_expected_messages (); - nm_setting_ip4_config_add_dns (s_ip4, 0x3344); + nm_setting_ip4_config_add_dns (s_ip4, "33.44.0.0"); ASSERT_CHANGED (nm_setting_ip4_config_clear_dns (s_ip4)); ASSERT_CHANGED (nm_setting_ip4_config_add_dns_search (s_ip4, "foobar.com")); @@ -2382,14 +2347,14 @@ test_setting_ip6_changed_signal (void) s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); - ASSERT_CHANGED (nm_setting_ip6_config_add_dns (s_ip6, &t)); + ASSERT_CHANGED (nm_setting_ip6_config_add_dns (s_ip6, "1:2:3::4:5:6")); ASSERT_CHANGED (nm_setting_ip6_config_remove_dns (s_ip6, 0)); g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); ASSERT_UNCHANGED (nm_setting_ip6_config_remove_dns (s_ip6, 1)); g_test_assert_expected_messages (); - nm_setting_ip6_config_add_dns (s_ip6, &t); + nm_setting_ip6_config_add_dns (s_ip6, "1:2:3::4:5:6"); ASSERT_CHANGED (nm_setting_ip6_config_clear_dns (s_ip6)); ASSERT_CHANGED (nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com")); @@ -2861,13 +2826,11 @@ static NMSetting * _add_setting_fcn_bluetooth (NMConnection *con) { NMSetting *setting; - GByteArray *bdaddr = nm_utils_hwaddr_atoba ("11:22:33:44:55:66", ETH_ALEN); setting = g_object_new (NM_TYPE_SETTING_BLUETOOTH, - NM_SETTING_BLUETOOTH_BDADDR, bdaddr, + NM_SETTING_BLUETOOTH_BDADDR, "11:22:33:44:55:66", NM_SETTING_BLUETOOTH_TYPE, NM_SETTING_BLUETOOTH_TYPE_PANU, NULL); - g_byte_array_free (bdaddr, TRUE); nm_connection_add_setting (con, setting); return setting; @@ -2942,14 +2905,14 @@ _add_setting_fcn_olpc_mesh (NMConnection *con) { NMSetting *setting; const char *ssid_data = "ssid-test"; - GByteArray *ssid = g_byte_array_new (); + GBytes *ssid; - g_byte_array_append (ssid, (const guint8 *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); setting = g_object_new (NM_TYPE_SETTING_OLPC_MESH, NM_SETTING_OLPC_MESH_SSID, ssid, NM_SETTING_OLPC_MESH_CHANNEL, 1, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); nm_connection_add_setting (con, setting); return setting; @@ -3018,13 +2981,13 @@ _add_setting_fcn_wireless (NMConnection *con) { NMSetting *setting; const char *ssid_data = "ssid-test"; - GByteArray *ssid = g_byte_array_new (); + GBytes *ssid; - g_byte_array_append (ssid, (const guint8 *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); setting = g_object_new (NM_TYPE_SETTING_WIRELESS, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); nm_connection_add_setting (con, setting); return setting; @@ -3033,12 +2996,9 @@ _add_setting_fcn_wireless (NMConnection *con) static void _prepare_normalizable_fcn_vlan (NMConnection *con) { - GByteArray *mac_addr = nm_utils_hwaddr_atoba ("11:22:33:44:55:66", ETH_ALEN); - nm_connection_add_setting (con, g_object_new (NM_TYPE_SETTING_WIRED, - NM_SETTING_WIRED_MAC_ADDRESS, mac_addr, + NM_SETTING_WIRED_MAC_ADDRESS, "11:22:33:44:55:66", NULL)); - g_byte_array_free (mac_addr, TRUE); } static void @@ -3195,7 +3155,6 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_setting_vpn_update_secrets", test_setting_vpn_update_secrets); g_test_add_func ("/core/general/test_setting_vpn_modify_during_foreach", test_setting_vpn_modify_during_foreach); g_test_add_func ("/core/general/test_setting_ip4_config_labels", test_setting_ip4_config_labels); - g_test_add_func ("/core/general/test_setting_ip6_config_old_address_array", test_setting_ip6_config_old_address_array); g_test_add_func ("/core/general/test_setting_gsm_apn_spaces", test_setting_gsm_apn_spaces); g_test_add_func ("/core/general/test_setting_gsm_apn_bad_chars", test_setting_gsm_apn_bad_chars); g_test_add_func ("/core/general/test_setting_gsm_apn_underscore", test_setting_gsm_apn_underscore); @@ -3203,6 +3162,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_setting_to_dbus_all", test_setting_to_dbus_all); g_test_add_func ("/core/general/test_setting_to_dbus_no_secrets", test_setting_to_dbus_no_secrets); g_test_add_func ("/core/general/test_setting_to_dbus_only_secrets", test_setting_to_dbus_only_secrets); + g_test_add_func ("/core/general/test_setting_to_dbus_transform", test_setting_to_dbus_transform); g_test_add_func ("/core/general/test_setting_compare_id", test_setting_compare_id); #define ADD_FUNC(func, secret_flags, comp_flags, remove_secret) \ g_test_add_data_func_full ("/core/general/" G_STRINGIFY (func), \ @@ -3221,6 +3181,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_connection_to_dbus_setting_name", test_connection_to_dbus_setting_name); g_test_add_func ("/core/general/test_connection_to_dbus_deprecated_props", test_connection_to_dbus_deprecated_props); g_test_add_func ("/core/general/test_setting_new_from_dbus", test_setting_new_from_dbus); + g_test_add_func ("/core/general/test_setting_new_from_dbus_transform", test_setting_new_from_dbus_transform); g_test_add_func ("/core/general/test_connection_replace_settings", test_connection_replace_settings); g_test_add_func ("/core/general/test_connection_replace_settings_from_connection", test_connection_replace_settings_from_connection); g_test_add_func ("/core/general/test_connection_replace_settings_bad", test_connection_replace_settings_bad); diff --git a/libnm-core/tests/test-secrets.c b/libnm-core/tests/test-secrets.c index 087a30f0cc..221fc76028 100644 --- a/libnm-core/tests/test-secrets.c +++ b/libnm-core/tests/test-secrets.c @@ -399,7 +399,7 @@ wifi_connection_new (void) NMSettingWirelessSecurity *s_wsec; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; char *uuid; - GByteArray *ssid; + GBytes *ssid; connection = nm_simple_connection_new (); g_assert (connection); @@ -422,12 +422,11 @@ wifi_connection_new (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); g_assert (s_wifi); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); /* Wifi security */ diff --git a/libnm-core/tests/test-setting-8021x.c b/libnm-core/tests/test-setting-8021x.c index 7b66b8eb50..a0c006de51 100644 --- a/libnm-core/tests/test-setting-8021x.c +++ b/libnm-core/tests/test-setting-8021x.c @@ -32,7 +32,7 @@ static void compare_blob_data (const char *test, const char *key_path, - const GByteArray *key) + GBytes *key) { char *contents = NULL; gsize len = 0; @@ -45,11 +45,11 @@ compare_blob_data (const char *test, ASSERT (len > 0, test, "blob key file invalid (size 0)"); - ASSERT (len == key->len, + ASSERT (len == g_bytes_get_size (key), test, "blob key file (%d) and setting key data (%d) lengths don't match", - len, key->len); + len, g_bytes_get_size (key)); - ASSERT (memcmp (contents, key->data, len) == 0, + ASSERT (memcmp (contents, g_bytes_get_data (key, NULL), len) == 0, test, "blob key file and blob key data don't match"); g_free (contents); @@ -58,9 +58,9 @@ compare_blob_data (const char *test, #define SCHEME_PATH "file://" static void -check_scheme_path (GByteArray *value, const char *path) +check_scheme_path (GBytes *value, const char *path) { - guint8 *p = value->data; + const guint8 *p = g_bytes_get_data (value, NULL); g_assert (memcmp (p, SCHEME_PATH, strlen (SCHEME_PATH)) == 0); p += strlen (SCHEME_PATH); @@ -79,7 +79,7 @@ test_private_key_import (const char *path, NMSetting8021xCKFormat format = NM_SETTING_802_1X_CK_FORMAT_UNKNOWN; NMSetting8021xCKFormat tmp_fmt; GError *error = NULL; - GByteArray *tmp_key = NULL, *client_cert = NULL; + GBytes *tmp_key = NULL, *client_cert = NULL; const char *pw; s_8021x = (NMSetting8021x *) nm_setting_802_1x_new (); @@ -108,14 +108,14 @@ test_private_key_import (const char *path, "private-key-import", "failed to compare private key password"); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { - tmp_key = (GByteArray *) nm_setting_802_1x_get_private_key_blob (s_8021x); + tmp_key = nm_setting_802_1x_get_private_key_blob (s_8021x); ASSERT (tmp_key != NULL, "private-key-import", "missing private key blob"); compare_blob_data ("private-key-import", path, tmp_key); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) { g_object_get (s_8021x, NM_SETTING_802_1X_PRIVATE_KEY, &tmp_key, NULL); ASSERT (tmp_key != NULL, "private-key-import", "missing private key value"); check_scheme_path (tmp_key, path); - g_byte_array_free (tmp_key, TRUE); + g_bytes_unref (tmp_key); } else g_assert_not_reached (); @@ -128,13 +128,11 @@ test_private_key_import (const char *path, ASSERT (client_cert != NULL, "private-key-import", "missing client certificate value"); /* make sure they are the same */ - ASSERT (tmp_key->len == client_cert->len, - "private-key-import", "unexpected different private key and client cert lengths"); - ASSERT (memcmp (tmp_key->data, client_cert->data, tmp_key->len) == 0, + ASSERT (g_bytes_equal (tmp_key, client_cert), "private-key-import", "unexpected different private key and client cert data"); - g_byte_array_free (tmp_key, TRUE); - g_byte_array_free (client_cert, TRUE); + g_bytes_unref (tmp_key); + g_bytes_unref (client_cert); } g_object_unref (s_8021x); @@ -150,7 +148,7 @@ test_phase2_private_key_import (const char *path, NMSetting8021xCKFormat format = NM_SETTING_802_1X_CK_FORMAT_UNKNOWN; NMSetting8021xCKFormat tmp_fmt; GError *error = NULL; - GByteArray *tmp_key = NULL, *client_cert = NULL; + GBytes *tmp_key = NULL, *client_cert = NULL; const char *pw; s_8021x = (NMSetting8021x *) nm_setting_802_1x_new (); @@ -179,7 +177,7 @@ test_phase2_private_key_import (const char *path, "phase2-private-key-import", "failed to compare private key password"); if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { - tmp_key = (GByteArray *) nm_setting_802_1x_get_phase2_private_key_blob (s_8021x); + tmp_key = nm_setting_802_1x_get_phase2_private_key_blob (s_8021x); ASSERT (tmp_key != NULL, "phase2-private-key-import", "missing private key blob"); compare_blob_data ("phase2-private-key-import", path, tmp_key); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) { @@ -198,13 +196,11 @@ test_phase2_private_key_import (const char *path, ASSERT (client_cert != NULL, "private-key-import", "missing client certificate value"); /* make sure they are the same */ - ASSERT (tmp_key->len == client_cert->len, - "private-key-import", "unexpected different private key and client cert lengths"); - ASSERT (memcmp (tmp_key->data, client_cert->data, tmp_key->len) == 0, + ASSERT (g_bytes_equal (tmp_key, client_cert), "private-key-import", "unexpected different private key and client cert data"); - g_byte_array_free (tmp_key, TRUE); - g_byte_array_free (client_cert, TRUE); + g_bytes_unref (tmp_key); + g_bytes_unref (client_cert); } g_object_unref (s_8021x); diff --git a/libnm/nm-access-point.c b/libnm/nm-access-point.c index 652cb6a892..650a567c18 100644 --- a/libnm/nm-access-point.c +++ b/libnm/nm-access-point.c @@ -236,9 +236,11 @@ nm_access_point_connection_valid (NMAccessPoint *ap, NMConnection *connection) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; const char *ctype, *ap_bssid; - const GByteArray *setting_ssid; + GBytes *setting_ssid; + const guint8 *setting_ssid_data; + gsize setting_ssid_len; const GByteArray *ap_ssid; - const GByteArray *setting_bssid; + const char *setting_bssid; const char *setting_mode; NM80211Mode ap_mode; const char *setting_band; @@ -258,9 +260,12 @@ nm_access_point_connection_valid (NMAccessPoint *ap, NMConnection *connection) ap_ssid = nm_access_point_get_ssid (ap); g_warn_if_fail (ap_ssid != NULL); setting_ssid = nm_setting_wireless_get_ssid (s_wifi); - if (!setting_ssid || !ap_ssid || (setting_ssid->len != ap_ssid->len)) + if (!setting_ssid || !ap_ssid) return FALSE; - if (memcmp (setting_ssid->data, ap_ssid->data, ap_ssid->len) != 0) + setting_ssid_data = g_bytes_get_data (setting_ssid, &setting_ssid_len); + if (setting_ssid_len != ap_ssid->len) + return FALSE; + if (memcmp (setting_ssid_data, ap_ssid->data, ap_ssid->len) != 0) return FALSE; /* BSSID checks */ @@ -268,7 +273,7 @@ nm_access_point_connection_valid (NMAccessPoint *ap, NMConnection *connection) g_warn_if_fail (ap_bssid); setting_bssid = nm_setting_wireless_get_bssid (s_wifi); if (setting_bssid && ap_bssid) { - if (!nm_utils_hwaddr_matches (ap_bssid, -1, setting_bssid->data, setting_bssid->len)) + if (!nm_utils_hwaddr_matches (ap_bssid, -1, setting_bssid, -1)) return FALSE; } diff --git a/libnm/nm-device-bt.c b/libnm/nm-device-bt.c index 40fc767097..925442e524 100644 --- a/libnm/nm-device-bt.c +++ b/libnm/nm-device-bt.c @@ -146,8 +146,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingConnection *s_con; NMSettingBluetooth *s_bt; const char *ctype; - const GByteArray *mac; - const char *hw_addr; + const char *hw_addr, *setting_addr; NMBluetoothCapabilities dev_caps; NMBluetoothCapabilities bt_type; @@ -176,8 +175,8 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro "Invalid device MAC address."); return FALSE; } - mac = nm_setting_bluetooth_get_bdaddr (s_bt); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, hw_addr, -1)) { + setting_addr = nm_setting_bluetooth_get_bdaddr (s_bt); + if (setting_addr && !nm_utils_hwaddr_matches (setting_addr, -1, hw_addr, -1)) { g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MAC_MISMATCH, "The MACs of the device and the connection didn't match."); return FALSE; diff --git a/libnm/nm-device-ethernet.c b/libnm/nm-device-ethernet.c index 65906913ff..118cda17d6 100644 --- a/libnm/nm-device-ethernet.c +++ b/libnm/nm-device-ethernet.c @@ -168,8 +168,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } if (s_wired) { - const GByteArray *mac; - const char *perm_addr; + const char *perm_addr, *setting_addr; /* FIXME: filter using s390 subchannels when they are exported over the bus */ @@ -181,8 +180,8 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro "Invalid device MAC address."); return FALSE; } - mac = nm_setting_wired_get_mac_address (s_wired); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, perm_addr, -1)) { + setting_addr = nm_setting_wired_get_mac_address (s_wired); + if (setting_addr && !nm_utils_hwaddr_matches (setting_addr, -1, perm_addr, -1)) { g_set_error (error, NM_DEVICE_ETHERNET_ERROR, NM_DEVICE_ETHERNET_ERROR_MAC_MISMATCH, "The MACs of the device and the connection didn't match."); return FALSE; diff --git a/libnm/nm-device-infiniband.c b/libnm/nm-device-infiniband.c index c196b0b84b..478c6e8c18 100644 --- a/libnm/nm-device-infiniband.c +++ b/libnm/nm-device-infiniband.c @@ -105,8 +105,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro { NMSettingConnection *s_con; NMSettingInfiniband *s_infiniband; - const char *ctype, *hwaddr; - const GByteArray *mac; + const char *ctype, *hwaddr, *setting_hwaddr; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -132,9 +131,9 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro "Invalid device MAC address."); return FALSE; } - mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, hwaddr, -1)) { + setting_hwaddr = nm_setting_infiniband_get_mac_address (s_infiniband); + if (setting_hwaddr && !nm_utils_hwaddr_matches (setting_hwaddr, -1, hwaddr, -1)) { g_set_error (error, NM_DEVICE_INFINIBAND_ERROR, NM_DEVICE_INFINIBAND_ERROR_MAC_MISMATCH, "The MACs of the device and the connection didn't match."); return FALSE; diff --git a/libnm/nm-device-vlan.c b/libnm/nm-device-vlan.c index e94e580582..b0deb1b089 100644 --- a/libnm/nm-device-vlan.c +++ b/libnm/nm-device-vlan.c @@ -123,8 +123,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingVlan *s_vlan; NMSettingWired *s_wired; const char *ctype, *dev_iface_name, *vlan_iface_name; - const GByteArray *mac_address; - char *mac_address_str; + const char *setting_hwaddr; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -159,16 +158,15 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro s_wired = nm_connection_get_setting_wired (connection); if (s_wired) - mac_address = nm_setting_wired_get_mac_address (s_wired); + setting_hwaddr = nm_setting_wired_get_mac_address (s_wired); else - mac_address = NULL; - if (mac_address) { - mac_address_str = nm_utils_hwaddr_ntoa (mac_address->data, mac_address->len); - if (!g_strcmp0 (mac_address_str, NM_DEVICE_VLAN_GET_PRIVATE (device)->hw_address)) { + setting_hwaddr = NULL; + if (setting_hwaddr) { + if (!nm_utils_hwaddr_matches (setting_hwaddr, -1, + NM_DEVICE_VLAN_GET_PRIVATE (device)->hw_address, -1)) { g_set_error (error, NM_DEVICE_VLAN_ERROR, NM_DEVICE_VLAN_ERROR_MAC_MISMATCH, "The hardware address of the device and the connection didn't match."); } - g_free (mac_address_str); } return NM_DEVICE_CLASS (nm_device_vlan_parent_class)->connection_compatible (device, connection, error); diff --git a/libnm/nm-device-wifi.c b/libnm/nm-device-wifi.c index d9a14efdee..f06232c374 100644 --- a/libnm/nm-device-wifi.c +++ b/libnm/nm-device-wifi.c @@ -418,8 +418,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; const char *ctype; - const GByteArray *mac; - const char *hw_addr; + const char *hwaddr, *setting_hwaddr; NMDeviceWifiCapabilities wifi_caps; const char *key_mgmt; @@ -441,15 +440,15 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } /* Check MAC address */ - hw_addr = nm_device_wifi_get_permanent_hw_address (NM_DEVICE_WIFI (device)); - if (hw_addr) { - if (!nm_utils_hwaddr_valid (hw_addr, ETH_ALEN)) { + hwaddr = nm_device_wifi_get_permanent_hw_address (NM_DEVICE_WIFI (device)); + if (hwaddr) { + if (!nm_utils_hwaddr_valid (hwaddr, ETH_ALEN)) { g_set_error (error, NM_DEVICE_WIFI_ERROR, NM_DEVICE_WIFI_ERROR_INVALID_DEVICE_MAC, "Invalid device MAC address."); return FALSE; } - mac = nm_setting_wireless_get_mac_address (s_wifi); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, hw_addr, -1)) { + setting_hwaddr = nm_setting_wireless_get_mac_address (s_wifi); + if (setting_hwaddr && !nm_utils_hwaddr_matches (setting_hwaddr, -1, hwaddr, -1)) { g_set_error (error, NM_DEVICE_WIFI_ERROR, NM_DEVICE_WIFI_ERROR_MAC_MISMATCH, "The MACs of the device and the connection didn't match."); return FALSE; diff --git a/libnm/nm-device-wimax.c b/libnm/nm-device-wimax.c index f842b83c89..72b25c2b6d 100644 --- a/libnm/nm-device-wimax.c +++ b/libnm/nm-device-wimax.c @@ -322,8 +322,7 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro NMSettingConnection *s_con; NMSettingWimax *s_wimax; const char *ctype; - const GByteArray *mac; - const char *hw_addr; + const char *hwaddr, *setting_hwaddr; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -343,15 +342,15 @@ connection_compatible (NMDevice *device, NMConnection *connection, GError **erro } /* Check MAC address */ - hw_addr = nm_device_wimax_get_hw_address (NM_DEVICE_WIMAX (device)); - if (hw_addr) { - if (!nm_utils_hwaddr_valid (hw_addr, ETH_ALEN)) { + hwaddr = nm_device_wimax_get_hw_address (NM_DEVICE_WIMAX (device)); + if (hwaddr) { + if (!nm_utils_hwaddr_valid (hwaddr, ETH_ALEN)) { g_set_error (error, NM_DEVICE_WIMAX_ERROR, NM_DEVICE_WIMAX_ERROR_INVALID_DEVICE_MAC, "Invalid device MAC address."); return FALSE; } - mac = nm_setting_wimax_get_mac_address (s_wimax); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, hw_addr, -1)) { + setting_hwaddr = nm_setting_wimax_get_mac_address (s_wimax); + if (setting_hwaddr && !nm_utils_hwaddr_matches (setting_hwaddr, -1, hwaddr, -1)) { g_set_error (error, NM_DEVICE_WIMAX_ERROR, NM_DEVICE_WIMAX_ERROR_MAC_MISMATCH, "The MACs of the device and the connection didn't match."); return FALSE; diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 827bc55ab4..8320862efb 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -63,30 +63,37 @@ * */ gboolean -nm_ethernet_address_is_valid (const guint8 *test_addr) +nm_ethernet_address_is_valid (gconstpointer addr, gsize len) { guint8 invalid_addr1[ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; guint8 invalid_addr2[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; guint8 invalid_addr3[ETH_ALEN] = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44}; guint8 invalid_addr4[ETH_ALEN] = {0x00, 0x30, 0xb4, 0x00, 0x00, 0x00}; /* prism54 dummy MAC */ + guchar first_octet; - g_return_val_if_fail (test_addr != NULL, FALSE); + g_return_val_if_fail (addr != NULL, FALSE); + g_return_val_if_fail (len == ETH_ALEN || len == -1, FALSE); /* Compare the AP address the card has with invalid ethernet MAC addresses. */ - if (!memcmp (test_addr, invalid_addr1, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, len, invalid_addr1, ETH_ALEN)) return FALSE; - if (!memcmp (test_addr, invalid_addr2, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, len, invalid_addr2, ETH_ALEN)) return FALSE; - if (!memcmp (test_addr, invalid_addr3, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, len, invalid_addr3, ETH_ALEN)) return FALSE; - if (!memcmp (test_addr, invalid_addr4, ETH_ALEN)) + if (nm_utils_hwaddr_matches (addr, len, invalid_addr4, ETH_ALEN)) return FALSE; - if (test_addr[0] & 1) /* Multicast addresses */ - return FALSE; + /* Check for multicast address */ + if (len == -1) + first_octet = strtoul (addr, NULL, 16); + else + first_octet = ((guint8 *)addr)[0]; + if (first_octet & 0x01) + return FALSE; return TRUE; } @@ -1237,7 +1244,7 @@ check_connection_mac_address (NMConnection *orig, GHashTable *settings) { GHashTable *props; - const GByteArray *orig_mac = NULL, *cand_mac = NULL; + const char *orig_mac = NULL, *cand_mac = NULL; NMSettingWired *s_wired_orig, *s_wired_cand; props = check_property_in_hash (settings, diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 1a9859f003..3d0753d987 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -28,7 +28,7 @@ #include "nm-connection.h" #include "nm-platform.h" -gboolean nm_ethernet_address_is_valid (const guint8 *test_addr); +gboolean nm_ethernet_address_is_valid (gconstpointer addr, gsize len); in_addr_t nm_utils_ip4_address_clear_host_address (in_addr_t addr, guint8 plen); void nm_utils_ip6_address_clear_host_address (struct in6_addr *dst, const struct in6_addr *src, guint8 plen); diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index fb596321df..8e6091150c 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -54,7 +54,6 @@ typedef struct { NMBluetoothCapabilities connection_bt_type; char *address; - guint8 bin_address[ETH_ALEN]; char *name; guint32 capabilities; gboolean connected; @@ -166,7 +165,6 @@ pan_connection_check_create (NMBluezDevice *self) NMConnection *added; NMSetting *setting; char *uuid, *id; - GByteArray *bdaddr_array; GError *error = NULL; NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); @@ -200,15 +198,12 @@ pan_connection_check_create (NMBluezDevice *self) nm_connection_add_setting (connection, setting); /* Setting: Bluetooth */ - bdaddr_array = g_byte_array_sized_new (sizeof (priv->bin_address)); - g_byte_array_append (bdaddr_array, priv->bin_address, sizeof (priv->bin_address)); setting = nm_setting_bluetooth_new (); g_object_set (G_OBJECT (setting), - NM_SETTING_BLUETOOTH_BDADDR, bdaddr_array, + NM_SETTING_BLUETOOTH_BDADDR, priv->address, NM_SETTING_BLUETOOTH_TYPE, NM_SETTING_BLUETOOTH_TYPE_PANU, NULL); nm_connection_add_setting (connection, setting); - g_byte_array_free (bdaddr_array, TRUE); /* Setting: IPv4 */ setting = nm_setting_ip4_config_new (); @@ -300,7 +295,7 @@ connection_compatible (NMBluezDevice *self, NMConnection *connection) NMBluezDevicePrivate *priv = NM_BLUEZ_DEVICE_GET_PRIVATE (self); NMSettingBluetooth *s_bt; const char *bt_type; - const GByteArray *bdaddr; + const char *bdaddr; if (!nm_connection_is_type (connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) return FALSE; @@ -309,14 +304,13 @@ connection_compatible (NMBluezDevice *self, NMConnection *connection) if (!s_bt) return FALSE; - if (!priv->address) { - /* unless address is set, bin_address is not initialized. */ + if (!priv->address) return FALSE; - } + bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); if (!bdaddr) return FALSE; - if (!nm_utils_hwaddr_matches (bdaddr->data, bdaddr->len, priv->bin_address, ETH_ALEN)) + if (!nm_utils_hwaddr_matches (bdaddr, -1, priv->address, -1)) return FALSE; bt_type = nm_setting_bluetooth_get_connection_type (s_bt); @@ -611,8 +605,6 @@ _set_property_capabilities (NMBluezDevice *self, const char **uuids) /** * priv->address can only be set one to a certain (non NULL) value. Every later attempt * to reset it to another value will be ignored and a warning will be logged. - * - * When setting the address for the first time, we also set bin_address. **/ static void _set_property_address (NMBluezDevice *self, const char *addr) @@ -632,13 +624,11 @@ _set_property_address (NMBluezDevice *self, const char *addr) return; } - if (!nm_utils_hwaddr_aton (addr, priv->bin_address, ETH_ALEN)) { - if (priv->address) - nm_log_warn (LOGD_BT, "bluez[%s] cannot reset address from '%s' to '%s' (invalid value)", priv->path, priv->address, addr); - else - nm_log_warn (LOGD_BT, "bluez[%s] cannot reset address from NULL to '%s' (invalid value)", priv->path, addr); + if (!nm_utils_hwaddr_valid (addr, ETH_ALEN)) { + nm_log_warn (LOGD_BT, "bluez[%s] cannot set address to '%s' (invalid value)", priv->path, addr); return; } + priv->address = g_strdup (addr); g_object_notify (G_OBJECT (self), NM_BLUEZ_DEVICE_ADDRESS); } diff --git a/src/devices/bluetooth/nm-device-bt.c b/src/devices/bluetooth/nm-device-bt.c index 9edcf28cdb..e08064b120 100644 --- a/src/devices/bluetooth/nm-device-bt.c +++ b/src/devices/bluetooth/nm-device-bt.c @@ -158,7 +158,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); NMSettingConnection *s_con; NMSettingBluetooth *s_bt; - const GByteArray *array; + const char *bdaddr; guint32 bt_type; if (!NM_DEVICE_CLASS (nm_device_bt_parent_class)->check_connection_compatible (device, connection)) @@ -178,10 +178,10 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) if (!(bt_type & priv->capabilities)) return FALSE; - array = nm_setting_bluetooth_get_bdaddr (s_bt); - if (!array) + bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); + if (!bdaddr) return FALSE; - if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, array->data, array->len)) + if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, bdaddr, -1)) return FALSE; return TRUE; @@ -215,7 +215,7 @@ complete_connection (NMDevice *device, { NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device); NMSettingBluetooth *s_bt; - const GByteArray *setting_bdaddr; + const char *setting_bdaddr; const char *ctype; gboolean is_dun = FALSE, is_pan = FALSE; NMSettingGsm *s_gsm; @@ -324,7 +324,7 @@ complete_connection (NMDevice *device, setting_bdaddr = nm_setting_bluetooth_get_bdaddr (s_bt); if (setting_bdaddr) { /* Make sure the setting BT Address (if any) matches the device's */ - if (!nm_utils_hwaddr_matches (setting_bdaddr->data, setting_bdaddr->len, priv->bdaddr, -1)) { + if (!nm_utils_hwaddr_matches (setting_bdaddr, -1, priv->bdaddr, -1)) { g_set_error_literal (error, NM_SETTING_BLUETOOTH_ERROR, NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY, @@ -332,14 +332,9 @@ complete_connection (NMDevice *device, return FALSE; } } else { - GByteArray *bdaddr; - /* Lock the connection to this device by default */ - if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, NULL, ETH_ALEN)) { - bdaddr = nm_utils_hwaddr_atoba (priv->bdaddr, ETH_ALEN); - g_object_set (G_OBJECT (s_bt), NM_SETTING_BLUETOOTH_BDADDR, bdaddr, NULL); - g_byte_array_free (bdaddr, TRUE); - } + if (!nm_utils_hwaddr_matches (priv->bdaddr, -1, NULL, ETH_ALEN)) + g_object_set (G_OBJECT (s_bt), NM_SETTING_BLUETOOTH_BDADDR, priv->bdaddr, NULL); } return TRUE; diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index 63bde86075..76a9c89bcd 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -101,7 +101,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) { const char *iface; NMSettingBridge *s_bridge; - const GByteArray *mac_address; + const char *mac_address; if (!NM_DEVICE_CLASS (nm_device_bridge_parent_class)->check_connection_compatible (device, connection)) return FALSE; @@ -120,8 +120,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) const char *hw_addr; hw_addr = nm_device_get_hw_address (device); - if ( !hw_addr - || !nm_utils_hwaddr_matches (hw_addr, -1, mac_address->data, mac_address->len)) + if (!hw_addr || !nm_utils_hwaddr_matches (hw_addr, -1, mac_address, -1)) return FALSE; } @@ -424,7 +423,8 @@ nm_device_bridge_new_for_connection (NMConnection *connection) { const char *iface; NMSettingBridge *s_bridge; - const GByteArray *mac_address; + const char *mac_address_str; + guint8 mac_address[NM_UTILS_HWADDR_LEN_MAX]; g_return_val_if_fail (connection != NULL, NULL); @@ -434,11 +434,15 @@ nm_device_bridge_new_for_connection (NMConnection *connection) s_bridge = nm_connection_get_setting_bridge (connection); g_return_val_if_fail (s_bridge, NULL); - mac_address = nm_setting_bridge_get_mac_address (s_bridge); + mac_address_str = nm_setting_bridge_get_mac_address (s_bridge); + if (mac_address_str) { + if (!nm_utils_hwaddr_aton (mac_address_str, mac_address, ETH_ALEN)) + mac_address_str = NULL; + } if ( !nm_platform_bridge_add (iface, - mac_address ? mac_address->data : NULL, - mac_address ? mac_address->len : 0) + mac_address_str ? mac_address : NULL, + mac_address_str ? ETH_ALEN : 0) && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { nm_log_warn (LOGD_DEVICE | LOGD_BRIDGE, "(%s): failed to create bridge master interface for '%s': %s", iface, nm_connection_get_id (connection), diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 5b27999c8c..d657c07480 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -101,8 +101,8 @@ typedef enum { } DcbWait; typedef struct { - char * perm_hw_addr; /* Permanent MAC address */ - guint8 initial_hw_addr[ETH_ALEN]; /* Initial MAC address (as seen when NM starts) */ + char * perm_hw_addr; /* Permanent MAC address */ + char * initial_hw_addr; /* Initial MAC address (as seen when NM starts) */ guint32 speed; @@ -361,7 +361,7 @@ update_permanent_hw_address (NMDevice *dev) errno = 0; ret = ioctl (fd, SIOCETHTOOL, &req); errsv = errno; - if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data)) { + if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data, ETH_ALEN)) { _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)", errsv); /* Fall back to current address */ mac = nm_device_get_hw_address (dev); @@ -382,16 +382,13 @@ update_initial_hw_address (NMDevice *dev) { NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev); NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); - const char *mac; /* This sets initial MAC address from current MAC address. It should only * be called from NMDevice constructor() to really get the initial address. */ - mac = nm_device_get_hw_address (dev); - if (mac) - nm_utils_hwaddr_aton (mac, priv->initial_hw_addr, ETH_ALEN); + priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (dev)); - _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", mac); + _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", priv->initial_hw_addr); } static guint32 @@ -412,7 +409,7 @@ static gboolean match_subchans (NMDeviceEthernet *self, NMSettingWired *s_wired, gboolean *try_mac) { NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); - const GPtrArray *subchans; + const char * const *subchans; int i; *try_mac = TRUE; @@ -426,8 +423,8 @@ match_subchans (NMDeviceEthernet *self, NMSettingWired *s_wired, gboolean *try_m return FALSE; /* Make sure each subchannel in the connection is a subchannel of this device */ - for (i = 0; i < subchans->len; i++) { - const char *candidate = g_ptr_array_index (subchans, i); + for (i = 0; subchans[i]; i++) { + const char *candidate = subchans[i]; if ( (priv->subchan1 && !strcmp (priv->subchan1, candidate)) || (priv->subchan2 && !strcmp (priv->subchan2, candidate)) @@ -462,7 +459,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; if (s_wired) { - const GByteArray *mac; + const char *mac; gboolean try_mac = TRUE; const GSList *mac_blacklist, *mac_blacklist_iter; @@ -470,21 +467,19 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wired_get_mac_address (s_wired); - if (try_mac && mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, -1)) + if (try_mac && mac && !nm_utils_hwaddr_matches (mac, -1, priv->perm_hw_addr, -1)) return FALSE; /* Check for MAC address blacklist */ mac_blacklist = nm_setting_wired_get_mac_address_blacklist (s_wired); for (mac_blacklist_iter = mac_blacklist; mac_blacklist_iter; mac_blacklist_iter = g_slist_next (mac_blacklist_iter)) { - guint8 addr[ETH_ALEN]; - - if (!nm_utils_hwaddr_aton (mac_blacklist_iter->data, addr, ETH_ALEN)) { + if (!nm_utils_hwaddr_valid (mac_blacklist_iter->data, ETH_ALEN)) { g_warn_if_reached (); return FALSE; } - if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, -1)) + if (nm_utils_hwaddr_matches (mac_blacklist_iter->data, -1, priv->perm_hw_addr, -1)) return FALSE; } } @@ -911,7 +906,7 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); NMActRequest *req; NMSettingWired *s_wired; - const GByteArray *cloned_mac; + const char *cloned_mac; NMActStageReturn ret = NM_ACT_STAGE_RETURN_SUCCESS; g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE); @@ -925,8 +920,8 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) if (s_wired) { /* Set device MAC address if the connection wants to change it */ cloned_mac = nm_setting_wired_get_cloned_mac_address (s_wired); - if (cloned_mac && (cloned_mac->len == ETH_ALEN)) - nm_device_set_hw_addr (dev, cloned_mac->data, "set", LOGD_ETHER); + if (cloned_mac) + nm_device_set_hw_addr (dev, cloned_mac, "set", LOGD_ETHER); } /* If we're re-activating a PPPoE connection a short while after @@ -1430,7 +1425,7 @@ complete_connection (NMDevice *device, NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device); NMSettingWired *s_wired; NMSettingPppoe *s_pppoe; - const GByteArray *setting_mac; + const char *setting_mac; s_pppoe = nm_connection_get_setting_pppoe (connection); @@ -1460,7 +1455,7 @@ complete_connection (NMDevice *device, setting_mac = nm_setting_wired_get_mac_address (s_wired); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, priv->perm_hw_addr, -1)) { + if (!nm_utils_hwaddr_matches (setting_mac, -1, priv->perm_hw_addr, -1)) { g_set_error_literal (error, NM_SETTING_WIRED_ERROR, NM_SETTING_WIRED_ERROR_INVALID_PROPERTY, @@ -1468,13 +1463,10 @@ complete_connection (NMDevice *device, return FALSE; } } else { - GByteArray *mac; - - /* Lock the connection to this device by default */ if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) { - mac = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); - g_object_set (G_OBJECT (s_wired), NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); + g_object_set (G_OBJECT (s_wired), + NM_SETTING_WIRED_MAC_ADDRESS, priv->perm_hw_addr, + NULL); } } @@ -1499,7 +1491,6 @@ update_connection (NMDevice *device, NMConnection *connection) NMSettingWired *s_wired = nm_connection_get_setting_wired (connection); const char *mac = nm_device_get_hw_address (device); const char *mac_prop = NM_SETTING_WIRED_MAC_ADDRESS; - GByteArray *array; GHashTableIter iter; gpointer key, value; @@ -1512,20 +1503,15 @@ update_connection (NMDevice *device, NMConnection *connection) * and the current MAC, if different, is the cloned MAC. */ if (!nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, NULL, ETH_ALEN)) { - array = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, array, NULL); - g_byte_array_unref (array); + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, priv->perm_hw_addr, NULL); mac_prop = NULL; if (mac && !nm_utils_hwaddr_matches (priv->perm_hw_addr, -1, mac, -1)) mac_prop = NM_SETTING_WIRED_CLONED_MAC_ADDRESS; } - if (mac_prop && mac && nm_utils_hwaddr_valid (mac, ETH_ALEN)) { - array = nm_utils_hwaddr_atoba (mac, ETH_ALEN); - g_object_set (s_wired, mac_prop, array, NULL); - g_byte_array_unref (array); - } + if (mac_prop && mac && nm_utils_hwaddr_valid (mac, ETH_ALEN)) + g_object_set (s_wired, mac_prop, mac, NULL); /* We don't set the MTU as we don't know whether it was set explicitly */ @@ -1628,6 +1614,7 @@ finalize (GObject *object) NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (self); g_free (priv->perm_hw_addr); + g_free (priv->initial_hw_addr); g_clear_object (&priv->supplicant.mgr); g_free (priv->subchan1); g_free (priv->subchan2); diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index 81a9017075..bbb12dff92 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -195,7 +195,6 @@ static gboolean check_connection_compatible (NMDevice *device, NMConnection *connection) { NMSettingInfiniband *s_infiniband; - const GByteArray *mac; if (!NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->check_connection_compatible (device, connection)) return FALSE; @@ -208,9 +207,10 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; if (s_infiniband) { + const char *mac; + mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, - nm_device_get_hw_address (device), -1)) + if (mac && !nm_utils_hwaddr_matches (mac, -1, nm_device_get_hw_address (device), -1)) return FALSE; } @@ -225,7 +225,7 @@ complete_connection (NMDevice *device, GError **error) { NMSettingInfiniband *s_infiniband; - const GByteArray *setting_mac; + const char *setting_mac; const char *hw_address; nm_utils_complete_generic (connection, @@ -246,7 +246,7 @@ complete_connection (NMDevice *device, hw_address = nm_device_get_hw_address (device); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's MAC */ - if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hw_address, INFINIBAND_ALEN)) { + if (!nm_utils_hwaddr_matches (setting_mac, -1, hw_address, -1)) { g_set_error_literal (error, NM_SETTING_INFINIBAND_ERROR, NM_SETTING_INFINIBAND_ERROR_INVALID_PROPERTY, @@ -254,12 +254,8 @@ complete_connection (NMDevice *device, return FALSE; } } else { - GByteArray *mac; - /* Lock the connection to this device by default */ - mac = nm_utils_hwaddr_atoba (hw_address, INFINIBAND_ALEN); - g_object_set (G_OBJECT (s_infiniband), NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); + g_object_set (G_OBJECT (s_infiniband), NM_SETTING_INFINIBAND_MAC_ADDRESS, hw_address, NULL); } if (!nm_setting_infiniband_get_transport_mode (s_infiniband)) @@ -273,7 +269,6 @@ update_connection (NMDevice *device, NMConnection *connection) { NMSettingInfiniband *s_infiniband = nm_connection_get_setting_infiniband (connection); const char *mac = nm_device_get_hw_address (device); - GByteArray *array; char *mode_path, *contents = NULL; const char *transport_mode = "datagram"; @@ -282,11 +277,8 @@ update_connection (NMDevice *device, NMConnection *connection) nm_connection_add_setting (connection, (NMSetting *) s_infiniband); } - if (mac && !nm_utils_hwaddr_matches (mac, -1, NULL, INFINIBAND_ALEN)) { - array = nm_utils_hwaddr_atoba (mac, INFINIBAND_ALEN); - g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, array, NULL); - g_byte_array_unref (array); - } + if (mac && !nm_utils_hwaddr_matches (mac, -1, NULL, INFINIBAND_ALEN)) + g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); mode_path = g_strdup_printf ("/sys/class/net/%s/mode", ASSERT_VALID_PATH_COMPONENT (nm_device_get_iface (device))); diff --git a/src/devices/nm-device-private.h b/src/devices/nm-device-private.h index 21776d47aa..16c4d03885 100644 --- a/src/devices/nm-device-private.h +++ b/src/devices/nm-device-private.h @@ -52,7 +52,7 @@ gboolean nm_device_bring_up (NMDevice *self, gboolean wait, gboolean *no_firmwar void nm_device_take_down (NMDevice *self, gboolean block); -gboolean nm_device_set_hw_addr (NMDevice *device, const guint8 *addr, +gboolean nm_device_set_hw_addr (NMDevice *device, const char *addr, const char *detail, guint64 hw_log_domain); gboolean nm_device_ip_config_should_fail (NMDevice *self, gboolean ip6); @@ -75,7 +75,7 @@ gboolean nm_device_activate_ip6_state_in_conf (NMDevice *device); gboolean nm_device_activate_ip6_state_in_wait (NMDevice *device); void nm_device_set_dhcp_timeout (NMDevice *device, guint32 timeout); -void nm_device_set_dhcp_anycast_address (NMDevice *device, guint8 *addr); +void nm_device_set_dhcp_anycast_address (NMDevice *device, const char *addr); gboolean nm_device_dhcp4_renew (NMDevice *device, gboolean release); gboolean nm_device_dhcp6_renew (NMDevice *device, gboolean release); diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index 817e3d2ac0..2b2e5711e1 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -51,7 +51,7 @@ G_DEFINE_TYPE (NMDeviceVlan, nm_device_vlan, NM_TYPE_DEVICE) #define NM_VLAN_ERROR (nm_vlan_error_quark ()) typedef struct { - guint8 initial_hw_addr[ETH_ALEN]; + char *initial_hw_addr; gboolean disposed; gboolean invalid; @@ -88,12 +88,9 @@ update_initial_hw_address (NMDevice *dev) { NMDeviceVlan *self = NM_DEVICE_VLAN (dev); NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self); - const char *mac_str; - mac_str = nm_device_get_hw_address (dev); - nm_utils_hwaddr_aton (mac_str, priv->initial_hw_addr, ETH_ALEN); - - _LOGD (LOGD_DEVICE | LOGD_VLAN, "read initial MAC address %s", mac_str); + priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (dev)); + _LOGD (LOGD_DEVICE | LOGD_VLAN, "read initial MAC address %s", priv->initial_hw_addr); } static guint32 @@ -157,20 +154,20 @@ static gboolean match_hwaddr (NMDevice *device, NMConnection *connection, gboolean fail_if_no_hwaddr) { NMSettingWired *s_wired; - const GByteArray *mac; + const char *setting_mac; const char *device_mac; s_wired = nm_connection_get_setting_wired (connection); if (!s_wired) return !fail_if_no_hwaddr; - mac = nm_setting_wired_get_mac_address (s_wired); - if (!mac) + setting_mac = nm_setting_wired_get_mac_address (s_wired); + if (!setting_mac) return !fail_if_no_hwaddr; device_mac = nm_device_get_hw_address (device); - return nm_utils_hwaddr_matches (mac->data, mac->len, device_mac, -1); + return nm_utils_hwaddr_matches (setting_mac, -1, device_mac, -1); } static gboolean @@ -333,7 +330,7 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) NMConnection *connection; NMSettingVlan *s_vlan; NMSettingWired *s_wired; - const GByteArray *cloned_mac; + const char *cloned_mac; NMActStageReturn ret; g_return_val_if_fail (reason != NULL, NM_ACT_STAGE_RETURN_FAILURE); @@ -352,8 +349,8 @@ act_stage1_prepare (NMDevice *dev, NMDeviceStateReason *reason) if (s_wired) { /* Set device MAC address if the connection wants to change it */ cloned_mac = nm_setting_wired_get_cloned_mac_address (s_wired); - if (cloned_mac && (cloned_mac->len == ETH_ALEN)) - nm_device_set_hw_addr (dev, (const guint8 *) cloned_mac->data, "set", LOGD_VLAN); + if (cloned_mac) + nm_device_set_hw_addr (dev, cloned_mac, "set", LOGD_VLAN); } s_vlan = nm_connection_get_setting_vlan (connection); @@ -605,6 +602,17 @@ dispose (GObject *object) G_OBJECT_CLASS (nm_device_vlan_parent_class)->dispose (object); } +static void +finalize (GObject *object) +{ + NMDeviceVlan *self = NM_DEVICE_VLAN (object); + NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (self); + + g_free (priv->initial_hw_addr); + + G_OBJECT_CLASS (nm_device_vlan_parent_class)->finalize (object); +} + static void nm_device_vlan_class_init (NMDeviceVlanClass *klass) { @@ -620,6 +628,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass) object_class->get_property = get_property; object_class->set_property = set_property; object_class->dispose = dispose; + object_class->finalize = finalize; parent_class->update_initial_hw_address = update_initial_hw_address; parent_class->get_generic_capabilities = get_generic_capabilities; diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 5996a779ac..ef10d0c239 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -232,7 +232,7 @@ typedef struct { /* Generic DHCP stuff */ guint32 dhcp_timeout; - GByteArray * dhcp_anycast_address; + char * dhcp_anycast_address; /* IP4 configuration info */ NMIP4Config * ip4_config; /* Combined config from VPN, settings, and device */ @@ -6076,23 +6076,17 @@ nm_device_set_dhcp_timeout (NMDevice *self, guint32 timeout) } void -nm_device_set_dhcp_anycast_address (NMDevice *self, guint8 *addr) +nm_device_set_dhcp_anycast_address (NMDevice *self, const char *addr) { NMDevicePrivate *priv; g_return_if_fail (NM_IS_DEVICE (self)); + g_return_if_fail (!addr || nm_utils_hwaddr_valid (addr, ETH_ALEN)); priv = NM_DEVICE_GET_PRIVATE (self); - if (priv->dhcp_anycast_address) { - g_byte_array_free (priv->dhcp_anycast_address, TRUE); - priv->dhcp_anycast_address = NULL; - } - - if (addr) { - priv->dhcp_anycast_address = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (priv->dhcp_anycast_address, addr, ETH_ALEN); - } + g_free (priv->dhcp_anycast_address); + priv->dhcp_anycast_address = g_strdup (addr); } /** @@ -6984,46 +6978,47 @@ nm_device_update_hw_address (NMDevice *self) } gboolean -nm_device_set_hw_addr (NMDevice *self, const guint8 *addr, +nm_device_set_hw_addr (NMDevice *self, const char *addr, const char *detail, guint64 hw_log_domain) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - char *mac_str = NULL; gboolean success = FALSE; const char *cur_addr = nm_device_get_hw_address (self); + guint8 addr_bytes[NM_UTILS_HWADDR_LEN_MAX]; g_return_val_if_fail (addr != NULL, FALSE); /* Do nothing if current MAC is same */ - if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, priv->hw_addr_len)) { + if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, -1)) { _LOGD (LOGD_DEVICE | hw_log_domain, "no MAC address change needed"); return TRUE; } - - mac_str = nm_utils_hwaddr_ntoa (addr, priv->hw_addr_len); + if (!nm_utils_hwaddr_aton (addr, addr_bytes, priv->hw_addr_len)) { + _LOGW (LOGD_DEVICE | hw_log_domain, "invalid MAC address %s", addr); + return FALSE; + } /* Can't change MAC address while device is up */ nm_device_take_down (self, FALSE); - success = nm_platform_link_set_address (nm_device_get_ip_ifindex (self), addr, priv->hw_addr_len); + success = nm_platform_link_set_address (nm_device_get_ip_ifindex (self), addr_bytes, priv->hw_addr_len); if (success) { /* MAC address succesfully changed; update the current MAC to match */ nm_device_update_hw_address (self); cur_addr = nm_device_get_hw_address (self); - if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, priv->hw_addr_len)) { + if (cur_addr && nm_utils_hwaddr_matches (cur_addr, -1, addr, -1)) { _LOGI (LOGD_DEVICE | hw_log_domain, "%s MAC address to %s", - detail, mac_str); + detail, addr); } else { _LOGW (LOGD_DEVICE | hw_log_domain, - "new MAC address %s not successfully set", mac_str); + "new MAC address %s not successfully set", addr); success = FALSE; } } else { _LOGW (LOGD_DEVICE | hw_log_domain, "failed to %s MAC address to %s", - detail, mac_str); + detail, addr); } nm_device_bring_up (self, TRUE, NULL); - g_free (mac_str); return success; } @@ -7316,8 +7311,7 @@ finalize (GObject *object) g_free (priv->driver_version); g_free (priv->firmware_version); g_free (priv->type_desc); - if (priv->dhcp_anycast_address) - g_byte_array_free (priv->dhcp_anycast_address, TRUE); + g_free (priv->dhcp_anycast_address); G_OBJECT_CLASS (nm_device_parent_class)->finalize (object); } diff --git a/src/devices/wifi/nm-device-olpc-mesh.c b/src/devices/wifi/nm-device-olpc-mesh.c index 94e7eb7721..2306155019 100644 --- a/src/devices/wifi/nm-device-olpc-mesh.c +++ b/src/devices/wifi/nm-device-olpc-mesh.c @@ -150,12 +150,9 @@ complete_connection (NMDevice *device, } if (!nm_setting_olpc_mesh_get_dhcp_anycast_address (s_mesh)) { - const guint8 anycast[ETH_ALEN] = { 0xC0, 0x27, 0xC0, 0x27, 0xC0, 0x27 }; + const char *anycast = "c0:27:c0:27:c0:27"; - tmp = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (tmp, anycast, sizeof (anycast)); - g_object_set (G_OBJECT (s_mesh), NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, tmp, NULL); - g_byte_array_free (tmp, TRUE); + g_object_set (G_OBJECT (s_mesh), NM_SETTING_OLPC_MESH_DHCP_ANYCAST_ADDRESS, anycast, NULL); } @@ -225,8 +222,8 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) NMConnection *connection; NMSettingOlpcMesh *s_mesh; guint32 channel; - const GByteArray *anycast_addr_array; - guint8 *anycast_addr = NULL; + GBytes *ssid; + const char *anycast_addr; connection = nm_device_get_connection (device); g_assert (connection); @@ -237,14 +234,15 @@ act_stage2_config (NMDevice *device, NMDeviceStateReason *reason) channel = nm_setting_olpc_mesh_get_channel (s_mesh); if (channel != 0) _mesh_set_channel (self, channel); + + ssid = nm_setting_olpc_mesh_get_ssid (s_mesh); nm_platform_mesh_set_ssid (nm_device_get_ifindex (device), - nm_setting_olpc_mesh_get_ssid (s_mesh)); - - anycast_addr_array = nm_setting_olpc_mesh_get_dhcp_anycast_address (s_mesh); - if (anycast_addr_array) - anycast_addr = anycast_addr_array->data; + g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + anycast_addr = nm_setting_olpc_mesh_get_dhcp_anycast_address (s_mesh); nm_device_set_dhcp_anycast_address (device, anycast_addr); + return NM_ACT_STAGE_RETURN_SUCCESS; } diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index e22b785f17..41a75da1a1 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -116,8 +116,8 @@ static guint signals[LAST_SIGNAL] = { 0 }; struct _NMDeviceWifiPrivate { gboolean disposed; - char * perm_hw_addr; /* Permanent MAC address */ - guint8 initial_hw_addr[ETH_ALEN]; /* Initial MAC address (as seen when NM starts) */ + char * perm_hw_addr; /* Permanent MAC address */ + char * initial_hw_addr; /* Initial MAC address (as seen when NM starts) */ gint8 invalid_strength_counter; @@ -377,7 +377,7 @@ find_active_ap (NMDeviceWifi *self, _LOGD (LOGD_WIFI, "active BSSID: %02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]); - if (!nm_ethernet_address_is_valid (bssid)) + if (!nm_ethernet_address_is_valid (bssid, ETH_ALEN)) return NULL; ssid = nm_platform_wifi_get_ssid (ifindex); @@ -399,31 +399,36 @@ find_active_ap (NMDeviceWifi *self, /* Find this SSID + BSSID in the device's AP list */ for (iter = priv->ap_list; iter; iter = g_slist_next (iter)) { NMAccessPoint *ap = NM_AP (iter->data); - const guint8 *ap_bssid = nm_ap_get_address (ap); + const char *ap_bssid = nm_ap_get_address (ap); const GByteArray *ap_ssid = nm_ap_get_ssid (ap); NM80211Mode apmode; guint32 apfreq; - _LOGD (LOGD_WIFI, " AP: %s%s%s %02x:%02x:%02x:%02x:%02x:%02x", + _LOGD (LOGD_WIFI, " AP: %s%s%s %s", ap_ssid ? "'" : "", ap_ssid ? nm_utils_escape_ssid (ap_ssid->data, ap_ssid->len) : "(none)", ap_ssid ? "'" : "", - ap_bssid[0], ap_bssid[1], ap_bssid[2], - ap_bssid[3], ap_bssid[4], ap_bssid[5]); + ap_bssid); if (ap == ignore_ap) { _LOGD (LOGD_WIFI, " ignored"); continue; } - if (!nm_utils_hwaddr_matches (bssid, ETH_ALEN, ap_bssid, ETH_ALEN)) { + if (!nm_utils_hwaddr_matches (bssid, ETH_ALEN, ap_bssid, -1)) { _LOGD (LOGD_WIFI, " BSSID mismatch"); continue; } - if ((i == 0) && !nm_utils_same_ssid (ssid, ap_ssid, TRUE)) { - _LOGD (LOGD_WIFI, " SSID mismatch"); - continue; + if (i == 0) { + if ( (ssid && !ap_ssid) + || (ap_ssid && !ssid) + || (ssid && ap_ssid && !nm_utils_same_ssid (ssid->data, ssid->len, + ap_ssid->data, ap_ssid->len, + TRUE))) { + _LOGD (LOGD_WIFI, " SSID mismatch"); + continue; + } } apmode = nm_ap_get_mode (ap); @@ -468,14 +473,14 @@ find_active_ap (NMDeviceWifi *self, * we can't match the AP based on frequency at all, just give up. */ if (match_nofreq && ((found_a_band != found_bg_band) || (devfreq == 0))) { - const guint8 *ap_bssid = nm_ap_get_address (match_nofreq); + const char *ap_bssid = nm_ap_get_address (match_nofreq); const GByteArray *ap_ssid = nm_ap_get_ssid (match_nofreq); - _LOGD (LOGD_WIFI, " matched %s%s%s %02x:%02x:%02x:%02x:%02x:%02x", + _LOGD (LOGD_WIFI, " matched %s%s%s %s", ap_ssid ? "'" : "", ap_ssid ? nm_utils_escape_ssid (ap_ssid->data, ap_ssid->len) : "(none)", ap_ssid ? "'" : "", - ap_bssid[0], ap_bssid[1], ap_bssid[2], ap_bssid[3], ap_bssid[4], ap_bssid[5]); + ap_bssid); active_ap = match_nofreq; goto done; @@ -602,8 +607,11 @@ periodic_update (NMDeviceWifi *self, NMAccessPoint *ignore_ap) /* 0x02 means "locally administered" and should be OR-ed into * the first byte of IBSS BSSIDs. */ - if ((bssid[0] & 0x02) && nm_ethernet_address_is_valid (bssid)) - nm_ap_set_address (priv->current_ap, bssid); + if ((bssid[0] & 0x02) && nm_ethernet_address_is_valid (bssid, ETH_ALEN)) { + char *bssid_str = nm_utils_hwaddr_ntoa (bssid, ETH_ALEN); + nm_ap_set_address (priv->current_ap, bssid_str); + g_free (bssid_str); + } } new_ap = find_active_ap (self, ignore_ap, FALSE); @@ -619,31 +627,26 @@ periodic_update (NMDeviceWifi *self, NMAccessPoint *ignore_ap) } if (new_ap != priv->current_ap) { - const guint8 *new_bssid = NULL; + const char *new_bssid = NULL; const GByteArray *new_ssid = NULL; - const guint8 *old_bssid = NULL; + const char *old_bssid = NULL; const GByteArray *old_ssid = NULL; - char *old_addr = NULL, *new_addr = NULL; if (new_ap) { new_bssid = nm_ap_get_address (new_ap); - new_addr = nm_utils_hwaddr_ntoa (new_bssid, ETH_ALEN); new_ssid = nm_ap_get_ssid (new_ap); } if (priv->current_ap) { old_bssid = nm_ap_get_address (priv->current_ap); - old_addr = nm_utils_hwaddr_ntoa (old_bssid, ETH_ALEN); old_ssid = nm_ap_get_ssid (priv->current_ap); } _LOGI (LOGD_WIFI, "roamed from BSSID %s (%s) to %s (%s)", - old_addr ? old_addr : "(none)", + old_bssid ? old_bssid : "(none)", old_ssid ? nm_utils_escape_ssid (old_ssid->data, old_ssid->len) : "(none)", - new_addr ? new_addr : "(none)", + new_bssid ? new_bssid : "(none)", new_ssid ? nm_utils_escape_ssid (new_ssid->data, new_ssid->len) : "(none)"); - g_free (old_addr); - g_free (new_addr); set_current_ap (self, new_ap, TRUE, FALSE); } @@ -810,7 +813,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); NMSettingConnection *s_con; NMSettingWireless *s_wireless; - const GByteArray *mac; + const char *mac; const GSList *mac_blacklist, *mac_blacklist_iter; const char *mode; @@ -828,21 +831,19 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wireless_get_mac_address (s_wireless); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, priv->perm_hw_addr, -1)) + if (mac && !nm_utils_hwaddr_matches (mac, -1, priv->perm_hw_addr, -1)) return FALSE; /* Check for MAC address blacklist */ mac_blacklist = nm_setting_wireless_get_mac_address_blacklist (s_wireless); for (mac_blacklist_iter = mac_blacklist; mac_blacklist_iter; mac_blacklist_iter = g_slist_next (mac_blacklist_iter)) { - guint8 addr[ETH_ALEN]; - - if (!nm_utils_hwaddr_aton (mac_blacklist_iter->data, addr, ETH_ALEN)) { + if (!nm_utils_hwaddr_valid (mac_blacklist_iter->data, ETH_ALEN)) { g_warn_if_reached (); return FALSE; } - if (nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->perm_hw_addr, -1)) + if (nm_utils_hwaddr_matches (mac_blacklist_iter->data, -1, priv->perm_hw_addr, -1)) return FALSE; } @@ -980,10 +981,12 @@ complete_connection (NMDevice *device, NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - const GByteArray *setting_mac; + const char *setting_mac; char *str_ssid = NULL; NMAccessPoint *ap = NULL; const GByteArray *ssid = NULL; + GByteArray *tmp_ssid = NULL; + GBytes *setting_ssid = NULL; GSList *iter; gboolean hidden = FALSE; @@ -1001,8 +1004,8 @@ complete_connection (NMDevice *device, return FALSE; } - ssid = nm_setting_wireless_get_ssid (s_wifi); - if (!ssid || !ssid->len) { + setting_ssid = nm_setting_wireless_get_ssid (s_wifi); + if (!setting_ssid || g_bytes_get_size (setting_ssid) == 0) { g_set_error_literal (error, NM_WIFI_ERROR, NM_WIFI_ERROR_CONNECTION_INVALID, @@ -1065,7 +1068,13 @@ complete_connection (NMDevice *device, * for the SSID. The AP object will still be used for encryption * settings and such. */ - ssid = nm_setting_wireless_get_ssid (s_wifi); + setting_ssid = nm_setting_wireless_get_ssid (s_wifi); + if (setting_ssid) { + ssid = tmp_ssid = g_byte_array_new (); + g_byte_array_append (tmp_ssid, + g_bytes_get_data (setting_ssid, NULL), + g_bytes_get_size (setting_ssid)); + } } if (ssid == NULL) { @@ -1085,8 +1094,11 @@ complete_connection (NMDevice *device, if (!nm_ap_complete_connection (ap, connection, is_manf_default_ssid (ssid), - error)) + error)) { + if (tmp_ssid) + g_byte_array_unref (tmp_ssid); return FALSE; + } } /* The kernel doesn't support Ad-Hoc WPA connections well at this time, @@ -1098,11 +1110,13 @@ complete_connection (NMDevice *device, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, "WPA Ad-Hoc disabled due to kernel bugs"); + if (tmp_ssid) + g_byte_array_unref (tmp_ssid); return FALSE; } g_assert (ssid); - str_ssid = nm_utils_ssid_to_utf8 (ssid); + str_ssid = nm_utils_ssid_to_utf8 (ssid->data, ssid->len); nm_utils_complete_generic (connection, NM_SETTING_WIRELESS_SETTING_NAME, @@ -1112,6 +1126,8 @@ complete_connection (NMDevice *device, NULL, TRUE); g_free (str_ssid); + if (tmp_ssid) + g_byte_array_unref (tmp_ssid); if (hidden) g_object_set (s_wifi, NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL); @@ -1119,7 +1135,7 @@ complete_connection (NMDevice *device, setting_mac = nm_setting_wireless_get_mac_address (s_wifi); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, priv->perm_hw_addr, -1)) { + if (!nm_utils_hwaddr_matches (setting_mac, -1, priv->perm_hw_addr, -1)) { g_set_error (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, @@ -1127,7 +1143,6 @@ complete_connection (NMDevice *device, return FALSE; } } else { - GByteArray *mac; guint8 perm_hw_addr[ETH_ALEN]; /* Lock the connection to this device by default if it uses a @@ -1136,9 +1151,9 @@ complete_connection (NMDevice *device, nm_utils_hwaddr_aton (priv->perm_hw_addr, perm_hw_addr, ETH_ALEN); if ( !(perm_hw_addr[0] & 0x02) && !nm_utils_hwaddr_matches (perm_hw_addr, ETH_ALEN, NULL, ETH_ALEN)) { - mac = nm_utils_hwaddr_atoba (priv->perm_hw_addr, ETH_ALEN); - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); + g_object_set (G_OBJECT (s_wifi), + NM_SETTING_WIRELESS_MAC_ADDRESS, priv->perm_hw_addr, + NULL); } } @@ -1389,7 +1404,6 @@ scanning_allowed (NMDeviceWifi *self) if (connection) { NMSettingWireless *s_wifi; const char *ip4_method = NULL; - const GByteArray *bssid; /* Don't scan when a shared connection is active; it makes drivers mad */ ip4_method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); @@ -1403,8 +1417,7 @@ scanning_allowed (NMDeviceWifi *self) */ s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - bssid = nm_setting_wireless_get_bssid (s_wifi); - if (bssid && bssid->len == ETH_ALEN) + if (nm_setting_wireless_get_bssid (s_wifi)) return FALSE; } @@ -1476,20 +1489,25 @@ build_hidden_probe_list (NMDeviceWifi *self) hidden_filter_func, NULL); if (connections && connections->data) { - ssids = g_ptr_array_sized_new (max_scan_ssids - 1); - g_ptr_array_add (ssids, nullssid); /* Add wildcard SSID */ + ssids = g_ptr_array_new_full (max_scan_ssids - 1, (GDestroyNotify) g_byte_array_unref); + g_ptr_array_add (ssids, g_byte_array_ref (nullssid)); /* Add wildcard SSID */ } for (iter = connections; iter; iter = g_slist_next (iter)) { NMConnection *connection = iter->data; NMSettingWireless *s_wifi; - const GByteArray *ssid; + GBytes *ssid; + GByteArray *ssid_array; s_wifi = (NMSettingWireless *) nm_connection_get_setting_wireless (connection); g_assert (s_wifi); ssid = nm_setting_wireless_get_ssid (s_wifi); g_assert (ssid); - g_ptr_array_add (ssids, (gpointer) ssid); + ssid_array = g_byte_array_new (); + g_byte_array_append (ssid_array, + g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + g_ptr_array_add (ssids, ssid_array); } g_slist_free (connections); @@ -1516,11 +1534,13 @@ request_wireless_scan (gpointer user_data) if (nm_logging_enabled (LOGL_DEBUG, LOGD_WIFI_SCAN)) { if (ssids) { + const GByteArray *ssid; guint i; char *foo; for (i = 0; i < ssids->len; i++) { - foo = nm_utils_ssid_to_utf8 (g_ptr_array_index (ssids, i)); + ssid = g_ptr_array_index (ssids, i); + foo = nm_utils_ssid_to_utf8 (ssid->data, ssid->len); _LOGD (LOGD_WIFI_SCAN, "(%d) probe scanning SSID '%s'", i, foo ? foo : ""); g_free (foo); @@ -1536,10 +1556,8 @@ request_wireless_scan (gpointer user_data) nm_device_add_pending_action (NM_DEVICE (self), "scan", TRUE); } - if (ssids) { - /* Elements owned by the connections, so we don't free them here */ - g_ptr_array_free (ssids, TRUE); - } + if (ssids) + g_ptr_array_unref (ssids); } else _LOGD (LOGD_WIFI_SCAN, "scan requested but not allowed at this time"); @@ -1639,7 +1657,7 @@ supplicant_iface_scan_done_cb (NMSupplicantInterface *iface, static void try_fill_ssid_for_hidden_ap (NMAccessPoint *ap) { - const guint8 *bssid; + const char *bssid; const GSList *connections, *iter; g_return_if_fail (nm_ap_get_ssid (ap) == NULL); @@ -1657,16 +1675,17 @@ try_fill_ssid_for_hidden_ap (NMAccessPoint *ap) s_wifi = nm_connection_get_setting_wireless (connection); if (s_wifi) { if (nm_settings_connection_has_seen_bssid (NM_SETTINGS_CONNECTION (connection), bssid)) { - nm_ap_set_ssid (ap, nm_setting_wireless_get_ssid (s_wifi)); + GBytes *ssid = nm_setting_wireless_get_ssid (s_wifi); + + nm_ap_set_ssid (ap, + g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); break; } } } } -#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -#define MAC_ARG(x) ((guint8*)(x))[0],((guint8*)(x))[1],((guint8*)(x))[2],((guint8*)(x))[3],((guint8*)(x))[4],((guint8*)(x))[5] - /* * merge_scanned_ap * @@ -1686,7 +1705,7 @@ merge_scanned_ap (NMDeviceWifi *self, NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); NMAccessPoint *found_ap = NULL; const GByteArray *ssid; - const guint8 *bssid; + const char *bssid; gboolean strict_match = TRUE; /* Let the manager try to fill in the SSID from seen-bssids lists */ @@ -1699,14 +1718,13 @@ merge_scanned_ap (NMDeviceWifi *self, ssid = nm_ap_get_ssid (merge_ap); if (ssid && (nm_utils_is_empty_ssid (ssid->data, ssid->len) == FALSE)) { /* Yay, matched it, no longer treat as hidden */ - _LOGD (LOGD_WIFI_SCAN, "matched hidden AP " MAC_FMT " => '%s'", - MAC_ARG (bssid), - nm_utils_escape_ssid (ssid->data, ssid->len)); + _LOGD (LOGD_WIFI_SCAN, "matched hidden AP %s => '%s'", + bssid, nm_utils_escape_ssid (ssid->data, ssid->len)); nm_ap_set_broadcast (merge_ap, FALSE); } else { /* Didn't have an entry for this AP in the database */ - _LOGD (LOGD_WIFI_SCAN, "failed to match hidden AP " MAC_FMT, - MAC_ARG (bssid)); + _LOGD (LOGD_WIFI_SCAN, "failed to match hidden AP %s", + bssid); } } @@ -1723,9 +1741,9 @@ merge_scanned_ap (NMDeviceWifi *self, if (!found_ap) found_ap = nm_ap_match_in_list (merge_ap, priv->ap_list, strict_match); if (found_ap) { - _LOGD (LOGD_WIFI_SCAN, "merging AP '%s' " MAC_FMT " (%p) with existing (%p)", + _LOGD (LOGD_WIFI_SCAN, "merging AP '%s' %s (%p) with existing (%p)", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)", - MAC_ARG (bssid), + bssid, merge_ap, found_ap); @@ -1745,10 +1763,9 @@ merge_scanned_ap (NMDeviceWifi *self, nm_ap_set_fake (found_ap, FALSE); } else { /* New entry in the list */ - _LOGD (LOGD_WIFI_SCAN, "adding new AP '%s' " MAC_FMT " (%p)", + _LOGD (LOGD_WIFI_SCAN, "adding new AP '%s' %s (%p)", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)", - MAC_ARG (bssid), - merge_ap); + bssid, merge_ap); g_object_ref (merge_ap); priv->ap_list = g_slist_prepend (priv->ap_list, merge_ap); @@ -1804,14 +1821,14 @@ cull_scan_list (NMDeviceWifi *self) /* Remove outdated APs */ for (elt = outdated_list; elt; elt = g_slist_next (elt)) { NMAccessPoint *outdated_ap = NM_AP (elt->data); - const guint8 *bssid; + const char *bssid; const GByteArray *ssid; bssid = nm_ap_get_address (outdated_ap); ssid = nm_ap_get_ssid (outdated_ap); _LOGD (LOGD_WIFI_SCAN, - " removing %02x:%02x:%02x:%02x:%02x:%02x (%s%s%s)", - bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], + " removing %s (%s%s%s)", + bssid, ssid ? "'" : "", ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)", ssid ? "'" : ""); @@ -2195,7 +2212,7 @@ supplicant_iface_state_cb (NMSupplicantInterface *iface, if (devstate == NM_DEVICE_STATE_CONFIG) { NMConnection *connection; NMSettingWireless *s_wifi; - const GByteArray *ssid; + GBytes *ssid; connection = nm_device_get_connection (NM_DEVICE (self)); g_return_if_fail (connection); @@ -2210,7 +2227,8 @@ supplicant_iface_state_cb (NMSupplicantInterface *iface, "Activation: (wifi) Stage 2 of 5 (Device Configure) successful. %s '%s'.", priv->mode == NM_802_11_MODE_AP ? "Started Wi-Fi Hotspot" : "Connected to wireless network", - ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(none)"); + ssid ? nm_utils_escape_ssid (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)) : "(none)"); nm_device_activate_schedule_stage3_ip_config_start (device); } else if (devstate == NM_DEVICE_STATE_ACTIVATED) periodic_update (self, NULL); @@ -2533,7 +2551,7 @@ update_permanent_hw_address (NMDevice *device) errno = 0; ret = ioctl (fd, SIOCETHTOOL, &req); errsv = errno; - if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data)) { + if ((ret < 0) || !nm_ethernet_address_is_valid (epaddr->data, ETH_ALEN)) { _LOGD (LOGD_HW | LOGD_ETHER, "unable to read permanent MAC address (error %d)", errsv); /* Fall back to current address */ @@ -2551,15 +2569,13 @@ update_initial_hw_address (NMDevice *device) { NMDeviceWifi *self = NM_DEVICE_WIFI (device); NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); - const char *mac_str; /* This sets initial MAC address from current MAC address. It should only * be called from NMDevice constructor() to really get the initial address. */ - mac_str = nm_device_get_hw_address (device); - nm_utils_hwaddr_aton (mac_str, priv->initial_hw_addr, ETH_ALEN); + priv->initial_hw_addr = g_strdup (nm_device_get_hw_address (device)); - _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", mac_str); + _LOGD (LOGD_DEVICE | LOGD_ETHER, "read initial MAC address %s", priv->initial_hw_addr); } static NMActStageReturn @@ -2572,7 +2588,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) NMActRequest *req; NMConnection *connection; NMSettingWireless *s_wireless; - const GByteArray *cloned_mac; + const char *cloned_mac; GSList *iter; const char *mode; const char *ap_path; @@ -2615,8 +2631,8 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) /* Set spoof MAC to the interface */ cloned_mac = nm_setting_wireless_get_cloned_mac_address (s_wireless); - if (cloned_mac && (cloned_mac->len == ETH_ALEN)) - nm_device_set_hw_addr (device, (const guint8 *) cloned_mac->data, "set", LOGD_WIFI); + if (cloned_mac) + nm_device_set_hw_addr (device, cloned_mac, "set", LOGD_WIFI); /* AP mode never uses a specific object or existing scanned AP */ if (priv->mode != NM_802_11_MODE_AP) { @@ -2653,12 +2669,8 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *reason) if (nm_ap_get_mode (ap) == NM_802_11_MODE_INFRA) nm_ap_set_broadcast (ap, FALSE); - else if (nm_ap_is_hotspot (ap)) { - guint8 addr[ETH_ALEN]; - - nm_utils_hwaddr_aton (nm_device_get_hw_address (device), addr, ETH_ALEN); - nm_ap_set_address (ap, addr); - } + else if (nm_ap_is_hotspot (ap)) + nm_ap_set_address (ap, nm_device_get_hw_address (device)); priv->ap_list = g_slist_prepend (priv->ap_list, ap); nm_ap_export_to_dbus (ap); @@ -3021,8 +3033,11 @@ activation_success_handler (NMDevice *device) * the BSSID off the card and fill in the BSSID of the activation AP. */ nm_platform_wifi_get_bssid (ifindex, bssid); - if (!nm_ethernet_address_is_valid (nm_ap_get_address (ap))) - nm_ap_set_address (ap, bssid); + if (!nm_ethernet_address_is_valid (nm_ap_get_address (ap), -1)) { + char *bssid_str = nm_utils_hwaddr_ntoa (bssid, ETH_ALEN); + nm_ap_set_address (ap, bssid_str); + g_free (bssid_str); + } if (!nm_ap_get_freq (ap)) nm_ap_set_freq (ap, nm_platform_wifi_get_frequency (ifindex)); if (!nm_ap_get_max_bitrate (ap)) @@ -3036,9 +3051,11 @@ activation_success_handler (NMDevice *device) * instead. */ - /* If the better match was a hidden AP, update it's SSID */ - if (!ssid || nm_utils_is_empty_ssid (ssid->data, ssid->len)) - nm_ap_set_ssid (tmp_ap, nm_ap_get_ssid (ap)); + /* If the better match was a hidden AP, update its SSID */ + if (!ssid || nm_utils_is_empty_ssid (ssid->data, ssid->len)) { + ssid = nm_ap_get_ssid (ap); + nm_ap_set_ssid (tmp_ap, ssid->data, ssid->len); + } nm_active_connection_set_specific_object (NM_ACTIVE_CONNECTION (req), nm_ap_get_dbus_path (tmp_ap)); @@ -3255,6 +3272,7 @@ finalize (GObject *object) NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self); g_free (priv->perm_hw_addr); + g_free (priv->initial_hw_addr); G_OBJECT_CLASS (nm_device_wifi_parent_class)->finalize (object); } diff --git a/src/devices/wifi/nm-wifi-ap-utils.c b/src/devices/wifi/nm-wifi-ap-utils.c index a390e2d523..19d8307e6d 100644 --- a/src/devices/wifi/nm-wifi-ap-utils.c +++ b/src/devices/wifi/nm-wifi-ap-utils.c @@ -466,7 +466,7 @@ verify_adhoc (NMSettingWirelessSecurity *s_wsec, gboolean nm_ap_utils_complete_connection (const GByteArray *ap_ssid, - const guint8 *ap_bssid, + const char *bssid, NM80211Mode ap_mode, guint32 ap_flags, guint32 ap_wpa_flags, @@ -478,7 +478,7 @@ nm_ap_utils_complete_connection (const GByteArray *ap_ssid, NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - const GByteArray *ssid; + GBytes *ssid, *ap_ssid_bytes; const char *mode, *key_mgmt, *auth_alg, *leap_username; gboolean adhoc = FALSE; @@ -488,26 +488,22 @@ nm_ap_utils_complete_connection (const GByteArray *ap_ssid, s_8021x = nm_connection_get_setting_802_1x (connection); /* Fill in missing SSID */ + ap_ssid_bytes = g_bytes_new (ap_ssid->data, ap_ssid->len); ssid = nm_setting_wireless_get_ssid (s_wifi); if (!ssid) - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_SSID, ap_ssid, NULL); - else if ( ssid->len != ap_ssid->len - || memcmp (ssid->data, ap_ssid->data, ssid->len)) { + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_SSID, ap_ssid_bytes, NULL); + else if (!g_bytes_equal (ssid, ap_ssid_bytes)) { g_set_error_literal (error, NM_SETTING_WIRELESS_ERROR, NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY, "Setting SSID did not match AP SSID"); + g_bytes_unref (ap_ssid_bytes); return FALSE; } + g_bytes_unref (ap_ssid_bytes); - if (lock_bssid && !nm_setting_wireless_get_bssid (s_wifi)) { - GByteArray *bssid; - - bssid = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (bssid, ap_bssid, ETH_ALEN); + if (lock_bssid && !nm_setting_wireless_get_bssid (s_wifi)) g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_BSSID, bssid, NULL); - g_byte_array_free (bssid, TRUE); - } /* And mode */ mode = nm_setting_wireless_get_mode (s_wifi); diff --git a/src/devices/wifi/nm-wifi-ap-utils.h b/src/devices/wifi/nm-wifi-ap-utils.h index d64d9dd53b..31c12cd7b2 100644 --- a/src/devices/wifi/nm-wifi-ap-utils.h +++ b/src/devices/wifi/nm-wifi-ap-utils.h @@ -29,7 +29,7 @@ #include gboolean nm_ap_utils_complete_connection (const GByteArray *ssid, - const guint8 *bssid, + const char *bssid, NM80211Mode mode, guint32 flags, guint32 wpa_flags, diff --git a/src/devices/wifi/nm-wifi-ap.c b/src/devices/wifi/nm-wifi-ap.c index f387dacd2f..0c252218fa 100644 --- a/src/devices/wifi/nm-wifi-ap.c +++ b/src/devices/wifi/nm-wifi-ap.c @@ -44,7 +44,7 @@ typedef struct /* Scanned or cached values */ GByteArray * ssid; - guint8 address[ETH_ALEN]; + char * address; NM80211Mode mode; gint8 strength; guint32 freq; /* Frequency in MHz; ie 2412 (== 2.412 GHz) */ @@ -101,6 +101,7 @@ finalize (GObject *object) g_free (priv->supplicant_path); if (priv->ssid) g_byte_array_free (priv->ssid, TRUE); + g_free (priv->address); G_OBJECT_CLASS (nm_ap_parent_class)->finalize (object); } @@ -110,6 +111,7 @@ set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { NMAccessPoint *ap = NM_AP (object); + GByteArray *ssid; switch (prop_id) { case PROP_FLAGS: @@ -122,7 +124,11 @@ set_property (GObject *object, guint prop_id, nm_ap_set_rsn_flags (ap, g_value_get_uint (value)); break; case PROP_SSID: - nm_ap_set_ssid (ap, (GByteArray *) g_value_get_boxed (value)); + ssid = g_value_get_boxed (value); + if (ssid) + nm_ap_set_ssid (ap, ssid->data, ssid->len); + else + nm_ap_set_ssid (ap, NULL, 0); break; case PROP_FREQUENCY: nm_ap_set_freq (ap, g_value_get_uint (value)); @@ -175,7 +181,7 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, priv->freq); break; case PROP_HW_ADDRESS: - g_value_take_string (value, nm_utils_hwaddr_ntoa (&priv->address, ETH_ALEN)); + g_value_set_string (value, priv->address); break; case PROP_MODE: g_value_set_uint (value, priv->mode); @@ -392,7 +398,6 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data) if (!strcmp (key, "SSID")) { guint32 len = MIN (32, array->len); - GByteArray *ssid; /* Stupid ieee80211 layer uses */ if (((len == 8) || (len == 9)) @@ -402,14 +407,15 @@ foreach_property_cb (gpointer key, gpointer value, gpointer user_data) if (nm_utils_is_empty_ssid ((const guint8 *) array->data, len)) return; - ssid = g_byte_array_sized_new (len); - g_byte_array_append (ssid, (const guint8 *) array->data, len); - nm_ap_set_ssid (ap, ssid); - g_byte_array_free (ssid, TRUE); + nm_ap_set_ssid (ap, (const guint8 *) array->data, len); } else if (!strcmp (key, "BSSID")) { + char *addr; + if (array->len != ETH_ALEN) return; - nm_ap_set_address (ap, (guint8 *) array->data); + addr = nm_utils_hwaddr_ntoa (array->data, array->len); + nm_ap_set_address (ap, addr); + g_free (addr); } else if (!strcmp (key, "Rates")) { guint32 maxrate = 0; int i; @@ -469,7 +475,7 @@ NMAccessPoint * nm_ap_new_from_properties (const char *supplicant_path, GHashTable *properties) { NMAccessPoint *ap; - const guint8 *addr; + const char *addr; const char bad_bssid1[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; const char bad_bssid2[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; @@ -484,8 +490,8 @@ nm_ap_new_from_properties (const char *supplicant_path, GHashTable *properties) /* ignore APs with invalid BSSIDs */ addr = nm_ap_get_address (ap); - if ( !(memcmp (addr, bad_bssid1, ETH_ALEN)) - || !(memcmp (addr, bad_bssid2, ETH_ALEN))) { + if ( nm_utils_hwaddr_matches (addr, -1, bad_bssid1, ETH_ALEN) + || nm_utils_hwaddr_matches (addr, -1, bad_bssid2, ETH_ALEN)) { g_object_unref (ap); return NULL; } @@ -583,7 +589,7 @@ nm_ap_new_fake_from_connection (NMConnection *connection) NMAccessPoint *ap; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wireless_sec; - const GByteArray *ssid; + GBytes *ssid; const char *mode, *band, *key_mgmt; guint32 channel; NM80211ApSecurityFlags flags; @@ -596,11 +602,11 @@ nm_ap_new_fake_from_connection (NMConnection *connection) ssid = nm_setting_wireless_get_ssid (s_wireless); g_return_val_if_fail (ssid != NULL, NULL); - g_return_val_if_fail (ssid->len > 0, NULL); + g_return_val_if_fail (g_bytes_get_size (ssid) > 0, NULL); ap = nm_ap_new (); nm_ap_set_fake (ap, TRUE); - nm_ap_set_ssid (ap, ssid); + nm_ap_set_ssid (ap, g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid)); // FIXME: bssid too? @@ -706,10 +712,6 @@ error: return NULL; } - -#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -#define MAC_ARG(x) ((guint8*)(x))[0],((guint8*)(x))[1],((guint8*)(x))[2],((guint8*)(x))[3],((guint8*)(x))[4],((guint8*)(x))[5] - void nm_ap_dump (NMAccessPoint *ap, const char *prefix) { @@ -723,7 +725,7 @@ nm_ap_dump (NMAccessPoint *ap, const char *prefix) prefix, priv->ssid ? nm_utils_escape_ssid (priv->ssid->data, priv->ssid->len) : "(none)", ap); - nm_log_dbg (LOGD_WIFI_SCAN, " BSSID " MAC_FMT, MAC_ARG (priv->address)); + nm_log_dbg (LOGD_WIFI_SCAN, " BSSID %s", priv->address); nm_log_dbg (LOGD_WIFI_SCAN, " mode %d", priv->mode); nm_log_dbg (LOGD_WIFI_SCAN, " flags 0x%X", priv->flags); nm_log_dbg (LOGD_WIFI_SCAN, " wpa flags 0x%X", priv->wpa_flags); @@ -772,20 +774,18 @@ const GByteArray * nm_ap_get_ssid (const NMAccessPoint *ap) } void -nm_ap_set_ssid (NMAccessPoint *ap, const GByteArray * ssid) +nm_ap_set_ssid (NMAccessPoint *ap, const guint8 *ssid, gsize len) { NMAccessPointPrivate *priv; g_return_if_fail (NM_IS_AP (ap)); + g_return_if_fail (ssid == NULL || len > 0); priv = NM_AP_GET_PRIVATE (ap); - if (ssid == priv->ssid) - return; - /* same SSID */ - if ((ssid && priv->ssid) && (ssid->len == priv->ssid->len)) { - if (!memcmp (ssid->data, priv->ssid->data, ssid->len)) + if ((ssid && priv->ssid) && (len == priv->ssid->len)) { + if (!memcmp (ssid, priv->ssid->data, len)) return; } @@ -795,14 +795,8 @@ nm_ap_set_ssid (NMAccessPoint *ap, const GByteArray * ssid) } if (ssid) { - /* Should never get zero-length SSIDs */ - g_warn_if_fail (ssid->len > 0); - - if (ssid->len) { - priv->ssid = g_byte_array_sized_new (ssid->len); - priv->ssid->len = ssid->len; - memcpy (priv->ssid->data, ssid->data, ssid->len); - } + priv->ssid = g_byte_array_new (); + g_byte_array_append (priv->ssid, ssid, len); } g_object_notify (G_OBJECT (ap), NM_AP_SSID); @@ -883,7 +877,7 @@ nm_ap_set_rsn_flags (NMAccessPoint *ap, NM80211ApSecurityFlags flags) * Get/set functions for address * */ -const guint8 * +const char * nm_ap_get_address (const NMAccessPoint *ap) { g_return_val_if_fail (NM_IS_AP (ap), NULL); @@ -892,17 +886,19 @@ nm_ap_get_address (const NMAccessPoint *ap) } void -nm_ap_set_address (NMAccessPoint *ap, const guint8 *addr) +nm_ap_set_address (NMAccessPoint *ap, const char *addr) { NMAccessPointPrivate *priv; g_return_if_fail (NM_IS_AP (ap)); g_return_if_fail (addr != NULL); + g_return_if_fail (nm_utils_hwaddr_valid (addr, ETH_ALEN)); priv = NM_AP_GET_PRIVATE (ap); - if (!nm_utils_hwaddr_matches (addr, ETH_ALEN, priv->address, sizeof (priv->address))) { - memcpy (NM_AP_GET_PRIVATE (ap)->address, addr, sizeof (priv->address)); + if (!priv->address || !nm_utils_hwaddr_matches (addr, -1, priv->address, -1)) { + g_free (priv->address); + priv->address = g_strdup (addr); g_object_notify (G_OBJECT (ap), NM_AP_HW_ADDRESS); } } @@ -1106,9 +1102,10 @@ nm_ap_check_compatible (NMAccessPoint *self, NMAccessPointPrivate *priv; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wireless_sec; + GBytes *ssid; const char *mode; const char *band; - const GByteArray *bssid; + const char *bssid; guint32 channel; g_return_val_if_fail (NM_IS_AP (self), FALSE); @@ -1120,11 +1117,19 @@ nm_ap_check_compatible (NMAccessPoint *self, if (s_wireless == NULL) return FALSE; - if (!nm_utils_same_ssid (nm_setting_wireless_get_ssid (s_wireless), priv->ssid, TRUE)) + ssid = nm_setting_wireless_get_ssid (s_wireless); + if ( (ssid && !priv->ssid) + || (priv->ssid && !ssid)) + return FALSE; + + if ( ssid && priv->ssid && + !nm_utils_same_ssid (g_bytes_get_data (ssid, NULL), g_bytes_get_size (ssid), + priv->ssid->data, priv->ssid->len, + TRUE)) return FALSE; bssid = nm_setting_wireless_get_bssid (s_wireless); - if (bssid && !nm_utils_hwaddr_matches (bssid->data, bssid->len, priv->address, ETH_ALEN)) + if (bssid && !nm_utils_hwaddr_matches (bssid, -1, priv->address, -1)) return FALSE; mode = nm_setting_wireless_get_mode (s_wireless); @@ -1221,23 +1226,28 @@ nm_ap_match_in_list (NMAccessPoint *find_ap, for (iter = ap_list; iter; iter = g_slist_next (iter)) { NMAccessPoint * list_ap = NM_AP (iter->data); const GByteArray * list_ssid = nm_ap_get_ssid (list_ap); - const guint8 * list_addr = nm_ap_get_address (list_ap); + const char * list_addr = nm_ap_get_address (list_ap); const GByteArray * find_ssid = nm_ap_get_ssid (find_ap); - const guint8 * find_addr = nm_ap_get_address (find_ap); + const char * find_addr = nm_ap_get_address (find_ap); /* SSID match; if both APs are hiding their SSIDs, * let matching continue on BSSID and other properties */ if ( (!list_ssid && find_ssid) - || (list_ssid && !find_ssid) - || !nm_utils_same_ssid (list_ssid, find_ssid, TRUE)) + || (list_ssid && !find_ssid)) + continue; + if ( list_ssid + && find_ssid + && !nm_utils_same_ssid (list_ssid->data, list_ssid->len, + find_ssid->data, find_ssid->len, + TRUE)) continue; /* BSSID match */ - if ( (strict_match || nm_ethernet_address_is_valid (find_addr)) - && nm_ethernet_address_is_valid (list_addr) - && !nm_utils_hwaddr_matches (list_addr, ETH_ALEN, find_addr, ETH_ALEN)) + if ( (strict_match || nm_ethernet_address_is_valid (find_addr, -1)) + && nm_ethernet_address_is_valid (list_addr, -1) + && !nm_utils_hwaddr_matches (list_addr, -1, find_addr, -1)) continue; /* mode match */ diff --git a/src/devices/wifi/nm-wifi-ap.h b/src/devices/wifi/nm-wifi-ap.h index c9175821b5..0abb28fccd 100644 --- a/src/devices/wifi/nm-wifi-ap.h +++ b/src/devices/wifi/nm-wifi-ap.h @@ -67,7 +67,7 @@ void nm_ap_set_supplicant_path (NMAccessPoint *ap, const char *path); const GByteArray *nm_ap_get_ssid (const NMAccessPoint * ap); -void nm_ap_set_ssid (NMAccessPoint * ap, const GByteArray * ssid); +void nm_ap_set_ssid (NMAccessPoint * ap, const guint8 * ssid, gsize len); NM80211ApFlags nm_ap_get_flags (NMAccessPoint *ap); void nm_ap_set_flags (NMAccessPoint *ap, NM80211ApFlags flags); @@ -78,8 +78,8 @@ void nm_ap_set_wpa_flags (NMAccessPoint *ap, NM80211ApSecurity NM80211ApSecurityFlags nm_ap_get_rsn_flags (NMAccessPoint *ap); void nm_ap_set_rsn_flags (NMAccessPoint *ap, NM80211ApSecurityFlags flags); -const guint8 *nm_ap_get_address (const NMAccessPoint *ap); -void nm_ap_set_address (NMAccessPoint *ap, const guint8 *addr); +const char *nm_ap_get_address (const NMAccessPoint *ap); +void nm_ap_set_address (NMAccessPoint *ap, const char *addr); NM80211Mode nm_ap_get_mode (NMAccessPoint *ap); void nm_ap_set_mode (NMAccessPoint *ap, const NM80211Mode mode); diff --git a/src/devices/wifi/tests/test-wifi-ap-utils.c b/src/devices/wifi/tests/test-wifi-ap-utils.c index 89b3d1644c..9ca49e4889 100644 --- a/src/devices/wifi/tests/test-wifi-ap-utils.c +++ b/src/devices/wifi/tests/test-wifi-ap-utils.c @@ -67,7 +67,7 @@ static gboolean complete_connection (const char *ssid, - const guint8 bssid[ETH_ALEN], + const char *bssid, NM80211Mode mode, guint32 flags, guint32 wpa_flags, @@ -114,7 +114,7 @@ set_items (NMSetting *setting, const KeyData *items) { const KeyData *item; GParamSpec *pspec; - GByteArray *tmp; + GBytes *tmp; for (item = items; item && item->key; item++) { g_assert (item->key); @@ -138,12 +138,11 @@ set_items (NMSetting *setting, const KeyData *items) g_assert (item->str == NULL); g_object_set (G_OBJECT (setting), item->key, foo, NULL); - } else if (pspec->value_type == DBUS_TYPE_G_UCHAR_ARRAY) { + } else if (pspec->value_type == G_TYPE_BYTES) { g_assert (item->str); - tmp = g_byte_array_sized_new (strlen (item->str)); - g_byte_array_append (tmp, (const guint8 *) item->str, strlen (item->str)); + tmp = g_bytes_new (item->str, strlen (item->str)); g_object_set (G_OBJECT (setting), item->key, tmp, NULL); - g_byte_array_free (tmp, TRUE); + g_bytes_unref (tmp); } else { /* Special types, check based on property name */ if (!strcmp (item->key, NM_SETTING_WIRELESS_SECURITY_PROTO)) @@ -218,12 +217,12 @@ fill_8021x (NMConnection *connection, const KeyData items[]) static NMConnection * create_basic (const char *ssid, - const guint8 *bssid, + const char *bssid, NM80211Mode mode) { NMConnection *connection; NMSettingWireless *s_wifi = NULL; - GByteArray *tmp; + GBytes *tmp; connection = nm_simple_connection_new (); @@ -231,18 +230,13 @@ create_basic (const char *ssid, nm_connection_add_setting (connection, NM_SETTING (s_wifi)); /* SSID */ - tmp = g_byte_array_sized_new (strlen (ssid)); - g_byte_array_append (tmp, (const guint8 *) ssid, strlen (ssid)); + tmp = g_bytes_new (ssid, strlen (ssid)); g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_SSID, tmp, NULL); - g_byte_array_free (tmp, TRUE); + g_bytes_unref (tmp); /* BSSID */ - if (bssid) { - tmp = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (tmp, bssid, ETH_ALEN); - g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_BSSID, tmp, NULL); - g_byte_array_free (tmp, TRUE); - } + if (bssid) + g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_BSSID, bssid, NULL); if (mode == NM_802_11_MODE_INFRA) g_object_set (G_OBJECT (s_wifi), NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); @@ -260,7 +254,7 @@ static void test_lock_bssid (void) { NMConnection *src, *expected; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *ssid = "blahblah"; gboolean success; GError *error = NULL; @@ -284,7 +278,7 @@ static void test_open_ap_empty_connection (void) { NMConnection *src, *expected; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *ssid = "blahblah"; gboolean success; GError *error = NULL; @@ -312,7 +306,7 @@ static void test_open_ap_leap_connection_1 (gconstpointer add_wifi) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_LEAP_USERNAME, "Bill Smith", 0 }, { NULL } }; gboolean success; GError *error = NULL; @@ -344,7 +338,7 @@ static void test_open_ap_leap_connection_2 (void) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NULL } }; gboolean success; GError *error = NULL; @@ -374,7 +368,7 @@ static void test_open_ap_wep_connection (gconstpointer add_wifi) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, "11111111111111111111111111", 0 }, { NM_SETTING_WIRELESS_SECURITY_WEP_TX_KEYIDX, NULL, 0 }, @@ -415,7 +409,7 @@ test_ap_wpa_psk_connection_base (const char *key_mgmt, { NMConnection *src; const char *ssid = "blahblah"; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData exp_wifi[] = { { NM_SETTING_WIRELESS_SSID, ssid, 0 }, { NM_SETTING_WIRELESS_MODE, "infrastructure", 0 }, @@ -525,7 +519,7 @@ test_ap_wpa_eap_connection_base (const char *key_mgmt, guint error_code) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_empty[] = { { NULL } }; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, key_mgmt, 0 }, @@ -731,7 +725,7 @@ static void test_priv_ap_empty_connection (void) { NMConnection *src, *expected; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *ssid = "blahblah"; const KeyData exp_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "none", 0 }, @@ -766,7 +760,7 @@ test_priv_ap_leap_connection_1 (gconstpointer add_wifi) { NMConnection *src, *expected; const char *ssid = "blahblah"; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *leap_username = "Bill Smith"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, @@ -811,7 +805,7 @@ static void test_priv_ap_leap_connection_2 (void) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, @@ -845,7 +839,7 @@ test_priv_ap_dynamic_wep_1 (void) { NMConnection *src, *expected; const char *ssid = "blahblah"; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, @@ -893,7 +887,7 @@ test_priv_ap_dynamic_wep_2 (void) { NMConnection *src, *expected; const char *ssid = "blahblah"; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "open", 0 }, { NULL } }; @@ -939,7 +933,7 @@ static void test_priv_ap_dynamic_wep_3 (void) { NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "shared", 0 }, { NULL } }; @@ -1052,7 +1046,7 @@ test_wpa_ap_empty_connection (gconstpointer data) { guint idx = GPOINTER_TO_UINT (data); NMConnection *src, *expected; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *ssid = "blahblah"; const KeyData exp_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk", 0 }, @@ -1090,7 +1084,7 @@ test_wpa_ap_leap_connection_1 (gconstpointer data) guint idx = GPOINTER_TO_UINT (data); NMConnection *src; const char *ssid = "blahblah"; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const char *leap_username = "Bill Smith"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, @@ -1125,7 +1119,7 @@ test_wpa_ap_leap_connection_2 (gconstpointer data) { guint idx = GPOINTER_TO_UINT (data); NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, "leap", 0 }, @@ -1159,7 +1153,7 @@ test_wpa_ap_dynamic_wep_connection (gconstpointer data) { guint idx = GPOINTER_TO_UINT (data); NMConnection *src; - const guint8 bssid[ETH_ALEN] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; + const char *bssid = "01:02:03:04:05:06"; const KeyData src_wsec[] = { { NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "ieee8021x", 0 }, { NULL } }; diff --git a/src/devices/wimax/nm-device-wimax.c b/src/devices/wimax/nm-device-wimax.c index 9ffbd89fa6..d428f1d2eb 100644 --- a/src/devices/wimax/nm-device-wimax.c +++ b/src/devices/wimax/nm-device-wimax.c @@ -315,7 +315,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) NMSettingConnection *s_con; NMSettingWimax *s_wimax; const char *connection_type; - const GByteArray *mac; + const char *mac; if (!NM_DEVICE_CLASS (nm_device_wimax_parent_class)->check_connection_compatible (device, connection)) return FALSE; @@ -332,7 +332,7 @@ check_connection_compatible (NMDevice *device, NMConnection *connection) return FALSE; mac = nm_setting_wimax_get_mac_address (s_wimax); - if (mac && !nm_utils_hwaddr_matches (mac->data, mac->len, nm_device_get_hw_address (device), -1)) + if (mac && !nm_utils_hwaddr_matches (mac, -1, nm_device_get_hw_address (device), -1)) return FALSE; return TRUE; @@ -371,7 +371,7 @@ complete_connection (NMDevice *device, NMDeviceWimax *self = NM_DEVICE_WIMAX (device); NMDeviceWimaxPrivate *priv = NM_DEVICE_WIMAX_GET_PRIVATE (self); NMSettingWimax *s_wimax; - const GByteArray *setting_mac; + const char *setting_mac; const char *hw_address; const char *nsp_name = NULL; NMWimaxNsp *nsp = NULL; @@ -450,7 +450,7 @@ complete_connection (NMDevice *device, hw_address = nm_device_get_hw_address (device); if (setting_mac) { /* Make sure the setting MAC (if any) matches the device's permanent MAC */ - if (!nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hw_address, -1)) { + if (!nm_utils_hwaddr_matches (setting_mac, -1, hw_address, -1)) { g_set_error (error, NM_SETTING_WIMAX_ERROR, NM_SETTING_WIMAX_ERROR_INVALID_PROPERTY, @@ -458,14 +458,9 @@ complete_connection (NMDevice *device, return FALSE; } } else { - GByteArray *mac; - /* Lock the connection to this device by default */ - if (!nm_utils_hwaddr_matches (hw_address, -1, NULL, ETH_ALEN)) { - mac = nm_utils_hwaddr_atoba (hw_address, ETH_ALEN); - g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); - } + if (!nm_utils_hwaddr_matches (hw_address, -1, NULL, ETH_ALEN)) + g_object_set (G_OBJECT (s_wimax), NM_SETTING_WIMAX_MAC_ADDRESS, hw_address, NULL); } return TRUE; diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index dc23490a79..ded7060bbc 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -348,7 +348,7 @@ nm_dhcp_client_watch_child (NMDhcpClient *self, pid_t pid) gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname) { NMDhcpClientPrivate *priv; @@ -480,7 +480,7 @@ get_duid (NMDhcpClient *self) gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy) diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index f458b13ff8..89fe821db5 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -65,11 +65,11 @@ typedef struct { gboolean (*ip4_start) (NMDhcpClient *self, const char *dhcp_client_id, - GByteArray *anycast_addr, + const char *anycast_addr, const char *hostname); gboolean (*ip6_start) (NMDhcpClient *self, - GByteArray *anycast_addr, + const char *anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, @@ -117,11 +117,11 @@ guint32 nm_dhcp_client_get_priority (NMDhcpClient *self); gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname); gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy); diff --git a/src/dhcp-manager/nm-dhcp-dhclient-utils.c b/src/dhcp-manager/nm-dhcp-dhclient-utils.c index 7991181f90..6c5be77cb8 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient-utils.c +++ b/src/dhcp-manager/nm-dhcp-dhclient-utils.c @@ -129,7 +129,7 @@ char * nm_dhcp_dhclient_create_config (const char *interface, gboolean is_ip6, const char *dhcp_client_id, - GByteArray *anycast_addr, + const char *anycast_addr, const char *hostname, const char *orig_path, const char *orig_contents) @@ -138,6 +138,8 @@ nm_dhcp_dhclient_create_config (const char *interface, GPtrArray *alsoreq; int i; + g_return_val_if_fail (!anycast_addr || nm_utils_hwaddr_valid (anycast_addr, ETH_ALEN), NULL); + new_contents = g_string_new (_("# Created by NetworkManager\n")); alsoreq = g_ptr_array_sized_new (5); @@ -246,22 +248,14 @@ nm_dhcp_dhclient_create_config (const char *interface, g_string_append_c (new_contents, '\n'); - if (anycast_addr && anycast_addr->len == 6) { - const guint8 *p_anycast_addr = anycast_addr->data; - + if (anycast_addr) { g_string_append_printf (new_contents, "interface \"%s\" {\n" " initial-interval 1; \n" - " anycast-mac ethernet %02x:%02x:%02x:%02x:%02x:%02x;\n" + " anycast-mac ethernet %s;\n" "}\n", - interface, - p_anycast_addr[0], p_anycast_addr[1], - p_anycast_addr[2], p_anycast_addr[3], - p_anycast_addr[4], p_anycast_addr[5]); + interface, anycast_addr); } - /* Finally, assert that anycast_addr was unset or a 48 bit mac address. */ - g_return_val_if_fail (!anycast_addr || anycast_addr->len == 6, g_string_free (new_contents, FALSE)); - return g_string_free (new_contents, FALSE); } diff --git a/src/dhcp-manager/nm-dhcp-dhclient-utils.h b/src/dhcp-manager/nm-dhcp-dhclient-utils.h index 61da24877d..3b786a65a6 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient-utils.h +++ b/src/dhcp-manager/nm-dhcp-dhclient-utils.h @@ -28,7 +28,7 @@ char *nm_dhcp_dhclient_create_config (const char *interface, gboolean is_ip6, const char *dhcp_client_id, - GByteArray *anycast_addr, + const char *anycast_addr, const char *hostname, const char *orig_path, const char *orig_contents); diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index f2fe29057d..d5ea6a5486 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -166,7 +166,7 @@ merge_dhclient_config (const char *iface, const char *conf_file, gboolean is_ip6, const char *dhcp_client_id, - GByteArray *anycast_addr, + const char *anycast_addr, const char *hostname, const char *orig_path, GError **error) @@ -272,7 +272,7 @@ create_dhclient_config (const char *iface, gboolean is_ip6, const char *uuid, const char *dhcp_client_id, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname) { char *orig = NULL, *new = NULL; @@ -482,7 +482,7 @@ dhclient_start (NMDhcpClient *client, static gboolean ip4_start (NMDhcpClient *client, const char *dhcp_client_id, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); @@ -502,7 +502,7 @@ ip4_start (NMDhcpClient *client, static gboolean ip6_start (NMDhcpClient *client, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index 646fea9fd9..8345034b84 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -88,7 +88,7 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED) static gboolean ip4_start (NMDhcpClient *client, const char *dhcp_client_id, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname) { NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client); @@ -171,7 +171,7 @@ ip4_start (NMDhcpClient *client, static gboolean ip6_start (NMDhcpClient *client, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 332be3b012..a1bbbef50f 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -377,7 +377,7 @@ client_start (NMDhcpManager *self, gboolean ipv6, const char *dhcp_client_id, guint32 timeout, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy) @@ -452,7 +452,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, const char *dhcp_hostname, const char *dhcp_client_id, guint32 timeout, - GByteArray *dhcp_anycast_addr) + const char *dhcp_anycast_addr) { const char *hostname = NULL; @@ -475,7 +475,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, guint priority, const char *dhcp_hostname, guint32 timeout, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy) { diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 23b1fa10f6..16af333274 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -71,7 +71,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager, const char *dhcp_hostname, const char *dhcp_client_id, guint32 timeout, - GByteArray *dhcp_anycast_addr); + const char *dhcp_anycast_addr); NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, const char *iface, @@ -81,7 +81,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, guint priority, const char *dhcp_hostname, guint32 timeout, - GByteArray *dhcp_anycast_addr, + const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy); diff --git a/src/dhcp-manager/tests/test-dhcp-dhclient.c b/src/dhcp-manager/tests/test-dhcp-dhclient.c index 7b8bcef2ca..39a898a925 100644 --- a/src/dhcp-manager/tests/test-dhcp-dhclient.c +++ b/src/dhcp-manager/tests/test-dhcp-dhclient.c @@ -35,7 +35,7 @@ test_config (const char *orig, const char *hostname, const char *dhcp_client_id, const char *iface, - GByteArray *anycast_addr) + const char *anycast_addr) { char *new; diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index e1395ed31c..aab50aeb8d 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -337,8 +337,7 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_PLATFORM_SOURCE_USER; - if (label) - g_strlcpy (address.label, label, sizeof (address.label)); + g_strlcpy (address.label, label, sizeof (address.label)); nm_ip4_config_add_address (config, &address); } @@ -368,8 +367,12 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i nm_ip4_config_reset_domains (config); nm_ip4_config_reset_searches (config); } - for (i = 0; i < nnameservers; i++) - nm_ip4_config_add_nameserver (config, nm_setting_ip4_config_get_dns (setting, i)); + for (i = 0; i < nnameservers; i++) { + guint32 ip; + + if (inet_pton (AF_INET, nm_setting_ip4_config_get_dns (setting, i), &ip) == 1) + nm_ip4_config_add_nameserver (config, ip); + } for (i = 0; i < nsearches; i++) nm_ip4_config_add_search (config, nm_setting_ip4_config_get_dns_search (setting, i)); @@ -425,10 +428,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (same_prefix (address->address, gateway, address->plen)) nm_ip4_address_set_gateway (s_addr, gateway); - if (*address->label) - _nm_setting_ip4_config_add_address_with_label (s_ip4, s_addr, address->label); - else - nm_setting_ip4_config_add_address (s_ip4, s_addr); + _nm_setting_ip4_config_add_address_with_label (s_ip4, s_addr, address->label); nm_ip4_address_unref (s_addr); } @@ -464,7 +464,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) for (i = 0; i < nnameservers; i++) { guint32 nameserver = nm_ip4_config_get_nameserver (config, i); - nm_setting_ip4_config_add_dns (s_ip4, nameserver); + nm_setting_ip4_config_add_dns (s_ip4, nm_utils_inet4_ntop (nameserver, NULL)); } for (i = 0; i < nsearches; i++) { const char *search = nm_ip4_config_get_search (config, i); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 0d4307f288..9d2b61f2c5 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -470,8 +470,12 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i nm_ip6_config_reset_domains (config); nm_ip6_config_reset_searches (config); } - for (i = 0; i < nnameservers; i++) - nm_ip6_config_add_nameserver (config, nm_setting_ip6_config_get_dns (setting, i)); + for (i = 0; i < nnameservers; i++) { + struct in6_addr ip; + + if (inet_pton (AF_INET6, nm_setting_ip6_config_get_dns (setting, i), &ip) == 1) + nm_ip6_config_add_nameserver (config, &ip); + } for (i = 0; i < nsearches; i++) nm_ip6_config_add_search (config, nm_setting_ip6_config_get_dns_search (setting, i)); @@ -571,7 +575,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) for (i = 0; i < nnameservers; i++) { const struct in6_addr *nameserver = nm_ip6_config_get_nameserver (config, i); - nm_setting_ip6_config_add_dns (s_ip6, nameserver); + nm_setting_ip6_config_add_dns (s_ip6, nm_utils_inet6_ntop (nameserver, NULL)); } for (i = 0; i < nsearches; i++) { const char *search = nm_ip6_config_get_search (config, i); diff --git a/src/nm-manager.c b/src/nm-manager.c index 8597e3b533..bee2c55f11 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -842,7 +842,7 @@ nm_manager_get_state (NMManager *manager) /*******************************************************************/ static NMDevice * -get_device_from_hwaddr (NMManager *self, const GByteArray *setting_mac) +get_device_from_hwaddr (NMManager *self, const char *setting_mac) { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); const char *device_mac; @@ -857,7 +857,7 @@ get_device_from_hwaddr (NMManager *self, const GByteArray *setting_mac) device_mac = nm_device_get_hw_address (iter->data); if (!device_mac) continue; - if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, device_mac, -1)) + if (nm_utils_hwaddr_matches (setting_mac, -1, device_mac, -1)) return device; } return NULL; @@ -873,7 +873,7 @@ find_vlan_parent (NMManager *self, NMConnection *parent_connection; const char *parent_iface; NMDevice *parent = NULL; - const GByteArray *setting_mac; + const char *setting_mac; GSList *iter; /* The 'parent' property could be given by an interface name, a @@ -926,7 +926,7 @@ find_infiniband_parent (NMManager *self, NMSettingInfiniband *s_infiniband; const char *parent_iface; NMDevice *parent = NULL; - const GByteArray *setting_mac; + const char *setting_mac; s_infiniband = nm_connection_get_setting_infiniband (connection); g_return_val_if_fail (s_infiniband != NULL, NULL); diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index f63234feda..f4a9396022 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -754,7 +754,7 @@ mesh_set_channel (NMPlatform *platform, int ifindex, guint32 channel) } static gboolean -mesh_set_ssid (NMPlatform *platform, int ifindex, const GByteArray *ssid) +mesh_set_ssid (NMPlatform *platform, int ifindex, const guint8 *ssid, gsize len) { return FALSE; } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index e366a3b8e9..feb86010fd 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -3276,14 +3276,14 @@ mesh_set_channel (NMPlatform *platform, int ifindex, guint32 channel) } static gboolean -mesh_set_ssid (NMPlatform *platform, int ifindex, const GByteArray *ssid) +mesh_set_ssid (NMPlatform *platform, int ifindex, const guint8 *ssid, gsize len) { WifiData *wifi_data = wifi_get_wifi_data (platform, ifindex); if (!wifi_data) return FALSE; - return wifi_utils_set_mesh_ssid (wifi_data, ssid); + return wifi_utils_set_mesh_ssid (wifi_data, ssid, len); } static gboolean diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index afefb771e4..b3ffe70a6e 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1377,14 +1377,14 @@ nm_platform_mesh_set_channel (int ifindex, guint32 channel) } gboolean -nm_platform_mesh_set_ssid (int ifindex, const GByteArray *ssid) +nm_platform_mesh_set_ssid (int ifindex, const guint8 *ssid, gsize len) { reset_error (); g_return_val_if_fail (ifindex > 0, FALSE); g_return_val_if_fail (ssid != NULL, FALSE); - return klass->mesh_set_ssid (platform, ifindex, ssid); + return klass->mesh_set_ssid (platform, ifindex, ssid, len); } /******************************************************************/ diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 8bcc30ec89..e706d2d74c 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -433,7 +433,7 @@ typedef struct { guint32 (*mesh_get_channel) (NMPlatform *, int ifindex); gboolean (*mesh_set_channel) (NMPlatform *, int ifindex, guint32 channel); - gboolean (*mesh_set_ssid) (NMPlatform *, int ifindex, const GByteArray *ssid); + gboolean (*mesh_set_ssid) (NMPlatform *, int ifindex, const guint8 *ssid, gsize len); GArray * (*ip4_address_get_all) (NMPlatform *, int ifindex); GArray * (*ip6_address_get_all) (NMPlatform *, int ifindex); @@ -573,7 +573,7 @@ void nm_platform_wifi_indicate_addressing_running (int ifindex, gboolean guint32 nm_platform_mesh_get_channel (int ifindex); gboolean nm_platform_mesh_set_channel (int ifindex, guint32 channel); -gboolean nm_platform_mesh_set_ssid (int ifindex, const GByteArray *ssid); +gboolean nm_platform_mesh_set_ssid (int ifindex, const guint8 *ssid, gsize len); GArray *nm_platform_ip4_address_get_all (int ifindex); GArray *nm_platform_ip6_address_get_all (int ifindex); diff --git a/src/platform/wifi/wifi-utils-private.h b/src/platform/wifi/wifi-utils-private.h index 91417fe7ea..aaedd5b7a7 100644 --- a/src/platform/wifi/wifi-utils-private.h +++ b/src/platform/wifi/wifi-utils-private.h @@ -66,7 +66,7 @@ struct WifiData { gboolean (*set_mesh_channel) (WifiData *data, guint32 channel); /* ssid == NULL means "auto SSID" */ - gboolean (*set_mesh_ssid) (WifiData *data, const GByteArray *ssid); + gboolean (*set_mesh_ssid) (WifiData *data, const guint8 *ssid, gsize len); gboolean (*indicate_addressing_running) (WifiData *data, gboolean running); }; diff --git a/src/platform/wifi/wifi-utils-wext.c b/src/platform/wifi/wifi-utils-wext.c index 90c764f2af..73dc7f0118 100644 --- a/src/platform/wifi/wifi-utils-wext.c +++ b/src/platform/wifi/wifi-utils-wext.c @@ -420,18 +420,15 @@ wifi_wext_set_mesh_channel (WifiData *data, guint32 channel) } static gboolean -wifi_wext_set_mesh_ssid (WifiData *data, const GByteArray *ssid) +wifi_wext_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len) { WifiDataWext *wext = (WifiDataWext *) data; struct iwreq wrq; - guint32 len = 0; char buf[IW_ESSID_MAX_SIZE + 1]; memset (buf, 0, sizeof (buf)); - if (ssid) { - len = ssid->len; - memcpy (buf, ssid->data, MIN (sizeof (buf) - 1, len)); - } + memcpy (buf, ssid, MIN (sizeof (buf) - 1, len)); + wrq.u.essid.pointer = (caddr_t) buf; wrq.u.essid.length = len; wrq.u.essid.flags = (len > 0) ? 1 : 0; /* 1=enable SSID, 0=disable/any */ @@ -444,7 +441,7 @@ wifi_wext_set_mesh_ssid (WifiData *data, const GByteArray *ssid) nm_log_err (LOGD_HW | LOGD_WIFI | LOGD_OLPC, "(%s): error setting SSID to '%s': %s", wext->parent.iface, - ssid ? nm_utils_escape_ssid (ssid->data, ssid->len) : "(null)", + ssid ? nm_utils_escape_ssid (ssid, len) : "(null)", strerror (errno)); } diff --git a/src/platform/wifi/wifi-utils.c b/src/platform/wifi/wifi-utils.c index 20eea22cd2..679b17a041 100644 --- a/src/platform/wifi/wifi-utils.c +++ b/src/platform/wifi/wifi-utils.c @@ -207,11 +207,11 @@ wifi_utils_set_mesh_channel (WifiData *data, guint32 channel) } gboolean -wifi_utils_set_mesh_ssid (WifiData *data, const GByteArray *ssid) +wifi_utils_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len) { g_return_val_if_fail (data != NULL, FALSE); g_return_val_if_fail (data->set_mesh_ssid != NULL, FALSE); - return data->set_mesh_ssid (data, ssid); + return data->set_mesh_ssid (data, ssid, len); } gboolean diff --git a/src/platform/wifi/wifi-utils.h b/src/platform/wifi/wifi-utils.h index a08ed8a2bb..984dee775e 100644 --- a/src/platform/wifi/wifi-utils.h +++ b/src/platform/wifi/wifi-utils.h @@ -71,6 +71,6 @@ guint32 wifi_utils_get_mesh_channel (WifiData *data); gboolean wifi_utils_set_mesh_channel (WifiData *data, guint32 channel); -gboolean wifi_utils_set_mesh_ssid (WifiData *data, const GByteArray *ssid); +gboolean wifi_utils_set_mesh_ssid (WifiData *data, const guint8 *ssid, gsize len); #endif /* __WIFI_UTILS_H__ */ diff --git a/src/settings/nm-agent-manager.c b/src/settings/nm-agent-manager.c index dda550c5ce..a2c0c6bb5f 100644 --- a/src/settings/nm-agent-manager.c +++ b/src/settings/nm-agent-manager.c @@ -887,8 +887,7 @@ set_secrets_not_required (NMConnection *connection, GHashTable *hash) * "secrets" property is actually a hash table of secrets. */ if ( strcmp (setting_name, NM_SETTING_VPN_SETTING_NAME) == 0 - && strcmp (key_name, NM_SETTING_VPN_SECRETS) == 0 - && G_VALUE_HOLDS (val, DBUS_TYPE_G_MAP_OF_STRING)) { + && strcmp (key_name, NM_SETTING_VPN_SECRETS) == 0) { GHashTableIter vpn_secret_iter; const char *secret_name; diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 0b821ecb9b..9e9f56b28d 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -1138,7 +1138,7 @@ get_settings_auth_cb (NMSettingsConnection *self, NMSettingConnection *s_con; NMSettingWireless *s_wifi; guint64 timestamp = 0; - GSList *bssid_list; + char **bssids; dupl_con = nm_simple_connection_new_clone (NM_CONNECTION (self)); g_assert (dupl_con); @@ -1159,11 +1159,11 @@ get_settings_auth_cb (NMSettingsConnection *self, * from the same reason as timestamp. Thus we put it here to GetSettings() * return settings too. */ - bssid_list = nm_settings_connection_get_seen_bssids (self); + bssids = nm_settings_connection_get_seen_bssids (self); s_wifi = nm_connection_get_setting_wireless (NM_CONNECTION (dupl_con)); - if (bssid_list && s_wifi) - g_object_set (s_wifi, NM_SETTING_WIRELESS_SEEN_BSSIDS, bssid_list, NULL); - g_slist_free (bssid_list); + if (bssids && bssids[0] && s_wifi) + g_object_set (s_wifi, NM_SETTING_WIRELESS_SEEN_BSSIDS, bssids, NULL); + g_free (bssids); /* Secrets should *never* be returned by the GetSettings method, they * get returned by the GetSecrets method which can be better @@ -1703,31 +1703,6 @@ nm_settings_connection_read_and_fill_timestamp (NMSettingsConnection *connection g_key_file_free (timestamps_file); } -static guint -mac_hash (gconstpointer v) -{ - const guint8 *p = v; - guint32 i, h = 5381; - - for (i = 0; i < ETH_ALEN; i++) - h = (h << 5) + h + p[i]; - return h; -} - -static gboolean -mac_equal (gconstpointer a, gconstpointer b) -{ - return memcmp (a, b, ETH_ALEN) == 0; -} - -static guint8 * -mac_dup (const guint8 *old) -{ - g_return_val_if_fail (old != NULL, NULL); - - return g_memdup (old, ETH_ALEN); -} - /** * nm_settings_connection_get_seen_bssids: * @connection: the #NMSettingsConnection @@ -1737,12 +1712,25 @@ mac_dup (const guint8 *old) * Returns: (transfer container) list of seen BSSIDs (in the standard hex-digits-and-colons notation). * The caller is responsible for freeing the list, but not the content. **/ -GSList * +char ** nm_settings_connection_get_seen_bssids (NMSettingsConnection *connection) { + NMSettingsConnectionPrivate *priv = NM_SETTINGS_CONNECTION_GET_PRIVATE (connection); + GHashTableIter iter; + char **bssids, *bssid; + int i; + g_return_val_if_fail (NM_IS_SETTINGS_CONNECTION (connection), NULL); - return _nm_utils_hash_values_to_slist (NM_SETTINGS_CONNECTION_GET_PRIVATE (connection)->seen_bssids); + bssids = g_new (char *, g_hash_table_size (priv->seen_bssids) + 1); + + i = 0; + g_hash_table_iter_init (&iter, priv->seen_bssids); + while (g_hash_table_iter_next (&iter, NULL, (gpointer) &bssid)) + bssids[i++] = bssid; + bssids[i] = NULL; + + return bssids; } /** @@ -1754,7 +1742,7 @@ nm_settings_connection_get_seen_bssids (NMSettingsConnection *connection) **/ gboolean nm_settings_connection_has_seen_bssid (NMSettingsConnection *connection, - const guint8 *bssid) + const char *bssid) { g_return_val_if_fail (NM_IS_SETTINGS_CONNECTION (connection), FALSE); g_return_val_if_fail (bssid != NULL, FALSE); @@ -1772,7 +1760,7 @@ nm_settings_connection_has_seen_bssid (NMSettingsConnection *connection, **/ void nm_settings_connection_add_seen_bssid (NMSettingsConnection *connection, - const guint8 *seen_bssid) + const char *seen_bssid) { NMSettingsConnectionPrivate *priv = NM_SETTINGS_CONNECTION_GET_PRIVATE (connection); const char *connection_uuid; @@ -1790,8 +1778,8 @@ nm_settings_connection_add_seen_bssid (NMSettingsConnection *connection, return; /* Already in the list */ /* Add the new BSSID; let the hash take ownership of the allocated BSSID string */ - bssid_str = nm_utils_hwaddr_ntoa (seen_bssid, ETH_ALEN); - g_hash_table_insert (priv->seen_bssids, mac_dup (seen_bssid), bssid_str); + bssid_str = g_strdup (seen_bssid); + g_hash_table_insert (priv->seen_bssids, bssid_str, bssid_str); /* Build up a list of all the BSSIDs in string form */ n = 0; @@ -1829,19 +1817,6 @@ nm_settings_connection_add_seen_bssid (NMSettingsConnection *connection, } } -static void -add_seen_bssid_string (NMSettingsConnection *self, const char *bssid) -{ - guint8 mac[ETH_ALEN]; - - g_return_if_fail (bssid != NULL); - if (nm_utils_hwaddr_aton (bssid, mac, ETH_ALEN)) { - g_hash_table_insert (NM_SETTINGS_CONNECTION_GET_PRIVATE (self)->seen_bssids, - mac_dup (mac), - g_strdup (bssid)); - } -} - /** * nm_settings_connection_read_and_fill_seen_bssids: * @connection: the #NMSettingsConnection @@ -1872,8 +1847,8 @@ nm_settings_connection_read_and_fill_seen_bssids (NMSettingsConnection *connecti if (tmp_strv) { g_hash_table_remove_all (priv->seen_bssids); for (i = 0; i < len; i++) - add_seen_bssid_string (connection, tmp_strv[i]); - g_strfreev (tmp_strv); + g_hash_table_insert (priv->seen_bssids, tmp_strv[i], tmp_strv[i]); + g_free (tmp_strv); } else { /* If this connection didn't have an entry in the seen-bssids database, * maybe this is the first time we've read it in, so populate the @@ -1883,8 +1858,11 @@ nm_settings_connection_read_and_fill_seen_bssids (NMSettingsConnection *connecti s_wifi = nm_connection_get_setting_wireless (NM_CONNECTION (connection)); if (s_wifi) { len = nm_setting_wireless_get_num_seen_bssids (s_wifi); - for (i = 0; i < len; i++) - add_seen_bssid_string (connection, nm_setting_wireless_get_seen_bssid (s_wifi, i)); + for (i = 0; i < len; i++) { + char *bssid_dup = g_strdup (nm_setting_wireless_get_seen_bssid (s_wifi, i)); + + g_hash_table_insert (priv->seen_bssids, bssid_dup, bssid_dup); + } } } } @@ -2011,7 +1989,7 @@ nm_settings_connection_init (NMSettingsConnection *self) priv->agent_mgr = nm_agent_manager_get (); - priv->seen_bssids = g_hash_table_new_full (mac_hash, mac_equal, g_free, g_free); + priv->seen_bssids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); priv->autoconnect_retries = AUTOCONNECT_RETRIES_DEFAULT; priv->autoconnect_blocked_reason = NM_DEVICE_STATE_REASON_NONE; diff --git a/src/settings/nm-settings-connection.h b/src/settings/nm-settings-connection.h index 8e29adc2a1..3ee62465fd 100644 --- a/src/settings/nm-settings-connection.h +++ b/src/settings/nm-settings-connection.h @@ -138,13 +138,13 @@ void nm_settings_connection_update_timestamp (NMSettingsConnection *connection, void nm_settings_connection_read_and_fill_timestamp (NMSettingsConnection *connection); -GSList *nm_settings_connection_get_seen_bssids (NMSettingsConnection *connection); +char **nm_settings_connection_get_seen_bssids (NMSettingsConnection *connection); gboolean nm_settings_connection_has_seen_bssid (NMSettingsConnection *connection, - const guint8 *bssid); + const char *bssid); void nm_settings_connection_add_seen_bssid (NMSettingsConnection *connection, - const guint8 *seen_bssid); + const char *seen_bssid); void nm_settings_connection_read_and_fill_seen_bssids (NMSettingsConnection *connection); diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 5499f43bb8..2957a21004 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -1472,12 +1472,12 @@ have_connection_for_device (NMSettings *self, NMDevice *device) gpointer data; NMSettingConnection *s_con; NMSettingWired *s_wired; - const GByteArray *setting_mac; - const char *hwaddr; + const char *setting_hwaddr; + const char *device_hwaddr; g_return_val_if_fail (NM_IS_SETTINGS (self), FALSE); - hwaddr = nm_device_get_hw_address (device); + device_hwaddr = nm_device_get_hw_address (device); /* Find a wired connection locked to the given MAC address, if any */ g_hash_table_iter_init (&iter, priv->connections); @@ -1505,10 +1505,10 @@ have_connection_for_device (NMSettings *self, NMDevice *device) g_assert (s_wired != NULL); - setting_mac = nm_setting_wired_get_mac_address (s_wired); - if (setting_mac && hwaddr) { + setting_hwaddr = nm_setting_wired_get_mac_address (s_wired); + if (setting_hwaddr) { /* A connection mac-locked to this device */ - if (nm_utils_hwaddr_matches (setting_mac->data, setting_mac->len, hwaddr, -1)) + if (nm_utils_hwaddr_matches (setting_hwaddr, -1, device_hwaddr, -1)) return TRUE; } else { /* A connection that applies to any wired device */ diff --git a/src/settings/plugins/ibft/reader.c b/src/settings/plugins/ibft/reader.c index 908ba69016..57fdb34536 100644 --- a/src/settings/plugins/ibft/reader.c +++ b/src/settings/plugins/ibft/reader.c @@ -367,10 +367,10 @@ ip4_setting_add_from_block (const GPtrArray *block, nm_setting_ip4_config_add_address (s_ip4, addr); nm_ip4_address_unref (addr); - if (dns1) - nm_setting_ip4_config_add_dns (s_ip4, dns1); - if (dns2) - nm_setting_ip4_config_add_dns (s_ip4, dns2); + if (s_dns1) + nm_setting_ip4_config_add_dns (s_ip4, s_dns1); + if (s_dns2) + nm_setting_ip4_config_add_dns (s_ip4, s_dns2); success: nm_connection_add_setting (connection, NM_SETTING (s_ip4)); @@ -489,29 +489,26 @@ wired_setting_add_from_block (const GPtrArray *block, GError **error) { NMSetting *s_wired = NULL; - const char *hwaddr_str = NULL; - GByteArray *hwaddr; + const char *hwaddr = NULL; g_assert (block); g_assert (connection); - if (!parse_ibft_config (block, NULL, ISCSI_HWADDR_TAG, &hwaddr_str, NULL)) { + if (!parse_ibft_config (block, NULL, ISCSI_HWADDR_TAG, &hwaddr, NULL)) { g_set_error_literal (error, IBFT_PLUGIN_ERROR, 0, "iBFT: malformed iscsiadm record: missing " ISCSI_HWADDR_TAG); return FALSE; } - hwaddr = nm_utils_hwaddr_atoba (hwaddr_str, ETH_ALEN); - if (!hwaddr) { + if (!nm_utils_hwaddr_valid (hwaddr, ETH_ALEN)) { g_set_error (error, IBFT_PLUGIN_ERROR, 0, "iBFT: malformed iscsiadm record: invalid " ISCSI_HWADDR_TAG " '%s'.", - hwaddr_str); + hwaddr); return FALSE; } s_wired = nm_setting_wired_new (); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, hwaddr, NULL); - g_byte_array_unref (hwaddr); nm_connection_add_setting (connection, s_wired); return TRUE; diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 185e0f690b..8252dda769 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -79,7 +79,7 @@ test_read_ibft_dhcp (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - const GByteArray *array; + const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f1"; GPtrArray *block; @@ -103,9 +103,9 @@ test_read_ibft_dhcp (void) /* ===== WIRED SETTING ===== */ s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - nmtst_assert_hwaddr_equals (array->data, array->len, expected_mac_address); + mac_address = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac_address); + g_assert (nm_utils_hwaddr_matches (mac_address, -1, expected_mac_address, -1)); g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); /* ===== IPv4 SETTING ===== */ @@ -124,7 +124,7 @@ test_read_ibft_static (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - const GByteArray *array; + const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; NMIP4Address *ip4_addr; GPtrArray *block; @@ -149,9 +149,9 @@ test_read_ibft_static (void) /* ===== WIRED SETTING ===== */ s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - nmtst_assert_hwaddr_equals (array->data, array->len, expected_mac_address); + mac_address = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac_address); + g_assert (nm_utils_hwaddr_matches (mac_address, -1, expected_mac_address, -1)); g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); /* ===== IPv4 SETTING ===== */ @@ -160,8 +160,8 @@ test_read_ibft_static (void) g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - nmtst_assert_ip4_address_equals (nm_setting_ip4_config_get_dns (s_ip4, 0), "10.16.255.2"); - nmtst_assert_ip4_address_equals (nm_setting_ip4_config_get_dns (s_ip4, 1), "10.16.255.3"); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "10.16.255.2"); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "10.16.255.3"); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); @@ -224,7 +224,7 @@ test_read_ibft_vlan (void) NMSettingWired *s_wired; NMSettingVlan *s_vlan; NMSettingIP4Config *s_ip4; - const GByteArray *array; + const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; NMIP4Address *ip4_addr; GError *error = NULL; @@ -243,9 +243,9 @@ test_read_ibft_vlan (void) /* ===== WIRED SETTING ===== */ s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - nmtst_assert_hwaddr_equals (array->data, array->len, expected_mac_address); + mac_address = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac_address); + g_assert (nm_utils_hwaddr_matches (mac_address, -1, expected_mac_address, -1)); /* ===== VLAN SETTING ===== */ s_vlan = nm_connection_get_setting_vlan (connection); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 4a8a686733..dfc8d289c1 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -244,36 +244,6 @@ make_connection_setting (const char *file, return NM_SETTING (s_con); } -static gboolean -read_mac_address (shvarFile *ifcfg, const char *key, gsize len, - GByteArray **array, GError **error) -{ - char *value = NULL; - - g_return_val_if_fail (ifcfg != NULL, FALSE); - g_return_val_if_fail (array != NULL, FALSE); - g_return_val_if_fail (*array == NULL, FALSE); - if (error) - g_return_val_if_fail (*error == NULL, FALSE); - - value = svGetValue (ifcfg, key, FALSE); - if (!value || !strlen (value)) { - g_free (value); - return TRUE; - } - - *array = nm_utils_hwaddr_atoba (value, len); - if (!*array) { - g_set_error (error, IFCFG_PLUGIN_ERROR, 0, - "%s: the MAC address '%s' was invalid.", key, value); - g_free (value); - return FALSE; - } - - g_free (value); - return TRUE; -} - /* Returns TRUE on missing address or valid address */ static gboolean read_ip4_address (shvarFile *ifcfg, @@ -1136,25 +1106,26 @@ make_ip4_setting (shvarFile *ifcfg, struct in6_addr ip6_dns; tag = g_strdup_printf ("DNS%u", i); - if (!read_ip4_address (ifcfg, tag, &dns, error)) { - gboolean valid = TRUE; + value = svGetValue (ifcfg, tag, FALSE); + if (value) { + if (!read_ip4_address (ifcfg, tag, &dns, error)) { + gboolean valid = TRUE; - /* Ignore IPv6 addresses */ - dns = 0; - value = svGetValue (ifcfg, tag, FALSE); - if (value) + /* Ignore IPv6 addresses */ valid = parse_ip6_address (value, &ip6_dns, NULL); - g_free (value); - - if (!valid) { - g_free (tag); - goto done; + if (!valid) { + g_free (tag); + goto done; + } + g_clear_error (error); + dns = 0; } - g_clear_error (error); + + if (dns && !nm_setting_ip4_config_add_dns (s_ip4, value)) + PARSE_WARNING ("duplicate DNS server %s", tag); + g_free (value); } - if (dns && !nm_setting_ip4_config_add_dns (s_ip4, dns)) - PARSE_WARNING ("duplicate DNS server %s", tag); g_free (tag); } @@ -1253,10 +1224,11 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo g_return_if_fail (s_ip4 != NULL); g_return_if_fail (filename != NULL); - base_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - if (!base_addr) + if (nm_setting_ip4_config_get_num_addresses (s_ip4) == 0) return; + base_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + dirname = g_path_get_dirname (filename); g_return_if_fail (dirname != NULL); base = g_path_get_basename (filename); @@ -1527,7 +1499,7 @@ make_ip6_setting (shvarFile *ifcfg, ip6_dns = in6addr_any; if (parse_ip6_address (value, &ip6_dns, NULL)) { - if (!IN6_IS_ADDR_UNSPECIFIED (&ip6_dns) && !nm_setting_ip6_config_add_dns (s_ip6, &ip6_dns)) + if (!IN6_IS_ADDR_UNSPECIFIED (&ip6_dns) && !nm_setting_ip6_config_add_dns (s_ip6, value)) PARSE_WARNING ("duplicate DNS server %s", tag); } else { /* Maybe an IPv4 address? If so ignore it */ @@ -2310,7 +2282,7 @@ fill_wpa_ciphers (shvarFile *ifcfg, static char * parse_wpa_psk (shvarFile *ifcfg, const char *file, - const GByteArray *ssid, + GBytes *ssid, GError **error) { shvarFile *keys_ifcfg; @@ -2945,8 +2917,7 @@ read_8021x_list_value (shvarFile *ifcfg, const char *prop_name) { char *value; - char **strv, **iter; - GSList *gslist = NULL; + char **strv; g_return_if_fail (ifcfg != NULL); g_return_if_fail (ifcfg_var_name != NULL); @@ -2957,16 +2928,8 @@ read_8021x_list_value (shvarFile *ifcfg, return; strv = g_strsplit_set (value, " \t", 0); - for (iter = strv; iter && *iter; iter++) { - if (*iter[0] == '\0') - continue; - gslist = g_slist_prepend (gslist, *iter); - } - if (gslist) { - gslist = g_slist_reverse (gslist); - g_object_set (setting, prop_name, gslist, NULL); - g_slist_free (gslist); - } + if (strv && strv[0]) + g_object_set (setting, prop_name, strv, NULL); g_strfreev (strv); g_free (value); } @@ -3075,7 +3038,7 @@ error: static NMSetting * make_wpa_setting (shvarFile *ifcfg, const char *file, - const GByteArray *ssid, + GBytes *ssid, gboolean adhoc, NMSetting8021x **s_8021x, GError **error) @@ -3245,7 +3208,7 @@ error: static NMSetting * make_wireless_security_setting (shvarFile *ifcfg, const char *file, - const GByteArray *ssid, + GBytes *ssid, gboolean adhoc, NMSetting8021x **s_8021x, GError **error) @@ -3280,54 +3243,33 @@ make_wireless_setting (shvarFile *ifcfg, GError **error) { NMSettingWireless *s_wireless; - GByteArray *array = NULL; - GSList *macaddr_blacklist = NULL; - char *value; + GBytes *bytes = NULL; + char *value = NULL; s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); - if (read_mac_address (ifcfg, "HWADDR", ETH_ALEN, &array, error)) { - if (array) { - g_object_set (s_wireless, NM_SETTING_WIRELESS_MAC_ADDRESS, array, NULL); - g_byte_array_free (array, TRUE); - } - } else { - g_object_unref (s_wireless); - return NULL; + value = svGetValue (ifcfg, "HWADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_wireless, NM_SETTING_WIRELESS_MAC_ADDRESS, value, NULL); + g_free (value); } - array = NULL; - if (read_mac_address (ifcfg, "MACADDR", ETH_ALEN, &array, error)) { - if (array) { - g_object_set (s_wireless, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, array, NULL); - g_byte_array_free (array, TRUE); - } - } else { - PARSE_WARNING ("%s", (*error)->message); - g_clear_error (error); + value = svGetValue (ifcfg, "MACADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_wireless, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, value, NULL); + g_free (value); } value = svGetValue (ifcfg, "HWADDR_BLACKLIST", FALSE); if (value) { - char **list = NULL, **iter; + char **list; list = g_strsplit_set (value, " \t", 0); - for (iter = list; iter && *iter; iter++) { - if (**iter == '\0') - continue; - if (!nm_utils_hwaddr_valid (*iter, ETH_ALEN)) { - PARSE_WARNING ("invalid MAC in HWADDR_BLACKLIST '%s'", *iter); - continue; - } - macaddr_blacklist = g_slist_prepend (macaddr_blacklist, *iter); - } - if (macaddr_blacklist) { - macaddr_blacklist = g_slist_reverse (macaddr_blacklist); - g_object_set (s_wireless, NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST, macaddr_blacklist, NULL); - g_slist_free (macaddr_blacklist); - } - g_free (value); + g_object_set (s_wireless, NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST, list, NULL); g_strfreev (list); + g_free (value); } value = svGetValue (ifcfg, "ESSID", TRUE); @@ -3382,10 +3324,9 @@ make_wireless_setting (shvarFile *ifcfg, goto error; } - array = g_byte_array_sized_new (ssid_len); - g_byte_array_append (array, (const guint8 *) p, ssid_len); - g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, array, NULL); - g_byte_array_free (array, TRUE); + bytes = g_bytes_new (p, ssid_len); + g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, bytes, NULL); + g_bytes_unref (bytes); g_free (value); } @@ -3415,18 +3356,8 @@ make_wireless_setting (shvarFile *ifcfg, value = svGetValue (ifcfg, "BSSID", FALSE); if (value) { - GByteArray *bssid; - - bssid = nm_utils_hwaddr_atoba (value, ETH_ALEN); - if (!bssid) { - g_set_error (error, IFCFG_PLUGIN_ERROR, 0, - "Invalid BSSID '%s'", value); - g_free (value); - goto error; - } - - g_object_set (s_wireless, NM_SETTING_WIRELESS_BSSID, bssid, NULL); - g_byte_array_free (bssid, TRUE); + value = g_strstrip (value); + g_object_set (s_wireless, NM_SETTING_WIRELESS_BSSID, value, NULL); g_free (value); } @@ -3488,7 +3419,7 @@ wireless_connection_from_ifcfg (const char *file, NMSetting *con_setting = NULL; NMSetting *wireless_setting = NULL; NMSetting8021x *s_8021x = NULL; - const GByteArray *ssid; + GBytes *ssid; NMSetting *security_setting = NULL; char *printable_ssid = NULL; const char *mode; @@ -3510,9 +3441,10 @@ wireless_connection_from_ifcfg (const char *file, nm_connection_add_setting (connection, wireless_setting); ssid = nm_setting_wireless_get_ssid (NM_SETTING_WIRELESS (wireless_setting)); - if (ssid) - printable_ssid = nm_utils_ssid_to_utf8 (ssid); - else + if (ssid) { + printable_ssid = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + } else printable_ssid = g_strdup_printf ("unmanaged"); mode = nm_setting_wireless_get_mode (NM_SETTING_WIRELESS (wireless_setting)); @@ -3557,7 +3489,6 @@ make_wired_setting (shvarFile *ifcfg, NMSettingWired *s_wired; char *value = NULL; int mtu; - GByteArray *mac = NULL; GSList *macaddr_blacklist = NULL; char *nettype; @@ -3575,14 +3506,11 @@ make_wired_setting (shvarFile *ifcfg, g_free (value); } - if (read_mac_address (ifcfg, "HWADDR", ETH_ALEN, &mac, error)) { - if (mac) { - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); - } - } else { - g_object_unref (s_wired); - return NULL; + value = svGetValue (ifcfg, "HWADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, value, NULL); + g_free (value); } value = svGetValue (ifcfg, "SUBCHANNELS", FALSE); @@ -3609,17 +3537,8 @@ make_wired_setting (shvarFile *ifcfg, if (num_chans < 2 || num_chans > 3) { PARSE_WARNING ("invalid SUBCHANNELS '%s' (%d channels, 2 or 3 expected)", value, g_strv_length (chans)); - } else { - GPtrArray *array = g_ptr_array_sized_new (num_chans); - - g_ptr_array_add (array, chans[0]); - g_ptr_array_add (array, chans[1]); - if (num_chans == 3) - g_ptr_array_add (array, chans[2]); - - g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, array, NULL); - g_ptr_array_free (array, TRUE); - } + } else + g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, chans, NULL); g_strfreev (chans); } g_free (value); @@ -3666,15 +3585,11 @@ make_wired_setting (shvarFile *ifcfg, } g_free (value); - mac = NULL; - if (read_mac_address (ifcfg, "MACADDR", ETH_ALEN, &mac, error)) { - if (mac) { - g_object_set (s_wired, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); - } - } else { - PARSE_WARNING ("%s", (*error)->message); - g_clear_error (error); + value = svGetValue (ifcfg, "MACADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_wired, NM_SETTING_WIRED_CLONED_MAC_ADDRESS, value, NULL); + g_free (value); } value = svGetValue (ifcfg, "HWADDR_BLACKLIST", FALSE); @@ -3834,7 +3749,6 @@ make_infiniband_setting (shvarFile *ifcfg, { NMSettingInfiniband *s_infiniband; char *value = NULL; - GByteArray *mac = NULL; int mtu; s_infiniband = NM_SETTING_INFINIBAND (nm_setting_infiniband_new ()); @@ -3851,14 +3765,11 @@ make_infiniband_setting (shvarFile *ifcfg, g_free (value); } - if (read_mac_address (ifcfg, "HWADDR", INFINIBAND_ALEN, &mac, error)) { - if (mac) { - g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); - } - } else { - g_object_unref (s_infiniband); - return NULL; + value = svGetValue (ifcfg, "HWADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, value, NULL); + g_free (value); } if (svTrueValue (ifcfg, "CONNECTED_MODE", FALSE)) @@ -4221,7 +4132,6 @@ make_bridge_setting (shvarFile *ifcfg, guint32 u; gboolean stp = FALSE; gboolean stp_set = FALSE; - GByteArray *array = NULL; s_bridge = NM_SETTING_BRIDGE (nm_setting_bridge_new ()); @@ -4232,14 +4142,11 @@ make_bridge_setting (shvarFile *ifcfg, } g_free (value); - if (read_mac_address (ifcfg, "MACADDR", ETH_ALEN, &array, error)) { - if (array) { - g_object_set (s_bridge, NM_SETTING_BRIDGE_MAC_ADDRESS, array, NULL); - g_byte_array_free (array, TRUE); - } - } else { - PARSE_WARNING ("%s", (*error)->message); - g_clear_error (error); + value = svGetValue (ifcfg, "MACADDR", FALSE); + if (value) { + value = g_strstrip (value); + g_object_set (s_bridge, NM_SETTING_BRIDGE_MAC_ADDRESS, value, NULL); + g_free (value); } value = svGetValue (ifcfg, "STP", FALSE); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 467ac3b453..3be7d721e0 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -186,7 +186,7 @@ test_read_basic (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; const char *expected_id = "System test-minimal"; guint64 expected_timestamp = 0; @@ -218,10 +218,9 @@ test_read_basic (void) g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, &expected_mac_address[0], ETH_ALEN) == 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN)); /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -246,7 +245,7 @@ test_read_variables_corner_cases (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; const char *expected_zone = "'"; const char *expected_id = "\""; @@ -275,10 +274,9 @@ test_read_variables_corner_cases (void) g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 0); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, &expected_mac_address[0], ETH_ALEN) == 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN)); /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -396,16 +394,11 @@ test_read_wired_static (const char *file, NMSettingIP6Config *s_ip6; char *unmanaged = NULL; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee }; - const char *expected_dns1 = "4.2.2.1"; - const char *expected_dns2 = "4.2.2.2"; - guint32 addr; struct in6_addr addr6; const char *expected6_address1 = "dead:beaf::1"; const char *expected6_address2 = "dead:beaf::2"; - const char *expected6_dns1 = "1:2:3:4::a"; - const char *expected6_dns2 = "1:2:3:4::b"; NMIP4Address *ip4_addr; NMIP6Address *ip6_addr; gboolean success; @@ -432,10 +425,9 @@ test_read_wired_static (const char *file, g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 1492); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, &expected_mac_address[0], ETH_ALEN) == 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN)); /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -445,10 +437,8 @@ test_read_wired_static (const char *file, /* DNS Addresses */ g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - g_assert_cmpint (inet_pton (AF_INET, expected_dns1, &addr), >, 0); - g_assert_cmpint (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, addr); - g_assert_cmpint (inet_pton (AF_INET, expected_dns2, &addr), >, 0); - g_assert_cmpint (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, addr); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); /* IP addresses */ g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); @@ -467,10 +457,8 @@ test_read_wired_static (const char *file, /* DNS Addresses */ g_assert_cmpint (nm_setting_ip6_config_get_num_dns (s_ip6), ==, 2); - g_assert_cmpint (inet_pton (AF_INET6, expected6_dns1, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), &addr6)); - g_assert_cmpint (inet_pton (AF_INET6, expected6_dns2, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 1), &addr6)); + g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); + g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); /* IP addresses */ g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 2); @@ -552,13 +540,10 @@ test_read_wired_dhcp (void) char *route6file = NULL; gboolean ignore_error = FALSE; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee }; const char *tmp; const char *expected_id = "System test-wired-dhcp"; - const char *expected_dns1 = "4.2.2.1"; - const char *expected_dns2 = "4.2.2.2"; - guint32 addr; const char *expected_dhcp_hostname = "foobar"; connection = connection_from_file (TEST_IFCFG_WIRED_DHCP, @@ -623,18 +608,13 @@ test_read_wired_dhcp (void) NM_SETTING_WIRED_SETTING_NAME); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - ASSERT (array != NULL, + mac = nm_setting_wired_get_mac_address (s_wired); + ASSERT (mac != NULL, "wired-dhcp-verify-wired", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIRED_DHCP, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "wired-dhcp-verify-wired", "failed to verify %s: unexpected %s / %s key value length", - TEST_IFCFG_WIRED_DHCP, - NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "wired-dhcp-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP, NM_SETTING_WIRED_SETTING_NAME, @@ -681,23 +661,13 @@ test_read_wired_dhcp (void) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns1, &addr) > 0, - "wired-dhcp-verify-ip4", "failed to verify %s: couldn't convert DNS IP address #1", - TEST_IFCFG_WIRED_DHCP, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 0) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns2, &addr) > 0, - "wired-dhcp-verify-ip4", "failed to verify %s: couldn't convert DNS IP address #2", - TEST_IFCFG_WIRED_DHCP, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 1) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -740,10 +710,8 @@ test_read_wired_dhcp_plus_ip (void) /* DNS Addresses */ g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - g_assert_cmpint (inet_pton (AF_INET, "4.2.2.1", &addr4), >, 0); - g_assert_cmpint (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, addr4); - g_assert_cmpint (inet_pton (AF_INET, "4.2.2.2", &addr4), >, 0); - g_assert_cmpint (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, addr4); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); + g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); /* IP addresses */ g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); @@ -769,10 +737,8 @@ test_read_wired_dhcp_plus_ip (void) /* DNS Addresses */ g_assert_cmpint (nm_setting_ip6_config_get_num_dns (s_ip6), ==, 2); - g_assert_cmpint (inet_pton (AF_INET6, "1:2:3:4::a", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), &addr6)); - g_assert_cmpint (inet_pton (AF_INET6, "1:2:3:4::b", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 1), &addr6)); + g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); + g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); /* IP addresses */ g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 3); @@ -1541,8 +1507,6 @@ test_read_wired_ipv6_manual (void) guint32 expected_prefix1 = 56; guint32 expected_prefix2 = 64; guint32 expected_prefix3 = 96; - const char *expected_dns1 = "1:2:3:4::a"; - const char *expected_dns2 = "1:2:3:4::b"; NMIP6Address *ip6_addr; NMIP6Route *ip6_route; struct in6_addr addr; @@ -1770,19 +1734,13 @@ test_read_wired_ipv6_manual (void) NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected_dns1, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert DNS IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), &addr), + ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected_dns2, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert DNS IP address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 1), &addr), + ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "1:2:3:4::b") == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, @@ -1822,7 +1780,6 @@ test_read_wired_ipv6_only (void) const char *expected_id = "System test-wired-ipv6-only"; const char *expected_address1 = "1001:abba::1234"; guint32 expected_prefix1 = 56; - const char *expected_dns1 = "1:2:3:4::a"; NMIP6Address *ip6_addr; struct in6_addr addr; const char *method; @@ -1936,10 +1893,7 @@ test_read_wired_ipv6_only (void) NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected_dns1, &addr) > 0, - "wired-ipv6-only-verify-ip6", "failed to verify %s: couldn't convert DNS IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), &addr), + ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, @@ -2562,7 +2516,7 @@ test_read_wired_aliases_good (void) const char *expected_id = "System aliasem0"; int expected_num_addresses = 4, expected_prefix = 24; const char *expected_address[4] = { "192.168.1.5", "192.168.1.6", "192.168.1.9", "192.168.1.99" }; - const char *expected_label[4] = { NULL, "aliasem0:1", "aliasem0:2", "aliasem0:99" }; + const char *expected_label[4] = { "", "aliasem0:1", "aliasem0:2", "aliasem0:99" }; const char *expected_gateway[4] = { "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1" }; int i, j; @@ -2707,7 +2661,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) const char *tmp; int expected_num_addresses = 1, expected_prefix = 24; const char *expected_address = "192.168.1.5"; - const char *expected_label = NULL; + const char *expected_label = ""; const char *expected_gateway = "192.168.1.1"; NMIP4Address *ip4_addr; struct in_addr addr; @@ -2842,7 +2796,8 @@ test_read_wifi_open (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; const char *expected_id = "System blahblah (test-wifi-open)"; guint64 expected_timestamp = 0; @@ -2914,18 +2869,13 @@ test_read_wifi_open (void) NM_SETTING_WIRELESS_SETTING_NAME); /* MAC address */ - array = nm_setting_wireless_get_mac_address (s_wireless); - ASSERT (array != NULL, + mac = nm_setting_wireless_get_mac_address (s_wireless); + ASSERT (mac != NULL, "wifi-open-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_OPEN, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "wifi-open-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", - TEST_IFCFG_WIFI_OPEN, - NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "wifi-open-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_OPEN, NM_SETTING_WIRELESS_SETTING_NAME, @@ -2937,18 +2887,18 @@ test_read_wifi_open (void) NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MTU); - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-open-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_OPEN, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-open-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_OPEN, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-open-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_OPEN, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3101,7 +3051,7 @@ test_read_wifi_open_ssid_hex (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; const char *expected_id = "System blahblah (test-wifi-open-ssid-hex)"; const char *expected_ssid = "blahblah"; @@ -3150,18 +3100,18 @@ test_read_wifi_open_ssid_hex (void) NM_SETTING_WIRELESS_SETTING_NAME); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-open-ssid-hex-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_OPEN_SSID_HEX, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-open-ssid-hex-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_OPEN_SSID_HEX, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-open-ssid-hex-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_OPEN_SSID_HEX, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3218,7 +3168,7 @@ test_read_wifi_open_ssid_quoted (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; const char *expected_id = "System foo\"bar\\ (test-wifi-open-ssid-quoted)"; const char *expected_ssid = "foo\"bar\\"; @@ -3267,18 +3217,18 @@ test_read_wifi_open_ssid_quoted (void) NM_SETTING_WIRELESS_SETTING_NAME); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-open-ssid-quoted-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_OPEN_SSID_QUOTED, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-open-ssid-quoted-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_OPEN_SSID_QUOTED, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-open-ssid-quoted-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_OPEN_SSID_QUOTED, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3308,7 +3258,8 @@ test_read_wifi_wep (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; const char *expected_id = "System blahblah (test-wifi-wep)"; guint64 expected_timestamp = 0; @@ -3382,18 +3333,13 @@ test_read_wifi_wep (void) NM_SETTING_WIRELESS_SETTING_NAME); /* MAC address */ - array = nm_setting_wireless_get_mac_address (s_wireless); - ASSERT (array != NULL, + mac = nm_setting_wireless_get_mac_address (s_wireless); + ASSERT (mac != NULL, "wifi-wep-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WEP, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "wifi-wep-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", - TEST_IFCFG_WIFI_WEP, - NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "wifi-wep-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3407,18 +3353,18 @@ test_read_wifi_wep (void) NM_SETTING_WIRELESS_MTU); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-wep-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WEP, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-wep-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_WEP, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-wep-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3570,14 +3516,11 @@ test_read_wifi_wep_adhoc (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; const char *expected_id = "System blahblah (test-wifi-wep-adhoc)"; const char *expected_ssid = "blahblah"; const char *expected_mode = "adhoc"; const char *expected_wep_key0 = "0123456789abcdef0123456789"; - guint32 addr; - const char *expected_dns1 = "4.2.2.1"; - const char *expected_dns2 = "4.2.2.2"; connection = connection_from_file (TEST_IFCFG_WIFI_WEP_ADHOC, NULL, @@ -3636,18 +3579,18 @@ test_read_wifi_wep_adhoc (void) NM_SETTING_WIRELESS_SETTING_NAME); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-wep-adhoc-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-wep-adhoc-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-wep-adhoc-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_WIRELESS_SETTING_NAME, @@ -3777,23 +3720,13 @@ test_read_wifi_wep_adhoc (void) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns1, &addr) > 0, - "wifi-wep-adhoc-verify-ip4", "failed to verify %s: couldn't convert DNS IP address #1", - TEST_IFCFG_WIFI_WEP_ADHOC, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 0) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns2, &addr) > 0, - "wifi-wep-adhoc-verify-ip4", "failed to verify %s: couldn't convert DNS IP address #2", - TEST_IFCFG_WIFI_WEP_ADHOC, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 1) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -4375,7 +4308,8 @@ test_read_wifi_wpa_psk (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; const char *expected_id = "System blahblah (test-wifi-wpa-psk)"; guint64 expected_timestamp = 0; @@ -4458,18 +4392,13 @@ test_read_wifi_wpa_psk (void) NM_SETTING_WIRELESS_SETTING_NAME); /* MAC address */ - array = nm_setting_wireless_get_mac_address (s_wireless); - ASSERT (array != NULL, + mac = nm_setting_wireless_get_mac_address (s_wireless); + ASSERT (mac != NULL, "wifi-wpa-psk-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "wifi-wpa-psk-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", - TEST_IFCFG_WIFI_WPA_PSK, - NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "wifi-wpa-psk-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_WIRELESS_SETTING_NAME, @@ -4483,18 +4412,18 @@ test_read_wifi_wpa_psk (void) NM_SETTING_WIRELESS_MTU); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-wpa-psk-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-wpa-psk-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-wpa-psk-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_WIRELESS_SETTING_NAME, @@ -5076,7 +5005,7 @@ test_read_wifi_wpa_psk_hex (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const GByteArray *array; + GBytes *ssid; const char *expected_id = "System blahblah (test-wifi-wpa-psk-hex)"; const char *expected_ssid = "blahblah"; const char *expected_key_mgmt = "wpa-psk"; @@ -5127,18 +5056,18 @@ test_read_wifi_wpa_psk_hex (void) NM_SETTING_WIRELESS_SETTING_NAME); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "wifi-wpa-psk-hex-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIFI_WPA_PSK_HEX, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (array->len == strlen (expected_ssid), + ASSERT (g_bytes_get_size (ssid) == strlen (expected_ssid), "wifi-wpa-psk-hex-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", TEST_IFCFG_WIFI_WPA_PSK_HEX, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - ASSERT (memcmp (array->data, expected_ssid, strlen (expected_ssid)) == 0, + ASSERT (memcmp (g_bytes_get_data (ssid, NULL), expected_ssid, strlen (expected_ssid)) == 0, "wifi-wpa-psk-hex-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK_HEX, NM_SETTING_WIRELESS_SETTING_NAME, @@ -5773,7 +5702,7 @@ test_write_wifi_hidden (void) gboolean success; GError *error = NULL; shvarFile *f; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x53, 0x49, 0x44 }; connection = nm_simple_connection_new (); @@ -5794,8 +5723,7 @@ test_write_wifi_hidden (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, @@ -5803,7 +5731,7 @@ test_write_wifi_hidden (void) NM_SETTING_WIRELESS_HIDDEN, TRUE, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); success = nm_connection_verify (connection, &error); g_assert_no_error (error); @@ -5866,11 +5794,11 @@ test_read_wired_qeth_static (void) GError *error = NULL; const char *tmp; const char *expected_id = "System test-wired-qeth-static"; - const GByteArray *array; + const char *mac; const char *expected_channel0 = "0.0.0600"; const char *expected_channel1 = "0.0.0601"; const char *expected_channel2 = "0.0.0602"; - const GPtrArray *subchannels; + const char * const *subchannels; connection = connection_from_file (TEST_IFCFG_WIRED_QETH_STATIC, NULL, @@ -5920,8 +5848,8 @@ test_read_wired_qeth_static (void) NM_SETTING_WIRED_SETTING_NAME); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - ASSERT (array == NULL, + mac = nm_setting_wired_get_mac_address (s_wired); + ASSERT (mac == NULL, "wired-qeth-static-verify-wired", "failed to verify %s: unexpected %s / %s key", TEST_IFCFG_WIRED_QETH_STATIC, NM_SETTING_WIRED_SETTING_NAME, @@ -5934,24 +5862,19 @@ test_read_wired_qeth_static (void) TEST_IFCFG_WIRED_QETH_STATIC, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_SUBCHANNELS); - ASSERT (subchannels->len == 3, + ASSERT (subchannels[0] && subchannels[1] && subchannels[2] && !subchannels[3], "wired-qeth-static-verify-wired", "failed to verify %s: invalid %s / %s key (not 3 elements)", TEST_IFCFG_WIRED_QETH_STATIC, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_SUBCHANNELS); - tmp = (const char *) g_ptr_array_index (subchannels, 0); - ASSERT (strcmp (tmp, expected_channel0) == 0, + ASSERT (strcmp (subchannels[0], expected_channel0) == 0, "wired-qeth-static-verify-wired", "failed to verify %s: unexpected subchannel #0", TEST_IFCFG_WIRED_QETH_STATIC); - - tmp = (const char *) g_ptr_array_index (subchannels, 1); - ASSERT (strcmp (tmp, expected_channel1) == 0, + ASSERT (strcmp (subchannels[1], expected_channel1) == 0, "wired-qeth-static-verify-wired", "failed to verify %s: unexpected subchannel #1", TEST_IFCFG_WIRED_QETH_STATIC); - - tmp = (const char *) g_ptr_array_index (subchannels, 2); - ASSERT (strcmp (tmp, expected_channel2) == 0, + ASSERT (strcmp (subchannels[2], expected_channel2) == 0, "wired-qeth-static-verify-wired", "failed to verify %s: unexpected subchannel #2", TEST_IFCFG_WIRED_QETH_STATIC); @@ -6042,7 +5965,7 @@ test_read_wired_ctc_static (void) const char *expected_id = "System test-wired-ctc-static"; const char *expected_channel0 = "0.0.1b00"; const char *expected_channel1 = "0.0.1b01"; - const GPtrArray *subchannels; + const char * const *subchannels; gboolean success; connection = connection_from_file (TEST_IFCFG_WIRED_CTC_STATIC, @@ -6076,10 +5999,10 @@ test_read_wired_ctc_static (void) /* Subchannels */ subchannels = nm_setting_wired_get_s390_subchannels (s_wired); g_assert (subchannels != NULL); - g_assert_cmpint (subchannels->len, ==, 2); + g_assert (subchannels[0] && subchannels[1] && !subchannels[2]); - g_assert_cmpstr (g_ptr_array_index (subchannels, 0), ==, expected_channel0); - g_assert_cmpstr (g_ptr_array_index (subchannels, 1), ==, expected_channel1); + g_assert_cmpstr (subchannels[0], ==, expected_channel0); + g_assert_cmpstr (subchannels[1], ==, expected_channel1); /* Nettype */ g_assert_cmpstr (nm_setting_wired_get_s390_nettype (s_wired), ==, "ctc"); @@ -6354,15 +6277,14 @@ test_write_wired_static (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4, *reread_s_ip4; NMSettingIP6Config *s_ip6, *reread_s_ip6; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; const guint32 ip1 = htonl (0x01010103); const guint32 ip2 = htonl (0x01010105); const guint32 gw = htonl (0x01010101); - const guint32 dns1 = htonl (0x04020201); - const guint32 dns2 = htonl (0x04020202); + const char *dns1 = "4.2.2.1"; + const char *dns2 = "4.2.2.2"; const guint32 prefix = 24; const char *dns_search1 = "foobar.com"; const char *dns_search2 = "lab.foobar.com"; @@ -6370,7 +6292,8 @@ test_write_wired_static (void) const char *dns_search4 = "lab6.foobar.com"; struct in6_addr ip6, ip6_1, ip6_2; struct in6_addr route1_dest, route2_dest, route1_nexthop, route2_nexthop; - struct in6_addr dns6_1, dns6_2; + const char *dns6_1 = "fade:0102:0103::face"; + const char *dns6_2 = "cafe:ffff:eeee:dddd:cccc:bbbb:aaaa:feed"; const guint32 route1_prefix = 64, route2_prefix = 128; const guint32 route1_metric = 99, route2_metric = 1; NMIP4Address *addr; @@ -6392,8 +6315,6 @@ test_write_wired_static (void) inet_pton (AF_INET6, "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", &route1_nexthop); inet_pton (AF_INET6, "::", &route2_dest); inet_pton (AF_INET6, "2222:aaaa::9999", &route2_nexthop); - inet_pton (AF_INET6, "fade:0102:0103::face", &dns6_1); - inet_pton (AF_INET6, "cafe:ffff:eeee:dddd:cccc:bbbb:aaaa:feed", &dns6_2); connection = nm_simple_connection_new (); @@ -6414,14 +6335,10 @@ test_write_wired_static (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, mtu, NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -6498,8 +6415,8 @@ test_write_wired_static (void) nm_ip6_route_unref (route6); /* DNS servers */ - nm_setting_ip6_config_add_dns (s_ip6, &dns6_1); - nm_setting_ip6_config_add_dns (s_ip6, &dns6_2); + nm_setting_ip6_config_add_dns (s_ip6, dns6_1); + nm_setting_ip6_config_add_dns (s_ip6, dns6_2); /* DNS domains */ nm_setting_ip6_config_add_dns_search (s_ip6, dns_search3); @@ -6797,11 +6714,10 @@ test_write_wired_static_ip6_only (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; char *uuid; struct in6_addr ip6; - struct in6_addr dns6; + const char *dns6 = "fade:0102:0103::face"; NMIP6Address *addr6; gboolean success; GError *error = NULL; @@ -6813,7 +6729,6 @@ test_write_wired_static_ip6_only (void) gboolean ignore_error = FALSE; inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - inet_pton (AF_INET6, "fade:0102:0103::face", &dns6); connection = nm_simple_connection_new (); @@ -6834,10 +6749,7 @@ test_write_wired_static_ip6_only (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -6863,7 +6775,7 @@ test_write_wired_static_ip6_only (void) nm_ip6_address_unref (addr6); /* DNS server */ - nm_setting_ip6_config_add_dns (s_ip6, &dns6); + nm_setting_ip6_config_add_dns (s_ip6, dns6); ASSERT (nm_connection_verify (connection, &error) == TRUE, "wired-static-ip6-only-write", "failed to verify connection: %s", @@ -6934,11 +6846,10 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; char *uuid; struct in6_addr ip6; - struct in6_addr dns6; + const char *dns6 = "fade:0102:0103::face"; NMIP6Address *addr6; gboolean success; GError *error = NULL; @@ -6958,7 +6869,6 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) } inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - inet_pton (AF_INET6, "fade:0102:0103::face", &dns6); if (gateway6) inet_ntop (AF_INET6, gateway6, s_gateway6, sizeof (s_gateway6)); @@ -6983,10 +6893,7 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -7014,7 +6921,7 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) nm_ip6_address_unref (addr6); /* DNS server */ - nm_setting_ip6_config_add_dns (s_ip6, &dns6); + nm_setting_ip6_config_add_dns (s_ip6, dns6); g_assert (nm_connection_verify (connection, &error)); @@ -7234,15 +7141,14 @@ test_write_wired_static_routes (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; const guint32 ip1 = htonl (0x01010103); const guint32 ip2 = htonl (0x01010105); const guint32 gw = htonl (0x01010101); - const guint32 dns1 = htonl (0x04020201); - const guint32 dns2 = htonl (0x04020202); + const char *dns1 = "4.2.2.1"; + const char *dns2 = "4.2.2.2"; const guint32 route_dst1 = htonl (0x01020300); const guint32 route_dst2= htonl (0x03020100); const guint32 route_gw1 = htonl (0xdeadbeef); @@ -7280,14 +7186,10 @@ test_write_wired_static_routes (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, mtu, NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -7774,7 +7676,7 @@ test_write_wired_aliases (void) char *uuid; int num_addresses = 4; guint32 ip[] = { 0x01010101, 0x01010102, 0x01010103, 0x01010104 }; - const char *label[] = { NULL, "alias0:2", NULL, "alias0:3" }; + const char *label[] = { "", "alias0:2", "", "alias0:3" }; const guint32 gw = htonl (0x01010101); const guint32 prefix = 24; NMIP4Address *addr; @@ -8126,13 +8028,11 @@ test_write_wifi_open (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x53, 0x49, 0x44 }; - GByteArray *bssid; - const unsigned char bssid_data[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; + const char *bssid = "11:22:33:44:55:66"; guint32 channel = 9, mtu = 1345; - GByteArray *mac; - const unsigned char mac_data[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; + const char *mac = "aa:bb:cc:dd:ee:ff"; shvarFile *ifcfg; char *tmp; @@ -8155,12 +8055,7 @@ test_write_wifi_open (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); - bssid = g_byte_array_sized_new (sizeof (bssid_data)); - g_byte_array_append (bssid, bssid_data, sizeof (bssid_data)); - mac = g_byte_array_sized_new (sizeof (mac_data)); - g_byte_array_append (mac, mac_data, sizeof (mac_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, @@ -8172,9 +8067,7 @@ test_write_wifi_open (void) NM_SETTING_WIRELESS_MTU, mtu, NULL); - g_byte_array_free (ssid, TRUE); - g_byte_array_free (bssid, TRUE); - g_byte_array_free (mac, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -8275,7 +8168,7 @@ test_write_wifi_open_hex_ssid (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd }; connection = nm_simple_connection_new (); @@ -8297,15 +8190,14 @@ test_write_wifi_open_hex_ssid (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -8390,8 +8282,8 @@ test_write_wifi_wep (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; struct stat statbuf; connection = nm_simple_connection_new (); @@ -8413,15 +8305,14 @@ test_write_wifi_wep (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -8532,13 +8423,13 @@ test_write_wifi_wep_adhoc (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; struct stat statbuf; NMIP4Address *addr; const guint32 ip1 = htonl (0x01010103); const guint32 gw = htonl (0x01010101); - const guint32 dns1 = htonl (0x04020201); + const char *dns1 = "4.2.2.1"; const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -8560,15 +8451,14 @@ test_write_wifi_wep_adhoc (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "adhoc", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -8682,8 +8572,8 @@ test_write_wifi_wep_passphrase (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; struct stat statbuf; connection = nm_simple_connection_new (); @@ -8705,15 +8595,14 @@ test_write_wifi_wep_passphrase (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -8822,8 +8711,8 @@ test_write_wifi_wep_40_ascii (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah40"; + GBytes *ssid; + const char *ssid_data = "blahblah40"; struct stat statbuf; connection = nm_simple_connection_new (); @@ -8845,15 +8734,14 @@ test_write_wifi_wep_40_ascii (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -8964,8 +8852,8 @@ test_write_wifi_wep_104_ascii (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah104"; + GBytes *ssid; + const char *ssid_data = "blahblah104"; struct stat statbuf; connection = nm_simple_connection_new (); @@ -8987,15 +8875,14 @@ test_write_wifi_wep_104_ascii (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9106,8 +8993,8 @@ test_write_wifi_leap (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; struct stat statbuf; connection = nm_simple_connection_new (); @@ -9129,15 +9016,14 @@ test_write_wifi_leap (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9245,8 +9131,8 @@ test_write_wifi_leap_secret_flags (NMSettingSecretFlags flags) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; connection = nm_simple_connection_new (); g_assert (connection); @@ -9269,13 +9155,12 @@ test_write_wifi_leap_secret_flags (NMSettingSecretFlags flags) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9385,8 +9270,8 @@ test_write_wifi_wpa_psk (const char *name, char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; g_return_if_fail (psk != NULL); @@ -9409,15 +9294,14 @@ test_write_wifi_wpa_psk (const char *name, s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9532,12 +9416,12 @@ test_write_wifi_wpa_psk_adhoc (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; NMIP4Address *addr; const guint32 ip1 = htonl (0x01010103); const guint32 gw = htonl (0x01010101); - const guint32 dns1 = htonl (0x04020201); + const char *dns1 = "4.2.2.1"; const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -9559,8 +9443,7 @@ test_write_wifi_wpa_psk_adhoc (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, @@ -9569,7 +9452,7 @@ test_write_wifi_wpa_psk_adhoc (void) NM_SETTING_WIRELESS_BAND, "bg", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9681,7 +9564,7 @@ test_write_wifi_wpa_eap_tls (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const char *ssid_data = "blahblah"; connection = nm_simple_connection_new (); @@ -9703,15 +9586,14 @@ test_write_wifi_wpa_eap_tls (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (strlen (ssid_data)); - g_byte_array_append (ssid, (const unsigned char *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -9846,7 +9728,7 @@ test_write_wifi_wpa_eap_ttls_tls (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const char *ssid_data = "blahblah"; connection = nm_simple_connection_new (); @@ -9868,15 +9750,14 @@ test_write_wifi_wpa_eap_ttls_tls (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (strlen (ssid_data)); - g_byte_array_append (ssid, (const unsigned char *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -10029,7 +9910,7 @@ test_write_wifi_wpa_eap_ttls_mschapv2 (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const char *ssid_data = "blahblah"; connection = nm_simple_connection_new (); @@ -10051,15 +9932,14 @@ test_write_wifi_wpa_eap_ttls_mschapv2 (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (strlen (ssid_data)); - g_byte_array_append (ssid, (const unsigned char *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -10183,8 +10063,8 @@ test_write_wifi_wpa_then_open (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - const unsigned char ssid_data[] = "blahblah"; + GBytes *ssid; + const char *ssid_data = "blahblah"; /* Test that writing out a WPA config then changing that to an open * config doesn't leave various WPA-related keys lying around in the ifcfg. @@ -10212,15 +10092,14 @@ test_write_wifi_wpa_then_open (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -10374,9 +10253,9 @@ test_write_wifi_wpa_then_wep_with_perms (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; - GSList *perm_list = NULL; - const unsigned char ssid_data[] = "SomeSSID"; + GBytes *ssid; + char **perms; + const char *ssid_data = "SomeSSID"; /* Test that writing out a WPA config then changing that to a WEP * config works and doesn't cause infinite loop or other issues. @@ -10391,16 +10270,16 @@ test_write_wifi_wpa_then_wep_with_perms (void) nm_connection_add_setting (connection, NM_SETTING (s_con)); uuid = nm_utils_uuid_generate (); - perm_list = g_slist_append (perm_list, "user:superman:"); + perms = g_strsplit ("user:superman:", ",", -1); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "random wifi connection 2", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_AUTOCONNECT, TRUE, - NM_SETTING_CONNECTION_PERMISSIONS, perm_list, + NM_SETTING_CONNECTION_PERMISSIONS, perms, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); g_free (uuid); - g_slist_free (perm_list); + g_strfreev (perms); ASSERT (nm_setting_connection_get_num_permissions (s_con) == 1, "test_write_wifi_wpa_then_wep_with_perms", "unexpected failure adding valid user permisson"); @@ -10409,15 +10288,14 @@ test_write_wifi_wpa_then_wep_with_perms (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -10579,7 +10457,7 @@ test_write_wifi_dynamic_wep_leap (void) char *routefile = NULL; char *route6file = NULL; gboolean ignore_error = FALSE; - GByteArray *ssid; + GBytes *ssid; const char *ssid_data = "blahblah"; shvarFile *ifcfg; char *tmp; @@ -10605,15 +10483,14 @@ test_write_wifi_dynamic_wep_leap (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (strlen (ssid_data)); - g_byte_array_append (ssid, (const unsigned char *) ssid_data, strlen (ssid_data)); + ssid = g_bytes_new (ssid_data, strlen (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wireless security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -10725,7 +10602,7 @@ test_write_wired_qeth_dhcp (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - GPtrArray *subchans; + char **subchans; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -10754,15 +10631,12 @@ test_write_wired_qeth_dhcp (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - subchans = g_ptr_array_sized_new (3); - g_ptr_array_add (subchans, "0.0.600"); - g_ptr_array_add (subchans, "0.0.601"); - g_ptr_array_add (subchans, "0.0.602"); + subchans = g_strsplit ("0.0.600,0.0.601,0.0.602", ",", -1); g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchans, NM_SETTING_WIRED_S390_NETTYPE, "qeth", NULL); - g_ptr_array_free (subchans, TRUE); + g_strfreev (subchans); nm_setting_wired_add_s390_option (s_wired, "portname", "FOOBAR"); nm_setting_wired_add_s390_option (s_wired, "portno", "1"); @@ -10849,7 +10723,7 @@ test_write_wired_ctc_dhcp (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - GPtrArray *subchans; + char **subchans; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -10882,14 +10756,12 @@ test_write_wired_ctc_dhcp (void) g_assert (s_wired); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - subchans = g_ptr_array_sized_new (2); - g_ptr_array_add (subchans, "0.0.600"); - g_ptr_array_add (subchans, "0.0.601"); + subchans = g_strsplit ("0.0.600,0.0.601", ",", -1); g_object_set (s_wired, NM_SETTING_WIRED_S390_SUBCHANNELS, subchans, NM_SETTING_WIRED_S390_NETTYPE, "ctc", NULL); - g_ptr_array_free (subchans, TRUE); + g_strfreev (subchans); nm_setting_wired_add_s390_option (s_wired, "ctcprot", "0"); /* IP4 setting */ @@ -11098,7 +10970,7 @@ test_write_wifi_wep_agent_keys (void) NMSettingIP6Config *s_ip6; char *uuid; const char *str_ssid = "foobarbaz"; - GByteArray *ssid; + GBytes *ssid; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -11145,13 +11017,12 @@ test_write_wifi_wep_agent_keys (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (strlen (str_ssid)); - g_byte_array_append (ssid, (guint8 *) str_ssid, strlen (str_ssid)); + ssid = g_bytes_new (str_ssid, strlen (str_ssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, "infrastructure", NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* Wifi security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -11448,7 +11319,7 @@ test_read_bridge_main (void) { NMConnection *connection; NMSettingBridge *s_bridge; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; char *unmanaged = NULL; char *keyfile = NULL; @@ -11482,11 +11353,11 @@ test_read_bridge_main (void) g_assert_cmpuint (nm_setting_bridge_get_hello_time (s_bridge), ==, 7); g_assert_cmpuint (nm_setting_bridge_get_max_age (s_bridge), ==, 39); g_assert_cmpuint (nm_setting_bridge_get_ageing_time (s_bridge), ==, 235352); + /* MAC address */ - array = nm_setting_bridge_get_mac_address (s_bridge); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, &expected_mac_address[0], ETH_ALEN) == 0); + mac = nm_setting_bridge_get_mac_address (s_bridge); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, ETH_ALEN)); g_free (unmanaged); g_free (keyfile); @@ -11509,8 +11380,7 @@ test_write_bridge_main (void) const guint32 gw = htonl (0x01010101); const guint32 prefix = 24; NMIP4Address *addr; - static unsigned char bridge_mac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac_array; + static const char *mac = "31:33:33:37:be:cd"; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -11543,12 +11413,9 @@ test_write_bridge_main (void) g_assert (s_bridge); nm_connection_add_setting (connection, NM_SETTING (s_bridge)); - mac_array = g_byte_array_sized_new (sizeof (bridge_mac)); - g_byte_array_append (mac_array, bridge_mac, sizeof (bridge_mac)); g_object_set (s_bridge, - NM_SETTING_BRIDGE_MAC_ADDRESS, mac_array, + NM_SETTING_BRIDGE_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac_array, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -11672,8 +11539,7 @@ test_write_bridge_component (void) NMSettingConnection *s_con; NMSettingWired *s_wired; NMSetting *s_port; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; gboolean success; @@ -11709,14 +11575,10 @@ test_write_bridge_component (void) g_assert (s_wired); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, mtu, NULL); - g_byte_array_free (mac, TRUE); /* Bridge port */ s_port = nm_setting_bridge_port_new (); @@ -12468,8 +12330,7 @@ test_write_bond_slave (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - static unsigned char tmpmac[] = { 0x31, 0x33, 0x33, 0x37, 0xbe, 0xcd }; - GByteArray *mac; + static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; gboolean success; @@ -12502,14 +12363,10 @@ test_write_bond_slave (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, mtu, NULL); - g_byte_array_free (mac, TRUE); ASSERT (nm_connection_verify (connection, &error) == TRUE, "bond-slave-write", "failed to verify connection: %s", @@ -12576,7 +12433,7 @@ test_read_infiniband (void) char *route6file = NULL; gboolean ignore_error = FALSE; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[INFINIBAND_ALEN] = { 0x80, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22 }; const char *transport_mode; @@ -12607,18 +12464,13 @@ test_read_infiniband (void) NM_SETTING_INFINIBAND_SETTING_NAME); /* MAC address */ - array = nm_setting_infiniband_get_mac_address (s_infiniband); - ASSERT (array != NULL, + mac = nm_setting_infiniband_get_mac_address (s_infiniband); + ASSERT (mac != NULL, "infiniband-verify-infiniband", "failed to verify %s: missing %s / %s key", TEST_IFCFG_INFINIBAND, NM_SETTING_INFINIBAND_SETTING_NAME, NM_SETTING_INFINIBAND_MAC_ADDRESS); - ASSERT (array->len == INFINIBAND_ALEN, - "infiniband-verify-infiniband", "failed to verify %s: unexpected %s / %s key value length", - TEST_IFCFG_INFINIBAND, - NM_SETTING_INFINIBAND_SETTING_NAME, - NM_SETTING_INFINIBAND_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "infiniband-verify-infiniband", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_INFINIBAND, NM_SETTING_INFINIBAND_SETTING_NAME, @@ -12653,8 +12505,7 @@ test_write_infiniband (void) NMSettingInfiniband *s_infiniband; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - unsigned char tmpmac[INFINIBAND_ALEN] = { 0x80, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22 }; - GByteArray *mac; + const char *mac = "80:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22"; guint32 mtu = 65520; char *uuid; const guint32 ip1 = htonl (0x01010103); @@ -12689,15 +12540,11 @@ test_write_infiniband (void) s_infiniband = (NMSettingInfiniband *) nm_setting_infiniband_new (); nm_connection_add_setting (connection, NM_SETTING (s_infiniband)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NM_SETTING_INFINIBAND_MTU, mtu, NM_SETTING_INFINIBAND_TRANSPORT_MODE, "connected", NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -12830,11 +12677,7 @@ test_write_bond_slave_ib (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingInfiniband *s_infiniband; - static unsigned char tmpmac[] = { - 0x80, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, - 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22 - }; - GByteArray *mac; + static const char *mac = "80:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22"; char *uuid; gboolean success; GError *error = NULL; @@ -12866,15 +12709,11 @@ test_write_bond_slave_ib (void) s_infiniband = (NMSettingInfiniband *) nm_setting_infiniband_new (); nm_connection_add_setting (connection, NM_SETTING (s_infiniband)); - mac = g_byte_array_sized_new (sizeof (tmpmac)); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); - g_object_set (s_infiniband, NM_SETTING_INFINIBAND_MAC_ADDRESS, mac, NM_SETTING_INFINIBAND_MTU, 2044, NM_SETTING_INFINIBAND_TRANSPORT_MODE, "datagram", NULL); - g_byte_array_free (mac, TRUE); ASSERT (nm_connection_verify (connection, &error) == TRUE, "bond-slave-write-ib", "failed to verify connection: %s", diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index c0a93909fc..106add4331 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -50,13 +50,6 @@ #include "crypto.h" -static void -svSetValue_free (shvarFile *s, const char *key, char *value, gboolean verbatim) -{ - svSetValue (s, key, value, verbatim); - g_free (value); -} - static void save_secret_flags (shvarFile *ifcfg, const char *key, @@ -204,7 +197,7 @@ typedef struct ObjectType { const char *setting_key; NMSetting8021xCKScheme (*scheme_func)(NMSetting8021x *setting); const char * (*path_func) (NMSetting8021x *setting); - const GByteArray * (*blob_func) (NMSetting8021x *setting); + GBytes * (*blob_func) (NMSetting8021x *setting); const char *ifcfg_key; const char *suffix; } ObjectType; @@ -289,7 +282,7 @@ write_object (NMSetting8021x *s_8021x, { NMSetting8021xCKScheme scheme; const char *path = NULL; - const GByteArray *blob = NULL; + GBytes *blob = NULL; g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (objtype != NULL, FALSE); @@ -354,7 +347,10 @@ write_object (NMSetting8021x *s_8021x, * can use paths from now on instead of pushing around the certificate * data itself. */ - success = write_secret_file (new_file, (const char *) blob->data, blob->len, &write_error); + success = write_secret_file (new_file, + (const char *) g_bytes_get_data (blob, NULL), + g_bytes_get_size (blob), + &write_error); if (success) { svSetValue (ifcfg, objtype->ifcfg_key, new_file, FALSE); g_free (new_file); @@ -807,8 +803,11 @@ write_wireless_setting (NMConnection *connection, { NMSettingWireless *s_wireless; char *tmp, *tmp2; - const GByteArray *ssid, *device_mac, *cloned_mac, *bssid; - const char *mode; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; + const char *mode, *bssid; + const char *device_mac, *cloned_mac; char buf[33]; guint32 mtu, chan, i; gboolean adhoc = FALSE, hex_ssid = FALSE; @@ -821,19 +820,11 @@ write_wireless_setting (NMConnection *connection, return FALSE; } - svSetValue (ifcfg, "HWADDR", NULL, FALSE); device_mac = nm_setting_wireless_get_mac_address (s_wireless); - if (device_mac) { - svSetValue_free (ifcfg, "HWADDR", - nm_utils_hwaddr_ntoa (device_mac->data, device_mac->len), FALSE); - } + svSetValue (ifcfg, "HWADDR", device_mac, FALSE); - svSetValue (ifcfg, "MACADDR", NULL, FALSE); cloned_mac = nm_setting_wireless_get_cloned_mac_address (s_wireless); - if (cloned_mac) { - svSetValue_free (ifcfg, "MACADDR", - nm_utils_hwaddr_ntoa (cloned_mac->data, cloned_mac->len), FALSE); - } + svSetValue (ifcfg, "MACADDR", cloned_mac, FALSE); svSetValue (ifcfg, "HWADDR_BLACKLIST", NULL, FALSE); macaddr_blacklist = nm_setting_wireless_get_mac_address_blacklist (s_wireless); @@ -866,7 +857,8 @@ write_wireless_setting (NMConnection *connection, "Missing SSID in '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME); return FALSE; } - if (!ssid->len || ssid->len > 32) { + ssid_data = g_bytes_get_data (ssid, &ssid_len); + if (!ssid_len || ssid_len > 32) { g_set_error (error, IFCFG_PLUGIN_ERROR, 0, "Invalid SSID in '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME); return FALSE; @@ -875,8 +867,8 @@ write_wireless_setting (NMConnection *connection, /* If the SSID contains any non-printable characters, we need to use the * hex notation of the SSID instead. */ - for (i = 0; i < ssid->len; i++) { - if (!g_ascii_isprint (ssid->data[i])) { + for (i = 0; i < ssid_len; i++) { + if (!g_ascii_isprint (ssid_data[i])) { hex_ssid = TRUE; break; } @@ -886,16 +878,16 @@ write_wireless_setting (NMConnection *connection, GString *str; /* Hex SSIDs don't get quoted */ - str = g_string_sized_new (ssid->len * 2 + 3); + str = g_string_sized_new (ssid_len * 2 + 3); g_string_append (str, "0x"); - for (i = 0; i < ssid->len; i++) - g_string_append_printf (str, "%02X", ssid->data[i]); + for (i = 0; i < ssid_len; i++) + g_string_append_printf (str, "%02X", ssid_data[i]); svSetValue (ifcfg, "ESSID", str->str, TRUE); g_string_free (str, TRUE); } else { /* Printable SSIDs always get quoted */ memset (buf, 0, sizeof (buf)); - memcpy (buf, ssid->data, ssid->len); + memcpy (buf, ssid_data, ssid_len); tmp = svEscape (buf); /* svEscape will usually quote the string, but just for consistency, @@ -931,12 +923,8 @@ write_wireless_setting (NMConnection *connection, g_free (tmp); } - svSetValue (ifcfg, "BSSID", NULL, FALSE); bssid = nm_setting_wireless_get_bssid (s_wireless); - if (bssid) { - svSetValue_free (ifcfg, "BSSID", - nm_utils_hwaddr_ntoa (bssid->data, bssid->len), FALSE); - } + svSetValue (ifcfg, "BSSID", bssid, FALSE); /* Ensure DEFAULTKEY and SECURITYMODE are cleared unless there's security; * otherwise there's no way to detect WEP vs. open when WEP keys aren't @@ -993,9 +981,8 @@ static gboolean write_infiniband_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) { NMSettingInfiniband *s_infiniband; - const GByteArray *mac; char *tmp; - const char *transport_mode, *parent; + const char *mac, *transport_mode, *parent; guint32 mtu; int p_key; @@ -1006,13 +993,8 @@ write_infiniband_setting (NMConnection *connection, shvarFile *ifcfg, GError **e return FALSE; } - svSetValue (ifcfg, "HWADDR", NULL, FALSE); mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (mac) { - tmp = nm_utils_hwaddr_ntoa (mac->data, mac->len); - svSetValue (ifcfg, "HWADDR", tmp, FALSE); - g_free (tmp); - } + svSetValue (ifcfg, "HWADDR", mac, FALSE); svSetValue (ifcfg, "MTU", NULL, FALSE); mtu = nm_setting_infiniband_get_mtu (s_infiniband); @@ -1048,11 +1030,11 @@ static gboolean write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) { NMSettingWired *s_wired; - const GByteArray *device_mac, *cloned_mac; + const char *device_mac, *cloned_mac; char *tmp; const char *nettype, *portname, *ctcprot, *s390_key, *s390_val; guint32 mtu, num_opts, i; - const GPtrArray *s390_subchannels; + const char *const *s390_subchannels; GString *str; const GSList *macaddr_blacklist; @@ -1063,19 +1045,11 @@ write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) return FALSE; } - svSetValue (ifcfg, "HWADDR", NULL, FALSE); device_mac = nm_setting_wired_get_mac_address (s_wired); - if (device_mac) { - svSetValue_free (ifcfg, "HWADDR", - nm_utils_hwaddr_ntoa (device_mac->data, device_mac->len), FALSE); - } + svSetValue (ifcfg, "HWADDR", device_mac, FALSE); - svSetValue (ifcfg, "MACADDR", NULL, FALSE); cloned_mac = nm_setting_wired_get_cloned_mac_address (s_wired); - if (cloned_mac) { - svSetValue_free (ifcfg, "MACADDR", - nm_utils_hwaddr_ntoa (cloned_mac->data, cloned_mac->len), FALSE); - } + svSetValue (ifcfg, "MACADDR", cloned_mac, FALSE); svSetValue (ifcfg, "HWADDR_BLACKLIST", NULL, FALSE); macaddr_blacklist = nm_setting_wired_get_mac_address_blacklist (s_wired); @@ -1105,16 +1079,14 @@ write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (ifcfg, "SUBCHANNELS", NULL, FALSE); s390_subchannels = nm_setting_wired_get_s390_subchannels (s_wired); if (s390_subchannels) { + int len = g_strv_length ((char **)s390_subchannels); + tmp = NULL; - if (s390_subchannels->len == 2) { - tmp = g_strdup_printf ("%s,%s", - (const char *) g_ptr_array_index (s390_subchannels, 0), - (const char *) g_ptr_array_index (s390_subchannels, 1)); - } else if (s390_subchannels->len == 3) { - tmp = g_strdup_printf ("%s,%s,%s", - (const char *) g_ptr_array_index (s390_subchannels, 0), - (const char *) g_ptr_array_index (s390_subchannels, 1), - (const char *) g_ptr_array_index (s390_subchannels, 2)); + if (len == 2) { + tmp = g_strdup_printf ("%s,%s", s390_subchannels[0], s390_subchannels[1]); + } else if (len == 3) { + tmp = g_strdup_printf ("%s,%s,%s", s390_subchannels[0], s390_subchannels[1], + s390_subchannels[2]); } svSetValue (ifcfg, "SUBCHANNELS", tmp, FALSE); g_free (tmp); @@ -1160,11 +1132,11 @@ write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) return TRUE; } -static GString * +static char * vlan_priority_maplist_to_stringlist (NMSettingVlan *s_vlan, NMVlanPriorityMap map) { - GSList *strlist = NULL, *iter; - GString *value = NULL; + char **strlist; + char *value; if (map == NM_VLAN_INGRESS_MAP) g_object_get (G_OBJECT (s_vlan), NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, &strlist, NULL); @@ -1173,11 +1145,11 @@ vlan_priority_maplist_to_stringlist (NMSettingVlan *s_vlan, NMVlanPriorityMap ma else return NULL; - value = g_string_new (""); - for (iter = strlist; iter; iter = g_slist_next (iter)) - g_string_append_printf (value, "%s%s", value->len ? "," : "", (const char *) iter->data); - - g_slist_free_full (strlist, g_free); + if (strlist[0]) + value = g_strjoinv (",", strlist); + else + value = NULL; + g_strfreev (strlist); return value; } @@ -1190,7 +1162,6 @@ write_vlan_setting (NMConnection *connection, shvarFile *ifcfg, gboolean *wired, NMSettingWired *s_wired; char *tmp; guint32 vlan_flags = 0; - GString *text = NULL; s_con = nm_connection_get_setting_connection (connection); if (!s_con) { @@ -1229,15 +1200,13 @@ write_vlan_setting (NMConnection *connection, shvarFile *ifcfg, gboolean *wired, } else if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING) svSetValue (ifcfg, "VLAN_FLAGS", "LOOSE_BINDING", FALSE); - text = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_INGRESS_MAP); - svSetValue (ifcfg, "VLAN_INGRESS_PRIORITY_MAP", text ? text->str : NULL, FALSE); - if (text) - g_string_free (text, TRUE); + tmp = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_INGRESS_MAP); + svSetValue (ifcfg, "VLAN_INGRESS_PRIORITY_MAP", tmp, FALSE); + g_free (tmp); - text = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_EGRESS_MAP); - svSetValue (ifcfg, "VLAN_EGRESS_PRIORITY_MAP", text ? text->str : NULL, FALSE); - if (text) - g_string_free (text, TRUE); + tmp = vlan_priority_maplist_to_stringlist (s_vlan, NM_VLAN_EGRESS_MAP); + svSetValue (ifcfg, "VLAN_EGRESS_PRIORITY_MAP", tmp, FALSE); + g_free (tmp); svSetValue (ifcfg, "HWADDR", NULL, FALSE); svSetValue (ifcfg, "MACADDR", NULL, FALSE); @@ -1245,24 +1214,18 @@ write_vlan_setting (NMConnection *connection, shvarFile *ifcfg, gboolean *wired, s_wired = nm_connection_get_setting_wired (connection); if (s_wired) { - const GByteArray *device_mac, *cloned_mac; + const char *device_mac, *cloned_mac; guint32 mtu; *wired = TRUE; device_mac = nm_setting_wired_get_mac_address (s_wired); - if (device_mac) { - tmp = nm_utils_hwaddr_ntoa (device_mac->data, device_mac->len); - svSetValue (ifcfg, "HWADDR", tmp, FALSE); - g_free (tmp); - } + if (device_mac) + svSetValue (ifcfg, "HWADDR", device_mac, FALSE); cloned_mac = nm_setting_wired_get_cloned_mac_address (s_wired); - if (cloned_mac) { - tmp = nm_utils_hwaddr_ntoa (cloned_mac->data, device_mac->len); - svSetValue (ifcfg, "MACADDR", tmp, FALSE); - g_free (tmp); - } + if (cloned_mac) + svSetValue (ifcfg, "MACADDR", cloned_mac, FALSE); mtu = nm_setting_wired_get_mtu (s_wired); if (mtu) { @@ -1378,7 +1341,7 @@ write_bridge_setting (NMConnection *connection, shvarFile *ifcfg, GError **error const char *iface; guint32 i; GString *opts; - const GByteArray *mac; + const char *mac; char *s; s_bridge = nm_connection_get_setting_bridge (connection); @@ -1398,11 +1361,9 @@ write_bridge_setting (NMConnection *connection, shvarFile *ifcfg, GError **error svSetValue (ifcfg, "BRIDGING_OPTS", NULL, FALSE); svSetValue (ifcfg, "STP", "no", FALSE); svSetValue (ifcfg, "DELAY", NULL, FALSE); - svSetValue (ifcfg, "MACADDR", NULL, FALSE); mac = nm_setting_bridge_get_mac_address (s_bridge); - if (mac) - svSetValue_free (ifcfg, "MACADDR", nm_utils_hwaddr_ntoa (mac->data, mac->len), FALSE); + svSetValue (ifcfg, "MACADDR", mac, FALSE); /* Bridge options */ opts = g_string_sized_new (32); @@ -1927,8 +1888,13 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) NMIP4Address *addr; guint32 ip; - if (i > 0 && _nm_setting_ip4_config_get_address_label (s_ip4, i)) - continue; + if (i > 0) { + const char *label; + + label = _nm_setting_ip4_config_get_address_label (s_ip4, i); + if (*label) + continue; + } if (n == 0) { /* Instead of index 0 use un-numbered variables. @@ -1995,19 +1961,15 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip4_config_get_num_dns (s_ip4); for (i = 0; i < 254; i++) { - char buf[INET_ADDRSTRLEN + 1]; - guint32 ip; + const char *dns; addr_key = g_strdup_printf ("DNS%d", i + 1); if (i >= num) svSetValue (ifcfg, addr_key, NULL, FALSE); else { - ip = nm_setting_ip4_config_get_dns (s_ip4, i); - - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, addr_key, &buf[0], FALSE); + dns = nm_setting_ip4_config_get_dns (s_ip4, i); + svSetValue (ifcfg, addr_key, dns, FALSE); } g_free (addr_key); } @@ -2203,8 +2165,6 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) shvarFile *ifcfg; label = _nm_setting_ip4_config_get_address_label (s_ip4, i); - if (!label) - continue; if ( strncmp (label, base_name, base_name_len) != 0 || label[base_name_len] != ':') continue; @@ -2320,6 +2280,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) char ipv6_defaultgw[INET6_ADDRSTRLEN]; NMIP6Address *addr; const struct in6_addr *ip; + const char *dns; GString *ip_str1, *ip_str2, *ip_ptr; char *route6_path; @@ -2414,11 +2375,8 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) if (i >= num) svSetValue (ifcfg, addr_key, NULL, FALSE); else { - ip = nm_setting_ip6_config_get_dns (s_ip6, i); - - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, sizeof (buf)); - svSetValue (ifcfg, addr_key, buf, FALSE); + dns = nm_setting_ip6_config_get_dns (s_ip6, i); + svSetValue (ifcfg, addr_key, dns, FALSE); } g_free (addr_key); } diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index a9383a65fb..15dd3874a5 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -498,20 +498,20 @@ guess_connection_type (const char *conn_name) /* Reading mac address for setting connection option. * Unmanaged device mac address is required by NetworkManager*/ static gboolean -read_mac_address (const char *conn_name, GByteArray **array, GError **error) +read_mac_address (const char *conn_name, const char **mac, GError **error) { const char *value = ifnet_get_data (conn_name, "mac"); if (!value || !strlen (value)) return TRUE; - *array = nm_utils_hwaddr_atoba (value, ETH_ALEN); - if (!*array) { + if (!nm_utils_hwaddr_valid (value, ETH_ALEN)) { g_set_error (error, ifnet_plugin_error_quark (), 0, - "The MAC address '%s' was invalid.", value); + "The MAC address '%s' was invalid.", value); return FALSE; } + *mac = value; return TRUE; } @@ -520,7 +520,7 @@ make_wired_connection_setting (NMConnection *connection, const char *conn_name, GError **error) { - GByteArray *mac = NULL; + const char *mac = NULL; NMSettingWired *s_wired = NULL; const char *value = NULL; @@ -544,7 +544,6 @@ make_wired_connection_setting (NMConnection *connection, if (mac) { g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); } } else { g_object_unref (s_wired); @@ -881,7 +880,8 @@ make_wireless_connection_setting (const char *conn_name, NMSetting8021x **s_8021x, GError **error) { - GByteArray *array, *mac = NULL; + GBytes *bytes; + const char *mac = NULL; NMSettingWireless *wireless_setting = NULL; gboolean adhoc = FALSE; const char *value; @@ -903,8 +903,6 @@ make_wireless_connection_setting (const char *conn_name, g_object_set (wireless_setting, NM_SETTING_WIRELESS_MAC_ADDRESS, mac, NULL); - g_byte_array_free (mac, TRUE); - } } else { g_object_unref (wireless_setting); @@ -951,10 +949,9 @@ make_wireless_connection_setting (const char *conn_name, conn_name, ssid_len); goto error; } - array = g_byte_array_sized_new (ssid_len); - g_byte_array_append (array, (const guint8 *) (converted ? converted : conn_name), ssid_len); - g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, array, NULL); - g_byte_array_free (array, TRUE); + bytes = g_bytes_new (converted ? converted : conn_name, ssid_len); + g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, bytes, NULL); + g_bytes_unref (bytes); g_free (converted); } else { g_set_error (error, ifnet_plugin_error_quark (), 0, @@ -979,18 +976,14 @@ make_wireless_connection_setting (const char *conn_name, /* BSSID setting */ value = wpa_get_value (conn_name, "bssid"); if (value) { - GByteArray *bssid; - - bssid = nm_utils_hwaddr_atoba (value, ETH_ALEN); - if (!bssid) { + if (!nm_utils_hwaddr_valid (value, ETH_ALEN)) { g_set_error (error, ifnet_plugin_error_quark (), 0, "Invalid BSSID '%s'", value); goto error; } g_object_set (wireless_setting, NM_SETTING_WIRELESS_BSSID, - bssid, NULL); - g_byte_array_free (bssid, TRUE); + value, NULL); } @@ -1727,7 +1720,7 @@ error: typedef NMSetting8021xCKScheme (*SchemeFunc) (NMSetting8021x * setting); typedef const char *(*PathFunc) (NMSetting8021x * setting); -typedef const GByteArray *(*BlobFunc) (NMSetting8021x * setting); +typedef GBytes *(*BlobFunc) (NMSetting8021x * setting); typedef struct ObjectType { const char *setting_key; @@ -1813,13 +1806,13 @@ static const ObjectType phase2_p12_type = { static gboolean write_object (NMSetting8021x *s_8021x, const char *conn_name, - const GByteArray *override_data, + GBytes *override_data, const ObjectType *objtype, GError **error) { NMSetting8021xCKScheme scheme; const char *path = NULL; - const GByteArray *blob = NULL; + GBytes *blob = NULL; g_return_val_if_fail (conn_name != NULL, FALSE); g_return_val_if_fail (objtype != NULL, FALSE); @@ -1867,8 +1860,8 @@ write_8021x_certs (NMSetting8021x *s_8021x, char *password = NULL; const ObjectType *otype = NULL; gboolean is_pkcs12 = FALSE, success = FALSE; - const GByteArray *blob = NULL; - GByteArray *enc_key = NULL; + GBytes *blob = NULL; + GBytes *enc_key = NULL; gchar *generated_pw = NULL; /* CA certificate */ @@ -1915,13 +1908,17 @@ write_8021x_certs (NMSetting8021x *s_8021x, * private key file, it'll be encrypted, so we don't need to re-encrypt. */ if (blob && !is_pkcs12) { + GByteArray *tmp_enc_key; + /* Encrypt the unencrypted private key with the fake password */ - enc_key = - nm_utils_rsa_key_encrypt (blob, password, &generated_pw, - error); - if (!enc_key) + tmp_enc_key = + nm_utils_rsa_key_encrypt (g_bytes_get_data (blob, NULL), g_bytes_get_size (blob), + password, &generated_pw, error); + if (!tmp_enc_key) goto out; + enc_key = g_byte_array_free_to_bytes (tmp_enc_key); + if (generated_pw) password = generated_pw; } @@ -1958,8 +1955,8 @@ out: g_free (generated_pw); } if (enc_key) { - memset (enc_key->data, 0, enc_key->len); - g_byte_array_free (enc_key, TRUE); + memset ((gpointer) g_bytes_get_data (enc_key, NULL), 0, g_bytes_get_size (enc_key)); + g_bytes_unref (enc_key); } return success; } @@ -2230,8 +2227,10 @@ write_wireless_setting (NMConnection *connection, GError **error) { NMSettingWireless *s_wireless; - const GByteArray *ssid, *mac, *bssid; - const char *mode; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; + const char *mac, *bssid, *mode; char buf[33]; guint32 mtu, i; gboolean adhoc = FALSE, hex_ssid = FALSE; @@ -2252,7 +2251,8 @@ write_wireless_setting (NMConnection *connection, NM_SETTING_WIRELESS_SETTING_NAME); return FALSE; } - if (!ssid->len || ssid->len > 32) { + ssid_data = g_bytes_get_data (ssid, &ssid_len); + if (!ssid_len || ssid_len > 32) { g_set_error (error, ifnet_plugin_error_quark (), 0, "Invalid SSID in '%s' setting", NM_SETTING_WIRELESS_SETTING_NAME); @@ -2263,8 +2263,8 @@ write_wireless_setting (NMConnection *connection, * the hex notation of the SSID instead. (Because openrc doesn't * support these characters, see bug #356337) */ - for (i = 0; i < ssid->len; i++) { - if (!g_ascii_isalnum (ssid->data[i])) { + for (i = 0; i < ssid_len; i++) { + if (!g_ascii_isalnum (ssid_data[i])) { hex_ssid = TRUE; break; } @@ -2274,16 +2274,16 @@ write_wireless_setting (NMConnection *connection, GString *str; /* Hex SSIDs don't get quoted */ - str = g_string_sized_new (ssid->len * 2 + 3); + str = g_string_sized_new (ssid_len * 2 + 3); g_string_append (str, "0x"); - for (i = 0; i < ssid->len; i++) - g_string_append_printf (str, "%02X", ssid->data[i]); + for (i = 0; i < ssid_len; i++) + g_string_append_printf (str, "%02X", ssid_data[i]); update_wireless_ssid (connection, conn_name, str->str, hex_ssid); ssid_str = g_string_free (str, FALSE); } else { /* Printable SSIDs get quoted */ memset (buf, 0, sizeof (buf)); - memcpy (buf, ssid->data, ssid->len); + memcpy (buf, ssid_data, ssid_len); g_strstrip (buf); update_wireless_ssid (connection, conn_name, buf, hex_ssid); ssid_str = g_strdup (buf); @@ -2291,11 +2291,8 @@ write_wireless_setting (NMConnection *connection, ifnet_set_data (ssid_str, "mac", NULL); mac = nm_setting_wireless_get_mac_address (s_wireless); - if (mac) { - tmp = nm_utils_hwaddr_ntoa (mac->data, mac->len); - ifnet_set_data (ssid_str, "mac", tmp); - g_free (tmp); - } + if (mac) + ifnet_set_data (ssid_str, "mac", mac); ifnet_set_data (ssid_str, "mtu", NULL); mtu = nm_setting_wireless_get_mtu (s_wireless); @@ -2320,11 +2317,8 @@ write_wireless_setting (NMConnection *connection, wpa_set_data (ssid_str, "bssid", NULL); bssid = nm_setting_wireless_get_bssid (s_wireless); - if (bssid) { - tmp = nm_utils_hwaddr_ntoa (bssid->data, bssid->len); - wpa_set_data (ssid_str, "bssid", tmp); - g_free (tmp); - } + if (bssid) + wpa_set_data (ssid_str, "bssid", bssid); if (nm_connection_get_setting_wireless_security (connection)) { if (!write_wireless_security_setting @@ -2345,7 +2339,7 @@ write_wired_setting (NMConnection *connection, GError **error) { NMSettingWired *s_wired; - const GByteArray *mac; + const char *mac; char *tmp; guint32 mtu; @@ -2359,11 +2353,8 @@ write_wired_setting (NMConnection *connection, ifnet_set_data (conn_name, "mac", NULL); mac = nm_setting_wired_get_mac_address (s_wired); - if (mac) { - tmp = nm_utils_hwaddr_ntoa (mac->data, mac->len); - ifnet_set_data (conn_name, "mac", tmp); - g_free (tmp); - } + if (mac) + ifnet_set_data (conn_name, "mac", mac); ifnet_set_data (conn_name, "mtu", NULL); mtu = nm_setting_wired_get_mtu (s_wired); @@ -2453,15 +2444,10 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err if (num > 0) { dns = g_string_new (NULL); for (i = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN + 1]; - guint32 ip; + const char *ip; ip = nm_setting_ip4_config_get_dns (s_ip4, i); - - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (dns, " %s", buf); + g_string_append_printf (dns, " %s", ip); } ifnet_set_data (conn_name, "dns_servers", dns->str); g_string_free (dns, TRUE); @@ -2667,17 +2653,15 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err const char *dns_servers = ifnet_get_data (conn_name, "dns_servers"); gchar *tmp; GString *dns_string = g_string_new (NULL); + const char *dns; if (!dns_servers) dns_servers = ""; for (i = 0; i < num; i++) { - ip = nm_setting_ip6_config_get_dns (s_ip6, i); + dns = nm_setting_ip6_config_get_dns (s_ip6, i); - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, - sizeof (buf)); - if (!strstr (dns_servers, buf)) - g_string_append_printf (dns_string, "%s ", buf); + if (!strstr (dns_servers, dns)) + g_string_append_printf (dns_string, "%s ", dns); } tmp = g_strdup_printf ("%s %s", dns_servers, dns_string->str); ifnet_set_data (conn_name, "dns_servers", tmp); @@ -2949,7 +2933,9 @@ static gchar * get_wireless_name (NMConnection * connection) { NMSettingWireless *s_wireless; - const GByteArray *ssid; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; gboolean hex_ssid = FALSE; gchar *result = NULL; char buf[33]; @@ -2960,12 +2946,13 @@ get_wireless_name (NMConnection * connection) return NULL; ssid = nm_setting_wireless_get_ssid (s_wireless); - if (!ssid->len || ssid->len > 32) { + ssid_data = g_bytes_get_data (ssid, &ssid_len); + if (!ssid_len || ssid_len > 32) { return NULL; } - for (i = 0; i < ssid->len; i++) { - if (!g_ascii_isprint (ssid->data[i])) { + for (i = 0; i < ssid_len; i++) { + if (!g_ascii_isprint (ssid_data[i])) { hex_ssid = TRUE; break; } @@ -2974,15 +2961,15 @@ get_wireless_name (NMConnection * connection) if (hex_ssid) { GString *str; - str = g_string_sized_new (ssid->len * 2 + 3); + str = g_string_sized_new (ssid_len * 2 + 3); g_string_append (str, "0x"); - for (i = 0; i < ssid->len; i++) - g_string_append_printf (str, "%02X", ssid->data[i]); + for (i = 0; i < ssid_len; i++) + g_string_append_printf (str, "%02X", ssid_data[i]); result = g_strdup (str->str); g_string_free (str, TRUE); } else { memset (buf, 0, sizeof (buf)); - memcpy (buf, ssid->data, ssid->len); + memcpy (buf, ssid_data, ssid_len); result = g_strdup_printf ("%s", buf); g_strstrip (result); } diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index 86ab8537da..1f7ca56f17 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -682,7 +682,6 @@ set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) gchar **server_list, *stripped; guint length, i; guint32 tmp_ip4_addr; - guint32 new_dns; dns_servers = ifnet_get_data (conn_name, "dns_servers"); if (!dns_servers) @@ -705,8 +704,7 @@ set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) nm_log_warn (LOGD_SETTINGS, "ignored dns: %s\n", server_list[i]); continue; } - new_dns = tmp_ip4_addr; - if (new_dns && !nm_setting_ip4_config_add_dns (s_ip4, new_dns)) + if (!nm_setting_ip4_config_add_dns (s_ip4, server_list[i])) nm_log_warn (LOGD_SETTINGS, "warning: duplicate DNS server %s", server_list[i]); } g_strfreev (server_list); @@ -742,8 +740,7 @@ set_ip6_dns_servers (NMSettingIP6Config *s_ip6, const char *conn_name) nm_log_warn (LOGD_SETTINGS, "ignored dns: %s\n", server_list[i]); continue; } - if (!IN6_IS_ADDR_UNSPECIFIED (&tmp_ip6_addr) - && !nm_setting_ip6_config_add_dns (s_ip6, &tmp_ip6_addr)) + if (!nm_setting_ip6_config_add_dns (s_ip6, server_list[i])) nm_log_warn (LOGD_SETTINGS, "warning: duplicate DNS server %s", server_list[i]); } g_strfreev (server_list); diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index b3fe546cb9..097fe7b64e 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -447,7 +447,7 @@ ifupdown_ip4_add_dns (NMSettingIP4Config *s_ip4, const char *dns) continue; } - if (!nm_setting_ip4_config_add_dns (s_ip4, addr)) + if (!nm_setting_ip4_config_add_dns (s_ip4, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); @@ -582,7 +582,7 @@ ifupdown_ip6_add_dns (NMSettingIP6Config *s_ip6, const char *dns) continue; } - if (!nm_setting_ip6_config_add_dns (s_ip6, &addr)) + if (!nm_setting_ip6_config_add_dns (s_ip6, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); diff --git a/src/settings/plugins/ifupdown/plugin.c b/src/settings/plugins/ifupdown/plugin.c index ba15c3e3fa..20928c6826 100644 --- a/src/settings/plugins/ifupdown/plugin.c +++ b/src/settings/plugins/ifupdown/plugin.c @@ -185,7 +185,6 @@ bind_device_to_connection (SCPluginIfupdown *self, GUdevDevice *device, NMIfupdownConnection *exported) { - GByteArray *mac_address; NMSettingWired *s_wired; NMSettingWireless *s_wifi; const char *iface, *address; @@ -202,8 +201,7 @@ bind_device_to_connection (SCPluginIfupdown *self, return; } - mac_address = nm_utils_hwaddr_atoba (address, ETH_ALEN); - if (!mac_address) { + if (!nm_utils_hwaddr_valid (address, ETH_ALEN)) { nm_log_warn (LOGD_SETTINGS, "failed to parse MAC address '%s' for %s", address, iface); return; @@ -213,12 +211,11 @@ bind_device_to_connection (SCPluginIfupdown *self, s_wifi = nm_connection_get_setting_wireless (NM_CONNECTION (exported)); if (s_wired) { nm_log_info (LOGD_SETTINGS, "locking wired connection setting"); - g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac_address, NULL); + g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, address, NULL); } else if (s_wifi) { nm_log_info (LOGD_SETTINGS, "locking wireless connection setting"); - g_object_set (s_wifi, NM_SETTING_WIRELESS_MAC_ADDRESS, mac_address, NULL); + g_object_set (s_wifi, NM_SETTING_WIRELESS_MAC_ADDRESS, address, NULL); } - g_byte_array_free (mac_address, TRUE); nm_settings_connection_commit_changes (NM_SETTINGS_CONNECTION (exported), NULL, NULL); } diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 07102c803a..10c07f1ca9 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -467,8 +467,6 @@ test17_read_static_ipv4 (const char *path) const char* tmp; const char *expected_address = "10.0.0.3"; const char *expected_id = "Ifupdown (eth0)"; - const char *expected_dns1 = "10.0.0.1"; - const char *expected_dns2 = "10.0.0.2"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; guint32 expected_prefix = 8; @@ -569,25 +567,13 @@ test17_read_static_ipv4 (const char *path) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns1, &addr) > 0, - TEST17_NAME, "failed to verify %s: couldn't convert DNS IP address #1", - file, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 0) == addr, + ASSERT (!strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "10.0.0.1"), TEST17_NAME, "failed to verify %s: unexpected %s / %s key value #1", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns2, &addr) > 0, - TEST17_NAME, "failed to verify %s: couldn't convert DNS IP address #2", - file, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 1) == addr, + ASSERT (!strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "10.0.0.2"), TEST17_NAME, "failed to verify %s: unexpected %s / %s key value #2", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -646,8 +632,6 @@ test18_read_static_ipv6 (const char *path) const char* tmp; const char *expected_address = "fc00::1"; const char *expected_id = "Ifupdown (myip6tunnel)"; - const char *expected_dns1 = "fc00::2"; - const char *expected_dns2 = "fc00::3"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; guint32 expected_prefix = 64; @@ -768,26 +752,14 @@ test18_read_static_ipv6 (const char *path) NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected_dns1, &addr) > 0, - TEST18_NAME, - "failed to verify %s: couldn't convert DNS IP address #1", - file); - - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), - &addr), + ASSERT (!strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "fc00::2"), TEST18_NAME, "failed to verify %s: unexpected %s / %s #1", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected_dns2, &addr) > 0, - TEST18_NAME, - "failed to verify %s: couldn't convert DNS IP address #2", - file); - - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 1), - &addr), + ASSERT (!strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "fc00::3"), TEST18_NAME, "failed to verify %s: unexpected %s / %s #2", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 098d7706d3..15410bbf6d 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -118,7 +118,7 @@ get_one_int (const char *str, guint32 max_val, const char *key_name, guint32 *ou static gpointer build_ip4_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) { - GArray *result; + gpointer result; guint32 addr; guint32 address = 0; guint32 gateway = 0; @@ -153,12 +153,18 @@ build_ip4_address_or_route (const char *key_name, const char *address_str, guint return NULL; } - result = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3 + !!route); - g_array_append_val (result, address); - g_array_append_val (result, plen); - g_array_append_val (result, gateway); - if (route) - g_array_append_val (result, metric); + if (route) { + result = nm_ip4_route_new (); + nm_ip4_route_set_dest (result, address); + nm_ip4_route_set_prefix (result, plen); + nm_ip4_route_set_next_hop (result, gateway); + nm_ip4_route_set_metric (result, metric); + } else { + result = nm_ip4_address_new (); + nm_ip4_address_set_address (result, address); + nm_ip4_address_set_prefix (result, plen); + nm_ip4_address_set_gateway (result, gateway); + } return result; } @@ -166,36 +172,31 @@ build_ip4_address_or_route (const char *key_name, const char *address_str, guint static gpointer build_ip6_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) { - GValueArray *result; + gpointer result; struct in6_addr addr; - GByteArray *address; - GByteArray *gateway; guint32 metric = 0; - GValue value = G_VALUE_INIT; int err; g_return_val_if_fail (address_str, NULL); - result = g_value_array_new (3); + if (route) + result = nm_ip6_route_new (); + else + result = nm_ip6_address_new (); - /* add address */ + /* add address and prefix length */ err = inet_pton (AF_INET6, address_str, &addr); if (err <= 0) { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 address '%s'", __func__, address_str); goto error_out; } - address = g_byte_array_new (); - g_byte_array_append (address, (guint8 *) addr.s6_addr, 16); - g_value_init (&value, DBUS_TYPE_G_UCHAR_ARRAY); - g_value_take_boxed (&value, address); - g_value_array_append (result, &value); - g_value_unset (&value); - - /* add prefix length */ - g_value_init (&value, G_TYPE_UINT); - g_value_set_uint (&value, plen); - g_value_array_append (result, &value); - g_value_unset (&value); + if (route) { + nm_ip6_route_set_dest (result, &addr); + nm_ip6_route_set_prefix (result, plen); + } else { + nm_ip6_address_set_address (result, &addr); + nm_ip6_address_set_prefix (result, plen); + } /* add gateway */ if (gateway_str && gateway_str[0]) { @@ -219,31 +220,25 @@ build_ip6_address_or_route (const char *key_name, const char *address_str, guint } else addr = in6addr_any; - /* parse metric, default to 0 */ - if (metric_str) { - if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) - goto error_out; - } - - gateway = g_byte_array_new (); - g_byte_array_append (gateway, (guint8 *) addr.s6_addr, 16); - g_value_init (&value, DBUS_TYPE_G_UCHAR_ARRAY); - g_value_take_boxed (&value, gateway); - g_value_array_append (result, &value); - g_value_unset (&value); - - /* add metric (for routing) */ if (route) { - g_value_init (&value, G_TYPE_UINT); - g_value_set_uint (&value, metric); - g_value_array_append (result, &value); - g_value_unset (&value); - } + nm_ip6_route_set_next_hop (result, &addr); + + /* parse metric, default to 0 */ + if (metric_str) { + if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) + goto error_out; + } + nm_ip6_route_set_metric (result, metric); + } else + nm_ip6_address_set_gateway (result, &addr); return result; error_out: - g_value_array_free (result); + if (route) + nm_ip6_route_unref (result); + else + nm_ip4_route_unref (result); return NULL; } @@ -331,15 +326,13 @@ read_field (char **current, char **error, const char *characters, const char *de * changed. The default for IPv4 is now 24, which is the closest * IPv4 equivalent. These defaults may just as well be changed to * match the iproute2 defaults (32 for IPv4 and 128 for IPv6). - * - * The returned result is GArray for IPv4 and GValueArray for IPv6. */ static gpointer read_one_ip_address_or_route (GKeyFile *file, - const char *setting_name, - const char *key_name, - gboolean ipv6, - gboolean route) + const char *setting_name, + const char *key_name, + gboolean ipv6, + gboolean route) { guint32 plen; gpointer result; @@ -425,12 +418,21 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi static const char *key_names_addresses[] = { "address", "addresses", NULL }; const char **key_names = routes ? key_names_routes : key_names_addresses; GPtrArray *list; + GDestroyNotify free_func; int i; - G_GNUC_BEGIN_IGNORE_DEPRECATIONS; - list = g_ptr_array_new_with_free_func ( - ipv6 ? (GDestroyNotify) g_value_array_free : (GDestroyNotify) g_array_unref); - G_GNUC_END_IGNORE_DEPRECATIONS; + if (ipv6) { + if (routes) + free_func = (GDestroyNotify) nm_ip6_route_unref; + else + free_func = (GDestroyNotify) nm_ip6_address_unref; + } else { + if (routes) + free_func = (GDestroyNotify) nm_ip4_route_unref; + else + free_func = (GDestroyNotify) nm_ip4_address_unref; + } + list = g_ptr_array_new_with_free_func (free_func); for (i = -1; i < 1000; i++) { const char **key_basename; @@ -464,7 +466,7 @@ static void ip4_dns_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) { const char *setting_name = nm_setting_get_name (setting); - GArray *array = NULL; + GPtrArray *array; gsize length; char **list, **iter; int ret; @@ -473,7 +475,7 @@ ip4_dns_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const ch if (!list || !g_strv_length (list)) return; - array = g_array_sized_new (FALSE, FALSE, sizeof (guint32), length); + array = g_ptr_array_sized_new (length + 1); for (iter = list; *iter; iter++) { guint32 addr; @@ -483,14 +485,13 @@ ip4_dns_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const ch continue; } - g_array_append_val (array, addr); + g_ptr_array_add (array, *iter); } - g_strfreev (list); + g_ptr_array_add (array, NULL); - if (array) { - g_object_set (setting, key, array, NULL); - g_array_unref (array); - } + g_object_set (setting, key, array->pdata, NULL); + g_ptr_array_unref (array); + g_strfreev (list); } static void @@ -506,10 +507,9 @@ ip6_dns_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const ch if (!list || !g_strv_length (list)) return; - array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_byte_array_unref); + array = g_ptr_array_sized_new (length + 1); for (iter = list; *iter; iter++) { - GByteArray *byte_array; struct in6_addr addr; ret = inet_pton (AF_INET6, *iter, &addr); @@ -517,24 +517,21 @@ ip6_dns_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const ch nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid DNS server IPv6 address '%s'", __func__, *iter); continue; } - byte_array = g_byte_array_new (); - g_byte_array_append (byte_array, (guint8 *) addr.s6_addr, 16); - g_ptr_array_add (array, byte_array); + g_ptr_array_add (array, *iter); } + g_ptr_array_add (array, NULL); + + g_object_set (setting, key, array->pdata, NULL); + g_ptr_array_unref (array); g_strfreev (list); - - if (array) { - g_object_set (setting, key, array, NULL); - g_ptr_array_unref (array); - } } static void mac_address_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path, gsize enforce_length) { const char *setting_name = nm_setting_get_name (setting); - char *tmp_string = NULL, *p; + char *tmp_string = NULL, *p, *mac_str; gint *tmp_list; GByteArray *array = NULL; gsize length; @@ -587,13 +584,16 @@ mac_address_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, cons g_free (tmp_list); } - if (array) { - g_object_set (setting, key, array, NULL); - g_byte_array_free (array, TRUE); - } else { + if (!array) { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid MAC address for %s / %s", __func__, setting_name, key); + return; } + + mac_str = nm_utils_hwaddr_ntoa (array->data, array->len); + g_object_set (setting, key, mac_str, NULL); + g_free (mac_str); + g_byte_array_free (array, TRUE); } static void @@ -652,12 +652,12 @@ unescape_semicolons (char *str) } } -static GByteArray * -get_uchar_array (GKeyFile *keyfile, - const char *setting_name, - const char *key, - gboolean zero_terminate, - gboolean unescape_semicolon) +static GBytes * +get_bytes (GKeyFile *keyfile, + const char *setting_name, + const char *key, + gboolean zero_terminate, + gboolean unescape_semicolon) { GByteArray *array = NULL; char *tmp_string; @@ -711,21 +711,21 @@ get_uchar_array (GKeyFile *keyfile, if (array->len == 0) { g_byte_array_free (array, TRUE); - array = NULL; - } - return array; + return NULL; + } else + return g_byte_array_free_to_bytes (array); } static void ssid_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) { const char *setting_name = nm_setting_get_name (setting); - GByteArray *array; + GBytes *bytes; - array = get_uchar_array (keyfile, setting_name, key, FALSE, TRUE); - if (array) { - g_object_set (setting, key, array, NULL); - g_byte_array_free (array, TRUE); + bytes = get_bytes (keyfile, setting_name, key, FALSE, TRUE); + if (bytes) { + g_object_set (setting, key, bytes, NULL); + g_bytes_unref (bytes); } else { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid SSID for %s / %s", __func__, setting_name, key); @@ -736,12 +736,12 @@ static void password_raw_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) { const char *setting_name = nm_setting_get_name (setting); - GByteArray *array; + GBytes *bytes; - array = get_uchar_array (keyfile, setting_name, key, FALSE, TRUE); - if (array) { - g_object_set (setting, key, array, NULL); - g_byte_array_free (array, TRUE); + bytes = get_bytes (keyfile, setting_name, key, FALSE, TRUE); + if (bytes) { + g_object_set (setting, key, bytes, NULL); + g_bytes_unref (bytes); } else { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid raw password for %s / %s", __func__, setting_name, key); @@ -749,7 +749,7 @@ password_raw_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, con } static char * -get_cert_path (const char *keyfile_path, GByteArray *cert_path) +get_cert_path (const char *keyfile_path, const guint8 *cert_path, gsize cert_path_len) { const char *base; char *p = NULL, *path, *dirname, *tmp; @@ -757,8 +757,8 @@ get_cert_path (const char *keyfile_path, GByteArray *cert_path) g_return_val_if_fail (keyfile_path != NULL, NULL); g_return_val_if_fail (cert_path != NULL, NULL); - base = path = g_malloc0 (cert_path->len + 1); - memcpy (path, cert_path->data, cert_path->len); + base = path = g_malloc0 (cert_path_len + 1); + memcpy (path, cert_path, cert_path_len); if (path[0] == '/') return path; @@ -791,37 +791,46 @@ has_cert_ext (const char *path) } static gboolean -handle_as_scheme (GByteArray *array, NMSetting *setting, const char *key) +handle_as_scheme (GBytes *bytes, NMSetting *setting, const char *key) { + const guint8 *data; + gsize data_len; + + data = g_bytes_get_data (bytes, &data_len); + /* It's the PATH scheme, can just set plain data */ - if ( (array->len > strlen (SCHEME_PATH)) - && g_str_has_prefix ((const char *) array->data, SCHEME_PATH) - && (array->data[array->len - 1] == '\0')) { - g_object_set (setting, key, array, NULL); + if ( (data_len > strlen (SCHEME_PATH)) + && g_str_has_prefix ((const char *) data, SCHEME_PATH) + && (data[data_len - 1] == '\0')) { + g_object_set (setting, key, bytes, NULL); return TRUE; } return FALSE; } static gboolean -handle_as_path (GByteArray *array, +handle_as_path (GBytes *bytes, NMSetting *setting, const char *key, const char *keyfile_path) { - gsize validate_len = array->len; - GByteArray *val; + const guint8 *data; + gsize data_len; + gsize validate_len; char *path; gboolean exists, success = FALSE; - if (array->len > 500 || array->len < 1) + data = g_bytes_get_data (bytes, &data_len); + if (data_len > 500 || data_len < 1) return FALSE; /* If there's a trailing NULL tell g_utf8_validate() to to until the NULL */ - if (array->data[array->len - 1] == '\0') + if (data[data_len - 1] == '\0') validate_len = -1; + else + validate_len = data_len; - if (g_utf8_validate ((const char *) array->data, validate_len, NULL) == FALSE) + if (g_utf8_validate ((const char *) data, validate_len, NULL) == FALSE) return FALSE; /* Might be a bare path without the file:// prefix; in that case @@ -829,18 +838,22 @@ handle_as_path (GByteArray *array, * relative path to the current directory. */ - path = get_cert_path (keyfile_path, array); + path = get_cert_path (keyfile_path, data, data_len); exists = g_file_test (path, G_FILE_TEST_EXISTS); if ( exists - || memchr (array->data, '/', array->len) + || memchr (data, '/', data_len) || has_cert_ext (path)) { + GByteArray *tmp; + GBytes *val; + /* Construct the proper value as required for the PATH scheme */ - val = g_byte_array_sized_new (strlen (SCHEME_PATH) + strlen (path) + 1); - g_byte_array_append (val, (const guint8 *) SCHEME_PATH, strlen (SCHEME_PATH)); - g_byte_array_append (val, (const guint8 *) path, strlen (path)); - g_byte_array_append (val, (const guint8 *) "\0", 1); + tmp = g_byte_array_sized_new (strlen (SCHEME_PATH) + strlen (path) + 1); + g_byte_array_append (tmp, (const guint8 *) SCHEME_PATH, strlen (SCHEME_PATH)); + g_byte_array_append (tmp, (const guint8 *) path, strlen (path)); + g_byte_array_append (tmp, (const guint8 *) "\0", 1); + val = g_byte_array_free_to_bytes (tmp); g_object_set (setting, key, val, NULL); - g_byte_array_free (val, TRUE); + g_bytes_unref (val); success = TRUE; /* Warn if the certificate didn't exist */ @@ -856,28 +869,28 @@ static void cert_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path) { const char *setting_name = nm_setting_get_name (setting); - GByteArray *array; + GBytes *bytes; gboolean success = FALSE; - array = get_uchar_array (keyfile, setting_name, key, TRUE, FALSE); - if (array && array->len > 0) { + bytes = get_bytes (keyfile, setting_name, key, TRUE, FALSE); + if (bytes) { /* Try as a path + scheme (ie, starts with "file://") */ - success = handle_as_scheme (array, setting, key); + success = handle_as_scheme (bytes, setting, key); /* If not, it might be a plain path */ if (success == FALSE) - success = handle_as_path (array, setting, key, keyfile_path); + success = handle_as_path (bytes, setting, key, keyfile_path); /* If neither of those two, assume blob with certificate data */ if (success == FALSE) - g_object_set (setting, key, array, NULL); + g_object_set (setting, key, bytes, NULL); } else { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid key/cert value for %s / %s", __func__, setting_name, key); } - if (array) - g_byte_array_free (array, TRUE); + if (bytes) + g_bytes_unref (bytes); } typedef struct { @@ -1107,9 +1120,10 @@ read_one_setting_value (NMSetting *setting, uint_val = g_ascii_strtoull (tmp_str, NULL, 10); g_free (tmp_str); g_object_set (setting, key, uint_val, NULL); - } else if (type == DBUS_TYPE_G_UCHAR_ARRAY) { + } else if (type == G_TYPE_BYTES) { gint *tmp; GByteArray *array; + GBytes *bytes; gsize length; int i; @@ -1128,27 +1142,20 @@ read_one_setting_value (NMSetting *setting, g_byte_array_append (array, (const unsigned char *) &v, sizeof (v)); } - g_object_set (setting, key, array, NULL); - g_byte_array_free (array, TRUE); + bytes = g_byte_array_free_to_bytes (array); + g_object_set (setting, key, bytes, NULL); + g_bytes_unref (bytes); g_free (tmp); - } else if (type == DBUS_TYPE_G_LIST_OF_STRING) { + } else if (type == G_TYPE_STRV) { gchar **sa; gsize length; - int i; - GSList *list = NULL; sa = nm_keyfile_plugin_kf_get_string_list (info->keyfile, setting_name, key, &length, NULL); - for (i = 0; i < length; i++) - list = g_slist_prepend (list, sa[i]); - - list = g_slist_reverse (list); - g_object_set (setting, key, list, NULL); - - g_slist_free (list); + g_object_set (setting, key, sa, NULL); g_strfreev (sa); - } else if (type == DBUS_TYPE_G_MAP_OF_STRING) { + } else if (type == G_TYPE_HASH_TABLE) { read_hash_of_string (info->keyfile, setting, key); - } else if (type == DBUS_TYPE_G_UINT_ARRAY) { + } else if (type == G_TYPE_ARRAY) { if (!read_array_of_uint (info->keyfile, setting, key)) { nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (read): '%s/%s' : '%s'", setting_name, key, G_VALUE_TYPE_NAME (value)); diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 3d5fce5367..5fbea1204b 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -139,19 +139,13 @@ test_read_valid_wired_connection (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; const char *tmp; const char *expected_id = "Test Wired Connection"; const char *expected_uuid = "4e80a56d-c99f-4aad-a6dd-b449bc398c57"; const guint64 expected_timestamp = 6654332; guint64 timestamp; - const char *expected_dns1 = "4.2.2.1"; - const char *expected_dns2 = "4.2.2.2"; - guint32 addr; - struct in6_addr addr6; - const char *expected6_dns1 = "1111:dddd::aaaa"; - const char *expected6_dns2 = "1::cafe"; const char *expected6_dnssearch1 = "super-domain.com"; const char *expected6_dnssearch2 = "redhat.com"; const char *expected6_dnssearch3 = "gnu.org"; @@ -250,18 +244,13 @@ test_read_valid_wired_connection (void) NM_SETTING_WIRED_SETTING_NAME); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - ASSERT (array != NULL, + mac = nm_setting_wired_get_mac_address (s_wired); + ASSERT (mac != NULL, "connection-verify-wired", "failed to verify %s: missing %s / %s key", TEST_WIRED_FILE, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value length", - TEST_WIRED_FILE, - NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_WIRED_SETTING_NAME, @@ -296,23 +285,13 @@ test_read_valid_wired_connection (void) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns1, &addr) > 0, - "connection-verify-wired", "failed to verify %s: couldn't convert DNS IP address #1", - TEST_WIRED_FILE, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 0) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #1", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - ASSERT (inet_pton (AF_INET, expected_dns2, &addr) > 0, - "connection-verify-wired", "failed to verify %s: couldn't convert DNS IP address #2", - TEST_WIRED_FILE, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_dns (s_ip4, 1) == addr, + ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #2", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -365,23 +344,13 @@ test_read_valid_wired_connection (void) NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected6_dns1, &addr6) > 0, - "connection-verify-wired", "failed to verify %s: couldn't convert DNS IP6 address #1", - TEST_WIRED_FILE, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 0), &addr6), + ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1111:dddd::aaaa") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #1", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - ASSERT (inet_pton (AF_INET6, expected6_dns2, &addr6) > 0, - "connection-verify-wired", "failed to verify %s: couldn't convert DNS IP address #2", - TEST_WIRED_FILE, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_setting_ip6_config_get_dns (s_ip6, 1), &addr6), + ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "1::cafe") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #2", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, @@ -539,16 +508,13 @@ test_write_wired_connection (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - GByteArray *mac; - unsigned char tmpmac[] = { 0x99, 0x88, 0x77, 0x66, 0x55, 0x44 }; + const char *mac = "99:88:77:66:55:44"; gboolean success; NMConnection *reread; char *testfile = NULL; GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - guint32 addr; - struct in6_addr addr6; const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; const char *address1 = "192.168.0.5"; @@ -599,13 +565,10 @@ test_write_wired_connection (void) s_wired = NM_SETTING_WIRED (nm_setting_wired_new ()); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, 900, NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ @@ -627,10 +590,8 @@ test_write_wired_connection (void) add_one_ip4_route (s_ip4, route4, route4_nh, 6, 4); /* DNS servers */ - inet_pton (AF_INET, dns1, &addr); - nm_setting_ip4_config_add_dns (s_ip4, addr); - inet_pton (AF_INET, dns2, &addr); - nm_setting_ip4_config_add_dns (s_ip4, addr); + nm_setting_ip4_config_add_dns (s_ip4, dns1); + nm_setting_ip4_config_add_dns (s_ip4, dns2); /* IP6 setting */ @@ -652,10 +613,8 @@ test_write_wired_connection (void) add_one_ip6_route (s_ip6, route6_4, route6_4_nh, 62, 0); /* DNS servers */ - inet_pton (AF_INET6, dns6_1, &addr6); - nm_setting_ip6_config_add_dns (s_ip6, &addr6); - inet_pton (AF_INET6, dns6_2, &addr6); - nm_setting_ip6_config_add_dns (s_ip6, &addr6); + nm_setting_ip6_config_add_dns (s_ip6, dns6_1); + nm_setting_ip6_config_add_dns (s_ip6, dns6_2); /* DNS searches */ nm_setting_ip6_config_add_dns_search (s_ip6, "wallaceandgromit.com"); @@ -810,7 +769,6 @@ test_write_ip6_wired_connection (void) GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - struct in6_addr addr6; const char *dns = "1::cafe"; const char *address = "abcd::beef"; const char *gw = "dcba::beef"; @@ -858,8 +816,7 @@ test_write_ip6_wired_connection (void) add_one_ip6_address (s_ip6, address, 64, gw); /* DNS servers */ - inet_pton (AF_INET6, dns, &addr6); - nm_setting_ip6_config_add_dns (s_ip6, &addr6); + nm_setting_ip6_config_add_dns (s_ip6, dns); /* DNS searches */ nm_setting_ip6_config_add_dns_search (s_ip6, "wallaceandgromit.com"); @@ -899,7 +856,7 @@ test_read_wired_mac_case (void) NMSettingConnection *s_con; NMSettingWired *s_wired; GError *error = NULL; - const GByteArray *array; + const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0xaa, 0xbb, 0xcc, 0x55 }; const char *tmp; const char *expected_id = "Test Wired Connection MAC Case"; @@ -962,18 +919,13 @@ test_read_wired_mac_case (void) NM_SETTING_WIRED_SETTING_NAME); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - ASSERT (array != NULL, + mac = nm_setting_wired_get_mac_address (s_wired); + ASSERT (mac != NULL, "connection-verify-wired", "failed to verify %s: missing %s / %s key", TEST_WIRED_MAC_CASE_FILE, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (array->len == ETH_ALEN, - "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value length", - TEST_WIRED_MAC_CASE_FILE, - NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_MAC_ADDRESS); - ASSERT (memcmp (array->data, &expected_mac_address[0], sizeof (expected_mac_address)) == 0, + ASSERT (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address)), "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_MAC_CASE_FILE, NM_SETTING_WIRED_SETTING_NAME, @@ -991,7 +943,7 @@ test_read_mac_old_format (void) NMSettingWired *s_wired; GError *error = NULL; gboolean success; - const GByteArray *array; + const char *mac; char expected_mac[ETH_ALEN] = { 0x00, 0x11, 0xaa, 0xbb, 0xcc, 0x55 }; char expected_cloned_mac[ETH_ALEN] = { 0x00, 0x16, 0xaa, 0xbb, 0xcc, 0xfe }; @@ -1007,16 +959,14 @@ test_read_mac_old_format (void) g_assert (s_wired); /* MAC address */ - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, expected_mac, ETH_ALEN) == 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, ETH_ALEN)); /* Cloned MAC address */ - array = nm_setting_wired_get_cloned_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert (memcmp (array->data, expected_cloned_mac, ETH_ALEN) == 0); + mac = nm_setting_wired_get_cloned_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_cloned_mac, ETH_ALEN)); g_object_unref (connection); } @@ -1030,7 +980,7 @@ test_read_mac_ib_old_format (void) NMSettingInfiniband *s_ib; GError *error = NULL; gboolean success; - const GByteArray *array; + const char *mac; 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 }; @@ -1047,10 +997,9 @@ test_read_mac_ib_old_format (void) g_assert (s_ib); /* MAC address */ - 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); + mac = nm_setting_infiniband_get_mac_address (s_ib); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); g_object_unref (connection); } @@ -1063,8 +1012,8 @@ test_read_valid_wireless_connection (void) NMSettingWireless *s_wireless; NMSettingIP4Config *s_ip4; GError *error = NULL; - const GByteArray *array; - char expected_bssid[ETH_ALEN] = { 0x00, 0x1a, 0x33, 0x44, 0x99, 0x82 }; + const char *bssid; + const guint8 expected_bssid[ETH_ALEN] = { 0x00, 0x1a, 0x33, 0x44, 0x99, 0x82 }; const char *tmp; const char *expected_id = "Test Wireless Connection"; const char *expected_uuid = "2f962388-e5f3-45af-a62c-ac220b8f7baa"; @@ -1136,18 +1085,13 @@ test_read_valid_wireless_connection (void) NM_SETTING_WIRED_SETTING_NAME); /* BSSID */ - array = nm_setting_wireless_get_bssid (s_wireless); - ASSERT (array != NULL, + bssid = nm_setting_wireless_get_bssid (s_wireless); + ASSERT (bssid != NULL, "connection-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_WIRELESS_FILE, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BSSID); - ASSERT (array->len == ETH_ALEN, - "connection-verify-wireless", "failed to verify %s: unexpected %s / %s key value length", - TEST_WIRELESS_FILE, - NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_BSSID); - ASSERT (memcmp (array->data, &expected_bssid[0], sizeof (expected_bssid)) == 0, + ASSERT (nm_utils_hwaddr_matches (bssid, -1, expected_bssid, sizeof (expected_bssid)), "connection-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_WIRELESS_FILE, NM_SETTING_WIRELESS_SETTING_NAME, @@ -1181,9 +1125,8 @@ test_write_wireless_connection (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - GByteArray *bssid; - unsigned char tmpbssid[] = { 0xaa, 0xb9, 0xa1, 0x74, 0x55, 0x44 }; - GByteArray *ssid; + const char *bssid = "aa:b9:a1:74:55:44"; + GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; gboolean success; NMConnection *reread; @@ -1215,11 +1158,7 @@ test_write_wireless_connection (void) s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); nm_connection_add_setting (connection, NM_SETTING (s_wireless)); - bssid = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (bssid, &tmpbssid[0], sizeof (tmpbssid)); - - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wireless, NM_SETTING_WIRELESS_BSSID, bssid, @@ -1227,8 +1166,7 @@ test_write_wireless_connection (void) NM_SETTING_WIRED_MTU, 1000, NULL); - g_byte_array_free (bssid, TRUE); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ @@ -1282,7 +1220,9 @@ test_read_string_ssid (void) NMConnection *connection; NMSettingWireless *s_wireless; GError *error = NULL; - const GByteArray *array; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; const char *expected_ssid = "blah blah ssid 1234"; connection = nm_keyfile_plugin_connection_from_file (TEST_STRING_SSID_FILE, NULL); @@ -1301,14 +1241,15 @@ test_read_string_ssid (void) NM_SETTING_WIRELESS_SETTING_NAME); /* SSID */ - array = nm_setting_wireless_get_ssid (s_wireless); - ASSERT (array != NULL, + ssid = nm_setting_wireless_get_ssid (s_wireless); + ASSERT (ssid != NULL, "connection-verify-wireless", "failed to verify %s: missing %s / %s key", TEST_STRING_SSID_FILE, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID); - g_assert_cmpint (array->len, ==, strlen (expected_ssid)); - g_assert (memcmp (array->data, expected_ssid, array->len) == 0); + ssid_data = g_bytes_get_data (ssid, &ssid_len); + g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); + g_assert (memcmp (ssid_data, expected_ssid, ssid_len) == 0); g_object_unref (connection); } @@ -1321,7 +1262,7 @@ test_write_string_ssid (void) NMSettingWireless *s_wireless; NMSettingIP4Config *s_ip4; char *uuid, *testfile = NULL, *tmp; - GByteArray *ssid; + GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 32, 46, 92, 46, 36, 37, 126, 93 }; gboolean success; NMConnection *reread; @@ -1350,10 +1291,9 @@ test_write_string_ssid (void) s_wireless = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); nm_connection_add_setting (connection, NM_SETTING (s_wireless)); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ @@ -1411,7 +1351,9 @@ test_read_intlist_ssid (void) NMSettingWireless *s_wifi; GError *error = NULL; gboolean success; - const GByteArray *array; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; const char *expected_ssid = "blah1234"; connection = nm_keyfile_plugin_connection_from_file (TEST_INTLIST_SSID_FILE, &error); @@ -1426,10 +1368,11 @@ test_read_intlist_ssid (void) s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - array = nm_setting_wireless_get_ssid (s_wifi); - g_assert (array != NULL); - g_assert_cmpint (array->len, ==, strlen (expected_ssid)); - g_assert_cmpint (memcmp (array->data, expected_ssid, strlen (expected_ssid)), ==, 0); + ssid = nm_setting_wireless_get_ssid (s_wifi); + g_assert (ssid != NULL); + ssid_data = g_bytes_get_data (ssid, &ssid_len); + g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); + g_assert_cmpint (memcmp (ssid_data, expected_ssid, strlen (expected_ssid)), ==, 0); g_object_unref (connection); } @@ -1442,7 +1385,7 @@ test_write_intlist_ssid (void) NMSettingWireless *s_wifi; NMSettingIP4Config *s_ip4; char *uuid, *testfile = NULL; - GByteArray *ssid; + GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; gboolean success; NMConnection *reread; @@ -1475,10 +1418,9 @@ test_write_intlist_ssid (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); @@ -1535,7 +1477,9 @@ test_read_intlike_ssid (void) NMSettingWireless *s_wifi; GError *error = NULL; gboolean success; - const GByteArray *array; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; const char *expected_ssid = "101"; connection = nm_keyfile_plugin_connection_from_file (TEST_INTLIKE_SSID_FILE, &error); @@ -1550,10 +1494,11 @@ test_read_intlike_ssid (void) s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - array = nm_setting_wireless_get_ssid (s_wifi); - g_assert (array != NULL); - g_assert_cmpint (array->len, ==, strlen (expected_ssid)); - g_assert_cmpint (memcmp (array->data, expected_ssid, strlen (expected_ssid)), ==, 0); + ssid = nm_setting_wireless_get_ssid (s_wifi); + g_assert (ssid != NULL); + ssid_data = g_bytes_get_data (ssid, &ssid_len); + g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); + g_assert_cmpint (memcmp (ssid_data, expected_ssid, strlen (expected_ssid)), ==, 0); g_object_unref (connection); } @@ -1567,7 +1512,9 @@ test_read_intlike_ssid_2 (void) NMSettingWireless *s_wifi; GError *error = NULL; gboolean success; - const GByteArray *array; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; const char *expected_ssid = "11;12;13;"; connection = nm_keyfile_plugin_connection_from_file (TEST_INTLIKE_SSID_2_FILE, &error); @@ -1582,10 +1529,11 @@ test_read_intlike_ssid_2 (void) s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - array = nm_setting_wireless_get_ssid (s_wifi); - g_assert (array != NULL); - g_assert_cmpint (array->len, ==, strlen (expected_ssid)); - g_assert_cmpint (memcmp (array->data, expected_ssid, strlen (expected_ssid)), ==, 0); + ssid = nm_setting_wireless_get_ssid (s_wifi); + g_assert (ssid != NULL); + ssid_data = g_bytes_get_data (ssid, &ssid_len); + g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); + g_assert_cmpint (memcmp (ssid_data, expected_ssid, strlen (expected_ssid)), ==, 0); g_object_unref (connection); } @@ -1598,7 +1546,7 @@ test_write_intlike_ssid (void) NMSettingWireless *s_wifi; NMSettingIP4Config *s_ip4; char *uuid, *testfile = NULL; - GByteArray *ssid; + GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; gboolean success; NMConnection *reread; @@ -1630,10 +1578,9 @@ test_write_intlike_ssid (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); @@ -1685,7 +1632,7 @@ test_write_intlike_ssid_2 (void) NMSettingWireless *s_wifi; NMSettingIP4Config *s_ip4; char *uuid, *testfile = NULL; - GByteArray *ssid; + GBytes *ssid; unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; gboolean success; NMConnection *reread; @@ -1717,10 +1664,9 @@ test_write_intlike_ssid_2 (void) g_assert (s_wifi); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); @@ -1775,8 +1721,8 @@ test_read_bt_dun_connection (void) NMSettingSerial *s_serial; NMSettingGsm *s_gsm; GError *error = NULL; - const GByteArray *array; - char expected_bdaddr[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; + const char *bdaddr; + const guint8 expected_bdaddr[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; const char *tmp; const char *expected_id = "AT&T Data Connect BT"; const char *expected_uuid = "089130ab-ce28-46e4-ad77-d44869b03d19"; @@ -1834,18 +1780,13 @@ test_read_bt_dun_connection (void) NM_SETTING_WIRED_SETTING_NAME); /* BDADDR */ - array = nm_setting_bluetooth_get_bdaddr (s_bluetooth); - ASSERT (array != NULL, + bdaddr = nm_setting_bluetooth_get_bdaddr (s_bluetooth); + ASSERT (bdaddr != NULL, "connection-verify-bt", "failed to verify %s: missing %s / %s key", TEST_BT_DUN_FILE, NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR); - ASSERT (array->len == ETH_ALEN, - "connection-verify-bt", "failed to verify %s: unexpected %s / %s key value length", - TEST_BT_DUN_FILE, - NM_SETTING_BLUETOOTH_SETTING_NAME, - NM_SETTING_BLUETOOTH_BDADDR); - ASSERT (memcmp (array->data, &expected_bdaddr[0], sizeof (expected_bdaddr)) == 0, + ASSERT (nm_utils_hwaddr_matches (bdaddr, -1, expected_bdaddr, sizeof (expected_bdaddr)), "connection-verify-bt", "failed to verify %s: unexpected %s / %s key value", TEST_BT_DUN_FILE, NM_SETTING_BLUETOOTH_SETTING_NAME, @@ -1931,8 +1872,7 @@ test_write_bt_dun_connection (void) NMSettingIP4Config *s_ip4; NMSettingGsm *s_gsm; char *uuid; - GByteArray *bdaddr; - unsigned char tmpbdaddr[] = { 0xaa, 0xb9, 0xa1, 0x74, 0x55, 0x44 }; + const char *bdaddr = "aa:b9:a1:74:55:44"; gboolean success; NMConnection *reread; char *testfile = NULL; @@ -1963,16 +1903,11 @@ test_write_bt_dun_connection (void) s_bt = NM_SETTING_BLUETOOTH (nm_setting_bluetooth_new ()); nm_connection_add_setting (connection, NM_SETTING (s_bt)); - bdaddr = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (bdaddr, &tmpbdaddr[0], sizeof (tmpbdaddr)); - g_object_set (s_bt, NM_SETTING_BLUETOOTH_BDADDR, bdaddr, NM_SETTING_BLUETOOTH_TYPE, NM_SETTING_BLUETOOTH_TYPE_DUN, NULL); - g_byte_array_free (bdaddr, TRUE); - /* IP4 setting */ s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); @@ -2262,7 +2197,7 @@ test_read_wired_8021x_tls_blob_connection (void) GError *error = NULL; const char *tmp; gboolean success; - const GByteArray *array; + GBytes *blob; connection = nm_keyfile_plugin_connection_from_file (TEST_WIRED_TLS_BLOB_FILE, &error); if (connection == NULL) { @@ -2306,9 +2241,9 @@ test_read_wired_8021x_tls_blob_connection (void) g_assert (tmp == NULL); /* Validate the path */ - array = nm_setting_802_1x_get_ca_cert_blob (s_8021x); - g_assert (array != NULL); - g_assert_cmpint (array->len, ==, 568); + blob = nm_setting_802_1x_get_ca_cert_blob (s_8021x); + g_assert (blob != NULL); + g_assert_cmpint (g_bytes_get_size (blob), ==, 568); tmp = nm_setting_802_1x_get_client_cert_path (s_8021x); g_assert_cmpstr (tmp, ==, "/home/dcbw/Desktop/certinfra/client.pem"); @@ -2776,7 +2711,7 @@ test_read_infiniband_connection (void) NMSettingConnection *s_con; NMSettingInfiniband *s_ib; GError *error = NULL; - const GByteArray *array; + const char *mac; 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 }; @@ -2801,10 +2736,9 @@ test_read_infiniband_connection (void) 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); + mac = nm_setting_infiniband_get_mac_address (s_ib); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); g_object_unref (connection); } @@ -2818,10 +2752,7 @@ test_write_infiniband_connection (void) 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 - }; + const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; gboolean success; NMConnection *reread; char *testfile = NULL; @@ -2852,14 +2783,11 @@ test_write_infiniband_connection (void) 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, NM_SETTING_INFINIBAND_TRANSPORT_MODE, "datagram", NULL); - g_byte_array_free (mac, TRUE); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -3028,7 +2956,7 @@ test_read_bridge_component (void) NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; - const GByteArray *array; + const char *mac; guint8 expected_mac[ETH_ALEN] = { 0x00, 0x22, 0x15, 0x59, 0x62, 0x97 }; GError *error = NULL; const char *expected_id = "Test Bridge Component"; @@ -3053,10 +2981,9 @@ test_read_bridge_component (void) /* Wired setting */ s_wired = nm_connection_get_setting_wired (connection); g_assert (s_wired); - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert_cmpint (memcmp (array->data, expected_mac, sizeof (expected_mac)), ==, 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); /* BridgePort setting */ s_port = nm_connection_get_setting_bridge_port (connection); @@ -3076,8 +3003,7 @@ test_write_bridge_component (void) NMSettingBridgePort *s_port; NMSettingWired *s_wired; char *uuid; - GByteArray *mac; - guint8 tmpmac[] = { 0x99, 0x88, 0x77, 0x66, 0x55, 0x44 }; + const char *mac = "99:88:77:66:55:44"; gboolean success; NMConnection *reread; char *testfile = NULL; @@ -3109,13 +3035,10 @@ test_write_bridge_component (void) g_assert (s_wired); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - mac = g_byte_array_sized_new (ETH_ALEN); - g_byte_array_append (mac, &tmpmac[0], sizeof (tmpmac)); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NM_SETTING_WIRED_MTU, 1300, NULL); - g_byte_array_free (mac, TRUE); /* BridgePort setting */ s_port = (NMSettingBridgePort *) nm_setting_bridge_port_new (); @@ -3154,7 +3077,7 @@ test_read_new_wired_group_name (void) { NMConnection *connection; NMSettingWired *s_wired; - const GByteArray *array; + const char *mac; guint8 expected_mac[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; GError *error = NULL; gboolean success; @@ -3171,10 +3094,9 @@ test_read_new_wired_group_name (void) g_assert (s_wired); g_assert_cmpint (nm_setting_wired_get_mtu (s_wired), ==, 1400); - array = nm_setting_wired_get_mac_address (s_wired); - g_assert (array); - g_assert_cmpint (array->len, ==, ETH_ALEN); - g_assert_cmpint (memcmp (array->data, expected_mac, sizeof (expected_mac)), ==, 0); + mac = nm_setting_wired_get_mac_address (s_wired); + g_assert (mac); + g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); g_object_unref (connection); } @@ -3260,7 +3182,9 @@ test_read_new_wireless_group_names (void) NMConnection *connection; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - const GByteArray *array; + GBytes *ssid; + const guint8 *ssid_data; + gsize ssid_len; const char *expected_ssid = "foobar"; GError *error = NULL; gboolean success; @@ -3276,10 +3200,11 @@ test_read_new_wireless_group_names (void) s_wifi = nm_connection_get_setting_wireless (connection); g_assert (s_wifi); - array = nm_setting_wireless_get_ssid (s_wifi); - g_assert (array); - g_assert_cmpint (array->len, ==, strlen (expected_ssid)); - g_assert_cmpint (memcmp (array->data, expected_ssid, array->len), ==, 0); + ssid = nm_setting_wireless_get_ssid (s_wifi); + g_assert (ssid); + ssid_data = g_bytes_get_data (ssid, &ssid_len); + g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); + g_assert_cmpint (memcmp (ssid_data, expected_ssid, ssid_len), ==, 0); g_assert_cmpstr (nm_setting_wireless_get_mode (s_wifi), ==, NM_SETTING_WIRELESS_MODE_INFRA); @@ -3300,7 +3225,7 @@ test_write_new_wireless_group_names (void) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; char *uuid; - GByteArray *ssid; + GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; const char *expected_psk = "asdfasdfasdfa12315"; gboolean success; @@ -3331,13 +3256,12 @@ test_write_new_wireless_group_names (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (tmpssid)); - g_byte_array_append (ssid, &tmpssid[0], sizeof (tmpssid)); + ssid = g_bytes_new (tmpssid, sizeof (tmpssid)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, NM_SETTING_WIRELESS_MODE, NM_SETTING_WIRELESS_MODE_INFRA, NULL); - g_byte_array_free (ssid, TRUE); + g_bytes_unref (ssid); /* WiFi security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 0813290324..ae8f0a912b 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -104,29 +104,13 @@ ip4_dns_writer (GKeyFile *file, const char *key, const GValue *value) { - GArray *array; char **list; - int i, num = 0; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UINT_ARRAY)); - - array = (GArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - list = g_new0 (char *, array->len + 1); - - for (i = 0; i < array->len; i++) { - char *buf = g_new (char, INET_ADDRSTRLEN); - guint32 addr; - - addr = g_array_index (array, guint32, i); - nm_utils_inet4_ntop (addr, buf); - list[num++] = buf; + list = g_value_get_boxed (value); + if (list && list[0]) { + nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, + (const char **) list, g_strv_length (list)); } - - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, (const char **) list, num); - g_strfreev (list); } static void @@ -148,12 +132,21 @@ write_ip4_values (GKeyFile *file, output = g_string_sized_new (2*INET_ADDRSTRLEN + 10); for (i = 0; i < array->len; i++) { - GArray *tuple = g_ptr_array_index (array, i); + if (is_route) { + NMIP4Route *route = array->pdata[i]; - addr = g_array_index (tuple, guint32, 0); - plen = g_array_index (tuple, guint32, 1); - gw = g_array_index (tuple, guint32, 2); - metric = is_route ? g_array_index (tuple, guint32, 3) : 0; + addr = nm_ip4_route_get_dest (route); + plen = nm_ip4_route_get_prefix (route); + gw = nm_ip4_route_get_next_hop (route); + metric = nm_ip4_route_get_metric (route); + } else { + NMIP4Address *address = array->pdata[i]; + + addr = nm_ip4_address_get_address (address); + plen = nm_ip4_address_get_prefix (address); + gw = nm_ip4_address_get_gateway (address); + metric = 0; + } g_string_set_size (output, 0); g_string_append_printf (output, "%s/%u", @@ -187,8 +180,6 @@ ip4_addr_writer (GKeyFile *file, GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT)); - array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) write_ip4_values (file, setting_name, array, FALSE); @@ -216,8 +207,6 @@ ip4_route_writer (GKeyFile *file, GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT)); - array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) write_ip4_values (file, setting_name, array, TRUE); @@ -231,79 +220,38 @@ ip6_dns_writer (GKeyFile *file, const char *key, const GValue *value) { - GPtrArray *array; - GByteArray *byte_array; char **list; - int i, num = 0; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR)); - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - list = g_new0 (char *, array->len + 1); - - for (i = 0; i < array->len; i++) { - char *buf = g_new (char, INET6_ADDRSTRLEN); - - byte_array = g_ptr_array_index (array, i); - nm_utils_inet6_ntop ((const struct in6_addr *) byte_array->data, buf); - list[num++] = buf; + list = g_value_get_boxed (value); + if (list && list[0]) { + nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, + (const char **) list, g_strv_length (list)); } - - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, (const char **) list, num); - g_strfreev (list); -} - -static void -ip6_array_to_addr (GValueArray *values, - guint32 idx, - char *buf, - struct in6_addr *out_addr) -{ - GByteArray *byte_array; - GValue *addr_val; - const struct in6_addr *addr; - - addr_val = g_value_array_get_nth (values, idx); - byte_array = g_value_get_boxed (addr_val); - addr = (const struct in6_addr *) byte_array->data; - - nm_utils_inet6_ntop (addr, buf); - - if (out_addr) - *out_addr = *addr; } static char * -ip6_array_to_addr_prefix (GValueArray *values, gboolean force_write_gateway) +ip6_values_to_addr_prefix (const struct in6_addr *addr, guint prefix, const struct in6_addr *gw, + gboolean force_write_gateway) { - GValue *prefix_val; - char *ret = NULL; GString *ip6_str; char buf[INET6_ADDRSTRLEN]; - struct in6_addr addr; /* address */ - ip6_array_to_addr (values, 0, buf, NULL); + nm_utils_inet6_ntop (addr, buf); /* Enough space for the address, '/', and the prefix */ ip6_str = g_string_sized_new ((INET6_ADDRSTRLEN * 2) + 5); /* prefix */ g_string_append (ip6_str, buf); - prefix_val = g_value_array_get_nth (values, 1); - g_string_append_printf (ip6_str, "/%u", g_value_get_uint (prefix_val)); + g_string_append_printf (ip6_str, "/%u", prefix); - ip6_array_to_addr (values, 2, buf, &addr); - if (force_write_gateway || !IN6_IS_ADDR_UNSPECIFIED (&addr)) + /* gateway */ + nm_utils_inet6_ntop (gw, buf); + if (force_write_gateway || !IN6_IS_ADDR_UNSPECIFIED (gw)) g_string_append_printf (ip6_str, ",%s", buf); - ret = ip6_str->str; - g_string_free (ip6_str, FALSE); - - return ret; + return g_string_free (ip6_str, FALSE); } static void @@ -318,24 +266,19 @@ ip6_addr_writer (GKeyFile *file, const char *setting_name = nm_setting_get_name (setting); int i, j; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS)); - array = (GPtrArray *) g_value_get_boxed (value); if (!array || !array->len) return; for (i = 0, j = 1; i < array->len; i++) { - GValueArray *values = g_ptr_array_index (array, i); + NMIP6Address *addr = array->pdata[i]; char *key_name, *ip6_addr; - if (values->n_values != 3) { - nm_log_warn (LOGD_SETTINGS, "%s: error writing IP6 address %d (address array " - "length %d is not 3)", __func__, i, values->n_values); - continue; - } - - /* we allow omitting the gateway if it's :: */ - ip6_addr = ip6_array_to_addr_prefix (values, FALSE); + ip6_addr = ip6_values_to_addr_prefix (nm_ip6_address_get_address (addr), + nm_ip6_address_get_prefix (addr), + nm_ip6_address_get_gateway (addr), + /* we allow omitting the gateway if it's :: */ + FALSE); /* Write it out */ key_name = g_strdup_printf ("address%d", j++); nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, ip6_addr); @@ -357,14 +300,12 @@ ip6_route_writer (GKeyFile *file, GString *output; int i, j; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE)); - array = (GPtrArray *) g_value_get_boxed (value); if (!array || !array->len) return; for (i = 0, j = 1; i < array->len; i++) { - GValueArray *values = g_ptr_array_index (array, i); + NMIP6Route *route = array->pdata[i]; char *key_name; char *addr_str; guint metric; @@ -372,8 +313,7 @@ ip6_route_writer (GKeyFile *file, output = g_string_new (""); /* Metric */ - value = g_value_array_get_nth (values, 3); - metric = g_value_get_uint (value); + metric = nm_ip6_route_get_metric (route); /* Address, prefix and next hop * We allow omitting the gateway ::, if we also omit the metric @@ -385,7 +325,10 @@ ip6_route_writer (GKeyFile *file, * But if possible, we omit them both (",::,0") or only the metric * (",0"). **/ - addr_str = ip6_array_to_addr_prefix (values, metric != 0); + addr_str = ip6_values_to_addr_prefix (nm_ip6_route_get_dest (route), + nm_ip6_route_get_prefix (route), + nm_ip6_route_get_next_hop (route), + metric != 0); g_string_append (output, addr_str); g_free (addr_str); @@ -402,29 +345,6 @@ ip6_route_writer (GKeyFile *file, } -static void -mac_address_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GByteArray *array; - const char *setting_name = nm_setting_get_name (setting); - char *mac; - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UCHAR_ARRAY)); - - array = (GByteArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - mac = nm_utils_hwaddr_ntoa (array->data, array->len); - nm_keyfile_plugin_kf_set_string (file, setting_name, key, mac); - g_free (mac); -} - static void write_hash_of_string (GKeyFile *file, NMSetting *setting, @@ -471,24 +391,29 @@ ssid_writer (GKeyFile *file, const char *key, const GValue *value) { - GByteArray *array; + GBytes *bytes; + const guint8 *ssid_data; + gsize ssid_len; const char *setting_name = nm_setting_get_name (setting); gboolean new_format = TRUE; unsigned int semicolons = 0; int i, *tmp_array; char *ssid; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UCHAR_ARRAY)); + g_return_if_fail (G_VALUE_HOLDS (value, G_TYPE_BYTES)); - array = (GByteArray *) g_value_get_boxed (value); - if (!array || !array->len) + bytes = g_value_get_boxed (value); + if (!bytes) + return; + ssid_data = g_bytes_get_data (bytes, &ssid_len); + if (ssid_len == 0) return; /* Check whether each byte is printable. If not, we have to use an * integer list, otherwise we can just use a string. */ - for (i = 0; i < array->len; i++) { - char c = array->data[i] & 0xFF; + for (i = 0; i < ssid_len; i++) { + char c = ssid_data[i] & 0xFF; if (!g_ascii_isprint (c)) { new_format = FALSE; break; @@ -498,26 +423,26 @@ ssid_writer (GKeyFile *file, } if (new_format) { - ssid = g_malloc0 (array->len + semicolons + 1); + ssid = g_malloc0 (ssid_len + semicolons + 1); if (semicolons == 0) - memcpy (ssid, array->data, array->len); + memcpy (ssid, ssid_data, ssid_len); else { /* Escape semicolons with backslashes to make strings * containing ';', such as '16;17;' unambiguous */ int j = 0; - for (i = 0; i < array->len; i++) { - if (array->data[i] == ';') + for (i = 0; i < ssid_len; i++) { + if (ssid_data[i] == ';') ssid[j++] = '\\'; - ssid[j++] = array->data[i]; + ssid[j++] = ssid_data[i]; } } nm_keyfile_plugin_kf_set_string (file, setting_name, key, ssid); g_free (ssid); } else { - tmp_array = g_new (gint, array->len); - for (i = 0; i < array->len; i++) - tmp_array[i] = (int) array->data[i]; - nm_keyfile_plugin_kf_set_integer_list (file, setting_name, key, tmp_array, array->len); + tmp_array = g_new (gint, ssid_len); + for (i = 0; i < ssid_len; i++) + tmp_array[i] = (int) ssid_data[i]; + nm_keyfile_plugin_kf_set_integer_list (file, setting_name, key, tmp_array, ssid_len); g_free (tmp_array); } } @@ -554,7 +479,7 @@ typedef struct ObjectType { NMSetting8021xCKScheme (*scheme_func) (NMSetting8021x *setting); NMSetting8021xCKFormat (*format_func) (NMSetting8021x *setting); const char * (*path_func) (NMSetting8021x *setting); - const GByteArray * (*blob_func) (NMSetting8021x *setting); + GBytes * (*blob_func) (NMSetting8021x *setting); } ObjectType; static const ObjectType objtypes[10] = { @@ -611,7 +536,8 @@ static const ObjectType objtypes[10] = { static gboolean write_cert_key_file (const char *path, - const GByteArray *data, + const guint8 *data, + gsize data_len, GError **error) { char *tmppath; @@ -644,8 +570,8 @@ write_cert_key_file (const char *path, } errno = 0; - written = write (fd, data->data, data->len); - if (written != data->len) { + written = write (fd, data, data_len); + if (written != data_len) { close (fd); unlink (tmppath); g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, @@ -713,13 +639,16 @@ cert_writer (GKeyFile *file, nm_keyfile_plugin_kf_set_string (file, setting_name, key, path); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { - const GByteArray *blob; + GBytes *blob; + const guint8 *blob_data; + gsize blob_len; gboolean success; GError *error = NULL; char *new_path; blob = objtype->blob_func (NM_SETTING_802_1X (setting)); g_assert (blob); + blob_data = g_bytes_get_data (blob, &blob_len); if (objtype->format_func) { /* Get the extension for a private key */ @@ -728,7 +657,7 @@ cert_writer (GKeyFile *file, ext = "p12"; } else { /* DER or PEM format certificate? */ - if (blob->len > 2 && blob->data[0] == 0x30 && blob->data[1] == 0x82) + if (blob_len > 2 && blob_data[0] == 0x30 && blob_data[1] == 0x82) ext = "der"; } @@ -738,7 +667,7 @@ cert_writer (GKeyFile *file, new_path = g_strdup_printf ("%s/%s-%s.%s", keyfile_dir, uuid, objtype->suffix, ext); g_assert (new_path); - success = write_cert_key_file (new_path, blob, &error); + success = write_cert_key_file (new_path, blob_data, blob_len, &error); if (success) { /* Write the path value to the keyfile */ nm_keyfile_plugin_kf_set_string (file, setting_name, key, new_path); @@ -773,9 +702,6 @@ static KeyWriter key_writers[] = { { NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_TYPE, setting_alias_writer }, - { NM_SETTING_BRIDGE_SETTING_NAME, - NM_SETTING_BRIDGE_MAC_ADDRESS, - mac_address_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES, ip4_addr_writer }, @@ -797,30 +723,6 @@ static KeyWriter key_writers[] = { { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS, ip6_dns_writer }, - { NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_CLONED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_BSSID, - mac_address_writer }, - { 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_WIMAX_SETTING_NAME, - NM_SETTING_WIMAX_MAC_ADDRESS, - mac_address_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, ssid_writer }, @@ -929,40 +831,33 @@ write_setting_value (NMSetting *setting, nm_keyfile_plugin_kf_set_boolean (info->keyfile, setting_name, key, g_value_get_boolean (value)); } else if (type == G_TYPE_CHAR) { nm_keyfile_plugin_kf_set_integer (info->keyfile, setting_name, key, (int) g_value_get_schar (value)); - } else if (type == DBUS_TYPE_G_UCHAR_ARRAY) { - GByteArray *array; + } else if (type == G_TYPE_BYTES) { + GBytes *bytes; + const guint8 *data; + gsize len = 0; - array = (GByteArray *) g_value_get_boxed (value); - if (array && array->len > 0) { + bytes = g_value_get_boxed (value); + data = bytes ? g_bytes_get_data (bytes, &len) : NULL; + + if (data != NULL && len > 0) { int *tmp_array; int i; - tmp_array = g_new (gint, array->len); - for (i = 0; i < array->len; i++) - tmp_array[i] = (int) array->data[i]; + tmp_array = g_new (gint, len); + for (i = 0; i < len; i++) + tmp_array[i] = (int) data[i]; - nm_keyfile_plugin_kf_set_integer_list (info->keyfile, setting_name, key, tmp_array, array->len); + nm_keyfile_plugin_kf_set_integer_list (info->keyfile, setting_name, key, tmp_array, len); g_free (tmp_array); } - } else if (type == DBUS_TYPE_G_LIST_OF_STRING) { - GSList *list; - GSList *iter; + } else if (type == G_TYPE_STRV) { + char **array; - list = (GSList *) g_value_get_boxed (value); - if (list) { - char **array; - int i = 0; - - array = g_new (char *, g_slist_length (list)); - for (iter = list; iter; iter = iter->next) - array[i++] = iter->data; - - nm_keyfile_plugin_kf_set_string_list (info->keyfile, setting_name, key, (const gchar **const) array, i); - g_free (array); - } - } else if (type == DBUS_TYPE_G_MAP_OF_STRING) { + array = (char **) g_value_get_boxed (value); + nm_keyfile_plugin_kf_set_string_list (info->keyfile, setting_name, key, (const gchar **const) array, g_strv_length (array)); + } else if (type == G_TYPE_HASH_TABLE) { write_hash_of_string (info->keyfile, setting, key, value); - } else if (type == DBUS_TYPE_G_UINT_ARRAY) { + } else if (type == G_TYPE_ARRAY) { if (!write_array_of_uint (info->keyfile, setting, key, value)) { nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (write) '%s/%s' : '%s'", setting_name, key, g_type_name (type)); diff --git a/src/supplicant-manager/nm-supplicant-config.c b/src/supplicant-manager/nm-supplicant-config.c index 3f7fd721c0..f4ee4c3a2c 100644 --- a/src/supplicant-manager/nm-supplicant-config.c +++ b/src/supplicant-manager/nm-supplicant-config.c @@ -165,7 +165,7 @@ nm_supplicant_config_add_option (NMSupplicantConfig *self, static gboolean nm_supplicant_config_add_blob (NMSupplicantConfig *self, const char *key, - const GByteArray *value, + GBytes *value, const char *blobid) { NMSupplicantConfigPrivate *priv; @@ -173,16 +173,20 @@ nm_supplicant_config_add_blob (NMSupplicantConfig *self, ConfigOption *opt; OptType type; GByteArray *blob; + const guint8 *data; + gsize data_len; g_return_val_if_fail (NM_IS_SUPPLICANT_CONFIG (self), FALSE); g_return_val_if_fail (key != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); - g_return_val_if_fail (value->len > 0, FALSE); g_return_val_if_fail (blobid != NULL, FALSE); + data = g_bytes_get_data (value, &data_len); + g_return_val_if_fail (data_len > 0, FALSE); + priv = NM_SUPPLICANT_CONFIG_GET_PRIVATE (self); - type = nm_supplicant_settings_verify_setting (key, (const char *) value->data, value->len); + type = nm_supplicant_settings_verify_setting (key, (const char *) data, data_len); if (type == TYPE_INVALID) { nm_log_warn (LOGD_SUPPLICANT, "Key '%s' and/or it's contained value is invalid.", key); return FALSE; @@ -194,8 +198,8 @@ nm_supplicant_config_add_blob (NMSupplicantConfig *self, return FALSE; } - blob = g_byte_array_sized_new (value->len); - g_byte_array_append (blob, value->data, value->len); + blob = g_byte_array_sized_new (data_len); + g_byte_array_append (blob, data, data_len); opt = g_slice_new0 (ConfigOption); opt->value = g_strdup_printf ("blob://%s", blobid); @@ -327,9 +331,6 @@ nm_supplicant_config_get_blobs (NMSupplicantConfig * self) return NM_SUPPLICANT_CONFIG_GET_PRIVATE (self)->blobs; } -#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x" -#define MAC_ARG(x) ((guint8*)(x))[0],((guint8*)(x))[1],((guint8*)(x))[2],((guint8*)(x))[3],((guint8*)(x))[4],((guint8*)(x))[5] - #define TWO_GHZ_FREQS "2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,2484" #define FIVE_GHZ_FREQS "4915,4920,4925,4935,4940,4945,4960,4980,5035,5040,5045,5055,5060,5080," \ "5170,5180,5190,5200,5210,5220,5230,5240,5260,5280,5300,5320,5500," \ @@ -345,7 +346,8 @@ nm_supplicant_config_add_setting_wireless (NMSupplicantConfig * self, NMSupplicantConfigPrivate *priv; gboolean is_adhoc, is_ap; const char *mode, *band; - const GByteArray *id; + GBytes *ssid; + const char *bssid; g_return_val_if_fail (NM_IS_SUPPLICANT_CONFIG (self), FALSE); g_return_val_if_fail (setting != NULL, FALSE); @@ -360,8 +362,11 @@ nm_supplicant_config_add_setting_wireless (NMSupplicantConfig * self, else priv->ap_scan = 1; - id = nm_setting_wireless_get_ssid (setting); - if (!nm_supplicant_config_add_option (self, "ssid", (char *) id->data, id->len, FALSE)) { + ssid = nm_setting_wireless_get_ssid (setting); + if (!nm_supplicant_config_add_option (self, "ssid", + (char *) g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid), + FALSE)) { nm_log_warn (LOGD_SUPPLICANT, "Error adding SSID to supplicant config."); return FALSE; } @@ -400,19 +405,14 @@ nm_supplicant_config_add_setting_wireless (NMSupplicantConfig * self, return FALSE; } - id = nm_setting_wireless_get_bssid (setting); - if (id && id->len) { - char *str_bssid; - - str_bssid = g_strdup_printf (MAC_FMT, MAC_ARG (id->data)); + bssid = nm_setting_wireless_get_bssid (setting); + if (bssid) { if (!nm_supplicant_config_add_option (self, "bssid", - str_bssid, strlen (str_bssid), + bssid, strlen (bssid), FALSE)) { - g_free (str_bssid); nm_log_warn (LOGD_SUPPLICANT, "Error adding BSSID to supplicant config."); return FALSE; } - g_free (str_bssid); } band = nm_setting_wireless_get_band (setting); @@ -495,7 +495,7 @@ get_blob_id (const char *name, const char *seed_uid) } #define ADD_BLOB_VAL(field, name, con_uid) \ - if (field && field->len) { \ + if (field && g_bytes_get_size (field)) { \ char *uid = get_blob_id (name, con_uid); \ success = nm_supplicant_config_add_blob (self, name, field, uid); \ g_free (uid); \ @@ -731,7 +731,7 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, const char *peapver, *value, *path; gboolean success, added; GString *phase1, *phase2; - const GByteArray *array; + GBytes *bytes; gboolean fast = FALSE; guint32 i, num_eap; gboolean fast_provisoning_allowed = FALSE; @@ -747,12 +747,12 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, if (!add_string_val (self, value, "password", FALSE, TRUE)) return FALSE; } else { - array = nm_setting_802_1x_get_password_raw (setting); - if (array) { + bytes = nm_setting_802_1x_get_password_raw (setting); + if (bytes) { success = nm_supplicant_config_add_option (self, "password", - (const char *)array->data, - array->len, + (const char *) g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes), TRUE); if (!success) { nm_log_warn (LOGD_SUPPLICANT, "Error adding password-raw to supplicant config."); @@ -893,8 +893,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, /* CA certificate */ switch (nm_setting_802_1x_get_ca_cert_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_ca_cert_blob (setting); - ADD_BLOB_VAL (array, "ca_cert", con_uuid); + bytes = nm_setting_802_1x_get_ca_cert_blob (setting); + ADD_BLOB_VAL (bytes, "ca_cert", con_uuid); break; case NM_SETTING_802_1X_CK_SCHEME_PATH: path = nm_setting_802_1x_get_ca_cert_path (setting); @@ -908,8 +908,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, /* Phase 2 CA certificate */ switch (nm_setting_802_1x_get_phase2_ca_cert_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_phase2_ca_cert_blob (setting); - ADD_BLOB_VAL (array, "ca_cert2", con_uuid); + bytes = nm_setting_802_1x_get_phase2_ca_cert_blob (setting); + ADD_BLOB_VAL (bytes, "ca_cert2", con_uuid); break; case NM_SETTING_802_1X_CK_SCHEME_PATH: path = nm_setting_802_1x_get_phase2_ca_cert_path (setting); @@ -936,8 +936,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, added = FALSE; switch (nm_setting_802_1x_get_private_key_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_private_key_blob (setting); - ADD_BLOB_VAL (array, "private_key", con_uuid); + bytes = nm_setting_802_1x_get_private_key_blob (setting); + ADD_BLOB_VAL (bytes, "private_key", con_uuid); added = TRUE; break; case NM_SETTING_802_1X_CK_SCHEME_PATH: @@ -974,8 +974,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, */ switch (nm_setting_802_1x_get_client_cert_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_client_cert_blob (setting); - ADD_BLOB_VAL (array, "client_cert", con_uuid); + bytes = nm_setting_802_1x_get_client_cert_blob (setting); + ADD_BLOB_VAL (bytes, "client_cert", con_uuid); break; case NM_SETTING_802_1X_CK_SCHEME_PATH: path = nm_setting_802_1x_get_client_cert_path (setting); @@ -992,8 +992,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, added = FALSE; switch (nm_setting_802_1x_get_phase2_private_key_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_phase2_private_key_blob (setting); - ADD_BLOB_VAL (array, "private_key2", con_uuid); + bytes = nm_setting_802_1x_get_phase2_private_key_blob (setting); + ADD_BLOB_VAL (bytes, "private_key2", con_uuid); added = TRUE; break; case NM_SETTING_802_1X_CK_SCHEME_PATH: @@ -1030,8 +1030,8 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, */ switch (nm_setting_802_1x_get_phase2_client_cert_scheme (setting)) { case NM_SETTING_802_1X_CK_SCHEME_BLOB: - array = nm_setting_802_1x_get_phase2_client_cert_blob (setting); - ADD_BLOB_VAL (array, "client_cert2", con_uuid); + bytes = nm_setting_802_1x_get_phase2_client_cert_blob (setting); + ADD_BLOB_VAL (bytes, "client_cert2", con_uuid); break; case NM_SETTING_802_1X_CK_SCHEME_PATH: path = nm_setting_802_1x_get_phase2_client_cert_path (setting); diff --git a/src/supplicant-manager/tests/test-supplicant-config.c b/src/supplicant-manager/tests/test-supplicant-config.c index 73f199cf5b..93de8bb5c8 100644 --- a/src/supplicant-manager/tests/test-supplicant-config.c +++ b/src/supplicant-manager/tests/test-supplicant-config.c @@ -120,10 +120,8 @@ test_wifi_open (void) char *uuid; gboolean success; GError *error = NULL; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x53, 0x49, 0x44 }; - GByteArray *bssid; - const unsigned char bssid_data[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; const char *bssid_str = "11:22:33:44:55:66"; connection = nm_simple_connection_new (); @@ -145,20 +143,16 @@ test_wifi_open (void) s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); - bssid = g_byte_array_sized_new (sizeof (bssid_data)); - g_byte_array_append (bssid, bssid_data, sizeof (bssid_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, - NM_SETTING_WIRELESS_BSSID, bssid, + NM_SETTING_WIRELESS_BSSID, bssid_str, NM_SETTING_WIRELESS_MODE, "infrastructure", NM_SETTING_WIRELESS_BAND, "bg", NULL); - g_byte_array_free (ssid, TRUE); - g_byte_array_free (bssid, TRUE); + g_bytes_unref (ssid); /* IP4 setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -221,10 +215,8 @@ test_wifi_wep_key (const char *detail, char *uuid; gboolean success; GError *error = NULL; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x53, 0x49, 0x44 }; - GByteArray *bssid; - const unsigned char bssid_data[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; const char *bssid_str = "11:22:33:44:55:66"; connection = nm_simple_connection_new (); @@ -246,20 +238,16 @@ test_wifi_wep_key (const char *detail, s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); - bssid = g_byte_array_sized_new (sizeof (bssid_data)); - g_byte_array_append (bssid, bssid_data, sizeof (bssid_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, - NM_SETTING_WIRELESS_BSSID, bssid, + NM_SETTING_WIRELESS_BSSID, bssid_str, NM_SETTING_WIRELESS_MODE, "infrastructure", NM_SETTING_WIRELESS_BAND, "bg", NULL); - g_byte_array_free (ssid, TRUE); - g_byte_array_free (bssid, TRUE); + g_bytes_unref (ssid); /* Wifi Security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); @@ -364,10 +352,8 @@ test_wifi_wpa_psk (const char *detail, char *uuid; gboolean success; GError *error = NULL; - GByteArray *ssid; + GBytes *ssid; const unsigned char ssid_data[] = { 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x53, 0x49, 0x44 }; - GByteArray *bssid; - const unsigned char bssid_data[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }; const char *bssid_str = "11:22:33:44:55:66"; connection = nm_simple_connection_new (); @@ -389,20 +375,16 @@ test_wifi_wpa_psk (const char *detail, s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); nm_connection_add_setting (connection, NM_SETTING (s_wifi)); - ssid = g_byte_array_sized_new (sizeof (ssid_data)); - g_byte_array_append (ssid, ssid_data, sizeof (ssid_data)); - bssid = g_byte_array_sized_new (sizeof (bssid_data)); - g_byte_array_append (bssid, bssid_data, sizeof (bssid_data)); + ssid = g_bytes_new (ssid_data, sizeof (ssid_data)); g_object_set (s_wifi, NM_SETTING_WIRELESS_SSID, ssid, - NM_SETTING_WIRELESS_BSSID, bssid, + NM_SETTING_WIRELESS_BSSID, bssid_str, NM_SETTING_WIRELESS_MODE, "infrastructure", NM_SETTING_WIRELESS_BAND, "bg", NULL); - g_byte_array_free (ssid, TRUE); - g_byte_array_free (bssid, TRUE); + g_bytes_unref (ssid); /* Wifi Security setting */ s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new (); diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 0a969c89f4..c1970b897c 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -482,13 +482,9 @@ test_connection_match_wired (void) NMConnection *orig, *copy, *matched; GSList *connections = NULL; NMSettingWired *s_wired; - GPtrArray *subchan_arr = g_ptr_array_sized_new (3); + char *subchan_arr[] = { "0.0.8000", "0.0.8001", "0.0.8002", NULL }; GByteArray *mac; - g_ptr_array_add (subchan_arr, "0.0.8000"); - g_ptr_array_add (subchan_arr, "0.0.8001"); - g_ptr_array_add (subchan_arr, "0.0.8002"); - orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); connections = g_slist_append (connections, copy); @@ -515,7 +511,6 @@ test_connection_match_wired (void) g_assert (matched == copy); g_slist_free (connections); - g_ptr_array_free (subchan_arr, TRUE); g_object_unref (orig); g_object_unref (copy); }