#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包的数据意义(简单实现例子)的更多相关文章

  1. TCP/IP协议栈与数据包封装+TCP与UDP区别

    ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用.如图2-1所示,是TCP/IP参考模型和OSI参考模型的对比示意图. T ...

  2. TCP/IP 数据包报文格式(IP包、TCP报头、UDP报头)(转)

    reference:http://blog.51cto.com/lyhbwwk/2162568                    https://blog.csdn.net/wangzhen209 ...

  3. 数据包报文格式(IP包、TCP报头、UDP报头)

    转自: https://blog.51cto.com/lyhbwwk/2162568 一.IP包格式 IP数据包是一种可变长分组,它由首部和数据负载两部分组成.首部长度一般为20-60字节(Byte) ...

  4. 以太网数据包、IP包、TCP/UDP 包的结构(转)

    源:以太网数据包.IP包.TCP/UDP 包的结构 版本号(Version):长度4比特.标识目前采用的IP协议的版本号.一般的值为0100(IPv4),0110(IPv6). IP包头长度(Head ...

  5. 以太网,IP,TCP,UDP数据包分析【转】

    原文地址:http://www.cnblogs.com/feitian629/archive/2012/11/16/2774065.html 1.ISO开放系统有以下几层: 7 应用层 6 表示层 5 ...

  6. 以太网,IP,TCP,UDP数据包分析(此文言简意赅,一遍看不懂的话,耐心的看个10遍就懂了,感谢作者无私奉献)

    1.ISO开放系统有以下几层: 7 应用层 6 表示层 5 会话层 4 传输层 3 网络层 2 数据链路层 1 物理层 2.TCP/IP 网络协议栈分为应用层(Application).传输层(Tra ...

  7. IP包格式

    网络层提供的服务就是在不同网段之间转发数据包. Ip包结构 1,格式(每行4byte*5) 2,版本 V4 V6 3,首部长度 20(固定)+可变长度 ,区分服务 Win2008开始:gpedit. ...

  8. 关于ip包长度

    http://blog.csdn.net/naturebe/article/details/6712153 这篇文章总结的不错,转自:http://hi.baidu.com/to_wait/blog/ ...

  9. [na]ip包格式

    网络层提供的服务就是在不同网段之间转发数据包. Ip包结构 1,格式(每行4byte*5) 2,版本 V4 V6 3,首部长度 20(固定)+可变长度 ,区分服务 Win2008开始:gpedit. ...

随机推荐

  1. JQuery EasyUI combobox动态添加option

    <input class="easyui-combobox" id="rwlb" name="rwlb" style="wi ...

  2. (Qt 翻译) QGLSceneNode

    #include <QGLSceneNode> QGLSceneNode ( QObject * parent = 0 ) QGLSceneNode ( const QGeometryDa ...

  3. jquery.session.js使用

    // jquery.session.js 简单使用方法 添加数据    $.session.set('key', 'value') 删除数据    $.session.remove('key'); 获 ...

  4. highcharts:根据Y的数值范围,动态改变图形的填充颜色

    图形实例: 源代码如下:   <!DOCTYPE html><html><head><meta charset="utf-8">&l ...

  5. PHP学习日记(一)——类、函数的使用

    一.自定义函数 function add($a,$b){ $c=$a+$b; echo 'add test:'; echo $c; return $c; } add(1,2); 输出结果: add t ...

  6. react native调试

    进入安卓终端 /usr/local/android-sdk-linux/platform-tools/adb shell 网络错误,模拟器不能连接主机,主要问题有2个: 移动端网络设置错误 服务没有启 ...

  7. struts2的对象工厂(ObjectFactory)

    ObjectFactory,是xwork中很重要的一个类,是产生action的地方.单独使用xwork的时候,action都是这个类创建的.struts2包装了ObjectFactory,自成了一个S ...

  8. rsyslog 读日志文件 ,当rsyslog 中断时,也会丢数据

    rsyslog 日志服务器: [root@dr-mysql01 winfae_log]# grep scan0819 wj-proxy01-catalina.out.2016-08-19 [root@ ...

  9. 使用 Node.js 做 Function Test

    Info 上周 meeting 上同事说他们现在在用 java 写 function test,产生了很多冗余的代码,整个项目也变得比较臃肿.现在迫切需要个简单的模板项目能快速搭建function t ...

  10. 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys

    很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...