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