/*** 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 */
#define RENEW_REQUESTED 5
/* release, possibly manually requested (SIGUSR2) */
#define RELEASED 6
-static smallint state;
static int d6_raw_socket(int ifindex)
{
: "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;
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 */
*/
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)
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, "^"
/* 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;
/* 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;
/* 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)
* 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");
}
/* Timed out, enter rebinding state */
log1("entering rebinding state");
- state = REBINDING;
+ client_config.state = REBINDING;
/* fall right through */
case REBINDING:
/* Switch to bcast receive */
/* 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;
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
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.
continue;
}
- switch (state) {
+ switch (client_config.state) {
case INIT_SELECTING:
if (packet.d6_msg_type == D6_MSG_ADVERTISE)
goto type_is_ok;
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;
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;
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;
/*** 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 */
#define RENEW_REQUESTED 5
/* release, possibly manually requested (SIGUSR2) */
#define RELEASED 6
-static smallint state;
static int udhcp_raw_socket(int ifindex)
{
: "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_PORT, client_config.interface);
+ client_config.sockfd = udhcp_listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
else if (new_mode != LISTEN_NONE)
- sockfd = udhcp_raw_socket(client_config.ifindex);
- /* else LISTEN_NONE: sockfd stays closed */
+ client_config.sockfd = udhcp_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 */
udhcp_run_script(NULL, "deconfig");
case REQUESTING:
case RELEASED:
change_listen_mode(LISTEN_RAW);
- state = INIT_SELECTING;
+ client_config.state = INIT_SELECTING;
break;
case INIT_SELECTING:
break;
struct in_addr temp_addr;
/* 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
) {
temp_addr.s_addr = server_addr;
strcpy(buffer, inet_ntoa(temp_addr));
udhcp_run_script(NULL, "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)
IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
client_config.interface = "eth0";
client_config.script = CONFIG_UDHCPC_DEFAULT_SCRIPT;
+ client_config.sockfd = -1;
str_V = "udhcp "BB_VER;
/* Parse command line */
/* We want random_xid to be random... */
srand(monotonic_us());
- state = INIT_SELECTING;
+ client_config.state = INIT_SELECTING;
udhcp_run_script(NULL, "deconfig");
change_listen_mode(LISTEN_RAW);
packet_num = 0;
/* 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;
/* 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)
* 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");
}
/* Timed out or error, enter rebinding state */
log1("entering rebinding state");
- state = REBINDING;
+ client_config.state = REBINDING;
/* fall right through */
case REBINDING:
/* Switch to bcast receive */
/* Timed out, enter init state */
bb_info_msg("lease lost, entering init state");
udhcp_run_script(NULL, "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;
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
int len;
/* A packet is ready, read it */
- if (listen_mode == LISTEN_KERNEL)
- len = udhcp_recv_kernel_packet(&packet, sockfd);
+ if (client_config.listen_mode == LISTEN_KERNEL)
+ len = udhcp_recv_kernel_packet(&packet, client_config.sockfd);
else
- len = udhcp_recv_raw_packet(&packet, sockfd);
+ len = udhcp_recv_raw_packet(&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.
continue;
}
- switch (state) {
+ switch (client_config.state) {
case INIT_SELECTING:
/* Must be a DHCPOFFER */
if (*message == DHCPOFFER) {
requested_ip = packet.yiaddr;
/* enter requesting state */
- state = REQUESTING;
+ client_config.state = REQUESTING;
timeout = 0;
packet_num = 0;
already_waited_sec = 0;
"(got ARP reply), declining");
send_decline(/*xid,*/ server_addr, packet.yiaddr);
- if (state != REQUESTING)
+ if (client_config.state != REQUESTING)
udhcp_run_script(NULL, "deconfig");
change_listen_mode(LISTEN_RAW);
- state = INIT_SELECTING;
+ client_config.state = INIT_SELECTING;
client_config.first_secs = 0; /* make secs field count from 0 */
requested_ip = 0;
timeout = tryagain_timeout;
requested_ip = packet.yiaddr;
start = monotonic_sec();
- udhcp_run_script(&packet, state == REQUESTING ? "bound" : "renew");
+ udhcp_run_script(&packet, client_config.state == REQUESTING ? "bound" : "renew");
already_waited_sec = (unsigned)monotonic_sec() - start;
timeout = lease_seconds / 2;
if ((unsigned)timeout < already_waited_sec) {
timeout = already_waited_sec = 0;
}
- state = BOUND;
+ client_config.state = BOUND;
change_listen_mode(LISTEN_NONE);
if (opt & OPT_q) { /* quit after lease */
goto ret0;
/* return to init state */
bb_info_msg("received %s", "DHCP NAK");
udhcp_run_script(&packet, "nak");
- if (state != REQUESTING)
+ if (client_config.state != REQUESTING)
udhcp_run_script(NULL, "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_ip = 0;
timeout = 0;