1 | #include <stdio.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <dlfcn.h> |
---|
4 | #include <map> |
---|
5 | #include "libpacketdump.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 | |
---|
27 | extern "C" |
---|
28 | void decode(int link_type,char *packet,int len) |
---|
29 | { |
---|
30 | struct udphdr *udp = (struct udphdr*)packet; |
---|
31 | printf(" UDP:"); |
---|
32 | if (SAFE(source)) { |
---|
33 | struct servent *ent=getservbyport(udp->source,"udp"); |
---|
34 | if(ent) { |
---|
35 | printf(" Source %i (%s)",htons(udp->source),ent->s_name); |
---|
36 | } else { |
---|
37 | printf(" Source %i",htons(udp->source)); |
---|
38 | } |
---|
39 | } |
---|
40 | else { |
---|
41 | printf("\n"); |
---|
42 | return; |
---|
43 | } |
---|
44 | if (SAFE(dest)) { |
---|
45 | struct servent *ent=getservbyport(udp->dest,"udp"); |
---|
46 | if(ent) { |
---|
47 | printf(" Dest %i (%s)",htons(udp->dest),ent->s_name); |
---|
48 | } else { |
---|
49 | printf(" Dest %i",htons(udp->dest)); |
---|
50 | } |
---|
51 | } |
---|
52 | else { |
---|
53 | printf("\n"); |
---|
54 | return; |
---|
55 | } |
---|
56 | printf("\n UDP:"); |
---|
57 | DISPLAYS(len," Len %u"); |
---|
58 | DISPLAYS(check," Checksum %u"); |
---|
59 | printf("\n"); |
---|
60 | if (htons(udp->source) < htons(udp->dest)) |
---|
61 | decode_next(packet+sizeof(*udp),len-sizeof(*udp),"udp",htons(udp->source)); |
---|
62 | else |
---|
63 | decode_next(packet+sizeof(*udp),len-sizeof(*udp),"udp",htons(udp->dest)); |
---|
64 | return; |
---|
65 | } |
---|