xref: /openbsd/usr.sbin/mopd/otherOS/loop-linux2.c (revision 043fbe51)
1772b9441Smaja /*
2772b9441Smaja  * Copyright (c) 1993-95 Mats O Jansson.  All rights reserved.
3772b9441Smaja  *
4772b9441Smaja  * Redistribution and use in source and binary forms, with or without
5772b9441Smaja  * modification, are permitted provided that the following conditions
6772b9441Smaja  * are met:
7772b9441Smaja  * 1. Redistributions of source code must retain the above copyright
8772b9441Smaja  *    notice, this list of conditions and the following disclaimer.
9772b9441Smaja  * 2. Redistributions in binary form must reproduce the above copyright
10772b9441Smaja  *    notice, this list of conditions and the following disclaimer in the
11772b9441Smaja  *    documentation and/or other materials provided with the distribution.
12772b9441Smaja  *
13772b9441Smaja  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14772b9441Smaja  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15772b9441Smaja  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16772b9441Smaja  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17772b9441Smaja  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18772b9441Smaja  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19772b9441Smaja  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20772b9441Smaja  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21772b9441Smaja  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22772b9441Smaja  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23772b9441Smaja  */
24772b9441Smaja 
25*ae5feee3Smillert #include <errno.h>
26772b9441Smaja #include <stdlib.h>
27772b9441Smaja #include <strings.h>
28772b9441Smaja #include <unistd.h>
29772b9441Smaja #if defined(__bsdi__) || defined(__FreeBSD__)
30772b9441Smaja #include <sys/time.h>
31772b9441Smaja #endif
32772b9441Smaja #include <sys/ioctl.h>
33772b9441Smaja 
34772b9441Smaja #include "os.h"
35772b9441Smaja #include "common/common.h"
36772b9441Smaja #include "common/mopdef.h"
37772b9441Smaja 
38772b9441Smaja int
mopOpenRC(p,trans)39772b9441Smaja mopOpenRC(p, trans)
40772b9441Smaja 	struct if_info *p;
41772b9441Smaja 	int	trans;
42772b9441Smaja {
43772b9441Smaja #ifndef NORC
44772b9441Smaja 	return (*(p->iopen))(p->if_name,
45772b9441Smaja 			     O_RDWR,
46772b9441Smaja 			     MOP_K_PROTO_RC,
47772b9441Smaja 			     trans);
48772b9441Smaja #else
49772b9441Smaja 	return -1;
50772b9441Smaja #endif
51772b9441Smaja }
52772b9441Smaja 
53772b9441Smaja int
mopOpenDL(p,trans)54772b9441Smaja mopOpenDL(p, trans)
55772b9441Smaja 	struct if_info *p;
56772b9441Smaja 	int	trans;
57772b9441Smaja {
58772b9441Smaja #ifndef NODL
59772b9441Smaja 	return (*(p->iopen))(p->if_name,
60772b9441Smaja 			     O_RDWR,
61772b9441Smaja 			     MOP_K_PROTO_DL,
62772b9441Smaja 			     trans);
63772b9441Smaja #else
64772b9441Smaja 	return -1;
65772b9441Smaja #endif
66772b9441Smaja }
67772b9441Smaja 
68772b9441Smaja void
mopReadRC()69772b9441Smaja mopReadRC()
70772b9441Smaja {
71772b9441Smaja }
72772b9441Smaja 
73772b9441Smaja void
mopReadDL()74772b9441Smaja mopReadDL()
75772b9441Smaja {
76772b9441Smaja }
77772b9441Smaja 
78772b9441Smaja /*
79772b9441Smaja  * The list of all interfaces that are being listened to.  loop()
80772b9441Smaja  * "selects" on the descriptors in this list.
81772b9441Smaja  */
82772b9441Smaja struct if_info *iflist;
83772b9441Smaja 
84c72b5b24Smillert void   mopProcess(struct if_info *, u_char *);
85772b9441Smaja 
86772b9441Smaja /*
87772b9441Smaja  * Loop indefinitely listening for MOP requests on the
88772b9441Smaja  * interfaces in 'iflist'.
89772b9441Smaja  */
90772b9441Smaja void
Loop()91772b9441Smaja Loop()
92772b9441Smaja {
93772b9441Smaja 	u_char *buf, *bp, *ep;
94772b9441Smaja 	int     cc;
95772b9441Smaja 	fd_set  fds, listeners;
96772b9441Smaja 	int     bufsize = 1100, maxfd =0;
97772b9441Smaja 	struct if_info *ii;
98772b9441Smaja 
99772b9441Smaja /* FIXME : this is a hack, for some reason specifying an interface would
100772b9441Smaja  * cause it to fail because bufsize is an impossible number, so I added a
101772b9441Smaja  * sanity check because I'm too lazy to figure out why. -- Karl
102772b9441Smaja  */
103772b9441Smaja 	if (bufsize > 1100)
104772b9441Smaja 	 	bufsize = 64;
105772b9441Smaja 
106772b9441Smaja 	if (iflist == 0) {
107772b9441Smaja 		syslog(LOG_ERR, "no interfaces");
108772b9441Smaja 		exit(0);
109772b9441Smaja 	}
110772b9441Smaja 
111772b9441Smaja 	 buf = (u_char *) malloc((unsigned) bufsize);
112772b9441Smaja 
113772b9441Smaja 	if (buf == 0) {
114772b9441Smaja 		syslog(LOG_ERR, "malloc: %m");
115772b9441Smaja 		exit(0);
116772b9441Smaja 	}
117772b9441Smaja 	/*
118772b9441Smaja          * Find the highest numbered file descriptor for select().
119772b9441Smaja          * Initialize the set of descriptors to listen to.
120772b9441Smaja          */
121772b9441Smaja 	FD_ZERO(&fds);
122772b9441Smaja 	for (ii = iflist; ii; ii = ii->next) {
123772b9441Smaja 		if (ii->fd != -1) {
124772b9441Smaja 			FD_SET(ii->fd, &fds);
125772b9441Smaja 			if (ii->fd > maxfd)
126772b9441Smaja 				maxfd = ii->fd;
127772b9441Smaja 	        }
128772b9441Smaja 	}
129772b9441Smaja 	while (1) {
130772b9441Smaja 		listeners = fds;
131772b9441Smaja 		if (select(maxfd + 1, &listeners, (fd_set *) 0,
132772b9441Smaja 			(fd_set *) 0, (struct timeval *) 0) < 0) {
133772b9441Smaja 			syslog(LOG_ERR, "select: %m");
134772b9441Smaja 			exit(0);
135772b9441Smaja 		}
136772b9441Smaja 		for (ii = iflist; ii; ii = ii->next) {
137772b9441Smaja 			if (ii->fd != -1) {
138772b9441Smaja 				if (!FD_ISSET(ii->fd, &listeners))
139772b9441Smaja 					continue;
140772b9441Smaja 			}
141772b9441Smaja 	again:
142772b9441Smaja 			cc = read(ii->fd, (char *) buf, bufsize);
143772b9441Smaja 			/* Don't choke when we get ptraced */
144772b9441Smaja 			if (cc < 0 && errno == EINTR)
145772b9441Smaja 				goto again;
146772b9441Smaja 
147772b9441Smaja 			bp = buf;
148772b9441Smaja 			ep = bp + cc;
149772b9441Smaja 
150772b9441Smaja if(bp < ep)
151772b9441Smaja 	{
152772b9441Smaja 	mopProcess(ii,buf);
153772b9441Smaja 	}
154772b9441Smaja 
155772b9441Smaja }
156772b9441Smaja 
157772b9441Smaja }
158772b9441Smaja }
159772b9441Smaja 
160