8749a1212704e18bf0251a7fb3df43a87b94b694
1 /* $OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david Exp $ */
4 * Copyright 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6 * The Internet Software Consortium. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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''.
44 //#include <sys/ioctl.h>
46 //#include <net/if_media.h>
47 //#include <ifaddrs.h>
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
*);
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.
66 int count
, to_msec
, err
;
69 time_t howlong
, cur_time
;
70 struct timeval timeval
;
72 if (!StartAdapterDiscovery())
79 * Call any expired timeouts, and then if there's still
80 * a timeout registered, time out the select call then.
87 if (timeouts
->when
<= cur_time
) {
89 timeouts
= timeouts
->next
;
90 (*(t
->func
))(t
->what
);
91 t
->next
= free_timeouts
;
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.
102 howlong
= timeouts
->when
- cur_time
;
103 if (howlong
> INT_MAX
/ 1000)
104 howlong
= INT_MAX
/ 1000;
105 to_msec
= howlong
* 1000;
109 /* Set up the descriptors to be polled. */
112 for (l
= protocols
; l
; l
= l
->next
)
115 /* Wait for a packet or a timeout... XXX */
116 timeval
.tv_sec
= to_msec
/ 1000;
117 timeval
.tv_usec
= to_msec
% 1000;
122 count
= select(0, &fds
, NULL
, NULL
, &timeval
);
130 DH_DbgPrint(MID_TRACE
,("Select: %d\n", count
));
132 /* Not likely to be transitory... */
133 if (count
== SOCKET_ERROR
) {
134 err
= WSAGetLastError();
135 error("poll: %d", err
);
139 for (l
= protocols
; l
; l
= l
->next
) {
140 struct interface_info
*ip
;
142 if (FD_ISSET(l
->fd
, &fds
)) {
143 if (ip
&& (l
->handler
!= got_one
||
145 DH_DbgPrint(MID_TRACE
,("Handling %x\n", l
));
156 got_one(struct protocol
*l
)
158 struct sockaddr_in from
;
159 struct hardware hfrom
;
164 * Packet input buffer. Must be as large as largest
167 unsigned char packbuf
[4095];
168 struct dhcp_packet packet
;
170 struct interface_info
*ip
= l
->local
;
171 PDHCP_ADAPTER adapter
;
173 if ((result
= receive_packet(ip
, u
.packbuf
, sizeof(u
), &from
,
175 warning("receive_packet failed on %s: %d", ip
->name
,
178 if (ip
->errors
> 20) {
179 /* our interface has gone away. */
180 warning("Interface %s no longer appears valid.",
185 adapter
= AdapterFindInfo(ip
);
187 RemoveEntryList(&adapter
->ListEntry
);
196 if (bootp_packet_handler
) {
198 memcpy(ifrom
.iabuf
, &from
.sin_addr
, ifrom
.len
);
201 adapter
= AdapterFindByHardwareAddress(u
.packet
.chaddr
,
205 warning("Discarding packet with a non-matching target physical address\n");
209 (*bootp_packet_handler
)(&adapter
->DhclientInfo
, &u
.packet
, result
,
210 from
.sin_port
, ifrom
, &hfrom
);
215 add_timeout(time_t when
, void (*where
)(void *), void *what
)
217 struct timeout
*t
, *q
;
219 DH_DbgPrint(MID_TRACE
,("Adding timeout %x %p %x\n", when
, where
, what
));
220 /* See if this timeout supersedes an existing timeout. */
222 for (q
= timeouts
; q
; q
= q
->next
) {
223 if (q
->func
== where
&& q
->what
== what
) {
233 /* If we didn't supersede a timeout, allocate a timeout
238 free_timeouts
= q
->next
;
242 q
= malloc(sizeof(struct timeout
));
244 error("Can't allocate timeout structure!");
254 /* Now sort this timeout into the timeout list. */
256 /* Beginning of list? */
257 if (!timeouts
|| timeouts
->when
> q
->when
) {
263 /* Middle of list? */
264 for (t
= timeouts
; t
->next
; t
= t
->next
) {
265 if (t
->next
->when
> q
->when
) {
278 cancel_timeout(void (*where
)(void *), void *what
)
280 struct timeout
*t
, *q
;
282 /* Look for this timeout on the list, and unlink it if we find it. */
284 for (q
= timeouts
; q
; q
= q
->next
) {
285 if (q
->func
== where
&& q
->what
== what
) {
295 /* If we found the timeout, put it on the free list. */
297 q
->next
= free_timeouts
;
302 /* Add a protocol to the list of protocols... */
304 add_protocol(char *name
, int fd
, void (*handler
)(struct protocol
*),
309 p
= malloc(sizeof(*p
));
311 error("can't allocate protocol struct for %s", name
);
314 p
->handler
= handler
;
321 remove_protocol(struct protocol
*proto
)
323 struct protocol
*p
, *next
, *prev
;
326 for (p
= protocols
; p
; p
= next
) {
330 prev
->next
= p
->next
;
339 find_protocol_by_adapter(struct interface_info
*info
)
343 for( p
= protocols
; p
; p
= p
->next
) {
344 if( p
->local
== (void *)info
) return p
;
351 interface_link_status(char *ifname
)