RealtimeCompLab2
Dependencies: mbed
Fork of PPP-Blinky by
Diff: main.cpp
- Revision:
- 76:00e208cceb8b
- Parent:
- 75:0d513869231f
- Child:
- 77:abf92baebb42
--- a/main.cpp Tue Jul 04 18:35:43 2017 +0000 +++ b/main.cpp Wed Jul 05 20:05:42 2017 +0000 @@ -331,26 +331,28 @@ } } -unsigned int dataCheckSum(char * ptr, int len) +unsigned int dataCheckSum(unsigned char * ptr, int len) { + unsigned int sum=0; - int placeHolder; + unsigned char placeHolder; if (len&1) { - placeHolder = ptr[len-1]; // when length is odd stuff in a zero byte - ptr[len-1]=0; + placeHolder = ptr[len]; // when length is odd stuff in a zero byte + ptr[len]=0; } for (int i=0; i<len/2; i++) { - int hi = *ptr; + unsigned int hi = *ptr; ptr++; - int lo = *ptr; + unsigned int lo = *ptr; ptr++; - int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); + unsigned int val = ( (hi<<8) | lo ); sum = sum + val; } - sum = sum + (sum>>16); if (len&1) { - ptr[len-1] = placeHolder; // restore the last byte for odd lengths + ptr[len] = placeHolder; // restore the last byte for odd lengths } + sum = (sum & 0xffff) + (sum>>16); + sum = (sum & 0xffff) + (sum>>16); // sum one more time to catch any carry from the carry return ~sum; } @@ -409,9 +411,9 @@ icmpSum[0]=0; icmpSum[1]=0; // zero the checksum for recalculation int icmpLength = packetLength - headerSizeIP; // length of ICMP data portion - int sum = dataCheckSum( icmpType, icmpLength); // this checksum on icmp data portion - icmpSum[0]=sum>>8; - icmpSum[1]=sum; // new checksum for ICMP data portion + unsigned int sum = dataCheckSum( (unsigned char *)icmpType, icmpLength); // this checksum on icmp data portion + icmpSum[0]=(sum>>8)&0xff; + icmpSum[1]=(sum )&0xff; // new checksum for ICMP data portion int printSize = icmpLength-8; // exclude size of icmp header char * icmpData = icmpType+8; // the actual payload data is after the header @@ -456,7 +458,7 @@ char * flags = ipPkt+6; // 2 bits char * ttl = ipPkt+8; // 1 byte char * protocol = ipPkt+9; // 1 byte - char * headercheck= ipPkt+10; // 2 bytes + unsigned char * headercheck= ipPkt+10; // 2 bytes #endif char * srcAdr = ipPkt+12; // 4 bytes char * dstAdr = ipPkt+16; // 4 bytes = total of 20 bytes @@ -471,7 +473,7 @@ int flagsIP = flags[0]>>14&3; int ttlIP = ttl[0]; int protocolIP = protocol[0]; - int checksumIP = (headercheck[0]<<8)|headercheck[1]; + unsigned int checksumIP = (headercheck[0]<<8)|headercheck[1]; #endif char srcIP [16]; @@ -697,7 +699,7 @@ checksumtcp[0]=0; checksumtcp[1]=0; - unsigned int pseudoHeaderSum=dataCheckSum(pseudoHeader,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header + unsigned int pseudoHeaderSum=dataCheckSum((unsigned char *)pseudoHeader,tcpSize+12); // calculate the TCP checksum starting at the pseudo-header checksumtcp[0]=pseudoHeaderSum>>8; checksumtcp[1]=pseudoHeaderSum; memcpy( s-12, tempHold, 12); // restore the 12 bytes that the pseudo-header overwrote