1 diff -x CVS -urN dnsmasq-1.18/dhcp.c dnsmasq.old/dhcp.c
2 --- dnsmasq-1.18/dhcp.c 2003-11-05 08:30:20.000000000 -0600
3 +++ dnsmasq.old/dhcp.c 2004-01-05 23:40:11.000000000 -0600
8 -static int next_token (char *token, int buffsize, FILE * fp);
9 +struct dhcpOfferedAddr {
10 + u_int8_t hostname[16];
11 + u_int8_t chaddr[16];
12 + u_int32_t yiaddr; /* network order */
13 + u_int32_t expires; /* host order */
16 void load_dhcp(char *file, char *suffix, time_t now, char *hostname)
18 - char token[MAXTOK], *dot;
20 struct all_addr host_address;
23 FILE *fp = fopen (file, "r");
24 + struct dhcpOfferedAddr lease;
30 /* remove all existing DHCP cache entries */
33 - while ((next_token(token, MAXTOK, fp)))
35 - if (strcmp(token, "lease") == 0)
38 - ttd = tts = (time_t)(-1);
40 - if (next_token(token, MAXTOK, fp) &&
41 - inet_pton(AF_INET, token, &host_address))
43 - if (next_token(token, MAXTOK, fp) &&
44 - (host_address.addr4.s_addr = inet_addr(token)) != (in_addr_t) -1)
47 - if (next_token(token, MAXTOK, fp) && *token == '{')
49 - while (next_token(token, MAXTOK, fp) && *token != '}')
51 + while (fread(&lease, sizeof(lease), 1, fp)) {
52 + host_address.addr.addr4.s_addr = lease.yiaddr;
54 + strcpy(hostname,lease.hostname);
55 + if (lease.expires>(unsigned)now)
56 + ttd = lease.expires;
59 + dot = strchr(hostname, '.');
62 - if ((strcmp(token, "client-hostname") == 0) ||
63 - (strcmp(token, "hostname") == 0))
65 - if (next_token(hostname, MAXDNAME, fp))
66 - if (!canonicalise(hostname))
69 - syslog(LOG_ERR, "bad name in %s", file);
72 - else if ((strcmp(token, "ends") == 0) ||
73 - (strcmp(token, "starts") == 0))
75 - struct tm lease_time;
76 - int is_ends = (strcmp(token, "ends") == 0);
77 - if (next_token(token, MAXTOK, fp) && /* skip weekday */
78 - next_token(token, MAXTOK, fp) && /* Get date from lease file */
79 - sscanf (token, "%d/%d/%d",
80 - &lease_time.tm_year,
82 - &lease_time.tm_mday) == 3 &&
83 - next_token(token, MAXTOK, fp) &&
84 - sscanf (token, "%d:%d:%d:",
85 - &lease_time.tm_hour,
87 - &lease_time.tm_sec) == 3)
89 - /* There doesn't seem to be a universally available library function
90 - which converts broken-down _GMT_ time to seconds-in-epoch.
91 - The following was borrowed from ISC dhcpd sources, where
92 - it is noted that it might not be entirely accurate for odd seconds.
93 - Since we're trying to get the same answer as dhcpd, that's just
95 - static int months [11] = { 31, 59, 90, 120, 151, 181,
96 - 212, 243, 273, 304, 334 };
97 - time_t time = ((((((365 * (lease_time.tm_year - 1970) + /* Days in years since '70 */
98 - (lease_time.tm_year - 1969) / 4 + /* Leap days since '70 */
99 - (lease_time.tm_mon > 1 /* Days in months this year */
100 - ? months [lease_time.tm_mon - 2]
102 - (lease_time.tm_mon > 2 && /* Leap day this year */
103 - !((lease_time.tm_year - 1972) & 3)) +
104 - lease_time.tm_mday - 1) * 24) + /* Day of month */
105 - lease_time.tm_hour) * 60) +
106 - lease_time.tm_min) * 60) + lease_time.tm_sec;
112 + { /* suffix and lease has ending: must match */
113 + if (strcmp(dot+1, suffix) != 0)
114 + syslog(LOG_WARNING,
115 + "Ignoring DHCP lease for %s because it has an illegal domain part", hostname);
117 + cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
121 - /* missing info? */
124 - if (ttd == (time_t)(-1))
127 - /* infinite lease to is represented by -1 */
128 - /* This makes is to the lease file as
129 - start time one less than end time. */
130 - /* We use -1 as infinite in ttd */
131 - if ((tts != -1) && (ttd == tts - 1))
132 - ttd = (time_t)(-1);
133 - else if (ttd < now)
136 - dot = strchr(hostname, '.');
140 - { /* suffix and lease has ending: must match */
141 - if (strcmp(dot+1, suffix) != 0)
142 - syslog(LOG_WARNING,
143 - "Ignoring DHCP lease for %s because it has an illegal domain part", hostname);
145 - cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
148 - { /* suffix exists but lease has no ending - add lease and lease.suffix */
149 - cache_add_dhcp_entry(hostname, &host_address, ttd, 0);
150 - strncat(hostname, ".", MAXDNAME);
151 - strncat(hostname, suffix, MAXDNAME);
152 - hostname[MAXDNAME-1] = 0; /* in case strncat hit limit */
153 - /* Make FQDN canonical for reverse lookups */
154 - cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
159 - if (dot) /* no lease ending allowed */
160 - syslog(LOG_WARNING,
161 - "Ignoring DHCP lease for %s because it has a domain part", hostname);
163 - cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
170 + { /* suffix exists but lease has no ending - add lease and lease.suffix */
171 + cache_add_dhcp_entry(hostname, &host_address, ttd, 0);
172 + strncat(hostname, ".", MAXDNAME);
173 + strncat(hostname, suffix, MAXDNAME);
174 + hostname[MAXDNAME-1] = 0; /* in case strncat hit limit */
175 + /* Make FQDN canonical for reverse lookups */
176 + cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
181 + if (dot) /* no lease ending allowed */
182 + syslog(LOG_WARNING,
183 + "Ignoring DHCP lease for %s because it has a domain part", hostname);
185 + cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
192 -static int next_token (char *token, int buffsize, FILE * fp)
197 - while((c = getc(fp)) != EOF)
200 - do { c = getc(fp); } while (c != '\n' && c != EOF);
202 - if (c == ' ' || c == '\t' || c == '\n' || c == ';')
207 - else if ((c != '"') && (count<buffsize-1))
215 - return count ? 1 : 0;