2007-09-12 16:23:53 +00:00
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* Ray Strode <rstrode@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
*
|
|
|
|
|
* (C) Copyright 2005 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <iwlib.h>
|
|
|
|
|
#include <wireless.h>
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
#include "nm-utils.h"
|
|
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
/* Shamelessly ripped from the Linux kernel ieee80211 stack */
|
|
|
|
|
gboolean
|
|
|
|
|
nm_utils_is_empty_ssid (const char * ssid, int len)
|
2007-09-12 16:23:53 +00:00
|
|
|
{
|
2007-10-09 08:57:35 +00:00
|
|
|
/* Single white space is for Linksys APs */
|
|
|
|
|
if (len == 1 && ssid[0] == ' ')
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
/* Otherwise, if the entire ssid is 0, we assume it is hidden */
|
|
|
|
|
while (len--) {
|
|
|
|
|
if (ssid[len] != '\0')
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
const char *
|
|
|
|
|
nm_utils_escape_ssid (const guint8 * ssid, guint32 len)
|
2007-09-12 16:23:53 +00:00
|
|
|
{
|
2007-10-09 08:57:35 +00:00
|
|
|
static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
|
|
|
|
|
const guint8 *s = ssid;
|
|
|
|
|
char *d = escaped;
|
2007-09-12 16:23:53 +00:00
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
if (nm_utils_is_empty_ssid ((const char *) ssid, len)) {
|
|
|
|
|
memcpy (escaped, "<hidden>", sizeof ("<hidden>"));
|
|
|
|
|
return escaped;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
len = MIN (len, (guint32) IW_ESSID_MAX_SIZE);
|
|
|
|
|
while (len--) {
|
|
|
|
|
if (*s == '\0') {
|
|
|
|
|
*d++ = '\\';
|
|
|
|
|
*d++ = '0';
|
|
|
|
|
s++;
|
|
|
|
|
} else {
|
|
|
|
|
*d++ = *s++;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-10-09 08:57:35 +00:00
|
|
|
*d = '\0';
|
|
|
|
|
return escaped;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
gboolean
|
|
|
|
|
nm_utils_same_ssid (const GByteArray * ssid1,
|
|
|
|
|
const GByteArray * ssid2,
|
|
|
|
|
gboolean ignore_trailing_null)
|
2007-09-12 16:23:53 +00:00
|
|
|
{
|
2007-10-09 08:57:35 +00:00
|
|
|
guint32 ssid1_len, ssid2_len;
|
|
|
|
|
|
|
|
|
|
if (ssid1 == ssid2)
|
|
|
|
|
return TRUE;
|
|
|
|
|
if ((ssid1 && !ssid2) || (!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--;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
if (ssid1_len != ssid2_len)
|
|
|
|
|
return FALSE;
|
2007-09-12 16:23:53 +00:00
|
|
|
|
2007-10-09 08:57:35 +00:00
|
|
|
return memcmp (ssid1->data, ssid2->data, ssid1_len) == 0 ? TRUE : FALSE;
|
2007-09-12 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
value_destroy (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GValue *value = (GValue *) data;
|
|
|
|
|
|
|
|
|
|
g_value_unset (value);
|
|
|
|
|
g_slice_free (GValue, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
value_dup (gpointer key, gpointer val, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GHashTable *dup = (GHashTable *) user_data;
|
|
|
|
|
GValue *value = (GValue *) val;
|
|
|
|
|
GValue *dup_value;
|
|
|
|
|
|
|
|
|
|
dup_value = g_slice_new0 (GValue);
|
|
|
|
|
g_value_init (dup_value, G_VALUE_TYPE (val));
|
|
|
|
|
g_value_copy (value, dup_value);
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (dup, g_strdup ((char *) key), dup_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GHashTable *
|
|
|
|
|
nm_utils_gvalue_hash_dup (GHashTable *hash)
|
|
|
|
|
{
|
|
|
|
|
GHashTable *dup;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (hash != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
dup = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
|
(GDestroyNotify) g_free,
|
|
|
|
|
value_destroy);
|
|
|
|
|
|
|
|
|
|
g_hash_table_foreach (hash, value_dup, dup);
|
|
|
|
|
|
|
|
|
|
return dup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
nm_utils_garray_to_string (GArray *array)
|
|
|
|
|
{
|
|
|
|
|
GString *str;
|
|
|
|
|
int i;
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
str = g_string_sized_new (array->len);
|
|
|
|
|
for (i = 0; i < array->len; i++) {
|
|
|
|
|
c = array->data[i];
|
|
|
|
|
|
|
|
|
|
/* Convert NULLs to spaces to increase the readability. */
|
|
|
|
|
if (c == '\0')
|
|
|
|
|
c = ' ';
|
|
|
|
|
str = g_string_append_c (str, c);
|
|
|
|
|
}
|
|
|
|
|
str = g_string_append_c (str, '\0');
|
|
|
|
|
|
|
|
|
|
return g_string_free (str, FALSE);
|
|
|
|
|
}
|