都没有改校验部分的代码,莫名其妙的UDP校验就对了

This commit is contained in:
Stone 2014-11-16 18:45:34 +00:00
parent cccf7bfe6f
commit 5ca2761e44
3 changed files with 23 additions and 20 deletions

View File

@ -95,6 +95,11 @@ void TinyIP::Process(MemoryStream* ms)
// 是否发给本机。注意memcmp相等返回0
if(!ip || !IsMyIP(ip->DestIP)) return;
Sys.ShowHex((byte*)ip, ip->Size(), '-');
debug_printf("\r\n");
Sys.ShowHex((byte*)ip->Next(), __REV16(ip->TotalLength) - ip->Size(), '-');
debug_printf("\r\n");
#if NET_DEBUG
if(eth->Type != ETH_IP)
{
@ -109,15 +114,9 @@ void TinyIP::Process(MemoryStream* ms)
RemoteIP = ip->SrcIP;
// 移交给ARP处理为了让它更新ARP表
//if(Arp && Arp->Enable) Arp->Process(NULL);
if(Arp) Arp->Process(NULL);
//!!! 太杯具了,收到的数据包可能有多余数据,这两个长度可能不等
//assert_param(__REV16(ip->TotalLength) == len);
// 数据包是否完整
//if(ms->Remain() < __REV16(ip->TotalLength)) return;
// 计算负载数据的长度注意ip可能变长长度Length的单位是4字节
//len -= sizeof(IP_HEADER);
// 前面的len不准确必须以这个为准
uint size = __REV16(ip->TotalLength) - (ip->Length << 2);
ms->Length = ms->Position() + size;
@ -257,7 +256,7 @@ void TinyIP::ShowInfo()
#endif
}
void TinyIP::SendEthernet(ETH_TYPE type, byte* buf, uint len)
void TinyIP::SendEthernet(ETH_TYPE type, const byte* buf, uint len)
{
ETH_HEADER* eth = (ETH_HEADER*)(buf - sizeof(ETH_HEADER));
assert_param(IS_ETH_TYPE(type));
@ -288,7 +287,7 @@ void TinyIP::SendEthernet(ETH_TYPE type, byte* buf, uint len)
_port->Write((byte*)eth, len);
}
void TinyIP::SendIP(IP_TYPE type, byte* buf, uint len)
void TinyIP::SendIP(IP_TYPE type, const byte* buf, uint len)
{
IP_HEADER* ip = (IP_HEADER*)(buf - sizeof(IP_HEADER));
assert_param(ip);
@ -363,7 +362,7 @@ void TinyIP::ShowMac(const MacAddress& mac)
debug_printf("-%02X", *m++);
}
ushort TinyIP::CheckSum(byte* buf, uint len, byte type)
ushort TinyIP::CheckSum(const byte* buf, uint len, byte type)
{
// type 0=ip
// 1=udp

View File

@ -91,10 +91,10 @@ public:
void ShowInfo();
static void ShowIP(IPAddress ip);
static void ShowMac(const MacAddress& mac);
ushort CheckSum(byte* buf, uint len, byte type);
ushort CheckSum(const byte* buf, uint len, byte type);
void SendEthernet(ETH_TYPE type, byte* buf, uint len);
void SendIP(IP_TYPE type, byte* buf, uint len);
void SendEthernet(ETH_TYPE type, const byte* buf, uint len);
void SendIP(IP_TYPE type, const byte* buf, uint len);
bool IsMyIP(IPAddress ip); // 是否发给我的IP地址
bool IsBroadcast(IPAddress ip); // 是否广播地址
};

View File

@ -61,6 +61,8 @@ bool UdpSocket::Process(MemoryStream* ms)
uint len = ms->Remain();
uint plen = __REV16(udp->Length);
assert_param(len + sizeof(UDP_HEADER) == plen);
//Sys.ShowHex((byte*)udp, udp->Size(), '-');
//debug_printf("\r\n");
#endif
OnProcess(udp, *ms);
@ -78,9 +80,10 @@ void UdpSocket::OnProcess(UDP_HEADER* udp, MemoryStream& ms)
#if NET_DEBUG
ushort oldsum = __REV16(udp->Checksum);
udp->Checksum = 0;
udp->Checksum = __REV16(Tip->CheckSum((byte*)udp, sizeof(UDP_HEADER) + len, 1));
debug_printf("UDP::Checksum ori=0x%02x new=0x%02x\r\n", oldsum, __REV16(udp->Checksum));
//udp->Checksum = 0;
//udp->Checksum = __REV16(Tip->CheckSum((byte*)udp, sizeof(UDP_HEADER) + len, 1));
ushort newsum = Tip->CheckSum((byte*)udp, sizeof(UDP_HEADER) + len, 1);
debug_printf("UDP::Checksum ori=0x%02x new=0x%02x\r\n", oldsum, newsum);
#endif
// 触发ITransport接口事件
@ -116,20 +119,21 @@ void UdpSocket::Send(UDP_HEADER* udp, uint len, bool checksum)
//UDP_HEADER* udp = (UDP_HEADER*)(buf - sizeof(UDP_HEADER));
assert_param(udp);
uint tlen = sizeof(UDP_HEADER) + len;
udp->SrcPort = __REV16(Port > 0 ? Port : LocalPort);
udp->DestPort = __REV16(RemotePort);
udp->Length = __REV16(sizeof(UDP_HEADER) + len);
udp->Length = __REV16(tlen);
// 必须在校验码之前设置,因为计算校验码需要地址
Tip->RemoteIP = RemoteIP;
// 网络序是大端
udp->Checksum = 0;
if(checksum) udp->Checksum = __REV16(Tip->CheckSum((byte*)udp, sizeof(UDP_HEADER) + len, 1));
if(checksum) udp->Checksum = __REV16(Tip->CheckSum((byte*)udp, tlen, 1));
debug_printf("SendUdp: len=%d(0x%x) %d => %d \r\n", udp->Length, udp->Length, __REV16(udp->SrcPort), __REV16(udp->DestPort));
debug_printf("SendUdp: len=%d(0x%x) %d => %d \r\n", tlen, tlen, __REV16(udp->SrcPort), RemotePort);
Tip->SendIP(IP_UDP, (byte*)udp, sizeof(UDP_HEADER) + len);
Tip->SendIP(IP_UDP, (byte*)udp, tlen);
}
UDP_HEADER* UdpSocket::Create()