资源简介
Linux 网络编程—— libpcap 详解,相关教程链接如下:
http://blog.csdn.net/tennysonsky/article/details/44811899

代码片段和文件信息
/*=========================================================================
工程名称: 01-libpcap-demo
组成文件: 01-libpcap-demo(接收一个数据包).c
功能描述: 通过捕获一个网络数据包,然后对其进行数据的解析分析
维护记录:
2015-04-02 v1.0 add by Mike
=========================================================================*/
#include
#include
#include
#include
#include
struct ether_header
{
unsigned char ether_dhost[6]; //目的mac
unsigned char ether_shost[6]; //源mac
unsigned short ether_type; //以太网类型
};
#define BUFSIZE 1514
/*=========================================================================
函数名称:int main(int argcchar *argv[])
功能描述:通过使用libpcap接收一个数据包,然后对数据包进行解析
参数传递:无
返回数据:无
维护记录:
2015-04-02 v1.0 add by Mike
=========================================================================*/
int main(int argcchar *argv[])
{
pcap_t * pcap_handle = NULL;
char error_content[100] = ““; // 出错信息
const unsigned char *p_packet_content = NULL; // 保存接收到的数据包的起始地址
unsigned char *p_mac_string = NULL; // 保存mac的地址,临时变量
unsigned short ethernet_type = 0; // 以太网类型
char *p_net_interface_name = NULL; // 接口名字
struct pcap_pkthdr protocol_header;
struct ether_header *ethernet_protocol;
//获得接口名
p_net_interface_name = pcap_lookupdev(error_content);
if(NULL == p_net_interface_name)
{
perror(“pcap_lookupdev“);
exit(-1);
}
//打开网络接口
pcap_handle = pcap_open_live(p_net_interface_nameBUFSIZE10error_content);
p_packet_content = pcap_next(pcap_handle&protocol_header);
printf(“------------------------------------------------------------------------\n“);
printf(“capture a Packet from p_net_interface_name :%s\n“p_net_interface_name);
printf(“Capture Time is :%s“ctime((const time_t *)&protocol_header.ts.tv_sec));
printf(“Packet Lenght is :%d\n“protocol_header.len);
/*
*分析以太网中的 源mac、目的mac
*/
ethernet_protocol = (struct ether_header *)p_packet_content;
p_mac_string = (unsigned char *)ethernet_protocol->ether_shost;//获取源mac
printf(“Mac Source Address is %02x:%02x:%02x:%02x:%02x:%02x\n“*(p_mac_string+0)*(p_mac_string+1)*(p_mac_string+2)*(p_mac_string+3)*(p_mac_string+4)*(p_mac_string+5));
p_mac_string = (unsigned char *)ethernet_protocol->ether_dhost;//获取目的mac
printf(“Mac Destination Address is %02x:%02x:%02x:%02x:%02x:%02x\n“*(p_mac_string+0)*(p_mac_string+1)*(p_mac_string+2)*(p_mac_string+3)*(p_mac_string+4)*(p_mac_string+5));
/*
*获得以太网的数据包的地址,然后分析出上层网络协议的类型
*/
ethernet_type = ntohs(ethernet_protocol->ether_type);
printf(“Ethernet type is :%04x\t“ethernet_type);
switch(ethernet_type)
{
case 0x0800:printf(“The network layer is IP protocol\n“);break;//ip
case 0x0806:printf(“The network layer is ARP protocol\n“);break;//arp
case 0x0835:printf(“The network layer is RARP protocol\n“);break;//rarp
default:printf(“The network layer unknow!\n“);break;
}
pcap_close(pcap_handle);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3400 2015-04-02 19:24 libpcap 详解源码\01-libpcap-demo(接收一个数据包).c
文件 3267 2015-04-02 19:28 libpcap 详解源码\02-libpcap-demo(接收多个数据包).c
文件 1301 2012-04-28 20:26 libpcap 详解源码\03-demo(设置过滤规则).c
目录 0 2015-04-02 20:07 libpcap 详解源码\
相关资源
- Windows异步套接字网络编程
- MFC网络编程实例
- IpHlpApi.h&IpHlpApi.lib
- SOCKET 网络编程 计算机网络 作业 客户
- VC 截获网络数据包.rar
- windows网络编程__罗莉琴__人民邮电出版
- 网络编程实用教程
- 网络编程socket文件图片等传送
- 网络编程实用教程第二版_各章的源程
- unix网络编程英文版第三版pdf
- Linux网络编程-网络基础-socket编程-高并
- UNIX网络编程卷1(第三版 英文版)
- 原始套接字Sniffer程序
- 用socket网页
- 网络编程实用教程(第二版)-源代码
- PiggyXP完成端口(CompletionPort)详解
- 网络调试助手-全平台
- socket网络编程服务端程序支持多客户
- Windows网络编程(第二版)
- UNIX网络编程 卷1(第三版 英文版)
- 网络课程设计购物网站
- Windows网络编程 带书签 第二版.pdf
- 网络编程实用教程.pdf
- socket网络编程,可以实现聊天系统,
- Linux网络编程+RouterOS_3.0中文教程+Rou
- 网络编程实用教程ppt+源码.rar
- TCP/IP高级编程
- UNIX Network Programming Volume 1 Third Editio
- 简单的聊天程序 网络编程 UDP TCP
- Windows网络编程第二版源码(补充材料
评论
共有 条评论