1 | #include <libtrace.h> |
---|
2 | #include <err.h> |
---|
3 | #include <time.h> |
---|
4 | #include "libpacketdump.h" |
---|
5 | #include "config.h" |
---|
6 | #include <stdio.h> |
---|
7 | #include <netdb.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <getopt.h> |
---|
10 | |
---|
11 | #include <sys/socket.h> |
---|
12 | |
---|
13 | #ifdef HAVE_NETINET_ETHER |
---|
14 | # include <netinet/ether.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | |
---|
18 | #ifdef HAVE_INTTYPES_H |
---|
19 | # include <inttypes.h> |
---|
20 | #else |
---|
21 | # error "Can't find inttypes.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #ifdef HAVE_LIMITS_H |
---|
25 | # include <limits.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #ifdef HAVE_SYS_LIMITS_H |
---|
29 | # include <sys/limits.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #ifdef HAVE_SYS_TYPES_H |
---|
33 | # include <sys/types.h> |
---|
34 | #endif |
---|
35 | #include <net/if.h> |
---|
36 | #include <netinet/in.h> |
---|
37 | #include <stdio.h> |
---|
38 | |
---|
39 | #include <net/if_arp.h> |
---|
40 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
41 | # include <netinet/if_ether.h> |
---|
42 | #endif |
---|
43 | #include <dlfcn.h> |
---|
44 | #include <map> |
---|
45 | #include <string> |
---|
46 | #include <ctype.h> |
---|
47 | #include "libpacketdump.h" |
---|
48 | |
---|
49 | typedef void (*decode_t)(uint16_t type,char *packet,int len); |
---|
50 | |
---|
51 | static std::map<std::string,std::map<uint16_t,decode_t> > decoders; |
---|
52 | |
---|
53 | #define WIDTH 16 |
---|
54 | |
---|
55 | #ifndef DIRNAME |
---|
56 | #define DIRNAME "./" |
---|
57 | #warning "No DIRNAME set!" |
---|
58 | #endif |
---|
59 | |
---|
60 | void trace_dump_packet(struct libtrace_packet_t *packet) |
---|
61 | { |
---|
62 | time_t sec = (time_t)trace_get_seconds(packet); |
---|
63 | char *link=(char *)trace_get_link(packet); |
---|
64 | |
---|
65 | printf("%s",ctime(&sec)); |
---|
66 | decode_next(link,packet->size-(link-packet->buffer),"link",trace_get_link_type(packet)); |
---|
67 | } |
---|
68 | |
---|
69 | static void generic_decode(uint16_t type,char *packet, int len) { |
---|
70 | int i; |
---|
71 | printf(" Unknown Protocol: %i",type); |
---|
72 | for(i=0;i<len; /* Nothing */ ) { |
---|
73 | int j; |
---|
74 | printf("\n "); |
---|
75 | for(j=0;j<WIDTH;j++) { |
---|
76 | if (i+j<len) |
---|
77 | printf(" %02x",(unsigned char)packet[i+j]); |
---|
78 | else |
---|
79 | printf(" "); |
---|
80 | } |
---|
81 | printf(" "); |
---|
82 | for(j=0;j<WIDTH;j++) { |
---|
83 | if (i+j<len) |
---|
84 | if (isprint((unsigned char)packet[i+j])) |
---|
85 | printf("%c",(unsigned char)packet[i+j]); |
---|
86 | else |
---|
87 | printf("."); |
---|
88 | else |
---|
89 | printf(" "); |
---|
90 | } |
---|
91 | if (i+WIDTH>len) |
---|
92 | break; |
---|
93 | else |
---|
94 | i+=WIDTH; |
---|
95 | } |
---|
96 | printf("\n"); |
---|
97 | } |
---|
98 | |
---|
99 | void decode_next(char *packet,int len,char *proto_name,int type) |
---|
100 | { |
---|
101 | std::string sname(proto_name); |
---|
102 | if (decoders[sname].find(type)==decoders[sname].end()) { |
---|
103 | void *hdl; |
---|
104 | char name[1024]; |
---|
105 | snprintf(name,sizeof(name),"%s/%s_%i.so",DIRNAME,sname.c_str(),type); |
---|
106 | hdl = dlopen(name,RTLD_LAZY); |
---|
107 | if (!hdl) |
---|
108 | decoders[sname][type]=generic_decode; |
---|
109 | else { |
---|
110 | void *s=dlsym(hdl,"decode"); |
---|
111 | if (!s) { |
---|
112 | decoders[sname][type]=generic_decode; |
---|
113 | } |
---|
114 | else |
---|
115 | decoders[sname][type]=(decode_t)s; |
---|
116 | } |
---|
117 | } |
---|
118 | decoders[sname][type](type,packet,len); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|