From 044f4199fa31ff8ec4f9c91c051e05fc3cf14d0f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 13 Mar 2008 23:27:05 +0100 Subject: [PATCH] Use list of known networks to re-connect to --- src/connman.h | 2 ++ src/iface-storage.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/iface.c | 11 +++++++-- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/connman.h b/src/connman.h index c253a8e..f9416d2 100644 --- a/src/connman.h +++ b/src/connman.h @@ -70,6 +70,8 @@ int __connman_iface_init_via_inet(struct connman_iface *iface); int __connman_iface_up(struct connman_iface *iface); int __connman_iface_down(struct connman_iface *iface); +char *__connman_iface_find_passphrase(struct connman_iface *iface, + const char *network); int __connman_iface_load(struct connman_iface *iface); int __connman_iface_store(struct connman_iface *iface); diff --git a/src/iface-storage.c b/src/iface-storage.c index a5b57a3..7872f7c 100644 --- a/src/iface-storage.c +++ b/src/iface-storage.c @@ -32,10 +32,62 @@ #define GROUP_CONFIG "Config" +char *__connman_iface_find_passphrase(struct connman_iface *iface, + const char *network) +{ + GKeyFile *keyfile; + gchar *pathname, *result = NULL; + gchar **list; + gsize list_len; + int i; + + DBG("iface %p", iface); + + if (iface->identifier == NULL) + return NULL; + + pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR, + iface->identifier); + if (pathname == NULL) + return NULL; + + keyfile = g_key_file_new(); + + g_key_file_set_list_separator(keyfile, ','); + + if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE) + goto done; + + if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE) + goto done; + + list = g_key_file_get_string_list(keyfile, GROUP_CONFIG, + "KnownNetworks", &list_len, NULL); + for (i = 0; i < list_len; i++) + if (g_str_equal(list[i], network) == TRUE) { + result = g_key_file_get_string(keyfile, network, + "PSK", NULL); + if (result == NULL) + result = g_strdup(""); + break; + } + + g_strfreev(list); + +done: + g_key_file_free(keyfile); + + g_free(pathname); + + return result; +} + int __connman_iface_load(struct connman_iface *iface) { GKeyFile *keyfile; gchar *pathname, *str; + gchar **list; + gsize list_len; DBG("iface %p", iface); @@ -49,6 +101,8 @@ int __connman_iface_load(struct connman_iface *iface) keyfile = g_key_file_new(); + g_key_file_set_list_separator(keyfile, ','); + if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE) goto done; @@ -61,7 +115,13 @@ int __connman_iface_load(struct connman_iface *iface) g_free(str); } - str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Network", NULL); + list = g_key_file_get_string_list(keyfile, GROUP_CONFIG, + "KnownNetworks", &list_len, NULL); + + g_strfreev(list); + + str = g_key_file_get_string(keyfile, GROUP_CONFIG, + "LastNetwork", NULL); if (str != NULL) { g_free(iface->network.identifier); iface->network.identifier = str; @@ -93,9 +153,10 @@ static void do_update(GKeyFile *keyfile, struct connman_iface *iface) if (iface->network.identifier != NULL) { g_key_file_set_string(keyfile, GROUP_CONFIG, - "Network", iface->network.identifier); + "LastNetwork", iface->network.identifier); } else - g_key_file_remove_key(keyfile, GROUP_CONFIG, "Network", NULL); + g_key_file_remove_key(keyfile, GROUP_CONFIG, + "LastNetwork", NULL); if (iface->network.identifier != NULL) g_key_file_set_string(keyfile, iface->network.identifier, diff --git a/src/iface.c b/src/iface.c index b9fe513..a35d22a 100644 --- a/src/iface.c +++ b/src/iface.c @@ -360,6 +360,7 @@ void connman_iface_indicate_station(struct connman_iface *iface, const char *name, int strength, int security) { DBusMessage *signal; + char *passphrase; DBG("iface %p security %d name %s", iface, security, name); @@ -376,9 +377,15 @@ void connman_iface_indicate_station(struct connman_iface *iface, dbus_connection_send(connection, signal, NULL); dbus_message_unref(signal); - if (g_str_equal(name, iface->network.identifier) == TRUE && - iface->state == CONNMAN_IFACE_STATE_SCANNING) { + if (iface->state != CONNMAN_IFACE_STATE_SCANNING) + return; + + passphrase = __connman_iface_find_passphrase(iface, name); + if (passphrase != NULL) { + g_free(iface->network.identifier); iface->network.identifier = g_strdup(name); + g_free(iface->network.passphrase); + iface->network.passphrase = passphrase; if (iface->driver->connect) { iface->driver->connect(iface, &iface->network); -- 2.7.4