Standard or Build in

地址相關

/*
 * [XSI] Structure used by kernel to store most addresses.
 */
struct sockaddr {
    __uint8_t    sa_len;        /* total length */
    sa_family_t    sa_family;    /* [XSI] address family */
    char        sa_data[14];    /* [XSI] addr value (actually larger) */
};
  • 說明:Socket地址結構,Socket指的是兩個程序互相溝通的管道,並完全只是網路使用,也有在本身上兩個程序互相溝通使用。
  • 成員:
    • sa_len:Socket地址總長度。
    • sa_family:Socket地址類型,這裡只要是AF_*(或PF_*)都接受。
    • sa_data:其他資料。


/*
 * Internet address (a structure for historical reasons)
 */
struct in_addr {
    in_addr_t s_addr;
};
  • 說明:IPv4網路順序地址結構。
  • 成員:
    • s_addr:IPv4地址,實際上是一個unsigned的32位元int。


/*
 * Socket address, internet style.
 */
struct sockaddr_in {
    __uint8_t    sin_len;
    sa_family_t    sin_family;
    in_port_t    sin_port;
    struct    in_addr sin_addr;
    char        sin_zero[8];
};
  • 說明:IPv4的Socket結構。
  • 成員:
    • sin_len:Socket地址總長度。
    • sin_family:只能為AF_INET
    • sin_port:port號碼,必須是網路順序。
    • sin_addr:IPv4網路順序地址。
    • sin_zero:補齊。


/*
 * IPv6 address
 */
struct in6_addr {
    union {
        __uint8_t   __u6_addr8[16];
        __uint16_t  __u6_addr16[8];
        __uint32_t  __u6_addr32[4];
    } __u6_addr;            /* 128-bit IP6 address */
};
  • 說明:IPv6網路順序地址結構。
  • 成員:
    • __u6_addr:一個聯合體(union),基本上就是一個128 bits長度的欄位資料。


/*
 * Socket address for IPv6
 */
#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
#define    SIN6_LEN
#endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
struct sockaddr_in6 {
    __uint8_t    sin6_len;    /* length of this struct(sa_family_t) */
    sa_family_t    sin6_family;    /* AF_INET6 (sa_family_t) */
    in_port_t    sin6_port;    /* Transport layer port # (in_port_t) */
    __uint32_t    sin6_flowinfo;    /* IP6 flow information */
    struct in6_addr    sin6_addr;    /* IP6 address */
    __uint32_t    sin6_scope_id;    /* scope zone index */
};
  • 說明:IPv6的Socket結構。
  • 成員:
    • sin6_len:Socket地址總長度。
    • sin6_family:只能為AF_INET6
    • sin6_port:port號碼,必須是網路順序。
    • sin6_flowinfo:IPv6的flow控制。
    • sin6_addr:IPv6網路順序地址。
    • sin6_scope_id:IPv6的Scope ID。


/*
 * [XSI] sockaddr_storage
 */
struct sockaddr_storage {
    __uint8_t    ss_len;        /* address length */
    sa_family_t    ss_family;    /* [XSI] address family */
    char            __ss_pad1[_SS_PAD1SIZE];
    __int64_t    __ss_align;    /* force structure storage alignment */
    char            __ss_pad2[_SS_PAD2SIZE];
};
  • 說明:當不確定要使用IPv4或IPv6時可以用這個結構體儲存,再利用ss_family判斷該轉成struct sockaddr_in或是struct sockaddr_in6
  • 成員:
    • ss_len:Socket地址總長度。
    • ss_family:Socket地址類型,這裡只有AF_INETAF_INET6表示不是IPv4就是IPv6。
    • __ss_pad1:補齊。
    • __ss_align:補齊。
    • __ss_pad2:補齊。


/*
 * Structure of a 48-bit Ethernet address.
 */
struct    ether_addr {
    u_char octet[ETHER_ADDR_LEN];
};
  • 說明:乙太的硬體地址(MAC Address)結構。
  • 成員:
    • octet:6個byte的unsigned char。

封包表頭相關

/*
 * The number of bytes in an ethernet (MAC) address.
 */
#define    ETHER_ADDR_LEN        6
/*
 * Structure of a 10Mb/s Ethernet header.
 */
struct    ether_header {
    u_char    ether_dhost[ETHER_ADDR_LEN];
    u_char    ether_shost[ETHER_ADDR_LEN];
    u_short    ether_type;
};
  • 說明:Ethernet封包的表頭。
  • 成員:
    • ether_dhost:Destination MAC Address,6 bytes
    • ether_shost:Source MAC Address,6 bytes
    • ether_type:Type,2 bytes


/*
 * Address Resolution Protocol.
 *
 * See RFC 826 for protocol description.  ARP packets are variable
 * in size; the arphdr structure defines the fixed-length portion.
 * Protocol type values are the same as those for 10 Mb/s Ethernet.
 * It is followed by the variable-sized fields ar_sha, arp_spa,
 * arp_tha and arp_tpa in that order, according to the lengths
 * specified.  Field names used correspond to RFC 826.
 */
struct    arphdr {
    u_short    ar_hrd;        /* format of hardware address */
#define ARPHRD_ETHER     1    /* ethernet hardware format */
#define ARPHRD_IEEE802    6    /* token-ring hardware format */
#define ARPHRD_FRELAY     15    /* frame relay hardware format */
#define ARPHRD_IEEE1394    24    /* IEEE1394 hardware address */
#define ARPHRD_IEEE1394_EUI64 27 /* IEEE1394 EUI-64 */
    u_short    ar_pro;        /* format of protocol address */
    u_char    ar_hln;        /* length of hardware address */
    u_char    ar_pln;        /* length of protocol address */
    u_short    ar_op;        /* one of: */
#define    ARPOP_REQUEST    1    /* request to resolve address */
#define    ARPOP_REPLY    2    /* response to previous request */
#define    ARPOP_REVREQUEST 3    /* request protocol address given hardware */
#define    ARPOP_REVREPLY    4    /* response giving protocol address */
#define ARPOP_INVREQUEST 8     /* request to identify peer */
#define ARPOP_INVREPLY    9    /* response identifying peer */
/*
 * The remaining fields are variable in size,
 * according to the sizes above.
 */
#ifdef COMMENT_ONLY
    u_char    ar_sha[];    /* sender hardware address */
    u_char    ar_spa[];    /* sender protocol address */
    u_char    ar_tha[];    /* target hardware address */
    u_char    ar_tpa[];    /* target protocol address */
#endif
};
  • 說明:ARP封包的"基本"表頭。
  • 成員:
    • ar_hrd:Hardware Type,2 bytes
    • ar_pro:Protocol Type,2 bytes
    • ar_hln:Hardware Length,1 byte
    • ar_pln:Protocol Length,1 byte
    • ar_op:Operation Code,2 bytes


*
 * 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_char    arp_sha[ETHER_ADDR_LEN];    /* sender hardware address */
    u_char    arp_spa[4];    /* sender protocol address */
    u_char    arp_tha[ETHER_ADDR_LEN];    /* target hardware address */
    u_char    arp_tpa[4];    /* target protocol address */
};
#define    arp_hrd    ea_hdr.ar_hrd
#define    arp_pro    ea_hdr.ar_pro
#define    arp_hln    ea_hdr.ar_hln
#define    arp_pln    ea_hdr.ar_pln
#define    arp_op    ea_hdr.ar_op
  • 說明:當ARP的硬體、協定類型與Ethernet相關的表頭。
  • 成員:
    • ea_hdr:ARP基本表頭,大小8 bytes
    • arp_sha:Sender Hardware Address,6 bytes
    • arp_spa:Sender Protocol Address,4 bytes
    • arp_tha:Target Hardware Address,6 bytes
    • arp_tpa:Target Protocol Address,4 bytes


/*
 * Definitions for internet protocol version 4.
 * Per RFC 791, September 1981.
 */
#define    IPVERSION    4

/*
 * Structure of an internet header, naked of options.
 */
struct ip {
#ifdef _IP_VHL
    u_char    ip_vhl;            /* version << 4 | header length >> 2 */
#else
#if BYTE_ORDER == LITTLE_ENDIAN
    u_int    ip_hl:4,        /* header length */
        ip_v:4;            /* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
    u_int    ip_v:4,            /* version */
        ip_hl:4;        /* header length */
#endif
#endif /* not _IP_VHL */
    u_char    ip_tos;            /* type of service */
    u_short    ip_len;            /* total length */
    u_short    ip_id;            /* identification */
    u_short    ip_off;            /* fragment offset field */
#define    IP_RF 0x8000            /* reserved fragment flag */
#define    IP_DF 0x4000            /* dont fragment flag */
#define    IP_MF 0x2000            /* more fragments flag */
#define    IP_OFFMASK 0x1fff        /* mask for fragmenting bits */
    u_char    ip_ttl;            /* time to live */
    u_char    ip_p;            /* protocol */
    u_short    ip_sum;            /* checksum */
    struct    in_addr ip_src,ip_dst;    /* source and dest address */
};
  • 說明:IPv4表頭。
  • 成員:
    • ip_vhl(ip_hl、ip_v):IP Version、Header Length(共用),1 byte,各4 bits
    • ip_tos:Type of Service,1 byte
    • ip_len:Total Length,2 bytes
    • ip_id:Identification,2 bytes
    • ip_off:Flags、Fragment Offset(共用),2 bytes,分別3 bits13 bits
    • ip_ttl:Time to Live,1 byte
    • ip_p:Next Protocol,1 byte
    • ip_sum:Checksum,2 bytes
    • ip_src:Source IP Address,4 bytes
    • ip_dst:Destination IP Address,4 bytes


/*
 * TCP header.
 * Per RFC 793, September, 1981.
 */
struct tcphdr {
    unsigned short    th_sport;    /* source port */
    unsigned short    th_dport;    /* destination port */
    tcp_seq    th_seq;            /* sequence number */
    tcp_seq    th_ack;            /* acknowledgement number */
#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
    unsigned int    th_x2:4,    /* (unused) */
            th_off:4;    /* data offset */
#endif
#if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN
    unsigned int    th_off:4,    /* data offset */
            th_x2:4;    /* (unused) */
#endif
    unsigned char    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
#define    TH_ECE    0x40
#define    TH_CWR    0x80
#define    TH_FLAGS    (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)

    unsigned short    th_win;        /* window */
    unsigned short    th_sum;        /* checksum */
    unsigned short    th_urp;        /* urgent pointer */
};
  • 說明:TCP表頭。
  • 成員:
    • th_sport:Source Port,2 bytes
    • th_dport:Destination Port,2 bytes
    • th_seq:Sequence Number,4 bytes
    • th_ack:Acknowledgment Number,4 bytes
    • th_x2:Reserved,4 bits
    • th_off:Header Length(Data Offset),4 bits
    • th_flags:Flags,8 bits
    • th_win:Window Size,2 bytes
    • th_sum:Checksum,2 bytes
    • th_urp:Urgent Pointer,2 bytes


/*
 * Udp protocol header.
 * Per RFC 768, September, 1981.
 */
struct udphdr {
    u_short    uh_sport;        /* source port */
    u_short    uh_dport;        /* destination port */
    u_short    uh_ulen;        /* udp length */
    u_short    uh_sum;            /* udp checksum */
};
  • 說明:UDP表頭。
  • 成員:
    • uh_sport:Source Port,2 bytes
    • uh_dport:Destination Port,2 bytes
    • uh_ulen:Length,2 bytes
    • uh_sum:Checksum,2 bytes


/*
 * Interface Control Message Protocol Definitions.
 * Per RFC 792, September 1981.
 */

/*
 * Internal of an ICMP Router Advertisement
 */
struct icmp_ra_addr {
    u_int32_t ira_addr;
    u_int32_t ira_preference;
};

/*
 * Structure of an icmp header.
 */
struct icmp {
    u_char    icmp_type;        /* type of message, see below */
    u_char    icmp_code;        /* type sub code */
    u_short    icmp_cksum;        /* ones complement cksum of struct */
    union {
        u_char ih_pptr;            /* ICMP_PARAMPROB */
        struct in_addr ih_gwaddr;    /* ICMP_REDIRECT */
        struct ih_idseq {
            n_short    icd_id;
            n_short    icd_seq;
        } ih_idseq;
        int ih_void;

        /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
        struct ih_pmtu {
            n_short ipm_void;
            n_short ipm_nextmtu;
        } ih_pmtu;

        struct ih_rtradv {
            u_char irt_num_addrs;
            u_char 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 id_ts {
            n_time its_otime;
            n_time its_rtime;
            n_time its_ttime;
        } id_ts;
        struct id_ip  {
            struct ip idi_ip;
            /* options and then 64 bits of data */
        } id_ip;
        struct icmp_ra_addr id_radv;
        u_int32_t id_mask;
        char    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
};
  • 說明:ICMP表頭。
  • 成員:
    • icmp_type:Type,1 byte
    • icmp_code:Code,1 byte
    • icmp_cksum:Checksum,2 bytes
    • icmp_hun:長度為4 bytes的聯合體(union)。這邊在不同Type及Code時,所要使用的成員皆不同,例如ICMP Redirect封包(Type 5)要使用成員ih_gwaddr來取得Router IP Address欄位。
    • icmp_icmp_dun:長度為20 bytes的聯合體(union),同成員icmp_hun

主機訊息相關

struct addrinfo {
    int    ai_flags;    /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
    int    ai_family;    /* PF_xxx */
    int    ai_socktype;    /* SOCK_xxx */
    int    ai_protocol;    /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
    socklen_t ai_addrlen;    /* length of ai_addr */
    char    *ai_canonname;    /* canonical name for hostname */
    struct    sockaddr *ai_addr;    /* binary address */
    struct    addrinfo *ai_next;    /* next structure in linked list */
};
  • 說明:主機訊息的結構。
  • 成員:
    • ai_flags:Flags有AI_ADDRCONFIGAI_ALLAI_CANONNAMEAI_NUMERICHOSTAI_NUMERICSERVAI_PASSIVEAI_V4MAPPEDAI_V4MAPPED_CFG以及AI_DEFAULT
    • ai_family:主要使用AF_INET以及AF_INET6指定要取得IPv4或IPv6地址,也可以設定成AF_UNSPEC不指定哪類地址。
    • ai_socktype:指定Socket類型,SOCK_STREAMSOCK_DGRAMSOCK_RAW
    • ai_protocol:傳輸層的協定,可以指定成IPPROTO_UDPIPPROTO_TCP
    • ai_addrlen:Socket地址長度。
    • ai_canonname:主機的正式名稱。
    • ai_addr:主機地址。
    • ai_next:指向下一個主機訊息結構。

results matching ""

    No results matching ""