diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-30 23:41:23 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-30 23:41:23 +0200 |
commit | b7d19cc400f14ccd64a1fedebe14022fe115029a (patch) | |
tree | cd4a50e2694628413a2460d3e1d2a706462d0e2b /networking/udhcp/dhcpc.c | |
parent | c88c1a01904b5791dfd1ca5342c805a9f392e17f (diff) | |
download | busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.tar.gz busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.tar.bz2 busybox-w32-b7d19cc400f14ccd64a1fedebe14022fe115029a.zip |
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 24ff82d2f..c2b21c695 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_ | |||
229 | /* put all the parameters into the environment */ | 229 | /* put all the parameters into the environment */ |
230 | static char **fill_envp(struct dhcp_packet *packet) | 230 | static char **fill_envp(struct dhcp_packet *packet) |
231 | { | 231 | { |
232 | int num_options = 0; | 232 | int envc; |
233 | int i; | 233 | int i; |
234 | char **envp, **curr; | 234 | char **envp, **curr; |
235 | const char *opt_name; | 235 | const char *opt_name; |
236 | uint8_t *temp; | 236 | uint8_t *temp; |
237 | uint8_t over = 0; | 237 | uint8_t overload = 0; |
238 | 238 | ||
239 | /* We need 6 elements for: | ||
240 | * "interface=IFACE" | ||
241 | * "ip=N.N.N.N" from packet->yiaddr | ||
242 | * "siaddr=IP" from packet->siaddr_nip (unless 0) | ||
243 | * "boot_file=FILE" from packet->file (unless overloaded) | ||
244 | * "sname=SERVER_HOSTNAME" from packet->sname (unless overloaded) | ||
245 | * terminating NULL | ||
246 | */ | ||
247 | envc = 6; | ||
248 | /* +1 element for each option, +2 for subnet option: */ | ||
239 | if (packet) { | 249 | if (packet) { |
240 | for (i = 0; dhcp_optflags[i].code; i++) { | 250 | for (i = 0; dhcp_optflags[i].code; i++) { |
241 | if (udhcp_get_option(packet, dhcp_optflags[i].code)) { | 251 | if (udhcp_get_option(packet, dhcp_optflags[i].code)) { |
242 | num_options++; | ||
243 | if (dhcp_optflags[i].code == DHCP_SUBNET) | 252 | if (dhcp_optflags[i].code == DHCP_SUBNET) |
244 | num_options++; /* for mton */ | 253 | envc++; /* for mton */ |
254 | envc++; | ||
245 | } | 255 | } |
246 | } | 256 | } |
247 | if (packet->siaddr_nip) | ||
248 | num_options++; | ||
249 | temp = udhcp_get_option(packet, DHCP_OPTION_OVERLOAD); | 257 | temp = udhcp_get_option(packet, DHCP_OPTION_OVERLOAD); |
250 | if (temp) | 258 | if (temp) |
251 | over = *temp; | 259 | overload = *temp; |
252 | if (!(over & FILE_FIELD) && packet->file[0]) | ||
253 | num_options++; | ||
254 | if (!(over & SNAME_FIELD) && packet->sname[0]) | ||
255 | num_options++; | ||
256 | } | 260 | } |
261 | curr = envp = xzalloc(sizeof(char *) * envc); | ||
257 | 262 | ||
258 | curr = envp = xzalloc(sizeof(char *) * (num_options + 3)); | ||
259 | *curr = xasprintf("interface=%s", client_config.interface); | 263 | *curr = xasprintf("interface=%s", client_config.interface); |
260 | putenv(*curr++); | 264 | putenv(*curr++); |
261 | 265 | ||
262 | if (packet == NULL) | 266 | if (!packet) |
263 | return envp; | 267 | return envp; |
264 | 268 | ||
265 | *curr = xmalloc(sizeof("ip=255.255.255.255")); | 269 | *curr = xmalloc(sizeof("ip=255.255.255.255")); |
@@ -274,9 +278,8 @@ static char **fill_envp(struct dhcp_packet *packet) | |||
274 | goto next; | 278 | goto next; |
275 | *curr = xmalloc_optname_optval(temp, &dhcp_optflags[i], opt_name); | 279 | *curr = xmalloc_optname_optval(temp, &dhcp_optflags[i], opt_name); |
276 | putenv(*curr++); | 280 | putenv(*curr++); |
277 | |||
278 | /* Fill in a subnet bits option for things like /24 */ | ||
279 | if (dhcp_optflags[i].code == DHCP_SUBNET) { | 281 | if (dhcp_optflags[i].code == DHCP_SUBNET) { |
282 | /* Subnet option: make things like "$ip/$mask" possible */ | ||
280 | uint32_t subnet; | 283 | uint32_t subnet; |
281 | move_from_unaligned32(subnet, temp); | 284 | move_from_unaligned32(subnet, temp); |
282 | *curr = xasprintf("mask=%d", mton(subnet)); | 285 | *curr = xasprintf("mask=%d", mton(subnet)); |
@@ -291,12 +294,12 @@ static char **fill_envp(struct dhcp_packet *packet) | |||
291 | sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip); | 294 | sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip); |
292 | putenv(*curr++); | 295 | putenv(*curr++); |
293 | } | 296 | } |
294 | if (!(over & FILE_FIELD) && packet->file[0]) { | 297 | if (!(overload & FILE_FIELD) && packet->file[0]) { |
295 | /* watch out for invalid packets */ | 298 | /* watch out for invalid packets */ |
296 | *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file); | 299 | *curr = xasprintf("boot_file=%."DHCP_PKT_FILE_LEN_STR"s", packet->file); |
297 | putenv(*curr++); | 300 | putenv(*curr++); |
298 | } | 301 | } |
299 | if (!(over & SNAME_FIELD) && packet->sname[0]) { | 302 | if (!(overload & SNAME_FIELD) && packet->sname[0]) { |
300 | /* watch out for invalid packets */ | 303 | /* watch out for invalid packets */ |
301 | *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname); | 304 | *curr = xasprintf("sname=%."DHCP_PKT_SNAME_LEN_STR"s", packet->sname); |
302 | putenv(*curr++); | 305 | putenv(*curr++); |