/* * BPF receiver */ #include #include #include #include #include #include #include #include #include #include #define MY_ETHERTYPE 0x9999 // Ethertype of my ethernetframe /* this filter checks for 0x9999 in the ethernet type field */ struct bpf_insn my_filter[] = { BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12), BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, MY_ETHERTYPE, 0, 1), BPF_STMT (BPF_RET+BPF_K, (u_int) -1), BPF_STMT (BPF_RET+BPF_K, 0), }; struct bpf_program filter = { sizeof (my_filter) / sizeof (struct bpf_insn), my_filter }; int main(int argc, char *argv[]) { int PktLen, fd, BytesRead; int i, ret,select_return; const int set=1; struct ifreq ifr; char ifname[]="en0"; fd_set fdset; char buf[2048]={}; memset(buf,0,2048); if (( fd = open("/dev/bpf0", O_RDONLY, 0)) < 0) { perror("open"); exit(EXIT_FAILURE); } strcpy (ifr.ifr_name, ifname); printf("device name set to %s\n",ifr.ifr_name); /* BPF buffer . */ if (ioctl_socket (fd, BIOCSBLEN, &PktLen) < 0) { perror("ioctl_socket BIOCSBLEN"); // this is all for errror checking } /* Set interface. */ if (ioctl_socket (fd, BIOCSETIF, &ifr) < 0) { perror("ioctl_socket BIOCSETIF "); } /* Read buffer immediately. */ if (ioctl_socket (fd, BIOCIMMEDIATE, &set) < 0) { perror("ioctl_socket BIOCIMMEDIATE "); } /* filter. */ if (ioctl_socket (fd, BIOCSETF, (caddr_t) &filter) < 0) { perror("ioctl_socket BIOCSETF "); } printf(" the bfd fd is %d",fd); /* setting up the select*/ FD_ZERO( &fdset); FD_SET( fd, &fdset); if((select_return = select( (1+fd), &fdset, NULL , NULL , NULL) )) { perror("Select "); printf("\n select return %d\n", select_return); /* check if the socket is set */ if( (FD_ISSET(fd, &fdset) )) { printf("\n reading from bfp device socket is set \n"); if ((BytesRead = read(fd, buf, PktLen)) < 0) { perror("read"); exit(EXIT_FAILURE); } printf("%d returned from read, errno = %d..\n", BytesRead, errno); } } close(fd); return EXIT_SUCCESS; }