wifi: Add wifi pointer NULL checks
[framework/connectivity/connman.git] / plugins / wifi.c
index 7cd5d81..06604d5 100644 (file)
@@ -108,6 +108,8 @@ struct wifi_data {
 
 static GList *iface_list = NULL;
 
+static void start_autoscan(struct connman_device *device);
+
 static void handle_tethering(struct wifi_data *wifi)
 {
        if (wifi->tethering == FALSE)
@@ -132,6 +134,9 @@ static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
        struct connman_device *device = user_data;
        struct wifi_data *wifi = connman_device_get_data(device);
 
+       if (wifi == NULL)
+               return;
+
        DBG("index %d flags %d change %d", wifi->index, flags, change);
 
        if (!change)
@@ -400,6 +405,9 @@ static int throw_wifi_scan(struct connman_device *device,
        struct wifi_data *wifi = connman_device_get_data(device);
        int ret;
 
+       if (wifi == NULL)
+               return -ENODEV;
+
        DBG("device %p %p", device, wifi->interface);
 
        if (wifi->tethering == TRUE)
@@ -420,25 +428,49 @@ static int throw_wifi_scan(struct connman_device *device,
        return ret;
 }
 
-static void hidden_scan_callback(int result,
-                       GSupplicantInterface *interface, void *user_data)
+static void hidden_free(struct hidden_params *hidden)
+{
+       if (hidden == NULL)
+               return;
+
+       g_free(hidden->identity);
+       g_free(hidden->passphrase);
+       g_free(hidden);
+}
+
+static void scan_callback(int result, GSupplicantInterface *interface,
+                                               void *user_data)
 {
        struct connman_device *device = user_data;
+       struct wifi_data *wifi = connman_device_get_data(device);
 
-       DBG("result %d", result);
+       DBG("result %d wifi %p", result, wifi);
+
+       if (wifi != NULL && wifi->hidden != NULL) {
+               connman_network_clear_hidden(wifi->hidden->user_data);
+               hidden_free(wifi->hidden);
+               wifi->hidden = NULL;
+       }
+
+       if (result < 0)
+               connman_device_reset_scanning(device);
 
        connman_device_set_scanning(device, FALSE);
+       start_autoscan(device);
        connman_device_unref(device);
 }
 
-static void autoscan_scan_callback(int result,
+static void scan_callback_hidden(int result,
                        GSupplicantInterface *interface, void *user_data)
 {
        struct connman_device *device = user_data;
        struct wifi_data *wifi = connman_device_get_data(device);
        int driver_max_ssids;
 
-       DBG("result %d", result);
+       DBG("result %d wifi %p", result, wifi);
+
+       if (wifi == NULL)
+               goto out;
 
        /*
         * Scan hidden networks so that we can autoconnect to them.
@@ -459,7 +491,7 @@ static void autoscan_scan_callback(int result,
                                                scan_params) > 0) {
                        ret = g_supplicant_interface_scan(wifi->interface,
                                                        scan_params,
-                                                       hidden_scan_callback,
+                                                       scan_callback,
                                                        device);
                        if (ret == 0)
                                return;
@@ -469,8 +501,7 @@ static void autoscan_scan_callback(int result,
        }
 
 out:
-       connman_device_set_scanning(device, FALSE);
-       connman_device_unref(device);
+       scan_callback(result, interface, user_data);
 }
 
 static gboolean autoscan_timeout(gpointer data)
@@ -480,6 +511,9 @@ static gboolean autoscan_timeout(gpointer data)
        struct autoscan_params *autoscan;
        int interval;
 
+       if (wifi == NULL)
+               return FALSE;
+
        autoscan = wifi->autoscan;
 
        if (autoscan->interval <= 0) {
@@ -491,7 +525,7 @@ static gboolean autoscan_timeout(gpointer data)
        if (autoscan->interval >= autoscan->limit)
                interval = autoscan->limit;
 
-       throw_wifi_scan(wifi->device, autoscan_scan_callback);
+       throw_wifi_scan(wifi->device, scan_callback_hidden);
 
 set_interval:
        DBG("interval %d", interval);
@@ -614,6 +648,9 @@ static int wifi_enable(struct connman_device *device)
 
        DBG("device %p %p", device, wifi);
 
+       if (wifi == NULL)
+               return -ENODEV;
+
        ret = g_supplicant_interface_create(interface, driver, NULL,
                                                interface_create_callback,
                                                        wifi);
@@ -628,7 +665,10 @@ static int wifi_disable(struct connman_device *device)
        struct wifi_data *wifi = connman_device_get_data(device);
        int ret;
 
-       DBG("device %p", device);
+       DBG("device %p wifi %p", device, wifi);
+
+       if (wifi == NULL)
+               return -ENODEV;
 
        wifi->connected = FALSE;
        wifi->disconnecting = FALSE;
@@ -653,39 +693,6 @@ static int wifi_disable(struct connman_device *device)
        return -EINPROGRESS;
 }
 
-static void hidden_free(struct hidden_params *hidden)
-{
-       if (hidden == NULL)
-               return;
-
-       g_free(hidden->identity);
-       g_free(hidden->passphrase);
-       g_free(hidden);
-}
-
-static void scan_callback(int result, GSupplicantInterface *interface,
-                                               void *user_data)
-{
-       struct connman_device *device = user_data;
-       struct wifi_data *wifi = connman_device_get_data(device);
-
-       DBG("result %d", result);
-
-       if (wifi != NULL && wifi->hidden != NULL) {
-               connman_network_clear_hidden(wifi->hidden->user_data);
-               hidden_free(wifi->hidden);
-               wifi->hidden = NULL;
-       }
-
-       if (result < 0)
-               connman_device_reset_scanning(device);
-
-       connman_device_set_scanning(device, FALSE);
-       connman_device_unref(device);
-
-       start_autoscan(device);
-}
-
 struct last_connected {
        GTimeVal modified;
        gchar *ssid;
@@ -819,7 +826,7 @@ static int wifi_scan(struct connman_device *device)
 {
        reset_autoscan(device);
 
-       return throw_wifi_scan(device, scan_callback);
+       return throw_wifi_scan(device, scan_callback_hidden);
 }
 
 static int wifi_scan_fast(struct connman_device *device)
@@ -829,6 +836,9 @@ static int wifi_scan_fast(struct connman_device *device)
        int ret;
        int driver_max_ssids = 0;
 
+       if (wifi == NULL)
+               return -ENODEV;
+
        DBG("device %p %p", device, wifi->interface);
 
        if (wifi->tethering == TRUE)
@@ -883,6 +893,9 @@ static int wifi_scan_hidden(struct connman_device *device,
        struct hidden_params *hidden;
        int ret;
 
+       if (wifi == NULL)
+               return -ENODEV;
+
        DBG("hidden SSID %s", ssid);
 
        if (wifi->tethering == TRUE || wifi->hidden != NULL)