xref: /netbsd-src/usr.sbin/mopd/common/loop-bsd.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: loop-bsd.c,v 1.7 2003/04/20 00:17:22 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
19  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: loop-bsd.c,v 1.7 2003/04/20 00:17:22 christos Exp $");
35 #endif
36 
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #if defined(__bsdi__) || defined(__FreeBSD__) || defined(__NetBSD__)
42 #include <sys/time.h>
43 #endif
44 #include <net/bpf.h>
45 #include <sys/ioctl.h>
46 #include <sys/poll.h>
47 #include <assert.h>
48 
49 #include "os.h"
50 #include "common.h"
51 #include "device.h"
52 #include "mopdef.h"
53 #include "log.h"
54 
55 int
56 mopOpenRC(p, trans)
57 	struct if_info *p;
58 	int	trans;
59 {
60 #ifndef NORC
61 	return (*(p->iopen))(p->if_name,
62 			     O_RDWR,
63 			     MOP_K_PROTO_RC,
64 			     trans);
65 #else
66 	return -1;
67 #endif
68 }
69 
70 int
71 mopOpenDL(p, trans)
72 	struct if_info *p;
73 	int	trans;
74 {
75 #ifndef NODL
76 	return (*(p->iopen))(p->if_name,
77 			     O_RDWR,
78 			     MOP_K_PROTO_DL,
79 			     trans);
80 #else
81 	return -1;
82 #endif
83 }
84 
85 void
86 mopReadRC()
87 {
88 }
89 
90 void
91 mopReadDL()
92 {
93 }
94 
95 /*
96  * The list of all interfaces that are being listened to.  loop()
97  * "selects" on the descriptors in this list.
98  */
99 struct if_info *iflist;
100 
101 void   mopProcess    __P((struct if_info *, u_char *));
102 
103 /*
104  * Loop indefinitely listening for MOP requests on the
105  * interfaces in 'iflist'.
106  */
107 void
108 Loop()
109 {
110 	u_char *buf, *bp, *ep;
111 	int     cc, n, m;
112 	struct	pollfd *set;
113 	int     bufsize;
114 	struct	if_info *ii;
115 
116 	if (iflist == 0)
117 		mopLogErrX("no interfaces");
118 	if (iflist->fd != -1) {
119 		if (ioctl(iflist->fd, BIOCGBLEN, (caddr_t) & bufsize) < 0)
120 			mopLogErr("BIOCGBLEN");
121 	}
122 	buf = (u_char *) malloc((unsigned) bufsize);
123 	if (buf == 0)
124 		mopLogErr("malloc");
125 	/*
126          * Find the highest numbered file descriptor for select().
127          * Initialize the set of descriptors to listen to.
128          */
129 	for (ii = iflist, n = 0; ii; ii = ii->next, n++)
130 		;
131 	set = malloc(n * sizeof(*set));
132 	for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
133 		assert(ii->fd != -1);
134 		set[m].fd = ii->fd;
135 		set[m].events = POLLIN;
136 	}
137 	for (;;) {
138 		if (poll(set, n, INFTIM) < 0)
139 			mopLogErr("poll");
140 		for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
141 			if (!(set[m].revents & POLLIN))
142 				continue;
143 	again:
144 			cc = read(ii->fd, (char *) buf, bufsize);
145 			/* Don't choke when we get ptraced */
146 			if (cc < 0 && errno == EINTR)
147 				goto again;
148 			/* Due to a SunOS bug, after 2^31 bytes, the file
149 			 * offset overflows and read fails with EINVAL.  The
150 			 * lseek() to 0 will fix things. */
151 			if (cc < 0) {
152 				if (errno == EINVAL &&
153 				    (lseek(ii->fd, 0, SEEK_CUR) + bufsize) < 0) {
154 					(void) lseek(ii->fd, 0, 0);
155 					goto again;
156 				}
157 				mopLogErr("read");
158 			}
159 			/* Loop through the packet(s) */
160 #define bhp ((struct bpf_hdr *)bp)
161 			bp = buf;
162 			ep = bp + cc;
163 			while (bp < ep) {
164 				int caplen, hdrlen;
165 
166 				caplen = bhp->bh_caplen;
167 				hdrlen = bhp->bh_hdrlen;
168 				mopProcess(ii, bp + hdrlen);
169 				bp += BPF_WORDALIGN(hdrlen + caplen);
170 			}
171 		}
172 	}
173 }
174