1 /* $OpenBSD: loop.c,v 1.3 1998/07/07 17:32:44 art 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 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Mats O Jansson. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef LINT 33 static char rcsid[] = "$OpenBSD: loop.c,v 1.3 1998/07/07 17:32:44 art Exp $"; 34 #endif 35 36 #include "os.h" 37 #include "common/common.h" 38 #include "common/mopdef.h" 39 40 /* 41 * The list of all interfaces that are being listened to. loop() 42 * "selects" on the descriptors in this list. 43 */ 44 struct if_info *iflist; 45 u_char buf[BUFSIZE]; 46 47 void mopProcess (/* struct if_info *, u_char * */); 48 49 int 50 mopOpenRC(p, trans) 51 struct if_info *p; 52 int trans; 53 { 54 #ifndef NORC 55 int fd; 56 57 fd = (*(p->iopen))(p->if_name, 58 O_RDWR, 59 MOP_K_PROTO_RC, 60 trans); 61 if (fd >= 0) { 62 pfAddMulti(fd, p->if_name, rc_mcst); 63 pfEthAddr(fd, p->eaddr); 64 } 65 66 return fd; 67 #else 68 return -1; 69 #endif 70 } 71 72 int 73 mopOpenDL(p, trans) 74 struct if_info *p; 75 int trans; 76 { 77 #ifndef NODL 78 int fd; 79 80 fd = (*(p->iopen))(p->if_name, 81 O_RDWR, 82 MOP_K_PROTO_DL, 83 trans); 84 if (fd >= 0) { 85 pfAddMulti(fd, p->if_name, dl_mcst); 86 pfEthAddr(fd, p->eaddr); 87 } 88 89 return fd; 90 #else 91 return -1; 92 #endif 93 } 94 95 void 96 mopReadRC(p, fd) 97 struct if_info *p; 98 int fd; 99 { 100 int cc; 101 102 if ((cc = pfRead(fd, buf+HDRSIZ, BUFSIZE-HDRSIZ)) < 0) { 103 return; 104 } 105 106 if (cc == 0) 107 return; 108 109 mopProcess(p, buf+HDRSIZ); 110 111 return; 112 } 113 114 void 115 mopReadDL(p, fd) 116 struct if_info *p; 117 int fd; 118 { 119 int cc; 120 121 if ((cc = pfRead(fd, buf+HDRSIZ, BUFSIZE-HDRSIZ)) < 0) { 122 return; 123 } 124 125 if (cc == 0) 126 return; 127 128 mopProcess(p, buf+HDRSIZ); 129 130 return; 131 } 132 133 /* 134 * Loop indefinitely listening for MOP requests on the 135 * interfaces in 'iflist'. 136 */ 137 void 138 Loop() 139 { 140 fd_set fds, listeners; 141 int maxfd = 0; 142 struct if_info *ii; 143 144 if (iflist == 0) { 145 fprintf(stderr,"no interfaces"); 146 exit(0); 147 } 148 149 /* 150 * Find the highest numbered file descriptor for select(). 151 * Initialize the set of descriptors to listen to. 152 */ 153 FD_ZERO(&fds); 154 for (ii = iflist; ii; ii = ii->next) { 155 if (ii->fd != -1) { 156 FD_SET(ii->fd, &fds); 157 if (ii->fd > maxfd) 158 maxfd = ii->fd; 159 } 160 } 161 while (1) { 162 listeners = fds; 163 if (select(maxfd + 1, &listeners, (fd_set *) 0, 164 (fd_set *) 0, (struct timeval *) 0) < 0) { 165 fprintf(stderr, "select: %s"); 166 exit(0); 167 } 168 for (ii = iflist; ii; ii = ii->next) { 169 if (ii->fd != -1) { 170 if (FD_ISSET(ii->fd, &listeners)) 171 (*(ii->read))(ii,ii->fd); 172 } 173 } 174 } 175 } 176 177 178 179 180 181