diff options
-rw-r--r-- | networking/udhcp/d6_dhcpc.c | 77 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.c | 81 | ||||
-rw-r--r-- | networking/udhcp/dhcpc.h | 4 |
3 files changed, 82 insertions, 80 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 | ||
906 | static 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 |
911 | static 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 |
927 | static smallint state; | ||
928 | 926 | ||
929 | static int d6_raw_socket(int ifindex) | 927 | static 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 */ |
1034 | static void perform_renew(void) | 1032 | static 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) | |||
1056 | static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cur_ipv6) | 1054 | static 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; |
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 | ||
987 | static 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 |
992 | static 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 |
1008 | static smallint state; | ||
1009 | 1007 | ||
1010 | static int udhcp_raw_socket(int ifindex) | 1008 | static 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 */ |
1118 | static void perform_renew(void) | 1116 | static 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 | ||
1171 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) | 1169 | static 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; |
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h index 7fdbc9a6c..2618b12b5 100644 --- a/networking/udhcp/dhcpc.h +++ b/networking/udhcp/dhcpc.h | |||
@@ -24,6 +24,10 @@ struct client_config_t { | |||
24 | 24 | ||
25 | uint16_t first_secs; | 25 | uint16_t first_secs; |
26 | uint16_t last_secs; | 26 | uint16_t last_secs; |
27 | |||
28 | int sockfd; | ||
29 | smallint listen_mode; | ||
30 | smallint state; | ||
27 | } FIX_ALIASING; | 31 | } FIX_ALIASING; |
28 | 32 | ||
29 | /* server_config sits in 1st half of bb_common_bufsiz1 */ | 33 | /* server_config sits in 1st half of bb_common_bufsiz1 */ |