在ubuntu 10.04下搭建libcap開發環境

來源:互聯網
上載者:User

【轉】http://hi.baidu.com/funnel_web_spider/blog/item/d86fa6651d3cb128ab184ca9.html

 

在linux下搭建libcap開發環境:
作業系統版本kubuntu 10.04
linux,核心版本2.6.32-22-generic
gcc版本:gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
libcap版本:libcap1.1.1 http://www.tcpdump.org/

1.安裝gcc g++編譯器 直接在終端執行sudo apt-get install build-essential
C 語言經典的入門例子是 *Hello World,下面是一範例程式碼:
#include <stdio.h>
  int main(void)
{
printf("Hello, world!/n");
return 0;
}
我們假定該代碼存為檔案‘hello.c’。
要用 編譯該檔案,使用下面的命令: $ gcc -Wall hello.c -o hello
該命令將檔案‘hello.c’中的代碼編譯為機器碼並儲存在可執行檔 ‘hello’中。
機器碼的檔案名稱是通過 選項指定的。該選項通常作為命令列中的最後一個參數。如果被省略,輸出檔案預設為 ‘a.out’。
如果目前的目錄中與可執行檔重名的檔案已經存在,它將被複蓋。 選項 開啟編譯器幾乎所有常用的警告──。 編譯器有很多其他的警告選項,但
是最常用的。預設情況下GCC 不會產生任何警告資訊。當編寫 C 或 C++ 程式時編譯器警告非常有助於檢測程式存在的問題。 本例中,編譯器使用了
選項而沒產生任何警告,因為樣本程式是完全合法的。
要運行該程式,輸入可執行檔的路徑如下: $ ./hello Hello, world!
這將可執行檔載入記憶體,並使 CPU 開始執行其包含的指令。 路徑 指代目前的目錄,因此 載入並執行目前的目錄下的可執行檔 ‘hello’。

2.安裝GNU M4 sudo apt-get install m4
這個是編譯flex必備的環境,否則會提示“GNU M4 1.4 is required”的錯誤

3. 安裝flex sudo apt-get install flex
沒有flex,直接安裝libpcap會提示“Your operating system's lex is insufficient to compile libpcap”錯誤。

4.編譯bison sudo apt-get install bison
在安裝flex後直接安裝libpcap會提示“don't have both flex and bison;reverting to lex/yacc”錯誤,前面安裝的是flex,就需要搭配bison

5.編譯libpcap
全面四步完成後,就可以使用下面三個指令安裝libpcap環境: 切換到libpcap目錄下(具體可查看libcap目  錄下官方提供的install文檔)
./configure
make
sudo make install

6. 運行 ldconfig,至此完成。

測試一下:

//simplesniffer.c

/* Simple Raw Sniffer                                                    */
/* Author: Luis Martin Garcia. luis.martingarcia [.at.] gmail [d0t] com  */
/* To compile: gcc simplesniffer.c -o simplesniffer -lpcap               */
/* Run as root!                                                          */
/*                                                                       */
/* This code is distributed under the GPL License. For more info check:  */
/* http://www.gnu.org/copyleft/gpl.html                                  */

#include <pcap.h>
#include <string.h>
#include <stdlib.h>

#define MAXBYTES2CAPTURE 2048

/* processPacket(): Callback function called by pcap_loop() everytime a packet */
/* arrives to the network card. This function prints the captured raw data in  */
/* hexadecimal.                                                                */
void processPacket(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char * packet){

 int i=0, *counter = (int *)arg;

 printf("Packet Count: %d/n", ++(*counter));
 printf("Received Packet Size: %d/n", pkthdr->len);
 printf("Payload:/n");
 for (i=0; i<pkthdr->len; i++){

    if ( isprint(packet[i]) ) /* If it is a printable character, print it */
        printf("%c ", packet[i]);
    else
        printf(". ");
   
     if( (i%16 == 0 && i!=0) || i==pkthdr->len-1 )
        printf("/n");
  }
 return;
}

/* main(): Main function. Opens network interface and calls pcap_loop() */
int main(int argc, char *argv[] ){
   
 int i=0, count=0;
 pcap_t *descr = NULL;
 char errbuf[PCAP_ERRBUF_SIZE], *device=NULL;
 memset(errbuf,0,PCAP_ERRBUF_SIZE);

 if( argc > 1){  /* If user supplied interface name, use it. */
    device = argv[1];
 }
 else{  /* Get the name of the first device suitable for capture */

    if ( (device = pcap_lookupdev(errbuf)) == NULL){
        fprintf(stderr, "ERROR: %s/n", errbuf);
        exit(1);
    }
 }

 printf("Opening device %s/n", device);
 
 /* Open device in promiscuous mode */
 if ( (descr = pcap_open_live(device, MAXBYTES2CAPTURE, 1,  512, errbuf)) == NULL){
    fprintf(stderr, "ERROR: %s/n", errbuf);
    exit(1);
 }

 /* Loop forever & call processPacket() for every received packet*/
 if ( pcap_loop(descr, -1, processPacket, (u_char *)&count) == -1){
    fprintf(stderr, "ERROR: %s/n", pcap_geterr(descr) );
    exit(1);
 }

return 0;

}

/* EOF*/

 

編譯 gcc -o simplesniffer simplesniffer.c -lpcap

然後運行 sudo ./simplesniffer 進行測試。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.