[DHCP/DHCPCSVC]
[reactos.git] / reactos / dll / win32 / dhcpcsvc / dhcp / dispatch.c
1 /* $OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david Exp $ */
2
3 /*
4 * Copyright 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6 * The Internet Software Consortium. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of The Internet Software Consortium nor the names
18 * of its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * This software has been written for the Internet Software Consortium
36 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37 * Enterprises. To learn more about the Internet Software Consortium,
38 * see ``http://www.vix.com/isc''. To learn more about Vixie
39 * Enterprises, see ``http://www.vix.com''.
40 */
41
42 #include "rosdhcp.h"
43 #include "dhcpd.h"
44 //#include <sys/ioctl.h>
45
46 //#include <net/if_media.h>
47 //#include <ifaddrs.h>
48 //#include <poll.h>
49
50 struct protocol *protocols = NULL;
51 struct timeout *timeouts = NULL;
52 static struct timeout *free_timeouts = NULL;
53 void (*bootp_packet_handler)(struct interface_info *,
54 struct dhcp_packet *, int, unsigned int,
55 struct iaddr, struct hardware *);
56
57 /*
58 * Wait for packets to come in using poll(). When a packet comes in,
59 * call receive_packet to receive the packet and possibly strip hardware
60 * addressing information from it, and then call through the
61 * bootp_packet_handler hook to try to do something with it.
62 */
63 void
64 dispatch(void)
65 {
66 int count, to_msec, err;
67 struct protocol *l;
68 fd_set fds;
69 time_t howlong, cur_time;
70 struct timeval timeval;
71
72 if (!AdapterDiscover())
73 return;
74
75 ApiLock();
76
77 do {
78 /*
79 * Call any expired timeouts, and then if there's still
80 * a timeout registered, time out the select call then.
81 */
82 time(&cur_time);
83
84 if (timeouts) {
85 struct timeout *t;
86
87 if (timeouts->when <= cur_time) {
88 t = timeouts;
89 timeouts = timeouts->next;
90 (*(t->func))(t->what);
91 t->next = free_timeouts;
92 free_timeouts = t;
93 continue;
94 }
95
96 /*
97 * Figure timeout in milliseconds, and check for
98 * potential overflow, so we can cram into an
99 * int for poll, while not polling with a
100 * negative timeout and blocking indefinitely.
101 */
102 howlong = timeouts->when - cur_time;
103 if (howlong > INT_MAX / 1000)
104 howlong = INT_MAX / 1000;
105 to_msec = howlong * 1000;
106 } else
107 to_msec = 5000;
108
109 /* Set up the descriptors to be polled. */
110 FD_ZERO(&fds);
111
112 for (l = protocols; l; l = l->next)
113 FD_SET(l->fd, &fds);
114
115 /* Wait for a packet or a timeout... XXX */
116 timeval.tv_sec = to_msec / 1000;
117 timeval.tv_usec = to_msec % 1000;
118
119 ApiUnlock();
120
121 if (protocols)
122 count = select(0, &fds, NULL, NULL, &timeval);
123 else {
124 Sleep(to_msec);
125 count = 0;
126 }
127
128 ApiLock();
129
130 DH_DbgPrint(MID_TRACE,("Select: %d\n", count));
131
132 /* Not likely to be transitory... */
133 if (count == SOCKET_ERROR) {
134 err = WSAGetLastError();
135 error("poll: %d", err);
136 break;
137 }
138
139 for (l = protocols; l; l = l->next) {
140 struct interface_info *ip;
141 ip = l->local;
142 if (FD_ISSET(l->fd, &fds)) {
143 if (ip && (l->handler != got_one ||
144 !ip->dead)) {
145 DH_DbgPrint(MID_TRACE,("Handling %x\n", l));
146 (*(l->handler))(l);
147 }
148 }
149 }
150 } while (1);
151
152 ApiUnlock();
153 }
154
155 void
156 got_one(struct protocol *l)
157 {
158 struct sockaddr_in from;
159 struct hardware hfrom;
160 struct iaddr ifrom;
161 ssize_t result;
162 union {
163 /*
164 * Packet input buffer. Must be as large as largest
165 * possible MTU.
166 */
167 unsigned char packbuf[4095];
168 struct dhcp_packet packet;
169 } u;
170 struct interface_info *ip = l->local;
171 PDHCP_ADAPTER adapter;
172
173 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
174 &hfrom)) == -1) {
175 warning("receive_packet failed on %s: %d", ip->name,
176 WSAGetLastError());
177 ip->errors++;
178 if (ip->errors > 20) {
179 /* our interface has gone away. */
180 warning("Interface %s no longer appears valid.",
181 ip->name);
182 ip->dead = 1;
183 closesocket(l->fd);
184 remove_protocol(l);
185 adapter = AdapterFindInfo(ip);
186 if (adapter) {
187 RemoveEntryList(&adapter->ListEntry);
188 free(adapter);
189 }
190 }
191 return;
192 }
193 if (result == 0)
194 return;
195
196 if (bootp_packet_handler) {
197 ifrom.len = 4;
198 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
199
200
201 adapter = AdapterFindByHardwareAddress(u.packet.chaddr,
202 u.packet.hlen);
203
204 if (!adapter) {
205 warning("Discarding packet with a non-matching target physical address\n");
206 return;
207 }
208
209 (*bootp_packet_handler)(&adapter->DhclientInfo, &u.packet, result,
210 from.sin_port, ifrom, &hfrom);
211 }
212 }
213
214 void
215 add_timeout(time_t when, void (*where)(void *), void *what)
216 {
217 struct timeout *t, *q;
218
219 DH_DbgPrint(MID_TRACE,("Adding timeout %x %p %x\n", when, where, what));
220 /* See if this timeout supersedes an existing timeout. */
221 t = NULL;
222 for (q = timeouts; q; q = q->next) {
223 if (q->func == where && q->what == what) {
224 if (t)
225 t->next = q->next;
226 else
227 timeouts = q->next;
228 break;
229 }
230 t = q;
231 }
232
233 /* If we didn't supersede a timeout, allocate a timeout
234 structure now. */
235 if (!q) {
236 if (free_timeouts) {
237 q = free_timeouts;
238 free_timeouts = q->next;
239 q->func = where;
240 q->what = what;
241 } else {
242 q = malloc(sizeof(struct timeout));
243 if (!q) {
244 error("Can't allocate timeout structure!");
245 return;
246 }
247 q->func = where;
248 q->what = what;
249 }
250 }
251
252 q->when = when;
253
254 /* Now sort this timeout into the timeout list. */
255
256 /* Beginning of list? */
257 if (!timeouts || timeouts->when > q->when) {
258 q->next = timeouts;
259 timeouts = q;
260 return;
261 }
262
263 /* Middle of list? */
264 for (t = timeouts; t->next; t = t->next) {
265 if (t->next->when > q->when) {
266 q->next = t->next;
267 t->next = q;
268 return;
269 }
270 }
271
272 /* End of list. */
273 t->next = q;
274 q->next = NULL;
275 }
276
277 void
278 cancel_timeout(void (*where)(void *), void *what)
279 {
280 struct timeout *t, *q;
281
282 /* Look for this timeout on the list, and unlink it if we find it. */
283 t = NULL;
284 for (q = timeouts; q; q = q->next) {
285 if (q->func == where && q->what == what) {
286 if (t)
287 t->next = q->next;
288 else
289 timeouts = q->next;
290 break;
291 }
292 t = q;
293 }
294
295 /* If we found the timeout, put it on the free list. */
296 if (q) {
297 q->next = free_timeouts;
298 free_timeouts = q;
299 }
300 }
301
302 /* Add a protocol to the list of protocols... */
303 void
304 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
305 void *local)
306 {
307 struct protocol *p;
308
309 p = malloc(sizeof(*p));
310 if (!p)
311 error("can't allocate protocol struct for %s", name);
312
313 p->fd = fd;
314 p->handler = handler;
315 p->local = local;
316 p->next = protocols;
317 protocols = p;
318 }
319
320 void
321 remove_protocol(struct protocol *proto)
322 {
323 struct protocol *p, *next, *prev;
324
325 prev = NULL;
326 for (p = protocols; p; p = next) {
327 next = p->next;
328 if (p == proto) {
329 if (prev)
330 prev->next = p->next;
331 else
332 protocols = p->next;
333 free(p);
334 }
335 }
336 }
337
338 struct protocol *
339 find_protocol_by_adapter(struct interface_info *info)
340 {
341 struct protocol *p;
342
343 for( p = protocols; p; p = p->next ) {
344 if( p->local == (void *)info ) return p;
345 }
346
347 return NULL;
348 }
349
350 int
351 interface_link_status(char *ifname)
352 {
353 return (1);
354 }