Move dhcp client runtime global state into global struct client_state_t cs.

This commit is contained in:
Nicholas J. Kain 2010-12-24 07:00:42 -05:00
parent 406e316274
commit 8a3deb3c6a

View File

@ -81,21 +81,36 @@ enum {
LISTEN_RAW LISTEN_RAW
}; };
static int epollfd, signalFd, arpFd = -1; struct client_state_t {
static struct epoll_event events[3]; unsigned long long leaseStartTime;
int dhcpState;
int arpPrevState;
int listenMode;
int packetNum;
int epollFd, signalFd, listenFd, arpFd;
int timeout;
uint32_t requestedIP, serverAddr;
uint32_t lease, t1, t2, xid;
};
static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT; struct client_state_t cs = {
.dhcpState = INIT_SELECTING,
static int timeout; .arpPrevState = DHCP_STATE_NULL,
static uint32_t requested_ip, server_addr; .listenMode = LISTEN_NONE,
static uint32_t lease, t1, t2, xid; .packetNum = 0,
static long long start; .xid = 0,
.timeout = 0,
static int dhcp_state = INIT_SELECTING; .leaseStartTime = 0,
static int arp_prev_dhcp_state = DHCP_STATE_NULL; .requestedIP = 0,
static int listen_mode = LISTEN_NONE; .serverAddr = 0,
static int listenFd = -1; .lease = 0,
static int packet_num; .t1 = 0,
.t2 = 0,
.epollFd = -1,
.signalFd = -1,
.listenFd = -1,
.arpFd = -1,
};
struct client_config_t client_config = { struct client_config_t client_config = {
/* Default options. */ /* Default options. */
@ -111,6 +126,8 @@ struct client_config_t client_config = {
.arp = "\0", .arp = "\0",
}; };
static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
static unsigned long long curms() static unsigned long long curms()
{ {
struct timeval tv; struct timeval tv;
@ -124,7 +141,7 @@ static void epoll_add(int fd)
int r; int r;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd; ev.data.fd = fd;
r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); r = epoll_ctl(cs.epollFd, EPOLL_CTL_ADD, fd, &ev);
if (r == -1) if (r == -1)
suicide("epoll_add failed %s", strerror(errno)); suicide("epoll_add failed %s", strerror(errno));
} }
@ -135,7 +152,7 @@ static void epoll_del(int fd)
int r; int r;
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
ev.data.fd = fd; ev.data.fd = fd;
r = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev); r = epoll_ctl(cs.epollFd, EPOLL_CTL_DEL, fd, &ev);
if (r == -1) if (r == -1)
suicide("epoll_del failed %s", strerror(errno)); suicide("epoll_del failed %s", strerror(errno));
} }
@ -168,24 +185,24 @@ static void change_listen_mode(int new_mode)
{ {
log_line("entering %s listen mode", log_line("entering %s listen mode",
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
listen_mode = new_mode; cs.listenMode = new_mode;
if (listenFd >= 0) { if (cs.listenFd >= 0) {
epoll_del(listenFd); epoll_del(cs.listenFd);
close(listenFd); close(cs.listenFd);
listenFd = -1; cs.listenFd = -1;
} }
if (new_mode == LISTEN_KERNEL) { if (new_mode == LISTEN_KERNEL) {
listenFd = listen_socket(INADDR_ANY, CLIENT_PORT, cs.listenFd = listen_socket(INADDR_ANY, CLIENT_PORT,
client_config.interface); client_config.interface);
epoll_add(listenFd); epoll_add(cs.listenFd);
} }
else if (new_mode == LISTEN_RAW) { else if (new_mode == LISTEN_RAW) {
listenFd = raw_socket(client_config.ifindex); cs.listenFd = raw_socket(client_config.ifindex);
epoll_add(listenFd); epoll_add(cs.listenFd);
} }
else /* LISTEN_NONE */ else /* LISTEN_NONE */
return; return;
if (listenFd < 0) { if (cs.listenFd < 0) {
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno)); log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -196,25 +213,25 @@ static void perform_renew(void)
{ {
log_line("Performing a DHCP renew..."); log_line("Performing a DHCP renew...");
retry: retry:
switch (dhcp_state) { switch (cs.dhcpState) {
case BOUND: case BOUND:
change_listen_mode(LISTEN_KERNEL); change_listen_mode(LISTEN_KERNEL);
case ARP_CHECK: case ARP_CHECK:
// Cancel arp ping in progress and treat as previous state. // Cancel arp ping in progress and treat as previous state.
epoll_del(arpFd); epoll_del(cs.arpFd);
arpFd = -1; cs.arpFd = -1;
dhcp_state = arp_prev_dhcp_state; cs.dhcpState = cs.arpPrevState;
goto retry; goto retry;
case RENEWING: case RENEWING:
case REBINDING: case REBINDING:
dhcp_state = RENEW_REQUESTED; cs.dhcpState = RENEW_REQUESTED;
break; break;
case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
case REQUESTING: case REQUESTING:
case RELEASED: case RELEASED:
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
break; break;
case INIT_SELECTING: case INIT_SELECTING:
default: default:
@ -222,10 +239,10 @@ static void perform_renew(void)
} }
/* start things over */ /* start things over */
packet_num = 0; cs.packetNum = 0;
/* Kill any timeouts because the user wants this to hurry along */ /* Kill any timeouts because the user wants this to hurry along */
timeout = 0; cs.timeout = 0;
} }
@ -235,24 +252,24 @@ static void perform_release(void)
struct in_addr temp_saddr, temp_raddr; struct in_addr temp_saddr, temp_raddr;
/* send release packet */ /* send release packet */
if (dhcp_state == BOUND || dhcp_state == RENEWING || if (cs.dhcpState == BOUND || cs.dhcpState == RENEWING ||
dhcp_state == REBINDING || dhcp_state == ARP_CHECK) { cs.dhcpState == REBINDING || cs.dhcpState == ARP_CHECK) {
temp_saddr.s_addr = server_addr; temp_saddr.s_addr = cs.serverAddr;
temp_raddr.s_addr = requested_ip; temp_raddr.s_addr = cs.requestedIP;
log_line("Unicasting a release of %s to %s.", log_line("Unicasting a release of %s to %s.",
inet_ntoa(temp_raddr), inet_ntoa(temp_saddr)); inet_ntoa(temp_raddr), inet_ntoa(temp_saddr));
send_release(server_addr, requested_ip); /* unicast */ send_release(cs.serverAddr, cs.requestedIP); /* unicast */
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
} }
log_line("Entering released state."); log_line("Entering released state.");
if (dhcp_state == ARP_CHECK) { if (cs.dhcpState == ARP_CHECK) {
epoll_del(arpFd); epoll_del(cs.arpFd);
arpFd = -1; cs.arpFd = -1;
} }
change_listen_mode(LISTEN_NONE); change_listen_mode(LISTEN_NONE);
dhcp_state = RELEASED; cs.dhcpState = RELEASED;
timeout = -1; cs.timeout = -1;
} }
static void background(void) static void background(void)
@ -277,16 +294,16 @@ static struct dhcpMessage arp_dhcp_packet;
static void arp_failed(void) static void arp_failed(void)
{ {
log_line("Offered address is in use: declining."); log_line("Offered address is in use: declining.");
epoll_del(arpFd); epoll_del(cs.arpFd);
arpFd = -1; cs.arpFd = -1;
send_decline(xid, server_addr, arp_dhcp_packet.yiaddr); send_decline(cs.xid, cs.serverAddr, arp_dhcp_packet.yiaddr);
if (arp_prev_dhcp_state != REQUESTING) if (cs.arpPrevState != REQUESTING)
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
requested_ip = 0; cs.requestedIP = 0;
timeout = 0; cs.timeout = 0;
packet_num = 0; cs.packetNum = 0;
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
} }
@ -294,26 +311,26 @@ static void arp_success(void)
{ {
struct in_addr temp_addr; struct in_addr temp_addr;
epoll_del(arpFd); epoll_del(cs.arpFd);
arpFd = -1; cs.arpFd = -1;
/* enter bound state */ /* enter bound state */
t1 = lease >> 1; cs.t1 = cs.lease >> 1;
/* little fixed point for n * .875 */ /* little fixed point for n * .875 */
t2 = (lease * 0x7) >> 3; cs.t2 = (cs.lease * 0x7) >> 3;
timeout = t1 * 1000; cs.timeout = cs.t1 * 1000;
start = curms(); cs.leaseStartTime = curms();
temp_addr.s_addr = arp_dhcp_packet.yiaddr; temp_addr.s_addr = arp_dhcp_packet.yiaddr;
log_line("Lease of %s obtained, lease time %ld.", log_line("Lease of %s obtained, lease time %ld.",
inet_ntoa(temp_addr), lease); inet_ntoa(temp_addr), cs.lease);
requested_ip = arp_dhcp_packet.yiaddr; cs.requestedIP = arp_dhcp_packet.yiaddr;
run_script(&arp_dhcp_packet, run_script(&arp_dhcp_packet,
((arp_prev_dhcp_state == RENEWING || ((cs.arpPrevState == RENEWING ||
arp_prev_dhcp_state == REBINDING) cs.arpPrevState == REBINDING)
? SCRIPT_RENEW : SCRIPT_BOUND)); ? SCRIPT_RENEW : SCRIPT_BOUND));
dhcp_state = BOUND; cs.dhcpState = BOUND;
change_listen_mode(LISTEN_NONE); change_listen_mode(LISTEN_NONE);
if (client_config.quit_after_lease) if (client_config.quit_after_lease)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -323,14 +340,14 @@ static void arp_success(void)
static void init_selecting_timeout() static void init_selecting_timeout()
{ {
if (packet_num < NUMPACKETS) { if (cs.packetNum < NUMPACKETS) {
if (packet_num == 0) if (cs.packetNum == 0)
xid = random_xid(); cs.xid = random_xid();
/* broadcast */ /* broadcast */
send_discover(xid, requested_ip); send_discover(cs.xid, cs.requestedIP);
timeout = ((packet_num == NUMPACKETS - 1) ? 4 : 2) * 1000; cs.timeout = ((cs.packetNum == NUMPACKETS - 1) ? 4 : 2) * 1000;
packet_num++; cs.packetNum++;
} else { } else {
if (client_config.background_if_no_lease) { if (client_config.background_if_no_lease) {
log_line("No lease, going to background."); log_line("No lease, going to background.");
@ -340,40 +357,40 @@ static void init_selecting_timeout()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* wait to try again */ /* wait to try again */
packet_num = 0; cs.packetNum = 0;
timeout = RETRY_DELAY * 1000; cs.timeout = RETRY_DELAY * 1000;
} }
} }
static void renew_requested_timeout() static void renew_requested_timeout()
{ {
if (packet_num < NUMPACKETS) { if (cs.packetNum < NUMPACKETS) {
/* send unicast request packet */ /* send unicast request packet */
send_renew(xid, server_addr, requested_ip); send_renew(cs.xid, cs.serverAddr, cs.requestedIP);
timeout = ((packet_num == NUMPACKETS - 1) ? 10 : 2) * 1000; cs.timeout = ((cs.packetNum == NUMPACKETS - 1) ? 10 : 2) * 1000;
packet_num++; cs.packetNum++;
} else { } else {
/* timed out, go back to init state */ /* timed out, go back to init state */
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
timeout = 0; cs.timeout = 0;
packet_num = 0; cs.packetNum = 0;
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
} }
} }
static void requesting_timeout() static void requesting_timeout()
{ {
if (packet_num < NUMPACKETS) { if (cs.packetNum < NUMPACKETS) {
/* send broadcast request packet */ /* send broadcast request packet */
send_selecting(xid, server_addr, requested_ip); send_selecting(cs.xid, cs.serverAddr, cs.requestedIP);
timeout = ((packet_num == NUMPACKETS - 1) ? 10 : 2) * 1000; cs.timeout = ((cs.packetNum == NUMPACKETS - 1) ? 10 : 2) * 1000;
packet_num++; cs.packetNum++;
} else { } else {
/* timed out, go back to init state */ /* timed out, go back to init state */
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
timeout = 0; cs.timeout = 0;
packet_num = 0; cs.packetNum = 0;
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
} }
} }
@ -381,24 +398,24 @@ static void requesting_timeout()
static void renewing_timeout() static void renewing_timeout()
{ {
/* Either set a new T1, or enter REBINDING state */ /* Either set a new T1, or enter REBINDING state */
if ((t2 - t1) <= (lease / 14400 + 1)) { if ((cs.t2 - cs.t1) <= (cs.lease / 14400 + 1)) {
/* timed out, enter rebinding state */ /* timed out, enter rebinding state */
dhcp_state = REBINDING; cs.dhcpState = REBINDING;
timeout = (t2 - t1) * 1000; cs.timeout = (cs.t2 - cs.t1) * 1000;
log_line("Entering rebinding state."); log_line("Entering rebinding state.");
} else { } else {
/* send a request packet */ /* send a request packet */
send_renew(xid, server_addr, requested_ip); /* unicast */ send_renew(cs.xid, cs.serverAddr, cs.requestedIP); /* unicast */
t1 = ((t2 - t1) >> 1) + t1; cs.t1 = ((cs.t2 - cs.t1) >> 1) + cs.t1;
timeout = (t1 * 1000) - (curms() - start); cs.timeout = (cs.t1 * 1000) - (curms() - cs.leaseStartTime);
} }
} }
static void bound_timeout() static void bound_timeout()
{ {
/* Lease is starting to run out, time to enter renewing state */ /* Lease is starting to run out, time to enter renewing state */
dhcp_state = RENEWING; cs.dhcpState = RENEWING;
change_listen_mode(LISTEN_KERNEL); change_listen_mode(LISTEN_KERNEL);
log_line("Entering renew state."); log_line("Entering renew state.");
renewing_timeout(); renewing_timeout();
@ -407,34 +424,34 @@ static void bound_timeout()
static void rebinding_timeout() static void rebinding_timeout()
{ {
/* Either set a new T2, or enter INIT state */ /* Either set a new T2, or enter INIT state */
if ((lease - t2) <= (lease / 14400 + 1)) { if ((cs.lease - cs.t2) <= (cs.lease / 14400 + 1)) {
/* timed out, enter init state */ /* timed out, enter init state */
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
log_line("Lease lost, entering init state."); log_line("Lease lost, entering init state.");
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
timeout = 0; cs.timeout = 0;
packet_num = 0; cs.packetNum = 0;
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
} else { } else {
/* send a request packet */ /* send a request packet */
send_renew(xid, 0, requested_ip); /* broadcast */ send_renew(cs.xid, 0, cs.requestedIP); /* broadcast */
t2 = ((lease - t2) >> 1) + t2; cs.t2 = ((cs.lease - cs.t2) >> 1) + cs.t2;
timeout = (t2 * 1000) - (curms() - start); cs.timeout = (cs.t2 * 1000) - (curms() - cs.leaseStartTime);
} }
} }
/* Handle epoll timeout expiring */ /* Handle epoll timeout expiring */
static void handle_timeout(void) static void handle_timeout(void)
{ {
switch (dhcp_state) { switch (cs.dhcpState) {
case INIT_SELECTING: init_selecting_timeout(); break; case INIT_SELECTING: init_selecting_timeout(); break;
case RENEW_REQUESTED: renew_requested_timeout(); break; case RENEW_REQUESTED: renew_requested_timeout(); break;
case REQUESTING: requesting_timeout(); break; case REQUESTING: requesting_timeout(); break;
case RENEWING: renewing_timeout(); break; case RENEWING: renewing_timeout(); break;
case BOUND: bound_timeout(); break; case BOUND: bound_timeout(); break;
case REBINDING: rebinding_timeout(); break; case REBINDING: rebinding_timeout(); break;
case RELEASED: timeout = -1; break; case RELEASED: cs.timeout = -1; break;
case ARP_CHECK: arp_success(); break; case ARP_CHECK: arp_success(); break;
default: break; default: break;
} }
@ -448,14 +465,14 @@ static void init_selecting_packet(struct dhcpMessage *packet,
if (*message == DHCPOFFER) { if (*message == DHCPOFFER) {
if ((temp = get_option(packet, DHCP_SERVER_ID))) { if ((temp = get_option(packet, DHCP_SERVER_ID))) {
/* Memcpy to a temp buffer to force alignment */ /* Memcpy to a temp buffer to force alignment */
memcpy(&server_addr, temp, 4); memcpy(&cs.serverAddr, temp, 4);
xid = packet->xid; cs.xid = packet->xid;
requested_ip = packet->yiaddr; cs.requestedIP = packet->yiaddr;
/* enter requesting state */ /* enter requesting state */
dhcp_state = REQUESTING; cs.dhcpState = REQUESTING;
timeout = 0; cs.timeout = 0;
packet_num = 0; cs.packetNum = 0;
} else { } else {
log_line("No server ID in message"); log_line("No server ID in message");
} }
@ -469,24 +486,24 @@ static void dhcp_ack_or_nak_packet(struct dhcpMessage *packet,
if (*message == DHCPACK) { if (*message == DHCPACK) {
if (!(temp = get_option(packet, DHCP_LEASE_TIME))) { if (!(temp = get_option(packet, DHCP_LEASE_TIME))) {
log_line("No lease time received, assuming 1h."); log_line("No lease time received, assuming 1h.");
lease = 60 * 60; cs.lease = 60 * 60;
} else { } else {
/* Memcpy to a temp buffer to force alignment */ /* Memcpy to a temp buffer to force alignment */
memcpy(&lease, temp, 4); memcpy(&cs.lease, temp, 4);
lease = ntohl(lease); cs.lease = ntohl(cs.lease);
/* Enforce upper and lower bounds on lease. */ /* Enforce upper and lower bounds on lease. */
lease &= 0x0fffffff; cs.lease &= 0x0fffffff;
if (lease < RETRY_DELAY) if (cs.lease < RETRY_DELAY)
lease = RETRY_DELAY; cs.lease = RETRY_DELAY;
} }
arp_prev_dhcp_state = dhcp_state; cs.arpPrevState = cs.dhcpState;
dhcp_state = ARP_CHECK; cs.dhcpState = ARP_CHECK;
memcpy(&arp_dhcp_packet, packet, sizeof (struct dhcpMessage)); memcpy(&arp_dhcp_packet, packet, sizeof (struct dhcpMessage));
arpFd = arpping(arp_dhcp_packet.yiaddr, NULL, 0, cs.arpFd = arpping(arp_dhcp_packet.yiaddr, NULL, 0,
client_config.arp, client_config.interface); client_config.arp, client_config.interface);
epoll_add(arpFd); epoll_add(cs.arpFd);
timeout = 2000; cs.timeout = 2000;
memset(&arpreply, 0, sizeof arpreply); memset(&arpreply, 0, sizeof arpreply);
arpreply_offset = 0; arpreply_offset = 0;
// Can transition to BOUND or INIT_SELECTING. // Can transition to BOUND or INIT_SELECTING.
@ -495,12 +512,12 @@ static void dhcp_ack_or_nak_packet(struct dhcpMessage *packet,
/* return to init state */ /* return to init state */
log_line("Received DHCP NAK."); log_line("Received DHCP NAK.");
run_script(packet, SCRIPT_NAK); run_script(packet, SCRIPT_NAK);
if (dhcp_state != REQUESTING) if (cs.dhcpState != REQUESTING)
run_script(NULL, SCRIPT_DECONFIG); run_script(NULL, SCRIPT_DECONFIG);
dhcp_state = INIT_SELECTING; cs.dhcpState = INIT_SELECTING;
timeout = 0; cs.timeout = 0;
requested_ip = 0; cs.requestedIP = 0;
packet_num = 0; cs.packetNum = 0;
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
// XXX: this isn't rfc compliant: should be exp backoff // XXX: this isn't rfc compliant: should be exp backoff
sleep(3); /* avoid excessive network traffic */ sleep(3); /* avoid excessive network traffic */
@ -513,24 +530,24 @@ static void handle_packet(void)
int len; int len;
struct dhcpMessage packet; struct dhcpMessage packet;
if (listen_mode == LISTEN_KERNEL) if (cs.listenMode == LISTEN_KERNEL)
len = get_packet(&packet, listenFd); len = get_packet(&packet, cs.listenFd);
else if (listen_mode == LISTEN_RAW) else if (cs.listenMode == LISTEN_RAW)
len = get_raw_packet(&packet, listenFd); len = get_raw_packet(&packet, cs.listenFd);
else /* LISTEN_NONE */ else /* LISTEN_NONE */
return; return;
if (len == -1 && errno != EINTR) { if (len == -1 && errno != EINTR) {
log_error("reopening socket."); log_error("reopening socket.");
change_listen_mode(listen_mode); /* just close and reopen */ change_listen_mode(cs.listenMode); /* just close and reopen */
} }
if (len < 0) if (len < 0)
return; return;
if (packet.xid != xid) { if (packet.xid != cs.xid) {
log_line("Ignoring XID %lx (our xid is %lx).", log_line("Ignoring XID %lx (our xid is %lx).",
(uint32_t) packet.xid, xid); (uint32_t) packet.xid, cs.xid);
return; return;
} }
@ -539,7 +556,7 @@ static void handle_packet(void)
return; return;
} }
switch (dhcp_state) { switch (cs.dhcpState) {
case INIT_SELECTING: case INIT_SELECTING:
init_selecting_packet(&packet, message); init_selecting_packet(&packet, message);
break; break;
@ -565,7 +582,7 @@ typedef uint32_t aliased_uint32_t __attribute__((__may_alias__));
static void handle_arp_response(void) static void handle_arp_response(void)
{ {
if (arpreply_offset < sizeof arpreply) { if (arpreply_offset < sizeof arpreply) {
int r = safe_read(arpFd, (char *)&arpreply + arpreply_offset, int r = safe_read(cs.arpFd, (char *)&arpreply + arpreply_offset,
sizeof arpreply - arpreply_offset); sizeof arpreply - arpreply_offset);
if (r < 0) { if (r < 0) {
arp_failed(); arp_failed();
@ -607,8 +624,8 @@ static void setup_signals()
sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
suicide("sigprocmask failed"); suicide("sigprocmask failed");
signalFd = signalfd(-1, &mask, SFD_NONBLOCK); cs.signalFd = signalfd(-1, &mask, SFD_NONBLOCK);
if (signalFd < 0) if (cs.signalFd < 0)
suicide("signalfd failed"); suicide("signalfd failed");
} }
@ -617,7 +634,7 @@ static void signal_dispatch()
int t, off = 0; int t, off = 0;
struct signalfd_siginfo si; struct signalfd_siginfo si;
again: again:
t = read(signalFd, (char *)&si + off, sizeof si - off); t = read(cs.signalFd, (char *)&si + off, sizeof si - off);
if (t < sizeof si - off) { if (t < sizeof si - off) {
if (t < 0) { if (t < 0) {
if (t == EAGAIN || t == EWOULDBLOCK || t == EINTR) if (t == EAGAIN || t == EWOULDBLOCK || t == EINTR)
@ -644,19 +661,20 @@ static void signal_dispatch()
static void do_work(void) static void do_work(void)
{ {
struct epoll_event events[3];
long long last_awake; long long last_awake;
int timeout_delta; int timeout_delta;
epollfd = epoll_create1(0); cs.epollFd = epoll_create1(0);
if (epollfd == -1) if (cs.epollFd == -1)
suicide("epoll_create1 failed"); suicide("epoll_create1 failed");
epoll_add(signalFd); epoll_add(cs.signalFd);
change_listen_mode(LISTEN_RAW); change_listen_mode(LISTEN_RAW);
handle_timeout(); handle_timeout();
for (;;) { for (;;) {
last_awake = curms(); last_awake = curms();
int r = epoll_wait(epollfd, events, 3, timeout); int r = epoll_wait(cs.epollFd, events, 3, cs.timeout);
if (r == -1) { if (r == -1) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
@ -665,20 +683,20 @@ static void do_work(void)
} }
for (int i = 0; i < r; ++i) { for (int i = 0; i < r; ++i) {
int fd = events[i].data.fd; int fd = events[i].data.fd;
if (fd == signalFd) if (fd == cs.signalFd)
signal_dispatch(); signal_dispatch();
else if (fd == listenFd) else if (fd == cs.listenFd)
handle_packet(); handle_packet();
else if (fd == arpFd) else if (fd == cs.arpFd)
handle_arp_response(); handle_arp_response();
else else
suicide("epoll_wait: unknown fd"); suicide("epoll_wait: unknown fd");
} }
timeout_delta = curms() - last_awake; timeout_delta = curms() - last_awake;
timeout -= timeout_delta; cs.timeout -= timeout_delta;
if (timeout <= 0) { if (cs.timeout <= 0) {
timeout = 0; cs.timeout = 0;
handle_timeout(); handle_timeout();
} }
} }
@ -759,7 +777,7 @@ int main(int argc, char **argv)
client_config.quit_after_lease = 1; client_config.quit_after_lease = 1;
break; break;
case 'r': case 'r':
requested_ip = inet_addr(optarg); cs.requestedIP = inet_addr(optarg);
break; break;
case 'u': case 'u':
pwd = getpwnam(optarg); pwd = getpwnam(optarg);