【转】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 进行测试。