1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
diff -x CVS -urN dnsmasq-1.18/dhcp.c dnsmasq.old/dhcp.c
--- dnsmasq-1.18/dhcp.c 2003-11-05 08:30:20.000000000 -0600
+++ dnsmasq.old/dhcp.c 2004-01-05 23:40:11.000000000 -0600
@@ -15,14 +15,20 @@
#include "dnsmasq.h"
-static int next_token (char *token, int buffsize, FILE * fp);
+struct dhcpOfferedAddr {
+ u_int8_t hostname[16];
+ u_int8_t chaddr[16];
+ u_int32_t yiaddr; /* network order */
+ u_int32_t expires; /* host order */
+};
void load_dhcp(char *file, char *suffix, time_t now, char *hostname)
{
- char token[MAXTOK], *dot;
+ char *dot;
struct all_addr host_address;
- time_t ttd, tts;
+ time_t ttd;
FILE *fp = fopen (file, "r");
+ struct dhcpOfferedAddr lease;
if (!fp)
{
@@ -34,154 +40,45 @@
/* remove all existing DHCP cache entries */
cache_unhash_dhcp();
-
- while ((next_token(token, MAXTOK, fp)))
- {
- if (strcmp(token, "lease") == 0)
- {
- hostname[0] = '\0';
- ttd = tts = (time_t)(-1);
-#ifdef HAVE_IPV6
- if (next_token(token, MAXTOK, fp) &&
- inet_pton(AF_INET, token, &host_address))
-#else
- if (next_token(token, MAXTOK, fp) &&
- (host_address.addr4.s_addr = inet_addr(token)) != (in_addr_t) -1)
-#endif
- {
- if (next_token(token, MAXTOK, fp) && *token == '{')
- {
- while (next_token(token, MAXTOK, fp) && *token != '}')
+
+ while (fread(&lease, sizeof(lease), 1, fp)) {
+ host_address.addr.addr4.s_addr = lease.yiaddr;
+
+ strcpy(hostname,lease.hostname);
+ if (lease.expires>(unsigned)now)
+ ttd = lease.expires;
+ else
+ ttd = -1;
+ dot = strchr(hostname, '.');
+ if (suffix)
{
- if ((strcmp(token, "client-hostname") == 0) ||
- (strcmp(token, "hostname") == 0))
- {
- if (next_token(hostname, MAXDNAME, fp))
- if (!canonicalise(hostname))
- {
- *hostname = 0;
- syslog(LOG_ERR, "bad name in %s", file);
- }
- }
- else if ((strcmp(token, "ends") == 0) ||
- (strcmp(token, "starts") == 0))
- {
- struct tm lease_time;
- int is_ends = (strcmp(token, "ends") == 0);
- if (next_token(token, MAXTOK, fp) && /* skip weekday */
- next_token(token, MAXTOK, fp) && /* Get date from lease file */
- sscanf (token, "%d/%d/%d",
- &lease_time.tm_year,
- &lease_time.tm_mon,
- &lease_time.tm_mday) == 3 &&
- next_token(token, MAXTOK, fp) &&
- sscanf (token, "%d:%d:%d:",
- &lease_time.tm_hour,
- &lease_time.tm_min,
- &lease_time.tm_sec) == 3)
- {
- /* There doesn't seem to be a universally available library function
- which converts broken-down _GMT_ time to seconds-in-epoch.
- The following was borrowed from ISC dhcpd sources, where
- it is noted that it might not be entirely accurate for odd seconds.
- Since we're trying to get the same answer as dhcpd, that's just
- fine here. */
- static int months [11] = { 31, 59, 90, 120, 151, 181,
- 212, 243, 273, 304, 334 };
- time_t time = ((((((365 * (lease_time.tm_year - 1970) + /* Days in years since '70 */
- (lease_time.tm_year - 1969) / 4 + /* Leap days since '70 */
- (lease_time.tm_mon > 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; }
+ if (dot)
+ { /* suffix and lease has ending: must match */
+ if (strcmp(dot+1, suffix) != 0)
+ syslog(LOG_WARNING,
+ "Ignoring DHCP lease for %s because it has an illegal domain part", hostname);
+ else
+ cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
}
- }
-
- /* missing info? */
- if (!*hostname)
- continue;
- if (ttd == (time_t)(-1))
- continue;
-
- /* infinite lease to is represented by -1 */
- /* This makes is to the lease file as
- start time one less than end time. */
- /* We use -1 as infinite in ttd */
- if ((tts != -1) && (ttd == tts - 1))
- ttd = (time_t)(-1);
- else if (ttd < now)
- continue;
-
- dot = strchr(hostname, '.');
- if (suffix)
- {
- if (dot)
- { /* suffix and lease has ending: must match */
- if (strcmp(dot+1, suffix) != 0)
- syslog(LOG_WARNING,
- "Ignoring DHCP lease for %s because it has an illegal domain part", hostname);
- else
- cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
- }
- else
- { /* suffix exists but lease has no ending - add lease and lease.suffix */
- cache_add_dhcp_entry(hostname, &host_address, ttd, 0);
- strncat(hostname, ".", MAXDNAME);
- strncat(hostname, suffix, MAXDNAME);
- hostname[MAXDNAME-1] = 0; /* in case strncat hit limit */
- /* Make FQDN canonical for reverse lookups */
- cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
- }
- }
- else
- { /* no suffix */
- if (dot) /* no lease ending allowed */
- syslog(LOG_WARNING,
- "Ignoring DHCP lease for %s because it has a domain part", hostname);
- else
- cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
- }
- }
- }
- }
- }
+ else
+ { /* suffix exists but lease has no ending - add lease and lease.suffix */
+ cache_add_dhcp_entry(hostname, &host_address, ttd, 0);
+ strncat(hostname, ".", MAXDNAME);
+ strncat(hostname, suffix, MAXDNAME);
+ hostname[MAXDNAME-1] = 0; /* in case strncat hit limit */
+ /* Make FQDN canonical for reverse lookups */
+ cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
+ }
+ }
+ else
+ { /* no suffix */
+ if (dot) /* no lease ending allowed */
+ syslog(LOG_WARNING,
+ "Ignoring DHCP lease for %s because it has a domain part", hostname);
+ else
+ cache_add_dhcp_entry(hostname, &host_address, ttd, F_REVERSE);
+ }
+ }
fclose(fp);
}
-
-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<buffsize-1))
- {
- *cp++ = c;
- count++;
- }
- }
-
- *cp = 0;
- return count ? 1 : 0;
-}
-
-
-
|