This sniffs the network connection and outputs the packet contents

Dependencies:   mbed

Committer:
westsand
Date:
Fri Mar 01 19:04:18 2013 +0000
Revision:
0:126d1599825c
Working Serial output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
westsand 0:126d1599825c 1 #include "mbed.h"
westsand 0:126d1599825c 2 #include <string>
westsand 0:126d1599825c 3 DigitalOut myled(LED1);
westsand 0:126d1599825c 4
westsand 0:126d1599825c 5 Ethernet eth;
westsand 0:126d1599825c 6
westsand 0:126d1599825c 7 string Protocol(int num){
westsand 0:126d1599825c 8 string s;
westsand 0:126d1599825c 9 switch(num){
westsand 0:126d1599825c 10 case 17 :
westsand 0:126d1599825c 11 s="UDP";
westsand 0:126d1599825c 12 break;
westsand 0:126d1599825c 13 case 1 :
westsand 0:126d1599825c 14 s="ICMP";
westsand 0:126d1599825c 15 break;
westsand 0:126d1599825c 16 case 6 :
westsand 0:126d1599825c 17 s="TCP";
westsand 0:126d1599825c 18 break;
westsand 0:126d1599825c 19 case 89 :
westsand 0:126d1599825c 20 s="OSPF";
westsand 0:126d1599825c 21 break;
westsand 0:126d1599825c 22 case 2 :
westsand 0:126d1599825c 23 s="IGMP";
westsand 0:126d1599825c 24 break;
westsand 0:126d1599825c 25 case 103 :
westsand 0:126d1599825c 26 s="PIM";
westsand 0:126d1599825c 27 break;
westsand 0:126d1599825c 28 default :
westsand 0:126d1599825c 29 s="None";
westsand 0:126d1599825c 30 }
westsand 0:126d1599825c 31
westsand 0:126d1599825c 32
westsand 0:126d1599825c 33
westsand 0:126d1599825c 34 return s;
westsand 0:126d1599825c 35
westsand 0:126d1599825c 36 }
westsand 0:126d1599825c 37
westsand 0:126d1599825c 38 int main() {
westsand 0:126d1599825c 39 int number=0;
westsand 0:126d1599825c 40 int type;
westsand 0:126d1599825c 41 char ver, prot;
westsand 0:126d1599825c 42 char hlen;
westsand 0:126d1599825c 43 char servtype;
westsand 0:126d1599825c 44 short tot_len,ident;
westsand 0:126d1599825c 45 string k;
westsand 0:126d1599825c 46 myled=1;
westsand 0:126d1599825c 47 char buf[0x600];
westsand 0:126d1599825c 48
westsand 0:126d1599825c 49 int* length=(int *)(buf+12);
westsand 0:126d1599825c 50
westsand 0:126d1599825c 51 while(1) {
westsand 0:126d1599825c 52 myled=!myled;
westsand 0:126d1599825c 53 number++;
westsand 0:126d1599825c 54 int size = eth.receive();
westsand 0:126d1599825c 55 if(size > 0) {
westsand 0:126d1599825c 56 eth.read(buf, size);
westsand 0:126d1599825c 57 type=(buf[12]<<8|buf[13]);
westsand 0:126d1599825c 58 ver= buf[14]>>4;
westsand 0:126d1599825c 59 hlen=buf[14]&0xF;
westsand 0:126d1599825c 60 servtype=buf[15];
westsand 0:126d1599825c 61 tot_len=(buf[16]<<8|buf[17]);
westsand 0:126d1599825c 62 ident=(buf[18]<<8|buf[19]);
westsand 0:126d1599825c 63 prot=buf[23];
westsand 0:126d1599825c 64
westsand 0:126d1599825c 65 //ip packet starts at 14
westsand 0:126d1599825c 66 // the IP address starts at
westsand 0:126d1599825c 67
westsand 0:126d1599825c 68 if(type==0x800){
westsand 0:126d1599825c 69 printf("Number:%i\r\n",number);
westsand 0:126d1599825c 70 printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
westsand 0:126d1599825c 71 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
westsand 0:126d1599825c 72 printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
westsand 0:126d1599825c 73 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
westsand 0:126d1599825c 74 printf("Type %02X\r\n",type);
westsand 0:126d1599825c 75 printf("Length:%i\r\n",size);
westsand 0:126d1599825c 76 k=Protocol(prot);
westsand 0:126d1599825c 77 printf("Protocol %i: %s\r\n",prot,k);
westsand 0:126d1599825c 78 printf("IP Source: %i.%i.%i.%i\r\n",
westsand 0:126d1599825c 79 buf[26], buf[27], buf[28], buf[29]);
westsand 0:126d1599825c 80 printf("IP Destination : %i.%i.%i.%i\r\n",
westsand 0:126d1599825c 81 buf[30], buf[31], buf[32], buf[33]);}
westsand 0:126d1599825c 82
westsand 0:126d1599825c 83
westsand 0:126d1599825c 84 }
westsand 0:126d1599825c 85
westsand 0:126d1599825c 86 wait(1);
westsand 0:126d1599825c 87 }
westsand 0:126d1599825c 88 }