From f903bcea88df0cf2e12583ae74e35c4bfb6760bf Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@mutabah.net>
Date: Thu, 4 Aug 2011 08:09:38 +0800
Subject: [PATCH] IPStack - Fixed TCP checksuming

- Padded with zero, instead of Undefined
- Fixed ping to return the ping time, instead of the timestamp
---
 Modules/IPStack/icmp.c | 2 +-
 Modules/IPStack/ipv4.c | 7 +++++--
 Modules/IPStack/tcp.c  | 2 ++
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/Modules/IPStack/icmp.c b/Modules/IPStack/icmp.c
index 969a1fa9..2cdc3f67 100644
--- a/Modules/IPStack/icmp.c
+++ b/Modules/IPStack/icmp.c
@@ -129,5 +129,5 @@ int ICMP_Ping(tInterface *Interface, tIPv4 Addr)
 	if(now() > end)
 		return -1;
 	
-	return (int)ts;
+	return (int)( now() - ts );
 }
diff --git a/Modules/IPStack/ipv4.c b/Modules/IPStack/ipv4.c
index b77581eb..6c387cc1 100644
--- a/Modules/IPStack/ipv4.c
+++ b/Modules/IPStack/ipv4.c
@@ -317,12 +317,15 @@ Uint16 IPv4_Checksum(const void *Buf, int Size)
 	const Uint16	*arr = Buf;
 	 int	i;
 	
-	Size = (Size + 1) >> 1;	// 16-bit word count
-	for(i = 0; i < Size; i++ )
+	// Sum all whole words
+	for(i = 0; i < Size/2; i++ )
 	{
 		Uint16	val = ntohs(arr[i]);
 		sum += val;
 	}
+	// Add the tail word
+//	if( i*2 != Size )
+//		sum += arr[i]&0xFF;
 	
 	// Apply one's complement
 	while (sum >> 16)
diff --git a/Modules/IPStack/tcp.c b/Modules/IPStack/tcp.c
index 68e72d76..1f51e10d 100644
--- a/Modules/IPStack/tcp.c
+++ b/Modules/IPStack/tcp.c
@@ -90,6 +90,8 @@ void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data )
 		buf[2] = (htons(Length)<<16) | (6<<8) | 0;
 		Data->Checksum = 0;
 		memcpy( &buf[3], Data, Length );
+		if(Length & 1)
+			((Uint8*)buf)[12+Length] = 0;
 		Data->Checksum = htons( IPv4_Checksum( buf, buflen ) );
 		free(buf);
 		IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data);
-- 
GitLab