博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struct ip 和struct iphdr && struct icmp 和 struct icmphdr
阅读量:5735 次
发布时间:2019-06-18

本文共 3483 字,大约阅读时间需要 11 分钟。

1 struct ip { 2 #if BYTE_ORDER == LITTLE_ENDIAN  3     u_char  ip_hl:4,        /* header length */ 4         ip_v:4;         /* version */ 5 #endif 6 #if BYTE_ORDER == BIG_ENDIAN  7     u_char  ip_v:4,         /* version */ 8         ip_hl:4;        /* header length */ 9 #endif10     u_char  ip_tos;         /* type of service */11     short   ip_len;         /* total length */12     u_short ip_id;          /* identification */13     short   ip_off;         /* fragment offset field */14 #define IP_DF 0x4000            /* dont fragment flag */15 #define IP_MF 0x2000            /* more fragments flag */16     u_char  ip_ttl;         /* time to live */17     u_char  ip_p;           /* protocol */18     u_short ip_sum;         /* checksum */19     struct  in_addr ip_src,ip_dst;  /* source and dest address */20 };
1 struct iphdr { 2     #if defined(__LITTLE_ENDIAN_BITFIELD) 3         __u8    ihl:4, 4                 version:4; 5     #elif defined (__BIG_ENDIAN_BITFIELD) 6         __u8    version:4, 7                 ihl:4; 8     #else 9         #error  "Please fix 
"10 #endif11 __u8 tos;12 __u16 tot_len;13 __u16 id;14 __u16 frag_off;15 __u8 ttl;16 __u8 protocol;17 __u16 check;18 __u32 saddr;19 __u32 daddr;20 /*The options start here. */21 };
struct ip 和struct iphdr

struct ip and struct iphdr are two different definitions of the same underlying structure, brought in from different places.

struct ip is defined in <netinet/ip.h>, which is a reasonably standard header on UNIX systems.

struct iphdr is defined in <linux/ip.h>. This header (and structure) are Linux-specific, and will not be present in other operating systems.

If you're not sure which one to use, use struct ip; code which uses this structure is more likely to be portable to non-Linux systems.

struct icmp and struct icmphdr are a messier situation:

<netinet/icmp.h> defines both struct icmp and struct icmphdr.

<linux/icmp.h> also defines struct icmphdr, with a similar structure (but, as usual, different field names) as the definition from <netinet/icmp.h>.
First: Don't include <linux/icmp.h> unless you have a very good reason. You cannot include both headers -- they will conflict -- and most software will expect the netinet definition.

Second: struct icmphdr is, as the name implies, the header. struct icmp defines the contents of a structured ICMP message, like a destination unreachable message.

(2)<netinet/*.h> 和 <linux/*.h>

The linux/*.h headers were really meant for internal kernel use and if Linux were being created today, these files would not even exist under /usr/include. But early on, a lot of the userspace libc (libc4 and libc5 at the time) relied on Linux headers to define types, constants, structures, etc. for use in userspace, so netinet/in.h contained just #include <linux/in.h> or similar, and the lovely tradition got started. Today the only headers in the linux tree that should be used for userspace apps are some things related to supporting specific hardware at a low level, like the Linux console, framebuffer, video4linux, etc.

In short, you should use netinet/in.h (the standard header specified by POSIX) and pretend you never saw linux/in.h. :-)

---------------------
作者:zhongyoubing
来源:CSDN
原文:https://blog.csdn.net/zhongyoubing/article/details/77434719
版权声明:本文为博主原创文章,转载请附上博文链接!

 

转载于:https://www.cnblogs.com/ming-michelle/p/10147908.html

你可能感兴趣的文章
pandas 十分钟入门
查看>>
nginx rewrite
查看>>
前端安全系列(一):如何防止XSS攻击?
查看>>
IK分词器安装
查看>>
查看Linux并发连接数
查看>>
你是谁不重要,关键是你跟谁!
查看>>
CSS中规则@media的用法
查看>>
pychecker:分析你的python代码
查看>>
关于linux上安装网络打印机
查看>>
css 默认不显示 之后显示
查看>>
我的友情链接
查看>>
DNS显性+隐性URL转发原理
查看>>
我的友情链接
查看>>
网易有道 IP地址、手机号码归属地和身份证 查询接口API
查看>>
鼠标停留在GridView某一行时行的颜色改变
查看>>
系列3:WAS Liberty Profile hello mysql jdbc
查看>>
基础知识:python模块的导入
查看>>
Android MVC之我的实现
查看>>
我的友情链接
查看>>
我的友情链接
查看>>