Add a plugin interface to wiretap.

So far I've done only regression testing (the new functionality and what's in wtap-plugins.c has not yet being tested).

it is a first step in the way to have lua opening files.



svn path=/trunk/; revision=21686
This commit is contained in:
Luis Ontanon 2007-05-04 21:10:55 +00:00
parent 334177b096
commit 131cecd1e8
7 changed files with 278 additions and 39 deletions

View File

@ -64,6 +64,7 @@ NONGENERATED_C_FILES = \
toshiba.c \
visual.c \
vms.c \
wtap-plugins.c \
wtap.c
# Header files that are not generated from other files

View File

@ -74,6 +74,8 @@
#include "catapult_dct2000.h"
#include "mpeg.h"
/* The open_file_* routines should return:
*
* -1 on an I/O error;
@ -91,7 +93,8 @@
* should be discovered as a libpcap file, not a toshiba file.
*/
static int (*const open_routines[])(wtap *, int *, char **) = {
static wtap_open_routine_t open_routines_base[] = {
/* Files that have magic bytes in fixed locations. These
* are easy to identify.
*/
@ -134,7 +137,36 @@ static int (*const open_routines[])(wtap *, int *, char **) = {
hcidump_open,
};
#define N_FILE_TYPES (sizeof open_routines / sizeof open_routines[0])
#define N_FILE_TYPES (sizeof open_routines_base / sizeof open_routines_base[0])
static wtap_open_routine_t* open_routines = NULL;
static GArray* open_routines_arr = NULL;
/* initialize the open routines array if it has not being initialized yet */
static void init_open_routines(void) {
if (open_routines_arr) return;
open_routines_arr = g_array_new(FALSE,TRUE,sizeof(wtap_open_routine_t));
g_array_append_vals(open_routines_arr,open_routines_base,N_FILE_TYPES);
open_routines = (void*)open_routines_arr->data;
}
void wtap_register_open_routine(wtap_open_routine_t open_routine, gboolean has_magic) {
init_open_routines();
if (has_magic)
g_array_prepend_val(open_routines_arr,open_routine);
else
g_array_append_val(open_routines_arr,open_routine);
open_routines = (void*)open_routines_arr->data;
wtap_num_file_types++;
}
/*
* Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
@ -277,9 +309,11 @@ wtap* wtap_open_offline(const char *filename, int *err, char **err_info,
wth->subtype_sequential_close = NULL;
wth->subtype_close = NULL;
wth->tsprecision = WTAP_FILE_TSPREC_USEC;
init_open_routines();
/* Try all file types */
for (i = 0; i < N_FILE_TYPES; i++) {
for (i = 0; i < open_routines_arr->len; i++) {
/* Seek back to the beginning of the file; the open routine
for the previous file type may have left the file
position somewhere other than the beginning, and the
@ -332,34 +366,7 @@ success:
}
/* Table of the file types we know about. */
static const struct file_type_info {
/* the file type name */
/* should be NULL for all "pseudo" types that are only internally used and not read/writeable */
const char *name;
/* the file type short name, used as a shortcut for the command line tools */
/* should be NULL for all "pseudo" types that are are only internally used and not read/writeable */
const char *short_name;
/* the common file extensions for this type (seperated by semicolon) */
/* should be *.* if no common extension is applicable */
const char *file_extensions;
/* the default file extension, used to save this type */
/* should be NULL if no default extension is known */
const char *file_extension_default;
/* can this type be compressed with gzip? */
gboolean can_compress;
/* can this type write this encapsulation format? */
/* should be NULL is this file type don't have write support */
int (*can_write_encap)(int);
/* the function to open the capture file for writing */
/* should be NULL is this file type don't have write support */
int (*dump_open)(wtap_dumper *, gboolean, int *);
} dump_open_table[WTAP_NUM_FILE_TYPES] = {
static const struct file_type_info dump_open_table[] = {
/* WTAP_FILE_UNKNOWN (only used internally for initialization) */
{ NULL, NULL, NULL, NULL, FALSE,
NULL, NULL },
@ -553,6 +560,8 @@ static const struct file_type_info {
k12text_dump_can_write_encap, k12text_dump_open },
};
gint wtap_num_file_types = sizeof(dump_open_table) / sizeof(struct file_type_info);
/* Name that should be somewhat descriptive. */
const char *wtap_file_type_string(int filetype)
{

View File

@ -278,7 +278,6 @@ static gboolean k12text_dump(wtap_dumper *wdh _U_, const struct wtap_pkthdr *phd
static gboolean k12text_dump_close(wtap_dumper *wdh _U_ , int *err _U_) {
(void)0;
return TRUE;
}

152
wiretap/wtap-plugins.c Normal file
View File

@ -0,0 +1,152 @@
/* wtap.h
*
* $Id: wtap.h 21651 2007-05-02 20:09:42Z lego $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <gmodule.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "wtap.h"
#include "file_util.h"
#define PLUGINS_DIR_NAME "wiretap_plugins"
static gboolean plugins_loaded = FALSE;
void wtap_load_plugins(char* dirname) {
#define FILENAME_LEN 1024
ETH_DIR *dir; /* scanned directory */
ETH_DIRENT *file; /* current file */
const char *name;
#if GLIB_MAJOR_VERSION < 2
gchar *hack_path; /* pathname used to construct lt_lib_ext */
gchar *lt_lib_ext; /* extension for loadable modules */
#endif
gchar filename[FILENAME_LEN]; /* current file name */
GModule *handle; /* handle returned by dlopen */
gchar *version;
gpointer gp;
gchar *dot;
if (plugins_loaded || ! dirname) return;
plugins_loaded = TRUE;
#if GLIB_MAJOR_VERSION < 2
/*
* We find the extension used on this platform for loadable modules
* by the sneaky hack of calling "g_module_build_path" to build
* the pathname for a module with an empty directory name and
* empty module name, and then search for the last "." and use
* everything from the last "." on.
*/
hack_path = g_module_build_path("", "");
lt_lib_ext = strrchr(hack_path, '.');
if (lt_lib_ext == NULL)
{
/*
* Does this mean there *is* no extension? Assume so.
*
* XXX - the code below assumes that all loadable modules have
* an extension....
*/
lt_lib_ext = "";
}
#endif
if ((dir = eth_dir_open(dirname, 0, NULL)) != NULL)
{
while ((file = eth_dir_read_name(dir)) != NULL)
{
name = eth_dir_get_name(file);
#if GLIB_MAJOR_VERSION < 2
/* don't try to open "." and ".." */
if (!(strcmp(name, "..") &&
strcmp(name, ".")))
continue;
/* skip anything but files with lt_lib_ext */
dot = strrchr(name, '.');
if (dot == NULL || strcmp(dot, lt_lib_ext) != 0)
continue;
#else /* GLIB 2 */
/*
* GLib 2.x defines G_MODULE_SUFFIX as the extension used on
* this platform for loadable modules.
*/
/* skip anything but files with G_MODULE_SUFFIX */
dot = strrchr(name, '.');
if (dot == NULL || strcmp(dot+1, G_MODULE_SUFFIX) != 0)
continue;
#endif
g_snprintf(filename, FILENAME_LEN, "%s" G_DIR_SEPARATOR_S "%s",
dirname, name);
if ((handle = g_module_open(filename, 0)) == NULL)
{
g_warning("Couldn't load module %s: %s", filename,
g_module_error());
continue;
}
if (!g_module_symbol(handle, "version", &gp))
{
g_warning("The plugin %s has no version symbol", name);
g_module_close(handle);
continue;
}
version = gp;
if (g_module_symbol(handle, "register_wtap_module", &gp))
{
void (*register_wtap_module)(void) = gp;
register_wtap_module();
}
}
eth_dir_close(dir);
}
#if GLIB_MAJOR_VERSION < 2
g_free(hack_path);
g_free(dirname);
#endif
}

View File

@ -84,10 +84,12 @@ wtap_file_tsprecision(wtap *wth)
}
/* Table of the encapsulation types we know about. */
static const struct encap_type_info {
struct encap_type_info {
const char *name;
const char *short_name;
} encap_table[WTAP_NUM_ENCAP_TYPES] = {
};
static struct encap_type_info encap_table_base[] = {
/* WTAP_ENCAP_UNKNOWN */
{ "Unknown", NULL },
@ -377,6 +379,41 @@ static const struct encap_type_info {
{ "MPEG", "mpeg" },
};
gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
static GArray* encap_table_arr = NULL;
static const struct encap_type_info* encap_table = NULL;
static void wtap_init_encap_types(void) {
if (encap_table_arr) return;
encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
encap_table = (void*)encap_table_arr->data;
}
int wtap_get_num_encap_types(void) {
wtap_init_encap_types();
return wtap_num_encap_types;
}
int wtap_register_encap_type(char* name, char* short_name) {
struct encap_type_info* e = g_malloc(sizeof(struct encap_type_info));
wtap_init_encap_types();
e->name = g_strdup(name);
e->short_name = g_strdup(short_name);
g_array_append_val(encap_table_arr,e);
encap_table = (void*)encap_table_arr->data;
return wtap_num_encap_types++;
}
/* Name that should be somewhat descriptive. */
const char
*wtap_encap_string(int encap)

View File

@ -1,4 +1,5 @@
EXPORTS
wtap_buf_ptr
wtap_close
wtap_dump
@ -20,6 +21,7 @@ wtap_file_type
wtap_file_type_short_string
wtap_file_type_string
wtap_get_bytes_dumped
wtap_get_num_encap_types
wtap_set_bytes_dumped
wtap_open_offline
wtap_pcap_encap_to_wtap_encap
@ -28,6 +30,8 @@ wtap_process_pcap_packet
wtap_pseudoheader
wtap_read
wtap_read_so_far
wtap_register_open_routine
wtap_register_open_routine
wtap_seek_read
wtap_sequential_close
wtap_short_string_to_encap

View File

@ -192,8 +192,7 @@ extern "C" {
#define WTAP_ENCAP_USB_LINUX 95
#define WTAP_ENCAP_MPEG 96
/* last WTAP_ENCAP_ value + 1 */
#define WTAP_NUM_ENCAP_TYPES 97
#define WTAP_NUM_ENCAP_TYPES wtap_get_num_encap_types()
/* File types that can be read by wiretap.
We support writing some many of these file types, too, so we
@ -247,7 +246,8 @@ extern "C" {
#define WTAP_FILE_MPEG 46
#define WTAP_FILE_K12TEXT 47
#define WTAP_NUM_FILE_TYPES 48
#define WTAP_NUM_FILE_TYPES wtap_num_file_types
extern gint wtap_num_file_types;
/* timestamp precision (currently only these values are supported) */
#define WTAP_FILE_TSPREC_SEC 0
@ -639,6 +639,39 @@ struct wtap_dumper;
typedef struct wtap wtap;
typedef struct wtap_dumper wtap_dumper;
struct file_type_info {
/* the file type name */
/* should be NULL for all "pseudo" types that are only internally used and not read/writeable */
const char *name;
/* the file type short name, used as a shortcut for the command line tools */
/* should be NULL for all "pseudo" types that are are only internally used and not read/writeable */
const char *short_name;
/* the common file extensions for this type (seperated by semicolon) */
/* should be *.* if no common extension is applicable */
const char *file_extensions;
/* the default file extension, used to save this type */
/* should be NULL if no default extension is known */
const char *file_extension_default;
/* can this type be compressed with gzip? */
gboolean can_compress;
/* can this type write this encapsulation format? */
/* should be NULL is this file type don't have write support */
int (*can_write_encap)(int);
/* the function to open the capture file for writing */
/* should be NULL is this file type don't have write support */
int (*dump_open)(wtap_dumper *, gboolean, int *);
};
typedef int (*wtap_open_routine_t)(struct wtap*, int *, char **);
/*
* On failure, "wtap_open_offline()" returns NULL, and puts into the
* "int" pointed to by its second argument:
@ -703,7 +736,11 @@ void wtap_dump_flush(wtap_dumper *);
gboolean wtap_dump_close(wtap_dumper *, int *);
gint64 wtap_get_bytes_dumped(wtap_dumper *);
void wtap_set_bytes_dumped(wtap_dumper *wdh, gint64 bytes_dumped);
int wtap_get_num_encap_types(void);
void wtap_register_open_routine(wtap_open_routine_t,gboolean);
int wtap_register_encap_type(char* name, char* short_name);
void wtap_register_file_type(const struct file_type_info*);
void wtap_load_plugins(char*);
/*
* Wiretap error codes.
*/