diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-19 04:07:21 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-19 04:07:21 +0200 |
| commit | 13ca4b12e2c7d022a1dcb5a4d9923bb176fe69a7 (patch) | |
| tree | 3299bfdb88c697a5dabc623560fc2dcaea90e176 | |
| parent | 252ccba948eb9f6372de0ddf6d61654df91ab360 (diff) | |
| download | busybox-w32-13ca4b12e2c7d022a1dcb5a4d9923bb176fe69a7.tar.gz busybox-w32-13ca4b12e2c7d022a1dcb5a4d9923bb176fe69a7.tar.bz2 busybox-w32-13ca4b12e2c7d022a1dcb5a4d9923bb176fe69a7.zip | |
udhcpc: simplify manual renew (make it more similar to automatic one)
function old new delta
udhcp_run_script 669 673 +4
udhcpc_main 2414 2401 -13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 4/-13) Total: -9 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | networking/udhcp/dhcpc.c | 75 |
1 files changed, 38 insertions, 37 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 077098f42..8b6019bc9 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
| @@ -7,9 +7,7 @@ | |||
| 7 | * | 7 | * |
| 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
| 9 | */ | 9 | */ |
| 10 | |||
| 11 | #include <syslog.h> | 10 | #include <syslog.h> |
| 12 | |||
| 13 | /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ | 11 | /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ |
| 14 | #define WANT_PIDFILE 1 | 12 | #define WANT_PIDFILE 1 |
| 15 | #include "common.h" | 13 | #include "common.h" |
| @@ -25,14 +23,20 @@ static int sockfd = -1; | |||
| 25 | #define LISTEN_RAW 2 | 23 | #define LISTEN_RAW 2 |
| 26 | static smallint listen_mode; | 24 | static smallint listen_mode; |
| 27 | 25 | ||
| 26 | /* initial state: (re)start DHCP negotiation */ | ||
| 28 | #define INIT_SELECTING 0 | 27 | #define INIT_SELECTING 0 |
| 28 | /* discover was sent, DHCPOFFER reply received */ | ||
| 29 | #define REQUESTING 1 | 29 | #define REQUESTING 1 |
| 30 | /* select/renew was sent, DHCPACK reply received */ | ||
| 30 | #define BOUND 2 | 31 | #define BOUND 2 |
| 32 | /* half of lease passed, want renew it by sending unicast renew requests */ | ||
| 31 | #define RENEWING 3 | 33 | #define RENEWING 3 |
| 34 | /* renew requests were not answered, lease is almost over, send broadcast renew */ | ||
| 32 | #define REBINDING 4 | 35 | #define REBINDING 4 |
| 33 | #define INIT_REBOOT 5 | 36 | /* manually requested renew (SIGUSR1) */ |
| 34 | #define RENEW_REQUESTED 6 | 37 | #define RENEW_REQUESTED 5 |
| 35 | #define RELEASED 7 | 38 | /* release, possibly manually requested (SIGUSR2) */ |
| 39 | #define RELEASED 6 | ||
| 36 | static smallint state; | 40 | static smallint state; |
| 37 | 41 | ||
| 38 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ | 42 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ |
| @@ -42,7 +46,10 @@ static smallint state; | |||
| 42 | static void change_listen_mode(int new_mode) | 46 | static void change_listen_mode(int new_mode) |
| 43 | { | 47 | { |
| 44 | log1("Entering listen mode: %s", | 48 | log1("Entering listen mode: %s", |
| 45 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); | 49 | new_mode != LISTEN_NONE |
| 50 | ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw") | ||
| 51 | : "none" | ||
| 52 | ); | ||
| 46 | 53 | ||
| 47 | listen_mode = new_mode; | 54 | listen_mode = new_mode; |
| 48 | if (sockfd >= 0) { | 55 | if (sockfd >= 0) { |
| @@ -349,14 +356,16 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 349 | tv.tv_sec = timeout - already_waited_sec; | 356 | tv.tv_sec = timeout - already_waited_sec; |
| 350 | tv.tv_usec = 0; | 357 | tv.tv_usec = 0; |
| 351 | retval = 0; /* If we already timed out, fall through, else... */ | 358 | retval = 0; /* If we already timed out, fall through, else... */ |
| 352 | if (tv.tv_sec > 0) { | 359 | if ((int)tv.tv_sec > 0) { |
| 353 | timestamp_before_wait = (unsigned)monotonic_sec(); | 360 | timestamp_before_wait = (unsigned)monotonic_sec(); |
| 354 | log1("Waiting on select..."); | 361 | log1("Waiting on select..."); |
| 355 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); | 362 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
| 356 | if (retval < 0) { | 363 | if (retval < 0) { |
| 357 | /* EINTR? A signal was caught, don't panic */ | 364 | /* EINTR? A signal was caught, don't panic */ |
| 358 | if (errno == EINTR) | 365 | if (errno == EINTR) { |
| 366 | already_waited_sec += (unsigned)monotonic_sec() - timestamp_before_wait; | ||
| 359 | continue; | 367 | continue; |
| 368 | } | ||
| 360 | /* Else: an error occured, panic! */ | 369 | /* Else: an error occured, panic! */ |
| 361 | bb_perror_msg_and_die("select"); | 370 | bb_perror_msg_and_die("select"); |
| 362 | } | 371 | } |
| @@ -374,9 +383,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 374 | if (packet_num < discover_retries) { | 383 | if (packet_num < discover_retries) { |
| 375 | if (packet_num == 0) | 384 | if (packet_num == 0) |
| 376 | xid = random_xid(); | 385 | xid = random_xid(); |
| 377 | 386 | /* broadcast */ | |
| 378 | send_discover(xid, requested_ip); /* broadcast */ | 387 | send_discover(xid, requested_ip); |
| 379 | |||
| 380 | timeout = discover_timeout; | 388 | timeout = discover_timeout; |
| 381 | packet_num++; | 389 | packet_num++; |
| 382 | continue; | 390 | continue; |
| @@ -400,44 +408,33 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 400 | timeout = tryagain_timeout; | 408 | timeout = tryagain_timeout; |
| 401 | packet_num = 0; | 409 | packet_num = 0; |
| 402 | continue; | 410 | continue; |
| 403 | case RENEW_REQUESTED: | ||
| 404 | case REQUESTING: | 411 | case REQUESTING: |
| 405 | if (packet_num < discover_retries) { | 412 | if (packet_num < discover_retries) { |
| 406 | /* send request packet */ | 413 | /* send broadcast select packet */ |
| 407 | if (state == RENEW_REQUESTED) /* unicast */ | 414 | send_select(xid, server_addr, requested_ip); |
| 408 | send_renew(xid, server_addr, requested_ip); | ||
| 409 | else /* broadcast */ | ||
| 410 | send_select(xid, server_addr, requested_ip); | ||
| 411 | |||
| 412 | timeout = discover_timeout; | 415 | timeout = discover_timeout; |
| 413 | packet_num++; | 416 | packet_num++; |
| 414 | continue; | 417 | continue; |
| 415 | } | 418 | } |
| 416 | /* timed out, go back to init state */ | 419 | /* Timed out, go back to init state. |
| 417 | if (state == RENEW_REQUESTED) | 420 | * "discover...select...discover..." loops |
| 418 | udhcp_run_script(NULL, "deconfig"); | ||
| 419 | change_listen_mode(LISTEN_RAW); | ||
| 420 | /* "discover...select...discover..." loops | ||
| 421 | * were seen in the wild. Treat them similarly | 421 | * were seen in the wild. Treat them similarly |
| 422 | * to "no response to discover" case */ | 422 | * to "no response to discover" case */ |
| 423 | if (state == REQUESTING) { | 423 | change_listen_mode(LISTEN_RAW); |
| 424 | state = INIT_SELECTING; | ||
| 425 | goto leasefail; | ||
| 426 | } | ||
| 427 | state = INIT_SELECTING; | 424 | state = INIT_SELECTING; |
| 428 | timeout = 0; | 425 | goto leasefail; |
| 429 | packet_num = 0; | ||
| 430 | continue; | ||
| 431 | case BOUND: | 426 | case BOUND: |
| 432 | /* Half of the lease passed, time to enter renewing state */ | 427 | /* Half of the lease passed, time to enter renewing state */ |
| 428 | state = RENEWING; | ||
| 433 | change_listen_mode(LISTEN_KERNEL); | 429 | change_listen_mode(LISTEN_KERNEL); |
| 434 | log1("Entering renew state"); | 430 | log1("Entering renew state"); |
| 435 | state = RENEWING; | ||
| 436 | /* fall right through */ | 431 | /* fall right through */ |
| 432 | case RENEW_REQUESTED: /* manual (SIGUSR1) renew */ | ||
| 433 | case_RENEW_REQUESTED: | ||
| 437 | case RENEWING: | 434 | case RENEWING: |
| 438 | if (timeout > 60) { | 435 | if (timeout > 60) { |
| 439 | /* send a request packet */ | 436 | /* send an unicast renew request packet */ |
| 440 | send_renew(xid, server_addr, requested_ip); /* unicast */ | 437 | send_renew(xid, server_addr, requested_ip); |
| 441 | timeout >>= 1; | 438 | timeout >>= 1; |
| 442 | continue; | 439 | continue; |
| 443 | } | 440 | } |
| @@ -449,8 +446,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 449 | /* Lease is *really* about to run out, | 446 | /* Lease is *really* about to run out, |
| 450 | * try to find DHCP server using broadcast */ | 447 | * try to find DHCP server using broadcast */ |
| 451 | if (timeout > 0) { | 448 | if (timeout > 0) { |
| 452 | /* send a request packet */ | 449 | /* send a broadcast renew request packet */ |
| 453 | send_renew(xid, 0 /*INADDR_ANY*/, requested_ip); /* broadcast */ | 450 | send_renew(xid, 0 /*INADDR_ANY*/, requested_ip); |
| 454 | timeout >>= 1; | 451 | timeout >>= 1; |
| 455 | continue; | 452 | continue; |
| 456 | } | 453 | } |
| @@ -476,6 +473,8 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 476 | switch (udhcp_sp_read(&rfds)) { | 473 | switch (udhcp_sp_read(&rfds)) { |
| 477 | case SIGUSR1: | 474 | case SIGUSR1: |
| 478 | perform_renew(); | 475 | perform_renew(); |
| 476 | if (state == RENEW_REQUESTED) | ||
| 477 | goto case_RENEW_REQUESTED; | ||
| 479 | /* Start things over */ | 478 | /* Start things over */ |
| 480 | packet_num = 0; | 479 | packet_num = 0; |
| 481 | /* Kill any timeouts because the user wants this to hurry along */ | 480 | /* Kill any timeouts because the user wants this to hurry along */ |
| @@ -562,9 +561,9 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 562 | already_waited_sec = 0; | 561 | already_waited_sec = 0; |
| 563 | } | 562 | } |
| 564 | continue; | 563 | continue; |
| 565 | case RENEW_REQUESTED: | ||
| 566 | case REQUESTING: | 564 | case REQUESTING: |
| 567 | case RENEWING: | 565 | case RENEWING: |
| 566 | case RENEW_REQUESTED: | ||
| 568 | case REBINDING: | 567 | case REBINDING: |
| 569 | if (*message == DHCPACK) { | 568 | if (*message == DHCPACK) { |
| 570 | temp = get_option(&packet, DHCP_LEASE_TIME); | 569 | temp = get_option(&packet, DHCP_LEASE_TIME); |
| @@ -622,7 +621,9 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) | |||
| 622 | } | 621 | } |
| 623 | requested_ip = packet.yiaddr; | 622 | requested_ip = packet.yiaddr; |
| 624 | udhcp_run_script(&packet, | 623 | udhcp_run_script(&packet, |
| 625 | ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); | 624 | ((state >= RENEWING && state <= RENEW_REQUESTED) |
| 625 | ? "renew" : "bound") | ||
| 626 | ); | ||
| 626 | 627 | ||
| 627 | state = BOUND; | 628 | state = BOUND; |
| 628 | change_listen_mode(LISTEN_NONE); | 629 | change_listen_mode(LISTEN_NONE); |
