Update copyright information
[connman] / plugins / supplicant.c
index b0d74c6..0d7d293 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
 #include <config.h>
 #endif
 
+#include <stdlib.h>
 #include <string.h>
 #include <dbus/dbus.h>
 
+#include <glib.h>
+
+#define CONNMAN_API_SUBJECT_TO_CHANGE
 #include <connman/log.h>
+#include <connman/dbus.h>
 
 #include "inet.h"
 #include "supplicant.h"
 #define IEEE80211_CAP_PRIVACY   0x0010
 
 struct supplicant_task {
-       DBusConnection *conn;
        int ifindex;
        gchar *ifname;
-       struct connman_element *element;
+       struct connman_device *device;
        struct supplicant_callback *callback;
        gchar *path;
        gboolean created;
@@ -49,9 +53,10 @@ struct supplicant_task {
        enum supplicant_state state;
 };
 
-static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
 static GSList *task_list = NULL;
 
+static DBusConnection *connection;
+
 static struct supplicant_task *find_task_by_index(int index)
 {
        GSList *list;
@@ -66,6 +71,20 @@ static struct supplicant_task *find_task_by_index(int index)
        return NULL;
 }
 
+static struct supplicant_task *find_task_by_path(const char *path)
+{
+       GSList *list;
+
+       for (list = task_list; list; list = list->next) {
+               struct supplicant_task *task = list->data;
+
+               if (g_str_equal(task->path, path) == TRUE)
+                       return task;
+       }
+
+       return NULL;
+}
+
 static int get_interface(struct supplicant_task *task)
 {
        DBusMessage *message, *reply;
@@ -84,7 +103,7 @@ static int get_interface(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -139,7 +158,7 @@ static int add_interface(struct supplicant_task *task)
        dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
                                                        DBUS_TYPE_INVALID);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -196,7 +215,7 @@ static int remove_interface(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -233,7 +252,7 @@ static int set_ap_scan(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -270,7 +289,7 @@ static int add_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -326,7 +345,7 @@ static int remove_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -368,7 +387,7 @@ static int select_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -404,7 +423,7 @@ static int enable_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -440,7 +459,7 @@ static int disable_network(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -459,39 +478,9 @@ static int disable_network(struct supplicant_task *task)
        return 0;
 }
 
-static void append_entry(DBusMessageIter *dict,
-                               const char *key, int type, void *val)
-{
-       DBusMessageIter entry, value;
-       const char *signature;
-
-       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
-                                                               NULL, &entry);
-
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
-
-       switch (type) {
-       case DBUS_TYPE_STRING:
-               signature = DBUS_TYPE_STRING_AS_STRING;
-               break;
-       case DBUS_TYPE_UINT16:
-               signature = DBUS_TYPE_UINT16_AS_STRING;
-               break;
-       default:
-               signature = DBUS_TYPE_VARIANT_AS_STRING;
-               break;
-       }
-
-       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
-                                                       signature, &value);
-       dbus_message_iter_append_basic(&value, type, val);
-       dbus_message_iter_close_container(&entry, &value);
-
-       dbus_message_iter_close_container(dict, &entry);
-}
-
-static int set_network(struct supplicant_task *task, const char *network,
-                                               const char *passphrase)
+static int set_network(struct supplicant_task *task,
+                               const unsigned char *network, int len,
+                               const char *security, const char *passphrase)
 {
        DBusMessage *message, *reply;
        DBusMessageIter array, dict;
@@ -514,22 +503,59 @@ static int set_network(struct supplicant_task *task, const char *network,
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
-       append_entry(&dict, "ssid", DBUS_TYPE_STRING, &network);
+       connman_dbus_dict_append_array(&dict, "ssid",
+                                       DBUS_TYPE_BYTE, &network, len);
 
-       if (passphrase && strlen(passphrase) > 0) {
+       if (g_ascii_strcasecmp(security, "wpa") == 0 ||
+                               g_ascii_strcasecmp(security, "wpa2") == 0) {
                const char *key_mgmt = "WPA-PSK";
-               append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
-               append_entry(&dict, "psk", DBUS_TYPE_STRING, &passphrase);
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
+
+               if (passphrase && strlen(passphrase) > 0)
+                       connman_dbus_dict_append_variant(&dict, "psk",
+                                               DBUS_TYPE_STRING, &passphrase);
+       } else if (g_ascii_strcasecmp(security, "wep") == 0) {
+               const char *key_mgmt = "NONE", *index = "0";
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
+
+               if (passphrase) {
+                       int size = strlen(passphrase);
+                       if (size == 10 || size == 26) {
+                               unsigned char *key = malloc(13);
+                               char tmp[3];
+                               int i;
+                               memset(tmp, 0, sizeof(tmp));
+                               if (key == NULL)
+                                       size = 0;
+                               for (i = 0; i < size / 2; i++) {
+                                       memcpy(tmp, passphrase + (i * 2), 2);
+                                       key[i] = (unsigned char) strtol(tmp,
+                                                               NULL, 16);
+                               }
+                               connman_dbus_dict_append_array(&dict,
+                                               "wep_key0", DBUS_TYPE_BYTE,
+                                                       &key, size / 2);
+                               free(key);
+                       } else
+                               connman_dbus_dict_append_variant(&dict,
+                                               "wep_key0", DBUS_TYPE_STRING,
+                                                               &passphrase);
+                       connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
+                                               DBUS_TYPE_STRING, &index);
+               }
        } else {
                const char *key_mgmt = "NONE";
-               append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
+               connman_dbus_dict_append_variant(&dict, "key_mgmt",
+                                               DBUS_TYPE_STRING, &key_mgmt);
        }
 
        dbus_message_iter_close_container(&array, &dict);
 
        dbus_error_init(&error);
 
-       reply = dbus_connection_send_with_reply_and_block(task->conn,
+       reply = dbus_connection_send_with_reply_and_block(connection,
                                                        message, -1, &error);
        if (reply == NULL) {
                if (dbus_error_is_set(&error) == TRUE) {
@@ -560,7 +586,7 @@ static int initiate_scan(struct supplicant_task *task)
        if (message == NULL)
                return -ENOMEM;
 
-       if (dbus_connection_send_with_reply(task->conn, message,
+       if (dbus_connection_send_with_reply(connection, message,
                                                &call, TIMEOUT) == FALSE) {
                connman_error("Failed to initiate scan");
                dbus_message_unref(message);
@@ -582,7 +608,21 @@ static void extract_ssid(struct supplicant_network *network,
        dbus_message_iter_recurse(value, &array);
        dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
 
-       network->identifier = g_strdup((char *) ssid);
+       if (ssid_len < 1)
+               return;
+
+       network->ssid = g_try_malloc(ssid_len);
+       if (network->ssid == NULL)
+               return;
+
+       memcpy(network->ssid, ssid, ssid_len);
+       network->ssid_len = ssid_len;
+
+       network->identifier = g_try_malloc0(ssid_len + 1);
+       if (network->identifier == NULL)
+               return;
+
+       memcpy(network->identifier, ssid, ssid_len);
 }
 
 static void extract_wpaie(struct supplicant_network *network,
@@ -616,13 +656,14 @@ static void extract_rsnie(struct supplicant_network *network,
 static void extract_capabilites(struct supplicant_network *network,
                                                DBusMessageIter *value)
 {
-       guint capabilities;
+       dbus_message_iter_get_basic(value, &network->capabilities);
 
-       dbus_message_iter_get_basic(value, &capabilities);
+       if (network->capabilities & IEEE80211_CAP_ESS)
+               network->adhoc = FALSE;
+       else if (network->capabilities & IEEE80211_CAP_IBSS)
+               network->adhoc = TRUE;
 
-       network->capabilities = capabilities;
-
-       if (capabilities & IEEE80211_CAP_PRIVACY)
+       if (network->capabilities & IEEE80211_CAP_PRIVACY)
                network->has_wep = TRUE;
 }
 
@@ -659,6 +700,19 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
                //type = dbus_message_iter_get_arg_type(&value);
                //dbus_message_iter_get_basic(&value, &val);
 
+               /* 
+                * bssid        : a (97)
+                * ssid         : a (97)
+                * wpaie        : a (97)
+                * rsnie        : a (97)
+                * frequency    : i (105)
+                * capabilities : q (113)
+                * quality      : i (105)
+                * noise        : i (105)
+                * level        : i (105)
+                * maxrate      : i (105)
+                */
+
                if (g_str_equal(key, "ssid") == TRUE)
                        extract_ssid(network, &value);
                else if (g_str_equal(key, "wpaie") == TRUE)
@@ -667,13 +721,24 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
                        extract_rsnie(network, &value);
                else if (g_str_equal(key, "capabilities") == TRUE)
                        extract_capabilites(network, &value);
+               else if (g_str_equal(key, "quality") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->quality);
+               else if (g_str_equal(key, "noise") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->noise);
+               else if (g_str_equal(key, "level") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->level);
+               else if (g_str_equal(key, "maxrate") == TRUE)
+                       dbus_message_iter_get_basic(&value, &network->maxrate);
+
 
                dbus_message_iter_next(&dict);
        }
 
        if (task->callback && task->callback->scan_result)
-               task->callback->scan_result(task->element, network);
+               task->callback->scan_result(task->device, network);
 
+       g_free(network->identifier);
+       g_free(network->ssid);
        g_free(network);
 
 done:
@@ -692,7 +757,7 @@ static int get_network_properties(struct supplicant_task *task,
        if (message == NULL)
                return -ENOMEM;
 
-       if (dbus_connection_send_with_reply(task->conn, message,
+       if (dbus_connection_send_with_reply(connection, message,
                                                &call, TIMEOUT) == FALSE) {
                connman_error("Failed to get network properties");
                dbus_message_unref(message);
@@ -732,6 +797,9 @@ static void scan_results_reply(DBusPendingCall *call, void *user_data)
                goto done;
        }
 
+       if (task->callback && task->callback->clear_results)
+                       task->callback->clear_results(task->device);
+
        for (i = 0; i < num_results; i++)
                get_network_properties(task, results[i]);
 
@@ -754,7 +822,7 @@ static int scan_results_available(struct supplicant_task *task)
        if (message == NULL)
                return -ENOMEM;
 
-       if (dbus_connection_send_with_reply(task->conn, message,
+       if (dbus_connection_send_with_reply(connection, message,
                                                &call, TIMEOUT) == FALSE) {
                connman_error("Failed to request scan result");
                dbus_message_unref(message);
@@ -806,7 +874,7 @@ static void state_change(struct supplicant_task *task, DBusMessage *msg)
                task->state = STATE_DISCONNECTED;
 
        if (task->callback && task->callback->state_change)
-               task->callback->state_change(task->element, task->state);
+               task->callback->state_change(task->device, task->state);
 
        switch (task->state) {
        case STATE_COMPLETED:
@@ -823,8 +891,8 @@ static void state_change(struct supplicant_task *task, DBusMessage *msg)
 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
                                                DBusMessage *msg, void *data)
 {
-       struct supplicant_task *task = data;
-       const char *member;
+       struct supplicant_task *task;
+       const char *member, *path;
 
        if (dbus_message_has_interface(msg,
                                SUPPLICANT_INTF ".Interface") == FALSE)
@@ -834,6 +902,14 @@ static DBusHandlerResult supplicant_filter(DBusConnection *conn,
        if (member == NULL)
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
+       path = dbus_message_get_path(msg);
+       if (path == NULL)
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
+       task = find_task_by_path(path);
+       if (task == NULL)
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+
        DBG("task %p member %s", task, member);
 
        if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
@@ -849,10 +925,6 @@ static int add_filter(struct supplicant_task *task)
        DBusError error;
        gchar *filter;
 
-       if (dbus_connection_add_filter(task->conn,
-                               supplicant_filter, task, NULL) == FALSE)
-               return -EIO;
-
        filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
                                                SUPPLICANT_INTF, task->path);
 
@@ -860,7 +932,7 @@ static int add_filter(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       dbus_bus_add_match(task->conn, filter, &error);
+       dbus_bus_add_match(connection, filter, &error);
 
        g_free(filter);
 
@@ -884,7 +956,7 @@ static int remove_filter(struct supplicant_task *task)
 
        dbus_error_init(&error);
 
-       dbus_bus_add_match(task->conn, filter, &error);
+       dbus_bus_remove_match(connection, filter, &error);
 
        g_free(filter);
 
@@ -893,26 +965,24 @@ static int remove_filter(struct supplicant_task *task)
                dbus_error_free(&error);
        }
 
-       dbus_connection_remove_filter(task->conn, supplicant_filter, task);
-
        return 0;
 }
 
-int __supplicant_start(struct connman_element *element,
+int __supplicant_start(struct connman_device *device,
                                        struct supplicant_callback *callback)
 {
        struct supplicant_task *task;
        int err;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
        task = g_try_new0(struct supplicant_task, 1);
        if (task == NULL)
                return -ENOMEM;
 
-       task->ifindex = element->index;
-       task->ifname = inet_index2name(element->index);
-       task->element = element;
+       task->ifindex = connman_device_get_index(device);
+       task->ifname = inet_index2name(task->ifindex);
+       task->device = device;
        task->callback = callback;
 
        if (task->ifname == NULL) {
@@ -920,18 +990,10 @@ int __supplicant_start(struct connman_element *element,
                return -ENOMEM;
        }
 
-       task->conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
-       if (task->conn == NULL) {
-               g_free(task);
-               return -EIO;
-       }
-
        task->created = FALSE;
        task->state = STATE_INACTIVE;
 
-       g_static_mutex_lock(&task_mutex);
        task_list = g_slist_append(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
 
        err = get_interface(task);
        if (err < 0) {
@@ -949,19 +1011,18 @@ int __supplicant_start(struct connman_element *element,
        return 0;
 }
 
-int __supplicant_stop(struct connman_element *element)
+int __supplicant_stop(struct connman_device *device)
 {
+       int index = connman_device_get_index(device);
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
-       task = find_task_by_index(element->index);
+       task = find_task_by_index(index);
        if (task == NULL)
                return -ENODEV;
 
-       g_static_mutex_lock(&task_mutex);
        task_list = g_slist_remove(task_list, task);
-       g_static_mutex_unlock(&task_mutex);
 
        disable_network(task);
 
@@ -971,8 +1032,6 @@ int __supplicant_stop(struct connman_element *element)
 
        remove_interface(task);
 
-       dbus_connection_unref(task->conn);
-
        g_free(task->ifname);
        g_free(task->path);
        g_free(task);
@@ -980,14 +1039,15 @@ int __supplicant_stop(struct connman_element *element)
        return 0;
 }
 
-int __supplicant_scan(struct connman_element *element)
+int __supplicant_scan(struct connman_device *device)
 {
+       int index = connman_device_get_index(device);
        struct supplicant_task *task;
        int err;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("device %p", device);
 
-       task = find_task_by_index(element->index);
+       task = find_task_by_index(index);
        if (task == NULL)
                return -ENODEV;
 
@@ -1008,11 +1068,13 @@ int __supplicant_scan(struct connman_element *element)
        return 0;
 }
 
-int __supplicant_connect(struct connman_element *element, const char *ssid)
+int __supplicant_connect(struct connman_element *element,
+                               const unsigned char *ssid, int ssid_len,
+                               const char *security, const char *passphrase)
 {
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("element %p", element);
 
        task = find_task_by_index(element->index);
        if (task == NULL)
@@ -1023,7 +1085,7 @@ int __supplicant_connect(struct connman_element *element, const char *ssid)
        select_network(task);
        disable_network(task);
 
-       set_network(task, ssid, NULL);
+       set_network(task, ssid, ssid_len, security, passphrase);
 
        enable_network(task);
 
@@ -1034,7 +1096,7 @@ int __supplicant_disconnect(struct connman_element *element)
 {
        struct supplicant_task *task;
 
-       DBG("element %p name %s", element, element->name);
+       DBG("element %p", element);
 
        task = find_task_by_index(element->index);
        if (task == NULL)
@@ -1046,3 +1108,43 @@ int __supplicant_disconnect(struct connman_element *element)
 
        return 0;
 }
+
+void __supplicant_activate(DBusConnection *conn)
+{
+       DBusMessage *message;
+
+       DBG("conn %p", conn);
+
+       message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
+                               DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
+       if (message == NULL)
+               return;
+
+       dbus_message_set_no_reply(message, TRUE);
+
+       dbus_connection_send(conn, message, NULL);
+
+       dbus_message_unref(message);
+}
+
+int __supplicant_init(DBusConnection *conn)
+{
+       DBG("conn %p", conn);
+
+       connection = conn;
+
+       if (dbus_connection_add_filter(connection,
+                               supplicant_filter, NULL, NULL) == FALSE) {
+               dbus_connection_unref(connection);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+void __supplicant_exit(void)
+{
+       DBG("conn %p", connection);
+
+       dbus_connection_remove_filter(connection, supplicant_filter, NULL);
+}