Imported Upstream version 1.29
[platform/upstream/connman.git] / gdhcp / ipv4ll.c
index b35626d..c43971f 100644 (file)
 
 #include <glib.h>
 #include "ipv4ll.h"
+#include "common.h"
 
 /**
- * Return a random link local IP
+ * Return a random link local IP (in host byte order)
  */
-uint32_t ipv4ll_random_ip(int seed)
+uint32_t ipv4ll_random_ip(void)
 {
        unsigned tmp;
+       uint64_t rand;
 
-       if (seed)
-               srand(seed);
-       else {
-               struct timeval tv;
-               gettimeofday(&tv, NULL);
-               srand(tv.tv_usec);
-       }
        do {
-               tmp = rand();
+               dhcp_get_random(&rand);
+               tmp = rand;
                tmp = tmp & IN_CLASSB_HOST;
        } while (tmp > (IN_CLASSB_HOST - 0x0200));
        return ((LINKLOCAL_ADDR + 0x0100) + tmp);
@@ -61,13 +57,10 @@ uint32_t ipv4ll_random_ip(int seed)
  */
 guint ipv4ll_random_delay_ms(guint secs)
 {
-       struct timeval tv;
-       guint tmp;
+       uint64_t rand;
 
-       gettimeofday(&tv, NULL);
-       srand(tv.tv_usec);
-       tmp = rand();
-       return tmp % (secs * 1000);
+       dhcp_get_random(&rand);
+       return rand % (secs * 1000);
 }
 
 int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
@@ -79,7 +72,7 @@ int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
        uint32_t ip_target;
        int fd, n;
 
-       fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
+       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_ARP));
        if (fd < 0)
                return -errno;
 
@@ -111,7 +104,7 @@ int ipv4ll_send_arp_packet(uint8_t* source_eth, uint32_t source_ip,
        n = sendto(fd, &p, sizeof(p), 0,
               (struct sockaddr*) &dest, sizeof(dest));
        if (n < 0)
-               return -errno;
+               n = -errno;
 
        close(fd);
 
@@ -122,10 +115,13 @@ int ipv4ll_arp_socket(int ifindex)
 {
        int fd;
        struct sockaddr_ll sock;
-       fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
+
+       fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_ARP));
        if (fd < 0)
                return fd;
 
+       memset(&sock, 0, sizeof(sock));
+
        sock.sll_family = AF_PACKET;
        sock.sll_protocol = htons(ETH_P_ARP);
        sock.sll_ifindex = ifindex;