diff options
author | Denys Vlasenko | 2010-05-30 23:41:23 +0200 |
---|---|---|
committer | Denys Vlasenko | 2010-05-30 23:41:23 +0200 |
commit | b7d19cc400f14ccd64a1fedebe14022fe115029a (patch) | |
tree | cd4a50e2694628413a2460d3e1d2a706462d0e2b /networking/udhcp/dhcpc.c | |
parent | c88c1a01904b5791dfd1ca5342c805a9f392e17f (diff) | |
download | busybox-b7d19cc400f14ccd64a1fedebe14022fe115029a.zip busybox-b7d19cc400f14ccd64a1fedebe14022fe115029a.tar.gz |
dhcp: readability cleanups and small code shrink
function old new delta
udhcp_run_script 654 617 -37
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp/dhcpc.c')
-rw-r--r-- | networking/udhcp/dhcpc.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 24ff82d..c2b21c6 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -229,37 +229,41 @@ static NOINLINE char *xmalloc_optname_optval(uint8_t *option, const struct dhcp_ /* put all the parameters into the environment */ static char **fill_envp(struct dhcp_packet *packet) { - int num_options = 0; + int envc; int i; char **envp, **curr; const char *opt_name; uint8_t *temp; - uint8_t over = 0; - + uint8_t overload = 0; + + /* We need 6 elements for: + * "interface=IFACE" + * "ip=N.N.N.N" from packet->yiaddr + * "siaddr=IP" from packet->siaddr_nip (unless 0) + * "boot_file=FILE" from packet->file (unless overloaded) + * "sname=SERVER_HOSTNAME" from packet->sname (unless overloaded) + * terminating NULL + */ + envc = 6; + /* +1 element for each option, +2 for subnet option: */ if (packet) { for (i = 0; dhcp_optflags[i].code; i++) { if (udhcp_get_option(packet, dhcp_optflags[i].code)) { - num_options++; if (dhcp_optflags[i].code == DHCP_SUBNET) - num_options++; /* for mton */ + envc++; /* for mton */ + envc++; } } - if (packet->siaddr_nip) - num_options++; temp = udhcp_get_option(packet, DHCP_OPTION_OVERLOAD); if (temp) - over = *temp; - if (!(over & FILE_FIELD) && packet->file[0]) - num_options++; - if (!(over & SNAME_FIELD) && packet->sname[0]) - num_options++; + overload = *temp; } + curr = envp = xzalloc(sizeof(char *) * envc); - curr = envp = xzalloc(sizeof(char *) * (num_options + 3)); *curr = xasprintf("interface=%s", client_config.interface); putenv(*curr++); - if (packet == NULL) + if (!packet) return envp; *curr = xmalloc(sizeof("ip=255.255.255.255")); @@ -274,9 +278,8 @@ static char **fill_envp(struct dhcp_packet *packet) goto next; *curr = xmalloc_optname_optval(temp, &dhcp_optflags[i], opt_name); putenv(*curr++); - - /* Fill in a subnet bits option for things like /24 */ if (dhcp_optflags[i].code == DHCP_SUBNET) { + /* Subnet option: make things like "$ip/$mask" possible */ uint32_t subnet; move_from_unaligned32(subnet, temp); *curr = xasprintf("mask=%d", mton(subnet)); @@ -291,12 +294,12 @@ static char **fill_envp(struct dhcp_packet *packet) sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip); putenv(*curr++); } - if (!(over & FILE_FIELD) && packet->file[0]) { + if (!(overload & FILE_FIELD) && packet->file[0]) { /* watch out for invalid packets */ *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file); putenv(*curr++); } - if (!(over & SNAME_FIELD) && packet->sname[0]) { + if (!(overload & SNAME_FIELD) && packet->sname[0]) { /* watch out for invalid packets */ *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname); putenv(*curr++); |