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