Add some ipconfig helpers
[framework/connectivity/connman.git] / plugins / supplicant.c
index c9876ab..535f1d3 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2010  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
@@ -177,7 +177,7 @@ struct supplicant_result {
 struct supplicant_task {
        int ifindex;
        char *ifname;
-       gboolean mac80211;
+       gboolean cfg80211;
        struct connman_device *device;
        struct connman_network *network;
        struct connman_network *pending_network;
@@ -313,8 +313,6 @@ static void add_interface_reply(DBusPendingCall *call, void *user_data)
        DBG("task %p", task);
 
        reply = dbus_pending_call_steal_reply(call);
-       if (reply == NULL)
-               return;
 
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
                goto failed;
@@ -340,11 +338,15 @@ static void add_interface_reply(DBusPendingCall *call, void *user_data)
 
        dbus_message_unref(reply);
 
+       dbus_pending_call_unref(call);
+
        return;
 
 failed:
        dbus_message_unref(reply);
 
+       dbus_pending_call_unref(call);
+
        task_list = g_slist_remove(task_list, task);
 
        connman_device_unref(task->device);
@@ -410,8 +412,6 @@ static void get_interface_reply(DBusPendingCall *call, void *user_data)
        DBG("task %p", task);
 
        reply = dbus_pending_call_steal_reply(call);
-       if (reply == NULL)
-               return;
 
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
                add_interface(task);
@@ -439,6 +439,8 @@ static void get_interface_reply(DBusPendingCall *call, void *user_data)
 
 done:
        dbus_message_unref(reply);
+
+       dbus_pending_call_unref(call);
 }
 
 static int create_interface(struct supplicant_task *task)
@@ -496,6 +498,8 @@ static void remove_interface_reply(DBusPendingCall *call, void *user_data)
        free_task(task);
 
        dbus_message_unref(reply);
+
+       dbus_pending_call_unref(call);
 }
 
 static int remove_interface(struct supplicant_task *task)
@@ -984,7 +988,7 @@ static int set_network(struct supplicant_task *task,
                const char *auth_alg = "OPEN";
                const char *key_index = "0";
 
-               if (task->mac80211 == TRUE)
+               if (task->cfg80211 == TRUE)
                        auth_alg = "OPEN SHARED";
 
                connman_dbus_dict_append_basic(&dict, "auth_alg",
@@ -1062,8 +1066,6 @@ static void scan_reply(DBusPendingCall *call, void *user_data)
        task->scan_call = NULL;
 
        reply = dbus_pending_call_steal_reply(call);
-       if (reply == NULL)
-               return;
 
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
                connman_device_set_scanning(task->device, FALSE);
@@ -1075,6 +1077,8 @@ static void scan_reply(DBusPendingCall *call, void *user_data)
 
 done:
        dbus_message_unref(reply);
+
+       dbus_pending_call_unref(call);
 }
 
 
@@ -1427,16 +1431,9 @@ static void properties_reply(DBusPendingCall *call, void *user_data)
        DBG("task %p", task);
 
        reply = dbus_pending_call_steal_reply(call);
-       if (reply == NULL) {
-               get_properties(task);
-               return;
-       }
 
-       if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
-               dbus_message_unref(reply);
-               get_properties(task);
-               return;
-       }
+       if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
+               goto unref;
 
        memset(&result, 0, sizeof(result));
        result.frequency = -1;
@@ -1600,8 +1597,11 @@ done:
        g_free(result.name);
        g_free(result.ssid);
 
+unref:
        dbus_message_unref(reply);
 
+       dbus_pending_call_unref(call);
+
        get_properties(task);
 }
 
@@ -1666,8 +1666,6 @@ static void scan_results_reply(DBusPendingCall *call, void *user_data)
        DBG("task %p", task);
 
        reply = dbus_pending_call_steal_reply(call);
-       if (reply == NULL)
-               goto noscan;
 
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
                goto done;
@@ -1701,6 +1699,8 @@ static void scan_results_reply(DBusPendingCall *call, void *user_data)
 
        dbus_message_unref(reply);
 
+       dbus_pending_call_unref(call);
+
        get_properties(task);
 
        return;
@@ -1708,7 +1708,8 @@ static void scan_results_reply(DBusPendingCall *call, void *user_data)
 done:
        dbus_message_unref(reply);
 
-noscan:
+       dbus_pending_call_unref(call);
+
        task->result_call = NULL;
 
        if (task->scanning == TRUE) {
@@ -1794,10 +1795,11 @@ static int task_connect(struct supplicant_task *task)
 
        DBG("address %s security %s", address, security);
 
-       if (security == NULL && passphrase == NULL)
+       if (security == NULL)
                return -EINVAL;
 
-       if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
+       if (passphrase == NULL && g_str_equal(security, "none") == FALSE &&
+                               g_str_equal(security, "ieee8021x") == FALSE)
                return -EINVAL;
 
        remove_network(task);
@@ -1953,27 +1955,23 @@ badstate:
                                                        oldstate, newstate);
 }
 
-static DBusHandlerResult supplicant_filter(DBusConnection *conn,
+static gboolean supplicant_filter(DBusConnection *conn,
                                                DBusMessage *msg, void *data)
 {
        struct supplicant_task *task;
        const char *member, *path;
 
-       if (dbus_message_has_interface(msg,
-                               SUPPLICANT_INTF ".Interface") == FALSE)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
        member = dbus_message_get_member(msg);
        if (member == NULL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               return TRUE;
 
        path = dbus_message_get_path(msg);
        if (path == NULL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               return TRUE;
 
        task = find_task_by_path(path);
        if (task == NULL)
-               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+               return TRUE;
 
        DBG("task %p member %s", task, member);
 
@@ -1984,7 +1982,7 @@ static DBusHandlerResult supplicant_filter(DBusConnection *conn,
        else if (g_str_equal(member, "StateChange") == TRUE)
                state_change(task, msg);
 
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       return TRUE;
 }
 
 int supplicant_start(struct connman_device *device)
@@ -2006,8 +2004,8 @@ int supplicant_start(struct connman_device *device)
                goto failed;
        }
 
-       task->mac80211 = connman_inet_is_mac80211(task->ifindex);
-       if (task->mac80211 == FALSE)
+       task->cfg80211 = connman_inet_is_cfg80211(task->ifindex);
+       if (task->cfg80211 == FALSE)
                connman_warn("Enabling quirks for unsupported driver");
 
        task->range = g_try_malloc0(sizeof(struct iw_range));
@@ -2215,9 +2213,8 @@ static void supplicant_remove(DBusConnection *conn, void *user_data)
        }
 }
 
-static const char *supplicant_rule = "type=signal,"
-                               "interface=" SUPPLICANT_INTF ".Interface";
 static guint watch;
+static guint iface_watch;
 
 static int supplicant_create(void)
 {
@@ -2230,19 +2227,20 @@ static int supplicant_create(void)
 
        DBG("connection %p", connection);
 
-       if (dbus_connection_add_filter(connection,
-                               supplicant_filter, NULL, NULL) == FALSE) {
-               dbus_connection_unref(connection);
-               connection = NULL;
-               return -EIO;
-       }
-
-       dbus_bus_add_match(connection, supplicant_rule, NULL);
-       dbus_connection_flush(connection);
-
        watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
                        supplicant_probe, supplicant_remove, NULL, NULL);
 
+       iface_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+                                               SUPPLICANT_INTF ".Interface",
+                                               NULL, supplicant_filter,
+                                               NULL, NULL);
+
+       if (watch == 0 || iface_watch == 0) {
+               g_dbus_remove_watch(connection, watch);
+               g_dbus_remove_watch(connection, iface_watch);
+               return -EIO;
+       }
+
        return 0;
 }
 
@@ -2253,13 +2251,8 @@ static void supplicant_destroy(void)
 
        DBG("connection %p", connection);
 
-       if (watch > 0)
-               g_dbus_remove_watch(connection, watch);
-
-       dbus_bus_remove_match(connection, supplicant_rule, NULL);
-       dbus_connection_flush(connection);
-
-       dbus_connection_remove_filter(connection, supplicant_filter, NULL);
+       g_dbus_remove_watch(connection, watch);
+       g_dbus_remove_watch(connection, iface_watch);
 
        dbus_connection_unref(connection);
        connection = NULL;