From da193ae35aeabe6eaf2a8e4250342597252c130a Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Tue, 19 Apr 2011 16:37:43 -0400 Subject: [PATCH] Add support for writing lease files. --- ndhc.sh | 3 +- ndhc/CMakeLists.txt | 13 +------- ndhc/README | 0 ndhc/arp.c | 4 ++- ndhc/arp.h | 0 ndhc/config.h | 0 ndhc/dhcpmsg.c | 0 ndhc/dhcpmsg.h | 0 ndhc/ifchange.c | 0 ndhc/ifchange.h | 0 ndhc/leasefile.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ ndhc/leasefile.h | 23 ++++++++++++++ ndhc/ndhc.c | 11 +++++-- ndhc/netlink.c | 0 ndhc/netlink.h | 0 ndhc/options.c | 0 ndhc/options.h | 0 ndhc/packet.c | 0 ndhc/packet.h | 0 ndhc/socket.c | 0 ndhc/socket.h | 0 ndhc/sys.c | 0 ndhc/sys.h | 0 ndhc/timeout.c | 0 ndhc/timeout.h | 0 25 files changed, 114 insertions(+), 16 deletions(-) mode change 100644 => 100755 ndhc/CMakeLists.txt mode change 100644 => 100755 ndhc/README mode change 100644 => 100755 ndhc/arp.c mode change 100644 => 100755 ndhc/arp.h mode change 100644 => 100755 ndhc/config.h mode change 100644 => 100755 ndhc/dhcpmsg.c mode change 100644 => 100755 ndhc/dhcpmsg.h mode change 100644 => 100755 ndhc/ifchange.c mode change 100644 => 100755 ndhc/ifchange.h create mode 100644 ndhc/leasefile.c create mode 100644 ndhc/leasefile.h mode change 100644 => 100755 ndhc/ndhc.c mode change 100644 => 100755 ndhc/netlink.c mode change 100644 => 100755 ndhc/netlink.h mode change 100644 => 100755 ndhc/options.c mode change 100644 => 100755 ndhc/options.h mode change 100644 => 100755 ndhc/packet.c mode change 100644 => 100755 ndhc/packet.h mode change 100644 => 100755 ndhc/socket.c mode change 100644 => 100755 ndhc/socket.h mode change 100644 => 100755 ndhc/sys.c mode change 100644 => 100755 ndhc/sys.h mode change 100644 => 100755 ndhc/timeout.c mode change 100644 => 100755 ndhc/timeout.h diff --git a/ndhc.sh b/ndhc.sh index be3c2dc..1fe0080 100644 --- a/ndhc.sh +++ b/ndhc.sh @@ -14,6 +14,7 @@ ndhc_start() { local args= opt= opts= pidfile="/var/run/ndhc-${IFACE}.pid" local sendhost=true + local leasefile="/var/state/${IFACE}.lease" eval args=\$ndhc_${IFVAR} @@ -63,7 +64,7 @@ ndhc_start() eval "${x}" "${args}" -r `cat /etc/firewall/tmp/OLDEXTIP` \ -n -i "${IFACE}" -u "ndhc" -C "/var/lib/ndhc" \ - -p "${pidfile}" >/dev/null + -p "${pidfile}" -l "${leasefile}" >/dev/null eend $? || return 1 _show_address diff --git a/ndhc/CMakeLists.txt b/ndhc/CMakeLists.txt old mode 100644 new mode 100755 index 5d0d4aa..ff69b71 --- a/ndhc/CMakeLists.txt +++ b/ndhc/CMakeLists.txt @@ -2,18 +2,7 @@ project (ndhc) cmake_minimum_required (VERSION 2.6) -set(NDHC_SRCS - sys.c - options.c - socket.c - packet.c - timeout.c - ifchange.c - dhcpmsg.c - arp.c - netlink.c - ndhc.c - ) +file(GLOB NDHC_SRCS "*.c") add_executable(ndhc ${NDHC_SRCS}) target_link_libraries(ndhc ncmlib) diff --git a/ndhc/README b/ndhc/README old mode 100644 new mode 100755 diff --git a/ndhc/arp.c b/ndhc/arp.c old mode 100644 new mode 100755 index 3320f61..dfd2b0e --- a/ndhc/arp.c +++ b/ndhc/arp.c @@ -1,5 +1,5 @@ /* arp.c - arp ping checking - * Time-stamp: <2011-03-31 02:29:09 nk> + * Time-stamp: <2011-04-19 16:21:14 njk> * * Copyright 2010-2011 Nicholas J. Kain * @@ -37,6 +37,7 @@ #include "socket.h" #include "sys.h" #include "ifchange.h" +#include "leasefile.h" #include "log.h" #include "strl.h" #include "io.h" @@ -209,6 +210,7 @@ void arp_success(struct client_state_t *cs) cs->arpPrevState == DS_REBINDING) ? IFCHANGE_RENEW : IFCHANGE_BOUND)); change_listen_mode(cs, LM_NONE); + write_leasefile(temp_addr); if (client_config.quit_after_lease) exit(EXIT_SUCCESS); if (!client_config.foreground) diff --git a/ndhc/arp.h b/ndhc/arp.h old mode 100644 new mode 100755 diff --git a/ndhc/config.h b/ndhc/config.h old mode 100644 new mode 100755 diff --git a/ndhc/dhcpmsg.c b/ndhc/dhcpmsg.c old mode 100644 new mode 100755 diff --git a/ndhc/dhcpmsg.h b/ndhc/dhcpmsg.h old mode 100644 new mode 100755 diff --git a/ndhc/ifchange.c b/ndhc/ifchange.c old mode 100644 new mode 100755 diff --git a/ndhc/ifchange.h b/ndhc/ifchange.h old mode 100644 new mode 100755 diff --git a/ndhc/leasefile.c b/ndhc/leasefile.c new file mode 100644 index 0000000..1415592 --- /dev/null +++ b/ndhc/leasefile.c @@ -0,0 +1,76 @@ +/* leasefile.c - functions for writing the lease file + * Time-stamp: <2011-04-19 16:22:36 njk> + * + * (c) 2011 Nicholas J. Kain + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "log.h" +#include "strl.h" +#include "io.h" +#include "defines.h" + +static char leasefile[PATH_MAX] = "\0"; +static int leasefilefd = -1; + +void set_leasefile(char *lf) +{ + strlcpy(leasefile, lf, sizeof leasefile); +} + +void open_leasefile() +{ + if (strlen(leasefile) > 0) { + leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644); + if (leasefilefd < 0) { + log_line("Failed to create lease file (%s)\n", leasefile); + exit(EXIT_FAILURE); + } + } +} + +void write_leasefile(struct in_addr ipnum) +{ + char ip[INET_ADDRSTRLEN+2]; + int ret; + if (leasefilefd < 0) + return; + inet_ntop(AF_INET, &ipnum, ip, sizeof ip); + strlcat(ip, "\n", sizeof ip); + retry_trunc: + ret = ftruncate(leasefilefd, 0); + switch (ret) { + default: break; + case -1: + if (errno == EINTR) + goto retry_trunc; + log_warning("Failed to truncate lease file.\n"); + return; + } + ret = safe_write(leasefilefd, ip, strlen(ip)); + if (ret == -1) + log_warning("Failed to write ip to lease file.\n"); +} diff --git a/ndhc/leasefile.h b/ndhc/leasefile.h new file mode 100644 index 0000000..9c99792 --- /dev/null +++ b/ndhc/leasefile.h @@ -0,0 +1,23 @@ +/* leasefile.h - functions for writing the lease file + * Time-stamp: <2011-04-19 16:22:47 njk> + * + * (c) 2011 Nicholas J. Kain + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +void set_leasefile(char *lf); +void open_leasefile(); +void write_leasefile(struct in_addr ipnum); diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c old mode 100644 new mode 100755 index f94a620..f139456 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -1,5 +1,5 @@ /* ndhc.c - DHCP client - * Time-stamp: <2011-03-31 01:38:17 nk> + * Time-stamp: <2011-04-19 16:04:53 njk> * * (c) 2004-2011 Nicholas J. Kain * @@ -49,6 +49,7 @@ #include "socket.h" #include "arp.h" #include "netlink.h" +#include "leasefile.h" #include "log.h" #include "chroot.h" @@ -274,6 +275,7 @@ int main(int argc, char **argv) {"foreground", no_argument, 0, 'f'}, {"background", no_argument, 0, 'b'}, {"pidfile", required_argument, 0, 'p'}, + {"leasefile", required_argument, 0, 'l'}, {"hostname", required_argument, 0, 'H'}, {"hostname", required_argument, 0, 'h'}, {"interface", required_argument, 0, 'i'}, @@ -290,7 +292,7 @@ int main(int argc, char **argv) /* get options */ while (1) { int option_index = 0; - c = getopt_long(argc, argv, "c:fbp:H:h:i:np:qr:u:C:v", arg_options, + c = getopt_long(argc, argv, "c:fbp:H:h:i:np:l:qr:u:C:v", arg_options, &option_index); if (c == -1) break; @@ -313,6 +315,9 @@ int main(int argc, char **argv) case 'p': strlcpy(pidfile, optarg, sizeof pidfile); break; + case 'l': + set_leasefile(optarg); + break; case 'h': case 'H': len = strlen(optarg) > 64 ? 64 : strlen(optarg); @@ -379,6 +384,8 @@ int main(int argc, char **argv) alloc_dhcp_client_id_option(1, client_config.arp, 6); } + open_leasefile(); + if (chdir(chroot_dir)) { printf("Failed to chdir(%s)!\n", chroot_dir); exit(EXIT_FAILURE); diff --git a/ndhc/netlink.c b/ndhc/netlink.c old mode 100644 new mode 100755 diff --git a/ndhc/netlink.h b/ndhc/netlink.h old mode 100644 new mode 100755 diff --git a/ndhc/options.c b/ndhc/options.c old mode 100644 new mode 100755 diff --git a/ndhc/options.h b/ndhc/options.h old mode 100644 new mode 100755 diff --git a/ndhc/packet.c b/ndhc/packet.c old mode 100644 new mode 100755 diff --git a/ndhc/packet.h b/ndhc/packet.h old mode 100644 new mode 100755 diff --git a/ndhc/socket.c b/ndhc/socket.c old mode 100644 new mode 100755 diff --git a/ndhc/socket.h b/ndhc/socket.h old mode 100644 new mode 100755 diff --git a/ndhc/sys.c b/ndhc/sys.c old mode 100644 new mode 100755 diff --git a/ndhc/sys.h b/ndhc/sys.h old mode 100644 new mode 100755 diff --git a/ndhc/timeout.c b/ndhc/timeout.c old mode 100644 new mode 100755 diff --git a/ndhc/timeout.h b/ndhc/timeout.h old mode 100644 new mode 100755