1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <stdio.h>
5 #include "libpacketdump.h"
6 #ifndef WIN32
7 	#include <netinet/in_systm.h>
8 #endif
9 #include <arpa/inet.h>
10 #include <netdb.h>
11 
12 #define DISPLAY_EXP(x,fmt,exp) \
13 	if ((unsigned int)len>=((char*)&ip->x-(char*)ip+sizeof(ip->x))) \
14 		printf(fmt,exp); \
15 	else \
16 		return;
17 
18 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,ip->x)
19 
20 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(ip->x))
21 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&ip->x))
22 
decode(int link_type UNUSED,const char * packet,unsigned len)23 DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
24 {
25 	libtrace_ip_t *ip = (libtrace_ip_t*)packet;
26 	if (len>=1) {
27 		printf(" IP: Header Len %i",ip->ip_hl*4);
28 		printf(" Ver %i",ip->ip_v);
29 	}
30 	//DISPLAY(ip_tos," TOS %02x")
31 	DISPLAY_EXP(ip_tos," DSCP %02x",ip->ip_tos >> 2)
32 	DISPLAY_EXP(ip_tos," ECN %x",ip->ip_tos & 0x2)
33 	DISPLAYS(ip_len," Total Length %i")
34 	printf("\n IP:");
35 	DISPLAYS(ip_id," Id %u");
36 
37 	if ((unsigned int)len >= ((char *)&ip->ip_ttl - (char *)ip - 2)) {
38 		printf(" Fragoff %i", ntohs(ip->ip_off) & 0x1FFF);
39 		if (ntohs(ip->ip_off) & 0x2000) printf(" MORE_FRAG");
40 		if (ntohs(ip->ip_off) & 0x4000) printf(" DONT_FRAG");
41 		if (ntohs(ip->ip_off) & 0x8000) printf(" RESV_FRAG");
42 	}
43 	//printf("\n IP:");
44 	DISPLAY(ip_ttl,"\n IP: TTL %i");
45 	if ((unsigned int)len>=((char*)&ip->ip_p-(char*)ip+sizeof(ip->ip_p))) {
46 		struct protoent *ent=getprotobynumber(ip->ip_p);
47 		if (ent) {
48 			printf(" Proto %i (%s)",ip->ip_p,ent->p_name);
49 		}
50 		else {
51 			printf(" Proto %i",ip->ip_p);
52 		}
53 	} else {
54 		printf("\n");
55 		return;
56 	}
57 	DISPLAYS(ip_sum," Checksum %i\n");
58 	DISPLAYIP(ip_src," IP: Source %s ");
59 	DISPLAYIP(ip_dst,"Destination %s\n");
60 	decode_next(packet+ip->ip_hl*4,len-ip->ip_hl*4,"ip",ip->ip_p);
61 	return;
62 }
63