libnids分析(4)

nids初始化中最后一个函数为:scan_init();

void scan_init()
{
  struct timeval tv;

  if (nids_params.scan_num_hosts > 0) //存储口哈希表大小,默认256
  {
    gettimeofday(&tv, 0);
    time0 = tv.tv_sec;
    hashhost = (struct host **) malloc(sizeof(struct host *) * nids_params.scan_num_hosts);
    if (!hashhost)
      nids_params.no_mem("scan_init");
    memset(hashhost, 0, sizeof(struct host *) * nids_params.scan_num_hosts);
  }
}
下面来看下run函数nids_run():
void nids_run()
{
    if (!desc) {
	strcpy(nids_errbuf, "Libnids not initialized");
	return;
    }
    pcap_loop(desc, -1, (pcap_handler) pcap_hand, 0);
    clear_stream_buffers();
    strcpy(nids_errbuf, "loop: ");
    strncat(nids_errbuf, pcap_geterr(desc), sizeof(nids_errbuf) - 7);
    pcap_close(desc);
}

run函数调用pcap_loop()函数。此函数为libpcap中的函数,无限循环抓包,抓到的包交给回调函数处理。

回调函数处理流程如下:

static void pcap_hand(u_char * par, struct pcap_pkthdr *hdr, u_char * data)
{
    struct proc_node *i;
    u_char *data_aligned;
#ifdef DLT_IEEE802_11
    unsigned short fc;
    int linkoffset_tweaked_by_prism_code = 0;
#endif
    nids_last_pcap_header = hdr;
    (void)par; /* warnings... */
    switch (linktype) {
    case DLT_EN10MB:
	if (hdr->caplen < 14)
	    return;
	/* Only handle IP packets and 802.1Q VLAN tagged packets below. */
	if (data[12] == 8 && data[13] == 0) {
	    /* Regular ethernet */
	    linkoffset = 14;
	} else if (data[12] == 0x81 && data[13] == 0) {
	    /* Skip 802.1Q VLAN and priority information */
	    linkoffset = 18;
	} else
	    /* non-ip frame */
	    return;
	break;
#ifdef DLT_PRISM_HEADER
#ifndef DLT_IEEE802_11
#error DLT_PRISM_HEADER is defined, but DLT_IEEE802_11 is not ???
#endif
    case DLT_PRISM_HEADER:
	linkoffset = 144; //sizeof(prism2_hdr);
	linkoffset_tweaked_by_prism_code = 1;
        //now let DLT_IEEE802_11 do the rest
#endif
#ifdef DLT_IEEE802_11
    case DLT_IEEE802_11:
	/* I don't know why frame control is always little endian, but it 
	 * works for tcpdump, so who am I to complain? (wam)
	 */
	if (!linkoffset_tweaked_by_prism_code)
		linkoffset = 0;
	fc = EXTRACT_LE_16BITS(data + linkoffset);
	if (FC_TYPE(fc) != T_DATA || FC_WEP(fc)) {
	    return;
	}
	if (FC_TO_DS(fc) && FC_FROM_DS(fc)) {
	    /* a wireless distribution system packet will have another
	     * MAC addr in the frame
	     */
	    linkoffset += 30;
	} else {
	    linkoffset += 24;
	}
	if (hdr->len < linkoffset + LLC_FRAME_SIZE)
	    return;
	if (ETHERTYPE_IP !=
	    EXTRACT_16BITS(data + linkoffset + LLC_OFFSET_TO_TYPE_FIELD)) {
	    /* EAP, LEAP, and other 802.11 enhancements can be 
	     * encapsulated within a data packet too.  Look only at
	     * encapsulated IP packets (Type field of the LLC frame).
	     */
	    return;
	}
	linkoffset += LLC_FRAME_SIZE;
	break;
#endif
    default:;
    }
    if (hdr->caplen < linkoffset)
	return;

/*
* sure, memcpy costs. But many EXTRACT_{SHORT, LONG} macros cost, too. 
* Anyway, libpcap tries to ensure proper layer 3 alignment (look for
* handle->offset in pcap sources), so memcpy should not be called.
*/
#ifdef LBL_ALIGN
    if ((unsigned long) (data + linkoffset) & 0x3) {
	data_aligned = alloca(hdr->caplen - linkoffset + 4);
	data_aligned -= (unsigned long) data_aligned % 4;
	memcpy(data_aligned, data + linkoffset, hdr->caplen - linkoffset);
    } else
#endif
	data_aligned = data + linkoffset;
    for (i = ip_frag_procs; i; i = i->next)
	(i->item) (data_aligned, hdr->caplen - linkoffset);
}
对于每一个包,都调用函数gen_ip_proc()进行处理,判断其类型,以便进行重组。
static void gen_ip_proc(u_char * data, int skblen)
{
    switch (((struct ip *) data)->ip_p) {
    case IPPROTO_TCP:
	process_tcp(data, skblen);
	break;
    case IPPROTO_UDP:
	process_udp(data);
	break;
    case IPPROTO_ICMP:
	if (nids_params.n_tcp_streams)
	    process_icmp(data);
	break;
    default:
	break;
    }
}


首先分析一下抓包函数和回调函数之间的关系

函数名称:int pcap_loop(pcap_t * p,int cnt, pcap_handler callback, uchar * user);  

函数功能:捕获数据包,不会响应pcap_open_live()函数设置的超时时间  

参数说明:p 是由pcap_open_live()返回的所打的网卡的指针;

cnt用于设置所捕获数据包的个数;

pcap_handler 是与void packet_handler()使用的一个参数,即回调函数的名称;

user值一般为NULL  

pcap_loop原型是pcap_loop(pcap_t *p,int cnt,pcap_handler callback,u_char *user)  

其中第一个参数是winpcap的句柄,第二个是指定捕获的数据包个数,如果为-1则无限循环捕获。第四个参数user是留给用户使用的。  

第三个是回调函数其原型如下:  pcap_callback(u_char* argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content)  

其中参数pcap_content表示的捕获到的数据包的内容  

参数argument是从函数pcap_loop()传递过来的。注意:这里的参数就是指 pcap_loop中的 *user 参数  

参数pcap_pkthdr 表示捕获到的数据包基本信息,包括时间,长度等信息.  另

外:回调函数必须是全局函数或静态函数,其参数默认,比如pcap_loop()可以写成  

pcap_loop(pcap_handle,10,pcap_callback,NULL)不能往里面传递实参.  

-----------------------------------------------------------------------------------------------------------------  

pcap_loop和callback之间参数存在联系: 

 pcap_loop的最后一个参数user是留给用户使用的,当callback被调用的时候这个值会传递给callback的第一个参数(也叫user),

callback的最后一个参数p指向一块内存空间,这个空间中存放的就是pcap_loop抓到的数据包。

callback的第二个参数是一个结构体指针,该结构体定义如下:  

struct pcap_pkthdr

{  struct timeval ts; /* 时间戳 */  

bpf_u_int32 caplen; /* 已捕获部分的长度 */  

bpf_u_int32 len; /* 该包的脱机长度 */  

}; 

 

这个结构体是由pcap_loop自己填充的,用来取得一些关于数据包的信息  

所以,在callback函数当中只有第一个user指针是可以留给用户使用的,

如果你想给callback传递自己参数,那就只能通过pcap_loop的最后一个参数user来实现了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值