Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2373660
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: LINUX

2011-04-06 22:20:33

14)网络编程常用结构

 

注:所以文章红色字体代表需要特别注意和有问题还未解决的地方,蓝色字体表示需要注意的地方

 

1.     本文所介绍的程序平台

开发板:arm9-mini2440

虚拟机为:Red Hat Enterprise Linux 5

开发板上系统内核版本:linux-2.6.32.2

 

struct sockaddr

 

{

u_short sa_family; //地址族,采用"AF_xxx"的形式,如:AF_INET

char sa_data[14]; //14字节的特定协议地址。

 

}

 

struct sockaddr_in

 

{

short int sin_family;  /* Internet地址族 */

unsigned short int sin_port;  /* 端口号 */

struct in_addr sin_addr;   /* IP地址 */

unsigned char sin_zero[8];  /* 填0 */

 

}

编程中一般并不直接针对sockaddr数据结构操作,而是使用与sockaddr等价的sockaddr_in数据结构

   struct hostent{

   char *h_name;                    /* official domain name of host */

   char **h_aliases;                /* null-terminated array of domain names */

   int h_addrtype;                   /* host address type (AF_INET) */

   int h_length;                       /* length of an address, in bytes */

   char **h_addr_list;              /* null-terminated array of in_addr structs */

   };

 

struct  in_addr

{

unsigned  long  s_addr; //32位的地址。

}

struct in_addr {

union {   

struct {     

u_char s_b1,s_b2,s_b3,s_b4;   

} S_un_b;      //An IPv4 address formatted as four u_chars.  

struct {     

u_short s_w1,s_w2;   

} S_un_w;   //An IPv4 address formatted as two u_shorts

u_long S_addr; //An IPv4 address formatted as a u_long

} S_un;

 

};

 

struct ip

{

#if __BYTE_ORDER == __LITTLE_ENDIAN     /* 如果为小端 */

    unsigned int ip_hl:4;                               /* 头部长度 */

    unsigned int ip_v:4;                                /* 版本 */

#endif

#if __BYTE_ORDER == __BIG_ENDIAN            /*如果为大端*/

    unsigned int ip_v:4;                                /* 版本 */

    unsigned int ip_hl:4;                               /* 头部长度 */

#endif

    u_int8_t ip_tos;                                        /* TOS,服务类型 */

    u_short ip_len;                                         /* 总长度 */

    u_short ip_id;                                          /* 标识值 */

    u_short ip_off;                                         /* 段偏移值 */

    ...

    ...

    u_int8_t ip_ttl;                                        /* TTL,生存时间 */

    u_int8_t ip_p;                                          /* 协议类型 */

    u_short ip_sum;                                         /* 校验和 */

    struct in_addr ip_src, ip_dst;          /* 源地址和目的地址 */

};

 

 

 

struct icmp

{

u_int8_t  icmp_type;  /* 消息类型 */

  u_int8_t  icmp_code;   /* 消息类型的子码 */

  u_int16_t icmp_cksum; /* 校验和 */

  union

  {

    u_char ih_pptr;      /* ICMP_PARAMPROB */

    struct in_addr ih_gwaddr;    /* 网关地址 */

    struct ih_idseq              /* 显示数据报 */

    {

      u_int16_t icd_id;      /* 数据报ID */

      u_int16_t icd_seq; /* 数据报的序号 */

    } ih_idseq;

    u_int32_t ih_void;

 

    /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */

    struct ih_pmtu

    {

      u_int16_t ipm_void;

      u_int16_t ipm_nextmtu;

    } ih_pmtu;

 

    struct ih_rtradv

    {

      u_int8_t irt_num_addrs;

      u_int8_t irt_wpa;

      u_int16_t irt_lifetime;

    } ih_rtradv;

  } icmp_hun;

#define icmp_pptr   icmp_hun.ih_pptr

#define icmp_gwaddr icmp_hun.ih_gwaddr

#define icmp_id     icmp_hun.ih_idseq.icd_id

#define icmp_seq    icmp_hun.ih_idseq.icd_seq

#define icmp_void   icmp_hun.ih_void

#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void

#define icmp_nextmtu    icmp_hun.ih_pmtu.ipm_nextmtu

#define icmp_num_addrs  icmp_hun.ih_rtradv.irt_num_addrs

#define icmp_wpa    icmp_hun.ih_rtradv.irt_wpa

#define icmp_lifetime   icmp_hun.ih_rtradv.irt_lifetime

  union

  {

    struct

    {

      u_int32_t its_otime;

      u_int32_t its_rtime;

      u_int32_t its_ttime;

    } id_ts;

    struct

    {

      struct ip idi_ip;

      /* options and then 64 bits of data */

    } id_ip;

    struct icmp_ra_addr id_radv;

    u_int32_t   id_mask;

    u_int8_t    id_data[1];  /* 数据 */

  } icmp_dun;

#define icmp_otime  icmp_dun.id_ts.its_otime

#define icmp_rtime  icmp_dun.id_ts.its_rtime

#define icmp_ttime  icmp_dun.id_ts.its_ttime

#define icmp_ip     icmp_dun.id_ip.idi_ip

#define icmp_radv   icmp_dun.id_radv

#define icmp_mask   icmp_dun.id_mask

#define icmp_data   icmp_dun.id_data

};

 

struct icmp

{

    u_int8_t  icmp_type; /* 消息类型 */

  u_int8_t  icmp_code;   /* 消息类型的子码 */

  u_int16_t icmp_cksum; /* 校验和 */

  union

  {   

    struct ih_idseq              /* 显示数据报 */

    {

      u_int16_t icd_id;      /* 数据报ID */

      u_int16_t icd_seq; /* 数据报的序号 */

    } ih_idseq;

  } icmp_hun;

 

#define icmp_id     icmp_hun.ih_idseq.icd_id

#define icmp_seq    icmp_hun.ih_idseq.icd_seq

  union

  {

    u_int8_t    id_data[1];  /* 数据 */

  } icmp_dun;

#define icmp_data   icmp_dun.id_data

};

 

 

#ifdef __FAVOR_BSD          /* 如果喜欢BSD样式 */

struct udphdr

{

  u_int16_t uh_sport;       /* 源地址端口 */

  u_int16_t uh_dport;       /* 目的地址端口 */

  u_int16_t uh_ulen;    /* UDP长度 */

  u_int16_t uh_sum;         /* UDP校验和 */

};

#else                                       /* Linux样式 */

struct udphdr

{

  u_int16_t source;         /* 源地址端口 */

  u_int16_t dest;               /* 目的地址端口 */

  u_int16_t len;            /* UDP长度 */

  u_int16_t check;          /* UDP校验和 */

};

#endif

 

struct tcphdr

{

    __u16   source;     /* 源地址端口 */

    __u16   dest;           /* 目的地址端口 */

    __u32   seq;            /* 序列号 */

    __u32   ack_seq;    /* 确认序列号 */

#if defined(__LITTLE_ENDIAN_BITFIELD)

    __u16   res1:4,     /* 保留 */

        doff:4,             /*  */

        fin:1,              /* 关闭连接标志 */

        syn:1,              /* 请求连接标志 */

        rst:1,              /* 重置连接标志 */

        psh:1,              /* 接收方尽快将数据放到应用层标志 */

        ack:1,              /* 确认序号标志 */

        urg:1,              /* 紧急指针标志 */

        ece:1,              /* 拥塞标志位 */

        cwr:1;              /* 拥塞标志位 */

#elif defined(__BIG_ENDIAN_BITFIELD)

    __u16   doff:4,

        res1:4,             /* 保留 */

        cwr:1,              /* 拥塞标志位 */

        ece:1,              /* 拥塞标志位 */

        urg:1,              /* 紧急指针标志 */

        ack:1,              /* 确认序号标志 */

        psh:1,              /* 接收方尽快将数据放到应用层标志 */

        rst:1,              /* 重置连接标志 */

        syn:1,              /* 请求连接标志 */

        fin:1;              /* 关闭连接标志 */

#else

#error  "Adjust your defines"

#endif 

    __u16   window;         /* 滑动窗口大小 */

    __u16   check;          /* 校验和 */

    __u16   urg_ptr;        /* 紧急字段指针 */

};

 

 

阅读(962) | 评论(0) | 转发(0) |
0

上一篇:(12)守护进程

下一篇:(13)原始套接字

给主人留下些什么吧!~~