[Network]网络协议结构体及文件

70 篇文章 3 订阅
14 篇文章 0 订阅
    本文,集中汇集了各个协议头部,通过理解头部,可以帮助理解网络协议的组成,或者应用开发等。

    然而,一开始学习的时候,发现在/usr/include目录里面,子目录Linux和netinet都分别有定义网络协议的相关文件。后来:whats-the-practical-difference-between-the-headers-linux-in-h-and-netinet-in, 链接
结论:
    Linux子目录是给 内核开发 使用,而我们一般 应用开发 是使用netinet目录下的头文件。

    为什么要写这篇文章?上面已经给出了一部分原因了,还有就是,作为嵌入式工程师或者是网络工程师,我确实感受到了网络的需求,有一次实现多网卡切换,就需要修改MAC帧结构。

    Linux网络结构体与定义文件, 链接:帮助搜索文件位置

顺序
  1. MAC帧
  2. ARP协议结构
  3. IP帧
  4. ICMP协议结构
  5. UDP帧
  6. TCP帧


MAC帧
net/ethernet.h

/* This is a name for the 48 bit ethernet address available on many systems.  */
struct ether_addr
{
  u_int8_t ether_addr_octet[ETH_ALEN];
} __attribute__ ((__packed__));

struct ether_header
{
  u_int8_t  ether_dhost[ETH_ALEN];    /* destination eth addr    */
  u_int8_t  ether_shost[ETH_ALEN];    /* source ether addr    */
  u_int16_t ether_type;                /* packet type ID field    */
} __attribute__ ((__packed__));


ARP协议结构
net/if_ether.h

/*
* Ethernet Address Resolution Protocol.
*
* See RFC 826 for protocol description.  Structure below is adapted
* to resolving internet addresses.  Field names used correspond to
* RFC 826.
*/
struct    ether_arp {
    struct    arphdr ea_hdr;        /* fixed-size header */
    u_int8_t arp_sha[ETH_ALEN];    /* sender hardware address */
    u_int8_t arp_spa[4];        /* sender protocol address */
    u_int8_t arp_tha[ETH_ALEN];    /* target hardware address */
    u_int8_t arp_tpa[4];        /* target protocol address */
};
理解:我是IPxxx,我的硬件地址是MACxxx,我想知道IPyyy的硬件地址MACyyy


IP帧
netinet/ip.h

struct iphdr
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ihl:4;
    unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
    unsigned int version:4;
    unsigned int ihl:4;
#else
# error    "Please fix <bits/endian.h>"
#endif
    u_int8_t tos;
    u_int16_t tot_len;
    u_int16_t id;
    u_int16_t frag_off;
    u_int8_t ttl;
    u_int8_t protocol;
    u_int16_t check;
    u_int32_t saddr;
    u_int32_t daddr;
    /*The options start here. */
  };


ICMP协议结构
netinet/ip_icmp.h

struct icmphdr
{
  u_int8_t type;        /* message type */
  u_int8_t code;        /* type sub-code */
  u_int16_t checksum;
  union
  {
    struct
    {
      u_int16_t    id;
      u_int16_t    sequence;
    } echo;            /* echo datagram */
    u_int32_t    gateway;    /* gateway address */
    struct
    {
      u_int16_t    __glibc_reserved;
      u_int16_t    mtu;
    } frag;            /* path mtu discovery */
  } un;
};


UDP帧
netinet/udp.h

/* UDP header as specified by RFC 768, August 1980. */
struct udphdr
{
  __extension__ union
  {
    struct
    {
      u_int16_t uh_sport;        /* source port */
      u_int16_t uh_dport;        /* destination port */
      u_int16_t uh_ulen;        /* udp length */
      u_int16_t uh_sum;        /* udp checksum */
    };
    struct
    {
      u_int16_t source;
      u_int16_t dest;
      u_int16_t len;
      u_int16_t check;
    };
  };
};


TCP帧
netinet/tcp.h

typedef    u_int32_t tcp_seq;
/*
* TCP header.
* Per RFC 793, September, 1981.
*/
struct tcphdr
  {
    __extension__ union
    {
      struct
      {
    u_int16_t th_sport;        /* source port */
    u_int16_t th_dport;        /* destination port */
    tcp_seq th_seq;        /* sequence number */
    tcp_seq th_ack;        /* acknowledgement number */
# if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int8_t th_x2:4;        /* (unused) */
    u_int8_t th_off:4;        /* data offset */
# endif
# if __BYTE_ORDER == __BIG_ENDIAN
    u_int8_t th_off:4;        /* data offset */
    u_int8_t th_x2:4;        /* (unused) */
# endif
    u_int8_t th_flags;
# define TH_FIN    0x01
# define TH_SYN    0x02
# define TH_RST    0x04
# define TH_PUSH    0x08
# define TH_ACK    0x10
# define TH_URG    0x20
    u_int16_t th_win;        /* window */
    u_int16_t th_sum;        /* checksum */
    u_int16_t th_urp;        /* urgent pointer */
      };
      struct
      {
    u_int16_t source;
    u_int16_t dest;
    u_int32_t seq;
    u_int32_t ack_seq;
# if __BYTE_ORDER == __LITTLE_ENDIAN
    u_int16_t res1:4;
    u_int16_t doff:4;
    u_int16_t fin:1;
    u_int16_t syn:1;
    u_int16_t rst:1;
    u_int16_t psh:1;
    u_int16_t ack:1;
    u_int16_t urg:1;
    u_int16_t res2:2;
# elif __BYTE_ORDER == __BIG_ENDIAN
    u_int16_t doff:4;
    u_int16_t res1:4;
    u_int16_t res2:2;
    u_int16_t urg:1;
    u_int16_t ack:1;
    u_int16_t psh:1;
    u_int16_t rst:1;
    u_int16_t syn:1;
    u_int16_t fin:1;
# else
#  error "Adjust your <bits/endian.h> defines"
# endif
    u_int16_t window;
    u_int16_t check;
    u_int16_t urg_ptr;
      };
    };
};



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值