gdhcp: Make sure we have the message before accessing it
[platform/upstream/connman.git] / gdhcp / client.c
index cf4cc3c..ec1b2a2 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  DHCP client library with GLib integration
  *
- *  Copyright (C) 2009-2011  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2009-2012  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
@@ -584,7 +584,7 @@ int g_dhcpv6_create_duid(GDHCPDuidType duid_type, int index, int type,
                get_interface_mac_address(index, &(*duid)[2 + 2 + 4]);
                (*duid)[2] = 0;
                (*duid)[3] = type;
-               duid_time = time(0) - DUID_TIME_EPOCH;
+               duid_time = time(NULL) - DUID_TIME_EPOCH;
                (*duid)[4] = duid_time >> 24;
                (*duid)[5] = duid_time >> 16;
                (*duid)[6] = duid_time >> 8;
@@ -910,7 +910,7 @@ GDHCPClient *g_dhcp_client_new(GDHCPType type,
        dhcp_client->require_list = NULL;
        dhcp_client->duid = NULL;
        dhcp_client->duid_len = 0;
-       dhcp_client->last_renew = dhcp_client->last_rebind = time(0);
+       dhcp_client->last_renew = dhcp_client->last_rebind = time(NULL);
        dhcp_client->expire = 0;
 
        *error = G_DHCP_CLIENT_ERROR_NONE;
@@ -1126,7 +1126,6 @@ static int ipv4ll_recv_arp_packet(GDHCPClient *dhcp_client)
        int target_conflict;
 
        memset(&arp, 0, sizeof(arp));
-       bytes = 0;
        bytes = read(dhcp_client->listener_sockfd, &arp, sizeof(arp));
        if (bytes < 0)
                return bytes;
@@ -1596,6 +1595,9 @@ static GList *get_addresses(GDHCPClient *dhcp_client,
        uint8_t *option;
        char *str;
 
+       if (value == NULL || len < 4)
+               return NULL;
+
        iaid = get_uint32(&value[0]);
        if (dhcp_client->iaid != iaid)
                return NULL;
@@ -1700,6 +1702,9 @@ static GList *get_dhcpv6_option_value_list(GDHCPClient *dhcp_client,
        char *str;
        int i;
 
+       if (value == NULL)
+               return NULL;
+
        switch (code) {
        case G_DHCPV6_DNS_SERVERS:      /* RFC 3646, chapter 3 */
        case G_DHCPV6_SNTP_SERVERS:     /* RFC 4075, chapter 4 */
@@ -1847,7 +1852,7 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
                        re = dhcp_recv_l3_packet(&packet,
                                                dhcp_client->listener_sockfd);
        } else if (dhcp_client->listen_mode == L_ARP) {
-               re = ipv4ll_recv_arp_packet(dhcp_client);
+               ipv4ll_recv_arp_packet(dhcp_client);
                return TRUE;
        }
        else
@@ -1860,6 +1865,9 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
                return TRUE;
 
        if (dhcp_client->type == G_DHCP_IPV6) {
+               if (packet6 == NULL)
+                       return TRUE;
+
                count = 0;
                client_id = dhcpv6_get_option(packet6, pkt_len,
                                G_DHCPV6_CLIENTID, &option_len, &count);
@@ -1892,8 +1900,11 @@ static gboolean listener_event(GIOChannel *channel, GIOCondition condition,
                } else
                        dhcp_client->status_code = 0;
 
-       } else
+       } else {
                message_type = dhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
+               if (message_type == NULL)
+                       return TRUE;
+       }
 
        if (message_type == NULL && client_id == NULL)
                /* No message type / client id option, ignore package */
@@ -2569,7 +2580,7 @@ void g_dhcpv6_client_reset_renew(GDHCPClient *dhcp_client)
        if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
                return;
 
-       dhcp_client->last_renew = time(0);
+       dhcp_client->last_renew = time(NULL);
 }
 
 void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
@@ -2577,7 +2588,7 @@ void g_dhcpv6_client_reset_rebind(GDHCPClient *dhcp_client)
        if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
                return;
 
-       dhcp_client->last_rebind = time(0);
+       dhcp_client->last_rebind = time(NULL);
 }
 
 void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
@@ -2585,7 +2596,7 @@ void g_dhcpv6_client_set_expire(GDHCPClient *dhcp_client, uint32_t timeout)
        if (dhcp_client == NULL || dhcp_client->type == G_DHCP_IPV4)
                return;
 
-       dhcp_client->expire = time(0) + timeout;
+       dhcp_client->expire = time(NULL) + timeout;
 }
 
 uint16_t g_dhcpv6_client_get_status(GDHCPClient *dhcp_client)