Release tizen_2.0_beta
[framework/connectivity/connman.git] / gdhcp / client.c
index 3508b1e..29c30fa 100644 (file)
@@ -23,6 +23,7 @@
 #include <config.h>
 #endif
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
@@ -71,7 +72,7 @@ typedef enum _dhcp_client_state {
 } ClientState;
 
 struct _GDHCPClient {
-       gint ref_count;
+       int ref_count;
        GDHCPType type;
        ClientState state;
        int ifindex;
@@ -108,6 +109,10 @@ struct _GDHCPClient {
        gpointer address_conflict_data;
        GDHCPDebugFunc debug_func;
        gpointer debug_data;
+       char *last_address;
+#if defined TIZEN_EXT
+       gboolean init_reboot;
+#endif
 };
 
 static inline void debug(GDHCPClient *client, const char *format, ...)
@@ -201,7 +206,6 @@ static int send_discover(GDHCPClient *dhcp_client, uint32_t requested)
 static int send_select(GDHCPClient *dhcp_client)
 {
        struct dhcp_packet packet;
-       struct in_addr addr;
 
        debug(dhcp_client, "sending DHCP select request");
 
@@ -211,14 +215,15 @@ static int send_select(GDHCPClient *dhcp_client)
 
        dhcp_add_simple_option(&packet, DHCP_REQUESTED_IP,
                                        dhcp_client->requested_ip);
+#if defined TIZEN_EXT
+       if (dhcp_client->init_reboot != TRUE)
+#endif
        dhcp_add_simple_option(&packet, DHCP_SERVER_ID, dhcp_client->server_ip);
 
        add_request_options(dhcp_client, &packet);
 
        add_send_options(dhcp_client, &packet);
 
-       addr.s_addr = dhcp_client->requested_ip;
-
        return dhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT,
                                        INADDR_BROADCAST, SERVER_PORT,
                                        MAC_BCAST_ADDR, dhcp_client->ifindex);
@@ -362,7 +367,7 @@ static void get_interface_mac_address(int index, uint8_t *mac_address)
        struct ifreq ifr;
        int sk, err;
 
-       sk = socket(PF_INET, SOCK_DGRAM, 0);
+       sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (sk < 0) {
                perror("Open socket error");
                return;
@@ -516,7 +521,7 @@ static int dhcp_l2_socket(int ifindex)
                .filter = (struct sock_filter *) filter_instr,
        };
 
-       fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
+       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
        if (fd < 0)
                return fd;
 
@@ -881,7 +886,7 @@ static void restart_dhcp(GDHCPClient *dhcp_client, int retry_times)
        dhcp_client->requested_ip = 0;
        switch_listening_mode(dhcp_client, L2);
 
-       g_dhcp_client_start(dhcp_client);
+       g_dhcp_client_start(dhcp_client, dhcp_client->last_address);
 }
 
 static gboolean start_rebound_timeout(gpointer user_data)
@@ -946,6 +951,9 @@ static gboolean start_renew_timeout(gpointer user_data)
        else {
                send_renew(dhcp_client);
 
+               if (dhcp_client->timeout > 0)
+                       g_source_remove(dhcp_client->timeout);
+
                dhcp_client->timeout =
                                g_timeout_add_seconds_full(G_PRIORITY_HIGH,
                                                dhcp_client->lease_seconds >> 1,
@@ -963,6 +971,9 @@ static void start_bound(GDHCPClient *dhcp_client)
 
        dhcp_client->state = BOUND;
 
+       if (dhcp_client->timeout > 0)
+               g_source_remove(dhcp_client->timeout);
+
        dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
                                        dhcp_client->lease_seconds >> 1,
                                        start_renew_timeout, dhcp_client,
@@ -1192,6 +1203,9 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
                        dhcp_client->timeout = 0;
 
                        dhcp_client->lease_seconds = get_lease(&packet);
+#if defined TIZEN_EXT
+                       debug(dhcp_client, "lease %d secs", dhcp_client->lease_seconds);
+#endif
 
                        get_request(dhcp_client, &packet);
 
@@ -1212,6 +1226,9 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
                        if (dhcp_client->timeout > 0)
                                g_source_remove(dhcp_client->timeout);
 
+#if defined TIZEN_EXT
+                       g_dhcp_client_set_address_known(dhcp_client, FALSE);
+#endif
                        dhcp_client->timeout = g_timeout_add_seconds_full(
                                                        G_PRIORITY_HIGH, 3,
                                                        restart_dhcp_timeout,
@@ -1236,7 +1253,12 @@ static gboolean discover_timeout(gpointer user_data)
 
        dhcp_client->retry_times++;
 
-       g_dhcp_client_start(dhcp_client);
+       /*
+        * We do not send the REQUESTED IP option if we are retrying because
+        * if the server is non-authoritative it will ignore the request if the
+        * option is present.
+        */
+       g_dhcp_client_start(dhcp_client, NULL);
 
        return FALSE;
 }
@@ -1302,9 +1324,10 @@ static gboolean ipv4ll_probe_timeout(gpointer dhcp_data)
        return FALSE;
 }
 
-int g_dhcp_client_start(GDHCPClient *dhcp_client)
+int g_dhcp_client_start(GDHCPClient *dhcp_client, const char *last_address)
 {
        int re;
+       uint32_t addr;
 
        if (dhcp_client->retry_times == DISCOVER_RETRIES) {
                ipv4ll_start(dhcp_client);
@@ -1323,7 +1346,27 @@ int g_dhcp_client_start(GDHCPClient *dhcp_client)
                dhcp_client->xid = rand();
        }
 
-       send_discover(dhcp_client, 0);
+       if (last_address == NULL) {
+               addr = 0;
+       } else {
+               addr = inet_addr(last_address);
+               if (addr == 0xFFFFFFFF) {
+                       addr = 0;
+               } else {
+                       g_free(dhcp_client->last_address);
+                       dhcp_client->last_address = g_strdup(last_address);
+               }
+       }
+#if defined TIZEN_EXT
+       if (dhcp_client->init_reboot == TRUE) {
+               dhcp_client->requested_ip = addr;
+
+               start_request(dhcp_client);
+
+               return 0;
+       }
+#endif
+       send_discover(dhcp_client, addr);
 
        dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
                                                        DISCOVER_TIMEOUT,
@@ -1413,6 +1456,30 @@ char *g_dhcp_client_get_address(GDHCPClient *dhcp_client)
        return g_strdup(dhcp_client->assigned_ip);
 }
 
+char *g_dhcp_client_get_netmask(GDHCPClient *dhcp_client)
+{
+       GList *option = NULL;
+
+       switch (dhcp_client->state) {
+       case IPV4LL_DEFEND:
+       case IPV4LL_MONITOR:
+               return g_strdup("255.255.0.0");
+       case BOUND:
+       case RENEWING:
+       case REBINDING:
+               option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
+               if (option != NULL)
+                       return g_strdup(option->data);
+       case INIT_SELECTING:
+       case REQUESTING:
+       case RELEASED:
+       case IPV4LL_PROBE:
+       case IPV4LL_ANNOUNCE:
+               break;
+       }
+       return NULL;
+}
+
 GDHCPClientError g_dhcp_client_set_request(GDHCPClient *dhcp_client,
                                                unsigned char option_code)
 {
@@ -1460,7 +1527,7 @@ GDHCPClient *g_dhcp_client_ref(GDHCPClient *dhcp_client)
        if (dhcp_client == NULL)
                return NULL;
 
-       g_atomic_int_inc(&dhcp_client->ref_count);
+       __sync_fetch_and_add(&dhcp_client->ref_count, 1);
 
        return dhcp_client;
 }
@@ -1470,13 +1537,14 @@ void g_dhcp_client_unref(GDHCPClient *dhcp_client)
        if (dhcp_client == NULL)
                return;
 
-       if (g_atomic_int_dec_and_test(&dhcp_client->ref_count) == FALSE)
+       if (__sync_fetch_and_sub(&dhcp_client->ref_count, 1) != 1)
                return;
 
        g_dhcp_client_stop(dhcp_client);
 
        g_free(dhcp_client->interface);
        g_free(dhcp_client->assigned_ip);
+       g_free(dhcp_client->last_address);
 
        g_list_free(dhcp_client->request_list);
        g_list_free(dhcp_client->require_list);
@@ -1496,3 +1564,19 @@ void g_dhcp_client_set_debug(GDHCPClient *dhcp_client,
        dhcp_client->debug_func = func;
        dhcp_client->debug_data = user_data;
 }
+
+#if defined TIZEN_EXT
+void g_dhcp_client_set_address_known(GDHCPClient *dhcp_client, gboolean known)
+{
+       /* DHCPREQUEST during INIT-REBOOT state (rfc2131)
+        * 4.4.3 Initialization with known network address
+        * 4.3.2 DHCPREQUEST generated during INIT-REBOOT state
+        */
+       debug(dhcp_client, "known network address (%d)", known);
+
+       if (dhcp_client->init_reboot == known)
+               return;
+
+       dhcp_client->init_reboot = known;
+}
+#endif