#include <fstream>
#include <iostream>
#include <iomanip>           //I/O流控制头文件
#include <WINSOCK2.H>
using namespace std;
struct eth_header            //帧头
{
unsigned char eth_dst[6];
unsigned char eth_src[6];
unsigned short upper_pro; //上层协议    0800表示是ip数据包
};
struct ip_header              //ip报头
{
union
{
unsigned char version;  //版本
unsigned char ihl;      //报头长度
};
unsigned char tos;          //服务类型
unsigned short total_len;   //总长度
unsigned short identifer;   //标识
union
{
unsigned short flags;    //标志
unsigned short offset;   //片偏移
};
unsigned char TTL;           //生存周期
unsigned char protocol;      //协议
unsigned short checksum;     //头部校验和
unsigned char ip_src[4];     //源ip地址
unsigned char ip_dst[4];     //目的ip地址
unsigned int option;         //选项+填充
//数据
};
void main()
{
int temp[38];               //? why 38?
ifstream infile("D:\\data"); //打开文件
int i;
for (i=0;i<38;i++)
{
infile>>hex>>temp[i];    //hex 格式化为十六进制数值数据 输入和输出
}
infile.close();
struct eth_header eth;
struct ip_header ip;
for (i=0;i<6;i++)
{
eth.eth_dst[i]=(unsigned char)temp[i];
}
for (i=6;i<12;i++)
{
eth.eth_src[i-6]=(unsigned char)temp[i];
}
eth.upper_pro = (temp[12]<<8)|temp[13];         //? why 左移8位
//以下给ip头部变量赋值
ip.version = (unsigned char)temp[14];
ip.tos = (unsigned char)temp[15];
ip.total_len=(temp[16]<<8)|temp[17];
ip.identifer = (temp[18]<<8)|temp[19];
ip.flags = (unsigned short)(temp[20]<<8)|(temp[21]);
ip.TTL = (unsigned char)temp[22];
ip.protocol = (unsigned char)temp[23];
ip.checksum = (temp[24]<<8)|temp[25];
for (i=26;i<30;i++)
{
ip.ip_src[i-26]=(unsigned char)temp[i];
}
for (i=30;i<34;i++)
{
ip.ip_dst[i-30]=(unsigned char)temp[i];
}
//以下输出mac头部信息
cout<<"输出mac帧头部信息(十六进制):"<<endl;
cout<<"目的mac地址为:";
for (i=0;i<5;i++)
{
//setfill是设置填充字符,setw设置输出的宽度,它们的作用表现在紧接着输入的字符串上
cout<<setfill('0')<<setw(2)<<hex<<(int)eth.eth_dst[i]<<"-";//空的地方填充0,宽度为2
}
cout<<setfill('0')<<setw(2)<<hex<<(int)eth.eth_dst[5]<<endl;
cout<<"源mac地址为:";
for (i=0;i<5;i++)
{
cout<<setfill('0')<<setw(2)<<hex<<(int)eth.eth_src[i]<<"-";
}
cout<<setfill('0')<<setw(2)<<hex<<(int)eth.eth_src[5]<<endl;
cout<<"上层协议类型为:";
cout<<setfill('0')<<setw(4)<<hex<<eth.upper_pro<<endl;
cout<<"-----------------------------"<<endl;
//以下输出ip头部信息
cout<<"输出ip头部信息(十进制):"<<endl;
cout<<"版本:"<<dec<<(ip.version>>4)<<endl;    //dec十进制 oct八进制 hex十六进制
ip.ihl=ip.version&0x0f;
cout<<"ip头部长度(ihl):"<<(ip.ihl*4)<<endl;
cout<<"服务类型:"<<(int)ip.tos<<"("<<"优先权子字段"
<<(ip.tos>>5)<<"tos子字段:"<<((ip.tos>>1)&0x0f)<<")"<<endl;
cout<<"总长度:"<<(int)ip.total_len<<endl;
cout<<"标识符:"<<(int)ip.identifer<<endl;
cout<<"片偏移:"<<(ip.offset&0x1fff)<<endl;
cout<<"生存期:"<<(int)ip.TTL<<endl;
cout<<"协议:"<<(int)ip.protocol<<endl;
cout<<"首部检校和:"<<(int)ip.checksum<<endl;
cout<<"源ip地址:";
for(i=0;i<3;i++)
{
cout<<(int)ip.ip_src[i]<<".";
}
cout<<(int)ip.ip_src[3]<<endl;
cout<<"目的ip地址:";
for (i=0;i<3;i++)
{
cout<<(int)ip.ip_dst[i]<<".";
}
cout<<(int)ip.ip_dst[3]<<endl;
//以下验证有没有选项字段
if (ip.ihl==5)
{
cout<<"没有选项字段"<<endl;
for (i=34;i<38;i++)
{
temp[i]=0;
}
}
else
{
ip.option=(unsigned int)(temp[34]<<24)|(temp[35]<<16)|(temp[36]<<8)|temp[37];
cout<<"选项:"<<(int)ip.option<<endl;//如果有选项字段 将选项和填充一起输出
}
cout<<"-----------------------------"<<endl;
//以下验证ip数据包头部的正确性
unsigned short m1,m2,sum;
unsigned int sum1=0;
for (i=14;i<38;i=i+2)
{
m1 = (unsigned short)(temp[i]<<8);
m2 = (unsigned short)temp[i+1];
sum = m1|m2;
sum1+=sum;
}
sum1 = (sum1>>16)+(sum1&0xffff);   //oxffff截掉进位那一位
sum1 = (sum1>>16)+(sum1&0xffff);
sum1 = (unsigned short)~sum1;
if (sum1==0)
{
cout<<"ip数据包是正确的"<<endl;
}
else
{
cout<<"ip数据包是错误的"<<endl;
}
cout<<"-----------------------------"<<endl;
cin.get();
}

读取网络抓包文件data得到参数相关推荐

  1. 网络抓包方式复现Tomcat- AJP协议文件读取/命令执行漏洞(CVE-2020-1938 / CNVD-2020-10487)

    目录 测试是否安装成功​编辑 基础简介 Tomcat Connector(连接器) ​编辑Servlet(服务程序) Tomcat内部处理请求流程 文件读取漏洞 抓包复现 需要将下图中抓取到的数据包修 ...

  2. 【学习点滴】linux调试工具、cmake和网络抓包

    目录 gdb 多进程调试 多线程调试: gdb底层原理 Linux下查看服务器端的并发连接个数: Valgrind memcheck strace Linux下,绑定1024以下的端口需要root权限 ...

  3. iOS系统网络抓包方法

    转自:http://www.cnblogs.com/ydhliphonedev/archive/2011/10/27/2226935.html 在进行iOS开发过程中,经常会遇到各种各样的网络访问问题 ...

  4. 如何在 Kubernetes Pod 内进行网络抓包

    作者 | Addo Zhang 来源 | 云原生指北 使用 Kubernetes 时,经常会遇到一些棘手的网络问题需要对 Pod 内的流量进行抓包分析.然而所使用的镜像一般不会带有 tcpdump 命 ...

  5. 超详细的网络抓包神器 tcpdump 使用指南

    tcpdump 是一款强大的网络抓包工具,它使用 libpcap 库来抓取网络数据包,这个库在几乎在所有的 Linux/Unix 中都有.熟悉 tcpdump 的使用能够帮助你分析调试网络数据,本文将 ...

  6. 关于fi dd ler 手机抓包 网卡地址地址_超详细的网络抓包神器 tcpdump 使用指南

    tcpdump 是一款强大的网络抓包工具,它使用 libpcap 库来抓取网络数据包,这个库在几乎在所有的 Linux/Unix 中都有.熟悉 tcpdump 的使用能够帮助你分析调试网络数据,本文将 ...

  7. 查看docker与宿主机网卡的对应关系,tcpdump在容器外网络抓包

    通过iflink找到网卡对应关系 1. 在宿主机上执行命令ip link $ ip link ...... 13: veth56ecf40@if12: <BROADCAST,MULTICAST, ...

  8. 网络抓包技术:libcap

    简介 Linux 下几乎所有的抓包技术都是基于libcap的,包括可以兼容DPDK 的pcaplusplus. libpcap 是一个网络数据包捕获函数库,功能非常强大,Linux 下著名的 tcpd ...

  9. C++ winpcap网络抓包代码实现,以及抓包内容解析。

    c++实现抓包代码 1.安装winpcap(windows packet capture)是windows平台下一个免费,公共的网络访问系统.开发winpcap这个项目的目的在于为win32应用程序提 ...

最新文章

  1. Redis、Redis+sentinel安装(Ubuntu 14.04下Redis安装及简单测试)
  2. CENTOS安装ElasticSearch
  3. springboot jar服务器运行后无法请求_Spring Boot微服务中Chaos Monkey的应用
  4. python自动化控制运动_Python +selenium自动化帮你预订运动场地
  5. ltp-ddt realtime_cpu_load涉及的cyclictest 交叉编译
  6. DOM对象和jquery对象相互转换
  7. 百面机器学习—3.逻辑回归与决策树要点总结
  8. latex max下面标注怎么写
  9. Atitit.远程接口 监控与木马   常用的api 标准化v2 q216
  10. Centos操作系统
  11. C6000 DSP技术深度探索(1)---关于启动方式
  12. 金蝶K3 SQL报表系列-BOM成本明细表
  13. 【如何在12306网站上购买上中下卧铺火车票呢?】
  14. MFC中使用SOUI的窗口示例
  15. 计算机远程控制安全吗,远程控制电脑危险吗?向日葵远程控制软件安全吗?
  16. costmap_2d 中计算footprint 的内切圆半径和外切圆半径的函数解析——点到线段的距离计算
  17. TSINGSEE青犀视频云-边-端架构视频智能分析平台EasyNVR如何实现直播地址和录像地址统一
  18. 怎么设置360主页的html,360安全浏览器怎么设置主页 设置主页原来这么简单
  19. ServU 5.0的配置
  20. 做开发的你需要了解:Serverless 可观测性的过去、现在与未来

热门文章

  1. 我所理解的性能测试是什么?
  2. 关联规则java apriori_关联规则算法(The Apriori algorithm)详解
  3. 一个上帝视角的java游戏_从上帝视角看Java如何运行
  4. 最详细,快速入门Web前端开发的正确姿势
  5. mysql一张表可以用吗_MySQL表操作
  6. c html循环ienumerable,C#方法返回IEnumerable实现用foreach遍历
  7. 怎么看电脑系统是win几_做设计,用win系统 还是苹果MAC电脑更好 (PS举例)
  8. flutter列表无法滚动到底部_在闲鱼实习做Flutter是什么感受?
  9. STM8单片机 ADC模拟看门狗中文资料错误
  10. JSP九大内置对象四大作用域