上篇文章中针对libpcap进行了理论总结,以及部分函数和结构的介绍,这里就整个数据包抓包由浅到深进行分析。
获取网络接口名字和掩码信息
#include
#include
#include
//in_addr头文件
int main(int argc,char **argv)
{
char error[PCAP_ERRBUF_SIZE];//#define PCAP_ERRBUF_SIZE 256
struct in_addr ip;
struct in_addr mask;
char *interface;
bpf_u_int32 net_ip;
bpf_u_int32 net_mask;
interface=pcap_lookupdev(error);//获取网络设备指针
if(interface==NULL)
{
printf("pcap_lookupdev error\n");
return -1;
}
printf("Network interface:%s\n",interface);
int ret=pcap_lookupnet(interface,&net_ip,&net_mask,error);//获取网络接口地址和掩码
if(ret<0)
{
printf("pcap_lookupnet error\n");
return -1;
}
ip.s_addr=net_ip;
mask.s_addr=net_mask;
printf("Interface ip:%s\n",inet_ntoa(ip));//地址输出
printf("Interface mask:%s\n",in