Linux下编译安装libpcap

要做Sniffer了,先在Linux下编译一下libpcap

1、编译安装flex

#libpcap 1.1要求flex必须在2.4.6及以上
wget http://prdownloads.sourceforge.net/flex/flex-2.5.35.tar.gz?download
tar -xzvf flex-2.5.35.tar.gz
cd flex-2.5.35
./configure --prefix=/usr
make -j
sudo make install

2、编译安装bison

#libpcap要求同时安装flex赫bison
wget http://ftp.gnu.org/gnu/bison/bison-2.4.tar.gz
tar -xzvf bison-2.4.tar.gz
./configure --prefix=/usr
make -j && make install

3、编译安装libpcap

#下面进入正题,安装libpcap
wget http://www.tcpdump.org/release/libpcap-1.1.1.tar.gz
tar -xzvf libpcap-1.1.1.tar.gz
./configure --prefix=/usr
make -j
make install

4、测试程序
编译参数加入-lpcap

#include <stdio.h>
#include <pcap.h>

int main(int argc, char *argv[]) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* devs;
pcap_if_t* d;
unsigned int i = 0;

//获取全部的dev
if (-1 == pcap_findalldevs(&devs, errbuf)) {
fprintf(stderr, "Could not list device: %s\n", errbuf);
} else {
d = devs;
while (d->next != NULL) {
printf("%d:%s\n", i++, d->name);
d = d->next;
}
}

//释放所有获取的dev
pcap_freealldevs(devs);
return (0);
}

2 thoughts on “Linux下编译安装libpcap

  1. 清风迅来

    太感谢了…… 按照LZ的步骤,我终于把 libpcap 库装好了……也可能我之前是安装libpcap-1.2.1的缘故...网络安全编程作业终于可以自己做了= =

    PS:centos下面可能还要装yacc的……

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *