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

Dependencies:   mbed

Committer:
rolf
Date:
Fri Sep 04 12:24:03 2009 +0000
Revision:
0:29e2df9de9f1

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rolf 0:29e2df9de9f1 1 #include "mbed.h"
rolf 0:29e2df9de9f1 2 #include "Ethernet.h"
rolf 0:29e2df9de9f1 3 #include "hexview.h"
rolf 0:29e2df9de9f1 4
rolf 0:29e2df9de9f1 5 using namespace mbed;
rolf 0:29e2df9de9f1 6
rolf 0:29e2df9de9f1 7 DigitalOut led4(LED4);
rolf 0:29e2df9de9f1 8
rolf 0:29e2df9de9f1 9 Ethernet eth;
rolf 0:29e2df9de9f1 10
rolf 0:29e2df9de9f1 11 /*
rolf 0:29e2df9de9f1 12 __packed struct eth {
rolf 0:29e2df9de9f1 13 unsigned char dst[6];
rolf 0:29e2df9de9f1 14 unsigned char src[6];
rolf 0:29e2df9de9f1 15 unsigned short type;
rolf 0:29e2df9de9f1 16 };
rolf 0:29e2df9de9f1 17
rolf 0:29e2df9de9f1 18 void show(char *buffer, int size) {
rolf 0:29e2df9de9f1 19 eth *e = (eth *)buffer;
rolf 0:29e2df9de9f1 20 printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
rolf 0:29e2df9de9f1 21 e->dst[0], e->dst[1], e->dst[2], e->dst[3], e->dst[4], e->dst[5]);
rolf 0:29e2df9de9f1 22 printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
rolf 0:29e2df9de9f1 23 e->src[0], e->src[1], e->src[2], e->src[3], e->src[4], e->src[5]);
rolf 0:29e2df9de9f1 24
rolf 0:29e2df9de9f1 25 printf("Type %hd\n", eth->type);
rolf 0:29e2df9de9f1 26 hexview(buffer, size);
rolf 0:29e2df9de9f1 27 }
rolf 0:29e2df9de9f1 28 */
rolf 0:29e2df9de9f1 29
rolf 0:29e2df9de9f1 30 unsigned short htons(unsigned short n) { // Host short to network shor
rolf 0:29e2df9de9f1 31 return ((n & 0xff) << 8) | ((n & 0xff00) >> 8); // Byte swapping
rolf 0:29e2df9de9f1 32 }
rolf 0:29e2df9de9f1 33
rolf 0:29e2df9de9f1 34 void show(char *buf, int size) {
rolf 0:29e2df9de9f1 35 printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
rolf 0:29e2df9de9f1 36 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
rolf 0:29e2df9de9f1 37 printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
rolf 0:29e2df9de9f1 38 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
rolf 0:29e2df9de9f1 39
rolf 0:29e2df9de9f1 40 printf("Type %hd\n", htons((short)buf[12]));
rolf 0:29e2df9de9f1 41
rolf 0:29e2df9de9f1 42 hexview(buf, size);
rolf 0:29e2df9de9f1 43 }
rolf 0:29e2df9de9f1 44
rolf 0:29e2df9de9f1 45
rolf 0:29e2df9de9f1 46 int main() {
rolf 0:29e2df9de9f1 47 char buffer[0x600];
rolf 0:29e2df9de9f1 48 int size = 0;
rolf 0:29e2df9de9f1 49
rolf 0:29e2df9de9f1 50 while(1) {
rolf 0:29e2df9de9f1 51 if((size = eth.receive()) != 0) {
rolf 0:29e2df9de9f1 52 eth.read(buffer, size);
rolf 0:29e2df9de9f1 53 show(buffer, size);
rolf 0:29e2df9de9f1 54 }
rolf 0:29e2df9de9f1 55
rolf 0:29e2df9de9f1 56 led4 = 1;
rolf 0:29e2df9de9f1 57 wait(0.2);
rolf 0:29e2df9de9f1 58 led4 = 0;
rolf 0:29e2df9de9f1 59 wait(0.2);
rolf 0:29e2df9de9f1 60 }
rolf 0:29e2df9de9f1 61 }