得到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. ...
随机推荐
- .net 生成缩略图
public static void CreateSmallImage(string minImageFullPath, System.Drawing.Image originalImage, int ...
- aspnetpager分页,不使用存储过程
一.前台显示界面代码Default.aspx(注意,代码运行环境是VS.2005) <%@ Page Language="C#" AutoEventWireup=" ...
- ckplayer 网页视频播放
最近做文件上传,可以上传的资源有图片,也有视频,在显示列表中点击视频播放图片(图1),需要弹出播放窗口(图2),播放视频.具体效果画面如下: 图1 图2:播放窗口 实现该功能使用的是ckplayer播 ...
- Android 尺寸 神图
- 关于Eclipse的编码配置和字体大小设置
编码设置 1.工作空间编码:Window-->Preferences-->General-->Workspace 2.工程文件编码:项目-->Properties-->R ...
- Composer加速
在composer.json中添加{ "repositories": [ {"type": "composer", "url&qu ...
- Linux03--文件打包与解压
参考了<鸟哥的Linux私房菜> 1.压缩命令 gzip(压缩)与zcat(解压并读出来) gzip 可以说是应用度最广的压缩命令了!目前 gzip 可以解开 compress, zip ...
- ASP.NET自定义控件加载资源WebResource问题
最近项目用日期控件,想把My97的资源文件跟TextBox封装成一个DatePicker控件,其实很简单的意见事情,但是还是用了一天多的时间,主要的问题就是解决资源文件加载的问题.通过一天多的努力,得 ...
- top k 算法
对于一个非有序的数组A[p..r],求数组中第k小的元素. 如何考虑 排序(部分排序)就不用说了..o(nlgn),当然如果在实际情况中要一直取值,当然要排序后,一次搞定,以后都是O(1) 我们这里提 ...
- error: ld returned 1 exit status 和 error:undefined reference
undefined reference 往往是链接时出现错误,无法解析引用.这篇文章总结的很好undefined reference问题总结 error: ld returned 1 exit sta ...