得到IP包的数据意义(简单实现例子)
#include <stdio.h>
#include <unistd.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include <linux/types.h> void show_mac(const unsigned char *data);
void show_ip(const unsigned char *data);
void show_arp(const unsigned char *data);
void show_udp(const unsigned char *data);
void show_tcp(const unsigned char *data);
void show_app(const unsigned char *data); int main()
{
unsigned char data[] = {
0x00, 0x26, 0xc6, 0x41, 0x06, 0xb2, 0x00, 0x26,
0xc6, 0x39, 0x8c, 0x36, 0x08, 0x00, 0x45, 0x00,
0x00, 0x20, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11,
0x7a, 0x90, 0xc0, 0xa8, 0x1f, 0x72, 0xc0, 0xa8,
0x1f, 0x7a, 0x94, 0x2b, 0x25, 0x37, 0x00, 0x0c,
0xa0, 0x6d, 0x77, 0x61, 0x6e, 0x67
}; show_mac(data); } void show_mac(const unsigned char *data)
{
struct ethhdr *eth = (struct ethhdr *)data;
printf("-------------物理层-------------\n");
printf("目的MAC地址: %02x:%02x:%02x:%02x:%02x:%02x\n",
eth->h_dest[], eth->h_dest[],
eth->h_dest[], eth->h_dest[],
eth->h_dest[], eth->h_dest[]
);
printf("源端MAC地址: %02x:%02x:%02x:%02x:%02x:%02x\n",
eth->h_source[], eth->h_source[],
eth->h_source[], eth->h_source[],
eth->h_source[], eth->h_source[]
);
printf("使用的协议: %04x\n\n", ntohs(eth->h_proto));
if(ntohs(eth->h_proto) == 0x0800)
show_ip(data);
if(ntohs(eth->h_proto) == 0x0806)
show_arp(data);
}
void show_ip(const unsigned char *data)
{
printf("-------------网络层-------------\n"); struct iphdr *ip = (struct iphdr *)(data + sizeof(struct ethhdr));
printf("版本号: %d\n", ip->version);
printf("IP首部长度: %d\n", (ip->ihl)*);
printf("服务类型: %d\n", ip->tos);
printf("总长度: %d\n", ntohs(ip->tot_len));
printf("标识: %d\n", ip->id);
printf("片偏移: %d\n", ip->frag_off);
printf("生存时间: %d\n", ip->ttl);
printf("上层使用协议: %d\n", ip->protocol);
printf("首部检验和: %d\n", ip->check);
printf("源IP地址: %s\n", inet_ntoa(ip->saddr));
printf("目的IP地址: %s\n\n", inet_ntoa(ip->daddr));
if(ip->protocol == )
show_tcp(data);
if(ip->protocol == )
show_udp(data);
}
void show_arp(const unsigned char *data)
{ }
void show_udp(const unsigned char *data)
{
printf("-------------传输层-------------\n");
struct udphdr *udp = (struct udphdr *)(data + sizeof(struct ethhdr)+ sizeof(struct iphdr));
printf("源端口号: %d\n", htons(udp->source));
printf("目的端口号: %d\n", htons(udp->dest));
printf("UDP长度: %d\n", htons(udp->len));
printf("UDP检验和: %x\n\n", htons(udp->check));
show_app(data);
}
void show_tcp(const unsigned char *data)
{ }
void show_app(const unsigned char *data)
{
printf("-------------应用层-------------\n");
char *p = (char *)(data + sizeof(struct ethhdr)+ sizeof(struct iphdr)+sizeof(struct udphdr));
printf("数据:%s\n\n", p); }
得到IP包的数据意义(简单实现例子)的更多相关文章
- TCP/IP协议栈与数据包封装+TCP与UDP区别
ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用.如图2-1所示,是TCP/IP参考模型和OSI参考模型的对比示意图. T ...
- TCP/IP 数据包报文格式(IP包、TCP报头、UDP报头)(转)
reference:http://blog.51cto.com/lyhbwwk/2162568 https://blog.csdn.net/wangzhen209 ...
- 数据包报文格式(IP包、TCP报头、UDP报头)
转自: https://blog.51cto.com/lyhbwwk/2162568 一.IP包格式 IP数据包是一种可变长分组,它由首部和数据负载两部分组成.首部长度一般为20-60字节(Byte) ...
- 以太网数据包、IP包、TCP/UDP 包的结构(转)
源:以太网数据包.IP包.TCP/UDP 包的结构 版本号(Version):长度4比特.标识目前采用的IP协议的版本号.一般的值为0100(IPv4),0110(IPv6). IP包头长度(Head ...
- 以太网,IP,TCP,UDP数据包分析【转】
原文地址:http://www.cnblogs.com/feitian629/archive/2012/11/16/2774065.html 1.ISO开放系统有以下几层: 7 应用层 6 表示层 5 ...
- 以太网,IP,TCP,UDP数据包分析(此文言简意赅,一遍看不懂的话,耐心的看个10遍就懂了,感谢作者无私奉献)
1.ISO开放系统有以下几层: 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 2.TCP/IP 网络协议栈分为应用层(Application).传输层(Tra ...
- IP包格式
网络层提供的服务就是在不同网段之间转发数据包. Ip包结构 1,格式(每行4byte*5) 2,版本 V4 V6 3,首部长度 20(固定)+可变长度 ,区分服务 Win2008开始:gpedit. ...
- 关于ip包长度
http://blog.csdn.net/naturebe/article/details/6712153 这篇文章总结的不错,转自:http://hi.baidu.com/to_wait/blog/ ...
- [na]ip包格式
网络层提供的服务就是在不同网段之间转发数据包. Ip包结构 1,格式(每行4byte*5) 2,版本 V4 V6 3,首部长度 20(固定)+可变长度 ,区分服务 Win2008开始:gpedit. ...
随机推荐
- eclipse 各种版本spring插件安装
一.安装之前先得知道怎么下载吧: 一般网上找到的资料是到这里就GG了的 --> http://spring.io/tools/sts/all 很明显这点版本是不够我们需要的,假如我们的eclip ...
- There is no satiety in study
好不容易考上了硕士.这个时候,才终于明白什么叫做学无止境.用了1周linux,发现需要学习的东西太多了.life is too short to learn c plus plus 果然如此.不过我们 ...
- linux下的gdb调试工具--内存调试
接着上一节的代码,在while(1)的循环里面增加代码:sum=0 #include <stdio.h> int main(void) { int sum = 0, i = 0; char ...
- Linux编程C/C++
C/C++基本数据类型 C/C++语言有一组基本数据类型,对应于计算机的基本存储单元和使用这些单元去保存数据的一些常用方式. 基本数据类型如下: 上面表格中的类型是基本的C/C++数据类型,但是在C+ ...
- Node.js内置的工具和第三方模块来进行单步调试
1.命令行调试: Node.js调试命令: run 执行脚本,在第一行暂停 restart 重新执行脚本 cont,c 继续执行,知道遇到下一个断点 next,n 单步执行 step,s 单步执行,并 ...
- sqlmap的详细使用
各项参数讲的太仔细了,对于只会几个参数的我来说,太受用了. 先mark下 http://drops.wooyun.org/tips/143 http://drops.wooyun.org/tips/4 ...
- python循环,判断及函数
python中的for循环 #for循环格式(类似Java中的foreach):for 标识符 in 列表名称 : >>> movies = ["movie1", ...
- Android系统移植与调试之------->如何修改Android设备的开机第一阶段Logo
1.切换到~/mx0831-0525/device/other/TBDG1073/res_pack目录下 2.更换bootup和poweron文件 找一张bmp16位的图片去除后缀名将这两张都替换,转 ...
- 飘逸的python - 两种with语句实现方法
第一种是实现上下文管理器协议,即魔法方法__enter__和__exit__. class Foo: def __enter__(self): print 'in' def __exit__(self ...
- ACM/ICPC2014鞍山现场赛E hdu5074Hatsune Miku
题目链接:pid=5074">http://acm.hdu.edu.cn/showproblem.php?pid=5074 题意: 给定一个m*m的矩阵mp.然后给定一个长度为n的序列 ...