A simple Ethernet sniffer. A demonstration how to use the Ethernet interface.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
rolf
Date:
Fri Sep 04 12:24:03 2009 +0000
Commit message:

Changed in this revision

hexview.h Show annotated file Show diff for this revision Revisions of this file
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 29e2df9de9f1 hexview.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hexview.h	Fri Sep 04 12:24:03 2009 +0000
@@ -0,0 +1,29 @@
+/* Function: hexview
+ *  Prints an array of char to stdout in hex.
+ *  The data is grouped in two 8 byte groups per line.
+ *  Each byte is displayed as 2 hex digits and every 
+ *  line starts with the address of the first byte.
+ *
+ *  There is no text view of a line.
+ *
+ * Variables:
+ *  buffer - The array to display.
+ *  size - The length of buffer.
+ */
+inline void hexview(char *buffer, unsigned int size) {
+    for(int i = 0; i < size; ++i) {
+        if((i%16)!=0) {
+            printf(" ");
+        } else {
+            printf("%04X:  ", (i));
+        }
+        printf("%02hhx", buffer[i]);
+        if((i%16) ==  7) { 
+            printf(" ");
+        }
+        if((i%16) == 15) {
+            printf("\n");
+        }
+    }
+    printf("\n\n\n");
+}
\ No newline at end of file
diff -r 000000000000 -r 29e2df9de9f1 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Sep 04 12:24:03 2009 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "Ethernet.h"
+#include "hexview.h"
+
+using namespace mbed;
+
+DigitalOut led4(LED4);
+
+Ethernet eth;
+
+/*
+__packed struct eth {
+    unsigned char    dst[6];
+    unsigned char    src[6];
+    unsigned short   type;
+};
+
+void show(char *buffer, int size) {
+    eth *e = (eth *)buffer;
+    printf("Destination:  %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
+            e->dst[0], e->dst[1], e->dst[2], e->dst[3], e->dst[4], e->dst[5]);
+    printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
+            e->src[0], e->src[1], e->src[2], e->src[3], e->src[4], e->src[5]);
+  
+    printf("Type %hd\n", eth->type);
+    hexview(buffer, size);
+}
+*/
+
+unsigned short htons(unsigned short n) {               // Host short to network shor
+  return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);      // Byte swapping
+}
+
+void show(char *buf, int size) {
+    printf("Destination:  %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
+            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
+    printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
+            buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
+  
+    printf("Type %hd\n", htons((short)buf[12]));
+    
+    hexview(buf, size);
+}
+
+
+int main() {
+    char buffer[0x600];
+    int size = 0;
+    
+    while(1) {
+        if((size = eth.receive()) != 0) {
+            eth.read(buffer, size);
+            show(buffer, size);
+        }
+        
+        led4 = 1;
+        wait(0.2);
+        led4 = 0;
+        wait(0.2);
+    }
+}
diff -r 000000000000 -r 29e2df9de9f1 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Sep 04 12:24:03 2009 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/f63353af7be8