xref: /openbsd/usr.sbin/mopd/otherOS/loop-linux2.c (revision 73471bf0)
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  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <strings.h>
28 #include <unistd.h>
29 #if defined(__bsdi__) || defined(__FreeBSD__)
30 #include <sys/time.h>
31 #endif
32 #include <sys/ioctl.h>
33 
34 #include "os.h"
35 #include "common/common.h"
36 #include "common/mopdef.h"
37 
38 int
39 mopOpenRC(p, trans)
40 	struct if_info *p;
41 	int	trans;
42 {
43 #ifndef NORC
44 	return (*(p->iopen))(p->if_name,
45 			     O_RDWR,
46 			     MOP_K_PROTO_RC,
47 			     trans);
48 #else
49 	return -1;
50 #endif
51 }
52 
53 int
54 mopOpenDL(p, trans)
55 	struct if_info *p;
56 	int	trans;
57 {
58 #ifndef NODL
59 	return (*(p->iopen))(p->if_name,
60 			     O_RDWR,
61 			     MOP_K_PROTO_DL,
62 			     trans);
63 #else
64 	return -1;
65 #endif
66 }
67 
68 void
69 mopReadRC()
70 {
71 }
72 
73 void
74 mopReadDL()
75 {
76 }
77 
78 /*
79  * The list of all interfaces that are being listened to.  loop()
80  * "selects" on the descriptors in this list.
81  */
82 struct if_info *iflist;
83 
84 void   mopProcess(struct if_info *, u_char *);
85 
86 /*
87  * Loop indefinitely listening for MOP requests on the
88  * interfaces in 'iflist'.
89  */
90 void
91 Loop()
92 {
93 	u_char *buf, *bp, *ep;
94 	int     cc;
95 	fd_set  fds, listeners;
96 	int     bufsize = 1100, maxfd =0;
97 	struct if_info *ii;
98 
99 /* FIXME : this is a hack, for some reason specifying an interface would
100  * cause it to fail because bufsize is an impossible number, so I added a
101  * sanity check because I'm too lazy to figure out why. -- Karl
102  */
103 	if (bufsize > 1100)
104 	 	bufsize = 64;
105 
106 	if (iflist == 0) {
107 		syslog(LOG_ERR, "no interfaces");
108 		exit(0);
109 	}
110 
111 	 buf = (u_char *) malloc((unsigned) bufsize);
112 
113 	if (buf == 0) {
114 		syslog(LOG_ERR, "malloc: %m");
115 		exit(0);
116 	}
117 	/*
118          * Find the highest numbered file descriptor for select().
119          * Initialize the set of descriptors to listen to.
120          */
121 	FD_ZERO(&fds);
122 	for (ii = iflist; ii; ii = ii->next) {
123 		if (ii->fd != -1) {
124 			FD_SET(ii->fd, &fds);
125 			if (ii->fd > maxfd)
126 				maxfd = ii->fd;
127 	        }
128 	}
129 	while (1) {
130 		listeners = fds;
131 		if (select(maxfd + 1, &listeners, (fd_set *) 0,
132 			(fd_set *) 0, (struct timeval *) 0) < 0) {
133 			syslog(LOG_ERR, "select: %m");
134 			exit(0);
135 		}
136 		for (ii = iflist; ii; ii = ii->next) {
137 			if (ii->fd != -1) {
138 				if (!FD_ISSET(ii->fd, &listeners))
139 					continue;
140 			}
141 	again:
142 			cc = read(ii->fd, (char *) buf, bufsize);
143 			/* Don't choke when we get ptraced */
144 			if (cc < 0 && errno == EINTR)
145 				goto again;
146 
147 			bp = buf;
148 			ep = bp + cc;
149 
150 if(bp < ep)
151 	{
152 	mopProcess(ii,buf);
153 	}
154 
155 }
156 
157 }
158 }
159 
160