0a0b24b99884d2c96e78cb366697d2c936f40fa2
[obnox/wireshark/wip.git] / tap.c
1 /* tap.c
2  * packet tap interface   2002 Ronnie Sahlberg
3  *
4  * $Id: tap.c,v 1.8 2002/11/28 20:28:28 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
37 #endif
38
39 #include <string.h>
40 #include "epan/packet_info.h"
41 #include "epan/dfilter/dfilter.h"
42 #include "tap.h"
43
44 static gboolean tapping_is_active=FALSE;
45 int num_tap_filters=0;
46
47 typedef struct _tap_dissector_t {
48         struct _tap_dissector_t *next;
49         char *name;
50 } tap_dissector_t;
51 static tap_dissector_t *tap_dissector_list=NULL;
52
53 /*
54  * This is the list of free and used packets queued for a tap.
55  * It is implemented here explicitely instead of using GLib objects
56  * in order to be as fast as possible as we need to build and tear down the
57  * queued list at least once for each packet we see, thus we must be able
58  * to build and tear it down as fast as possible.
59  */
60 typedef struct _tap_packet_t {
61         struct _tap_packet_t *next;
62         int tap_id;
63         packet_info *pinfo;
64         void *tap_specific_data;
65 } tap_packet_t;
66 static tap_packet_t *tap_packet_list_free=NULL;
67 static tap_packet_t *tap_packet_list_queue=NULL;
68 #define TAP_PACKET_QUEUE_LEN 100
69
70
71 typedef struct _tap_listener_t {
72         struct _tap_listener_t *next;
73         int tap_id;
74         int needs_redraw;
75         dfilter_t *code;
76         void *tapdata;
77         tap_reset_cb reset;
78         tap_packet_cb packet;
79         tap_draw_cb draw;
80 } tap_listener_t;
81 static volatile tap_listener_t *tap_listener_queue=NULL;
82
83 /* **********************************************************************
84  * Init routine only called from epan at application startup
85  * ********************************************************************** */
86 /* This function is called once when ethereal starts up and is used
87    to init any data structures we may need later.
88 */
89 void
90 tap_init(void)
91 {
92         int i;
93         tap_packet_t *tpt;
94
95         for(i=0;i<TAP_PACKET_QUEUE_LEN;i++){
96                 tpt=g_malloc(sizeof(tap_packet_t));
97                 tpt->next=tap_packet_list_free;
98                 tap_packet_list_free=tpt;
99         }
100         tap_packet_list_queue=NULL;
101
102         return;
103 }
104
105
106
107 /* **********************************************************************
108  * Functions called from dissector when made tappable
109  * ********************************************************************** */
110 /* the following two functions are used from dissectors to
111    1, register the ability to tap packets from this subdissector
112    2, push packets encountered by the subdissector to anyone tapping
113 */
114
115 /* This function registers that a dissector has the packet tap ability
116    available.  The name parameter is the name of this tap and extensions can
117    use open_tap(char *name,... to specify that it wants to receive packets/
118    events from this tap.
119
120    This function is only to be called once, when the dissector initializes.
121
122    The return value from this call is later used as a parameter to the
123    tap_packet(unsinged int *tap_id,...
124    call so that the tap subsystem knows to which tap point this tapped
125    packet is associated.
126 */  
127 int
128 register_tap(char *name)
129 {
130         tap_dissector_t *td, *tdl;
131         int i;
132
133         td=g_malloc(sizeof(tap_dissector_t));
134         td->next=NULL;
135         td->name = g_strdup(name);
136
137         if(!tap_dissector_list){
138                 tap_dissector_list=td;
139                 i=1;
140         } else {
141                 for(i=2,tdl=tap_dissector_list;tdl->next;i++,tdl=tdl->next)
142                         ;
143                 tdl->next=td;
144         }
145         return i;
146 }
147
148
149 /* Everytime the dissector has finished dissecting a packet (and all
150    subdissectors have returned) and if the dissector has been made "tappable"
151    it will push some data to everyone tapping this layer by a call
152    to tap_queue_packet().
153    The first parameter is the tap_id returned by the register_tap()
154    call for this dissector (so the tap system can keep track of who it came
155    from and who is listening to it)
156    The second is the packet_info structure which many tap readers will find
157    interesting.
158    The third argument is specific to each tap point or NULL if no additional 
159    data is available to this tap.  A tap point in say IP will probably want to
160    push the IP header structure here. Same thing for TCP and ONCRPC.
161   
162    The pinfo and the specific pointer are what is supplied to every listener
163    in the read_callback() call made to every one currently listening to this
164    tap.
165  
166    The tap reader is responsible to know how to parse any structure pointed 
167    to by the tap specific data pointer.
168 */
169 void 
170 tap_queue_packet(int tap_id, packet_info *pinfo, void *tap_specific_data)
171 {
172         tap_packet_t *tpt;
173
174         if(!tapping_is_active){
175                 return;
176         }
177
178         /* get a free tap_packet structure, this is CHEAP */
179         tpt=tap_packet_list_free;
180         tap_packet_list_free=tpt->next;
181         tpt->next=tap_packet_list_queue;
182         tap_packet_list_queue=tpt;
183
184         tpt->tap_id=tap_id;
185         tpt->pinfo=pinfo;
186         tpt->tap_specific_data=tap_specific_data;
187
188 }
189
190
191
192
193
194 /* **********************************************************************
195  * Functions used by file.c to drive the tap subsystem
196  * ********************************************************************** */
197 /* This function is used to delete/initialize the tap queue and prime an
198    epan_dissect_t with all the filters for tap listeners.
199    To free the tap queue, we just prepend the used queue to the free queue.
200 */
201 void
202 tap_queue_init(epan_dissect_t *edt)
203 {
204         tap_packet_t *tpt;
205         tap_listener_t *tl;
206
207         /* nothing to do, just return */
208         if(!tap_listener_queue){
209                 return;
210         }
211
212         tapping_is_active=TRUE;
213         tpt=tap_packet_list_queue;
214         if(tpt){
215                 for(;tpt->next;tpt=tpt->next)
216                         ;
217
218                 tpt->next=tap_packet_list_free;
219                 tap_packet_list_free=tap_packet_list_queue;
220                 tap_packet_list_queue=NULL;
221         }
222
223         /* loop over all tap listeners and build the list of all
224            interesting hf_fields */
225         for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
226                 if(tl->code){
227                         epan_dissect_prime_dfilter(edt, tl->code);
228                 }
229         }
230 }
231
232 /* this function is called after a packet has been fully dissected to push the tapped
233    data to all extensions that has callbacks registered.
234 */
235 void 
236 tap_push_tapped_queue(epan_dissect_t *edt)
237 {
238         tap_packet_t *tp;
239         tap_listener_t *tl;
240
241         /* nothing to do, just return */
242         if(!tapping_is_active){
243                 return;
244         }
245
246         tapping_is_active=FALSE;
247
248         /* nothing to do, just return */
249         if(!tap_packet_list_queue){
250                 return;
251         }
252
253         /* loop over all tap listeners and call the listener callback
254            for all packets that match the filter. */
255         for(tp=tap_packet_list_queue;tp;tp=tp->next){
256                 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
257                         if(tp->tap_id==tl->tap_id){
258                                 int passed=TRUE;
259                                 if(tl->code){
260                                         passed=dfilter_apply_edt(tl->code, edt);
261                                 }
262                                 if(passed && tl->packet){
263                                         tl->needs_redraw|=tl->packet(tl->tapdata, tp->pinfo, edt, tp->tap_specific_data);
264                                 }
265                         }
266                 }
267         }
268 }
269
270 /* This function is called when we need to reset all tap listeners, for example
271    when we open/start a new capture or if we need to rescan the packet list.
272 */
273 void
274 reset_tap_listeners(void)
275 {
276         tap_listener_t *tl;
277
278         for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
279                 if(tl->reset){
280                         tl->reset(tl->tapdata);
281                 }
282                 tl->needs_redraw=1;
283         }
284
285 }
286
287
288 /* This function is called when we need to redraw all tap listeners, for example
289    when we open/start a new capture or if we need to rescan the packet list.
290    this one should be called from a low priority thread say once every 3 seconds
291  
292    If draw_all is true, redraw all aplications regardless if they have 
293    changed or not.
294 */
295 void
296 draw_tap_listeners(gboolean draw_all)
297 {
298         tap_listener_t *tl;
299
300         for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
301                 if(tl->needs_redraw || draw_all){
302                         if(tl->draw){
303                                 tl->draw(tl->tapdata);
304                         }
305                 }
306                 tl->needs_redraw=0;
307         }
308 }
309
310
311
312 /* **********************************************************************
313  * Functions used by tap to
314  * 1, register that a really simple extension is available for use by
315  *    ethereal. 
316  * 2, start tapping from a subdissector 
317  * 3, close an already open tap
318  * ********************************************************************** */
319 /* this function will return the tap_id for the specific protocol tap
320    or 0 if no such tap was found.
321  */
322 int 
323 find_tap_id(char *name)
324 {
325         tap_dissector_t *td;
326         int i;
327
328         for(i=1,td=tap_dissector_list;td;i++,td=td->next) {
329                 if(!strcmp(td->name,name)){
330                         return i;
331                 }
332         }
333         return 0;
334 }
335
336 /* this function attaches the tap_listener to the named tap.
337  * function returns :
338  *  0: ok.
339  * !0: error
340 */
341 int
342 register_tap_listener(char *tapname, void *tapdata, char *fstring, tap_reset_cb reset, tap_packet_cb packet, tap_draw_cb draw)
343 {
344         tap_listener_t *tl;
345         int tap_id;
346
347         tap_id=find_tap_id(tapname);
348         if(!tap_id){
349                 fprintf(stderr, "tap not found\n");
350                 exit(10);
351         }
352
353         tl=g_malloc(sizeof(tap_listener_t));
354         tl->code=NULL;
355         tl->needs_redraw=1;
356         if(fstring){
357                 if(!dfilter_compile(fstring ,&tl->code)){
358                         g_free(tl);
359                         fprintf(stderr,"register_tap_listener(): %s\n", dfilter_error_msg);
360                         return 1;
361                 } else {
362                         num_tap_filters++;
363                 }
364         }
365
366         tl->tap_id=tap_id;
367         tl->tapdata=tapdata;
368         tl->reset=reset;
369         tl->packet=packet;
370         tl->draw=draw;
371         tl->next=(tap_listener_t *)tap_listener_queue;
372
373         tap_listener_queue=tl;
374
375         return 0;
376 }
377
378 /* this function removes a tap listener
379  */
380 void
381 remove_tap_listener(void *tapdata)
382 {
383         tap_listener_t *tl=NULL,*tl2;
384
385         if(!tap_listener_queue){
386                 return;
387         }
388
389         if(tap_listener_queue->tapdata==tapdata){
390                 tl=(tap_listener_t *)tap_listener_queue;
391                 tap_listener_queue=tap_listener_queue->next;
392         } else {
393                 for(tl2=(tap_listener_t *)tap_listener_queue;tl2->next;tl2=tl2->next){
394                         if(tl2->next->tapdata==tapdata){
395                                 tl=tl2->next;
396                                 tl2->next=tl2->next->next;
397                                 break;
398                         }
399                         
400                 }
401         }
402
403         if(tl){
404                 if(tl->code){
405                         dfilter_free(tl->code);
406                         num_tap_filters--;
407                 }
408                 g_free(tl);
409         }
410
411         return;
412 }
413
414
415