Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
13:d882b8a042b4
Parent:
12:db0dc91f0231
Child:
14:c65831c25aaa
--- a/main.cpp	Sun Jan 01 01:20:33 2017 +0000
+++ b/main.cpp	Sun Jan 01 02:09:18 2017 +0000
@@ -22,7 +22,7 @@
 DigitalOut led1(LED1);
 
 #define FRAME_7E (0x7e)
-#define BUFLEN (1<<12)
+#define BUFLEN (1<<13)
 char rxbuf[BUFLEN];
 char frbuf[3000]; // buffer for ppp frame
 
@@ -175,8 +175,8 @@
     xx.printf("UDP %d.%d.%d.%d:%d ", srcIP[0],srcIP[1],srcIP[2],srcIP[3],srcPort);
     xx.printf("%d.%d.%d.%d:%d ",    dstIP[1],dstIP[1],dstIP[1],dstIP[1],dstPort);
     xx.printf("Len %d ", udpLength);
-    int printSize = udpLength > 20 ? 20 : udpLength;
-    for (int i=0; i<printSize; i++) { xx.putc( udpInf[i]); } // printf first 20 characters of the data
+    int printSize = udpLength; if (printSize > 20) printSize = 20; // print only first 20 characters
+    for (int i=0; i<printSize; i++) { char ch = udpInf[i]; if (ch>31 && ch<127) xx.putc(ch); else xx.putc('?'); } 
     xx.printf("\n");
 }
 
@@ -215,15 +215,15 @@
     int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length
     int headerSize = (( ipPkt[0]&0xf)*4);
     char * icmpType = ipPkt + headerSize; // icmp data start
-    char * icmpData = icmpType; // icmp packet type
-    char * icmpSum = icmpData+2; // icmp checksum
+    char * icmpSum = icmpType+2; // icmp checksum
+
     #define ICMP_TYPE_PING_REQUEST 8
     if ( icmpType[0] == ICMP_TYPE_PING_REQUEST ) { 
-        xx.printf("ICMP PING\n");
         char * ipTTL = ipPkt+8; // time to live
         ipTTL[0]--; // decrement time to live
         char * srcAdr = ipPkt+12;
         char * dstAdr = ipPkt+16;
+        xx.printf("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]);
         char src[4]; char dst[4];
         memcpy(src, srcAdr,4);
         memcpy(dst, dstAdr,4);
@@ -236,9 +236,15 @@
         icmpType[0]=ICMP_TYPE_ECHO_REPLY; // icmp echo replay
         icmpSum[0]=0; icmpSum[1]=0; // zero the checksum for recalculation
         int dataLength = packetLength - headerSize; // length of ICMP data portion
-        int sum = dataCheckSum( icmpData, dataLength); // new data portion checksum
+        int sum = dataCheckSum( icmpType, dataLength); // this checksum on icmp data portion
         icmpSum[0]=sum>>8; icmpSum[1]=sum; // new checksum for ICMP data portion
-        sendFrame();
+        sendFrame(); // reply to the ping
+        
+        int printSize = dataLength-8; // exclude size of icmp header
+        char * icmpData = icmpType+8; // the actual data is after the header
+        if (printSize > 20) printSize = 20; // print only first 20 characters
+        for (int i=0; i<printSize; i++) { char ch = icmpData[i]; if (ch>31 && ch<127) xx.putc(ch); else xx.putc('?'); } 
+        xx.putc('\n');
     } else {
         xx.printf("ICMP type=%d \n", icmpType[0]); 
     }