2 * packet tap interface 2002 Ronnie Sahlberg
4 * $Id: tap.c,v 1.9 2003/04/23 08:20:02 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
40 #include "epan/packet_info.h"
41 #include "epan/dfilter/dfilter.h"
44 static gboolean tapping_is_active=FALSE;
45 int num_tap_filters=0;
47 typedef struct _tap_dissector_t {
48 struct _tap_dissector_t *next;
51 static tap_dissector_t *tap_dissector_list=NULL;
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.
60 typedef struct _tap_packet_t {
61 struct _tap_packet_t *next;
64 void *tap_specific_data;
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
71 typedef struct _tap_listener_t {
72 struct _tap_listener_t *next;
81 static volatile tap_listener_t *tap_listener_queue=NULL;
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.
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;
100 tap_packet_list_queue=NULL;
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
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.
120 This function is only to be called once, when the dissector initializes.
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.
128 register_tap(char *name)
130 tap_dissector_t *td, *tdl;
133 td=g_malloc(sizeof(tap_dissector_t));
135 td->name = g_strdup(name);
137 if(!tap_dissector_list){
138 tap_dissector_list=td;
141 for(i=2,tdl=tap_dissector_list;tdl->next;i++,tdl=tdl->next)
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
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.
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
166 The tap reader is responsible to know how to parse any structure pointed
167 to by the tap specific data pointer.
170 tap_queue_packet(int tap_id, packet_info *pinfo, void *tap_specific_data)
174 if(!tapping_is_active){
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;
186 tpt->tap_specific_data=tap_specific_data;
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.
202 tap_queue_init(epan_dissect_t *edt)
207 /* nothing to do, just return */
208 if(!tap_listener_queue){
212 tapping_is_active=TRUE;
213 tpt=tap_packet_list_queue;
215 for(;tpt->next;tpt=tpt->next)
218 tpt->next=tap_packet_list_free;
219 tap_packet_list_free=tap_packet_list_queue;
220 tap_packet_list_queue=NULL;
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){
227 epan_dissect_prime_dfilter(edt, tl->code);
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.
236 tap_push_tapped_queue(epan_dissect_t *edt)
241 /* nothing to do, just return */
242 if(!tapping_is_active){
246 tapping_is_active=FALSE;
248 /* nothing to do, just return */
249 if(!tap_packet_list_queue){
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){
260 passed=dfilter_apply_edt(tl->code, edt);
262 if(passed && tl->packet){
263 tl->needs_redraw|=tl->packet(tl->tapdata, tp->pinfo, edt, tp->tap_specific_data);
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.
274 reset_tap_listeners(void)
278 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
280 tl->reset(tl->tapdata);
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
292 If draw_all is true, redraw all aplications regardless if they have
296 draw_tap_listeners(gboolean draw_all)
300 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
301 if(tl->needs_redraw || draw_all){
303 tl->draw(tl->tapdata);
312 /* **********************************************************************
313 * Functions used by tap to
314 * 1, register that a really simple extension is available for use by
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.
323 find_tap_id(char *name)
328 for(i=1,td=tap_dissector_list;td;i++,td=td->next) {
329 if(!strcmp(td->name,name)){
336 /* this function attaches the tap_listener to the named tap.
339 * non-NULL: error, return value points to GString containing error
343 register_tap_listener(char *tapname, void *tapdata, char *fstring, tap_reset_cb reset, tap_packet_cb packet, tap_draw_cb draw)
347 GString *error_string;
349 tap_id=find_tap_id(tapname);
351 error_string = g_string_new("");
352 g_string_sprintf(error_string, "Tap %s not found", tapname);
356 tl=g_malloc(sizeof(tap_listener_t));
360 if(!dfilter_compile(fstring, &tl->code)){
361 error_string = g_string_new("");
362 g_string_sprintf(error_string,
363 "Filter \"%s\" is invalid - %s",
364 fstring, dfilter_error_msg);
377 tl->next=(tap_listener_t *)tap_listener_queue;
379 tap_listener_queue=tl;
384 /* this function removes a tap listener
387 remove_tap_listener(void *tapdata)
389 tap_listener_t *tl=NULL,*tl2;
391 if(!tap_listener_queue){
395 if(tap_listener_queue->tapdata==tapdata){
396 tl=(tap_listener_t *)tap_listener_queue;
397 tap_listener_queue=tap_listener_queue->next;
399 for(tl2=(tap_listener_t *)tap_listener_queue;tl2->next;tl2=tl2->next){
400 if(tl2->next->tapdata==tapdata){
402 tl2->next=tl2->next->next;
411 dfilter_free(tl->code);