This sniffs the network connection and outputs the packet contents

Dependencies:   mbed

Revision:
0:126d1599825c
diff -r 000000000000 -r 126d1599825c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Mar 01 19:04:18 2013 +0000
@@ -0,0 +1,88 @@
+#include "mbed.h"
+#include <string>
+DigitalOut myled(LED1);
+
+Ethernet eth;
+
+string Protocol(int num){
+string s;
+switch(num){
+    case 17 : 
+    s="UDP";
+    break;
+    case 1 : 
+    s="ICMP";
+    break;
+    case 6 : 
+    s="TCP";
+    break;
+    case 89 : 
+    s="OSPF";
+    break;
+    case 2 : 
+    s="IGMP";
+    break;
+    case 103 : 
+    s="PIM";
+    break;
+    default :
+    s="None";
+    }
+
+
+
+return s;
+
+}
+ 
+int main() {
+    int number=0;
+    int type;
+    char ver, prot;
+    char hlen;
+    char servtype;
+    short tot_len,ident;
+    string k;
+    myled=1;
+    char buf[0x600];
+    
+    int* length=(int *)(buf+12);
+ 
+    while(1) {
+        myled=!myled;
+        number++;
+        int size = eth.receive();
+        if(size > 0) {
+            eth.read(buf, size);
+            type=(buf[12]<<8|buf[13]);
+            ver= buf[14]>>4;
+            hlen=buf[14]&0xF;
+            servtype=buf[15];
+            tot_len=(buf[16]<<8|buf[17]);
+            ident=(buf[18]<<8|buf[19]);
+            prot=buf[23];
+            
+                    //ip packet starts at 14
+                    // the IP address starts at 
+            
+            if(type==0x800){
+                    printf("Number:%i\r\n",number);
+            printf("Destination:  %02X:%02X:%02X:%02X:%02X:%02X\r\n",
+                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
+            printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
+                    buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
+            printf("Type %02X\r\n",type);
+                    printf("Length:%i\r\n",size);
+                    k=Protocol(prot);
+            printf("Protocol  %i: %s\r\n",prot,k);
+            printf("IP Source:  %i.%i.%i.%i\r\n",
+                    buf[26], buf[27], buf[28], buf[29]);
+            printf("IP Destination : %i.%i.%i.%i\r\n",
+                    buf[30], buf[31], buf[32], buf[33]);}
+            
+                    
+        }
+ 
+        wait(1);
+    }
+}