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