2 * packet tap interface 2002 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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 {
63 const void *tap_specific_data;
66 #define TAP_PACKET_QUEUE_LEN 100
67 static tap_packet_t tap_packet_array[TAP_PACKET_QUEUE_LEN];
68 static guint tap_packet_index;
70 typedef struct _tap_listener_t {
71 struct _tap_listener_t *next;
80 static volatile tap_listener_t *tap_listener_queue=NULL;
82 /* **********************************************************************
83 * Init routine only called from epan at application startup
84 * ********************************************************************** */
85 /* This function is called once when wireshark starts up and is used
86 to init any data structures we may need later.
96 /* **********************************************************************
97 * Functions called from dissector when made tappable
98 * ********************************************************************** */
99 /* the following two functions are used from dissectors to
100 1, register the ability to tap packets from this subdissector
101 2, push packets encountered by the subdissector to anyone tapping
104 /* This function registers that a dissector has the packet tap ability
105 available. The name parameter is the name of this tap and extensions can
106 use open_tap(char *name,... to specify that it wants to receive packets/
107 events from this tap.
109 This function is only to be called once, when the dissector initializes.
111 The return value from this call is later used as a parameter to the
112 tap_packet(unsinged int *tap_id,...
113 call so that the tap subsystem knows to which tap point this tapped
114 packet is associated.
117 register_tap(const char *name)
119 tap_dissector_t *td, *tdl;
122 if(tap_dissector_list){
123 tap_id=find_tap_id(name);
128 td=g_malloc(sizeof(tap_dissector_t));
130 td->name = g_strdup(name);
132 if(!tap_dissector_list){
133 tap_dissector_list=td;
136 for(i=2,tdl=tap_dissector_list;tdl->next;i++,tdl=tdl->next)
144 /* Everytime the dissector has finished dissecting a packet (and all
145 subdissectors have returned) and if the dissector has been made "tappable"
146 it will push some data to everyone tapping this layer by a call
147 to tap_queue_packet().
148 The first parameter is the tap_id returned by the register_tap()
149 call for this dissector (so the tap system can keep track of who it came
150 from and who is listening to it)
151 The second is the packet_info structure which many tap readers will find
153 The third argument is specific to each tap point or NULL if no additional
154 data is available to this tap. A tap point in say IP will probably want to
155 push the IP header structure here. Same thing for TCP and ONCRPC.
157 The pinfo and the specific pointer are what is supplied to every listener
158 in the read_callback() call made to every one currently listening to this
161 The tap reader is responsible to know how to parse any structure pointed
162 to by the tap specific data pointer.
165 tap_queue_packet(int tap_id, packet_info *pinfo, const void *tap_specific_data)
169 if(!tapping_is_active){
173 * XXX - should we allocate this with an ep_allocator,
174 * rather than having a fixed maximum number of entries?
176 if(tap_packet_index >= TAP_PACKET_QUEUE_LEN){
177 g_warning("Too many taps queued");
181 tpt=&tap_packet_array[tap_packet_index];
184 tpt->tap_specific_data=tap_specific_data;
192 /* **********************************************************************
193 * Functions used by file.c to drive the tap subsystem
194 * ********************************************************************** */
195 /* This function is used to delete/initialize the tap queue and prime an
196 epan_dissect_t with all the filters for tap listeners.
197 To free the tap queue, we just prepend the used queue to the free queue.
200 tap_queue_init(epan_dissect_t *edt)
204 /* nothing to do, just return */
205 if(!tap_listener_queue){
209 tapping_is_active=TRUE;
213 /* loop over all tap listeners and build the list of all
214 interesting hf_fields */
215 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
217 epan_dissect_prime_dfilter(edt, tl->code);
222 /* this function is called after a packet has been fully dissected to push the tapped
223 data to all extensions that has callbacks registered.
226 tap_push_tapped_queue(epan_dissect_t *edt)
232 /* nothing to do, just return */
233 if(!tapping_is_active){
237 tapping_is_active=FALSE;
239 /* nothing to do, just return */
240 if(!tap_packet_index){
244 /* loop over all tap listeners and call the listener callback
245 for all packets that match the filter. */
246 for(i=0;i<tap_packet_index;i++){
247 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
248 tp=&tap_packet_array[i];
249 if(tp->tap_id==tl->tap_id){
252 passed=dfilter_apply_edt(tl->code, edt);
254 if(passed && tl->packet){
255 tl->needs_redraw|=tl->packet(tl->tapdata, tp->pinfo, edt, tp->tap_specific_data);
263 /* This function can be used by a dissector to fetch any tapped data before
265 * This can be useful if one wants to extract the data inside dissector BEFORE
266 * it exists as an alternative to the callbacks that are all called AFTER the
267 * dissection has completed.
269 * Example: SMB2 uses this mechanism to extract the data tapped from NTLMSSP
270 * containing the account and domain names before exiting.
271 * Note that the SMB2 tap listener specifies all three callbacks as NULL.
273 * Beware: when using this mechanism to extract the tapped data you can not
274 * use "filters" and should specify the "filter" as NULL when registering
278 fetch_tapped_data(int tap_id, int idx)
283 /* nothing to do, just return */
284 if(!tapping_is_active){
288 /* nothing to do, just return */
289 if(!tap_packet_index){
293 /* loop over all tapped packets and return the one with index idx */
294 for(i=0;i<tap_packet_index;i++){
295 tp=&tap_packet_array[i];
296 if(tp->tap_id==tap_id){
298 return tp->tap_specific_data;
306 /* This function is called when we need to reset all tap listeners, for example
307 when we open/start a new capture or if we need to rescan the packet list.
310 reset_tap_listeners(void)
314 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
316 tl->reset(tl->tapdata);
324 /* This function is called when we need to redraw all tap listeners, for example
325 when we open/start a new capture or if we need to rescan the packet list.
326 this one should be called from a low priority thread say once every 3 seconds
328 If draw_all is true, redraw all aplications regardless if they have
332 draw_tap_listeners(gboolean draw_all)
336 for(tl=(tap_listener_t *)tap_listener_queue;tl;tl=tl->next){
337 if(tl->needs_redraw || draw_all){
339 tl->draw(tl->tapdata);
348 /* **********************************************************************
349 * Functions used by tap to
350 * 1, register that a really simple extension is available for use by
352 * 2, start tapping from a subdissector
353 * 3, close an already open tap
354 * ********************************************************************** */
355 /* this function will return the tap_id for the specific protocol tap
356 or 0 if no such tap was found.
359 find_tap_id(const char *name)
364 for(i=1,td=tap_dissector_list;td;i++,td=td->next) {
365 if(!strcmp(td->name,name)){
372 /* this function attaches the tap_listener to the named tap.
375 * non-NULL: error, return value points to GString containing error
379 register_tap_listener(const char *tapname, void *tapdata, const char *fstring, tap_reset_cb reset, tap_packet_cb packet, tap_draw_cb draw)
383 GString *error_string;
385 tap_id=find_tap_id(tapname);
387 error_string = g_string_new("");
388 g_string_printf(error_string, "Tap %s not found", tapname);
392 tl=g_malloc(sizeof(tap_listener_t));
396 if(!dfilter_compile(fstring, &tl->code)){
397 error_string = g_string_new("");
398 g_string_printf(error_string,
399 "Filter \"%s\" is invalid - %s",
400 fstring, dfilter_error_msg);
413 tl->next=(tap_listener_t *)tap_listener_queue;
415 tap_listener_queue=tl;
420 /* this function sets a new dfilter to a tap listener
423 set_tap_dfilter(void *tapdata, const char *fstring)
425 tap_listener_t *tl=NULL,*tl2;
426 GString *error_string;
428 if(!tap_listener_queue){
432 if(tap_listener_queue->tapdata==tapdata){
433 tl=(tap_listener_t *)tap_listener_queue;
435 for(tl2=(tap_listener_t *)tap_listener_queue;tl2->next;tl2=tl2->next){
436 if(tl2->next->tapdata==tapdata){
446 dfilter_free(tl->code);
452 if(!dfilter_compile(fstring, &tl->code)){
453 error_string = g_string_new("");
454 g_string_printf(error_string,
455 "Filter \"%s\" is invalid - %s",
456 fstring, dfilter_error_msg);
467 /* this function removes a tap listener
470 remove_tap_listener(void *tapdata)
472 tap_listener_t *tl=NULL,*tl2;
474 if(!tap_listener_queue){
478 if(tap_listener_queue->tapdata==tapdata){
479 tl=(tap_listener_t *)tap_listener_queue;
480 tap_listener_queue=tap_listener_queue->next;
482 for(tl2=(tap_listener_t *)tap_listener_queue;tl2->next;tl2=tl2->next){
483 if(tl2->next->tapdata==tapdata){
485 tl2->next=tl2->next->next;
494 dfilter_free(tl->code);
504 * Return TRUE if we have tap listeners, FALSE otherwise.
505 * Checking "num_tap_filters" isn't the right way to check whether we need
506 * to do any dissection in order to run taps, as not all taps necessarily
507 * have filters, and "num_tap_filters" is the number of tap filters, not
508 * the number of tap listeners; it's only the right way to check whether
509 * we need to build a protocol tree when doing dissection.
512 have_tap_listeners(void)
514 return tap_listener_queue != NULL;
517 /* Returns TRUE there is an active tap listener for the specified tap id. */
519 have_tap_listener(int tap_id)
521 volatile tap_listener_t *tap_queue = tap_listener_queue;
524 if(tap_queue->tap_id == tap_id)
527 tap_queue = tap_queue->next;