mirror of
https://github.com/sheumann/hush.git
synced 2024-10-27 07:28:31 +00:00
udhcpc: shrink
udhcpc: guard against zero lease time function old new delta timeout 4 - -4 server_addr 4 - -4 requested_ip 4 - -4 perform_release 134 112 -22 udhcpc_main 2511 2485 -26 ------------------------------------------------------------------------------ (add/remove: 0/3 grow/shrink: 0/2 up/down: 0/-60) Total: -60 bytes
This commit is contained in:
parent
6bb55cfb99
commit
fc9e108032
@ -135,7 +135,18 @@
|
|||||||
#define SWAP_LE64(x) (x)
|
#define SWAP_LE64(x) (x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ---- Unaligned access ------------------------------------ */
|
||||||
|
|
||||||
|
/* parameter is supposed to be an uint32_t* ptr */
|
||||||
|
#if defined(i386) || defined(__x86_64__) /* + other arches? */
|
||||||
|
#define get_unaligned_u32p(u32p) (*(u32p))
|
||||||
|
#else
|
||||||
|
/* performs reasonably well (gcc usually inlines memcpy here) */
|
||||||
|
#define get_unaligned_u32p(u32p) ({ uint32_t __t; memcpy(&__t, (u32p), 4); __t; })
|
||||||
|
#endif
|
||||||
|
|
||||||
/* ---- Networking ------------------------------------------ */
|
/* ---- Networking ------------------------------------------ */
|
||||||
|
|
||||||
#ifndef __APPLE__
|
#ifndef __APPLE__
|
||||||
# include <arpa/inet.h>
|
# include <arpa/inet.h>
|
||||||
# ifndef __socklen_t_defined
|
# ifndef __socklen_t_defined
|
||||||
@ -146,6 +157,7 @@ typedef int socklen_t;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ---- Compiler dependent settings ------------------------- */
|
/* ---- Compiler dependent settings ------------------------- */
|
||||||
|
|
||||||
#if (defined __digital__ && defined __unix__) || defined __APPLE__
|
#if (defined __digital__ && defined __unix__) || defined __APPLE__
|
||||||
# undef HAVE_MNTENT_H
|
# undef HAVE_MNTENT_H
|
||||||
# undef HAVE_SYS_STATFS_H
|
# undef HAVE_SYS_STATFS_H
|
||||||
@ -163,9 +175,10 @@ __extension__ typedef unsigned long long __u64;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*----- Kernel versioning ------------------------------------*/
|
/*----- Kernel versioning ------------------------------------*/
|
||||||
|
|
||||||
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
|
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
|
||||||
|
|
||||||
/* ---- miscellaneous --------------------------------------- */
|
/* ---- Miscellaneous --------------------------------------- */
|
||||||
|
|
||||||
#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
|
#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
|
||||||
!defined(__dietlibc__) && \
|
!defined(__dietlibc__) && \
|
||||||
|
@ -18,9 +18,6 @@
|
|||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
|
||||||
|
|
||||||
static int timeout; /* = 0. Must be signed */
|
|
||||||
static uint32_t requested_ip; /* = 0 */
|
|
||||||
static uint32_t server_addr;
|
|
||||||
static int sockfd = -1;
|
static int sockfd = -1;
|
||||||
|
|
||||||
#define LISTEN_NONE 0
|
#define LISTEN_NONE 0
|
||||||
@ -28,6 +25,14 @@ static int sockfd = -1;
|
|||||||
#define LISTEN_RAW 2
|
#define LISTEN_RAW 2
|
||||||
static smallint listen_mode;
|
static smallint listen_mode;
|
||||||
|
|
||||||
|
#define INIT_SELECTING 0
|
||||||
|
#define REQUESTING 1
|
||||||
|
#define BOUND 2
|
||||||
|
#define RENEWING 3
|
||||||
|
#define REBINDING 4
|
||||||
|
#define INIT_REBOOT 5
|
||||||
|
#define RENEW_REQUESTED 6
|
||||||
|
#define RELEASED 7
|
||||||
static smallint state;
|
static smallint state;
|
||||||
|
|
||||||
/* struct client_config_t client_config is in bb_common_bufsiz1 */
|
/* struct client_config_t client_config is in bb_common_bufsiz1 */
|
||||||
@ -71,7 +76,7 @@ static void perform_renew(void)
|
|||||||
|
|
||||||
|
|
||||||
/* perform a release */
|
/* perform a release */
|
||||||
static void perform_release(void)
|
static void perform_release(uint32_t requested_ip, uint32_t server_addr)
|
||||||
{
|
{
|
||||||
char buffer[sizeof("255.255.255.255")];
|
char buffer[sizeof("255.255.255.255")];
|
||||||
struct in_addr temp_addr;
|
struct in_addr temp_addr;
|
||||||
@ -90,7 +95,6 @@ static void perform_release(void)
|
|||||||
|
|
||||||
change_listen_mode(LISTEN_NONE);
|
change_listen_mode(LISTEN_NONE);
|
||||||
state = RELEASED;
|
state = RELEASED;
|
||||||
timeout = INT_MAX;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -140,9 +144,12 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
int tryagain_timeout = 20;
|
int tryagain_timeout = 20;
|
||||||
int discover_timeout = 3;
|
int discover_timeout = 3;
|
||||||
int discover_retries = 3;
|
int discover_retries = 3;
|
||||||
|
uint32_t server_addr = server_addr; /* for compiler */
|
||||||
|
uint32_t requested_ip = 0;
|
||||||
uint32_t xid = 0;
|
uint32_t xid = 0;
|
||||||
uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
|
uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
|
||||||
int packet_num;
|
int packet_num;
|
||||||
|
int timeout; /* must be signed */
|
||||||
unsigned already_waited_sec;
|
unsigned already_waited_sec;
|
||||||
unsigned opt;
|
unsigned opt;
|
||||||
int max_fd;
|
int max_fd;
|
||||||
@ -332,6 +339,7 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
udhcp_run_script(NULL, "deconfig");
|
udhcp_run_script(NULL, "deconfig");
|
||||||
change_listen_mode(LISTEN_RAW);
|
change_listen_mode(LISTEN_RAW);
|
||||||
packet_num = 0;
|
packet_num = 0;
|
||||||
|
timeout = 0;
|
||||||
already_waited_sec = 0;
|
already_waited_sec = 0;
|
||||||
|
|
||||||
/* Main event loop. select() waits on signal pipe and possibly
|
/* Main event loop. select() waits on signal pipe and possibly
|
||||||
@ -510,8 +518,8 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
continue;
|
continue;
|
||||||
/* still selecting - this server looks bad */
|
/* still selecting - this server looks bad */
|
||||||
}
|
}
|
||||||
/* can be misaligned, thus memcpy */
|
/* it IS unaligned sometimes, don't "optimize" */
|
||||||
memcpy(&server_addr, temp, 4);
|
server_addr = get_unaligned_u32p((uint32_t*)temp);
|
||||||
xid = packet.xid;
|
xid = packet.xid;
|
||||||
requested_ip = packet.yiaddr;
|
requested_ip = packet.yiaddr;
|
||||||
|
|
||||||
@ -535,7 +543,9 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
/* can be misaligned, thus memcpy */
|
/* can be misaligned, thus memcpy */
|
||||||
memcpy(&lease_seconds, temp, 4);
|
memcpy(&lease_seconds, temp, 4);
|
||||||
lease_seconds = ntohl(lease_seconds);
|
lease_seconds = ntohl(lease_seconds);
|
||||||
lease_seconds &= 0x0fffffff; /* paranoia: must not be negative */
|
lease_seconds &= 0x0fffffff; /* paranoia: must not be prone to overflows */
|
||||||
|
if (lease_seconds < 10) /* and not too small */
|
||||||
|
lease_seconds = 10;
|
||||||
}
|
}
|
||||||
#if ENABLE_FEATURE_UDHCPC_ARPING
|
#if ENABLE_FEATURE_UDHCPC_ARPING
|
||||||
if (opt & OPT_a) {
|
if (opt & OPT_a) {
|
||||||
@ -576,7 +586,7 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
change_listen_mode(LISTEN_NONE);
|
change_listen_mode(LISTEN_NONE);
|
||||||
if (client_config.quit_after_lease) {
|
if (client_config.quit_after_lease) {
|
||||||
if (client_config.release_on_quit)
|
if (client_config.release_on_quit)
|
||||||
perform_release();
|
perform_release(requested_ip, server_addr);
|
||||||
goto ret0;
|
goto ret0;
|
||||||
}
|
}
|
||||||
if (!client_config.foreground)
|
if (!client_config.foreground)
|
||||||
@ -618,12 +628,13 @@ int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
break;
|
break;
|
||||||
case SIGUSR2:
|
case SIGUSR2:
|
||||||
perform_release();
|
perform_release(requested_ip, server_addr);
|
||||||
|
timeout = INT_MAX;
|
||||||
break;
|
break;
|
||||||
case SIGTERM:
|
case SIGTERM:
|
||||||
bb_info_msg("Received SIGTERM");
|
bb_info_msg("Received SIGTERM");
|
||||||
if (client_config.release_on_quit)
|
if (client_config.release_on_quit)
|
||||||
perform_release();
|
perform_release(requested_ip, server_addr);
|
||||||
goto ret0;
|
goto ret0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,6 @@
|
|||||||
# pragma GCC visibility push(hidden)
|
# pragma GCC visibility push(hidden)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define INIT_SELECTING 0
|
|
||||||
#define REQUESTING 1
|
|
||||||
#define BOUND 2
|
|
||||||
#define RENEWING 3
|
|
||||||
#define REBINDING 4
|
|
||||||
#define INIT_REBOOT 5
|
|
||||||
#define RENEW_REQUESTED 6
|
|
||||||
#define RELEASED 7
|
|
||||||
|
|
||||||
struct client_config_t {
|
struct client_config_t {
|
||||||
/* TODO: combine flag fields into single "unsigned opt" */
|
/* TODO: combine flag fields into single "unsigned opt" */
|
||||||
/* (can be set directly to the result of getopt32) */
|
/* (can be set directly to the result of getopt32) */
|
||||||
|
Loading…
Reference in New Issue
Block a user