最近使用WinPcap/libpcap抓取网络数据包,从而提取HTML和图片。分析到TCP包的时候被字节序搞的脑袋大了。最后耐着性子把标志位字段分别读取出来。
TCP头部结构体声明:
标志位取值:
PS:好像电信部门现在紧抓服务器内容分析,以后上网都得小心了,不小心可能就要去吃窝窝头了。
TCP头部结构体声明:
//structure of TCP header
struct tcp_hdr {
u_short sport; //source port
u_short dport; //destinate port
u_int32_t sn; //SN
u_int32_t an; //AN
u_int16_t other; //header length(4 bit) +
//reserved(6 bit) +
//URG + ACK + PSH + RST + SYN + FIN
u_int16_t win_size; //window size
u_int16_t checksum; //Checksum
u_int16_t urg_ptr; //urgent pointer
u_int32_t option; //32-bit
};
标志位取值:
/**
* Get URG bit
* */
int tcp_bit_urg(struct tcp_hdr *th) {
return (th->other & 0x2000) == 0 ? 0 : 1;
}
/**
* Get ACK bit
* */
int tcp_bit_ack(struct tcp_hdr *th) {
return (th->other & 0x1000) == 0 ? 0 : 1;
}
/**
* Get PSH bit
* */
int tcp_bit_psh(struct tcp_hdr *th) {
return (th->other & 0x0800) == 0 ? 0 : 1;
}
/**
* Get RST bit
* */
int tcp_bit_rst(struct tcp_hdr *th) {
return (th->other & 0x0400) == 0 ? 0 : 1;
}
/**
* Get SYN bit
* */
int tcp_bit_syn(struct tcp_hdr *th) {
return (th->other & 0x0200) == 0 ? 0 : 1;
}
/**
* Get FIN bit
* */
int tcp_bit_fin(struct tcp_hdr *th) {
return (th->other & 0x0100) == 0 ? 0 : 1;
}
/**
* Get tcp header length
* */
int tcp_header_len(struct tcp_hdr *th) {
return ((th->other >> 4) & 0xf) * 4;
}
PS:好像电信部门现在紧抓服务器内容分析,以后上网都得小心了,不小心可能就要去吃窝窝头了。