*ebuf = '\0';
pd = pcap_open_live(device, snaplen, !pflag, 1000, ebuf); if (pd == NULL) error("%s", ebuf); else if (*ebuf) warning("%s", ebuf); #endif /* HAVE_PCAP_CREATE */ //如果没有定义HAVE_PCAP_CREATE, //则通过pcap_open_live 获得用于捕获网络数据包的数据包捕获描述字 、、~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* * Let user own process after socket has been opened. */ #ifndef WIN32 if (setgid(getgid()) != 0 || setuid(getuid()) != 0) fprintf(stderr, "Warning: setgid/setuid failed !\n"); #endif /* WIN32 */ //设置用户ID和组ID
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if !defined(HAVE_PCAP_CREATE) && defined(WIN32)
if(Bflag != 0) if(pcap_setbuff(pd, Bflag)==-1) { error("%s", pcap_geterr(pd)); } #endif /* !defined(HAVE_PCAP_CREATE) && defined(WIN32) */ //设置内核缓冲区 //自定义检查点: 1 2 3、、~~~~~~~~~~~~~~~~~~~~~~~~~~
i = pcap_snapshot(pd);
if (snaplen < i) { warning("snaplen raised from %d to %d", snaplen, i); snaplen = i; } //返回快照长度,并与snaplen比较、、~~~~~~~~~~
if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0)
{ localnet = 0; netmask = 0; warning("%s", ebuf); } //获取网卡的配置信息//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
if (infile)
cmdbuf = read_infile(infile); else cmdbuf = copy_argv(&argv[optind]); //这里假设我们没有读取文件 //设置cmdbuf指向缓冲区//自定义检查点: 1 2 3
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
error("%s", pcap_geterr(pd)); //设置过滤条件/
init_addrtoname(localnet, netmask);
init_checksum(); //做一些初始化操作
#ifndef WIN32 (void)setsignal(SIGPIPE, cleanup); (void)setsignal(SIGTERM, cleanup); (void)setsignal(SIGINT, cleanup); #endif /* WIN32 */ //设置一些信号处理函数句柄
#if defined(HAVE_FORK) || defined(HAVE_VFORK)
(void)setsignal(SIGCHLD, child_cleanup); #endif /* Cooperate with nohup(1) */ //设置一些信号处理函数句柄