Refactor Code
[platform/upstream/connman.git] / src / dhcp.c
index 42e9f41..5ad1efb 100644 (file)
 
 #define RATE_LIMIT_INTERVAL    60      /* delay between successive attempts */
 
+#if defined TIZEN_EXT
+#define DHCP_SUCCESS           "DHCP_SUCCESS"
+#define DHCP_FAIL              "DHCP_FAIL"
+#define DHCP_STARTED           "DHCP_STARTED"
+#endif
+
 struct connman_dhcp {
        struct connman_ipconfig *ipconfig;
        struct connman_network *network;
@@ -67,6 +73,9 @@ static GHashTable *ipconfig_table;
 
 static void dhcp_free(struct connman_dhcp *dhcp)
 {
+#if defined TIZEN_EXT
+       DBG("dhcp_free [%p]", dhcp);
+#endif
        g_strfreev(dhcp->nameservers);
        g_strfreev(dhcp->timeservers);
        g_free(dhcp->pac);
@@ -76,10 +85,16 @@ static void dhcp_free(struct connman_dhcp *dhcp)
        dhcp->pac = NULL;
 
        g_free(dhcp);
+#if defined TIZEN_EXT
+       dhcp = NULL;
+#endif
 }
 
 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
 {
+#if defined TIZEN_EXT
+       DBG("dhcp [%p] ipv4ll_client [%p]", dhcp, dhcp->ipv4ll_client);
+#endif
        if (!dhcp->ipv4ll_client)
                return;
 
@@ -119,8 +134,14 @@ static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
        }
        if (dhcp->nameservers) {
                for (i = 0; dhcp->nameservers[i]; i++) {
+#if defined TIZEN_EXT
+                       __connman_service_nameserver_remove(service,
+                                       dhcp->nameservers[i], false,
+                                       CONNMAN_IPCONFIG_TYPE_IPV4);
+#else
                        __connman_service_nameserver_remove(service,
                                                dhcp->nameservers[i], false);
+#endif
                }
                g_strfreev(dhcp->nameservers);
                dhcp->nameservers = NULL;
@@ -193,6 +214,10 @@ static int ipv4ll_start_client(struct connman_dhcp *dhcp)
        int index;
        int err;
 
+#if defined TIZEN_EXT
+       DBG("dhcp %p", dhcp);
+#endif
+
        if (dhcp->ipv4ll_client)
                return -EALREADY;
 
@@ -202,12 +227,16 @@ static int ipv4ll_start_client(struct connman_dhcp *dhcp)
        if (error != G_DHCP_CLIENT_ERROR_NONE)
                return -EINVAL;
 
+#if !defined TIZEN_EXT
        if (getenv("CONNMAN_DHCP_DEBUG")) {
+#endif
                dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
                                                        index);
                g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
                                        dhcp->ipv4ll_debug_prefix);
+#if !defined TIZEN_EXT
        }
+#endif
 
        g_dhcp_client_set_id(ipv4ll_client);
 
@@ -243,6 +272,10 @@ static gboolean dhcp_retry_cb(gpointer user_data)
 
        dhcp->timeout = 0;
 
+#if defined TIZEN_EXT
+       DBG("dhcp %p", dhcp);
+       DBG("dhcp->timeout %d", dhcp->timeout);
+#endif
        g_dhcp_client_start(dhcp->dhcp_client,
                        __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
 
@@ -257,6 +290,30 @@ static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
        DBG("No lease available ipv4ll %d client %p", dhcp->ipv4ll_running,
                dhcp->ipv4ll_client);
 
+#if defined TIZEN_EXT
+       if (dhcp->network &&
+                       connman_network_get_bool(dhcp->network, "WiFi.RoamingDHCP")) {
+
+               connman_network_set_bool(dhcp->network, "WiFi.RoamingDHCP", false);
+               __connman_network_enable_ipconfig(dhcp->network, dhcp->ipconfig);
+               __connman_network_notify_dhcp_changed(DHCP_FAIL,
+                                       g_dhcp_client_get_interface(dhcp_client));
+
+               return;
+       }
+
+       if (connman_setting_get_bool("EnableAutoIp") == false) {
+               DBG("link-local address autoconfiguration is disabled.");
+
+               if (dhcp->network) {
+                       DBG("[DHCP-C] auto ip is not used, set dhcp-fail error and disconnect");
+                       __connman_network_notify_dhcp_changed(DHCP_FAIL,
+                                               g_dhcp_client_get_interface(dhcp_client));
+                       connman_network_set_error(dhcp->network, CONNMAN_NETWORK_ERROR_DHCP_FAIL);
+               }
+               return;
+       }
+#endif
        if (dhcp->timeout > 0)
                g_source_remove(dhcp->timeout);
 
@@ -382,18 +439,32 @@ static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
 
        if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
                if (dhcp->nameservers) {
+#if defined TIZEN_EXT
+                       for (i = 0; dhcp->nameservers[i] != NULL; i++) {
+                               __connman_service_nameserver_remove(service,
+                                               dhcp->nameservers[i], false,
+                                               CONNMAN_IPCONFIG_TYPE_IPV4);
+                       }
+#else
                        for (i = 0; dhcp->nameservers[i]; i++) {
                                __connman_service_nameserver_remove(service,
                                                dhcp->nameservers[i], false);
                        }
+#endif
                        g_strfreev(dhcp->nameservers);
                }
 
                dhcp->nameservers = nameservers;
 
                for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
+#if defined TIZEN_EXT
+                       __connman_service_nameserver_append(service,
+                                               dhcp->nameservers[i], false,
+                                               CONNMAN_IPCONFIG_TYPE_IPV4);
+#else
                        __connman_service_nameserver_append(service,
                                                dhcp->nameservers[i], false);
+#endif
                }
        } else {
                g_strfreev(nameservers);
@@ -458,6 +529,10 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
        __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
        DBG("last address %s", address);
 
+#if defined TIZEN_EXT
+       int dhcp_lease_duration = g_dhcp_client_get_dhcp_lease_duration(dhcp_client);
+#endif
+
        option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
        if (option)
                netmask = g_strdup(option->data);
@@ -492,6 +567,12 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
        __connman_ipconfig_set_method(dhcp->ipconfig,
                                                CONNMAN_IPCONFIG_METHOD_DHCP);
 
+#if defined TIZEN_EXT
+       __connman_ipconfig_set_dhcp_lease_duration(dhcp->ipconfig, dhcp_lease_duration);
+       __connman_network_notify_dhcp_changed(DHCP_SUCCESS,
+                               g_dhcp_client_get_interface(dhcp_client));
+#endif
+
        /*
         * Notify IPv4.Configuration's method moved back to DHCP.
         *
@@ -506,6 +587,17 @@ static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
                        __connman_service_notify_ipv4_configuration(service);
        }
 
+#if defined TIZEN_EXT
+       if (connman_network_get_bool(dhcp->network, "WiFi.RoamingDHCP")) {
+
+               if (ip_change)
+                       connman_service_notify_reconnection_roaming(
+                               connman_service_lookup_from_network(dhcp->network));
+
+               connman_network_set_bool(dhcp->network, "WiFi.RoamingDHCP", false);
+       }
+#endif
+
        if (ip_change) {
                __connman_ipconfig_set_local(dhcp->ipconfig, address);
                __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
@@ -565,6 +657,11 @@ static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
 
        g_free(address);
        g_free(netmask);
+
+#if defined TIZEN_EXT
+       __connman_network_notify_dhcp_changed(DHCP_FAIL,
+                       g_dhcp_client_get_interface(ipv4ll_client));
+#endif
 }
 
 static int dhcp_initialize(struct connman_dhcp *dhcp)
@@ -580,14 +677,25 @@ static int dhcp_initialize(struct connman_dhcp *dhcp)
 
        dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
        if (error != G_DHCP_CLIENT_ERROR_NONE)
+#if defined TIZEN_EXT
+       {
+               DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
+#endif
                return -EINVAL;
+#if defined TIZEN_EXT
+       }
+#endif
 
+#if !defined TIZEN_EXT
        if (getenv("CONNMAN_DHCP_DEBUG")) {
+#endif
                dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
                                                        index);
                g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
                                        dhcp->dhcp_debug_prefix);
+#if !defined TIZEN_EXT
        }
+#endif
 
        g_dhcp_client_set_id(dhcp_client);
 
@@ -616,7 +724,7 @@ static int dhcp_initialize(struct connman_dhcp *dhcp)
        g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
        g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
 
-       vendor_class_id = connman_option_get_string("VendorClassID");
+       vendor_class_id = connman_setting_get_string("VendorClassID");
        if (vendor_class_id)
                g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
                                        vendor_class_id);
@@ -671,11 +779,48 @@ char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
        return g_dhcp_client_get_server_address(dhcp->dhcp_client);
 }
 
+#if defined TIZEN_EXT_WIFI_MESH
+int __connman_mesh_dhcp_start(struct connman_ipconfig *ipconfig,
+                       dhcp_cb callback, gpointer user_data)
+{
+       struct connman_dhcp *dhcp;
+       int err;
+
+       DBG("");
+
+       dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
+       if (!dhcp) {
+
+               dhcp = g_try_new0(struct connman_dhcp, 1);
+               if (!dhcp)
+                       return -ENOMEM;
+
+               dhcp->ipconfig = ipconfig;
+               __connman_ipconfig_ref(ipconfig);
+
+               err = dhcp_initialize(dhcp);
+
+               if (err < 0) {
+                       g_free(dhcp);
+                       return err;
+               }
+
+               g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
+       }
+
+       dhcp->callback = callback;
+       dhcp->user_data = user_data;
+       return g_dhcp_client_start(dhcp->dhcp_client, NULL);
+}
+#endif
+
 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
                        struct connman_network *network, dhcp_cb callback,
                        gpointer user_data)
 {
+#if !defined TIZEN_EXT
        const char *last_addr = NULL;
+#endif
        struct connman_dhcp *dhcp;
        int err;
 
@@ -689,7 +834,9 @@ int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
                        return -EINVAL;
        }
 
+#if !defined TIZEN_EXT
        last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
+#endif
 
        dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
        if (!dhcp) {
@@ -721,7 +868,22 @@ int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
        dhcp->callback = callback;
        dhcp->user_data = user_data;
 
+#if defined TIZEN_EXT
+       __connman_network_notify_dhcp_changed(DHCP_STARTED,
+                       g_dhcp_client_get_interface(dhcp->dhcp_client));
+
+       if (network && connman_network_get_bool(network, "WiFi.RoamingDHCP")) {
+               const char *last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
+
+               DBG("Start DHCP with last address request");
+               return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
+       } else {
+               DBG("Start DHCP with DHCPDISCOVER request");
+               return g_dhcp_client_start(dhcp->dhcp_client, NULL);
+       }
+#else
        return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
+#endif
 }
 
 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)