aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/d6_dhcpc.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-05-26 13:05:04 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-05-26 13:05:04 +0200
commita4ed2c45b905057108e41aceda10c8b7976534ed (patch)
treec7b93e7ba4499805f3ebcb5b92a21ca33d6c0770 /networking/udhcp/d6_dhcpc.c
parentdff2bd733fc2dac08d34f2cfad0e68aeb8e7a7a2 (diff)
downloadbusybox-w32-a4ed2c45b905057108e41aceda10c8b7976534ed.tar.gz
busybox-w32-a4ed2c45b905057108e41aceda10c8b7976534ed.tar.bz2
busybox-w32-a4ed2c45b905057108e41aceda10c8b7976534ed.zip
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 <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp/d6_dhcpc.c')
-rw-r--r--networking/udhcp/d6_dhcpc.c77
1 files changed, 38 insertions, 39 deletions
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
903 903
904/*** Main ***/ 904/*** Main ***/
905 905
906static int sockfd = -1; 906/* Values for client_config.listen_mode */
907
908#define LISTEN_NONE 0 907#define LISTEN_NONE 0
909#define LISTEN_KERNEL 1 908#define LISTEN_KERNEL 1
910#define LISTEN_RAW 2 909#define LISTEN_RAW 2
911static smallint listen_mode;
912 910
911/* Values for client_config.state */
913/* initial state: (re)start DHCP negotiation */ 912/* initial state: (re)start DHCP negotiation */
914#define INIT_SELECTING 0 913#define INIT_SELECTING 0
915/* discover was sent, DHCPOFFER reply received */ 914/* discover was sent, DHCPOFFER reply received */
@@ -924,7 +923,6 @@ static smallint listen_mode;
924#define RENEW_REQUESTED 5 923#define RENEW_REQUESTED 5
925/* release, possibly manually requested (SIGUSR2) */ 924/* release, possibly manually requested (SIGUSR2) */
926#define RELEASED 6 925#define RELEASED 6
927static smallint state;
928 926
929static int d6_raw_socket(int ifindex) 927static int d6_raw_socket(int ifindex)
930{ 928{
@@ -1018,35 +1016,35 @@ static void change_listen_mode(int new_mode)
1018 : "none" 1016 : "none"
1019 ); 1017 );
1020 1018
1021 listen_mode = new_mode; 1019 client_config.listen_mode = new_mode;
1022 if (sockfd >= 0) { 1020 if (client_config.sockfd >= 0) {
1023 close(sockfd); 1021 close(client_config.sockfd);
1024 sockfd = -1; 1022 client_config.sockfd = -1;
1025 } 1023 }
1026 if (new_mode == LISTEN_KERNEL) 1024 if (new_mode == LISTEN_KERNEL)
1027 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface); 1025 client_config.sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT6, client_config.interface);
1028 else if (new_mode != LISTEN_NONE) 1026 else if (new_mode != LISTEN_NONE)
1029 sockfd = d6_raw_socket(client_config.ifindex); 1027 client_config.sockfd = d6_raw_socket(client_config.ifindex);
1030 /* else LISTEN_NONE: sockfd stays closed */ 1028 /* else LISTEN_NONE: client_config.sockfd stays closed */
1031} 1029}
1032 1030
1033/* Called only on SIGUSR1 */ 1031/* Called only on SIGUSR1 */
1034static void perform_renew(void) 1032static void perform_renew(void)
1035{ 1033{
1036 bb_info_msg("performing DHCP renew"); 1034 bb_info_msg("performing DHCP renew");
1037 switch (state) { 1035 switch (client_config.state) {
1038 case BOUND: 1036 case BOUND:
1039 change_listen_mode(LISTEN_KERNEL); 1037 change_listen_mode(LISTEN_KERNEL);
1040 case RENEWING: 1038 case RENEWING:
1041 case REBINDING: 1039 case REBINDING:
1042 state = RENEW_REQUESTED; 1040 client_config.state = RENEW_REQUESTED;
1043 break; 1041 break;
1044 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ 1042 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
1045 d6_run_script_no_option("deconfig"); 1043 d6_run_script_no_option("deconfig");
1046 case REQUESTING: 1044 case REQUESTING:
1047 case RELEASED: 1045 case RELEASED:
1048 change_listen_mode(LISTEN_RAW); 1046 change_listen_mode(LISTEN_RAW);
1049 state = INIT_SELECTING; 1047 client_config.state = INIT_SELECTING;
1050 break; 1048 break;
1051 case INIT_SELECTING: 1049 case INIT_SELECTING:
1052 break; 1050 break;
@@ -1056,10 +1054,10 @@ static void perform_renew(void)
1056static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) 1054static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6)
1057{ 1055{
1058 /* send release packet */ 1056 /* send release packet */
1059 if (state == BOUND 1057 if (client_config.state == BOUND
1060 || state == RENEWING 1058 || client_config.state == RENEWING
1061 || state == REBINDING 1059 || client_config.state == REBINDING
1062 || state == RENEW_REQUESTED 1060 || client_config.state == RENEW_REQUESTED
1063 ) { 1061 ) {
1064 bb_info_msg("unicasting a release"); 1062 bb_info_msg("unicasting a release");
1065 send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */ 1063 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
1073 */ 1071 */
1074 d6_run_script_no_option("deconfig"); 1072 d6_run_script_no_option("deconfig");
1075 change_listen_mode(LISTEN_NONE); 1073 change_listen_mode(LISTEN_NONE);
1076 state = RELEASED; 1074 client_config.state = RELEASED;
1077} 1075}
1078 1076
1079///static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) 1077///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)
1174 IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;) 1172 IF_FEATURE_UDHCP_PORT(CLIENT_PORT6 = 546;)
1175 client_config.interface = "eth0"; 1173 client_config.interface = "eth0";
1176 client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT; 1174 client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
1175 client_config.sockfd = -1;
1177 1176
1178 /* Parse command line */ 1177 /* Parse command line */
1179 opt = getopt32long(argv, "^" 1178 opt = getopt32long(argv, "^"
@@ -1278,7 +1277,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1278 /* Set up the signal pipe */ 1277 /* Set up the signal pipe */
1279 udhcp_sp_setup(); 1278 udhcp_sp_setup();
1280 1279
1281 state = INIT_SELECTING; 1280 client_config.state = INIT_SELECTING;
1282 d6_run_script_no_option("deconfig"); 1281 d6_run_script_no_option("deconfig");
1283 change_listen_mode(LISTEN_RAW); 1282 change_listen_mode(LISTEN_RAW);
1284 packet_num = 0; 1283 packet_num = 0;
@@ -1297,16 +1296,16 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1297 /* silence "uninitialized!" warning */ 1296 /* silence "uninitialized!" warning */
1298 unsigned timestamp_before_wait = timestamp_before_wait; 1297 unsigned timestamp_before_wait = timestamp_before_wait;
1299 1298
1300 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode); 1299 //bb_error_msg("sockfd:%d, listen_mode:%d", client_config.sockfd, client_config.listen_mode);
1301 1300
1302 /* Was opening raw or udp socket here 1301 /* Was opening raw or udp socket here
1303 * if (listen_mode != LISTEN_NONE && sockfd < 0), 1302 * if (client_config.listen_mode != LISTEN_NONE && client_config.sockfd < 0),
1304 * but on fast network renew responses return faster 1303 * but on fast network renew responses return faster
1305 * than we open sockets. Thus this code is moved 1304 * than we open sockets. Thus this code is moved
1306 * to change_listen_mode(). Thus we open listen socket 1305 * to change_listen_mode(). Thus we open listen socket
1307 * BEFORE we send renew request (see "case BOUND:"). */ 1306 * BEFORE we send renew request (see "case BOUND:"). */
1308 1307
1309 udhcp_sp_fd_set(pfds, sockfd); 1308 udhcp_sp_fd_set(pfds, client_config.sockfd);
1310 1309
1311 tv = timeout - already_waited_sec; 1310 tv = timeout - already_waited_sec;
1312 retval = 0; 1311 retval = 0;
@@ -1348,7 +1347,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1348 /* We will restart the wait in any case */ 1347 /* We will restart the wait in any case */
1349 already_waited_sec = 0; 1348 already_waited_sec = 0;
1350 1349
1351 switch (state) { 1350 switch (client_config.state) {
1352 case INIT_SELECTING: 1351 case INIT_SELECTING:
1353 if (!discover_retries || packet_num < discover_retries) { 1352 if (!discover_retries || packet_num < discover_retries) {
1354 if (packet_num == 0) 1353 if (packet_num == 0)
@@ -1397,11 +1396,11 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1397 * were seen in the wild. Treat them similarly 1396 * were seen in the wild. Treat them similarly
1398 * to "no response to discover" case */ 1397 * to "no response to discover" case */
1399 change_listen_mode(LISTEN_RAW); 1398 change_listen_mode(LISTEN_RAW);
1400 state = INIT_SELECTING; 1399 client_config.state = INIT_SELECTING;
1401 goto leasefail; 1400 goto leasefail;
1402 case BOUND: 1401 case BOUND:
1403 /* 1/2 lease passed, enter renewing state */ 1402 /* 1/2 lease passed, enter renewing state */
1404 state = RENEWING; 1403 client_config.state = RENEWING;
1405 client_config.first_secs = 0; /* make secs field count from 0 */ 1404 client_config.first_secs = 0; /* make secs field count from 0 */
1406 change_listen_mode(LISTEN_KERNEL); 1405 change_listen_mode(LISTEN_KERNEL);
1407 log1("entering renew state"); 1406 log1("entering renew state");
@@ -1425,7 +1424,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1425 } 1424 }
1426 /* Timed out, enter rebinding state */ 1425 /* Timed out, enter rebinding state */
1427 log1("entering rebinding state"); 1426 log1("entering rebinding state");
1428 state = REBINDING; 1427 client_config.state = REBINDING;
1429 /* fall right through */ 1428 /* fall right through */
1430 case REBINDING: 1429 case REBINDING:
1431 /* Switch to bcast receive */ 1430 /* Switch to bcast receive */
@@ -1441,7 +1440,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1441 /* Timed out, enter init state */ 1440 /* Timed out, enter init state */
1442 bb_info_msg("lease lost, entering init state"); 1441 bb_info_msg("lease lost, entering init state");
1443 d6_run_script_no_option("deconfig"); 1442 d6_run_script_no_option("deconfig");
1444 state = INIT_SELECTING; 1443 client_config.state = INIT_SELECTING;
1445 client_config.first_secs = 0; /* make secs field count from 0 */ 1444 client_config.first_secs = 0; /* make secs field count from 0 */
1446 /*timeout = 0; - already is */ 1445 /*timeout = 0; - already is */
1447 packet_num = 0; 1446 packet_num = 0;
@@ -1461,7 +1460,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1461 client_config.first_secs = 0; /* make secs field count from 0 */ 1460 client_config.first_secs = 0; /* make secs field count from 0 */
1462 already_waited_sec = 0; 1461 already_waited_sec = 0;
1463 perform_renew(); 1462 perform_renew();
1464 if (state == RENEW_REQUESTED) { 1463 if (client_config.state == RENEW_REQUESTED) {
1465 /* We might be either on the same network 1464 /* We might be either on the same network
1466 * (in which case renew might work), 1465 * (in which case renew might work),
1467 * or we might be on a completely different one 1466 * or we might be on a completely different one
@@ -1496,15 +1495,15 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1496 int len; 1495 int len;
1497 1496
1498 /* A packet is ready, read it */ 1497 /* A packet is ready, read it */
1499 if (listen_mode == LISTEN_KERNEL) 1498 if (client_config.listen_mode == LISTEN_KERNEL)
1500 len = d6_recv_kernel_packet(&srv6_buf, &packet, sockfd); 1499 len = d6_recv_kernel_packet(&srv6_buf, &packet, client_config.sockfd);
1501 else 1500 else
1502 len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd); 1501 len = d6_recv_raw_packet(&srv6_buf, &packet, client_config.sockfd);
1503 if (len == -1) { 1502 if (len == -1) {
1504 /* Error is severe, reopen socket */ 1503 /* Error is severe, reopen socket */
1505 bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO); 1504 bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
1506 sleep(discover_timeout); /* 3 seconds by default */ 1505 sleep(discover_timeout); /* 3 seconds by default */
1507 change_listen_mode(listen_mode); /* just close and reopen */ 1506 change_listen_mode(client_config.listen_mode); /* just close and reopen */
1508 } 1507 }
1509 /* If this packet will turn out to be unrelated/bogus, 1508 /* If this packet will turn out to be unrelated/bogus,
1510 * we will go back and wait for next one. 1509 * we will go back and wait for next one.
@@ -1521,7 +1520,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1521 continue; 1520 continue;
1522 } 1521 }
1523 1522
1524 switch (state) { 1523 switch (client_config.state) {
1525 case INIT_SELECTING: 1524 case INIT_SELECTING:
1526 if (packet.d6_msg_type == D6_MSG_ADVERTISE) 1525 if (packet.d6_msg_type == D6_MSG_ADVERTISE)
1527 goto type_is_ok; 1526 goto type_is_ok;
@@ -1547,11 +1546,11 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1547 bb_info_msg("received DHCP NAK (%u)", option->data[4]); 1546 bb_info_msg("received DHCP NAK (%u)", option->data[4]);
1548 d6_run_script(packet.d6_options, 1547 d6_run_script(packet.d6_options,
1549 packet_end, "nak"); 1548 packet_end, "nak");
1550 if (state != REQUESTING) 1549 if (client_config.state != REQUESTING)
1551 d6_run_script_no_option("deconfig"); 1550 d6_run_script_no_option("deconfig");
1552 change_listen_mode(LISTEN_RAW); 1551 change_listen_mode(LISTEN_RAW);
1553 sleep(3); /* avoid excessive network traffic */ 1552 sleep(3); /* avoid excessive network traffic */
1554 state = INIT_SELECTING; 1553 client_config.state = INIT_SELECTING;
1555 client_config.first_secs = 0; /* make secs field count from 0 */ 1554 client_config.first_secs = 0; /* make secs field count from 0 */
1556 requested_ipv6 = NULL; 1555 requested_ipv6 = NULL;
1557 timeout = 0; 1556 timeout = 0;
@@ -1572,7 +1571,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1572 client6_data.server_id = option; 1571 client6_data.server_id = option;
1573 if (packet.d6_msg_type == D6_MSG_ADVERTISE) { 1572 if (packet.d6_msg_type == D6_MSG_ADVERTISE) {
1574 /* enter requesting state */ 1573 /* enter requesting state */
1575 state = REQUESTING; 1574 client_config.state = REQUESTING;
1576 timeout = 0; 1575 timeout = 0;
1577 packet_num = 0; 1576 packet_num = 0;
1578 already_waited_sec = 0; 1577 already_waited_sec = 0;
@@ -1747,9 +1746,9 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
1747 timeout = 61; 1746 timeout = 61;
1748 /* enter bound state */ 1747 /* enter bound state */
1749 d6_run_script(packet.d6_options, packet_end, 1748 d6_run_script(packet.d6_options, packet_end,
1750 (state == REQUESTING ? "bound" : "renew")); 1749 (client_config.state == REQUESTING ? "bound" : "renew"));
1751 1750
1752 state = BOUND; 1751 client_config.state = BOUND;
1753 change_listen_mode(LISTEN_NONE); 1752 change_listen_mode(LISTEN_NONE);
1754 if (opt & OPT_q) { /* quit after lease */ 1753 if (opt & OPT_q) { /* quit after lease */
1755 goto ret0; 1754 goto ret0;