利用libpcap分析以太网协议例程

本文展示了如何在Fedora环境下使用libpcap库捕获并分析以太网协议,包括获取MAC地址、判断网络层协议类型(如IP、ARP、RARP)等。
摘要由CSDN通过智能技术生成

本代码编写环境:Fedora 17 gcc

编译命令 #gcc get_ethernet.c -o get_ethernet -lpcap

  1 #include <pcap.h>
  2 #define BUFSIZE 1024
  3 struct ether_header
  4 {
  5         u_int8_t ether_dhost[6];
  6         u_int8_t ether_shost[6];
  7         u_int16_t ether_type;
  8 };
  9 void main()
 10 {
 11         char err_content[PCAP_ERRBUF_SIZE];
 12         pcap_t* pcap_handle;
 13         const u_char* packet_content;
 14         u_char* mac_string;
 15         u_short ethernet_type;
 16         bpf_u_int32 net_mask;
 17         bpf_u_int32 net_ip;
 18         char* net_interface;
 19         struct pcap_pkthdr pcap_header;
 20         struct ether_header* ethernet_protocol;
 21         struct bpf_program bpf_filter;
 22         char bpf_filter_string[]="ip or arp or rarp";
 23         net_interface=pcap_loo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值