Add support for writing lease files.

This commit is contained in:
Nicholas J. Kain 2011-04-19 16:37:43 -04:00
parent c7ff945be8
commit da193ae35a
25 changed files with 114 additions and 16 deletions

View File

@ -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

13
ndhc/CMakeLists.txt Normal file → Executable file
View File

@ -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)

0
ndhc/README Normal file → Executable file
View File

4
ndhc/arp.c Normal file → Executable file
View File

@ -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 <njkain@gmail.com>
*
@ -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)

0
ndhc/arp.h Normal file → Executable file
View File

0
ndhc/config.h Normal file → Executable file
View File

0
ndhc/dhcpmsg.c Normal file → Executable file
View File

0
ndhc/dhcpmsg.h Normal file → Executable file
View File

0
ndhc/ifchange.c Normal file → Executable file
View File

0
ndhc/ifchange.h Normal file → Executable file
View File

76
ndhc/leasefile.c Normal file
View File

@ -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 <njkain at gmail dot com>
*
* 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 <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#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");
}

23
ndhc/leasefile.h Normal file
View File

@ -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 <njkain at gmail dot com>
*
* 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);

11
ndhc/ndhc.c Normal file → Executable file
View File

@ -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 <njkain at gmail dot com>
*
@ -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);

0
ndhc/netlink.c Normal file → Executable file
View File

0
ndhc/netlink.h Normal file → Executable file
View File

0
ndhc/options.c Normal file → Executable file
View File

0
ndhc/options.h Normal file → Executable file
View File

0
ndhc/packet.c Normal file → Executable file
View File

0
ndhc/packet.h Normal file → Executable file
View File

0
ndhc/socket.c Normal file → Executable file
View File

0
ndhc/socket.h Normal file → Executable file
View File

0
ndhc/sys.c Normal file → Executable file
View File

0
ndhc/sys.h Normal file → Executable file
View File

0
ndhc/timeout.c Normal file → Executable file
View File

0
ndhc/timeout.h Normal file → Executable file
View File