Diff for /ansh/src/utils.c between versions 1.1.1.1.2.1 and 1.1.1.1.2.4

version 1.1.1.1.2.1, 2011/10/07 13:41:26 version 1.1.1.1.2.4, 2011/10/10 14:15:40
Line 29  PrepareL2(const char *psDev, int *bpflen) Line 29  PrepareL2(const char *psDev, int *bpflen)
         register int i;          register int i;
         char szStr[STRSIZ];          char szStr[STRSIZ];
         struct ifreq ifr;          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);          FTRACE(3);
         assert(psDev);          assert(psDev);
   
           fcode.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
           fcode.bf_insns = insns;
   
         for (i = 0; i < 10; i++) {          for (i = 0; i < 10; i++) {
                 memset(szStr, 0, sizeof szStr);                  memset(szStr, 0, sizeof szStr);
                 snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);                  snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
Line 51  PrepareL2(const char *psDev, int *bpflen) Line 61  PrepareL2(const char *psDev, int *bpflen)
                 close(h);                  close(h);
                 return -1;                  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) {          if (ioctl(h, BIOCIMMEDIATE, &n) == -1) {
                 printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));                  printf("Error:: set interface %s to bpf #%d - %s\n", psDev, errno, strerror(errno));
                 close(h);                  close(h);
                 return -1;                  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) {          if (ioctl(h, BIOCGBLEN, bpflen) == -1) {
                 printf("Error:: get interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno));                  printf("Error:: get interface %s buffer length #%d - %s\n", psDev, errno, strerror(errno));
                 close(h);                  close(h);
                 return -1;                  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);          VERB(3) LOG("Openned device handle %d with bpf buflen %d", h, *bpflen);
         return h;          return h;
 }  }
Line 218  icmpSend(int s, u_short id, char flg, u_int crypted, u Line 245  icmpSend(int s, u_short id, char flg, u_int crypted, u
 }  }
   
 int  int
pktSend(int s, u_short id, char flg, 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 };          u_char *pos, buf[USHRT_MAX] = { 0 };
         struct ether_header *e = (struct ether_header*) buf;          struct ether_header *e = (struct ether_header*) buf;
Line 229  pktSend(int s, u_short id, char flg, u_char *data, int Line 256  pktSend(int s, u_short id, char flg, u_char *data, int
         if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen)          if ((sizeof buf - ETHER_HDR_LEN + sizeof(struct ansh_hdr)) < datlen)
                 return ANSH_FLG_ERR;                  return ANSH_FLG_ERR;
   
        e->ether_type = htons(id);        e->ether_type = ntohs(ANSH_ID);
         memcpy(e->ether_dhost, ea->octet, ETHER_ADDR_LEN);          memcpy(e->ether_dhost, ea->octet, ETHER_ADDR_LEN);
         hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN);          hdr = (struct ansh_hdr*) (buf + ETHER_HDR_LEN);
         pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);          pos = ((u_char*) hdr) + sizeof(struct ansh_hdr);
Line 239  pktSend(int s, u_short id, char flg, u_char *data, int Line 266  pktSend(int s, u_short id, char flg, u_char *data, int
         hdr->ansh_ver = ANSH_VERSION;          hdr->ansh_ver = ANSH_VERSION;
         hdr->ansh_flg = flg;          hdr->ansh_flg = flg;
         hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));          hdr->ansh_len = htons(datlen + sizeof(struct ansh_hdr));
           hdr->ansh_nonce = htonl(crypted);
         hdr->ansh_crc = 0;          hdr->ansh_crc = 0;
         hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));          hdr->ansh_crc = htonl(crcAdler((u_char*) hdr, ntohs(hdr->ansh_len)));
   
Line 257  pktSend(int s, u_short id, char flg, u_char *data, int Line 285  pktSend(int s, u_short id, char flg, u_char *data, int
 }  }
   
 char  char
pktRecv(int s, u_char * __restrict data, int * __restrict datlen, struct ether_header *eth)pktRecv(int s, u_int * __restrict crypted, u_char * __restrict data, int * __restrict datlen, 
                 struct ether_header *eth)
 {  {
         int ret = 0;          int ret = 0;
         struct bpf_hdr *bpf;          struct bpf_hdr *bpf;
Line 300  pktRecv(int s, u_char * __restrict data, int * __restr Line 329  pktRecv(int s, u_char * __restrict data, int * __restr
                 free(buf);                  free(buf);
                 return ANSH_FLG_ERR;                  return ANSH_FLG_ERR;
         }          }
           if (crypted) {
                   if (hdr->ansh_nonce && !*crypted) {
                           VERB(3) LOG("Channel INSECURED:: Crypted communication not supported at this moment ...");
                           return ANSH_FLG_ERR;
                   }
                   if (!hdr->ansh_nonce && *crypted) {
                           VERB(3) LOG("Channel SECURED:: Plain text communication not supported at this moment ...");
                           return ANSH_FLG_ERR;
                   }
   
                   *crypted = ntohl(hdr->ansh_nonce);
           }
   
         /* check crc of packet */          /* check crc of packet */
         crc = hdr->ansh_crc;          crc = hdr->ansh_crc;
         hdr->ansh_crc ^= hdr->ansh_crc;          hdr->ansh_crc ^= hdr->ansh_crc;
Line 314  pktRecv(int s, u_char * __restrict data, int * __restr Line 356  pktRecv(int s, u_char * __restrict data, int * __restr
         if (data) {          if (data) {
                 memset(data, 0, *datlen);                  memset(data, 0, *datlen);
                 *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);                  *datlen = ntohs(hdr->ansh_len) - sizeof(struct ansh_hdr);
                memcpy(data, hdr + sizeof(struct ansh_hdr), *datlen);                memcpy(data, buf + bpf->bh_hdrlen + ETHER_HDR_LEN + sizeof(struct ansh_hdr), *datlen);
         }          }
   
         ret = (char) hdr->ansh_flg;          ret = (char) hdr->ansh_flg;

Removed from v.1.1.1.1.2.1  
changed lines
  Added in v.1.1.1.1.2.4


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>