diff --git a/ndhc/arpping.c b/ndhc/arpping.c index f76b283..6a2f58b 100644 --- a/ndhc/arpping.c +++ b/ndhc/arpping.c @@ -29,9 +29,6 @@ static struct arpMsg arpreply; static int arpreply_offset; static struct dhcpMessage arp_dhcp_packet; -// from ndhc.c -void background(void); - /* Returns fd of the arp socket, or -1 on failure. */ static int arpping(uint32_t test_nip, const uint8_t *safe_mac, uint32_t from_ip, uint8_t *from_mac, const char *interface) @@ -80,7 +77,6 @@ static int arpping(uint32_t test_nip, const uint8_t *safe_mac, return arpfd; } -// only called from packet.c void arp_check(struct client_state_t *cs, struct dhcpMessage *packet) { cs->arpPrevState = cs->dhcpState; @@ -110,7 +106,6 @@ static void arp_failed(struct client_state_t *cs) change_listen_mode(cs, LM_RAW); } -// only called from timeout.c void arp_success(struct client_state_t *cs) { struct in_addr temp_addr; diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index 7752e70..a8e6935 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -95,8 +95,6 @@ struct client_config_t client_config = { .arp = "\0", }; -static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT; - static void show_usage(void) { printf( @@ -184,21 +182,6 @@ static void perform_release(void) cs.timeout = -1; } -void background(void) -{ - static char called; - if (!called && daemon(0, 0) == -1) { - perror("fork"); - exit(EXIT_SUCCESS); - } - called = 1; /* Do not fork again. */ - if (file_exists(pidfile, "w") == -1) { - log_line("FATAL - cannot open pidfile for write!"); - exit(EXIT_FAILURE); - } - write_pid(pidfile); -} - static void setup_signals() { sigset_t mask; diff --git a/ndhc/sys.c b/ndhc/sys.c new file mode 100644 index 0000000..7db4a75 --- /dev/null +++ b/ndhc/sys.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "log.h" +#include "pidfile.h" +#include "sys.h" + +char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT; + +void background(void) +{ + static char called; + if (!called && daemon(0, 0) == -1) { + perror("fork"); + exit(EXIT_SUCCESS); + } + called = 1; /* Do not fork again. */ + if (file_exists(pidfile, "w") == -1) { + log_line("FATAL - cannot open pidfile for write!"); + exit(EXIT_FAILURE); + } + write_pid(pidfile); +} + +void epoll_add(struct client_state_t *cs, int fd) +{ + struct epoll_event ev; + int r; + ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; + ev.data.fd = fd; + r = epoll_ctl(cs->epollFd, EPOLL_CTL_ADD, fd, &ev); + if (r == -1) + suicide("epoll_add failed %s", strerror(errno)); +} + +void epoll_del(struct client_state_t *cs, int fd) +{ + struct epoll_event ev; + int r; + ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP; + ev.data.fd = fd; + r = epoll_ctl(cs->epollFd, EPOLL_CTL_DEL, fd, &ev); + if (r == -1) + suicide("epoll_del failed %s", strerror(errno)); +} diff --git a/ndhc/sys.h b/ndhc/sys.h new file mode 100644 index 0000000..d55df16 --- /dev/null +++ b/ndhc/sys.h @@ -0,0 +1,20 @@ +#ifndef SYS_H_ +#define SYS_H_ + +#include +#include "ndhc-defines.h" + +static inline unsigned long long curms() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000ULL + tv.tv_usec / 1000ULL; +} + +extern char pidfile[MAX_PATH_LENGTH]; + +void background(void); +void epoll_add(struct client_state_t *cs, int fd); +void epoll_del(struct client_state_t *cs, int fd); + +#endif /* SYS_H_ */ diff --git a/ndhc/timeout.c b/ndhc/timeout.c index a326b41..2ba2a86 100644 --- a/ndhc/timeout.c +++ b/ndhc/timeout.c @@ -9,9 +9,6 @@ #include "arpping.h" #include "log.h" -// from ndhc.c -void background(void); - static void init_selecting_timeout(struct client_state_t *cs) { if (cs->packetNum < NUMPACKETS) {