loop-linux2.c revision 1.5
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#ifndef LINT 26static const char rcsid[] = "$Id: loop-linux2.c,v 1.5 2005/12/21 01:40:24 millert Exp $"; 27#endif 28 29#include <errno.h> 30#include <stdlib.h> 31#include <strings.h> 32#include <unistd.h> 33#if defined(__bsdi__) || defined(__FreeBSD__) 34#include <sys/time.h> 35#endif 36#include <sys/ioctl.h> 37 38#include "os.h" 39#include "common/common.h" 40#include "common/mopdef.h" 41 42int 43mopOpenRC(p, trans) 44 struct if_info *p; 45 int trans; 46{ 47#ifndef NORC 48 return (*(p->iopen))(p->if_name, 49 O_RDWR, 50 MOP_K_PROTO_RC, 51 trans); 52#else 53 return -1; 54#endif 55} 56 57int 58mopOpenDL(p, trans) 59 struct if_info *p; 60 int trans; 61{ 62#ifndef NODL 63 return (*(p->iopen))(p->if_name, 64 O_RDWR, 65 MOP_K_PROTO_DL, 66 trans); 67#else 68 return -1; 69#endif 70} 71 72void 73mopReadRC() 74{ 75} 76 77void 78mopReadDL() 79{ 80} 81 82/* 83 * The list of all interfaces that are being listened to. loop() 84 * "selects" on the descriptors in this list. 85 */ 86struct if_info *iflist; 87 88void mopProcess(struct if_info *, u_char *); 89 90/* 91 * Loop indefinitely listening for MOP requests on the 92 * interfaces in 'iflist'. 93 */ 94void 95Loop() 96{ 97 u_char *buf, *bp, *ep; 98 int cc; 99 fd_set fds, listeners; 100 int bufsize = 1100, maxfd =0; 101 struct if_info *ii; 102 103/* FIXME : this is a hack, for some reason specifying an interface would 104 * cause it to fail because bufsize is an impossible number, so I added a 105 * sanity check because I'm too lazy to figure out why. -- Karl 106 */ 107 if (bufsize > 1100) 108 bufsize = 64; 109 110 if (iflist == 0) { 111 syslog(LOG_ERR, "no interfaces"); 112 exit(0); 113 } 114 115 buf = (u_char *) malloc((unsigned) bufsize); 116 117 if (buf == 0) { 118 syslog(LOG_ERR, "malloc: %m"); 119 exit(0); 120 } 121 /* 122 * Find the highest numbered file descriptor for select(). 123 * Initialize the set of descriptors to listen to. 124 */ 125 FD_ZERO(&fds); 126 for (ii = iflist; ii; ii = ii->next) { 127 if (ii->fd != -1) { 128 FD_SET(ii->fd, &fds); 129 if (ii->fd > maxfd) 130 maxfd = ii->fd; 131 } 132 } 133 while (1) { 134 listeners = fds; 135 if (select(maxfd + 1, &listeners, (fd_set *) 0, 136 (fd_set *) 0, (struct timeval *) 0) < 0) { 137 syslog(LOG_ERR, "select: %m"); 138 exit(0); 139 } 140 for (ii = iflist; ii; ii = ii->next) { 141 if (ii->fd != -1) { 142 if (!FD_ISSET(ii->fd, &listeners)) 143 continue; 144 } 145 again: 146 cc = read(ii->fd, (char *) buf, bufsize); 147 /* Don't choke when we get ptraced */ 148 if (cc < 0 && errno == EINTR) 149 goto again; 150 151 bp = buf; 152 ep = bp + cc; 153 154if(bp < ep) 155 { 156 mopProcess(ii,buf); 157 } 158 159} 160 161} 162} 163 164