* 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)
+uint32_t 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) {
- bb_info_msg("Could not load seed "
- "from /dev/urandom: %s", strerror(errno));
- seed = time(0);
- }
- if (fd >= 0) close(fd);
- srand(seed);
- initialized++;
+ srand(monotonic_us());
+ initialized = 1;
}
return rand();
}
udhcp_init_header(packet, type);
memcpy(packet->chaddr, client_config.arp, 6);
if (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.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);
}
/* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
-int send_discover(unsigned long xid, unsigned long requested)
+int send_discover(uint32_t xid, uint32_t requested)
{
struct dhcpMessage packet;
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);
}
/* Broadcasts a DHCP request message */
-int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
+int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
{
struct dhcpMessage packet;
struct in_addr addr;
/* Unicasts or broadcasts a DHCP renew message */
-int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
+int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
{
struct dhcpMessage packet;
- int ret = 0;
init_packet(&packet, DHCPREQUEST);
packet.xid = xid;
add_requests(&packet);
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,
+ return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+
+ return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
- return ret;
}
/* Unicasts a DHCP release message */
-int send_release(unsigned long server, unsigned long ciaddr)
+int send_release(uint32_t server, uint32_t ciaddr)
{
struct dhcpMessage packet;
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
if (bytes < 0) {
- DEBUG("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;
}
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;
}
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!");
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
-
}