1 | #include <libtrace.h> |
---|
2 | #include <err.h> |
---|
3 | #include <time.h> |
---|
4 | #include "libpacketdump.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <getopt.h> |
---|
8 | #include <netinet/ether.h> |
---|
9 | #include <netinet/in.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <inttypes.h> |
---|
12 | #include <dlfcn.h> |
---|
13 | #include <map> |
---|
14 | #include <string> |
---|
15 | #include <ctype.h> |
---|
16 | #include "libpacketdump.h" |
---|
17 | |
---|
18 | typedef void (*decode_t)(uint16_t type,char *packet,int len); |
---|
19 | |
---|
20 | static std::map<std::string,std::map<uint16_t,decode_t> > decoders; |
---|
21 | |
---|
22 | #define WIDTH 16 |
---|
23 | |
---|
24 | #ifndef DIRNAME |
---|
25 | #define DIRNAME "./" |
---|
26 | #warning "No DIRNAME set!" |
---|
27 | #endif |
---|
28 | |
---|
29 | void trace_dump_packet(struct libtrace_packet_t *packet) |
---|
30 | { |
---|
31 | time_t sec = (time_t)trace_get_seconds(packet); |
---|
32 | char *link=(char *)trace_get_link(packet); |
---|
33 | |
---|
34 | printf("%s",ctime(&sec)); |
---|
35 | decode_next(link,packet->size-(link-packet->buffer),"link",trace_get_link_type(packet)); |
---|
36 | } |
---|
37 | |
---|
38 | static void generic_decode(uint16_t type,char *packet, int len) { |
---|
39 | int i; |
---|
40 | printf(" Unknown Protocol: %i",type); |
---|
41 | for(i=0;i<len; /* Nothing */ ) { |
---|
42 | int j; |
---|
43 | printf("\n "); |
---|
44 | for(j=0;j<WIDTH;j++) { |
---|
45 | if (i+j<len) |
---|
46 | printf(" %02x",(unsigned char)packet[i+j]); |
---|
47 | else |
---|
48 | printf(" "); |
---|
49 | } |
---|
50 | printf(" "); |
---|
51 | for(j=0;j<WIDTH;j++) { |
---|
52 | if (i+j<len) |
---|
53 | if (isprint((unsigned char)packet[i+j])) |
---|
54 | printf("%c",(unsigned char)packet[i+j]); |
---|
55 | else |
---|
56 | printf("."); |
---|
57 | else |
---|
58 | printf(" "); |
---|
59 | } |
---|
60 | if (i+WIDTH>len) |
---|
61 | break; |
---|
62 | else |
---|
63 | i+=WIDTH; |
---|
64 | } |
---|
65 | printf("\n"); |
---|
66 | } |
---|
67 | |
---|
68 | void decode_next(char *packet,int len,char *proto_name,int type) |
---|
69 | { |
---|
70 | std::string sname(proto_name); |
---|
71 | if (decoders[sname].find(type)==decoders[sname].end()) { |
---|
72 | void *hdl; |
---|
73 | char name[1024]; |
---|
74 | snprintf(name,sizeof(name),"%s/%s_%i.so",DIRNAME,sname.c_str(),type); |
---|
75 | hdl = dlopen(name,RTLD_LAZY); |
---|
76 | if (!hdl) |
---|
77 | decoders[sname][type]=generic_decode; |
---|
78 | else { |
---|
79 | void *s=dlsym(hdl,"decode"); |
---|
80 | if (!s) { |
---|
81 | decoders[sname][type]=generic_decode; |
---|
82 | } |
---|
83 | else |
---|
84 | decoders[sname][type]=(decode_t)s; |
---|
85 | } |
---|
86 | } |
---|
87 | decoders[sname][type](type,packet,len); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|