From 13862b488c1f41bf4055a0e0b4bd235f2b80549d Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sat, 11 Jun 2011 05:03:33 -0400 Subject: [PATCH] Cosmetic improvements for change_listen_mode(). Printed messages now look better, and epoll_add() is not called before exit() in the failure case. --- ndhc/packet.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ndhc/packet.c b/ndhc/packet.c index 2479267..0c03903 100644 --- a/ndhc/packet.c +++ b/ndhc/packet.c @@ -1,5 +1,5 @@ /* packet.c - send and react to DHCP message packets - * Time-stamp: <2011-06-11 04:26:22 njk> + * Time-stamp: <2011-06-11 05:02:18 njk> * * (c) 2004-2011 Nicholas J. Kain * (c) 2001 Russ Dill @@ -205,32 +205,34 @@ int kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, return result; } -/* Switch listen socket between raw (if-bound), kernel (ip-bound), and none */ +// Switch listen socket between raw (if-bound), kernel (ip-bound), and none void change_listen_mode(struct client_state_t *cs, int new_mode) { - log_line("entering %s listen mode", - new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); cs->listenMode = new_mode; if (cs->listenFd >= 0) { epoll_del(cs, cs->listenFd); close(cs->listenFd); cs->listenFd = -1; } - if (new_mode == LM_KERNEL) { - cs->listenFd = listen_socket(INADDR_ANY, DHCP_CLIENT_PORT, - client_config.interface); - epoll_add(cs, cs->listenFd); + switch (new_mode) { + case LM_NONE: + log_line("Stopped listening for DHCP packets."); + return; + case LM_RAW: + cs->listenFd = raw_socket(client_config.ifindex); + break; + case LM_KERNEL: + cs->listenFd = listen_socket(INADDR_ANY, DHCP_CLIENT_PORT, + client_config.interface); + break; } - else if (new_mode == LM_RAW) { - cs->listenFd = raw_socket(client_config.ifindex); - epoll_add(cs, cs->listenFd); - } - else /* LM_NONE */ - return; if (cs->listenFd < 0) { log_error("FATAL: couldn't listen on socket: %s.", strerror(errno)); exit(EXIT_FAILURE); } + epoll_add(cs, cs->listenFd); + log_line("Listening for DHCP packets using a %s socket.", + new_mode == LM_RAW ? "raw" : "cooked"); } static void init_selecting_packet(struct client_state_t *cs,