【问题标题】:More data in packet payload数据包有效载荷中的更多数据
【发布时间】:2011-01-19 08:59:48
【问题描述】:

我有以下代码

int ParseData(unsigned char *packet, int len) { 结构 ethhdr *ethernet_header; 结构 iphdr *ip_header; 结构 tcphdr *tcp_header; 无符号字符*数据; int data_len;

    /* Check if any data is there */

    if(len > (sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr)))
    {

            ip_header = (struct iphdr*)(packet + sizeof(struct ethhdr));


            data = (packet + sizeof(struct ethhdr) + ip_header->ihl*4 + sizeof(struct tcphdr));
            data_len = ntohs(ip_header->tot_len) - ip_header->ihl*4 - sizeof(struct tcphdr);

            if(data_len)
            {
                    printf("Data Len : %d\n", data_len);
                    PrintData("Data : ", data, data_len);
                    printf("\n\n");
                    return 1;
            }
            else
            {
                    printf("No Data in packet\n");
                    return 0;
            }
    }

}

我正在尝试以 ASCII 码打印有效负载,并使用这样的简单函数

PrintData(char *mesg, unsigned char *p, int len) { printf(mesg);

    while(len--)
    {
            if(isprint(*p))
                    printf("%c", *p);
            else
                    printf(".");
            p++;
    }

}

代码看起来不错,没有编译问题/警告。问题是第一个有效载荷 字符不在位置 0 处打印,而是在 12 个字节后打印。

我认为所有“len”字节都是我必须打印的确切数据。

我的数据点在 data = (packet + sizeof(struct ethhdr) + ip_header->ihl*4 + sizeof(struct tcphdr)); 但是 data[0] 不可打印。问题是什么?我错过了什么吗?我是否必须检查 TCP 选项部分?

谢谢

【问题讨论】:

    标签: network-programming tcp capture payload


    【解决方案1】:

    没错,添加 sizeof(struct tcphdr) 只会让您通过标题,而不是选项。要获取实际数据,您应该使用 TCP 标头中的“偏移”字段。偏移量从 TCP 标头开始计算,以 4 字节为单位,例如如果偏移量为 8,则标头 + 选项长度为 32。

    【讨论】:

    • 换句话说,而不是 sizeof(struct tcphdr) 我需要把 tcp_header->doff*4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2018-03-31
    • 2023-03-08
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多