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 (!AdapterDiscover()) {
81 * Call any expired timeouts, and then if there's still
82 * a timeout registered, time out the select call then.
89 if (timeouts
->when
<= cur_time
) {
91 timeouts
= timeouts
->next
;
92 (*(t
->func
))(t
->what
);
93 t
->next
= free_timeouts
;
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.
104 howlong
= timeouts
->when
- cur_time
;
105 if (howlong
> INT_MAX
/ 1000)
106 howlong
= INT_MAX
/ 1000;
107 to_msec
= howlong
* 1000;
111 /* Set up the descriptors to be polled. */
114 for (l
= protocols
; l
; l
= l
->next
)
117 /* Wait for a packet or a timeout... XXX */
118 timeval
.tv_sec
= to_msec
/ 1000;
119 timeval
.tv_usec
= to_msec
% 1000;
124 count
= select(0, &fds
, NULL
, NULL
, &timeval
);
132 DH_DbgPrint(MID_TRACE
,("Select: %d\n", count
));
134 /* Not likely to be transitory... */
135 if (count
== SOCKET_ERROR
) {
136 err
= WSAGetLastError();
137 error("poll: %d", err
);
141 for (l
= protocols
; l
; l
= l
->next
) {
142 struct interface_info
*ip
;
144 if (FD_ISSET(l
->fd
, &fds
)) {
145 if (ip
&& (l
->handler
!= got_one
||
147 DH_DbgPrint(MID_TRACE
,("Handling %x\n", l
));
154 ApiUnlock(); /* Not reached currently */
158 got_one(struct protocol
*l
)
160 struct sockaddr_in from
;
161 struct hardware hfrom
;
166 * Packet input buffer. Must be as large as largest
169 unsigned char packbuf
[4095];
170 struct dhcp_packet packet
;
172 struct interface_info
*ip
= l
->local
;
173 PDHCP_ADAPTER adapter
;
175 if ((result
= receive_packet(ip
, u
.packbuf
, sizeof(u
), &from
,
177 warning("receive_packet failed on %s: %d", ip
->name
,
180 if (ip
->errors
> 20) {
181 /* our interface has gone away. */
182 warning("Interface %s no longer appears valid.",
187 adapter
= AdapterFindInfo(ip
);
189 RemoveEntryList(&adapter
->ListEntry
);
198 if (bootp_packet_handler
) {
200 memcpy(ifrom
.iabuf
, &from
.sin_addr
, ifrom
.len
);
203 adapter
= AdapterFindByHardwareAddress(u
.packet
.chaddr
,
207 warning("Discarding packet with a non-matching target physical address\n");
211 (*bootp_packet_handler
)(&adapter
->DhclientInfo
, &u
.packet
, result
,
212 from
.sin_port
, ifrom
, &hfrom
);
217 add_timeout(time_t when
, void (*where
)(void *), void *what
)
219 struct timeout
*t
, *q
;
221 DH_DbgPrint(MID_TRACE
,("Adding timeout %x %p %x\n", when
, where
, what
));
222 /* See if this timeout supersedes an existing timeout. */
224 for (q
= timeouts
; q
; q
= q
->next
) {
225 if (q
->func
== where
&& q
->what
== what
) {
235 /* If we didn't supersede a timeout, allocate a timeout
240 free_timeouts
= q
->next
;
244 q
= malloc(sizeof(struct timeout
));
246 error("Can't allocate timeout structure!");
256 /* Now sort this timeout into the timeout list. */
258 /* Beginning of list? */
259 if (!timeouts
|| timeouts
->when
> q
->when
) {
265 /* Middle of list? */
266 for (t
= timeouts
; t
->next
; t
= t
->next
) {
267 if (t
->next
->when
> q
->when
) {
280 cancel_timeout(void (*where
)(void *), void *what
)
282 struct timeout
*t
, *q
;
284 /* Look for this timeout on the list, and unlink it if we find it. */
286 for (q
= timeouts
; q
; q
= q
->next
) {
287 if (q
->func
== where
&& q
->what
== what
) {
297 /* If we found the timeout, put it on the free list. */
299 q
->next
= free_timeouts
;
304 /* Add a protocol to the list of protocols... */
306 add_protocol(char *name
, int fd
, void (*handler
)(struct protocol
*),
311 p
= malloc(sizeof(*p
));
313 error("can't allocate protocol struct for %s", name
);
316 p
->handler
= handler
;
323 remove_protocol(struct protocol
*proto
)
325 struct protocol
*p
, *next
, *prev
;
328 for (p
= protocols
; p
; p
= next
) {
332 prev
->next
= p
->next
;
341 find_protocol_by_adapter(struct interface_info
*info
)
345 for( p
= protocols
; p
; p
= p
->next
) {
346 if( p
->local
== (void *)info
) return p
;
353 interface_link_status(char *ifname
)