WinPcap/libpcap网络抓包分析TCP头标志位

最近使用WinPcap/libpcap抓取网络数据包,从而提取HTML和图片。分析到TCP包的时候被字节序搞的脑袋大了。最后耐着性子把标志位字段分别读取出来。
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:好像电信部门现在紧抓服务器内容分析,以后上网都得小心了,不小心可能就要去吃窝窝头了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值