在utils_http.c你有以下功能:
static int handle_tcp(const struct tcphdr *tcp, int len)
{
char buf[PCAP_SNAPLEN];
memcpy(buf, tcp + 1, len - sizeof(*tcp));
DEBUG("DANY TCPDs tcp string: %s",buf);
if (0 == handle_http(buf, len - sizeof(*tcp)))
return 0;
return 1;
}
这是假设 TCP 负载总是在 TCP 标头开始后 20 个字节开始(总是 20,因为sizeof(*tcp) == 20)。这不考虑任何 TCP 选项。如果您收到带有 TCP 选项的数据包(这很常见),handle_http() 将在其缓冲区的开头包含二进制编码的 TCP 选项,这可能就是您所看到的。
试试这样的:
static int handle_tcp(const struct tcphdr *tcp, int len)
{
char buf[PCAP_SNAPLEN];
memcpy(buf, (void*)tcp + tcp->doff*4, len - tcp->doff*4);
DEBUG("DANY TCPDs tcp string: %s",buf);
if (0 == handle_http(buf, len - tcp->doff*4))
return 0;
return 1;
}
或者更好的是,我不知道为什么你每次有机会都会不断地制作几十个缓冲区副本。除非我遗漏了什么,否则你可以直接传递指针:
static int handle_tcp(const struct tcphdr *tcp, int len) {
return handle_http((void*)tcp + tcp->doff*4, len - tcp->doff*4);
}