libpcap 过滤 pcap_compile

libpcap 是使用 lex 和 yacc 生成 scanner.c 等文件,用以过滤的规则生成的。

/* make 中的 lex、yacc 部分步骤 */
./runlex.sh lex -oscanner.c scanner.l
mv scanner.c scanner.c.bottom
cat ./scanner.c.top scanner.c.bottom > scanner.c
yacc -d grammar.y
yacc: 38 shift/reduce conflicts.
mv y.tab.c grammar.c
mv y.tab.h tokdefs.h

pcap_compile 函数主要调用层次

pcap_compile()
{
  lex_init(buf1) {
    yy_scan_string(buf2) {
      yy_scan_bytes(buf3) {
        yy_switch_to_buffer(buf4);  // 把 buf 中的过滤方式写到相应的地方,准备供 yyparse() 函数使用。
      }
    }
  }

  init_linktype();
  pcap_parse() {
    yyparse(); // 把过滤的字符串进行编码
  } 
}

 

posted @ 2016-02-02 09:17  liuyj_vv  阅读(2767)  评论(0编辑  收藏  举报