1 /* $NetBSD: loop-bsd.c,v 1.4 1998/02/03 04:51:29 perry Exp $ */
4 * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Mats O Jansson.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: loop-bsd.c,v 1.4 1998/02/03 04:51:29 perry Exp $");
41 #if defined(__bsdi__) || defined(__FreeBSD__) || defined(__NetBSD__)
45 #include <sys/ioctl.h>
58 return (*(p
->iopen
))(p
->if_name
,
73 return (*(p
->iopen
))(p
->if_name
,
93 * The list of all interfaces that are being listened to. loop()
94 * "selects" on the descriptors in this list.
96 struct if_info
*iflist
;
98 void mopProcess
__P((struct if_info
*, u_char
*));
101 * Loop indefinitely listening for MOP requests on the
102 * interfaces in 'iflist'.
107 u_char
*buf
, *bp
, *ep
;
109 fd_set fds
, listeners
;
110 int bufsize
, maxfd
= 0;
114 syslog(LOG_ERR
, "no interfaces");
117 if (iflist
->fd
!= -1) {
118 if (ioctl(iflist
->fd
, BIOCGBLEN
, (caddr_t
) & bufsize
) < 0) {
119 syslog(LOG_ERR
, "BIOCGBLEN: %m");
123 buf
= (u_char
*) malloc((unsigned) bufsize
);
125 syslog(LOG_ERR
, "malloc: %m");
129 * Find the highest numbered file descriptor for select().
130 * Initialize the set of descriptors to listen to.
133 for (ii
= iflist
; ii
; ii
= ii
->next
) {
135 FD_SET(ii
->fd
, &fds
);
142 if (select(maxfd
+ 1, &listeners
, (struct fd_set
*) 0,
143 (struct fd_set
*) 0, (struct timeval
*) 0) < 0) {
144 syslog(LOG_ERR
, "select: %m");
147 for (ii
= iflist
; ii
; ii
= ii
->next
) {
149 if (!FD_ISSET(ii
->fd
, &listeners
))
153 cc
= read(ii
->fd
, (char *) buf
, bufsize
);
154 /* Don't choke when we get ptraced */
155 if (cc
< 0 && errno
== EINTR
)
157 /* Due to a SunOS bug, after 2^31 bytes, the file
158 * offset overflows and read fails with EINVAL. The
159 * lseek() to 0 will fix things. */
161 if (errno
== EINVAL
&&
162 (lseek(ii
->fd
, 0, SEEK_CUR
) + bufsize
) < 0) {
163 (void) lseek(ii
->fd
, 0, 0);
166 syslog(LOG_ERR
, "read: %m");
169 /* Loop through the packet(s) */
170 #define bhp ((struct bpf_hdr *)bp)
176 caplen
= bhp
->bh_caplen
;
177 hdrlen
= bhp
->bh_hdrlen
;
178 mopProcess(ii
, bp
+ hdrlen
);
179 bp
+= BPF_WORDALIGN(hdrlen
+ caplen
);