Scale down the wallpaper to 800x600. For some reason, scaling down the image through...
[reactos.git] / reactos / base / services / 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 AdapterStop();
74 return;
75 }
76
77 ApiLock();
78
79 do {
80 /*
81 * Call any expired timeouts, and then if there's still
82 * a timeout registered, time out the select call then.
83 */
84 time(&cur_time);
85
86 if (timeouts) {
87 struct timeout *t;
88
89 if (timeouts->when <= cur_time) {
90 t = timeouts;
91 timeouts = timeouts->next;
92 (*(t->func))(t->what);
93 t->next = free_timeouts;
94 free_timeouts = t;
95 continue;
96 }
97
98 /*
99 * Figure timeout in milliseconds, and check for
100 * potential overflow, so we can cram into an
101 * int for poll, while not polling with a
102 * negative timeout and blocking indefinitely.
103 */
104 howlong = timeouts->when - cur_time;
105 if (howlong > INT_MAX / 1000)
106 howlong = INT_MAX / 1000;
107 to_msec = howlong * 1000;
108 } else
109 to_msec = 5000;
110
111 /* Set up the descriptors to be polled. */
112 FD_ZERO(&fds);
113
114 for (l = protocols; l; l = l->next)
115 FD_SET(l->fd, &fds);
116
117 /* Wait for a packet or a timeout... XXX */
118 timeval.tv_sec = to_msec / 1000;
119 timeval.tv_usec = to_msec % 1000;
120
121 ApiUnlock();
122
123 if (protocols)
124 count = select(0, &fds, NULL, NULL, &timeval);
125 else {
126 Sleep(to_msec);
127 count = 0;
128 }
129
130 ApiLock();
131
132 DH_DbgPrint(MID_TRACE,("Select: %d\n", count));
133
134 /* Not likely to be transitory... */
135 if (count == SOCKET_ERROR) {
136 err = WSAGetLastError();
137 error("poll: %d", err);
138 break;
139 }
140
141 for (l = protocols; l; l = l->next) {
142 struct interface_info *ip;
143 ip = l->local;
144 if (FD_ISSET(l->fd, &fds)) {
145 if (ip && (l->handler != got_one ||
146 !ip->dead)) {
147 DH_DbgPrint(MID_TRACE,("Handling %x\n", l));
148 (*(l->handler))(l);
149 }
150 }
151 }
152 } while (1);
153
154 ApiUnlock(); /* Not reached currently */
155 }
156
157 void
158 got_one(struct protocol *l)
159 {
160 struct sockaddr_in from;
161 struct hardware hfrom;
162 struct iaddr ifrom;
163 ssize_t result;
164 union {
165 /*
166 * Packet input buffer. Must be as large as largest
167 * possible MTU.
168 */
169 unsigned char packbuf[4095];
170 struct dhcp_packet packet;
171 } u;
172 struct interface_info *ip = l->local;
173 PDHCP_ADAPTER adapter;
174
175 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
176 &hfrom)) == -1) {
177 warning("receive_packet failed on %s: %d", ip->name,
178 WSAGetLastError());
179 ip->errors++;
180 if (ip->errors > 20) {
181 /* our interface has gone away. */
182 warning("Interface %s no longer appears valid.",
183 ip->name);
184 ip->dead = 1;
185 close(l->fd);
186 remove_protocol(l);
187 adapter = AdapterFindInfo(ip);
188 if (adapter) {
189 RemoveEntryList(&adapter->ListEntry);
190 free(adapter);
191 }
192 }
193 return;
194 }
195 if (result == 0)
196 return;
197
198 if (bootp_packet_handler) {
199 ifrom.len = 4;
200 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
201
202
203 adapter = AdapterFindByHardwareAddress(u.packet.chaddr,
204 u.packet.hlen);
205
206 if (!adapter) {
207 warning("Discarding packet with a non-matching target physical address\n");
208 return;
209 }
210
211 (*bootp_packet_handler)(&adapter->DhclientInfo, &u.packet, result,
212 from.sin_port, ifrom, &hfrom);
213 }
214 }
215
216 void
217 add_timeout(time_t when, void (*where)(void *), void *what)
218 {
219 struct timeout *t, *q;
220
221 DH_DbgPrint(MID_TRACE,("Adding timeout %x %p %x\n", when, where, what));
222 /* See if this timeout supersedes an existing timeout. */
223 t = NULL;
224 for (q = timeouts; q; q = q->next) {
225 if (q->func == where && q->what == what) {
226 if (t)
227 t->next = q->next;
228 else
229 timeouts = q->next;
230 break;
231 }
232 t = q;
233 }
234
235 /* If we didn't supersede a timeout, allocate a timeout
236 structure now. */
237 if (!q) {
238 if (free_timeouts) {
239 q = free_timeouts;
240 free_timeouts = q->next;
241 q->func = where;
242 q->what = what;
243 } else {
244 q = malloc(sizeof(struct timeout));
245 if (!q) {
246 error("Can't allocate timeout structure!");
247 return;
248 }
249 q->func = where;
250 q->what = what;
251 }
252 }
253
254 q->when = when;
255
256 /* Now sort this timeout into the timeout list. */
257
258 /* Beginning of list? */
259 if (!timeouts || timeouts->when > q->when) {
260 q->next = timeouts;
261 timeouts = q;
262 return;
263 }
264
265 /* Middle of list? */
266 for (t = timeouts; t->next; t = t->next) {
267 if (t->next->when > q->when) {
268 q->next = t->next;
269 t->next = q;
270 return;
271 }
272 }
273
274 /* End of list. */
275 t->next = q;
276 q->next = NULL;
277 }
278
279 void
280 cancel_timeout(void (*where)(void *), void *what)
281 {
282 struct timeout *t, *q;
283
284 /* Look for this timeout on the list, and unlink it if we find it. */
285 t = NULL;
286 for (q = timeouts; q; q = q->next) {
287 if (q->func == where && q->what == what) {
288 if (t)
289 t->next = q->next;
290 else
291 timeouts = q->next;
292 break;
293 }
294 t = q;
295 }
296
297 /* If we found the timeout, put it on the free list. */
298 if (q) {
299 q->next = free_timeouts;
300 free_timeouts = q;
301 }
302 }
303
304 /* Add a protocol to the list of protocols... */
305 void
306 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
307 void *local)
308 {
309 struct protocol *p;
310
311 p = malloc(sizeof(*p));
312 if (!p)
313 error("can't allocate protocol struct for %s", name);
314
315 p->fd = fd;
316 p->handler = handler;
317 p->local = local;
318 p->next = protocols;
319 protocols = p;
320 }
321
322 void
323 remove_protocol(struct protocol *proto)
324 {
325 struct protocol *p, *next, *prev;
326
327 prev = NULL;
328 for (p = protocols; p; p = next) {
329 next = p->next;
330 if (p == proto) {
331 if (prev)
332 prev->next = p->next;
333 else
334 protocols = p->next;
335 free(p);
336 }
337 }
338 }
339
340 struct protocol *
341 find_protocol_by_adapter(struct interface_info *info)
342 {
343 struct protocol *p;
344
345 for( p = protocols; p; p = p->next ) {
346 if( p->local == (void *)info ) return p;
347 }
348
349 return NULL;
350 }
351
352 int
353 interface_link_status(char *ifname)
354 {
355 return (1);
356 }