1 #include <stdio.h>
2 #include <inttypes.h>
3 #include <dlfcn.h>
4 #include "libpacketdump.h"
5 #include "libtrace.h"
6 #include <netinet/udp.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
9 
10 #define STRUCT udp
11 
12 #define SAFE(x) \
13 	((unsigned int)len>=((char*)&STRUCT->x-(char*)STRUCT+sizeof(STRUCT->x)))
14 #define DISPLAY_EXP(x,fmt,exp) \
15 	if (SAFE(x)) \
16 		printf(fmt,exp); \
17 	else \
18 		return;
19 
20 #define DISPLAY(x,fmt) DISPLAY_EXP(x,fmt,STRUCT->x)
21 
22 #define DISPLAYS(x,fmt) DISPLAY_EXP(x,fmt,htons(STRUCT->x))
23 #define DISPLAYL(x,fmt) DISPLAY_EXP(x,fmt,htonl(STRUCT->x))
24 #define DISPLAYIP(x,fmt) DISPLAY_EXP(x,fmt,inet_ntoa(*(struct in_addr*)&STRUCT->x))
25 
26 
decode(int link_type UNUSED,const char * packet,unsigned len)27 DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len)
28 {
29 	struct libtrace_udp *udp = (struct libtrace_udp*)packet;
30 	printf(" UDP:");
31 	if (SAFE(source)) {
32 		struct servent *ent=getservbyport(udp->source,"udp");
33 		if(ent) {
34 			printf(" Source %i (%s)",htons(udp->source),ent->s_name);
35 		} else {
36 			printf(" Source %i",htons(udp->source));
37 		}
38 	}
39 	else {
40 		printf("\n");
41 		return;
42 	}
43 	if (SAFE(dest)) {
44 		struct servent *ent=getservbyport(udp->dest,"udp");
45 		if(ent) {
46 			printf(" Dest %i (%s)",htons(udp->dest),ent->s_name);
47 		} else {
48 			printf(" Dest %i",htons(udp->dest));
49 		}
50 	}
51 	else {
52 		printf("\n");
53 		return;
54 	}
55 	printf("\n UDP:");
56 	DISPLAYS(len," Len %u");
57 	DISPLAYS(check," Checksum %u");
58 	printf("\n");
59 	if (htons(udp->source) < htons(udp->dest))
60 		decode_next(packet+sizeof(*udp),len-sizeof(*udp),"udp",htons(udp->source));
61 	else
62 		decode_next(packet+sizeof(*udp),len-sizeof(*udp),"udp",htons(udp->dest));
63 	return;
64 }
65