From a4ed2c45b905057108e41aceda10c8b7976534ed Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 26 May 2019 13:05:04 +0200 Subject: dhcp: get rid of last global data function old new delta udhcpc_main 2680 2684 +4 state 1 - -1 listen_mode 1 - -1 sockfd 4 - -4 ------------------------------------------------------------------------------ (add/remove: 0/3 grow/shrink: 1/0 up/down: 4/-6) Total: -2 bytes Signed-off-by: Denys Vlasenko --- networking/udhcp/d6_dhcpc.c | 77 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'networking/udhcp/d6_dhcpc.c') diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index 6cc2316c4..1973af99b 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c @@ -903,13 +903,12 @@ static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6, struct d6_pac /*** Main ***/ -static int sockfd = -1; - +/* Values for client_config.listen_mode */ #define LISTEN_NONE 0 #define LISTEN_KERNEL 1 #define LISTEN_RAW 2 -static smallint listen_mode; +/* Values for client_config.state */ /* initial state: (re)start DHCP negotiation */ #define INIT_SELECTING 0 /* discover was sent, DHCPOFFER reply received */ @@ -924,7 +923,6 @@ static smallint listen_mode; #define RENEW_REQUESTED 5 /* release, possibly manually requested (SIGUSR2) */ #define RELEASED 6 -static smallint state; static int d6_raw_socket(int ifindex) { @@ -1018,35 +1016,35 @@ static void change_listen_mode(int new_mode) : "none" ); - listen_mode = new_mode; - if (sockfd >= 0) { - close(sockfd); - sockfd = -1; + client_config.listen_mode = new_mode; + if (client_config.sockfd >= 0) { + close(client_config.sockfd); + client_config.sockfd = -1; } if (new_mode == LISTEN_KERNEL) - sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface); + client_config.sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface); else if (new_mode != LISTEN_NONE) - sockfd = d6_raw_socket(client_config.ifindex); - /* else LISTEN_NONE: sockfd stays closed */ + client_config.sockfd = d6_raw_socket(client_config.ifindex); + /* else LISTEN_NONE: client_config.sockfd stays closed */ } /* Called only on SIGUSR1 */ static void perform_renew(void) { bb_info_msg("performing DHCP renew"); - switch (state) { + switch (client_config.state) { case BOUND: change_listen_mode(LISTEN_KERNEL); case RENEWING: case REBINDING: - state = RENEW_REQUESTED; + client_config.state = RENEW_REQUESTED; break; case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ d6_run_script_no_option("deconfig"); case REQUESTING: case RELEASED: change_listen_mode(LISTEN_RAW); - state = INIT_SELECTING; + client_config.state = INIT_SELECTING; break; case INIT_SELECTING: break; @@ -1056,10 +1054,10 @@ static void perform_renew(void) static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) { /* send release packet */ - if (state == BOUND - || state == RENEWING - || state == REBINDING - || state == RENEW_REQUESTED + if (client_config.state == BOUND + || client_config.state == RENEWING + || client_config.state == REBINDING + || client_config.state == RENEW_REQUESTED ) { bb_info_msg("unicasting a release"); send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */ @@ -1073,7 +1071,7 @@ static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *ou */ d6_run_script_no_option("deconfig"); change_listen_mode(LISTEN_NONE); - state = RELEASED; + client_config.state = RELEASED; } ///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) @@ -1174,6 +1172,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;) client_config.interface = "eth0"; client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT; + client_config.sockfd = -1; /* Parse command line */ opt = getopt32long(argv, "^" @@ -1278,7 +1277,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) /* Set up the signal pipe */ udhcp_sp_setup(); - state = INIT_SELECTING; + client_config.state = INIT_SELECTING; d6_run_script_no_option("deconfig"); change_listen_mode(LISTEN_RAW); packet_num = 0; @@ -1297,16 +1296,16 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) /* silence "uninitialized!" warning */ unsigned timestamp_before_wait = timestamp_before_wait; - //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode); + //bb_error_msg("sockfd:%d, listen_mode:%d", client_config.sockfd, client_config.listen_mode); /* Was opening raw or udp socket here - * if (listen_mode != LISTEN_NONE && sockfd < 0), + * if (client_config.listen_mode != LISTEN_NONE && client_config.sockfd < 0), * but on fast network renew responses return faster * than we open sockets. Thus this code is moved * to change_listen_mode(). Thus we open listen socket * BEFORE we send renew request (see "case BOUND:"). */ - udhcp_sp_fd_set(pfds, sockfd); + udhcp_sp_fd_set(pfds, client_config.sockfd); tv = timeout - already_waited_sec; retval = 0; @@ -1348,7 +1347,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) /* We will restart the wait in any case */ already_waited_sec = 0; - switch (state) { + switch (client_config.state) { case INIT_SELECTING: if (!discover_retries || packet_num < discover_retries) { if (packet_num == 0) @@ -1397,11 +1396,11 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) * were seen in the wild. Treat them similarly * to "no response to discover" case */ change_listen_mode(LISTEN_RAW); - state = INIT_SELECTING; + client_config.state = INIT_SELECTING; goto leasefail; case BOUND: /* 1/2 lease passed, enter renewing state */ - state = RENEWING; + client_config.state = RENEWING; client_config.first_secs = 0; /* make secs field count from 0 */ change_listen_mode(LISTEN_KERNEL); log1("entering renew state"); @@ -1425,7 +1424,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) } /* Timed out, enter rebinding state */ log1("entering rebinding state"); - state = REBINDING; + client_config.state = REBINDING; /* fall right through */ case REBINDING: /* Switch to bcast receive */ @@ -1441,7 +1440,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) /* Timed out, enter init state */ bb_info_msg("lease lost, entering init state"); d6_run_script_no_option("deconfig"); - state = INIT_SELECTING; + client_config.state = INIT_SELECTING; client_config.first_secs = 0; /* make secs field count from 0 */ /*timeout = 0; - already is */ packet_num = 0; @@ -1461,7 +1460,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) client_config.first_secs = 0; /* make secs field count from 0 */ already_waited_sec = 0; perform_renew(); - if (state == RENEW_REQUESTED) { + if (client_config.state == RENEW_REQUESTED) { /* We might be either on the same network * (in which case renew might work), * or we might be on a completely different one @@ -1496,15 +1495,15 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) int len; /* A packet is ready, read it */ - if (listen_mode == LISTEN_KERNEL) - len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd); + if (client_config.listen_mode == LISTEN_KERNEL) + len = d6_recv_kernel_packet(&srv6_buf, &packet, client_config.sockfd); else - len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd); + len = d6_recv_raw_packet(&srv6_buf, &packet, client_config.sockfd); if (len == -1) { /* Error is severe, reopen socket */ bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO); sleep(discover_timeout); /* 3 seconds by default */ - change_listen_mode(listen_mode); /* just close and reopen */ + change_listen_mode(client_config.listen_mode); /* just close and reopen */ } /* If this packet will turn out to be unrelated/bogus, * we will go back and wait for next one. @@ -1521,7 +1520,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) continue; } - switch (state) { + switch (client_config.state) { case INIT_SELECTING: if (packet.d6_msg_type == D6_MSG_ADVERTISE) goto type_is_ok; @@ -1547,11 +1546,11 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) bb_info_msg("received DHCP NAK (%u)", option->data[4]); d6_run_script(packet.d6_options, packet_end, "nak"); - if (state != REQUESTING) + if (client_config.state != REQUESTING) d6_run_script_no_option("deconfig"); change_listen_mode(LISTEN_RAW); sleep(3); /* avoid excessive network traffic */ - state = INIT_SELECTING; + client_config.state = INIT_SELECTING; client_config.first_secs = 0; /* make secs field count from 0 */ requested_ipv6 = NULL; timeout = 0; @@ -1572,7 +1571,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) client6_data.server_id = option; if (packet.d6_msg_type == D6_MSG_ADVERTISE) { /* enter requesting state */ - state = REQUESTING; + client_config.state = REQUESTING; timeout = 0; packet_num = 0; already_waited_sec = 0; @@ -1747,9 +1746,9 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) timeout = 61; /* enter bound state */ d6_run_script(packet.d6_options, packet_end, - (state == REQUESTING ? "bound" : "renew")); + (client_config.state == REQUESTING ? "bound" : "renew")); - state = BOUND; + client_config.state = BOUND; change_listen_mode(LISTEN_NONE); if (opt & OPT_q) { /* quit after lease */ goto ret0; -- cgit v1.2.3