RealtimeCompLab2
Dependencies: mbed
Fork of PPP-Blinky by
Diff: main.cpp
- Revision:
- 25:0b0450e1b08b
- Parent:
- 24:9f3db3bf7f9c
- Child:
- 26:11f4eb2663a7
--- a/main.cpp Mon Jan 02 18:43:15 2017 +0000 +++ b/main.cpp Mon Jan 02 19:36:21 2017 +0000 @@ -196,15 +196,14 @@ int dataCheckSum(char * ptr, int len) { int sum=0; int placeHolder; - if (len&1) { placeHolder = ptr[len-1]; ptr[len-1]=0; } // case where length is odd + if (len&1) { placeHolder = ptr[len-1]; ptr[len-1]=0; } // when length is odd zero stuff for (int i=0;i<len/2;i++) { - int hi = *ptr; ptr++; - int lo = *ptr; ptr++; + int hi = *ptr; ptr++; int lo = *ptr; ptr++; int val = ( lo & 0xff ) | ( (hi<<8) & 0xff00 ); sum = sum + val; } sum = sum + (sum>>16); - if (len&1) { ptr[len-1] = placeHolder; } // for odd lengths restore the last byte + if (len&1) { ptr[len-1] = placeHolder; } // restore the last byte for odd lengths return ~sum; } @@ -241,7 +240,8 @@ char * dstAdr = ipPkt+16; int icmpIdent = (icmpType[4]<<8)|icmpType[5]; int icmpSequence = (icmpType[6]<<8)|icmpType[7]; - debug(("ICMP PING %d.%d.%d.%d %d.%d.%d.%d Ident %04x Sequence %04d ", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3],dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3],icmpIdent,icmpSequence)); + debug(("ICMP PING %d.%d.%d.d %d.%d.%d.%d ", srcAdr[0],srcAdr[1],srcAdr[2],srcAdr[3],dstAdr[0],dstAdr[1],dstAdr[2],dstAdr[3])); + debug(("Ident %04x Sequence %04d ",icmpIdent,icmpSequence)); char src[4]; char dst[4]; memcpy(src, srcAdr,4); memcpy(dst, dstAdr,4); @@ -258,11 +258,13 @@ icmpSum[0]=sum>>8; icmpSum[1]=sum; // new checksum for ICMP data portion int printSize = icmpLength-8; // exclude size of icmp header - char * icmpData = icmpType+8; // the actual data is after the header - if (printSize > 10) printSize = 10; // print only first 20 characters - if (printSize >0) for (int i=0; i<printSize; i++) { char ch = icmpData[i]; if (ch>31 && ch<127) { debug(("%c",ch)); } else { debug(("%c",'_')); }} - debug(("%c",'\n')); + char * icmpData = icmpType+8; // the actual payload data is after the header + if (printSize > 10) printSize = 10; // print up to 20 characters + for (int i=0; i<printSize; i++) { char ch = icmpData[i]; if (ch>31 && ch<127) { debug(("%c",ch)); } else { debug(("_")); }} + debug(("\n")); + sendFrame(); // reply to the ping + } else { debug(("ICMP type=%d \n", icmpType[0])); }