doc: Remove deprecated properties and values
[framework/connectivity/connman.git] / gdhcp / client.c
index 0cf39cd..3270346 100644 (file)
@@ -72,7 +72,7 @@ typedef enum _dhcp_client_state {
 } ClientState;
 
 struct _GDHCPClient {
-       gint ref_count;
+       int ref_count;
        GDHCPType type;
        ClientState state;
        int ifindex;
@@ -109,6 +109,7 @@ struct _GDHCPClient {
        gpointer address_conflict_data;
        GDHCPDebugFunc debug_func;
        gpointer debug_data;
+       char *last_address;
 };
 
 static inline void debug(GDHCPClient *client, const char *format, ...)
@@ -360,7 +361,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;
@@ -514,7 +515,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;
 
@@ -879,7 +880,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)
@@ -1240,7 +1241,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;
 }
@@ -1306,9 +1312,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);
@@ -1327,7 +1334,18 @@ 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);
+               }
+       }
+       send_discover(dhcp_client, addr);
 
        dhcp_client->timeout = g_timeout_add_seconds_full(G_PRIORITY_HIGH,
                                                        DISCOVER_TIMEOUT,
@@ -1488,7 +1506,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;
 }
@@ -1498,13 +1516,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);