如何在C中使用Winpcap打印HTTP标头

时间:2016-08-08 15:14:53

标签: c winpcap

我试图编写一个嗅探器(仅限TCP),我想打印出HTTP标头。标头保存为txt日志文件。但输出并不像预期的那样。 output_screenshoot

这是C代码:

void Print_http_request(u_char *Buffer, int Size) { //Buffer: The beginning of the captured packets.
    int iphdrlen = 0; //IP header length
    int tcphdrlen = 0; //TCP header length
    iphdr = (IPV4_HDR *)(Buffer + sizeof(ETHER_HDR)); //allocate IP header
    iphdrlen = iphdr->ip_header_len * 4; //4(bytes)* ip_header_len(words)
    tcpheader = (TCP_HDR*)(Buffer + iphdrlen + sizeof(ETHER_HDR));//allocate TCP header
    tcphdrlen = tcpheader->data_offset * 4;

    data = (Buffer + sizeof(ETHER_HDR) + iphdrlen + tcphdrlen); //Allocate TCP data = the beginning of HTTP header.
    data_size = iphdr->ip_total_length * 4 - iphdrlen - tcphdrlen; //TCP data size (such as HTTP header, HTTP body)

    //fprintf(logfile, "HTTP HEADER\n");
    fprintf(logfile, "\n===========================\n");
    int _i = 0;
    while (_i < data_size) {
        fprintf(logfile, "%c", data[_i++]);
    }
    fprintf(logfile, "\n===========================\n");
}

我非常感谢你的帮助。

0 个答案:

没有答案