name change
[obnox/wireshark/wip.git] / plugins / mate / mate.h
1 /* mate.h
2 * MATE -- Meta Analysis and Tracing Engine 
3 *
4 * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
5 *
6 * $Id$
7 *
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27
28 #ifndef __MATE_H_
29 #define __MATE_H_
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #ifndef ENABLE_STATIC
36 #include "moduleinfo.h"
37 #include <gmodule.h>
38 #else 
39 #include <glib.h>
40 #endif
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45
46 #include <epan/packet.h>
47 #include <epan/strutil.h>
48 #include <epan/prefs.h>
49 #include <epan/proto.h>
50 #include <epan/epan_dissect.h>
51 #include <epan/tap.h>
52 #include <epan/filesystem.h>
53 #include <epan/report_err.h>
54
55 #include "mate_util.h"
56
57 /* defaults */
58
59 #define DEFAULT_GOG_EXPIRATION 2.0
60
61 #ifdef WIN32
62 #define DIR_SEP '\\'
63 #else
64 #define DIR_SEP '/'
65 #endif
66
67 #define DEFAULT_MATE_LIB_PATH "matelib"
68
69 #define MATE_ITEM_ID_SIZE 24
70
71 #define VALUE_TOO ((void*)1)
72
73 #define MateConfigError 65535
74
75 typedef enum _gop_tree_mode_t {
76         GOP_NULL_TREE,
77         GOP_BASIC_TREE,
78         GOP_FULL_TREE
79 } gop_tree_mode_t;
80
81 typedef enum _gop_pdu_tree {
82         GOP_NO_TREE,
83         GOP_PDU_TREE,
84         GOP_FRAME_TREE,
85         GOP_BASIC_PDU_TREE
86 } gop_pdu_tree_t;
87
88 typedef enum _accept_mode_t {
89         ACCEPT_MODE,
90         REJECT_MODE
91 } accept_mode_t;
92
93
94 typedef struct _mate_cfg_pdu {
95         gchar* name;
96         guint last_id; /* keeps the last id given to an item of this kind */
97
98         GHashTable* items; /* all the items of this type */
99         GPtrArray* transforms; /* transformations to be applied */
100         
101         int hfid;
102         
103         int hfid_proto;
104         int hfid_pdu_rel_time;
105         int hfid_pdu_time_in_gop;
106
107         GHashTable* my_hfids; /* for creating register info */
108         
109         gint ett;
110         gint ett_attr;
111         
112         GHashTable* hfids_attr; /* k=hfid v=avp_name */
113
114         gboolean discard;
115         gboolean last_extracted;
116         gboolean drop_unassigned;
117
118         GPtrArray* transport_ranges; /* hfids of candidate transport ranges from which to extract attributes */
119         GPtrArray* payload_ranges; /* hfids of candidate payload ranges from which to extract attributes */
120         
121         avpl_match_mode criterium_match_mode;
122         accept_mode_t criterium_accept_mode;
123         AVPL* criterium;
124 } mate_cfg_pdu;
125
126
127 typedef struct _mate_cfg_gop {
128         gchar* name;
129         guint last_id; /* keeps the last id given to an item of this kind */
130         GHashTable* items; /* all the items of this type */
131
132         GPtrArray* transforms; /* transformations to be applied */
133         gchar* on_pdu;
134
135         AVPL* key; /* key candidate avpl */
136         AVPL* start; /* start candidate avpl */
137         AVPL* stop;  /* stop candidate avpl */
138         AVPL* extra; /* attributes to be added */
139         
140         float expiration;
141         float idle_timeout;
142         float lifetime;
143         
144         gboolean drop_unassigned;
145         gop_pdu_tree_t pdu_tree_mode;
146         gboolean show_times;
147         
148         GHashTable* my_hfids; /* for creating register info */
149         int hfid;
150         int hfid_start_time;
151         int hfid_stop_time;
152         int hfid_last_time;
153         int hfid_gop_pdu;
154         int hfid_gop_num_pdus;
155         
156         gint ett;
157         gint ett_attr;
158         gint ett_times;
159         gint ett_children;
160         
161         GHashTable* gop_index;
162         GHashTable* gog_index;
163 } mate_cfg_gop;
164
165
166 typedef struct _mate_cfg_gog {
167         gchar* name;
168
169         GHashTable* items; /* all the items of this type */
170         guint last_id; /* keeps the last id given to an item of this kind */
171         
172         GPtrArray* transforms; /* transformations to be applied */
173
174         LoAL* keys;
175         AVPL* extra; /* attributes to be added */
176
177         float expiration;
178         gop_tree_mode_t gop_tree_mode;
179         gboolean show_times;
180         
181         GHashTable* my_hfids; /* for creating register info */
182         int hfid;
183         int hfid_gog_num_of_gops;
184         int hfid_gog_gop;
185         int hfid_gog_gopstart;
186         int hfid_gog_gopstop;   
187         int hfid_start_time;
188         int hfid_stop_time;
189         int hfid_last_time;
190         gint ett;
191         gint ett_attr;
192         gint ett_times;
193         gint ett_children;
194         gint ett_gog_gop;
195 } mate_cfg_gog;
196
197 typedef struct _mate_config {
198         gchar* mate_config_file; /* name of the config file */
199
200         int hfid_mate;
201         
202         GString* fields_filter; /* "ip.addr || dns.id || ... " for the tap */
203         GString* protos_filter; /* "dns || ftp || ..." for the tap */
204         gchar* tap_filter;
205         
206         FILE* dbg_facility; /* where to dump dbgprint output g_message if null */
207         
208         gchar* mate_lib_path; /* where to look for "Include" files first */
209         
210         GHashTable* pducfgs; /* k=pducfg->name v=pducfg */
211         GHashTable* gopcfgs; /* k=gopcfg->name v=gopcfg */
212         GHashTable* gogcfgs; /* k=gogcfg->name v=gogcfg */
213         GHashTable* transfs; /* k=transform->name v=transform */
214         
215         GPtrArray* pducfglist; /* pducfgs in order of "execution" */
216         GHashTable* gops_by_pduname; /* k=pducfg->name v=gopcfg */
217         GHashTable* gogs_by_gopname; /* k=gopname v=loal where avpl->name == matchedgop->name */
218         
219         GArray* hfrs;
220         gint ett_root;
221         GArray* ett;
222         
223         /* defaults */
224         struct _mate_cfg_defaults {
225                 struct _pdu_defaults {
226                         avpl_match_mode match_mode;
227                         avpl_replace_mode replace_mode;
228                         gboolean last_extracted;
229                         
230                         gboolean drop_unassigned;
231                         gboolean discard;
232                 } pdu;
233                 
234                 struct _gop_defaults {
235                         float expiration;
236                         float idle_timeout;
237                         float lifetime;
238                         
239                         gop_pdu_tree_t pdu_tree_mode;
240                         gboolean show_times;
241                         gboolean drop_unassigned;
242
243                 } gop;
244                 
245                 struct _gog_defaults {
246                         float expiration;
247                         
248                         gop_tree_mode_t gop_tree_mode;
249                 } gog;
250         } defaults;
251         
252         /* what to dbgprint */
253         int dbg_lvl;    
254         int dbg_pdu_lvl;
255         int dbg_gop_lvl;
256         int dbg_gog_lvl;
257         
258         GPtrArray* config_stack;
259         GString* config_error;
260         
261 } mate_config;
262
263
264 typedef struct _mate_config_frame {
265         gchar* filename;
266         guint  linenum;
267 } mate_config_frame;
268
269
270 typedef struct _mate_runtime_data {
271         guint current_items; /* a count of items */
272         GMemChunk* mate_items;
273         float now;
274         guint highest_analyzed_frame;
275         
276         GHashTable* frames; /* k=frame.num v=pdus */
277         
278 } mate_runtime_data;
279
280 typedef struct _mate_pdu mate_pdu;
281 typedef struct _mate_gop mate_gop;
282 typedef struct _mate_gog mate_gog;
283
284 /* these are used to contain information regarding pdus, gops and gogs */
285 struct _mate_pdu {
286         guint32 id; /* 1:1 -> saving a g_malloc */
287         mate_cfg_pdu* cfg; /* the type of this item */
288
289         AVPL* avpl;
290         
291         guint32 frame; /* wich frame I belog to? */
292         mate_pdu* next_in_frame; /* points to the next pdu in this frame */
293         float rel_time; /* time since start of capture  */
294         
295         mate_gop* gop; /* the gop the pdu belongs to (if any) */
296         mate_pdu* next; /* next in gop */
297         float time_in_gop; /* time since gop start */
298         
299         gboolean first; /* is this the first pdu in this frame? */
300         gboolean is_start; /* this is the start pdu for this gop */
301         gboolean is_stop; /* this is the stop pdu for this gop */
302         gboolean after_release; /* this pdu comes after the stop */
303         
304 };
305
306
307 struct _mate_gop {
308         guint32 id;
309         mate_cfg_gop* cfg; 
310         
311         gchar* gop_key;
312         AVPL* avpl; /* the attributes of the pdu/gop/gog */
313         guint last_n;
314         
315         mate_gog* gog; /* the gog of a gop */
316         mate_gop* next; /* next in gog; */
317         
318         float expiration; /* when will it expire after release (all gops releases if gog)? */
319         float idle_expiration; /* when will it expire if no new pdus are assigned to it */
320         float time_to_die;
321         float time_to_timeout;
322         
323         float start_time; /* time of start */
324         float release_time; /* when this gop/gog was released */
325         float last_time; /* the rel_time at which the last pdu has been added (to gop or gog's gop) */
326
327
328         int num_of_pdus; /* how many gops a gog has? */
329         int num_of_after_release_pdus;  /* how many pdus have arrived since it's been released */
330         mate_pdu* pdus; /* pdus that belong to a gop (NULL in gog) */
331         mate_pdu* last_pdu; /* last pdu in pdu's list */
332         
333         gboolean released; /* has this gop been released? */
334 };
335
336
337 struct _mate_gog {
338         guint32 id;
339         mate_cfg_gog* cfg; 
340         
341         AVPL* avpl; /* the attributes of the pdu/gop/gog */
342         guint last_n; /* the number of attributes the avpl had the last time we checked */
343         
344         gboolean released; /* has this gop been released? */
345         
346         float expiration; /* when will it expire after release (all gops releases if gog)? */
347         float idle_expiration; /* when will it expire if no new pdus are assigned to it */
348         
349         /* on gop and gog: */
350         float start_time; /* time of start */
351         float release_time; /* when this gog was released */
352         float last_time; /* the rel_time at which the last pdu has been added */
353         
354         mate_gop* gops; /* gops that belong to a gog (NULL in gop) */
355         mate_gop* last_gop; /* last gop in gop's list */
356         
357         int num_of_gops; /* how many gops a gog has? */
358         int num_of_counting_gops;  /* how many of them count for gog release */
359         int num_of_released_gops;  /* how many of them have already been released */
360         GPtrArray* gog_keys; /* the keys under which this gog is stored in the gogs hash */
361 };
362
363 typedef union _mate_max_size {
364         mate_pdu pdu;
365         mate_gop gop;
366         mate_gog gog;
367 } mate_max_size;
368
369 /* from mate_runtime.c */
370 extern void initialize_mate_runtime(void);
371 extern mate_pdu* mate_get_pdus(guint32 framenum);
372 extern void mate_analyze_frame(packet_info *pinfo, proto_tree* tree);
373
374 /* from mate_setup.c */
375 extern mate_config* mate_make_config(const gchar* filename, int mate_hfid);
376
377 extern mate_config* mate_cfg(void);
378 extern mate_cfg_pdu* new_pducfg(gchar* name);
379 extern mate_cfg_gop* new_gopcfg(gchar* name);
380 extern mate_cfg_gog* new_gogcfg(gchar* name);
381
382 extern gboolean add_hfid(header_field_info*  hfi, gchar* as, GHashTable* where);
383 extern gchar* add_ranges(gchar* range, GPtrArray* range_ptr_arr);
384
385
386 /* from mate_parser.l */
387 extern gboolean mate_load_config(const gchar* filename, mate_config* mc);
388
389 #endif