1 diff --git a/Makefile b/Makefile
2 index 58a7975..616c6b7 100644
5 @@ -69,7 +69,7 @@ objs = cache.o rfc1035.o util.o option.o forward.o network.o \
6 dnsmasq.o dhcp.o lease.o rfc2131.o netlink.o dbus.o bpf.o \
7 helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
8 dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
9 - domain.o dnssec.o blockdata.o tables.o loop.o
10 + domain.o dnssec.o blockdata.o tables.o loop.o isc.o
12 hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
13 dns-protocol.h radv-protocol.h ip6addr.h
14 diff --git a/src/cache.c b/src/cache.c
15 index 2c3a498..77a7046 100644
21 static struct crec *cache_head = NULL, *cache_tail = NULL, **hash_table = NULL;
23 +#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
24 static struct crec *dhcp_spare = NULL;
26 static struct crec *new_chain = NULL;
27 @@ -222,6 +222,9 @@ static void cache_free(struct crec *crecp)
28 crecp->flags &= ~F_BIGNAME;
31 + if (crecp->flags & F_DHCP)
32 + free(crecp->name.namep);
35 cache_blockdata_free(crecp);
37 @@ -1110,7 +1113,7 @@ void cache_reload(void)
38 total_size = read_hostsfile(ah->fname, ah->index, total_size, (struct crec **)daemon->packet, revhashsz);
42 +#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
43 struct in_addr a_record_from_hosts(char *name, time_t now)
45 struct crec *crecp = NULL;
46 @@ -1188,7 +1191,7 @@ void cache_add_dhcp_entry(char *host_name, int prot,
47 addrlen = sizeof(struct in6_addr);
52 inet_ntop(prot, host_address, daemon->addrbuff, ADDRSTRLEN);
54 while ((crec = cache_find_by_name(crec, host_name, 0, flags | F_CNAME)))
55 @@ -1253,7 +1256,11 @@ void cache_add_dhcp_entry(char *host_name, int prot,
58 crec->addr.addr = *host_address;
59 +#ifdef HAVE_ISC_READER
60 + crec->name.namep = strdup(host_name);
62 crec->name.namep = host_name;
64 crec->uid = next_uid();
67 diff --git a/src/dnsmasq.c b/src/dnsmasq.c
68 index f4a89fc..a448ec4 100644
71 @@ -940,6 +940,11 @@ int main (int argc, char **argv)
73 poll_resolv(0, daemon->last_resolv != 0, now);
74 daemon->last_resolv = now;
76 +#ifdef HAVE_ISC_READER
77 + if (daemon->lease_file && !daemon->dhcp)
82 if (FD_ISSET(piperead, &rset))
83 diff --git a/src/dnsmasq.h b/src/dnsmasq.h
84 index e74b15a..4a35168 100644
87 @@ -1463,9 +1463,13 @@ time_t periodic_slaac(time_t now, struct dhcp_lease *leases);
88 void slaac_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface, struct dhcp_lease *leases);
92 +#ifdef HAVE_ISC_READER
93 +void load_dhcp(time_t now);
98 void loop_send_probes();
99 int detect_loop(char *query, int type);
102 diff --git a/src/isc.c b/src/isc.c
104 index 0000000..5106442
108 +/* dnsmasq is Copyright (c) 2014 John Volpe, Simon Kelley and
111 + This program is free software; you can redistribute it and/or modify
112 + it under the terms of the GNU General Public License as published by
113 + the Free Software Foundation; version 2 dated June, 1991, or
114 + (at your option) version 3 dated 29 June, 2007.
116 + This program is distributed in the hope that it will be useful,
117 + but WITHOUT ANY WARRANTY; without even the implied warranty of
118 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
119 + GNU General Public License for more details.
121 + You should have received a copy of the GNU General Public License
122 + along with this program. If not, see <http://www.gnu.org/licenses/>.
124 + Code in this file is based on contributions by John Volpe and
125 + Simon Kelley. Updated for recent versions of dnsmasq by
129 +#include "dnsmasq.h"
131 +#ifdef HAVE_ISC_READER
134 +struct isc_dhcp_lease {
138 + struct in_addr addr;
139 + struct isc_dhcp_lease* next;
142 +static struct isc_dhcp_lease* dhcp_lease_new(const char* hostname) {
143 + struct isc_dhcp_lease* lease = whine_malloc(sizeof(*lease));
145 + lease->name = strdup(hostname);
146 + if (daemon->domain_suffix) {
147 + asprintf(&lease->fqdn, "%s.%s", hostname, daemon->domain_suffix);
149 + lease->expires = 0;
150 + lease->next = NULL;
155 +static void dhcp_lease_free(struct isc_dhcp_lease* lease) {
166 +static int next_token(char* token, int buffsize, FILE* fp) {
170 + while ((c = getc(fp)) != EOF) {
174 + } while (c != '\n' && c != EOF);
177 + if (c == ' ' || c == '\t' || c == '\n' || c == ';') {
180 + } else if ((c != '"') && (count < buffsize - 1)) {
187 + return count ? 1 : 0;
190 +static long get_utc_offset() {
191 + time_t t = time(NULL);
192 + struct tm* time_struct = localtime(&t);
194 + return time_struct->tm_gmtoff;
197 +static time_t parse_lease_time(const char* token_date, const char* token_time) {
198 + time_t time = (time_t)(-1);
199 + struct tm lease_time;
201 + if (sscanf(token_date, "%d/%d/%d", &lease_time.tm_year, &lease_time.tm_mon, &lease_time.tm_mday) == 3) {
202 + lease_time.tm_year -= 1900;
203 + lease_time.tm_mon -= 1;
205 + if (sscanf(token_time, "%d:%d:%d", &lease_time.tm_hour, &lease_time.tm_min, &lease_time.tm_sec) == 3) {
206 + time = mktime(&lease_time) + get_utc_offset();
213 +static struct isc_dhcp_lease* find_lease(const char* hostname, struct isc_dhcp_lease* leases) {
214 + struct isc_dhcp_lease* lease = leases;
217 + if (strcmp(hostname, lease->name) == 0) {
220 + lease = lease->next;
226 +static off_t lease_file_size = (off_t)0;
227 +static ino_t lease_file_inode = (ino_t)0;
229 +void load_dhcp(time_t now) {
230 + struct isc_dhcp_lease* leases = NULL;
232 + struct stat statbuf;
233 + if (stat(daemon->lease_file, &statbuf) == -1) {
237 + /* Do nothing if the lease file has not changed. */
238 + if ((statbuf.st_size <= lease_file_size) && (statbuf.st_ino == lease_file_inode))
241 + lease_file_size = statbuf.st_size;
242 + lease_file_inode = statbuf.st_ino;
244 + FILE* fp = fopen(daemon->lease_file, "r");
246 + my_syslog(LOG_ERR, _("failed to load %s:%s"), daemon->lease_file, strerror(errno));
250 + my_syslog(LOG_INFO, _("reading %s"), daemon->lease_file);
252 + char* hostname = daemon->namebuff;
253 + struct in_addr host_address;
254 + time_t time_starts = -1;
255 + time_t time_ends = -1;
258 + char token[MAXTOK];
259 + while ((next_token(token, MAXTOK, fp))) {
260 + if (strcmp(token, "lease") == 0) {
261 + hostname[0] = '\0';
263 + if (next_token(token, MAXTOK, fp) && ((host_address.s_addr = inet_addr(token)) != (in_addr_t)-1)) {
264 + if (next_token(token, MAXTOK, fp) && *token == '{') {
265 + while (next_token(token, MAXTOK, fp) && *token != '}') {
266 + if ((strcmp(token, "client-hostname") == 0) || (strcmp(token, "hostname") == 0)) {
267 + if (next_token(hostname, MAXDNAME, fp)) {
268 + if (!canonicalise(hostname, &nomem)) {
270 + my_syslog(LOG_ERR, _("bad name in %s"), daemon->lease_file);
273 + } else if ((strcmp(token, "starts") == 0) || (strcmp(token, "ends") == 0)) {
274 + char token_date[MAXTOK];
275 + char token_time[MAXTOK];
277 + int is_starts = strcmp(token, "starts") == 0;
279 + // Throw away the weekday and parse the date.
280 + if (next_token(token, MAXTOK, fp) && next_token(token_date, MAXTOK, fp) && next_token(token_time, MAXTOK, fp)) {
281 + time_t time = parse_lease_time(token_date, token_time);
284 + time_starts = time;
294 + if ((time_starts == -1) || (time_ends == -1))
297 + if (difftime(now, time_ends) > 0)
300 + char* dot = strchr(hostname, '.');
302 + if (!daemon->domain_suffix || hostname_isequal(dot + 1, daemon->domain_suffix)) {
303 + my_syslog(LOG_WARNING,
304 + _("Ignoring DHCP lease for %s because it has an illegal domain part"),
311 + // Search for an existing lease in the list
312 + // with the given host name and update the data
314 + struct isc_dhcp_lease* lease = find_lease(hostname, leases);
316 + // If no lease already exists, we create a new one
317 + // and append it to the list.
319 + lease = dhcp_lease_new(hostname);
321 + lease->next = leases;
325 + // Only update more recent leases.
326 + if (lease->expires > time_ends)
329 + lease->addr = host_address;
330 + lease->expires = time_ends;
338 + // Drop all entries.
339 + cache_unhash_dhcp();
342 + struct isc_dhcp_lease *lease = leases;
343 + leases = lease->next;
346 + cache_add_dhcp_entry(lease->fqdn, AF_INET, (struct all_addr*)&lease->addr.s_addr, lease->expires);
350 + cache_add_dhcp_entry(lease->name, AF_INET, (struct all_addr*)&lease->addr.s_addr, lease->expires);
354 + dhcp_lease_free(lease);
359 diff --git a/src/option.c b/src/option.c
360 index 45d8875..29c9ee5 100644
363 @@ -1669,7 +1669,7 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
364 ret_err(_("bad MX target"));
368 +#if (defined HAVE_DHCP) || (defined HAVE_ISC_READER)
369 case 'l': /* --dhcp-leasefile */
370 daemon->lease_file = opt_string_alloc(arg);