📄 pfilt.c
字号:
#ifndef lintstatic char *sccsid = "@(#)pfilt.c 4.2 (ULTRIX) 2/26/91";#endif lint/************************************************************************ * * * Copyright (c) 1989 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* * Modification History: * * 2-Aug-89 Jeffrey Mogul/DECWRL * Slight restructuring. * * 13-Jun-89 jsd * Created this module for use with the Ethernet Packet Filter * */#include <sys/socket.h>#include <sys/time.h> /* for timeval struct in pfilt.h */#include <sys/file.h>#include <sys/errno.h>#include <net/if.h>#include <net/pfilt.h>#include <stdio.h>#define PFPREFIX "/dev/pf/pfilt" /* prefix for device names *//* define this for reverse compatibility */#define PFCOMPATNAME "/dev/eneta0" /* old style name */#define PFMAXMINORS 256 /* 8-bit minor device field */extern int errno;/* * pfopen(ifname, flags): to support access to the Ethernet Packet Filter. * (using kernel option PACKETFILTER, pseudo-device packetfilter) * * ifname is a ptr to the Ethernet device name ("ln0", "qe0", "pf0", etc.) * or NULL for default * flags are passed to the open() system call. * * return value: * special device file descriptor on success * -1 on failure with errno set to indicate the error * */pfopen(ifname, flags)char *ifname; /* "qe0", "ln0", "ni1", "pf0", etc. or NULL */int flags;{ int i; /* loop counter */ int fd; /* file descriptor */ char tryname[128]; /* device name: "/dev/pf/pfiltnn" */ char genif[16]; /* generic interface name, if needed */ static int setif(); if (ifname && (ifname[0] == 0)) ifname = NULL; /* change empty string to NULL string */#ifdef PFCOMPATNAME /* backwards compatible with Stanford-style packet filter */ if ((fd = open(PFCOMPATNAME, flags, 0)) >= 0) { return(setif(fd, ifname)); }#endif PFCOMPATNAME /* find next available device under the /dev/pf directory */ for (i = 0; i < PFMAXMINORS; i++) { sprintf(tryname, "%s%d", PFPREFIX, i); fd = open(tryname, flags, 0); if (fd < 0) { switch (errno) { case EBUSY: /* device in use */ continue; /* try the next entry */ case ENOENT: /* ran out of filenames */ case ENXIO: /* no more configured in kernel */ default: /* something else went wrong */ return(-1); } } /* open succeeded, set the interface name */ return(setif(fd, ifname)); } return(-1); /* didn't find an openable device */}static int setif(fd, ifname)int fd;char *ifname;{ if (ifname == NULL) /* use default */ return(fd); if (ioctl(fd, EIOCSETIF, ifname) < 0) { close(fd); return(-1); } /* return the file descriptor */ return(fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -