xref: /openbsd-src/usr.sbin/mopd/otherOS/loop-linux2.c (revision 772b9441e1f8d1e24eba31ceabb1be60d8749ffb)
1 /*
2  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. All advertising materials mentioning features or use of this software
13  *    must display the following acknowledgement:
14  *	This product includes software developed by Mats O Jansson.
15  * 4. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef LINT
31 static char rcsid[] = "$Id: loop-linux2.c,v 1.1 1999/03/27 14:31:22 maja Exp $";
32 #endif
33 
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #if defined(__bsdi__) || defined(__FreeBSD__)
38 #include <sys/time.h>
39 #endif
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 
43 #include "os.h"
44 #include "common/common.h"
45 #include "common/mopdef.h"
46 
47 int
48 mopOpenRC(p, trans)
49 	struct if_info *p;
50 	int	trans;
51 {
52 #ifndef NORC
53 	return (*(p->iopen))(p->if_name,
54 			     O_RDWR,
55 			     MOP_K_PROTO_RC,
56 			     trans);
57 #else
58 	return -1;
59 #endif
60 }
61 
62 int
63 mopOpenDL(p, trans)
64 	struct if_info *p;
65 	int	trans;
66 {
67 #ifndef NODL
68 	return (*(p->iopen))(p->if_name,
69 			     O_RDWR,
70 			     MOP_K_PROTO_DL,
71 			     trans);
72 #else
73 	return -1;
74 #endif
75 }
76 
77 void
78 mopReadRC()
79 {
80 }
81 
82 void
83 mopReadDL()
84 {
85 }
86 
87 /*
88  * The list of all interfaces that are being listened to.  loop()
89  * "selects" on the descriptors in this list.
90  */
91 struct if_info *iflist;
92 
93 void   mopProcess    __P((struct if_info *, u_char *));
94 
95 /*
96  * Loop indefinitely listening for MOP requests on the
97  * interfaces in 'iflist'.
98  */
99 void
100 Loop()
101 {
102 	u_char *buf, *bp, *ep;
103 	int     cc;
104 	fd_set  fds, listeners;
105 	int     bufsize = 1100, maxfd =0;
106 	struct if_info *ii;
107 
108 /* FIXME : this is a hack, for some reason specifying an interface would
109  * cause it to fail because bufsize is an impossible number, so I added a
110  * sanity check because I'm too lazy to figure out why. -- Karl
111  */
112 	if (bufsize > 1100)
113 	 	bufsize = 64;
114 
115 	if (iflist == 0) {
116 		syslog(LOG_ERR, "no interfaces");
117 		exit(0);
118 	}
119 
120 	 buf = (u_char *) malloc((unsigned) bufsize);
121 
122 	if (buf == 0) {
123 		syslog(LOG_ERR, "malloc: %m");
124 		exit(0);
125 	}
126 	/*
127          * Find the highest numbered file descriptor for select().
128          * Initialize the set of descriptors to listen to.
129          */
130 	FD_ZERO(&fds);
131 	for (ii = iflist; ii; ii = ii->next) {
132 		if (ii->fd != -1) {
133 			FD_SET(ii->fd, &fds);
134 			if (ii->fd > maxfd)
135 				maxfd = ii->fd;
136 	        }
137 	}
138 	while (1) {
139 		listeners = fds;
140 		if (select(maxfd + 1, &listeners, (fd_set *) 0,
141 			(fd_set *) 0, (struct timeval *) 0) < 0) {
142 			syslog(LOG_ERR, "select: %m");
143 			exit(0);
144 		}
145 		for (ii = iflist; ii; ii = ii->next) {
146 			if (ii->fd != -1) {
147 				if (!FD_ISSET(ii->fd, &listeners))
148 					continue;
149 			}
150 	again:
151 			cc = read(ii->fd, (char *) buf, bufsize);
152 			/* Don't choke when we get ptraced */
153 			if (cc < 0 && errno == EINTR)
154 				goto again;
155 
156 			bp = buf;
157 			ep = bp + cc;
158 
159 if(bp < ep)
160 	{
161 	mopProcess(ii,buf);
162 	}
163 
164 }
165 
166 }
167 }
168 
169