Move from my Netease blog
Struct sniff_ethernet {
U_char ether_dhost [ETHER_ADDR_LEN];/* destination host address */
U_char ether_shost [ETHER_ADDR_LEN];/* source host address */
U_short ether_type;/* IP? ARP? RARP? Etc */
};
/* IP packet header */
Struct sniff_ip {
# If BYTE_ORDER = LITTLE_ENDIAN
U_int ip_hl: 4,/* Header Length */
Ip_v: 4;/* version */
# If BYTE_ORDER = BIG_ENDIAN
U_int ip_v: 4,/* version */
Ip_hl: 4;/* head length */
# Endif
# Endif/* not _ IP_VHL */
U_char ip_tos;/* service type */
U_short ip_len;/* total length */
U_short ip_id;/* package ID */
U_short ip_off;/* Fragment Offset */
# Define IP_RF 0x8000/* Reserved fragment flag */
# Define IP_DF 0x4000/* dont fragment flag */
# Define IP_MF 0x2000/* multi-fragment flag */
# Define ip_offmask 0x1fff/* segment bit */
U_char ip_ttl;/* data packet survival time */
U_char ip_p;/* protocol used */
U_short ip_sum;/* checksum */
Struct in_addr ip_src, ip_dst;/* Source Address, Destination Address */
};
/* TCP packet header */
Struct sniff_tcp {
U_short th_sport;/* Source Port */
U_short th_dport;/* destination port */
Tcp_seq th_seq;/* Package number */
Tcp_seq th_ack;/* confirm the sequence number */
# If byte_order = little_endian
U_int th_x2: 4,/* not used yet */
Th_off: 4;/* Data offset */
# Endif
# If byte_order = big_endian
U_int th_off: 4,/* Data offset */
Th_x2: 4;/* not used yet */
# Endif
U_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)
U_short th_win;/* TCP sliding window */
U_short th_sum;/* Header checksum */
U_short th_urp;/* Emergency Service bit */
};
/* TCP Header */
Typedef u_int tcp_seq;
Struct sniff_tcp
{
U_short th_sport;
U_short th_dport;
Tcp_seq th_seq;
Tcp_seq th_ack;
U_char th_offx2;
U_char th_flags;
U_short th_win;
U_short th_sum;
U_short th_urp;
};
/* UDP header */
Struct sniff_udp
{
U_short udp_sport;
U_short udp_dport;
U_short udp_len;
U_short udp_sum;
};
/* DNS header */
Struct sniff_dns
{
U_short dns_id;
U_short dns_flag;
U_short dns_ques;
U_short dns_ans;
U_short dns_auth;
U_short dns_add;
U_int8_t * dsn_data;
};