#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. beforefieldinit释义(3)

    1.看下面的例子: public static class MyClass<T> { public static readonly DateTime Time = GetNow(); pr ...

  2. Vim 扩展工具 vim-ide (转)

    通过简单的配置文件将 vim 打造成专业 ide,支持 mac linux cygwin.看过数篇 vim 配置文件,必要时去定制vim 的插件,将 vim 的 ide 用户体验尽量做到极致. 使用范 ...

  3. 一.ubuntu14.04安装、亮度设置、显卡设置等一体化讲解

    一.ubuntu14.04安装 安装步骤很简单的,相信你只要知道并且决定安装ubuntu,你就不会在安装上有问题,下载网址 http://www.ithome.com/html/soft/81539. ...

  4. 关于fsockopen pfsockopen函数被禁用的解决方法

    服务器同时禁用了fsockopen pfsockopen,那么用其他函数代替,如stream_socket_client().注意:stream_socket_client()和fsockopen() ...

  5. Web QQ自动强制加好友代码

    也许见过强行聊天的代码:  tencent://Message/?Uin=574201314&websiteName=www.oicqzone.com&Menu=yes 但是你应该不知 ...

  6. web安全记录

    前端 CSRF 跨站请求伪造 客户端添加伪随机数,后台验证 验证码 中间人攻击 SSL证书加密 xss(跨站脚本攻击)漏洞,微软的字符检验(自动) 文本展示编码处理 做标签展示的文本尤其过滤脚本 Co ...

  7. Github上的优秀安卓项目

    http://www.cnblogs.com/hawkon/p/3593709.html

  8. FreeRTOS 中断优先级嵌套错误引发HardFault异常解决

          最近在使用FreeRTOS的时候,突然发现程序在运行了几分钟之后所有的任务都不再调用了,只有几个中断能正常使用,看来是系统挂掉了,连续测试了几次想找出问题,可是这个真的有点不知所措.   ...

  9. Mirantis Fuel fundations

    Mirantis Nailgun is the most important service a RESTful application written in Python that contains ...

  10. e = e || window.event用法细节讨论

    e = e || window.event是我们在做事件处理时候区分IE和其他浏览器事件对象时常用的写法.但是这行兼容性代码有没有必要出现在所有的事件句柄中呢?标准事件调用方式需要这行代码吗?下边我们 ...