C-使用pcap库的负有效负载长度

C-使用pcap库的负有效负载长度,c,pcap,C,Pcap,我正在使用pcap库用C编写一个包嗅探器,我注意到我的一些包的有效负载长度为负 IP、MAC、校验和以及所有这些似乎都是正常的,但一些数据包(并非全部)似乎有负负载 我是这样做的:我有一个指针,指向数据包的开头(即u_char)。我将指针向右移动(以越过L2,3,4标题)。在这个过程中,我还打印报头(源MAC、目标MAC、源IP等等)。标题似乎是有序的,但我不知道为什么有时会出现负负载 如果需要,我可以提供代码 谢谢大家! void got_packet(u_char* args, struct

我正在使用pcap库用C编写一个包嗅探器,我注意到我的一些包的有效负载长度为负

IP、MAC、校验和以及所有这些似乎都是正常的,但一些数据包(并非全部)似乎有负负载

我是这样做的:我有一个指针,指向数据包的开头(即u_char)。我将指针向右移动(以越过L2,3,4标题)。在这个过程中,我还打印报头(源MAC、目标MAC、源IP等等)。标题似乎是有序的,但我不知道为什么有时会出现负负载

如果需要,我可以提供代码

谢谢大家!

void got_packet(u_char* args, struct pcap_pkthdr* hdr, const u_char* packet) {

    FILE* f = fopen("/home/bogdan/C_Stuff/log.txt", "w+");
    static int packet_counter = 1;
    printf("\n\n");
    printf("PACKET NUMBER %i\n", packet_counter);   

    int ethernet_header_length = 14;
    int ip_header_length;
    int tcp_header_length;
    int payload_length;

    const u_char* ip_header;
    const u_char* tcp_header;
    const u_char* payload;

    ip_header = packet + ethernet_header_length;

    /* The second part of the IP Header contains 
        the length of the IP Header */
    ip_header_length = ((*ip_header) & 0x0F);   
    ip_header_length = ip_header_length * 4;      

    printf("IP Header Length = %i\n", ip_header_length);
    /* At the 10th byte we find info about TCP/UDP stuff */
    if(*(ip_header + 9) == IPPROTO_TCP) {
        printf("TCP Packet\n");
    }

    tcp_header = ip_header + ip_header_length;
    tcp_header_length = ((*(tcp_header) + 12) & 0xF0) >> 4;
    tcp_header_length *= 4;
    printf("TCP Header Length = %i\n", tcp_header_length);

    // Total header size
    int total_header_size = ethernet_header_length + ip_header_length + tcp_header_length;
    payload = packet + total_header_size;
    payload_length = hdr->caplen - total_header_size;
    printf("Payload Length = %i\n", payload_length);

    if(payload_length > 0) {
        int i;
        printf("\n**************************************************");
        printf("\nPAYLOAD:\n");
        for(i = 0; i < payload_length; i++) {
            printf("%i ", payload[i]);
            if(i % 10 == 0) {
                printf("\n");
            }
        }
        printf;
        printf("\n");
    }


    struct ether_header* eptr;
    eptr = (struct ether_header*)packet;    

    if(ntohs(eptr->ether_type) == ETHERTYPE_IP) {
        printf("IP PACKET\n");
    } else if(ntohs(eptr->ether_type) == ETHERTYPE_ARP) {
        printf("ARP PACKET\n");
    }

    struct ip* ip;
        ip = (struct ip*)(packet + sizeof(struct ether_header));

    printf("Source MAC:      %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_shost[0], eptr->ether_shost[1], eptr->ether_shost[2], eptr->ether_shost[3], eptr->ether_shost[4], eptr->ether_shost[5]);

    printf("Destination MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", eptr->ether_dhost[0], eptr->ether_dhost[1], eptr->ether_dhost[2], eptr->ether_dhost[3], eptr->ether_dhost[4], eptr->ether_dhost[5]);

    printf("Source IP: %s (IPv%i) | Total length = %i |\nTTL = %i | Checksum = %i\n", inet_ntoa(ip->ip_src), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);

    printf("Destination IP: %s (IPv%i) | Total length = %i |\nTTL= %i | Checksum = %i\n", inet_ntoa(ip->ip_dst), ip->ip_v, ip->ip_len, ip->ip_ttl, ip->ip_sum);

    printf("\n====================================================\n"); 

    fprintf(f, "%s", inet_ntoa(ip->ip_dst));

    fflush(stdout);
    packet_counter++;

    fclose(f);
}
void got_数据包(u_char*args,struct pcap\u pkthdr*hdr,const u_char*packet){
文件*f=fopen(“/home/bogdan/C_Stuff/log.txt”,“w+”);
静态int数据包_计数器=1;
printf(“\n\n”);
printf(“数据包编号%i\n”,数据包计数器);
int ethernet_header_length=14;
int ip_头长度;
int tcp_头长度;
int有效载荷长度;
const u_char*ip_头;
const u_char*tcp_头;
const u_char*有效载荷;
ip_头=数据包+以太网_头长度;
/*IP头的第二部分包含
IP头的长度*/
ip_头长度=(*ip_头)和0x0F);
ip_头长度=ip_头长度*4;
printf(“IP头长度=%i\n”,IP头长度);
/*在第10个字节,我们找到了关于TCP/UDP的信息*/
如果(*(ip_头+9)=IPPROTO_TCP){
printf(“TCP数据包”);
}
tcp_头=ip_头+ip_头长度;
tcp_头长度=(*(tcp_头)+12)和0xF0)>>4;
tcp_头_长度*=4;
printf(“TCP头长度=%i\n”,TCP头长度);
//总页眉大小
int total_header_size=以太网_header_长度+ip_header_长度+tcp_header_长度;
有效载荷=数据包+总头大小;
有效载荷长度=hdr->caplen-总头大小;
printf(“有效负载长度=%i\n”,有效负载长度);
如果(有效载荷长度>0){
int i;
printf(“\n**********************************************************”);
printf(“\n附件:\n”);
对于(i=0;i乙醚类型)=乙醚类型IP){
printf(“IP数据包\n”);
}否则如果(ntohs(eptr->Ethernet\u type)=Ethernet\u ARP){
printf(“ARP数据包\n”);
}
结构ip*ip;
ip=(结构ip*)(数据包+大小of(结构以太网头));
printf(“源MAC:%02x:%02x:%02x:%02x:%02x:%02x\n”,eptr->ether_-shost[0],eptr->ether_-shost[1],eptr->ether_-shost[2],eptr->ether_-shost[3],eptr->ether_-shost[4],eptr->ether_-shost[5]);
printf(“目标MAC:%02x:%02x:%02x:%02x:%02x:%02x\n”,eptr->ether_dhost[0],eptr->ether_dhost[1],eptr->ether_dhost[2],eptr->ether_dhost[3],eptr->ether_dhost[4],eptr->ether_dhost[5]);
printf(“源IP:%s(IPv%i)|总长度=%i | \nTTL=%i |校验和=%i\n”,inet_ntoa(IP->IP_src),IP->IP_v,IP->IP_len,IP->IP_ttl,IP->IP_sum);
printf(“目标IP:%s(IPv%i)|总长度=%i | \nTTL=%i |校验和=%i\n”,inet|ntoa(IP->IP|dst),IP->IP|v,IP->IP|len,IP->IP|ttl,IP->IP|IP|sum);
printf(“\n==================================================================================\n”);
fprintf(f,“%s”,inet_ntoa(ip->ip_dst));
fflush(stdout);
数据包计数器++;
fclose(f);
}
下面是一个示例捕获(除了有效负载长度之外,一切似乎都正常):

数据包编号9 IP头长度=20 TCP数据包TCP头长度= 52有效负载长度=-20 IP数据包源MAC:
24:0a:64:22:a2:39目标MAC:78:d7:52:29:06:db源IP: 192.168.100.10(IPv4)|总长度=13312 | TTL=64 |校验和=11794目标IP:216.58.209.165(IPv4)|总长度=13312| TTL=64 |校验和=11794


在我看来,输出中的TCP头长度=52。TCP头通常没有选项,TCP头的长度为20字节,没有选项

我已经很长时间没有写C了,所以我不太确定,但可能
tcp_头长度=((*(tcp_头)+12)&0xF0)>>4是错误的。
应该是:

tcp_header_length = (*(tcp_header + 12) & 0xF0) >> 4;

而且,您应该使用
hdr->len
而不是
hdr->caplen
来始终获得注释中指出的捕获数据包的实际长度。

是,请提供代码!更有用的是一个包的有效负载长度为负的例子。此外,我还上传了一个带有负负载的示例捕获数据包。非常感谢。可能是hdr->caplen小于实际数据包长度,请参阅: