|
From: <ljs...@us...> - 2007-05-25 14:28:36
|
Revision: 410
http://svn.sourceforge.net/cadcdev/?rev=410&view=rev
Author: ljsebald
Date: 2007-05-25 07:28:34 -0700 (Fri, 25 May 2007)
Log Message:
-----------
Made net_ipv4_checksum accept the number of bytes in the data to be checksummed rather than the number of words.
Modified Paths:
--------------
kos/kernel/net/net_icmp.c
kos/kernel/net/net_ipv4.c
kos/kernel/net/net_ipv4.h
kos/kernel/net/net_udp.c
Modified: kos/kernel/net/net_icmp.c
===================================================================
--- kos/kernel/net/net_icmp.c 2007-05-25 03:14:34 UTC (rev 409)
+++ kos/kernel/net/net_icmp.c 2007-05-25 14:28:34 UTC (rev 410)
@@ -102,13 +102,13 @@
/* Recompute the IP header checksum */
ip->checksum = 0;
- ip->checksum = net_ipv4_checksum((uint16*)ip,
- 2 * (ip->version_ihl & 0x0f));
+ ip->checksum = net_ipv4_checksum((uint8 *)ip,
+ 4 * (ip->version_ihl & 0x0f));
/* Recompute the ICMP header checksum */
icmp->checksum = 0;
- icmp->checksum = net_ipv4_checksum((uint16*)icmp, ntohs(ip->length) /
- 2 - 2 * (ip->version_ihl & 0x0f));
+ icmp->checksum = net_ipv4_checksum((uint8 *)icmp, ntohs(ip->length) -
+ 4 * (ip->version_ihl & 0x0f));
/* Send it */
memcpy(pktbuf, ip, 20);
@@ -129,9 +129,8 @@
i = icmp->checksum;
icmp->checksum = 0;
memcpy(pktbuf, icmp, ntohs(ip->length) - 4 * (ip->version_ihl & 0x0f));
- icmp->checksum = net_ipv4_checksum((uint16*)pktbuf,
- (ntohs(ip->length) + 1) / 2 -
- 2 * (ip->version_ihl & 0x0f));
+ icmp->checksum = net_ipv4_checksum(pktbuf, (ntohs(ip->length) + 1) -
+ 4 * (ip->version_ihl & 0x0f));
if (i != icmp->checksum) {
dbglog(DBG_KDEBUG, "net_icmp: icmp with invalid checksum\n");
@@ -198,11 +197,10 @@
ip.dest = htonl(net_ipv4_address(ipaddr));
/* Compute the ICMP Checksum */
- icmp->checksum = net_ipv4_checksum((uint16*)databuf,
- (sizeof(icmp_hdr_t) + size) / 2);
+ icmp->checksum = net_ipv4_checksum(databuf, sizeof(icmp_hdr_t) + size);
/* Compute the IP Checksum */
- ip.checksum = net_ipv4_checksum((uint16*)&ip, sizeof(ip_hdr_t) / 2);
+ ip.checksum = net_ipv4_checksum((uint8 *)&ip, sizeof(ip_hdr_t));
newping = (struct __ping_pkt*) malloc(sizeof(struct __ping_pkt));
newping->data = (uint8 *)malloc(size);
Modified: kos/kernel/net/net_ipv4.c
===================================================================
--- kos/kernel/net/net_ipv4.c 2007-05-25 03:14:34 UTC (rev 409)
+++ kos/kernel/net/net_ipv4.c 2007-05-25 14:28:34 UTC (rev 410)
@@ -19,20 +19,45 @@
#include "net_udp.h"
/* Perform an IP-style checksum on a block of data */
-uint16 net_ipv4_checksum(const uint16 *data, int words) {
- uint32 sum;
+uint16 net_ipv4_checksum(const uint8 *data, int bytes) {
+ uint32 sum = 0;
int i;
- sum = 0;
- for (i=0; i<words; i++) {
- sum += data[i];
- if (sum & 0xffff0000) {
- sum &= 0xffff;
- sum++;
- }
- }
+ /* Make sure we don't do any unaligned memory accesses */
+ if(((uint32)data) & 0x01) {
+ for(i = 0; i < bytes; i += 2) {
+ sum += (data[i]) | (data[i + 1] << 8);
- return ~(sum & 0xffff);
+ if(sum & 0xFFFF0000) {
+ sum &= 0xFFFF;
+ ++sum;
+ }
+ }
+ }
+ else {
+ uint16 *ptr = (uint16 *)data;
+
+ for(i = 0; i < (bytes >> 1); ++i) {
+ sum += ptr[i];
+
+ if(sum & 0xFFFF0000) {
+ sum &= 0xFFFF;
+ ++sum;
+ }
+ }
+ }
+
+ /* Handle the last byte, if we have an odd byte count */
+ if(bytes & 0x01) {
+ sum += data[bytes - 1];
+
+ if(sum & 0xFFFF0000) {
+ sum &= 0xFFFF;
+ ++sum;
+ }
+ }
+
+ return sum ^ 0xFFFF;
}
/* Determine if a given IP is in the current network */
@@ -130,7 +155,7 @@
/* Check ip header checksum */
i = ip->checksum;
ip->checksum = 0;
- ip->checksum = net_ipv4_checksum((uint16*)ip, 2 *
+ ip->checksum = net_ipv4_checksum((uint8 *)ip, 4 *
(ip->version_ihl & 0x0f));
if(i != ip->checksum) {
Modified: kos/kernel/net/net_ipv4.h
===================================================================
--- kos/kernel/net/net_ipv4.h 2007-05-25 03:14:34 UTC (rev 409)
+++ kos/kernel/net/net_ipv4.h 2007-05-25 14:28:34 UTC (rev 410)
@@ -43,7 +43,7 @@
} ip_pseudo_hdr_t;
#undef packed
-uint16 net_ipv4_checksum(const uint16 *data, int words);
+uint16 net_ipv4_checksum(const uint8 *data, int bytes);
int net_ipv4_send_packet(netif_t *net, ip_hdr_t *hdr, const uint8 *data, int size);
int net_ipv4_input(netif_t *src, const uint8 *pkt, int pktsize);
Modified: kos/kernel/net/net_udp.c
===================================================================
--- kos/kernel/net/net_udp.c 2007-05-25 03:14:34 UTC (rev 409)
+++ kos/kernel/net/net_udp.c 2007-05-25 14:28:34 UTC (rev 410)
@@ -537,12 +537,12 @@
ps->data[size - sizeof(udp_hdr_t)] = 0;
checksum = ps->checksum;
ps->checksum = 0;
- ps->checksum = net_ipv4_checksum((uint16 *) buf, (size + 13) >> 1);
+ ps->checksum = net_ipv4_checksum(buf, size + 13);
}
else {
checksum = ps->checksum;
ps->checksum = 0;
- ps->checksum = net_ipv4_checksum((uint16 *) buf, (size + 12) >> 1);
+ ps->checksum = net_ipv4_checksum(buf, size + 12);
}
if(checksum != ps->checksum) {
@@ -631,12 +631,10 @@
/* Compute the UDP checksum */
if(size & 0x01) {
- ps->checksum = net_ipv4_checksum((uint16 *) buf,
- (size + 13) >> 1);
+ ps->checksum = net_ipv4_checksum(buf, size + 13);
}
else {
- ps->checksum = net_ipv4_checksum((uint16 *) buf,
- (size + 12) >> 1);
+ ps->checksum = net_ipv4_checksum(buf, size + 12);
}
/* Fill in the IPv4 Header */
@@ -652,7 +650,7 @@
ip.dest = ps->dst_addr;
/* Compute the IPv4 checksum */
- ip.checksum = net_ipv4_checksum((uint16 *) &ip, sizeof(ip_hdr_t) >> 1);
+ ip.checksum = net_ipv4_checksum((uint8 *) &ip, sizeof(ip_hdr_t));
/* send it away.... */
if(net_ipv4_send_packet(net, &ip, buf + 12, size)) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|