/* dnsmasq is Copyright (c) 2000 - 2004 by Simon Kelley 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; version 2 dated June, 1991. 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. */ /* Code in this file is based on contributions by John Volpe. */ #include "dnsmasq.h" #ifdef HAVE_ISC_READER struct isc_lease { char *name, *fqdn; time_t expires; struct in_addr addr; struct isc_lease *next; }; static struct isc_lease *leases = NULL; static off_t lease_file_size = (off_t)0; static ino_t lease_file_inode = (ino_t)0; static int logged_lease = 0; static int next_token (char *token, int buffsize, FILE * fp) { int c, count = 0; char *cp = token; while((c = getc(fp)) != EOF) { if (c == '#') do { c = getc(fp); } while (c != '\n' && c != EOF); if (c == ' ' || c == '\t' || c == '\n' || c == ';') { if (count) break; } else if ((c != '"') && (count 1 /* Days in months this year */ ? months [lease_time.tm_mon - 2] : 0) + (lease_time.tm_mon > 2 && /* Leap day this year */ !((lease_time.tm_year - 1972) & 3)) + lease_time.tm_mday - 1) * 24) + /* Day of month */ lease_time.tm_hour) * 60) + lease_time.tm_min) * 60) + lease_time.tm_sec; if (is_ends) ttd = time; else tts = time; } } } /* missing info? */ if (!*hostname) continue; if (ttd == (time_t)(-1)) continue; /* We use 0 as infinite in ttd */ if ((tts != -1) && (ttd == tts - 1)) ttd = (time_t)0; else if (difftime(now, ttd) > 0) continue; if ((dot = strchr(hostname, '.'))) { if (!suffix || hostname_isequal(dot+1, suffix)) { #ifdef USE_SYSLOG /* foxconn wklin added, 08/13/2007 */ syslog(LOG_WARNING, "Ignoring DHCP lease for %s because it has an illegal domain part", hostname); #endif continue; } *dot = 0; } for (lease = leases; lease; lease = lease->next) if (hostname_isequal(lease->name, hostname)) { lease->expires = ttd; lease->addr = host_address; break; } if (!lease && (lease = malloc(sizeof(struct isc_lease)))) { lease->expires = ttd; lease->addr = host_address; lease->fqdn = NULL; lease->next = leases; if (!(lease->name = malloc(strlen(hostname)+1))) free(lease); else { leases = lease; strcpy(lease->name, hostname); if (suffix && (lease->fqdn = malloc(strlen(hostname) + strlen(suffix) + 2))) { strcpy(lease->fqdn, hostname); strcat(lease->fqdn, "."); strcat(lease->fqdn, suffix); } } } } } } } fclose(fp); /* prune expired leases */ for (lease = leases, up = &leases; lease; lease = tmp) { tmp = lease->next; if (lease->expires != (time_t)0 && difftime(now, lease->expires) > 0) { *up = lease->next; /* unlink */ free(lease->name); if (lease->fqdn) free(lease->fqdn); free(lease); } else up = &lease->next; } /* remove all existing DHCP cache entries */ cache_unhash_dhcp(); for (lease = leases; lease; lease = lease->next) { cache_add_dhcp_entry(lease->fqdn, &lease->addr, lease->expires); cache_add_dhcp_entry(lease->name, &lease->addr, lease->expires); } } #endif