Home | History | Annotate | Line # | Download | only in common
loop-bsd.c revision 1.1
      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  * 3. All advertising materials mentioning features or use of this software
     13  *    must display the following acknowledgement:
     14  *	This product includes software developed by Mats O Jansson.
     15  * 4. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #ifndef LINT
     31 static char rcsid[] = "$Id: loop-bsd.c,v 1.1 1997/03/16 22:23:36 cjs Exp $";
     32 #endif
     33 
     34 #include <stdlib.h>
     35 #include <strings.h>
     36 #include <unistd.h>
     37 #if defined(__bsdi__) || defined(__FreeBSD__)
     38 #include <sys/time.h>
     39 #endif
     40 #include <net/bpf.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/errno.h>
     43 
     44 #include "os.h"
     45 #include "common/common.h"
     46 #include "common/mopdef.h"
     47 
     48 int
     49 mopOpenRC(p, trans)
     50 	struct if_info *p;
     51 	int	trans;
     52 {
     53 #ifndef NORC
     54 	return (*(p->iopen))(p->if_name,
     55 			     O_RDWR,
     56 			     MOP_K_PROTO_RC,
     57 			     trans);
     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 	return (*(p->iopen))(p->if_name,
     70 			     O_RDWR,
     71 			     MOP_K_PROTO_DL,
     72 			     trans);
     73 #else
     74 	return -1;
     75 #endif
     76 }
     77 
     78 void
     79 mopReadRC()
     80 {
     81 }
     82 
     83 void
     84 mopReadDL()
     85 {
     86 }
     87 
     88 /*
     89  * The list of all interfaces that are being listened to.  loop()
     90  * "selects" on the descriptors in this list.
     91  */
     92 struct if_info *iflist;
     93 
     94 void   mopProcess    __P((struct if_info *, u_char *));
     95 
     96 /*
     97  * Loop indefinitely listening for MOP requests on the
     98  * interfaces in 'iflist'.
     99  */
    100 void
    101 Loop()
    102 {
    103 	u_char *buf, *bp, *ep;
    104 	int     cc;
    105 	fd_set  fds, listeners;
    106 	int     bufsize, maxfd = 0;
    107 	struct if_info *ii;
    108 
    109 	if (iflist == 0) {
    110 		syslog(LOG_ERR, "no interfaces");
    111 		exit(0);
    112 	}
    113 	if (iflist->fd != -1) {
    114 		if (ioctl(iflist->fd, BIOCGBLEN, (caddr_t) & bufsize) < 0) {
    115 			syslog(LOG_ERR, "BIOCGBLEN: %m");
    116 			exit(0);
    117 	        }
    118 	}
    119 	buf = (u_char *) malloc((unsigned) bufsize);
    120 	if (buf == 0) {
    121 		syslog(LOG_ERR, "malloc: %m");
    122 		exit(0);
    123 	}
    124 	/*
    125          * Find the highest numbered file descriptor for select().
    126          * Initialize the set of descriptors to listen to.
    127          */
    128 	FD_ZERO(&fds);
    129 	for (ii = iflist; ii; ii = ii->next) {
    130 		if (ii->fd != -1) {
    131 			FD_SET(ii->fd, &fds);
    132 			if (ii->fd > maxfd)
    133 				maxfd = ii->fd;
    134 	        }
    135 	}
    136 	while (1) {
    137 		listeners = fds;
    138 		if (select(maxfd + 1, &listeners, (struct fd_set *) 0,
    139 			(struct fd_set *) 0, (struct timeval *) 0) < 0) {
    140 			syslog(LOG_ERR, "select: %m");
    141 			exit(0);
    142 		}
    143 		for (ii = iflist; ii; ii = ii->next) {
    144 			if (ii->fd != -1) {
    145 				if (!FD_ISSET(ii->fd, &listeners))
    146 					continue;
    147 			}
    148 	again:
    149 			cc = read(ii->fd, (char *) buf, bufsize);
    150 			/* Don't choke when we get ptraced */
    151 			if (cc < 0 && errno == EINTR)
    152 				goto again;
    153 			/* Due to a SunOS bug, after 2^31 bytes, the file
    154 			 * offset overflows and read fails with EINVAL.  The
    155 			 * lseek() to 0 will fix things. */
    156 			if (cc < 0) {
    157 				if (errno == EINVAL &&
    158 				    (lseek(ii->fd, 0, SEEK_CUR) + bufsize) < 0) {
    159 					(void) lseek(ii->fd, 0, 0);
    160 					goto again;
    161 				}
    162 				syslog(LOG_ERR, "read: %m");
    163 				exit(0);
    164 			}
    165 			/* Loop through the packet(s) */
    166 #define bhp ((struct bpf_hdr *)bp)
    167 			bp = buf;
    168 			ep = bp + cc;
    169 			while (bp < ep) {
    170 				register int caplen, hdrlen;
    171 
    172 				caplen = bhp->bh_caplen;
    173 				hdrlen = bhp->bh_hdrlen;
    174 				mopProcess(ii, bp + hdrlen);
    175 				bp += BPF_WORDALIGN(hdrlen + caplen);
    176 			}
    177 		}
    178 	}
    179 }
    180