#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. VS2012JSON自动生成对应的类

    一.复制JSON数据如图 {Key:"aaaa",Value:"bbbb"} 二.点击以下操作

  2. HTML5中的服务器‘推送’技术 -Server-Sent Events

    转帖:http://www.developersky.net/thread-63-1-1.html 一直以来,HTTP协议都是严格遵循Request-Response模型的.客户端发送一个Reques ...

  3. cocos2d-x中常见的场景切换

    本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3010779.html bool HelloWorld::init() { /// ...

  4. Oracle EBS-SQL (PO-2):检查当月到货补单的记录数.sql

    SELECT          DECODE(PLLA.FROM_LINE_ID,'-1','手工','','自动创建') 下达方式,          rsh.receipt_num         ...

  5. MFC中SQLite数据库的使用

    1打开数据库 BOOL playDlg::openData() { WCHAR a[100]; CString path; path = m_exePath+L"sentence_makin ...

  6. Mac OS X:Dock 的附加功能

    Dock 提供了可能并非显而易见的有用控件和菜单.通过修饰键的不同组合(如 Option 键.Control 键)及鼠标点按的不同类型(点按与按下及按住),即可使用下列附加功能.本文适用于 Mac O ...

  7. 简洁的jsp

    在开发 是使用tomcat7版本(7的jslt表达式语法检查更加严格) 1.去除生产html的不必要的空行 <%@ page trimDirectiveWhitespaces="tru ...

  8. Dynamics CRM 2013 初体验(5):Business Rule

    新系统中的Business Rule是个不错的功能,相信它的出现能减少大量的开发工作.在日常开发中,我们需要对记录做大量的业务控制.比如:某字段是否要隐藏,某字段的值是否符合要求以及现实提醒信息等.在 ...

  9. MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天

    ufn_GetWeekFirstAndEndDay    获取指定日期所在星期的第一天和最后一天日期 ALTER FUNCTION [dbo].[ufn_GetWeekFirstAndEndDay]( ...

  10. jquery validation plugin 使用

    <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Prope ...