summaryrefslogtreecommitdiff
path: root/networking/udhcp/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/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/dhcpc.c')
-rw-r--r--networking/udhcp/dhcpc.c81
1 files changed, 40 insertions, 41 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 0e673ae7e..7b1b23706 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -984,13 +984,12 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
984 984
985/*** Main ***/ 985/*** Main ***/
986 986
987static int sockfd = -1; 987/* Values for client_config.listen_mode */
988
989#define LISTEN_NONE 0 988#define LISTEN_NONE 0
990#define LISTEN_KERNEL 1 989#define LISTEN_KERNEL 1
991#define LISTEN_RAW 2 990#define LISTEN_RAW 2
992static smallint listen_mode;
993 991
992/* Values for client_config.state */
994/* initial state: (re)start DHCP negotiation */ 993/* initial state: (re)start DHCP negotiation */
995#define INIT_SELECTING 0 994#define INIT_SELECTING 0
996/* discover was sent, DHCPOFFER reply received */ 995/* discover was sent, DHCPOFFER reply received */
@@ -1005,7 +1004,6 @@ static smallint listen_mode;
1005#define RENEW_REQUESTED 5 1004#define RENEW_REQUESTED 5
1006/* release, possibly manually requested (SIGUSR2) */ 1005/* release, possibly manually requested (SIGUSR2) */
1007#define RELEASED 6 1006#define RELEASED 6
1008static smallint state;
1009 1007
1010static int udhcp_raw_socket(int ifindex) 1008static int udhcp_raw_socket(int ifindex)
1011{ 1009{
@@ -1102,35 +1100,35 @@ static void change_listen_mode(int new_mode)
1102 : "none" 1100 : "none"
1103 ); 1101 );
1104 1102
1105 listen_mode = new_mode; 1103 client_config.listen_mode = new_mode;
1106 if (sockfd >= 0) { 1104 if (client_config.sockfd >= 0) {
1107 close(sockfd); 1105 close(client_config.sockfd);
1108 sockfd = -1; 1106 client_config.sockfd = -1;
1109 } 1107 }
1110 if (new_mode == LISTEN_KERNEL) 1108 if (new_mode == LISTEN_KERNEL)
1111 sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); 1109 client_config.sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
1112 else if (new_mode != LISTEN_NONE) 1110 else if (new_mode != LISTEN_NONE)
1113 sockfd = udhcp_raw_socket(client_config.ifindex); 1111 client_config.sockfd = udhcp_raw_socket(client_config.ifindex);
1114 /* else LISTEN_NONE: sockfd stays closed */ 1112 /* else LISTEN_NONE: client_config.sockfd stays closed */
1115} 1113}
1116 1114
1117/* Called only on SIGUSR1 */ 1115/* Called only on SIGUSR1 */
1118static void perform_renew(void) 1116static void perform_renew(void)
1119{ 1117{
1120 bb_info_msg("performing DHCP renew"); 1118 bb_info_msg("performing DHCP renew");
1121 switch (state) { 1119 switch (client_config.state) {
1122 case BOUND: 1120 case BOUND:
1123 change_listen_mode(LISTEN_KERNEL); 1121 change_listen_mode(LISTEN_KERNEL);
1124 case RENEWING: 1122 case RENEWING:
1125 case REBINDING: 1123 case REBINDING:
1126 state = RENEW_REQUESTED; 1124 client_config.state = RENEW_REQUESTED;
1127 break; 1125 break;
1128 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ 1126 case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
1129 udhcp_run_script(NULL, "deconfig"); 1127 udhcp_run_script(NULL, "deconfig");
1130 case REQUESTING: 1128 case REQUESTING:
1131 case RELEASED: 1129 case RELEASED:
1132 change_listen_mode(LISTEN_RAW); 1130 change_listen_mode(LISTEN_RAW);
1133 state = INIT_SELECTING; 1131 client_config.state = INIT_SELECTING;
1134 break; 1132 break;
1135 case INIT_SELECTING: 1133 case INIT_SELECTING:
1136 break; 1134 break;
@@ -1143,10 +1141,10 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip)
1143 struct in_addr temp_addr; 1141 struct in_addr temp_addr;
1144 1142
1145 /* send release packet */ 1143 /* send release packet */
1146 if (state == BOUND 1144 if (client_config.state == BOUND
1147 || state == RENEWING 1145 || client_config.state == RENEWING
1148 || state == REBINDING 1146 || client_config.state == REBINDING
1149 || state == RENEW_REQUESTED 1147 || client_config.state == RENEW_REQUESTED
1150 ) { 1148 ) {
1151 temp_addr.s_addr = server_addr; 1149 temp_addr.s_addr = server_addr;
1152 strcpy(buffer, inet_ntoa(temp_addr)); 1150 strcpy(buffer, inet_ntoa(temp_addr));
@@ -1165,7 +1163,7 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip)
1165 udhcp_run_script(NULL, "deconfig"); 1163 udhcp_run_script(NULL, "deconfig");
1166 1164
1167 change_listen_mode(LISTEN_NONE); 1165 change_listen_mode(LISTEN_NONE);
1168 state = RELEASED; 1166 client_config.state = RELEASED;
1169} 1167}
1170 1168
1171static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) 1169static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
@@ -1270,6 +1268,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1270 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) 1268 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
1271 client_config.interface = "eth0"; 1269 client_config.interface = "eth0";
1272 client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT; 1270 client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
1271 client_config.sockfd = -1;
1273 str_V = "udhcp "BB_VER; 1272 str_V = "udhcp "BB_VER;
1274 1273
1275 /* Parse command line */ 1274 /* Parse command line */
@@ -1397,7 +1396,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1397 /* We want random_xid to be random... */ 1396 /* We want random_xid to be random... */
1398 srand(monotonic_us()); 1397 srand(monotonic_us());
1399 1398
1400 state = INIT_SELECTING; 1399 client_config.state = INIT_SELECTING;
1401 udhcp_run_script(NULL, "deconfig"); 1400 udhcp_run_script(NULL, "deconfig");
1402 change_listen_mode(LISTEN_RAW); 1401 change_listen_mode(LISTEN_RAW);
1403 packet_num = 0; 1402 packet_num = 0;
@@ -1415,16 +1414,16 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1415 /* silence "uninitialized!" warning */ 1414 /* silence "uninitialized!" warning */
1416 unsigned timestamp_before_wait = timestamp_before_wait; 1415 unsigned timestamp_before_wait = timestamp_before_wait;
1417 1416
1418 //bb_error_msg("sockfd:%d, listen_mode:%d", sockfd, listen_mode); 1417 //bb_error_msg("sockfd:%d, listen_mode:%d", client_config.sockfd, client_config.listen_mode);
1419 1418
1420 /* Was opening raw or udp socket here 1419 /* Was opening raw or udp socket here
1421 * if (listen_mode != LISTEN_NONE && sockfd < 0), 1420 * if (client_config.listen_mode != LISTEN_NONE && client_config.sockfd < 0),
1422 * but on fast network renew responses return faster 1421 * but on fast network renew responses return faster
1423 * than we open sockets. Thus this code is moved 1422 * than we open sockets. Thus this code is moved
1424 * to change_listen_mode(). Thus we open listen socket 1423 * to change_listen_mode(). Thus we open listen socket
1425 * BEFORE we send renew request (see "case BOUND:"). */ 1424 * BEFORE we send renew request (see "case BOUND:"). */
1426 1425
1427 udhcp_sp_fd_set(pfds, sockfd); 1426 udhcp_sp_fd_set(pfds, client_config.sockfd);
1428 1427
1429 tv = timeout - already_waited_sec; 1428 tv = timeout - already_waited_sec;
1430 retval = 0; 1429 retval = 0;
@@ -1466,7 +1465,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1466 /* We will restart the wait in any case */ 1465 /* We will restart the wait in any case */
1467 already_waited_sec = 0; 1466 already_waited_sec = 0;
1468 1467
1469 switch (state) { 1468 switch (client_config.state) {
1470 case INIT_SELECTING: 1469 case INIT_SELECTING:
1471 if (!discover_retries || packet_num < discover_retries) { 1470 if (!discover_retries || packet_num < discover_retries) {
1472 if (packet_num == 0) 1471 if (packet_num == 0)
@@ -1515,11 +1514,11 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1515 * were seen in the wild. Treat them similarly 1514 * were seen in the wild. Treat them similarly
1516 * to "no response to discover" case */ 1515 * to "no response to discover" case */
1517 change_listen_mode(LISTEN_RAW); 1516 change_listen_mode(LISTEN_RAW);
1518 state = INIT_SELECTING; 1517 client_config.state = INIT_SELECTING;
1519 goto leasefail; 1518 goto leasefail;
1520 case BOUND: 1519 case BOUND:
1521 /* 1/2 lease passed, enter renewing state */ 1520 /* 1/2 lease passed, enter renewing state */
1522 state = RENEWING; 1521 client_config.state = RENEWING;
1523 client_config.first_secs = 0; /* make secs field count from 0 */ 1522 client_config.first_secs = 0; /* make secs field count from 0 */
1524 change_listen_mode(LISTEN_KERNEL); 1523 change_listen_mode(LISTEN_KERNEL);
1525 log1("entering renew state"); 1524 log1("entering renew state");
@@ -1556,7 +1555,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1556 } 1555 }
1557 /* Timed out or error, enter rebinding state */ 1556 /* Timed out or error, enter rebinding state */
1558 log1("entering rebinding state"); 1557 log1("entering rebinding state");
1559 state = REBINDING; 1558 client_config.state = REBINDING;
1560 /* fall right through */ 1559 /* fall right through */
1561 case REBINDING: 1560 case REBINDING:
1562 /* Switch to bcast receive */ 1561 /* Switch to bcast receive */
@@ -1572,7 +1571,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1572 /* Timed out, enter init state */ 1571 /* Timed out, enter init state */
1573 bb_info_msg("lease lost, entering init state"); 1572 bb_info_msg("lease lost, entering init state");
1574 udhcp_run_script(NULL, "deconfig"); 1573 udhcp_run_script(NULL, "deconfig");
1575 state = INIT_SELECTING; 1574 client_config.state = INIT_SELECTING;
1576 client_config.first_secs = 0; /* make secs field count from 0 */ 1575 client_config.first_secs = 0; /* make secs field count from 0 */
1577 /*timeout = 0; - already is */ 1576 /*timeout = 0; - already is */
1578 packet_num = 0; 1577 packet_num = 0;
@@ -1592,7 +1591,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1592 client_config.first_secs = 0; /* make secs field count from 0 */ 1591 client_config.first_secs = 0; /* make secs field count from 0 */
1593 already_waited_sec = 0; 1592 already_waited_sec = 0;
1594 perform_renew(); 1593 perform_renew();
1595 if (state == RENEW_REQUESTED) { 1594 if (client_config.state == RENEW_REQUESTED) {
1596 /* We might be either on the same network 1595 /* We might be either on the same network
1597 * (in which case renew might work), 1596 * (in which case renew might work),
1598 * or we might be on a completely different one 1597 * or we might be on a completely different one
@@ -1627,15 +1626,15 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1627 int len; 1626 int len;
1628 1627
1629 /* A packet is ready, read it */ 1628 /* A packet is ready, read it */
1630 if (listen_mode == LISTEN_KERNEL) 1629 if (client_config.listen_mode == LISTEN_KERNEL)
1631 len = udhcp_recv_kernel_packet(&packet, sockfd); 1630 len = udhcp_recv_kernel_packet(&packet, client_config.sockfd);
1632 else 1631 else
1633 len = udhcp_recv_raw_packet(&packet, sockfd); 1632 len = udhcp_recv_raw_packet(&packet, client_config.sockfd);
1634 if (len == -1) { 1633 if (len == -1) {
1635 /* Error is severe, reopen socket */ 1634 /* Error is severe, reopen socket */
1636 bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO); 1635 bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
1637 sleep(discover_timeout); /* 3 seconds by default */ 1636 sleep(discover_timeout); /* 3 seconds by default */
1638 change_listen_mode(listen_mode); /* just close and reopen */ 1637 change_listen_mode(client_config.listen_mode); /* just close and reopen */
1639 } 1638 }
1640 /* If this packet will turn out to be unrelated/bogus, 1639 /* If this packet will turn out to be unrelated/bogus,
1641 * we will go back and wait for next one. 1640 * we will go back and wait for next one.
@@ -1666,7 +1665,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1666 continue; 1665 continue;
1667 } 1666 }
1668 1667
1669 switch (state) { 1668 switch (client_config.state) {
1670 case INIT_SELECTING: 1669 case INIT_SELECTING:
1671 /* Must be a DHCPOFFER */ 1670 /* Must be a DHCPOFFER */
1672 if (*message == DHCPOFFER) { 1671 if (*message == DHCPOFFER) {
@@ -1708,7 +1707,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1708 requested_ip = packet.yiaddr; 1707 requested_ip = packet.yiaddr;
1709 1708
1710 /* enter requesting state */ 1709 /* enter requesting state */
1711 state = REQUESTING; 1710 client_config.state = REQUESTING;
1712 timeout = 0; 1711 timeout = 0;
1713 packet_num = 0; 1712 packet_num = 0;
1714 already_waited_sec = 0; 1713 already_waited_sec = 0;
@@ -1763,10 +1762,10 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1763 "(got ARP reply), declining"); 1762 "(got ARP reply), declining");
1764 send_decline(/*xid,*/ server_addr, packet.yiaddr); 1763 send_decline(/*xid,*/ server_addr, packet.yiaddr);
1765 1764
1766 if (state != REQUESTING) 1765 if (client_config.state != REQUESTING)
1767 udhcp_run_script(NULL, "deconfig"); 1766 udhcp_run_script(NULL, "deconfig");
1768 change_listen_mode(LISTEN_RAW); 1767 change_listen_mode(LISTEN_RAW);
1769 state = INIT_SELECTING; 1768 client_config.state = INIT_SELECTING;
1770 client_config.first_secs = 0; /* make secs field count from 0 */ 1769 client_config.first_secs = 0; /* make secs field count from 0 */
1771 requested_ip = 0; 1770 requested_ip = 0;
1772 timeout = tryagain_timeout; 1771 timeout = tryagain_timeout;
@@ -1783,7 +1782,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1783 requested_ip = packet.yiaddr; 1782 requested_ip = packet.yiaddr;
1784 1783
1785 start = monotonic_sec(); 1784 start = monotonic_sec();
1786 udhcp_run_script(&packet, state == REQUESTING ? "bound" : "renew"); 1785 udhcp_run_script(&packet, client_config.state == REQUESTING ? "bound" : "renew");
1787 already_waited_sec = (unsigned)monotonic_sec() - start; 1786 already_waited_sec = (unsigned)monotonic_sec() - start;
1788 timeout = lease_seconds / 2; 1787 timeout = lease_seconds / 2;
1789 if ((unsigned)timeout < already_waited_sec) { 1788 if ((unsigned)timeout < already_waited_sec) {
@@ -1791,7 +1790,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1791 timeout = already_waited_sec = 0; 1790 timeout = already_waited_sec = 0;
1792 } 1791 }
1793 1792
1794 state = BOUND; 1793 client_config.state = BOUND;
1795 change_listen_mode(LISTEN_NONE); 1794 change_listen_mode(LISTEN_NONE);
1796 if (opt & OPT_q) { /* quit after lease */ 1795 if (opt & OPT_q) { /* quit after lease */
1797 goto ret0; 1796 goto ret0;
@@ -1833,11 +1832,11 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
1833 /* return to init state */ 1832 /* return to init state */
1834 bb_info_msg("received %s", "DHCP NAK"); 1833 bb_info_msg("received %s", "DHCP NAK");
1835 udhcp_run_script(&packet, "nak"); 1834 udhcp_run_script(&packet, "nak");
1836 if (state != REQUESTING) 1835 if (client_config.state != REQUESTING)
1837 udhcp_run_script(NULL, "deconfig"); 1836 udhcp_run_script(NULL, "deconfig");
1838 change_listen_mode(LISTEN_RAW); 1837 change_listen_mode(LISTEN_RAW);
1839 sleep(3); /* avoid excessive network traffic */ 1838 sleep(3); /* avoid excessive network traffic */
1840 state = INIT_SELECTING; 1839 client_config.state = INIT_SELECTING;
1841 client_config.first_secs = 0; /* make secs field count from 0 */ 1840 client_config.first_secs = 0; /* make secs field count from 0 */
1842 requested_ip = 0; 1841 requested_ip = 0;
1843 timeout = 0; 1842 timeout = 0;