| 1 | #include <stdio.h> |
|---|
| 2 | #include "libpacketdump.h" |
|---|
| 3 | |
|---|
| 4 | #define LE(lhs,n) \ |
|---|
| 5 | do { \ |
|---|
| 6 | uint64_t num=0; \ |
|---|
| 7 | int size=0; \ |
|---|
| 8 | if ((offset+n)>len*8) return; \ |
|---|
| 9 | if (n>16) { \ |
|---|
| 10 | num=htonl(*(uint32_t*)(packet+offset/8));\ |
|---|
| 11 | size = 32;\ |
|---|
| 12 | } else if (n>8) { \ |
|---|
| 13 | num=htons(*(uint16_t*)(packet+offset/8));\ |
|---|
| 14 | size = 16; \ |
|---|
| 15 | } else { \ |
|---|
| 16 | num=*(uint8_t*)(packet+offset/8); \ |
|---|
| 17 | size = 8; \ |
|---|
| 18 | } \ |
|---|
| 19 | num=num>>(size - (n + (offset % 8))); \ |
|---|
| 20 | offset+=n; \ |
|---|
| 21 | lhs=num&((1<<(n))-1); \ |
|---|
| 22 | } while(0) |
|---|
| 23 | |
|---|
| 24 | void decode(int link_type,const char *packet,unsigned len) |
|---|
| 25 | { |
|---|
| 26 | int offset=0; |
|---|
| 27 | int value; |
|---|
| 28 | int more = 0; |
|---|
| 29 | LE(value,20); printf(" MPLS: Label: %d\n",value); |
|---|
| 30 | LE(value,3); printf(" MPLS: Class of service: %d\n",value); |
|---|
| 31 | LE(value,1); printf(" MPLS: Stack: %s\n",value?"Last" :"More"); |
|---|
| 32 | if (value == 0) more = 1; |
|---|
| 33 | LE(value,8); printf(" MPLS: TTL: %d\n",value); |
|---|
| 34 | |
|---|
| 35 | /* MPLS doesn't say what it's encapsulating, so we make an educated |
|---|
| 36 | * guess and pray. |
|---|
| 37 | */ |
|---|
| 38 | if (more) |
|---|
| 39 | decode_next(packet+offset/8,len-4,"eth",0x8847); |
|---|
| 40 | else if ((*(packet+4)&0xF0) == 0x40) |
|---|
| 41 | decode_next(packet+offset/8,len-4,"eth",0x0800); |
|---|
| 42 | else if ((*(packet+4)&0xF0) == 0x60) |
|---|
| 43 | decode_next(packet+offset/8,len-4,"eth",0x86DD); |
|---|
| 44 | else |
|---|
| 45 | decode_next(packet+offset/8,len-4,"link",1); |
|---|
| 46 | |
|---|
| 47 | return; |
|---|
| 48 | } |
|---|