From 9ef66af020463100306286198f0568637bcf695a Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Mon, 4 Jul 2011 20:51:27 -0400 Subject: [PATCH] Simplify the ifchange API. --- ndhc/arp.c | 14 +++++++++----- ndhc/ifchange.c | 28 +++------------------------- ndhc/ifchange.h | 14 +++----------- ndhc/ndhc.c | 4 ++-- ndhc/netlink.c | 2 +- ndhc/state.c | 10 ++++------ 6 files changed, 22 insertions(+), 50 deletions(-) diff --git a/ndhc/arp.c b/ndhc/arp.c index a40ab53..7f45c44 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -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 * @@ -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); diff --git a/ndhc/ifchange.c b/ndhc/ifchange.c index 8ab2aaa..d501c12 100644 --- a/ndhc/ifchange.c +++ b/ndhc/ifchange.c @@ -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 * @@ -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; - } -} - diff --git a/ndhc/ifchange.h b/ndhc/ifchange.h index 5745b87..8e6a797 100644 --- a/ndhc/ifchange.h +++ b/ndhc/ifchange.h @@ -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 * @@ -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 diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index e7091fd..8f61096 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -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 * @@ -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. diff --git a/ndhc/netlink.c b/ndhc/netlink.c index 39dd913..3fecde1 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -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; diff --git a/ndhc/state.c b/ndhc/state.c index a3306c9..06c263e 100644 --- a/ndhc/state.c +++ b/ndhc/state.c @@ -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); }