c语言实现ip协议_C语言嗅探器带报告

关键代码
通过C语言,实现了一个网络的嗅探器的基本功能。可以做到能检测计算机中的所有网卡,实现了网卡的选择并打开混杂模式。监听网络中所有的数据包,并解析出数据包是使用的协议为哪种,以及每种协议首部的各种字段,如源IP地址、目的IP地址、源端口、目的端口、源MAC地址、目的MAC地址、数据包的长度等。同时能解析出数据包数据部分的内容,并尽可能的以可读的方式输出。此外,还可以选择要监听的数据包协议的类型,在监听的过程中可以检测到 ping 命令等。
下载链接:https://download.csdn.net/download/RONNIE_Zz/12652059

43a254f685f5dd0e5c43a21e479d7a77.png
a48658bc1f6d81e7050eda18dc8f0081.png
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data){ //回调函数,当收到每一个数据包时会被libpcap所调用if(header->caplen>400) return;    struct tm *ltime;    char timestr[16];ip_header * ip_hd;udp_header * udp_hd;tcp_header * tcp_hd;    ethe_header * ethe_hd;int ip_len,tcp_len,start;u_short sport,dport;printf("");    ltime=localtime(&header->ts.tv_sec); //将时间戳转换为可读字符     strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);    printf("时间:%s",timestr);        ethe_hd = (ethe_header *)pkt_data;    ip_hd = (ip_header *)(pkt_data + 14);ip_len = (ip_hd ->ver_ihl & 0xf) * 4; //ip首部长度 udp_hd = (udp_header *)((u_char *)ip_hd + ip_len);sport = ntohs(udp_hd->sport);dport = ntohs(udp_hd->dport);if(ip_hd->proto==17){printf("协议:UDP");start=ip_len+8; }else if(ip_hd->proto==6){printf("协议:TCP");tcp_hd = (tcp_header *)((u_char *)ip_hd + ip_len);tcp_len=ntohs(tcp_hd->sum)>>12;start=ip_len+tcp_len*4;}else if(ip_hd->proto==1){printf("协议:ICMP");start=ip_len+23;}else printf("协议:其他");//printf("start=%d",start);printf("                      数据报的长度:%d",header->caplen);    printf("源IP地址: %d.%d.%d.%d:%d      目的IP地址:%d.%d.%d.%d:%d源端口:%d                     目的端口:%d源物理地址: %x-%x-%x-%x-%x-%x   目的物理地址:%x-%x-%x-%x-%x-%x",  ip_hd->saddr.b1, ip_hd->saddr.b2, ip_hd->saddr.b3, ip_hd->saddr.b4,            ip_hd->daddr.b1, ip_hd->daddr.b2, ip_hd->daddr.b3, ip_hd->daddr.b4, sport, dport,   ethe_hd->mac_source_address.b1, ethe_hd->mac_source_address.b2, ethe_hd->mac_source_address.b3,   ethe_hd->mac_source_address.b4, ethe_hd->mac_source_address.b5, ethe_hd->mac_source_address.b6,   ethe_hd->mac_dest_address.b1, ethe_hd->mac_dest_address.b2, ethe_hd->mac_dest_address.b3,   ethe_hd->mac_dest_address.b4, ethe_hd->mac_dest_address.b5, ethe_hd->mac_dest_address.b6);//输出数据部分    printf("数据部分内容为:");    for (int i=start; (i < header->caplen + 1 ) ; i++)    {        printf("%.2x ", pkt_data[i-1]);        if ( (i % LINE_LEN) == 0) printf("");    }    printf("");}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值