Tcpdump源碼分析系列 5 : main函數研究

上文是假設當前進行live capture windows

那麼接下來的代碼是: 函數

if (device == NULL)
  {
   device = pcap_lookupdev(ebuf);
   if (device == NULL)
    error("%s", ebuf);
  }

這裏看到了一個函數pcap_lookupdev,做用:pcap_lookupdev()來找網卡設備。 this

~~~~~~~~~~~~~~~~~~~~~
spa

#ifdef WIN32
  /*
   * Print a message to the standard error on Windows.
   * XXX - why do it here, with a different message?
   */
  if(strlen(device) == 1) //we assume that an ASCII string is always longer than 1 char
  {      //a Unicode string has a \0 as second byte (so strlen() is 1)
   fprintf(stderr, "%s: listening on %ws\n", program_name, device);
  }
  else
  {
   fprintf(stderr, "%s: listening on %s\n", program_name, device);
  } code

  fflush(stderr); 
  #endif /* WIN32 */
  //windows環境下,不考慮這個代碼 get

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string

 pd = pcap_create(device, ebuf);
  if (pd == NULL)
   error("%s", ebuf);
  //調用pcap_create()能夠爲一個活動的抓取器打開一個句柄
  //自定義檢查點: 1 2 3 it

//~~~~~~~~~~~~~~~~~~ im

 /*
   * Is this an interface that supports monitor mode?
   */
  if (pcap_can_set_rfmon(pd) == 1)
   supports_monitor_mode = 1;
  else
   supports_monitor_mode = 0;
  //能夠用於肯定適配器是否能夠進入監視模式 error

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

status = pcap_set_snaplen(pd, snaplen);
  if (status != 0)
   error("%s: Can't set snapshot length: %s",
       device, pcap_statustostr(status));
  //設置快照長度爲65535

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 status = pcap_set_promisc(pd, !pflag);
  if (status != 0)
   error("%s: Can't set promiscuous mode: %s",
       device, pcap_statustostr(status));
  //設置混雜模式

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if (Iflag)
  {
   status = pcap_set_rfmon(pd, 1);
   if (status != 0)
    error("%s: Can't set monitor mode: %s",
        device, pcap_statustostr(status));
  }
  //設置監視模式
  
  status = pcap_set_timeout(pd, 1000);
  if (status != 0)
   error("%s: pcap_set_timeout failed: %s",
       device, pcap_statustostr(status));
  //設置超時時間
  
  if (Bflag != 0)
  {
   status = pcap_set_buffer_size(pd, Bflag);
   if (status != 0)
    error("%s: Can't set buffer size: %s",
        device, pcap_statustostr(status));
  }
  //設置緩衝區大小

status = pcap_activate(pd);   if (status < 0) {    /*     * pcap_activate() failed.     */    cp = pcap_geterr(pd);    if (status == PCAP_ERROR)     error("%s", cp);    else if ((status == PCAP_ERROR_NO_SUCH_DEVICE ||              status == PCAP_ERROR_PERM_DENIED) &&             *cp != '\0')     error("%s: %s\n(%s)", device,         pcap_statustostr(status), cp);    else     error("%s: %s", device,         pcap_statustostr(status));   } else if (status > 0) {    /*     * pcap_activate() succeeded, but it's warning us     * of a problem it had.     */    cp = pcap_geterr(pd);    if (status == PCAP_WARNING)     warning("%s", cp);    else if (status == PCAP_WARNING_PROMISC_NOTSUP &&             *cp != '\0')     warning("%s: %s\n(%s)", device,         pcap_statustostr(status), cp);    else     warning("%s: %s", device,         pcap_statustostr(status));   }   //激活句柄

相關文章
相關標籤/搜索