1 | #include <stdio.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <dlfcn.h> |
---|
4 | #include <map> |
---|
5 | #include "libpacketdump.h" |
---|
6 | #include <netinet/ip_icmp.h> |
---|
7 | #include <netinet/in.h> |
---|
8 | |
---|
9 | #define STRUCT icmp |
---|
10 | |
---|
11 | #define SAFE(x) \ |
---|
12 | ((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x))) |
---|
13 | #define DISPLAY_EXP(x,fmt,exp) \ |
---|
14 | if (SAFE(x)) \ |
---|
15 | printf(fmt,exp); \ |
---|
16 | else \ |
---|
17 | return; |
---|
18 | |
---|
19 | #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x) |
---|
20 | |
---|
21 | #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x)) |
---|
22 | #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x)) |
---|
23 | #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x)) |
---|
24 | |
---|
25 | static char *unreach_types[]={ |
---|
26 | "Destination Network Unreachable", |
---|
27 | "Destination Host Unreachable", |
---|
28 | "Destination Protocol Unreachable", |
---|
29 | "Destination Port Unreachable", |
---|
30 | "Fragmentation Required And Dont Fragment Set", |
---|
31 | "Source Route Failed", |
---|
32 | "Destination Network Unknown", |
---|
33 | "Destination Host Unknown", |
---|
34 | "Source Host Isolated", |
---|
35 | "Destination Network Administratively Prohibited", |
---|
36 | "Destination Host Administratively Prohibited", |
---|
37 | "Destination Network Unreachable For Type Of Service", |
---|
38 | "Destination Host Unreachable For Type Of Service", |
---|
39 | "Communication Administratively Prohibited", |
---|
40 | "Host Precedence Violation", |
---|
41 | "Precedence Cutoff In Effect", |
---|
42 | }; |
---|
43 | |
---|
44 | extern "C" |
---|
45 | void decode(int link_type,char *packet,int len) |
---|
46 | { |
---|
47 | struct icmphdr *icmp = (struct icmphdr*)packet; |
---|
48 | if (len<1) |
---|
49 | return; |
---|
50 | printf(" ICMP:"); |
---|
51 | switch(icmp->type) { |
---|
52 | case 0: |
---|
53 | printf(" Type: 0 (ICMP Echo Reply)\n"); |
---|
54 | break; |
---|
55 | case 3: |
---|
56 | printf(" Type: 3 (ICMP Destination Unreachable)\n"); |
---|
57 | if (len<2) |
---|
58 | return; |
---|
59 | if (icmp->code<sizeof(unreach_types)) { |
---|
60 | printf(" ICMP: Code: %i (%s)\n",icmp->code, |
---|
61 | unreach_types[icmp->code]); |
---|
62 | } |
---|
63 | else { |
---|
64 | printf(" ICMP: Code: %i (Unknown)\n",icmp->code); |
---|
65 | } |
---|
66 | // Pretend that this was just passed up from ethernet |
---|
67 | decode_next(packet+8,len-8, |
---|
68 | "eth",0x0800); |
---|
69 | |
---|
70 | break; |
---|
71 | case 8: |
---|
72 | printf(" Type: 8 (ICMP Echo Request)\n"); |
---|
73 | break; |
---|
74 | case 11: |
---|
75 | printf(" Type: 11 (ICMP TTL Exceeded)\n"); |
---|
76 | decode_next(packet+8,len-8, |
---|
77 | "eth",0x0800); |
---|
78 | break; |
---|
79 | default: |
---|
80 | printf(" Type: %i (Unknown)\n",icmp->type); |
---|
81 | break; |
---|
82 | |
---|
83 | } |
---|
84 | return; |
---|
85 | } |
---|