This sniffs the network connection and outputs the packet contents

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
westsand
Date:
Fri Mar 01 19:04:18 2013 +0000
Commit message:
Working Serial output

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
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);
+    }
+}
diff -r 000000000000 -r 126d1599825c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 01 19:04:18 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/3d0ef94e36ec
\ No newline at end of file