diff --git a/ndhc/arp.c b/ndhc/arp.c index c24a8c7..096c55c 100644 --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -186,7 +186,7 @@ static void arp_failed(struct client_state_t *cs) cs->requestedIP = 0; cs->timeout = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } void arp_gw_failed(struct client_state_t *cs) @@ -200,7 +200,7 @@ void arp_gw_failed(struct client_state_t *cs) cs->timeout = 0; cs->requestedIP = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } void arp_success(struct client_state_t *cs) @@ -218,7 +218,7 @@ void arp_success(struct client_state_t *cs) ((cs->arpPrevState == DS_RENEWING || cs->arpPrevState == DS_REBINDING) ? IFCHANGE_RENEW : IFCHANGE_BOUND)); - change_listen_mode(cs, LM_NONE); + set_listen_none(cs); write_leasefile(temp_addr); if (client_config.quit_after_lease) exit(EXIT_SUCCESS); diff --git a/ndhc/config.h b/ndhc/config.h index be0da71..f66dda4 100644 --- a/ndhc/config.h +++ b/ndhc/config.h @@ -23,12 +23,6 @@ #include -enum { - LM_NONE = 0, - LM_KERNEL, - LM_RAW -}; - enum { IFS_NONE = 0, IFS_UP, diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index 645c701..be013df 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -63,7 +63,7 @@ struct client_state_t cs = { .dhcpState = DS_SELECTING, .arpPrevState = DS_SELECTING, .ifsPrevState = IFS_NONE, - .listenMode = LM_NONE, + .listenMode = 0, .packetNum = 0, .xid = 0, .timeout = 0, @@ -161,7 +161,7 @@ static void do_work(void) suicide("epoll_create1 failed"); setup_signals(&cs); epoll_add(&cs, cs.nlFd); - change_listen_mode(&cs, LM_RAW); + set_listen_raw(&cs); timeout_action(&cs); for (;;) { diff --git a/ndhc/netlink.c b/ndhc/netlink.c index 794411f..38e6d6e 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -117,7 +117,7 @@ static void takedown_if(struct client_state_t *cs) cs->timeout = 0; cs->requestedIP = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } // Decode netlink messages and process them diff --git a/ndhc/packet.c b/ndhc/packet.c index 0a12dab..77fe7c3 100644 --- a/ndhc/packet.c +++ b/ndhc/packet.c @@ -49,6 +49,12 @@ #include "strl.h" #include "random.h" +typedef enum { + LM_NONE = 0, + LM_COOKED, + LM_RAW +} listen_mode_t; + // Returns fd of new udp socket bound on success, or -1 on failure. static int create_udp_socket(uint32_t ip, uint16_t port, char *iface) { @@ -433,7 +439,7 @@ static int send_dhcp_raw(struct dhcpmsg *payload) } // Switch listen socket between raw (if-bound), kernel (ip-bound), and none -void change_listen_mode(struct client_state_t *cs, int new_mode) +static void change_listen_mode(struct client_state_t *cs, int new_mode) { cs->listenMode = new_mode; if (cs->listenFd >= 0) { @@ -441,7 +447,7 @@ void change_listen_mode(struct client_state_t *cs, int new_mode) close(cs->listenFd); cs->listenFd = -1; } - if (new_mode != (LM_RAW || LM_KERNEL)) { + if (new_mode != (LM_RAW || LM_COOKED)) { log_line("Stopped listening for DHCP packets."); return; } @@ -457,6 +463,21 @@ void change_listen_mode(struct client_state_t *cs, int new_mode) new_mode == LM_RAW ? "raw" : "cooked"); } +void set_listen_raw(struct client_state_t *cs) +{ + change_listen_mode(cs, LM_RAW); +} + +void set_listen_cooked(struct client_state_t *cs) +{ + change_listen_mode(cs, LM_COOKED); +} + +void set_listen_none(struct client_state_t *cs) +{ + change_listen_mode(cs, LM_NONE); +} + void handle_packet(struct client_state_t *cs) { uint8_t *message = NULL; @@ -464,7 +485,7 @@ void handle_packet(struct client_state_t *cs) struct dhcpmsg packet; ssize_t optlen; - if (cs->listenMode == LM_KERNEL) + if (cs->listenMode == LM_COOKED) len = get_cooked_packet(&packet, cs->listenFd); else if (cs->listenMode == LM_RAW) len = get_raw_packet(&packet, cs->listenFd); diff --git a/ndhc/packet.h b/ndhc/packet.h index 958535f..fb71d9b 100644 --- a/ndhc/packet.h +++ b/ndhc/packet.h @@ -75,7 +75,9 @@ struct udp_dhcp_packet { struct dhcpmsg data; }; -void change_listen_mode(struct client_state_t *cs, int new_mode); +void set_listen_raw(struct client_state_t *cs); +void set_listen_cooked(struct client_state_t *cs); +void set_listen_none(struct client_state_t *cs); void handle_packet(struct client_state_t *cs); int send_discover(uint32_t xid, uint32_t requested); int send_selecting(uint32_t xid, uint32_t server, uint32_t requested); diff --git a/ndhc/state.c b/ndhc/state.c index 29defd6..7dd0224 100644 --- a/ndhc/state.c +++ b/ndhc/state.c @@ -70,7 +70,7 @@ static void requesting_timeout(struct client_state_t *cs) cs->dhcpState = DS_SELECTING; cs->timeout = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } } @@ -79,7 +79,7 @@ static void requesting_timeout(struct client_state_t *cs) static void bound_timeout(struct client_state_t *cs) { cs->dhcpState = DS_RENEWING; - change_listen_mode(cs, LM_KERNEL); + set_listen_cooked(cs); log_line("Entering renew state."); renewing_timeout(cs); } @@ -91,7 +91,7 @@ static void lease_timedout(struct client_state_t *cs) ifchange(NULL, IFCHANGE_DECONFIG); cs->timeout = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } // Triggered when a DHCP renew request has been sent and no reply has been @@ -178,7 +178,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet, cs->timeout = 30000; cs->requestedIP = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } } else if (*message == DHCPNAK) { @@ -190,7 +190,7 @@ static void an_packet(struct client_state_t *cs, struct dhcpmsg *packet, cs->timeout = 3000; cs->requestedIP = 0; cs->packetNum = 0; - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); } } @@ -255,7 +255,7 @@ static void nfrelease(struct client_state_t *cs) static void frelease(struct client_state_t *cs) { log_line("Entering released state."); - change_listen_mode(cs, LM_NONE); + set_listen_none(cs); cs->dhcpState = DS_RELEASED; cs->timeout = -1; } @@ -269,7 +269,7 @@ static void frenew(struct client_state_t *cs) case DS_BOUND_GW_CHECK: arp_close_fd(cs); cs->dhcpState = DS_RENEWING; - change_listen_mode(cs, LM_KERNEL); + set_listen_cooked(cs); send_renew(cs->xid, cs->serverAddr, cs->requestedIP); break; case DS_ARP_CHECK: @@ -278,7 +278,7 @@ static void frenew(struct client_state_t *cs) cs->dhcpState = cs->arpPrevState; goto retry; case DS_RELEASED: - change_listen_mode(cs, LM_RAW); + set_listen_raw(cs); cs->dhcpState = DS_SELECTING; break; case DS_RENEWING: