从这个网站:http ://www.enderunix.org/docs/en/rawipspoof/
我拉了以下功能
unsigned short in_cksum(unsigned short *addr, int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *) (&answer) = *(unsigned char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
unsigned short in_cksum_tcp(int src, int dst, unsigned short *addr, int len)
{
struct psd_tcp buf;
u_short ans;
memset(&buf, 0, sizeof(buf));
buf.src.s_addr = src;
buf.dst.s_addr = dst;
buf.pad = 0;
buf.proto = IPPROTO_TCP;
buf.tcp_len = htons(len);
memcpy(&(buf.tcp), addr, len);
ans = in_cksum((unsigned short *)&buf, 12 + len);
return (ans);
}
但是,当我对我看到的传入 TCP 数据包运行它时,我没有得到与当前相同的校验和结果(我保存传入的 TCP 校验和并将数据包上的校验和设置为零,然后再尝试运行校验和功能)
现在,当我对 IP 标头运行“in_cksum”时,我得到了正确的答案。
这是该站点的 TCP 伪标头:
struct psd_tcp {
struct in_addr src;
struct in_addr dst;
unsigned char pad;
unsigned char proto;
unsigned short tcp_len;
struct tcphdr tcp;
};
它是否缺少使这项工作有效的东西?