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