Add support for writing lease files.
This commit is contained in:
3
ndhc.sh
3
ndhc.sh
@ -14,6 +14,7 @@ ndhc_start()
|
|||||||
{
|
{
|
||||||
local args= opt= opts= pidfile="/var/run/ndhc-${IFACE}.pid"
|
local args= opt= opts= pidfile="/var/run/ndhc-${IFACE}.pid"
|
||||||
local sendhost=true
|
local sendhost=true
|
||||||
|
local leasefile="/var/state/${IFACE}.lease"
|
||||||
|
|
||||||
eval args=\$ndhc_${IFVAR}
|
eval args=\$ndhc_${IFVAR}
|
||||||
|
|
||||||
@ -63,7 +64,7 @@ ndhc_start()
|
|||||||
|
|
||||||
eval "${x}" "${args}" -r `cat /etc/firewall/tmp/OLDEXTIP` \
|
eval "${x}" "${args}" -r `cat /etc/firewall/tmp/OLDEXTIP` \
|
||||||
-n -i "${IFACE}" -u "ndhc" -C "/var/lib/ndhc" \
|
-n -i "${IFACE}" -u "ndhc" -C "/var/lib/ndhc" \
|
||||||
-p "${pidfile}" >/dev/null
|
-p "${pidfile}" -l "${leasefile}" >/dev/null
|
||||||
eend $? || return 1
|
eend $? || return 1
|
||||||
|
|
||||||
_show_address
|
_show_address
|
||||||
|
13
ndhc/CMakeLists.txt
Normal file → Executable file
13
ndhc/CMakeLists.txt
Normal file → Executable file
@ -2,18 +2,7 @@ project (ndhc)
|
|||||||
|
|
||||||
cmake_minimum_required (VERSION 2.6)
|
cmake_minimum_required (VERSION 2.6)
|
||||||
|
|
||||||
set(NDHC_SRCS
|
file(GLOB NDHC_SRCS "*.c")
|
||||||
sys.c
|
|
||||||
options.c
|
|
||||||
socket.c
|
|
||||||
packet.c
|
|
||||||
timeout.c
|
|
||||||
ifchange.c
|
|
||||||
dhcpmsg.c
|
|
||||||
arp.c
|
|
||||||
netlink.c
|
|
||||||
ndhc.c
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(ndhc ${NDHC_SRCS})
|
add_executable(ndhc ${NDHC_SRCS})
|
||||||
target_link_libraries(ndhc ncmlib)
|
target_link_libraries(ndhc ncmlib)
|
||||||
|
0
ndhc/README
Normal file → Executable file
0
ndhc/README
Normal file → Executable file
4
ndhc/arp.c
Normal file → Executable file
4
ndhc/arp.c
Normal file → Executable file
@ -1,5 +1,5 @@
|
|||||||
/* arp.c - arp ping checking
|
/* 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>
|
* Copyright 2010-2011 Nicholas J. Kain <njkain@gmail.com>
|
||||||
*
|
*
|
||||||
@ -37,6 +37,7 @@
|
|||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "sys.h"
|
#include "sys.h"
|
||||||
#include "ifchange.h"
|
#include "ifchange.h"
|
||||||
|
#include "leasefile.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "strl.h"
|
#include "strl.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
@ -209,6 +210,7 @@ void arp_success(struct client_state_t *cs)
|
|||||||
cs->arpPrevState == DS_REBINDING)
|
cs->arpPrevState == DS_REBINDING)
|
||||||
? IFCHANGE_RENEW : IFCHANGE_BOUND));
|
? IFCHANGE_RENEW : IFCHANGE_BOUND));
|
||||||
change_listen_mode(cs, LM_NONE);
|
change_listen_mode(cs, LM_NONE);
|
||||||
|
write_leasefile(temp_addr);
|
||||||
if (client_config.quit_after_lease)
|
if (client_config.quit_after_lease)
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
if (!client_config.foreground)
|
if (!client_config.foreground)
|
||||||
|
0
ndhc/arp.h
Normal file → Executable file
0
ndhc/arp.h
Normal file → Executable file
0
ndhc/config.h
Normal file → Executable file
0
ndhc/config.h
Normal file → Executable file
0
ndhc/dhcpmsg.c
Normal file → Executable file
0
ndhc/dhcpmsg.c
Normal file → Executable file
0
ndhc/dhcpmsg.h
Normal file → Executable file
0
ndhc/dhcpmsg.h
Normal file → Executable file
0
ndhc/ifchange.c
Normal file → Executable file
0
ndhc/ifchange.c
Normal file → Executable file
0
ndhc/ifchange.h
Normal file → Executable file
0
ndhc/ifchange.h
Normal file → Executable file
76
ndhc/leasefile.c
Normal file
76
ndhc/leasefile.c
Normal 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
23
ndhc/leasefile.h
Normal 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
11
ndhc/ndhc.c
Normal file → Executable file
@ -1,5 +1,5 @@
|
|||||||
/* ndhc.c - DHCP client
|
/* 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>
|
* (c) 2004-2011 Nicholas J. Kain <njkain at gmail dot com>
|
||||||
*
|
*
|
||||||
@ -49,6 +49,7 @@
|
|||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "arp.h"
|
#include "arp.h"
|
||||||
#include "netlink.h"
|
#include "netlink.h"
|
||||||
|
#include "leasefile.h"
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "chroot.h"
|
#include "chroot.h"
|
||||||
@ -274,6 +275,7 @@ int main(int argc, char **argv)
|
|||||||
{"foreground", no_argument, 0, 'f'},
|
{"foreground", no_argument, 0, 'f'},
|
||||||
{"background", no_argument, 0, 'b'},
|
{"background", no_argument, 0, 'b'},
|
||||||
{"pidfile", required_argument, 0, 'p'},
|
{"pidfile", required_argument, 0, 'p'},
|
||||||
|
{"leasefile", required_argument, 0, 'l'},
|
||||||
{"hostname", required_argument, 0, 'H'},
|
{"hostname", required_argument, 0, 'H'},
|
||||||
{"hostname", required_argument, 0, 'h'},
|
{"hostname", required_argument, 0, 'h'},
|
||||||
{"interface", required_argument, 0, 'i'},
|
{"interface", required_argument, 0, 'i'},
|
||||||
@ -290,7 +292,7 @@ int main(int argc, char **argv)
|
|||||||
/* get options */
|
/* get options */
|
||||||
while (1) {
|
while (1) {
|
||||||
int option_index = 0;
|
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);
|
&option_index);
|
||||||
if (c == -1) break;
|
if (c == -1) break;
|
||||||
|
|
||||||
@ -313,6 +315,9 @@ int main(int argc, char **argv)
|
|||||||
case 'p':
|
case 'p':
|
||||||
strlcpy(pidfile, optarg, sizeof pidfile);
|
strlcpy(pidfile, optarg, sizeof pidfile);
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
set_leasefile(optarg);
|
||||||
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
case 'H':
|
case 'H':
|
||||||
len = strlen(optarg) > 64 ? 64 : strlen(optarg);
|
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);
|
alloc_dhcp_client_id_option(1, client_config.arp, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open_leasefile();
|
||||||
|
|
||||||
if (chdir(chroot_dir)) {
|
if (chdir(chroot_dir)) {
|
||||||
printf("Failed to chdir(%s)!\n", chroot_dir);
|
printf("Failed to chdir(%s)!\n", chroot_dir);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
0
ndhc/netlink.c
Normal file → Executable file
0
ndhc/netlink.c
Normal file → Executable file
0
ndhc/netlink.h
Normal file → Executable file
0
ndhc/netlink.h
Normal file → Executable file
0
ndhc/options.c
Normal file → Executable file
0
ndhc/options.c
Normal file → Executable file
0
ndhc/options.h
Normal file → Executable file
0
ndhc/options.h
Normal file → Executable file
0
ndhc/packet.c
Normal file → Executable file
0
ndhc/packet.c
Normal file → Executable file
0
ndhc/packet.h
Normal file → Executable file
0
ndhc/packet.h
Normal file → Executable file
0
ndhc/socket.c
Normal file → Executable file
0
ndhc/socket.c
Normal file → Executable file
0
ndhc/socket.h
Normal file → Executable file
0
ndhc/socket.h
Normal file → Executable file
0
ndhc/sys.c
Normal file → Executable file
0
ndhc/sys.c
Normal file → Executable file
0
ndhc/sys.h
Normal file → Executable file
0
ndhc/sys.h
Normal file → Executable file
0
ndhc/timeout.c
Normal file → Executable file
0
ndhc/timeout.c
Normal file → Executable file
0
ndhc/timeout.h
Normal file → Executable file
0
ndhc/timeout.h
Normal file → Executable file
Reference in New Issue
Block a user