"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/lease.c" between
dnsmasq-2.80.tar.gz and dnsmasq-2.81.tar.xz

About: Dnsmasq is a lightweight caching DNS forwarder and DHCP server.

lease.c  (dnsmasq-2.80):lease.c  (dnsmasq-2.81.tar.xz)
/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991, or the Free Software Foundation; version 2 dated June, 1991, or
(at your option) version 3 dated 29 June, 2007. (at your option) version 3 dated 29 June, 2007.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
skipping to change at line 27 skipping to change at line 27
#include "dnsmasq.h" #include "dnsmasq.h"
#ifdef HAVE_DHCP #ifdef HAVE_DHCP
static struct dhcp_lease *leases = NULL, *old_leases = NULL; static struct dhcp_lease *leases = NULL, *old_leases = NULL;
static int dns_dirty, file_dirty, leases_left; static int dns_dirty, file_dirty, leases_left;
static int read_leases(time_t now, FILE *leasestream) static int read_leases(time_t now, FILE *leasestream)
{ {
unsigned long ei; unsigned long ei;
struct all_addr addr; union all_addr addr;
struct dhcp_lease *lease; struct dhcp_lease *lease;
int clid_len, hw_len, hw_type; int clid_len, hw_len, hw_type;
int items; int items;
char *domain = NULL; char *domain = NULL;
*daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0'; *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0';
/* client-id max length is 255 which is 255*2 digits + 254 colons /* client-id max length is 255 which is 255*2 digits + 254 colons
borrow DNS packet buffer which is always larger than 1000 bytes borrow DNS packet buffer which is always larger than 1000 bytes
skipping to change at line 63 skipping to change at line 63
if (daemon->duid_len < 0) if (daemon->duid_len < 0)
return 0; return 0;
daemon->duid = safe_malloc(daemon->duid_len); daemon->duid = safe_malloc(daemon->duid_len);
memcpy(daemon->duid, daemon->dhcp_buff2, daemon->duid_len); memcpy(daemon->duid, daemon->dhcp_buff2, daemon->duid_len);
continue; continue;
} }
#endif #endif
if (fscanf(leasestream, " %64s %255s %764s", if (fscanf(leasestream, " %64s %255s %764s",
daemon->namebuff, daemon->dhcp_buff, daemon->packet) != 3) daemon->namebuff, daemon->dhcp_buff, daemon->packet) != 3)
return 0; {
my_syslog(MS_DHCP | LOG_WARNING, _("ignoring invalid line in lease da
tabase: %s %s %s %s ..."),
daemon->dhcp_buff3, daemon->dhcp_buff2,
daemon->namebuff, daemon->dhcp_buff);
continue;
}
if (inet_pton(AF_INET, daemon->namebuff, &addr.addr.addr4)) if (inet_pton(AF_INET, daemon->namebuff, &addr.addr4))
{ {
if ((lease = lease4_allocate(addr.addr.addr4))) if ((lease = lease4_allocate(addr.addr4)))
domain = get_domain(lease->addr); domain = get_domain(lease->addr);
hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_ buff2, DHCP_CHADDR_MAX, NULL, &hw_type); hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_ buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
/* For backwards compatibility, no explicit MAC address type means et her. */ /* For backwards compatibility, no explicit MAC address type means et her. */
if (hw_type == 0 && hw_len != 0) if (hw_type == 0 && hw_len != 0)
hw_type = ARPHRD_ETHER; hw_type = ARPHRD_ETHER;
} }
#ifdef HAVE_DHCP6 #ifdef HAVE_DHCP6
else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6)) else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr6))
{ {
char *s = daemon->dhcp_buff2; char *s = daemon->dhcp_buff2;
int lease_type = LEASE_NA; int lease_type = LEASE_NA;
if (s[0] == 'T') if (s[0] == 'T')
{ {
lease_type = LEASE_TA; lease_type = LEASE_TA;
s++; s++;
} }
if ((lease = lease6_allocate(&addr.addr.addr6, lease_type))) if ((lease = lease6_allocate(&addr.addr6, lease_type)))
{ {
lease_set_iaid(lease, strtoul(s, NULL, 10)); lease_set_iaid(lease, strtoul(s, NULL, 10));
domain = get_domain6(&lease->addr6); domain = get_domain6(&lease->addr6);
} }
} }
#endif #endif
else else
return 0; {
my_syslog(MS_DHCP | LOG_WARNING, _("ignoring invalid line in lease da
tabase, bad address: %s"),
daemon->namebuff);
continue;
}
if (!lease) if (!lease)
die (_("too many stored leases"), NULL, EC_MISC); die (_("too many stored leases"), NULL, EC_MISC);
if (strcmp(daemon->packet, "*") != 0) if (strcmp(daemon->packet, "*") != 0)
clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 2 55, NULL, NULL); clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 2 55, NULL, NULL);
lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned ch ar *)daemon->packet, lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned ch ar *)daemon->packet,
hw_len, hw_type, clid_len, now, 0); hw_len, hw_type, clid_len, now, 0);
skipping to change at line 175 skipping to change at line 184
if (!leasestream) if (!leasestream)
die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_ FILE); die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_ FILE);
/* a+ mode leaves pointer at end. */ /* a+ mode leaves pointer at end. */
rewind(leasestream); rewind(leasestream);
} }
if (leasestream) if (leasestream)
{ {
if (!read_leases(now, leasestream)) if (!read_leases(now, leasestream))
my_syslog(MS_DHCP | LOG_ERR, _("failed to parse lease database, invalid l my_syslog(MS_DHCP | LOG_ERR, _("failed to parse lease database cleanly"))
ine: %s %s %s %s ..."), ;
daemon->dhcp_buff3, daemon->dhcp_buff2,
daemon->namebuff, daemon->dhcp_buff);
if (ferror(leasestream)) if (ferror(leasestream))
die(_("failed to read lease file %s: %s"), daemon->lease_file, EC_FILE); die(_("failed to read lease file %s: %s"), daemon->lease_file, EC_FILE);
} }
#ifdef HAVE_SCRIPT #ifdef HAVE_SCRIPT
if (!daemon->lease_stream) if (!daemon->lease_stream)
{ {
int rc = 0; int rc = 0;
skipping to change at line 225 skipping to change at line 232
/* changes to the config may change current leases. */ /* changes to the config may change current leases. */
struct dhcp_lease *lease; struct dhcp_lease *lease;
struct dhcp_config *config; struct dhcp_config *config;
char *name; char *name;
for (lease = leases; lease; lease = lease->next) for (lease = leases; lease; lease = lease->next)
if (lease->flags & (LEASE_TA | LEASE_NA)) if (lease->flags & (LEASE_TA | LEASE_NA))
continue; continue;
else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease-> clid_len, else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease-> clid_len,
lease->hwaddr, lease->hwaddr_len, lease->hwadd r_type, NULL)) && lease->hwaddr, lease->hwaddr_len, lease->hwadd r_type, NULL, NULL)) &&
(config->flags & CONFIG_NAME) && (config->flags & CONFIG_NAME) &&
(!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->add r.s_addr)) (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->add r.s_addr))
lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NU LL); lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NU LL);
else if ((name = host_from_dns(lease->addr))) else if ((name = host_from_dns(lease->addr)))
lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* upda tes auth flag only */ lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* upda tes auth flag only */
} }
static void ourprintf(int *errp, char *format, ...) static void ourprintf(int *errp, char *format, ...)
{ {
va_list ap; va_list ap;
skipping to change at line 512 skipping to change at line 519
if (lease->flags & (LEASE_TA | LEASE_NA)) if (lease->flags & (LEASE_TA | LEASE_NA))
prot = AF_INET6; prot = AF_INET6;
else if (lease->hostname || lease->fqdn) else if (lease->hostname || lease->fqdn)
{ {
struct slaac_address *slaac; struct slaac_address *slaac;
for (slaac = lease->slaac_address; slaac; slaac = slaac->next) for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
if (slaac->backoff == 0) if (slaac->backoff == 0)
{ {
if (lease->fqdn) if (lease->fqdn)
cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_add r *)&slaac->addr, lease->expires); cache_add_dhcp_entry(lease->fqdn, AF_INET6, (union all_addr *)&slaac->addr, lease->expires);
if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all _addr *)&slaac->addr, lease->expires); cache_add_dhcp_entry(lease->hostname, AF_INET6, (union all_ addr *)&slaac->addr, lease->expires);
} }
} }
if (lease->fqdn) if (lease->fqdn)
cache_add_dhcp_entry(lease->fqdn, prot, cache_add_dhcp_entry(lease->fqdn, prot,
prot == AF_INET ? (struct all_addr *)&lease->add r : (struct all_addr *)&lease->addr6, prot == AF_INET ? (union all_addr *)&lease->addr : (union all_addr *)&lease->addr6,
lease->expires); lease->expires);
if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
cache_add_dhcp_entry(lease->hostname, prot, cache_add_dhcp_entry(lease->hostname, prot,
prot == AF_INET ? (struct all_addr *)&lease->add r : (struct all_addr *)&lease->addr6, prot == AF_INET ? (union all_addr *)&lease->addr : (union all_addr *)&lease->addr6,
lease->expires); lease->expires);
#else #else
if (lease->fqdn) if (lease->fqdn)
cache_add_dhcp_entry(lease->fqdn, prot, (struct all_addr *)&lease->ad dr, lease->expires); cache_add_dhcp_entry(lease->fqdn, prot, (union all_addr *)&lease->add r, lease->expires);
if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
cache_add_dhcp_entry(lease->hostname, prot, (struct all_addr *)&lease ->addr, lease->expires); cache_add_dhcp_entry(lease->hostname, prot, (union all_addr *)&lease- >addr, lease->expires);
#endif #endif
} }
dns_dirty = 0; dns_dirty = 0;
} }
} }
void lease_prune(struct dhcp_lease *target, time_t now) void lease_prune(struct dhcp_lease *target, time_t now)
{ {
struct dhcp_lease *lease, *tmp, **up; struct dhcp_lease *lease, *tmp, **up;
for (lease = leases, up = &leases; lease; lease = tmp) for (lease = leases, up = &leases; lease; lease = tmp)
{ {
tmp = lease->next; tmp = lease->next;
if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target) if ((lease->expires != 0 && difftime(now, lease->expires) >= 0) || lease = = target)
{ {
file_dirty = 1; file_dirty = 1;
if (lease->hostname) if (lease->hostname)
dns_dirty = 1; dns_dirty = 1;
daemon->metrics[lease->addr.s_addr ? METRIC_LEASES_PRUNED_4 : METRIC_LE ASES_PRUNED_6]++; daemon->metrics[lease->addr.s_addr ? METRIC_LEASES_PRUNED_4 : METRIC_LE ASES_PRUNED_6]++;
*up = lease->next; /* unlink */ *up = lease->next; /* unlink */
/* Put on old_leases list 'till we /* Put on old_leases list 'till we
skipping to change at line 624 skipping to change at line 631
if (lease->addr.s_addr == addr.s_addr) if (lease->addr.s_addr == addr.s_addr)
return lease; return lease;
} }
return NULL; return NULL;
} }
#ifdef HAVE_DHCP6 #ifdef HAVE_DHCP6
/* find address for {CLID, IAID, address} */ /* find address for {CLID, IAID, address} */
struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len, struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
int lease_type, int iaid, struct in6_addr *addr) int lease_type, unsigned int iaid,
struct in6_addr *addr)
{ {
struct dhcp_lease *lease; struct dhcp_lease *lease;
for (lease = leases; lease; lease = lease->next) for (lease = leases; lease; lease = lease->next)
{ {
if (!(lease->flags & lease_type) || lease->iaid != iaid) if (!(lease->flags & lease_type) || lease->iaid != iaid)
continue; continue;
if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr)) if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr))
continue; continue;
skipping to change at line 656 skipping to change at line 664
/* reset "USED flags */ /* reset "USED flags */
void lease6_reset(void) void lease6_reset(void)
{ {
struct dhcp_lease *lease; struct dhcp_lease *lease;
for (lease = leases; lease; lease = lease->next) for (lease = leases; lease; lease = lease->next)
lease->flags &= ~LEASE_USED; lease->flags &= ~LEASE_USED;
} }
/* enumerate all leases belonging to {CLID, IAID} */ /* enumerate all leases belonging to {CLID, IAID} */
struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_typ struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_typ
e, unsigned char *clid, int clid_len, int iaid) e,
unsigned char *clid, int clid_len,
unsigned int iaid)
{ {
struct dhcp_lease *lease; struct dhcp_lease *lease;
if (!first) if (!first)
first = leases; first = leases;
else else
first = first->next; first = first->next;
for (lease = first; lease; lease = lease->next) for (lease = first; lease; lease = lease->next)
{ {
skipping to change at line 822 skipping to change at line 832
thing we can do. */ thing we can do. */
if (difftime(exp, now) <= 0.0) if (difftime(exp, now) <= 0.0)
exp = 0; exp = 0;
} }
if (exp != lease->expires) if (exp != lease->expires)
{ {
dns_dirty = 1; dns_dirty = 1;
lease->expires = exp; lease->expires = exp;
#ifndef HAVE_BROKEN_RTC #ifndef HAVE_BROKEN_RTC
lease->flags |= LEASE_AUX_CHANGED; lease->flags |= LEASE_AUX_CHANGED | LEASE_EXP_CHANGED;
file_dirty = 1; file_dirty = 1;
#endif #endif
} }
#ifdef HAVE_BROKEN_RTC #ifdef HAVE_BROKEN_RTC
if (len != lease->length) if (len != lease->length)
{ {
lease->length = len; lease->length = len;
lease->flags |= LEASE_AUX_CHANGED; lease->flags |= LEASE_AUX_CHANGED;
file_dirty = 1; file_dirty = 1;
} }
#endif #endif
} }
#ifdef HAVE_DHCP6 #ifdef HAVE_DHCP6
void lease_set_iaid(struct dhcp_lease *lease, int iaid) void lease_set_iaid(struct dhcp_lease *lease, unsigned int iaid)
{ {
if (lease->iaid != iaid) if (lease->iaid != iaid)
{ {
lease->iaid = iaid; lease->iaid = iaid;
lease->flags |= LEASE_CHANGED; lease->flags |= LEASE_CHANGED;
} }
} }
#endif #endif
void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr, void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
skipping to change at line 1122 skipping to change at line 1132
#ifdef HAVE_SCRIPT #ifdef HAVE_SCRIPT
queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
#endif #endif
free(lease->old_hostname); free(lease->old_hostname);
lease->old_hostname = NULL; lease->old_hostname = NULL;
return 1; return 1;
} }
for (lease = leases; lease; lease = lease->next) for (lease = leases; lease; lease = lease->next)
if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) || if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO))) ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)) ||
((lease->flags & LEASE_EXP_CHANGED) && option_bool(OPT_LEASE_RENEW)))
{ {
#ifdef HAVE_SCRIPT #ifdef HAVE_SCRIPT
queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
lease->fqdn ? lease->fqdn : lease->hostname, now); lease->fqdn ? lease->fqdn : lease->hostname, now);
#endif #endif
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, le ase, emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, le ase,
lease->fqdn ? lease->fqdn : lease->hostname); lease->fqdn ? lease->fqdn : lease->hostname);
#endif #endif
lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED); lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED | LEASE_E XP_CHANGED);
/* this is used for the "add" call, then junked, since they're not in the database */ /* this is used for the "add" call, then junked, since they're not in the database */
free(lease->extradata); free(lease->extradata);
lease->extradata = NULL; lease->extradata = NULL;
return 1; return 1;
} }
return 0; /* nothing to do */ return 0; /* nothing to do */
} }
 End of changes. 23 change blocks. 
27 lines changed or deleted 40 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)