wget: use monotonic_sec instead of gettimeofday
[oweals/busybox.git] / networking / udhcp / clientpacket.c
index 14e6c6678d9df4cb75a062a41edcc953500b90b4..af97962a0b523873a82af7bbfefa988e3119dd9b 100644 (file)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
 /* clientpacket.c
  *
  * Packet generation and dispatching functions for the DHCP client.
@@ -7,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) {
-               int fd;
-               unsigned long seed;
-
-               fd = open("/dev/urandom", 0);
-               if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
-                       LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m");
-                       seed = time(0);
-               }
-               if (fd >= 0) close(fd);
-               srand(seed);
-               initialized++;
+               srand(monotonic_us());
+               initialized = 1;
        }
        return rand();
 }
@@ -60,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);
@@ -96,9 +79,9 @@ int send_discover(unsigned long xid, unsigned long requested)
                add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
 
        add_requests(&packet);
-       LOG(LOG_DEBUG, "Sending discover...");
+       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);
 }
 
 
@@ -116,7 +99,7 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques
 
        add_requests(&packet);
        addr.s_addr = requested;
-       LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
+       bb_info_msg("Sending select for %s...", inet_ntoa(addr));
        return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
                                SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
@@ -133,7 +116,7 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
        packet.ciaddr = ciaddr;
 
        add_requests(&packet);
-       LOG(LOG_DEBUG, "Sending renew...");
+       bb_info_msg("Sending renew...");
        if (server)
                ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
        else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
@@ -154,7 +137,7 @@ int send_release(unsigned long server, unsigned long ciaddr)
        add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
        add_simple_option(packet.options, DHCP_SERVER_ID, server);
 
-       LOG(LOG_DEBUG, "Sending release...");
+       bb_info_msg("Sending release...");
        return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
 }
 
@@ -170,18 +153,18 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
        memset(&packet, 0, sizeof(struct udp_dhcp_packet));
        bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
        if (bytes < 0) {
-               DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
+               DEBUG("Cannot read on raw listening socket - ignoring");
                usleep(500000); /* possible down interface, looping condition */
                return -1;
        }
 
        if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
-               DEBUG(LOG_INFO, "message too short, ignoring");
+               DEBUG("Message too short, ignoring");
                return -2;
        }
 
        if (bytes < ntohs(packet.ip.tot_len)) {
-               DEBUG(LOG_INFO, "Truncated packet");
+               DEBUG("Truncated packet");
                return -2;
        }
 
@@ -189,11 +172,13 @@ 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))) {
-               DEBUG(LOG_INFO, "unrelated/bogus packet");
+       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;
        }
 
@@ -201,7 +186,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
        check = packet.ip.check;
        packet.ip.check = 0;
        if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
-               DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
+               DEBUG("bad IP header checksum, ignoring");
                return -1;
        }
 
@@ -217,17 +202,17 @@ 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)) {
-               DEBUG(LOG_ERR, "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) {
-               LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
+               bb_error_msg("received bogus message (bad magic) - ignoring");
                return -2;
        }
-       DEBUG(LOG_INFO, "oooooh!!! got some!");
+       DEBUG("oooooh!!! got some!");
        return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
 
 }