利用libpcap打印ip包
#include <stdio.h>
#include <pcap.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <iostream> using namespace std; //IP层数据包格式
typedef struct
{
int header_len:;
int version:;
u_char tos:;
int total_len:;
int ident:;
int flags:;
u_char ttl:;
u_char proto:;
int checksum:;
u_char sourceIP[];
u_char destIP[];
}IPHEADER; void callback(u_char* user,const struct pcap_pkthdr* header,const u_char* pkt_data); int main(int argc, char **argv)
{
char *device = NULL;
char errbuf[];
pcap_t *pcap;
int link_data; if((device = pcap_lookupdev(errbuf)) == NULL)
{
perror(errbuf);
return ;
} pcap = pcap_open_live(device, , , , errbuf);
if(pcap == NULL)
{
perror(errbuf);
return ;
} if((link_data = pcap_datalink(pcap)) == -)
{
fprintf(stderr,"pcap_link_data: %s\n",pcap_geterr(pcap));
return ;
} cout<<"开始抓包"<<endl;
pcap_loop(pcap, -, callback, NULL); return ;
} void callback(u_char* user,const struct pcap_pkthdr* header,const u_char* pkt_data)
{
cout<<"\t\t抓到一个包"<<endl;
cout<<"-------------------------------------------------"<<endl;
//解析数据包IP头部
if(header->len>=){
IPHEADER *ip_header=(IPHEADER*)(pkt_data+);
//解析协议类型
cout<<"|版本 "<<ip_header->version<<"|首部长度"<<ip_header->header_len*<<"字节|\t\t|"
"总长度"<<ip_header->total_len<<"字节|"<<endl;
cout<<"-------------------------------------------------"<<endl;
cout<<"|\t\t\t|\t|\t\t|"<<endl;
cout<<"-------------------------------------------------"<<endl;
cout<<"|ttl "<<int(ip_header->ttl)<<"\t|协议 ";
switch(ip_header->proto)
{
case :
cout<<"ICMP";
break;
case :
cout<<"IGMP";
break;
case :
cout<<"TCP ";
break;
case :
cout<<"UDP ";
break;
case :
cout<<"IPv6";
break;
default:
cout<<"IPv4";
}
cout<<"\t|首部校验和 "<<ip_header->checksum<<"\t|"<<endl;
cout<<"-------------------------------------------------"<<endl;
printf("|\t\t源地址 : %d.%d.%d.%d\t\t|\n",\
ip_header->sourceIP[],ip_header->sourceIP[],ip_header->sourceIP[],ip_header->sourceIP[]);
cout<<"-------------------------------------------------"<<endl;
printf("|\t\t目的地址 : %d.%d.%d.%d\t\t|\n",\
ip_header->destIP[],ip_header->destIP[],ip_header->destIP[],ip_header->destIP[]);
cout<<"-------------------------------------------------"<<endl;
cout<<endl;
}
}

利用libpcap打印ip包的更多相关文章
- 利用libpcap抓取数据包
转载自:http://blog.csdn.net/tennysonsky/article/details/44811899 概述 libpcap是一个网络数据包捕获函数库,tcpdump就是以libp ...
- Java网络编程:利用apache的HttpClient包进行http操作
本文介绍如何利用apache的HttpClient包进行http操作,包括get操作和post操作. 一.下面的代码是对HttpClient包的封装,以便于更好的编写应用代码. import java ...
- C# 利用SharpPcap实现网络包捕获嗅探
本文是利用SharpPcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享. 什么是SharpPcap? SharpPcap 是一个.NET 环境下的网络包捕获框架,基于 ...
- TCP 、UDP、IP包的最大长度
1.概述 首先要看TCP/IP协议,涉及到四层:链路层,网络层,传输层,应用层. 其中以太网(Ethernet)的数据帧在链路层 IP包在网络层 TCP或UDP包在传输层 TCP或UDP中的数据(Da ...
- 【Android】利用Fiddler进行抓包详解教程。抓取接口以及数据,可以抓真实安卓手机或者模拟器。
大家都知道抓包的方法很多.我这里给大家介绍介绍一种,利用fiddler进行抓包,当然比如Wireshark也可以抓包,我们这里不做介绍.我这里演示的是fiddler+天天模拟器,当然真实安卓手机也是一 ...
- requests利用selenium,代理Ip,云打码,验证码抠图操作 爬取搜狗微信公众号内容
爬取思路,爬取搜狗微信公众号内容,爬取第一层url时请求太快出现验证码,我这里用的蘑菇云代理,并在程序中我判断什么情况下是否+代理,做到合理运用代理ip.爬取第二层url时验证码出现次数更严重(和第一 ...
- iptables - IP包过滤器管理
总览 iptables -ADC 指定链的规则 [-A 添加 -D 删除 -C 修改] iptables - RI iptables -D chain rule num[option] iptable ...
- 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)
1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...
- IP包格式
网络层提供的服务就是在不同网段之间转发数据包. Ip包结构 1,格式(每行4byte*5) 2,版本 V4 V6 3,首部长度 20(固定)+可变长度 ,区分服务 Win2008开始:gpedit. ...
随机推荐
- bdb mvcc: buffer 何时可以被 看到; mvcc trans何时被移除
# txn.h struct __db_txnregion SH_TAILQ_HEAD(__active) active_txn; SH_TAILQ_HEAD(__mvcc) mvcc_txn; # ...
- 结对项目——高级四则运算检验器记录(168 & 187)
首先,上图(*+﹏+*)~@ 1.如何看待结对编程 结对编程优点: 1.两个人能够相互支持,相互监督,客服编程过程中可能出现的烦躁的情况0_0. 2.在开发功能的同时,伴随了UnitTest的进行,可 ...
- modal的使用
$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...
- Python成长笔记 - 基础篇 (十一)----RabbitMQ、Redis 、线程queue
本节内容: 1.RabbitMQ 消息队列 2.Redis 3.Mysql PY 中的线程queue(threading Queue):用于多个线程之间进行数据交换,不能在进程间进行通信 进程qu ...
- U盘的不识别问题
1.案例: 我重新装过电脑以后出现过一个问题是: 我的U盘只能在我电脑上用,在其他电脑上不能用 其他人的U盘不能在我的电脑上用. 2.根本问题: 是驱动问题.接口上没符合的驱动,未能指定路径.电脑上可 ...
- android 连接蓝牙扫码枪,程序崩溃之onConfigurationChanged
当android手机通过蓝牙连接扫码枪时,程序崩溃的原因之一是:键盘弹出或隐藏,触发程序走了onDestory->onCreate的生命周期,从而可能使得页面的某些初始化数据被清除了. 解决方法 ...
- 练习2-3:十六进制数字字符串转换为等价整型值,字符串允许包含的数字包括:0~9、a~f、A~F、x、X(C程序设计语言 第2版)
#include <stdio.h> #include <string.h> #include <math.h> int htoi(char s[]){ unsig ...
- html+css+javascript实现简易轮播图片
html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <lin ...
- DBCC 命令2
状态查询:收集和显示各类信息,状态检查. 如cachestats.pss.sqlmgrstats.memorystatus.proccache.freeproccache.freesystemcach ...
- Windows Server 2008 系统设置集合
1.禁用IPV6 netsh interface teredo set state disabled netsh interface 6to4 set state disabled netsh int ...