1 #include <stdio.h>
2 #include <inttypes.h>
3 #include <dlfcn.h>
4 #include "libpacketdump.h"
5 
6 #define STRUCT icmp
7 
8 #define SAFE(x) \
9 	((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x)))
10 #define DISPLAY_EXP(x,fmt,exp) \
11 	if (SAFE(x)) \
12 		printf(fmt,exp); \
13 	else \
14 		return;
15 
16 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x)
17 
18 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x))
19 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x))
20 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x))
21 
22 static char *unreach_types[]={
23 	"Destination Network Unreachable",
24 	"Destination Host Unreachable",
25 	"Destination Protocol Unreachable",
26 	"Destination Port Unreachable",
27 	"Fragmentation Required And Dont Fragment Set",
28 	"Source Route Failed",
29 	"Destination Network Unknown",
30 	"Destination Host Unknown",
31 	"Source Host Isolated",
32 	"Destination Network Administratively Prohibited",
33 	"Destination Host Administratively Prohibited",
34 	"Destination Network Unreachable For Type Of Service",
35 	"Destination Host Unreachable For Type Of Service",
36 	"Communication Administratively Prohibited",
37 	"Host Precedence Violation",
38 	"Precedence Cutoff In Effect",
39 };
40 
decode(int link_type UNUSED,const char * packet,unsigned len)41 DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
42 {
43 	libtrace_icmp_t *icmp = (libtrace_icmp_t*)packet;
44 	if (len<1)
45 		return;
46 	printf(" ICMP:");
47 	switch(icmp->type) {
48 		case 0:
49 			printf(" Type: 0 (ICMP Echo Reply) Sequence: ");
50 			if (len < 4)
51 				printf("(Truncated)\n");
52 			else
53 				printf("%u\n", ntohs(icmp->un.echo.sequence));
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) Sequence: ");
73 			if (len < 4)
74 				printf("(Truncated)\n");
75 			else
76 				printf("%u\n", ntohs(icmp->un.echo.sequence));
77 			break;
78 		case 11:
79 			printf(" Type: 11 (ICMP TTL Exceeded)\n");
80 			decode_next(packet+8,len-8,
81 					"eth",0x0800);
82 			break;
83 		default:
84 			printf(" Type: %i (Unknown)\n",icmp->type);
85 			break;
86 
87 	}
88 	printf(" ICMP: Checksum: ");
89 	if (len < 8)
90 		printf("(Truncated)\n");
91 	else
92 		printf("%u\n", ntohs(icmp->checksum));
93 
94 
95 	return;
96 }
97