wireshark/epan/dissectors/packet-k12.c

200 lines
5.1 KiB
C

/* packet-k12.c
* Routines for displaying frames from k12 rf5 files
*
* Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998
*
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <epan/packet.h>
#include <prefs.h>
static int proto_k12 = -1;
static int hf_k12_port_id = -1;
static int hf_k12_port_name = -1;
static int hf_k12_stack_file = -1;
static gint ett_k12 = -1;
static dissector_handle_t k12_handle;
static dissector_handle_t data_handle;
static module_t *k12_module;
static char* k12_config_filename = "";
static GHashTable* k12_cfg = NULL;
static void dissect_k12(tvbuff_t* tvb,packet_info* pinfo,proto_tree* tree) {
proto_item* k12_item;
proto_tree* k12_tree;
dissector_handle_t sub_handle;
k12_item = proto_tree_add_protocol_format(tree, proto_k12, tvb, 0, 0, "Packet from: '%s' (0x%.8x)",
pinfo->pseudo_header->k12.src_name,
pinfo->pseudo_header->k12.src_id);
k12_tree = proto_item_add_subtree(k12_item, ett_k12);
proto_tree_add_uint(k12_tree, hf_k12_port_id, tvb, 0,0,pinfo->pseudo_header->k12.src_id);
proto_tree_add_string(k12_tree, hf_k12_port_name, tvb, 0,0,pinfo->pseudo_header->k12.src_name);
proto_tree_add_string(k12_tree, hf_k12_stack_file, tvb, 0,0,pinfo->pseudo_header->k12.stack_file);
if (! k12_cfg ) {
sub_handle = data_handle;
} else {
sub_handle = g_hash_table_lookup(k12_cfg,pinfo->pseudo_header->k12.stack_file);
if (! sub_handle )
sub_handle = data_handle;
}
call_dissector(sub_handle, tvb, pinfo, tree);
}
static gboolean free_just_key (gpointer k, gpointer v _U_, gpointer p _U_) {
g_free(k);
return TRUE;
}
static GHashTable* k12_load_config(FILE* fp) {
gchar buffer[0x10000];
size_t len;
GHashTable* hash = g_hash_table_new(g_str_hash,g_str_equal);
gchar** curr;
gchar** lines = NULL;
guint i;
dissector_handle_t handle;
len = fread(buffer,1,0xFFFF,fp);
if (len > 0) {
lines = g_strsplit(buffer,"\n",0);
for (i = 0 ; lines[i]; i++) {
g_strstrip(lines[i]);
if(*(lines[i]) == '#') continue;
if(*(lines[i]) == '\0') break;
curr = g_strsplit(lines[i]," ",0);
if (! (curr[0] != NULL && *curr[0] != '\0' && curr[1] != NULL && *curr[1] != '\0' ) ) {
/* XXX report_error */
g_warning("K12xx: Format error in line %u",i+1);
g_strfreev(curr);
g_strfreev(lines);
g_hash_table_foreach_remove(hash,free_just_key,NULL);
g_hash_table_destroy(hash);
return NULL;
}
g_strstrip(curr[0]);
g_strstrip(curr[1]);
handle = find_dissector(curr[1]);
if (! handle ) {
/* XXX report_error */
g_warning("k12: proto %s not found",curr[1]);
handle = data_handle;
}
g_hash_table_insert(hash,g_strdup(curr[0]),handle);
g_strfreev(curr);
}
g_strfreev(lines);
return hash;
}
g_hash_table_destroy(hash);
/* XXX report_error */
g_warning("K12xx: I/O error");
return NULL;
}
static void k12_load_prefs(void) {
FILE* fp;
if (k12_cfg) {
g_hash_table_foreach_remove(k12_cfg,free_just_key,NULL);
g_hash_table_destroy(k12_cfg);
k12_cfg = NULL;
}
if (*k12_config_filename != '\0') {
if (( fp = fopen(k12_config_filename,"r") )) {
k12_cfg = k12_load_config(fp);
return;
} else {
/* XXX report_error */
g_warning("failed to open: %s",k12_config_filename);
}
}
}
void proto_reg_handoff_k12(void) {
k12_handle = find_dissector("k12");
data_handle = find_dissector("data");
dissector_add("wtap_encap", WTAP_ENCAP_K12, k12_handle);
}
void
proto_register_k12(void)
{
static hf_register_info hf[] = {
{ &hf_k12_port_id, { "Port Id", "k12.port_id", FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }},
{ &hf_k12_port_name, { "Port Name", "k12.port_name", FT_STRING, BASE_NONE, NULL, 0x0,"", HFILL }},
{ &hf_k12_stack_file, { "Stack file used", "k12.stack_file", FT_STRING, BASE_NONE, NULL, 0x0,"", HFILL }}
};
static gint *ett[] = {
&ett_k12,
};
proto_k12 = proto_register_protocol("K12xx", "K12xx", "k12");
proto_register_field_array(proto_k12, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
register_dissector("k12", dissect_k12, proto_k12);
k12_module = prefs_register_protocol(proto_k12, k12_load_prefs);
prefs_register_string_preference(k12_module, "config",
"Configuration filename",
"K12 module configuration filename",
&k12_config_filename);
}