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 | extern "C"{ |
---|
49 | #include "grammar.h" |
---|
50 | } |
---|
51 | |
---|
52 | enum decode_style_t { |
---|
53 | DECODE_NORMAL, |
---|
54 | DECODE_PARSER |
---|
55 | }; |
---|
56 | |
---|
57 | typedef void (*decode_norm_t)(uint16_t type,const char *packet,int len); |
---|
58 | typedef void (*decode_parser_t)(uint16_t type,const char *packet,int len, element_t* el); |
---|
59 | |
---|
60 | typedef union decode_funcs { |
---|
61 | decode_norm_t decode_n; |
---|
62 | decode_parser_t decode_p; |
---|
63 | } decode_funcs_t; |
---|
64 | |
---|
65 | typedef struct decoder { |
---|
66 | enum decode_style_t style; |
---|
67 | decode_funcs_t *func; |
---|
68 | element_t *el; // make a union of structs with all args in it for all funcs? |
---|
69 | } decode_t; |
---|
70 | |
---|
71 | |
---|
72 | static std::map<std::string,std::map<uint16_t,decode_t> > decoders; |
---|
73 | |
---|
74 | #define WIDTH 16 |
---|
75 | |
---|
76 | #ifndef DIRNAME |
---|
77 | #define DIRNAME "./" |
---|
78 | #warning "No DIRNAME set!" |
---|
79 | #endif |
---|
80 | |
---|
81 | void trace_dump_packet(struct libtrace_packet_t *packet) |
---|
82 | { |
---|
83 | time_t sec = (time_t)trace_get_seconds(packet); |
---|
84 | libtrace_linktype_t linktype; |
---|
85 | uint32_t length; |
---|
86 | const char *link=(char *)trace_get_packet_buffer(packet,&linktype,&length); |
---|
87 | |
---|
88 | printf("\n%s",ctime(&sec)); |
---|
89 | printf(" Capture: Packet Length: %i/%i Direction Value: %i\n", |
---|
90 | (int)length, |
---|
91 | (int)trace_get_wire_length(packet), |
---|
92 | (int)trace_get_direction(packet)); |
---|
93 | if (!link) |
---|
94 | printf(" [No link layer available]\n"); |
---|
95 | else |
---|
96 | decode_next(link,length, "link", |
---|
97 | linktype); |
---|
98 | } |
---|
99 | |
---|
100 | static void generic_decode(uint16_t type,const char *packet, int len) { |
---|
101 | int i; |
---|
102 | printf(" Unknown Protocol: %i",type); |
---|
103 | for(i=0;i<len; /* Nothing */ ) { |
---|
104 | int j; |
---|
105 | printf("\n "); |
---|
106 | for(j=0;j<WIDTH;j++) { |
---|
107 | if (i+j<len) |
---|
108 | printf(" %02x",(unsigned char)packet[i+j]); |
---|
109 | else |
---|
110 | printf(" "); |
---|
111 | } |
---|
112 | printf(" "); |
---|
113 | for(j=0;j<WIDTH;j++) { |
---|
114 | if (i+j<len) |
---|
115 | if (isprint((unsigned char)packet[i+j])) |
---|
116 | printf("%c",(unsigned char)packet[i+j]); |
---|
117 | else |
---|
118 | printf("."); |
---|
119 | else |
---|
120 | printf(" "); |
---|
121 | } |
---|
122 | if (i+WIDTH>len) |
---|
123 | break; |
---|
124 | else |
---|
125 | i+=WIDTH; |
---|
126 | } |
---|
127 | printf("\n"); |
---|
128 | } |
---|
129 | |
---|
130 | void decode_next(const char *packet,int len,const char *proto_name,int type) |
---|
131 | { |
---|
132 | std::string sname(proto_name); |
---|
133 | |
---|
134 | // if we haven't worked out how to decode this type yet, load the |
---|
135 | // appropriate files to do so |
---|
136 | if (decoders[sname].find(type)==decoders[sname].end()) { |
---|
137 | void *hdl; |
---|
138 | char name[1024]; |
---|
139 | decode_funcs_t *func = new decode_funcs_t; |
---|
140 | decode_t dec; |
---|
141 | snprintf(name,sizeof(name),"%s/%s_%i.so",DIRNAME,sname.c_str(),type); |
---|
142 | hdl = dlopen(name,RTLD_LAZY); |
---|
143 | if (!hdl) { |
---|
144 | // if there is no shared library, try a protocol file |
---|
145 | snprintf(name,sizeof(name),"%s/%s_%i.protocol", |
---|
146 | DIRNAME,sname.c_str(),type); |
---|
147 | hdl = parse_protocol_file(name); |
---|
148 | |
---|
149 | if(!hdl) |
---|
150 | { |
---|
151 | // no protocol file either, use a generic one |
---|
152 | func->decode_n = generic_decode; |
---|
153 | dec.style = DECODE_NORMAL; |
---|
154 | dec.el = NULL; |
---|
155 | } else { |
---|
156 | // use the protocol file |
---|
157 | func->decode_p = decode_protocol_file; |
---|
158 | dec.style = DECODE_PARSER; |
---|
159 | dec.el = (element_t*)hdl; |
---|
160 | } |
---|
161 | } else { |
---|
162 | void *s=dlsym(hdl,"decode"); |
---|
163 | if (!s) { |
---|
164 | // the shared library doesnt have a decode func |
---|
165 | // TODO should try the protocol file now |
---|
166 | func->decode_n = generic_decode; |
---|
167 | dec.style = DECODE_NORMAL; |
---|
168 | dec.el = NULL; |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | // use the shared library |
---|
173 | func->decode_n = (decode_norm_t)s; |
---|
174 | dec.style = DECODE_NORMAL; |
---|
175 | dec.el = NULL; |
---|
176 | } |
---|
177 | } |
---|
178 | dec.func = func; |
---|
179 | decoders[sname][type] = dec; |
---|
180 | } |
---|
181 | |
---|
182 | if (decoders[sname][type].func->decode_n == generic_decode) { |
---|
183 | /* We can't decode a link, so lets skip that and see if libtrace |
---|
184 | * knows how to find us the ip header |
---|
185 | */ |
---|
186 | |
---|
187 | /* Also, don't try to skip if the linktype is not valid, |
---|
188 | * because libtrace will just assert fail and that's never |
---|
189 | * good */ |
---|
190 | if (sname=="link" && type != -1) { |
---|
191 | uint16_t newtype; |
---|
192 | uint32_t newlen=len; |
---|
193 | const char *network=(const char*)trace_get_payload_from_link((void*)packet, |
---|
194 | (libtrace_linktype_t)type, |
---|
195 | &newtype,&newlen); |
---|
196 | if (network) { |
---|
197 | printf("skipping unknown link header of type %i to network type %i\n",type,newtype); |
---|
198 | decode_next(network,newlen,"eth",newtype); |
---|
199 | return; |
---|
200 | } |
---|
201 | } |
---|
202 | else { |
---|
203 | printf("unknown protocol %s/%i\n",sname.c_str(),type); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | // decode using the appropriate function |
---|
208 | switch(decoders[sname][type].style) |
---|
209 | { |
---|
210 | case DECODE_NORMAL: |
---|
211 | decoders[sname][type].func->decode_n(type,packet,len); |
---|
212 | break; |
---|
213 | |
---|
214 | case DECODE_PARSER: |
---|
215 | decoders[sname][type].func->decode_p(type,packet,len, |
---|
216 | decoders[sname][type].el); |
---|
217 | break; |
---|
218 | |
---|
219 | }; |
---|
220 | } |
---|