Simplify the ifchange API.

This commit is contained in:
Nicholas J. Kain 2011-07-04 20:51:27 -04:00
parent bb83b14237
commit 9ef66af020
6 changed files with 22 additions and 50 deletions

View File

@ -1,5 +1,5 @@
/* arp.c - arp ping checking
* Time-stamp: <2011-07-04 20:17:51 njk>
* Time-stamp: <2011-07-04 20:49:18 njk>
*
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
*
@ -291,6 +291,7 @@ static void arp_switch_state(struct client_state_t *cs, arp_state_t state)
log_line("DEBUG: arp_switch_state: leaving.");
}
// Callable from DS_SELECTING, DS_RENEWING, or DS_REBINDING via an_packet()
int arp_check(struct client_state_t *cs, struct dhcpmsg *packet)
{
arp_switch_state(cs, AS_COLLISION_CHECK);
@ -304,6 +305,7 @@ int arp_check(struct client_state_t *cs, struct dhcpmsg *packet)
return 0;
}
// Callable only from DS_BOUND via netlink.c:nl_process_msgs().
int arp_gw_check(struct client_state_t *cs)
{
arp_switch_state(cs, AS_GW_CHECK);
@ -338,7 +340,7 @@ static void arp_failed(struct client_state_t *cs)
send_decline(cs, arp_dhcp_packet.yiaddr);
if (cs->arpPrevState != DS_REQUESTING)
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->dhcpState = DS_SELECTING;
cs->clientAddr = 0;
cs->timeout = 0;
@ -351,7 +353,7 @@ void arp_gw_failed(struct client_state_t *cs)
log_line("arp: Gateway appears to have changed, getting new lease");
arp_close_fd(cs);
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->dhcpState = DS_SELECTING;
cs->oldTimeout = 0;
cs->timeout = 0;
@ -371,13 +373,15 @@ void arp_success(struct client_state_t *cs)
cs->dhcpState = DS_BOUND;
cs->init = 0;
if (cs->arpPrevState == DS_RENEWING || cs->arpPrevState == DS_REBINDING) {
ifchange(&arp_dhcp_packet, IFCHANGE_RENEW); // XXX when does this happen?
// XXX We need to be smarter about this and only issue an ifchange if
// the lease has actually changed.
ifchange_bind(&arp_dhcp_packet);
arp_switch_state(cs, AS_DEFENSE);
} else {
ssize_t ol;
uint8_t *od;
od = get_option_data(&arp_dhcp_packet, DHCP_ROUTER, &ol);
ifchange(&arp_dhcp_packet, IFCHANGE_BOUND);
ifchange_bind(&arp_dhcp_packet);
if (ol == 4) {
memcpy(&cs->routerAddr, od, 4);
arp_get_gw_hwaddr(cs);

View File

@ -1,5 +1,5 @@
/* ifchange.c - functions to call the interface change daemon
* Time-stamp: <2011-07-04 18:50:59 njk>
* Time-stamp: <2011-07-04 20:48:03 njk>
*
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
*
@ -155,7 +155,7 @@ static void sockwrite(int fd, const char *buf, size_t count)
log_line("sent to ifchd: %s", buf);
}
static void deconfig_if(void)
void ifchange_deconfig(void)
{
int sockfd;
char buf[256];
@ -189,7 +189,7 @@ static void send_cmd(int sockfd, struct dhcpmsg *packet, uint8_t code)
sockwrite(sockfd, buf, strlen(buf));
}
static void bound_if(struct dhcpmsg *packet, int mode)
void ifchange_bind(struct dhcpmsg *packet)
{
int sockfd;
char buf[256];
@ -218,25 +218,3 @@ static void bound_if(struct dhcpmsg *packet, int mode)
close(sockfd);
}
void ifchange(struct dhcpmsg *packet, int mode)
{
switch (mode) {
case IFCHANGE_DECONFIG:
deconfig_if();
break;
case IFCHANGE_BOUND:
bound_if(packet, mode);
break;
case IFCHANGE_RENEW:
bound_if(packet, mode);
break;
case IFCHANGE_NAK:
deconfig_if();
break;
default:
log_error("invalid ifchange mode: %d", mode);
break;
}
}

View File

@ -1,5 +1,5 @@
/* ifchange.h - functions to call the interface change daemon
* Time-stamp: <2011-03-31 03:44:18 nk>
* Time-stamp: <2011-07-04 20:47:58 njk>
*
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
*
@ -21,15 +21,7 @@
#ifndef IFCHANGE_H_
#define IFCHANGE_H_
#include "dhcp.h"
enum {
IFCHANGE_DECONFIG = 0,
IFCHANGE_BOUND = 1,
IFCHANGE_RENEW = 2,
IFCHANGE_NAK = 4
};
void ifchange(struct dhcpmsg *packet, int mode);
void ifchange_bind(struct dhcpmsg *packet);
void ifchange_deconfig(void);
#endif

View File

@ -1,5 +1,5 @@
/* ndhc.c - DHCP client
* Time-stamp: <2011-07-03 17:19:25 njk>
* Time-stamp: <2011-07-04 20:49:48 njk>
*
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
*
@ -335,7 +335,7 @@ int main(int argc, char **argv)
drop_root(uid, gid);
if (cs.ifsPrevState != IFS_UP)
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
do_work();
return EXIT_SUCCESS; // Never reached.

View File

@ -46,7 +46,7 @@ static void restart_if(struct client_state_t *cs)
{
log_line("nl: %s back, querying for new lease", client_config.interface);
// XXX: Same as packet.c - merge somehow?
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->dhcpState = DS_SELECTING;
cs->timeout = 0;
cs->clientAddr = 0;

View File

@ -88,7 +88,7 @@ static void lease_timedout(struct client_state_t *cs)
{
cs->dhcpState = DS_SELECTING;
log_line("Lease lost, entering init state.");
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->timeout = 0;
cs->packetNum = 0;
set_listen_raw(cs);
@ -173,7 +173,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
// Can transition from DS_ARP_CHECK to DS_BOUND or DS_SELECTING.
if (arp_check(cs, packet) == -1) {
log_warning("arp_check failed to make arp socket, retrying lease");
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->dhcpState = DS_SELECTING;
cs->timeout = 30000;
cs->clientAddr = 0;
@ -183,9 +183,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet,
} else if (*message == DHCPNAK) {
log_line("Received DHCP NAK.");
ifchange(packet, IFCHANGE_NAK);
if (cs->dhcpState != DS_REQUESTING)
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
cs->dhcpState = DS_SELECTING;
cs->timeout = 3000;
cs->clientAddr = 0;
@ -248,7 +246,7 @@ static void nfrelease(struct client_state_t *cs)
inet_ntoa((struct in_addr){.s_addr=cs->clientAddr}),
inet_ntoa((struct in_addr){.s_addr=cs->serverAddr}));
send_release(cs);
ifchange(NULL, IFCHANGE_DECONFIG);
ifchange_deconfig();
frelease(cs);
}