home *** CD-ROM | disk | FTP | other *** search
- /*
- * etherinit.c
- *
- * Network-related subroutines for ether statistics programs
- *
- */
-
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <net/pfilt.h>
-
- struct endevp DevParams;
- int backlog = -1;
-
- /* Returns fid for ethernet device */
- StartEther(ifname, MulticastOnly, BroadcastOnly)
- char *ifname;
- int MulticastOnly;
- int BroadcastOnly;
- {
- struct eniocb IOCB;
- short enmode;
- int EtherFid;
-
- if ((EtherFid = pfopen(ifname, 0)) < 0) {
- perror(ifname);
- exit(1);
- }
-
- if (ioctl(EtherFid, EIOCDEVP, &DevParams) < 0) {
- perror(ifname);
- exit(1);
- }
-
- IOCB.en_rtout = 50; /* in clock ticks */
-
- if (ioctl(EtherFid, EIOCSETP, &IOCB) < 0) {
- perror(ifname);
- exit(1);
- }
-
- enmode = ENBATCH|ENTSTAMP|ENPROMISC|ENNONEXCL;
- if (ioctl(EtherFid, EIOCMBIS, &enmode) < 0) {
- perror(ifname);
- exit(1);
- }
-
- if (ioctl(EtherFid, EIOCSETW, &backlog) < 0) {
- perror(ifname);
- exit(1);
- }
-
- BuildFilter(EtherFid, MulticastOnly, BroadcastOnly);
-
- return(EtherFid);
- }
-
- /*
- * filter accepts all packets (only multicasts if mcOnly is true)
- */
-
- BuildFilter(EtherFid, mcOnly, bcOnly)
- int EtherFid;
- int mcOnly;
- int bcOnly;
- {
- struct enfilter Filter;
-
- Filter.enf_Priority = 37; /* anything > 2 should work */
- Filter.enf_FilterLen = 0;
-
- /* null filter should come out to be "always true" */
-
- if (mcOnly) { /* check the destination address multicast bit */
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 0;
- #ifndef ENF_PUSHONE
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHLIT | ENF_AND;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- htons(0x0100);
- /* low bit of first byte is first bit on ether */
- #else
- /* low bit of first byte is first bit on ether */
- if (htons(0x0100) == 1) {
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHONE | ENF_AND;
- }
- else {
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHLIT | ENF_AND;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- htons(0x0100);
- }
- #endif ENF_PUSHONE
- } else if (bcOnly) { /* check all destination words */
- #ifndef ENF_PUSHONE
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 0;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHLIT | ENF_CAND;
- Filter.enf_Filter[Filter.enf_FilterLen++] = 0xFFFF;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 1;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHLIT | ENF_CAND;
- Filter.enf_Filter[Filter.enf_FilterLen++] = 0xFFFF;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 2;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHLIT | ENF_EQ;
- Filter.enf_Filter[Filter.enf_FilterLen++] = 0xFFFF;
- #else
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 0;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHFFFF | ENF_CAND;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 1;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHFFFF | ENF_CAND;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHWORD + 2;
- Filter.enf_Filter[Filter.enf_FilterLen++] =
- ENF_PUSHFFFF | ENF_EQ;
- #endif ENF_PUSHONE
- }
-
- if (ioctl(EtherFid, EIOCSETF, &Filter) < 0) {
- perror("setting filter");
- exit(1);
- }
- }
-
- char *EtherInterfaceName(EtherFid)
- int EtherFid;
- {
- static struct ifreq ifr;
-
- if (ioctl(EtherFid, EIOCIFNAME, &ifr) < 0) {
- perror("getting interface name");
- return("unknown");
- }
- return(ifr.ifr_name);
- }
-