--- ansh/src/utils.c 2011/10/10 09:11:48 1.1.1.1.2.2 +++ ansh/src/utils.c 2011/10/10 13:56:30 1.1.1.1.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: utils.c,v 1.1.1.1.2.2 2011/10/10 09:11:48 misho Exp $ + * $Id: utils.c,v 1.1.1.1.2.3 2011/10/10 13:56:30 misho Exp $ * *************************************************************************/ #include "global.h" @@ -29,10 +29,20 @@ PrepareL2(const char *psDev, int *bpflen) register int i; char szStr[STRSIZ]; struct ifreq ifr; + struct bpf_program fcode = { 0 }; + struct bpf_insn insns[] = { + BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ANSH_ID, 0, 1), + BPF_STMT(BPF_RET + BPF_K, -1), + BPF_STMT(BPF_RET + BPF_K, 0), + }; FTRACE(3); assert(psDev); + fcode.bf_len = sizeof(insns) / sizeof(struct bpf_insn); + fcode.bf_insns = insns; + for (i = 0; i < 10; i++) { memset(szStr, 0, sizeof szStr); snprintf(szStr, sizeof szStr, "/dev/bpf%d", i); @@ -51,17 +61,32 @@ PrepareL2(const char *psDev, int *bpflen) close(h); return -1; } + /* + if (ioctl(h, BIOCSHDRCMPLT, &n) == -1) { + printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno)); + close(h); + return -1; + } + */ if (ioctl(h, BIOCIMMEDIATE, &n) == -1) { printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno)); close(h); return -1; } + if (ioctl(h, BIOCSETF, &fcode) == -1) { + printf("Error:: set filter interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno)); + close(h); + return -1; + } if (ioctl(h, BIOCGBLEN, bpflen) == -1) { printf("Error:: get interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno)); close(h); return -1; } + n = fcntl(h, F_GETFL); + fcntl(h, F_SETFL, n | O_NONBLOCK); + VERB(3) LOG("Openned device handle %d with bpf buflen %d", h, *bpflen); return h; } @@ -218,7 +243,7 @@ icmpSend(int s, u_short id, char flg, u_int crypted, u } int -pktSend(int s, u_short id, char flg, u_int crypted, u_char *data, int datlen, struct ether_addr *ea) +pktSend(int s, char flg, u_int crypted, u_char *data, int datlen, struct ether_addr *ea) { u_char *pos, buf[USHRT_MAX] = { 0 }; struct ether_header *e = (struct ether_header*) buf; @@ -229,7 +254,7 @@ pktSend(int s, u_short id, char flg, u_int crypted, u_ if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen) return ANSH_FLG_ERR; - e->ether_type = htons(id); + e->ether_type = ntohs(ANSH_ID); memcpy(e->ether_dhost, ea->octet, ETHER_ADDR_LEN); hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN); pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);