IPStack - Changed IPv4_Checksum to use a word count
authorJohn Hodge <[email protected]>
Thu, 4 Aug 2011 09:02:43 +0000 (17:02 +0800)
committerJohn Hodge <[email protected]>
Thu, 4 Aug 2011 09:02:43 +0000 (17:02 +0800)
Modules/IPStack/icmp.c
Modules/IPStack/ipv4.c
Modules/IPStack/ipv4.h
Modules/IPStack/tcp.c

index 2cdc3f6..1603719 100644 (file)
@@ -81,7 +81,7 @@ void ICMP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buff
                //Log_Debug("ICMPv4", "Replying");
                hdr->Type = ICMP_ECHOREPLY;
                hdr->Checksum = 0;
-               hdr->Checksum = htons( IPv4_Checksum(hdr, Length) );
+               hdr->Checksum = htons( IPv4_Checksum( (Uint16*)hdr, Length/2 ) );
                //Log_Debug("ICMPv4", "Checksum = 0x%04x", hdr->Checksum);
                IPv4_SendPacket(Interface, *(tIPv4*)Address, 1, ntohs(hdr->Sequence), Length, hdr);
                break;
@@ -117,7 +117,7 @@ int ICMP_Ping(tInterface *Interface, tIPv4 Addr)
        gICMP_PingSlots[i].bArrived = 0;
        hdr->ID = i;
        hdr->Sequence = ~i;
-       hdr->Checksum = htons( IPv4_Checksum(hdr, sizeof(buf)) );
+       hdr->Checksum = htons( IPv4_Checksum((Uint16*)hdr, sizeof(buf)/2) );
        
        ts = now();
        
index 6c387cc..04b8f4b 100644 (file)
@@ -21,7 +21,7 @@ extern tMacAddr       ARP_Resolve4(tInterface *Interface, tIPv4 Address);
 void   IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
 tInterface     *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
 Uint32 IPv4_Netmask(int FixedBits);
-Uint16 IPv4_Checksum(const void *Buf, int Size);
+Uint16 IPv4_Checksum(const Uint16 *Buf, int WordCount);
  int   IPv4_Ping(tInterface *Iface, tIPv4 Addr);
 
 // === GLOBALS ===
@@ -105,7 +105,7 @@ int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int
        hdr->HeaderChecksum = 0;        // Will be set later
        hdr->Source = *(tIPv4*)Iface->Address;
        hdr->Destination = Address;
-       hdr->HeaderChecksum = htons(IPv4_Checksum(hdr, sizeof(tIPv4Header)));
+       hdr->HeaderChecksum = htons(IPv4_Checksum((Uint16*)hdr, sizeof(tIPv4Header)/2));
        
        Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
                Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
@@ -154,7 +154,7 @@ void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buff
                Uint16  hdrVal, compVal;
                hdrVal = ntohs(hdr->HeaderChecksum);
                hdr->HeaderChecksum = 0;
-               compVal = IPv4_Checksum(hdr, hdr->HeaderLength * 4);
+               compVal = IPv4_Checksum((Uint16*)hdr, (hdr->HeaderLength * 4) / 2);
                if(hdrVal != compVal) {
                        Log_Log("IPv4", "Header checksum fails (%04x != %04x)", hdrVal, compVal);
                        return ;
@@ -311,21 +311,17 @@ Uint32 IPv4_Netmask(int FixedBits)
  * 
  * One's complement sum of all 16-bit words (bitwise inverted)
  */
-Uint16 IPv4_Checksum(const void *Buf, int Size)
+Uint16 IPv4_Checksum(const Uint16 *Buf, int WordCount)
 {
        Uint32  sum = 0;
-       const Uint16    *arr = Buf;
         int    i;
        
        // Sum all whole words
-       for(i = 0; i < Size/2; i++ )
+       for(i = 0; i < WordCount; i++ )
        {
-               Uint16  val = ntohs(arr[i]);
+               Uint16  val = ntohs(Buf[i]);
                sum += val;
        }
-       // Add the tail word
-//     if( i*2 != Size )
-//             sum += arr[i]&0xFF;
        
        // Apply one's complement
        while (sum >> 16)
index 33507b2..563965c 100644 (file)
@@ -46,7 +46,7 @@ struct sIPv4Header
 
 // === FUNCTIONS ===
 extern int     IPv4_RegisterCallback(int ID, tIPCallback Callback);
-extern Uint16  IPv4_Checksum(const void *Buf, int Size);
+extern Uint16  IPv4_Checksum(const Uint16 *Buf, int WordCount);
 extern int     IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data);
 
 #endif
index 1f51e10..1600bb4 100644 (file)
@@ -92,7 +92,7 @@ void TCP_SendPacket( tTCPConnection *Conn, size_t Length, tTCPHeader *Data )
                memcpy( &buf[3], Data, Length );
                if(Length & 1)
                        ((Uint8*)buf)[12+Length] = 0;
-               Data->Checksum = htons( IPv4_Checksum( buf, buflen ) );
+               Data->Checksum = htons( IPv4_Checksum( (Uint16*)buf, buflen/2 ) );
                free(buf);
                IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data);
                break;
@@ -137,7 +137,7 @@ void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffe
        {
                Log_Log("TCP", "TCP_GetPacket: SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
                Debug_HexDump(
-                       "[TCP  ] Packet Data = ",
+                       "TCP_GetPacket: Packet Data = ",
                        (Uint8*)hdr + (hdr->DataOffset >> 4)*4,
                        Length - (hdr->DataOffset >> 4)*4
                        );
@@ -1076,8 +1076,7 @@ void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, void *Dat
        memcpy(packet->Options, Data, Length);
        
        Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
-       Debug_HexDump("[TCP     ] TCP_INT_SendDataPacket: Data = ",
-               Data, Length);
+       Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
        
        TCP_SendPacket( Connection, sizeof(tTCPHeader)+Length, packet );
        

UCC git Repository :: git.ucc.asn.au