From 253c4e787a799a3e1f92957ed791b5222f8d2f64 Mon Sep 17 00:00:00 2001 From: James Byrne Date: Fri, 12 Apr 2019 17:01:51 +0000 Subject: Optionally re-introduce bb_info_msg() Between Busybox 1.24.2 and 1.25.0 the bb_info_msg() function was eliminated and calls to it changed to be bb_error_msg(). The downside of this is that daemons now log all messages to syslog at the LOG_ERR level which makes it hard to filter errors from informational messages. This change optionally re-introduces bb_info_msg(), controlled by a new option FEATURE_SYSLOG_INFO, restores all the calls to bb_info_msg() that were removed (only in applets that set logmode to LOGMODE_SYSLOG or LOGMODE_BOTH), and also changes informational messages in ifplugd and ntpd. The code size change of this is as follows (using 'defconfig' on x86_64 with gcc 7.3.0-27ubuntu1~18.04) function old new delta bb_info_msg - 182 +182 bb_vinfo_msg - 27 +27 static.log7 194 198 +4 log8 190 191 +1 log5 190 191 +1 crondlog 45 - -45 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 215/-45) Total: 170 bytes If you don't care about everything being logged at LOG_ERR level then when FEATURE_SYSLOG_INFO is disabled Busybox actually gets smaller: function old new delta static.log7 194 200 +6 log8 190 193 +3 log5 190 193 +3 syslog_level 1 - -1 bb_verror_msg 583 581 -2 crondlog 45 - -45 ------------------------------------------------------------------------------ (add/remove: 0/2 grow/shrink: 3/1 up/down: 12/-48) Total: -36 bytes Signed-off-by: James Byrne Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpd.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 0c55fa5e4..d248d2b67 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -104,7 +104,7 @@ static void log_static_leases(struct static_lease **st_lease_pp) cur = *st_lease_pp; while (cur) { - bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", + bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", cur->mac[0], cur->mac[1], cur->mac[2], cur->mac[3], cur->mac[4], cur->mac[5], cur->nip @@ -242,7 +242,7 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigne return r; temp.s_addr = nip; - bb_error_msg("%s belongs to someone, reserving it for %u seconds", + bb_info_msg("%s belongs to someone, reserving it for %u seconds", inet_ntoa(temp), (unsigned)server_config.conflict_time); add_lease(NULL, nip, server_config.conflict_time, NULL, 0); return 0; @@ -722,7 +722,7 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, add_server_options(&packet); addr.s_addr = packet.yiaddr; - bb_error_msg("sending OFFER of %s", inet_ntoa(addr)); + bb_info_msg("sending OFFER of %s", inet_ntoa(addr)); /* send_packet emits error message itself if it detects failure */ send_packet(&packet, /*force_bcast:*/ 0); } @@ -755,7 +755,7 @@ static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) add_server_options(&packet); addr.s_addr = yiaddr; - bb_error_msg("sending ACK to %s", inet_ntoa(addr)); + bb_info_msg("sending ACK to %s", inet_ntoa(addr)); send_packet(&packet, /*force_bcast:*/ 0); p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); @@ -865,7 +865,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) write_pidfile(server_config.pidfile); /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ - bb_error_msg("started, v"BB_VER); + bb_info_msg("started, v"BB_VER); option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); server_config.max_lease_sec = DEFAULT_LEASE_TIME; @@ -944,12 +944,12 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) if (pfds[0].revents) switch (udhcp_sp_read()) { case SIGUSR1: - bb_error_msg("received %s", "SIGUSR1"); + bb_info_msg("received %s", "SIGUSR1"); write_leases(); /* why not just reset the timeout, eh */ goto continue_with_autotime; case SIGTERM: - bb_error_msg("received %s", "SIGTERM"); + bb_info_msg("received %s", "SIGTERM"); write_leases(); goto ret0; } @@ -973,16 +973,16 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) continue; } if (packet.hlen != 6) { - bb_error_msg("MAC length != 6, ignoring packet"); + bb_info_msg("MAC length != 6, ignoring packet"); continue; } if (packet.op != BOOTREQUEST) { - bb_error_msg("not a REQUEST, ignoring packet"); + bb_info_msg("not a REQUEST, ignoring packet"); continue; } state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { - bb_error_msg("no or bad message type option, ignoring packet"); + bb_info_msg("no or bad message type option, ignoring packet"); continue; } @@ -1001,7 +1001,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) /* Look for a static/dynamic lease */ static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); if (static_lease_nip) { - bb_error_msg("found static lease: %x", static_lease_nip); + bb_info_msg("found static lease: %x", static_lease_nip); memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); fake_lease.lease_nip = static_lease_nip; fake_lease.expires = 0; -- cgit v1.2.3-55-g6feb From 15021f393d3d19d689028fceb5c35da930059430 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 10 May 2019 15:55:12 +0200 Subject: udhcpd: code shrink function old new delta is_nip_reserved_as_static - 28 +28 get_static_nip_by_mac 43 47 +4 udhcpd_main 1459 1454 -5 send_offer 449 444 -5 read_leases 309 299 -10 is_nip_reserved 20 - -20 packed_usage 33283 33243 -40 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 1/4 up/down: 32/-80) Total: -48 bytes Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpd.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index d248d2b67..c46e1721e 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -70,8 +70,10 @@ static void add_static_lease(struct static_lease **st_lease_pp, } /* Find static lease IP by mac */ -static uint32_t get_static_nip_by_mac(struct static_lease *st_lease, void *mac) +static uint32_t get_static_nip_by_mac(void *mac) { + struct static_lease *st_lease = server_config.static_leases; + while (st_lease) { if (memcmp(st_lease->mac, mac, 6) == 0) return st_lease->nip; @@ -81,8 +83,10 @@ static uint32_t get_static_nip_by_mac(struct static_lease *st_lease, void *mac) return 0; } -static int is_nip_reserved(struct static_lease *st_lease, uint32_t nip) +static int is_nip_reserved_as_static(uint32_t nip) { + struct static_lease *st_lease = server_config.static_leases; + while (st_lease) { if (st_lease->nip == nip) return 1; @@ -288,7 +292,7 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi if (nip == server_config.server_nip) goto next_addr; /* is this a static lease addr? */ - if (is_nip_reserved(server_config.static_leases, nip)) + if (is_nip_reserved_as_static(nip)) goto next_addr; lease = find_lease_by_nip(nip); @@ -518,13 +522,13 @@ static NOINLINE void read_leases(const char *file) expires = 0; /* Check if there is a different static lease for this IP or MAC */ - static_nip = get_static_nip_by_mac(server_config.static_leases, lease.lease_mac); + static_nip = get_static_nip_by_mac(lease.lease_mac); if (static_nip) { /* NB: we do not add lease even if static_nip == lease.lease_nip. */ continue; } - if (is_nip_reserved(server_config.static_leases, lease.lease_nip)) + if (is_nip_reserved_as_static(lease.lease_nip)) continue; /* NB: add_lease takes "relative time", IOW, @@ -999,7 +1003,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) } /* Look for a static/dynamic lease */ - static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); + static_lease_nip = get_static_nip_by_mac(&packet.chaddr); if (static_lease_nip) { bb_info_msg("found static lease: %x", static_lease_nip); memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); -- cgit v1.2.3-55-g6feb From 8c317f03f6d4d89fd7b0cc1e6eaf515040b8e701 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 14 May 2019 17:26:47 +0200 Subject: style fix, no code changes Signed-off-by: Denys Vlasenko --- libbb/lineedit.c | 13 ++++++++++--- modutils/lsmod.c | 5 ++++- networking/tc.c | 6 ++++-- networking/udhcp/dhcpd.c | 3 ++- networking/wget.c | 2 +- util-linux/mount.c | 5 ++++- 6 files changed, 25 insertions(+), 9 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 1d5fef5ee..fbabc6c12 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -70,13 +70,20 @@ #if ENABLE_UNICODE_SUPPORT # define BB_NUL ((wchar_t)0) # define CHAR_T wchar_t -static bool BB_isspace(CHAR_T c) { return ((unsigned)c < 256 && isspace(c)); } +static bool BB_isspace(CHAR_T c) +{ + return ((unsigned)c < 256 && isspace(c)); +} # if ENABLE_FEATURE_EDITING_VI -static bool BB_isalnum_or_underscore(CHAR_T c) { +static bool BB_isalnum_or_underscore(CHAR_T c) +{ return ((unsigned)c < 256 && isalnum(c)) || c == '_'; } # endif -static bool BB_ispunct(CHAR_T c) { return ((unsigned)c < 256 && ispunct(c)); } +static bool BB_ispunct(CHAR_T c) +{ + return ((unsigned)c < 256 && ispunct(c)); +} # undef isspace # undef isalnum # undef ispunct diff --git a/modutils/lsmod.c b/modutils/lsmod.c index 694205fda..39dc8e6b7 100644 --- a/modutils/lsmod.c +++ b/modutils/lsmod.c @@ -66,7 +66,10 @@ static void check_tainted(void) } } #else -static void check_tainted(void) { putchar('\n'); } +static ALWAYS_INLINE void check_tainted(void) +{ + putchar('\n'); +} #endif int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; diff --git a/networking/tc.c b/networking/tc.c index 3e9808328..2e1078d31 100644 --- a/networking/tc.c +++ b/networking/tc.c @@ -124,7 +124,8 @@ static char* print_tc_classid(uint32_t cid) } /* Get a qdisc handle. Return 0 on success, !0 otherwise. */ -static int get_qdisc_handle(uint32_t *h, const char *str) { +static int get_qdisc_handle(uint32_t *h, const char *str) +{ uint32_t maj; char *p; @@ -143,7 +144,8 @@ static int get_qdisc_handle(uint32_t *h, const char *str) { } /* Get class ID. Return 0 on success, !0 otherwise. */ -static int get_tc_classid(uint32_t *h, const char *str) { +static int get_tc_classid(uint32_t *h, const char *str) +{ uint32_t maj, min; char *p; diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index c46e1721e..0642fc826 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -365,7 +365,8 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg) return 1; } -static int FAST_FUNC read_optset(const char *line, void *arg) { +static int FAST_FUNC read_optset(const char *line, void *arg) +{ return udhcp_str2optset(line, arg, dhcp_optflags, dhcp_option_strings, /*dhcpv6:*/ 0 diff --git a/networking/wget.c b/networking/wget.c index fa4d21afd..b6f9d605a 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -312,7 +312,7 @@ static void progress_meter(int flag) } } #else -static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) { } +static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) {} #endif diff --git a/util-linux/mount.c b/util-linux/mount.c index 526b4130c..e6bad7c2c 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -1194,7 +1194,10 @@ static int daemonize(void) return 1; } #else -static inline int daemonize(void) { return -ENOSYS; } +static inline int daemonize(void) +{ + return -ENOSYS; +} #endif /* TODO */ -- cgit v1.2.3-55-g6feb From 8402969d4892891ddfde524fbb9ee73e076f3771 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 15 May 2019 13:08:48 +0200 Subject: udhcpd: code shrink - do not fetch requested IP twice function old new delta send_offer 444 443 -1 udhcpd_main 1454 1442 -12 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-13) Total: -13 bytes Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpd.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 0642fc826..bf44320a1 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -662,7 +662,7 @@ static uint32_t select_lease_time(struct dhcp_packet *packet) static NOINLINE void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease, - uint8_t *requested_ip_opt, + uint32_t requested_nip, unsigned arpping_ms) { struct dhcp_packet packet; @@ -676,7 +676,6 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, /* Else: */ if (!static_lease_nip) { /* We have no static lease for client's chaddr */ - uint32_t req_nip; const char *p_host_name; if (lease) { @@ -687,18 +686,16 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, packet.yiaddr = lease->lease_nip; } /* Or: if client has requested an IP */ - else if (requested_ip_opt != NULL - /* (read IP) */ - && (move_from_unaligned32(req_nip, requested_ip_opt), 1) + else if (requested_nip != 0 /* and the IP is in the lease range */ - && ntohl(req_nip) >= server_config.start_ip - && ntohl(req_nip) <= server_config.end_ip + && ntohl(requested_nip) >= server_config.start_ip + && ntohl(requested_nip) <= server_config.end_ip /* and */ - && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ + && ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */ || is_expired_lease(lease) /* or is taken, but expired */ ) ) { - packet.yiaddr = req_nip; + packet.yiaddr = requested_nip; } else { /* Otherwise, find a free IP */ @@ -913,7 +910,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) int tv; uint8_t *server_id_opt; uint8_t *requested_ip_opt; - uint32_t requested_nip = requested_nip; /* for compiler */ + uint32_t requested_nip; uint32_t static_lease_nip; struct dyn_lease *lease, fake_lease; @@ -1016,6 +1013,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) } /* Get REQUESTED_IP if present */ + requested_nip = 0; requested_ip_opt = udhcp_get_option32(&packet, DHCP_REQUESTED_IP); if (requested_ip_opt) { move_from_unaligned32(requested_nip, requested_ip_opt); @@ -1026,7 +1024,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) case DHCPDISCOVER: log1("received %s", "DISCOVER"); - send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms); + send_offer(&packet, static_lease_nip, lease, requested_nip, arpping_ms); break; case DHCPREQUEST: -- cgit v1.2.3-55-g6feb From a840884531df649aabc72debb2d6025dabe2abb3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 16 May 2019 11:18:49 +0200 Subject: udhcpd: support per-client hostnames in static leases function old new delta read_staticlease 222 299 +77 add_server_options 92 154 +62 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 139/0) Total: 139 bytes Signed-off-by: Denys Vlasenko --- examples/udhcp/udhcpd.conf | 9 ++++-- networking/udhcp/dhcpd.c | 72 ++++++++++++++++++++++++++++++++++++++++------ networking/udhcp/dhcpd.h | 6 +--- 3 files changed, 71 insertions(+), 16 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/examples/udhcp/udhcpd.conf b/examples/udhcp/udhcpd.conf index bb8774e08..df1258aaf 100644 --- a/examples/udhcp/udhcpd.conf +++ b/examples/udhcp/udhcpd.conf @@ -44,7 +44,7 @@ interface eth0 #notify_file # default: no script #notify_file dumpleases # useful for debugging -# The following are bootp specific options +# The following are BOOTP specific options # next server to use in bootstrap #siaddr 192.168.0.22 # default: 0.0.0.0 (none) # tftp server name @@ -52,9 +52,14 @@ interface eth0 # tftp file to download (e.g. kernel image) #boot_file /var/nfs_root # default: none +# NOTE: "boot_file FILE" and "opt bootfile FILE" are conceptually the same, +# but "boot_file" goes into BOOTP-defined fixed-size field in the packet, +# whereas "opt bootfile" goes into DHCP option 0x43. +# Same for "sname HOST" and "opt tftp HOST". + # Static leases map #static_lease 00:60:08:11:CE:4E 192.168.0.54 -#static_lease 00:60:08:11:CE:3E 192.168.0.44 +#static_lease 00:60:08:11:CE:3E 192.168.0.44 optional_hostname # The remainder of options are DHCP options and can be specified with the # keyword 'opt' or 'option'. If an option can take multiple items, such diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index bf44320a1..f231e4001 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -48,14 +48,23 @@ #define g_leases ((struct dyn_lease*)ptr_to_globals) /* struct server_config_t server_config is in bb_common_bufsiz1 */ +struct static_lease { + struct static_lease *next; + uint32_t nip; + uint8_t mac[6]; + uint8_t opt[1]; +}; + /* Takes the address of the pointer to the static_leases linked list, * address to a 6 byte mac address, * 4 byte IP address */ static void add_static_lease(struct static_lease **st_lease_pp, uint8_t *mac, - uint32_t nip) + uint32_t nip, + const char *opts) { struct static_lease *st_lease; + unsigned optlen; /* Find the tail of the list */ while ((st_lease = *st_lease_pp) != NULL) { @@ -63,10 +72,17 @@ static void add_static_lease(struct static_lease **st_lease_pp, } /* Add new node */ - *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease)); + optlen = (opts ? 1+1+strnlen(opts, 120) : 0); + *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease) + optlen); memcpy(st_lease->mac, mac, 6); st_lease->nip = nip; /*st_lease->next = NULL;*/ + if (optlen) { + st_lease->opt[OPT_CODE] = DHCP_HOST_NAME; + optlen -= 2; + st_lease->opt[OPT_LEN] = optlen; + memcpy(&st_lease->opt[OPT_DATA], opts, optlen); + } } /* Find static lease IP by mac */ @@ -344,6 +360,7 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg) char *line; char *mac_string; char *ip_string; + char *opts; struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */ uint32_t nip; @@ -358,7 +375,10 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg) if (!ip_string || !udhcp_str2nip(ip_string, &nip)) return 0; - add_static_lease(arg, (uint8_t*) &mac_bytes, nip); + opts = strtok_r(NULL, " \t", &line); + /* opts might be NULL, that's not an error */ + + add_static_lease(arg, (uint8_t*) &mac_bytes, nip, opts); log_static_leases(arg); @@ -626,14 +646,49 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke */ static void add_server_options(struct dhcp_packet *packet) { - struct option_set *curr = server_config.options; + struct option_set *config_opts; + uint8_t *client_hostname_opt; + + client_hostname_opt = NULL; + if (packet->yiaddr) { /* if we aren't from send_inform()... */ + struct static_lease *st_lease = server_config.static_leases; + while (st_lease) { + if (st_lease->nip == packet->yiaddr) { + if (st_lease->opt[0] != 0) + client_hostname_opt = st_lease->opt; + break; + } + st_lease = st_lease->next; + } + } - while (curr) { - if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) - udhcp_add_binary_option(packet, curr->data); - curr = curr->next; + config_opts = server_config.options; + while (config_opts) { + if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) { + /* ^^^^ + * DHCP_LEASE_TIME is already filled, or in case of + * send_inform(), should not be filled at all. + */ + if (config_opts->data[OPT_CODE] != DHCP_HOST_NAME + || !client_hostname_opt + ) { + /* Why "!client_hostname_opt": + * add hostname only if client has no hostname + * on its static lease line. + * (Not that "opt hostname HOST" + * makes much sense in udhcpd.conf, + * that'd give all clients the same hostname, + * but it's a valid configuration). + */ + udhcp_add_binary_option(packet, config_opts->data); + } + } + config_opts = config_opts->next; } + if (client_hostname_opt) + udhcp_add_binary_option(packet, client_hostname_opt); + packet->siaddr_nip = server_config.siaddr_nip; if (server_config.sname) @@ -753,7 +808,6 @@ static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) lease_time_sec = select_lease_time(oldpacket); udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); - add_server_options(&packet); addr.s_addr = yiaddr; diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h index b8f96b029..5c3bf5147 100644 --- a/networking/udhcp/dhcpd.h +++ b/networking/udhcp/dhcpd.h @@ -15,11 +15,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN #define DHCPD_CONF_FILE "/etc/udhcpd.conf" -struct static_lease { - struct static_lease *next; - uint32_t nip; - uint8_t mac[6]; -}; +struct static_lease; struct server_config_t { char *interface; /* interface to use */ -- cgit v1.2.3-55-g6feb From 25393fb55e515cab25bb932420813f2c5bbd18f9 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 16 May 2019 11:27:28 +0200 Subject: udhcpd: code shrink function old new delta send_packet_verbose - 35 +35 send_offer 443 423 -20 send_ACK 152 131 -21 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/2 up/down: 35/-41) Total: -6 bytes Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpd.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index f231e4001..3d60abf8f 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -627,6 +627,15 @@ static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) send_packet_to_client(dhcp_pkt, force_broadcast); } +static void send_packet_verbose(struct dhcp_packet *dhcp_pkt, const char *fmt) +{ + struct in_addr addr; + addr.s_addr = dhcp_pkt->yiaddr; + bb_info_msg(fmt, inet_ntoa(addr)); + /* send_packet emits error message itself if it detects failure */ + send_packet(dhcp_pkt, /*force_bcast:*/ 0); +} + static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) { /* Sets op, htype, hlen, cookie fields @@ -722,7 +731,6 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, { struct dhcp_packet packet; uint32_t lease_time_sec; - struct in_addr addr; init_packet(&packet, oldpacket, DHCPOFFER); @@ -778,10 +786,8 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); add_server_options(&packet); - addr.s_addr = packet.yiaddr; - bb_info_msg("sending OFFER of %s", inet_ntoa(addr)); /* send_packet emits error message itself if it detects failure */ - send_packet(&packet, /*force_bcast:*/ 0); + send_packet_verbose(&packet, "sending OFFER to %s"); } /* NOINLINE: limit stack usage in caller */ @@ -800,7 +806,6 @@ static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) { struct dhcp_packet packet; uint32_t lease_time_sec; - struct in_addr addr; const char *p_host_name; init_packet(&packet, oldpacket, DHCPACK); @@ -810,9 +815,7 @@ static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); add_server_options(&packet); - addr.s_addr = yiaddr; - bb_info_msg("sending ACK to %s", inet_ntoa(addr)); - send_packet(&packet, /*force_bcast:*/ 0); + send_packet_verbose(&packet, "sending ACK to %s"); p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); add_lease(packet.chaddr, packet.yiaddr, @@ -852,6 +855,7 @@ static NOINLINE void send_inform(struct dhcp_packet *oldpacket) add_server_options(&packet); send_packet(&packet, /*force_bcast:*/ 0); + // or maybe? send_packet_verbose(&packet, "sending ACK to %s"); } int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; -- cgit v1.2.3-55-g6feb From 831844c13931f14bd0433c1eb848c863288a8c06 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 21 May 2019 16:06:34 +0200 Subject: udhcpd: fix printing of static leases function old new delta read_staticlease 299 282 -17 Signed-off-by: Denys Vlasenko --- networking/udhcp/dhcpd.c | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) (limited to 'networking/udhcp/dhcpd.c') diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 3d60abf8f..058f86bca 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -66,13 +66,14 @@ static void add_static_lease(struct static_lease **st_lease_pp, struct static_lease *st_lease; unsigned optlen; + optlen = (opts ? 1+1+strnlen(opts, 120) : 0); + /* Find the tail of the list */ while ((st_lease = *st_lease_pp) != NULL) { st_lease_pp = &st_lease->next; } /* Add new node */ - optlen = (opts ? 1+1+strnlen(opts, 120) : 0); *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease) + optlen); memcpy(st_lease->mac, mac, 6); st_lease->nip = nip; @@ -83,6 +84,17 @@ static void add_static_lease(struct static_lease **st_lease_pp, st_lease->opt[OPT_LEN] = optlen; memcpy(&st_lease->opt[OPT_DATA], opts, optlen); } + +#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 + /* Print out static leases just to check what's going on */ + if (dhcp_verbose >= 2) { + bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", + st_lease->mac[0], st_lease->mac[1], st_lease->mac[2], + st_lease->mac[3], st_lease->mac[4], st_lease->mac[5], + st_lease->nip + ); + } +#endif } /* Find static lease IP by mac */ @@ -112,30 +124,6 @@ static int is_nip_reserved_as_static(uint32_t nip) return 0; } -#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 -/* Print out static leases just to check what's going on */ -/* Takes the address of the pointer to the static_leases linked list */ -static void log_static_leases(struct static_lease **st_lease_pp) -{ - struct static_lease *cur; - - if (dhcp_verbose < 2) - return; - - cur = *st_lease_pp; - while (cur) { - bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", - cur->mac[0], cur->mac[1], cur->mac[2], - cur->mac[3], cur->mac[4], cur->mac[5], - cur->nip - ); - cur = cur->next; - } -} -#else -# define log_static_leases(st_lease_pp) ((void)0) -#endif - /* Find the oldest expired lease, NULL if there are no expired leases */ static struct dyn_lease *oldest_expired_lease(void) { @@ -380,8 +368,6 @@ static int FAST_FUNC read_staticlease(const char *const_line, void *arg) add_static_lease(arg, (uint8_t*) &mac_bytes, nip, opts); - log_static_leases(arg); - return 1; } -- cgit v1.2.3-55-g6feb