wget: use monotonic_sec instead of gettimeofday
[oweals/busybox.git] / networking / udhcp / clientpacket.c
index 439aa0250eb444c740c0920975fa62c9171457c5..af97962a0b523873a82af7bbfefa988e3119dd9b 100644 (file)
@@ -8,10 +8,8 @@
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
-#include <string.h>
-#include <sys/socket.h>
 #include <features.h>
-#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION
+#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
 #include <netpacket/packet.h>
 #include <net/ethernet.h>
 #else
 #include <linux/if_packet.h>
 #include <linux/if_ether.h>
 #endif
-#include <stdlib.h>
-#include <time.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <fcntl.h>
-
 
+#include "common.h"
 #include "dhcpd.h"
-#include "clientpacket.h"
-#include "options.h"
 #include "dhcpc.h"
-#include "common.h"
+#include "options.h"
 
 
 /* Create a random xid */
-unsigned long random_xid(void)
+unsigned random_xid(void)
 {
-       static int initialized;
+       static smallint initialized;
+
        if (!initialized) {
-               unsigned long seed;
-
-               if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
-                       bb_info_msg("Cannot load seed "
-                               "from /dev/urandom: %s", strerror(errno));
-                       seed = time(0);
-               }
-               srand(seed);
-               initialized++;
+               srand(monotonic_us());
+               initialized = 1;
        }
        return rand();
 }
@@ -59,7 +43,7 @@ static void init_packet(struct dhcpMessage *packet, char type)
        udhcp_init_header(packet, type);
        memcpy(packet->chaddr, client_config.arp, 6);
        if (client_config.clientid)
-           add_option_string(packet->options, client_config.clientid);
+               add_option_string(packet->options, client_config.clientid);
        if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
        if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
        add_option_string(packet->options, client_config.vendorclass);
@@ -97,7 +81,7 @@ int send_discover(unsigned long xid, unsigned long requested)
        add_requests(&packet);
        bb_info_msg("Sending discover...");
        return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
-                               SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
+                       SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
 
@@ -188,10 +172,12 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
        bytes = ntohs(packet.ip.tot_len);
 
        /* Make sure its the right packet for us, and that it passes sanity checks */
-       if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
-           packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
-           bytes > (int) sizeof(struct udp_dhcp_packet) ||
-           ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
+       if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
+        || packet.ip.ihl != sizeof(packet.ip) >> 2
+        || packet.udp.dest != htons(CLIENT_PORT)
+        || bytes > (int) sizeof(struct udp_dhcp_packet)
+        || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
+       ) {
                DEBUG("Unrelated/bogus packet");
                return -2;
        }
@@ -216,14 +202,14 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
        packet.ip.daddr = dest;
        packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
        if (check && check != udhcp_checksum(&packet, bytes)) {
-               bb_error_msg("Packet with bad UDP checksum received, ignoring");
+               bb_error_msg("packet with bad UDP checksum received, ignoring");
                return -2;
        }
 
        memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
 
        if (ntohl(payload->cookie) != DHCP_MAGIC) {
-               bb_error_msg("Received bogus message (bad magic) - ignoring");
+               bb_error_msg("received bogus message (bad magic) - ignoring");
                return -2;
        }
        DEBUG("oooooh!!! got some!");