xref: /openbsd-src/usr.sbin/mopd/common/loop-bsd.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: loop-bsd.c,v 1.7 2003/06/02 21:38:39 maja 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef LINT
28 static char rcsid[] = "$OpenBSD: loop-bsd.c,v 1.7 2003/06/02 21:38:39 maja Exp $";
29 #endif
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #if defined(__bsdi__) || defined(__FreeBSD__)
35 #include <sys/time.h>
36 #endif
37 #include <net/bpf.h>
38 #include <sys/ioctl.h>
39 #include <sys/errno.h>
40 
41 #include "os.h"
42 #include "common/common.h"
43 #include "common/mopdef.h"
44 
45 int
46 mopOpenRC(p, trans)
47 	struct if_info *p;
48 	int	trans;
49 {
50 #ifndef NORC
51 	return (*(p->iopen))(p->if_name,
52 			     O_RDWR,
53 			     MOP_K_PROTO_RC,
54 			     trans);
55 #else
56 	return -1;
57 #endif
58 }
59 
60 int
61 mopOpenDL(p, trans)
62 	struct if_info *p;
63 	int	trans;
64 {
65 #ifndef NODL
66 	return (*(p->iopen))(p->if_name,
67 			     O_RDWR,
68 			     MOP_K_PROTO_DL,
69 			     trans);
70 #else
71 	return -1;
72 #endif
73 }
74 
75 void
76 mopReadRC()
77 {
78 }
79 
80 void
81 mopReadDL()
82 {
83 }
84 
85 /*
86  * The list of all interfaces that are being listened to.  loop()
87  * "selects" on the descriptors in this list.
88  */
89 struct if_info *iflist;
90 
91 void   mopProcess(struct if_info *, u_char *);
92 
93 /*
94  * Loop indefinitely listening for MOP requests on the
95  * interfaces in 'iflist'.
96  */
97 void
98 Loop()
99 {
100 	u_char *buf, *bp, *ep;
101 	int     cc;
102 	fd_set  fds, listeners;
103 	int     bufsize, maxfd = 0;
104 	struct if_info *ii;
105 
106 	if (iflist == 0) {
107 		syslog(LOG_ERR, "no interfaces");
108 		exit(0);
109 	}
110 	if (iflist->fd != -1) {
111 		if (ioctl(iflist->fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
112 			syslog(LOG_ERR, "BIOCGBLEN: %m");
113 			exit(0);
114 	        }
115 	}
116 	buf = (u_char *) malloc((unsigned) bufsize);
117 	if (buf == 0) {
118 		syslog(LOG_ERR, "malloc: %m");
119 		exit(0);
120 	}
121 	/*
122          * Find the highest numbered file descriptor for select().
123          * Initialize the set of descriptors to listen to.
124          */
125 	FD_ZERO(&fds);
126 	for (ii = iflist; ii; ii = ii->next) {
127 		if (ii->fd != -1) {
128 			FD_SET(ii->fd, &fds);
129 			if (ii->fd > maxfd)
130 				maxfd = ii->fd;
131 	        }
132 	}
133 	while (1) {
134 		listeners = fds;
135 		if (select(maxfd + 1, &listeners, (fd_set *) 0,
136 			(fd_set *) 0, (struct timeval *) 0) < 0) {
137 			syslog(LOG_ERR, "select: %m");
138 			exit(0);
139 		}
140 		for (ii = iflist; ii; ii = ii->next) {
141 			if (ii->fd != -1) {
142 				if (!FD_ISSET(ii->fd, &listeners))
143 					continue;
144 			}
145 	again:
146 			cc = read(ii->fd, (char *) buf, bufsize);
147 			/* Don't choke when we get ptraced */
148 			if (cc < 0 && errno == EINTR)
149 				goto again;
150 			/* Due to a SunOS bug, after 2^31 bytes, the file
151 			 * offset overflows and read fails with EINVAL.  The
152 			 * lseek() to 0 will fix things. */
153 			if (cc < 0) {
154 				if (errno == EINVAL &&
155 				    (lseek(ii->fd, 0, SEEK_CUR) + bufsize) < 0) {
156 					(void) lseek(ii->fd, 0, 0);
157 					goto again;
158 				}
159 				syslog(LOG_ERR, "read: %m");
160 				exit(0);
161 			}
162 			/* Loop through the packet(s) */
163 #define bhp ((struct bpf_hdr *)bp)
164 			bp = buf;
165 			ep = bp + cc;
166 			while (bp < ep) {
167 				int caplen, hdrlen;
168 
169 				caplen = bhp->bh_caplen;
170 				hdrlen = bhp->bh_hdrlen;
171 				mopProcess(ii, bp + hdrlen);
172 				bp += BPF_WORDALIGN(hdrlen + caplen);
173 			}
174 		}
175 	}
176 }
177