gpt4 book ai didi

c++ - ICMP 校验和错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:55:20 24 4
gpt4 key购买 nike

我正在尝试使用与 tcp 和 udp 相同的技术对 icmp 数据包进行校验和,但它得到错误的校验和,你能告诉我我的问题在哪里吗?

        ICMP_HEADER *icmpheader = new ICMP_HEADER;
memcpy(icmpheader,ICMPHeader,sizeof(ICMP_HEADER));
icmpheader->Checksum = 0;

PSEUDO_HEADER *psheader = new PSEUDO_HEADER;
memcpy(&psheader->daddr, &IPHeader->DestinationAddress, sizeof(UINT));
memcpy(&psheader->saddr, &IPHeader->SourceAddress, sizeof(UINT));
psheader->protocol = IPHeader->Protocol;
psheader->length = htons((USHORT)(sizeof(ICMP_HEADER) + ICMPDataSize));
psheader->zero = 0x0000;

UINT packet_size = sizeof(ICMP_HEADER) + ICMPDataSize + sizeof(PSEUDO_HEADER);
packet_size = packet_size + ((packet_size%2)*2);
UCHAR *icmppacket = (UCHAR*)malloc(packet_size);

memset(icmppacket,0, packet_size);
memcpy(icmppacket, psheader, sizeof(PSEUDO_HEADER));
memcpy(&icmppacket[sizeof(PSEUDO_HEADER)], icmpheader,sizeof(ICMP_HEADER));
memcpy(&icmppacket[sizeof(PSEUDO_HEADER) + sizeof(ICMP_HEADER)],ICMPData,ICMPDataSize);


if (GlobalChecksum((USHORT*)icmppacket,packet_size) != ICMPHeader->Checksum)
{
isMalformed = true;
PacketError = PACKET_ICMP_CHECKSUM;
}

USHORT cPacket::GlobalChecksum(USHORT *buffer, UINT length)
{
register int sum = 0;
USHORT answer = 0;
register USHORT *w = buffer;
register int nleft = length;

while(nleft > 1){
sum += *w++;
nleft -= 2;
}

sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}

最佳答案

通过删除伪 header 并仅计算 icmp header 和 icmp 数据解决了这个问题

ICMP_HEADER *icmpheader = new ICMP_HEADER;
memcpy(icmpheader,ICMPHeader,sizeof(ICMP_HEADER));
icmpheader->Checksum = 0x0000;

UINT packet_size = sizeof(ICMP_HEADER) + ICMPDataSize;
packet_size = packet_size + ((packet_size%2)*2);
UCHAR *icmppacket = (UCHAR*)malloc(packet_size);

memset(icmppacket,0, packet_size);
memcpy(icmppacket, icmpheader,sizeof(ICMP_HEADER));
memcpy(&icmppacket[sizeof(ICMP_HEADER)],ICMPData,ICMPDataSize);

if (GlobalChecksum((USHORT*)icmppacket,packet_size) != ICMPHeader->Checksum)
{
isMalformed = true;
PacketError = PACKET_ICMP_CHECKSUM;
}

关于c++ - ICMP 校验和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com