/* packet.c
* Routines for packet disassembly
*
- * $Id: packet.c,v 1.52 1999/10/29 02:25:54 guy Exp $
+ * $Id: packet.c,v 1.93 2000/05/31 03:58:55 gram Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
#include <glib.h>
#include <stdio.h>
+#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
# include <netinet/in.h>
#endif
+#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
+#endif
#ifdef NEED_INET_V6DEFS_H
# include "inet_v6defs.h"
#include "packet.h"
#include "print.h"
+#include "timestamp.h"
#include "file.h"
+#include "packet-ascend.h"
+#include "packet-atalk.h"
+#include "packet-atm.h"
+#include "packet-clip.h"
+#include "packet-eth.h"
+#include "packet-fddi.h"
+#include "packet-ipv6.h"
+#include "packet-lapb.h"
+#include "packet-lapd.h"
+#include "packet-null.h"
+#include "packet-ppp.h"
+#include "packet-raw.h"
+#include "packet-sna.h"
+#include "packet-tr.h"
+#include "packet-v120.h"
+#include "packet-vines.h"
+
+#ifndef __RESOLV_H__
+#include "resolv.h"
+#endif
+
+#ifndef __TVBUFF_H__
+#include "tvbuff.h"
+#endif
+
+#include "plugins.h"
+
extern capture_file cf;
-gboolean tree_is_expanded[NUM_TREE_TYPES];
+static int proto_frame = -1;
+static int hf_frame_arrival_time = -1;
+static int hf_frame_time_delta = -1;
+static int hf_frame_number = -1;
+static int hf_frame_packet_len = -1;
+static int hf_frame_capture_len = -1;
+static int proto_short = -1;
+static int proto_malformed = -1;
+
+static gint ett_frame = -1;
+
+GMemChunk *frame_proto_data_area = NULL;
+
+/*
+ * Free up any space allocated for frame proto data areas and then
+ * allocate a new area.
+ *
+ * We can free the area, as the structures it contains are pointed to by
+ * frames, that will be freed as well.
+ */
+static void
+packet_init_protocol(void)
+{
+
+ if (frame_proto_data_area)
+ g_mem_chunk_destroy(frame_proto_data_area);
+
+ frame_proto_data_area = g_mem_chunk_new("frame_proto_data_area",
+ sizeof(frame_proto_data),
+ 20 * sizeof(frame_proto_data), /* FIXME*/
+ G_ALLOC_ONLY);
+
+}
-int proto_frame = -1;
-int hf_frame_arrival_time = -1;
-int hf_frame_time_delta = -1;
-int hf_frame_number = -1;
-int hf_frame_packet_len = -1;
-int hf_frame_capture_len = -1;
+/* Wrapper for the most common case of asking
+ * for a string using a colon as the hex-digit separator.
+ */
+gchar *
+ether_to_str(const guint8 *ad)
+{
+ return ether_to_str_punct(ad, ':');
+}
+/* Places char punct in the string as the hex-digit separator.
+ * If punct is '\0', no punctuation is applied (and thus
+ * the resulting string is 5 bytes shorter)
+ */
gchar *
-ether_to_str(const guint8 *ad) {
+ether_to_str_punct(const guint8 *ad, char punct) {
static gchar str[3][18];
static gchar *cur;
gchar *p;
*--p = hex_digits[octet&0xF];
if (i == 0)
break;
- *--p = ':';
+ if (punct)
+ *--p = punct;
i--;
}
return p;
cur = &str[0][0];
}
+ if (time == 0) {
+ sprintf(cur, "0 time");
+ return cur;
+ }
+
secs = time % 60;
time /= 60;
mins = time % 60;
}
/* Max string length for displaying byte string. */
-#define MAX_BYTE_STR_LEN 20
+#define MAX_BYTE_STR_LEN 32
/* Turn an array of bytes into a string showing the bytes in hex. */
+#define N_BYTES_TO_STR_STRINGS 6
gchar *
bytes_to_str(const guint8 *bd, int bd_len) {
- static gchar str[3][MAX_BYTE_STR_LEN+3+1];
- static gchar *cur;
+ static gchar str[N_BYTES_TO_STR_STRINGS][MAX_BYTE_STR_LEN+3+1];
+ static int cur_idx;
+ gchar *cur;
gchar *p;
int len;
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
- if (cur == &str[0][0]) {
- cur = &str[1][0];
- } else if (cur == &str[1][0]) {
- cur = &str[2][0];
- } else {
- cur = &str[0][0];
- }
+ cur_idx++;
+ if (cur_idx >= N_BYTES_TO_STR_STRINGS)
+ cur_idx = 0;
+ cur = &str[cur_idx][0];
p = cur;
len = MAX_BYTE_STR_LEN;
while (bd_len > 0 && len > 0) {
* Put "..." and quit.
*/
strcpy(fmtbufp, " ...");
+ fmtbufp += 4;
break;
}
c = *string++;
}
}
}
+
+/* To do: Add check_col checks to the col_add* routines */
+
+static void
+col_set_abs_time(frame_data *fd, int col)
+{
+ struct tm *tmp;
+ time_t then;
+
+ then = fd->abs_secs;
+ tmp = localtime(&then);
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%02d:%02d:%02d.%04ld",
+ tmp->tm_hour,
+ tmp->tm_min,
+ tmp->tm_sec,
+ (long)fd->abs_usecs/100);
+}
+
+static void
+col_set_rel_time(frame_data *fd, int col)
+{
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%d.%06d", fd->rel_secs,
+ fd->rel_usecs);
+}
+
+static void
+col_set_delta_time(frame_data *fd, int col)
+{
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%d.%06d", fd->del_secs,
+ fd->del_usecs);
+}
+
+/* Add "command-line-specified" time.
+ XXX - this is called from "file.c" when the user changes the time
+ format they want for "command-line-specified" time; it's a bit ugly
+ that we have to export it, but if we go to a CList-like widget that
+ invokes callbacks to get the text for the columns rather than
+ requiring us to stuff the text into the widget from outside, we
+ might be able to clean this up. */
+void
+col_set_cls_time(frame_data *fd, int col)
+{
+ switch (timestamp_type) {
+ case ABSOLUTE:
+ col_set_abs_time(fd, col);
+ break;
+
+ case RELATIVE:
+ col_set_rel_time(fd, col);
+ break;
+
+ case DELTA:
+ col_set_delta_time(fd, col);
+ break;
+ }
+}
+
+static void
+col_set_addr(frame_data *fd, int col, address *addr, gboolean is_res)
+{
+ u_int ipv4_addr;
+ struct e_in6_addr ipv6_addr;
+ struct atalk_ddp_addr ddp_addr;
+ struct sna_fid_type_4_addr sna_fid_type_4_addr;
+
+ switch (addr->type) {
+
+ case AT_ETHER:
+ if (is_res)
+ strncpy(fd->cinfo->col_data[col], get_ether_name(addr->data), COL_MAX_LEN);
+ else
+ strncpy(fd->cinfo->col_data[col], ether_to_str(addr->data), COL_MAX_LEN);
+ break;
+
+ case AT_IPv4:
+ memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
+ if (is_res)
+ strncpy(fd->cinfo->col_data[col], get_hostname(ipv4_addr), COL_MAX_LEN);
+ else
+ strncpy(fd->cinfo->col_data[col], ip_to_str(addr->data), COL_MAX_LEN);
+ break;
+
+ case AT_IPv6:
+ memcpy(&ipv6_addr.s6_addr, addr->data, sizeof ipv6_addr.s6_addr);
+ if (is_res)
+ strncpy(fd->cinfo->col_data[col], get_hostname6(&ipv6_addr), COL_MAX_LEN);
+ else
+ strncpy(fd->cinfo->col_data[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN);
+ break;
+
+ case AT_IPX:
+ strncpy(fd->cinfo->col_data[col],
+ ipx_addr_to_str(pntohl(&addr->data[0]), &addr->data[4]), COL_MAX_LEN);
+ break;
+
+ case AT_SNA:
+ switch (addr->len) {
+
+ case 1:
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%04X", addr->data[0]);
+ break;
+
+ case 2:
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%04X",
+ pntohs(&addr->data[0]));
+ break;
+
+ case SNA_FID_TYPE_4_ADDR_LEN:
+ memcpy(&sna_fid_type_4_addr, addr->data, SNA_FID_TYPE_4_ADDR_LEN);
+ strncpy(fd->cinfo->col_data[col],
+ sna_fid_type_4_addr_to_str(&sna_fid_type_4_addr), COL_MAX_LEN);
+ break;
+ }
+ break;
+
+ case AT_ATALK:
+ memcpy(&ddp_addr, addr->data, sizeof ddp_addr);
+ strncpy(fd->cinfo->col_data[col], atalk_addr_to_str(&ddp_addr),
+ COL_MAX_LEN);
+ break;
+
+ case AT_VINES:
+ strncpy(fd->cinfo->col_data[col], vines_addr_to_str(&addr->data[0]),
+ COL_MAX_LEN);
+ break;
+
+ default:
+ break;
+ }
+ fd->cinfo->col_data[col][COL_MAX_LEN - 1] = '\0';
+}
+
+static void
+col_set_port(frame_data *fd, int col, port_type ptype, guint32 port,
+ gboolean is_res)
+{
+ switch (ptype) {
+
+ case PT_TCP:
+ if (is_res)
+ strncpy(fd->cinfo->col_data[col], get_tcp_port(port), COL_MAX_LEN);
+ else
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%u", port);
+ break;
+
+ case PT_UDP:
+ if (is_res)
+ strncpy(fd->cinfo->col_data[col], get_udp_port(port), COL_MAX_LEN);
+ else
+ snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%u", port);
+ break;
+
+ default:
+ break;
+ }
+ fd->cinfo->col_data[col][COL_MAX_LEN - 1] = '\0';
+}
+
+void
+fill_in_columns(frame_data *fd)
+{
+ int i;
+
+ for (i = 0; i < fd->cinfo->num_cols; i++) {
+ switch (fd->cinfo->col_fmt[i]) {
+
+ case COL_NUMBER:
+ snprintf(fd->cinfo->col_data[i], COL_MAX_LEN, "%u", fd->num);
+ break;
+
+ case COL_CLS_TIME:
+ col_set_cls_time(fd, i);
+ break;
+
+ case COL_ABS_TIME:
+ col_set_abs_time(fd, i);
+ break;
+
+ case COL_REL_TIME:
+ col_set_rel_time(fd, i);
+ break;
+
+ case COL_DELTA_TIME:
+ col_set_delta_time(fd, i);
+ break;
+
+ case COL_DEF_SRC:
+ case COL_RES_SRC: /* COL_DEF_SRC is currently just like COL_RES_SRC */
+ col_set_addr(fd, i, &pi.src, TRUE);
+ break;
+
+ case COL_UNRES_SRC:
+ col_set_addr(fd, i, &pi.src, FALSE);
+ break;
+
+ case COL_DEF_DL_SRC:
+ case COL_RES_DL_SRC:
+ col_set_addr(fd, i, &pi.dl_src, TRUE);
+ break;
+
+ case COL_UNRES_DL_SRC:
+ col_set_addr(fd, i, &pi.dl_src, FALSE);
+ break;
+
+ case COL_DEF_NET_SRC:
+ case COL_RES_NET_SRC:
+ col_set_addr(fd, i, &pi.net_src, TRUE);
+ break;
+
+ case COL_UNRES_NET_SRC:
+ col_set_addr(fd, i, &pi.net_src, FALSE);
+ break;
+
+ case COL_DEF_DST:
+ case COL_RES_DST: /* COL_DEF_DST is currently just like COL_RES_DST */
+ col_set_addr(fd, i, &pi.dst, TRUE);
+ break;
+
+ case COL_UNRES_DST:
+ col_set_addr(fd, i, &pi.dst, FALSE);
+ break;
+
+ case COL_DEF_DL_DST:
+ case COL_RES_DL_DST:
+ col_set_addr(fd, i, &pi.dl_dst, TRUE);
+ break;
+
+ case COL_UNRES_DL_DST:
+ col_set_addr(fd, i, &pi.dl_dst, FALSE);
+ break;
+
+ case COL_DEF_NET_DST:
+ case COL_RES_NET_DST:
+ col_set_addr(fd, i, &pi.net_dst, TRUE);
+ break;
+
+ case COL_UNRES_NET_DST:
+ col_set_addr(fd, i, &pi.net_dst, FALSE);
+ break;
+
+ case COL_DEF_SRC_PORT:
+ case COL_RES_SRC_PORT: /* COL_DEF_SRC_PORT is currently just like COL_RES_SRC_PORT */
+ col_set_port(fd, i, pi.ptype, pi.srcport, TRUE);
+ break;
+
+ case COL_UNRES_SRC_PORT:
+ col_set_port(fd, i, pi.ptype, pi.srcport, FALSE);
+ break;
+
+ case COL_DEF_DST_PORT:
+ case COL_RES_DST_PORT: /* COL_DEF_DST_PORT is currently just like COL_RES_DST_PORT */
+ col_set_port(fd, i, pi.ptype, pi.destport, TRUE);
+ break;
+
+ case COL_UNRES_DST_PORT:
+ col_set_port(fd, i, pi.ptype, pi.destport, FALSE);
+ break;
+
+ case COL_PROTOCOL: /* currently done by dissectors */
+ case COL_INFO: /* currently done by dissectors */
+ break;
+
+ case COL_PACKET_LENGTH:
+ snprintf(fd->cinfo->col_data[i], COL_MAX_LEN, "%d", fd->pkt_len);
+ break;
+
+ case NUM_COL_FMTS: /* keep compiler happy - shouldn't get here */
+ break;
+ }
+ }
+}
void blank_packetinfo(void)
{
pi.ptype = PT_NONE;
pi.srcport = 0;
pi.destport = 0;
+ pi.current_proto = "<Missing Protocol Name>";
+}
+
+/* Do all one-time initialization. */
+void
+dissect_init(void)
+{
+ except_init();
+ tvbuff_init();
+ proto_init();
+ dfilter_init();
+#ifdef HAVE_PLUGINS
+ init_plugins();
+#endif
+}
+
+void
+dissect_cleanup(void)
+{
+ dfilter_cleanup();
+ proto_cleanup();
+ tvbuff_cleanup();
+ except_deinit();
+}
+
+/* Allow protocols to register "init" routines, which are called before
+ we make a pass through a capture file and dissect all its packets
+ (e.g., when we read in a new capture file, or run a "filter packets"
+ or "colorize packets" pass over the current capture file). */
+static GSList *init_routines;
+
+void
+register_init_routine(void (*func)(void))
+{
+ init_routines = g_slist_append(init_routines, func);
+}
+
+/* Call all the registered "init" routines. */
+static void
+call_init_routine(gpointer routine, gpointer dummy)
+{
+ void (*func)(void) = routine;
+
+ (*func)();
+}
+
+void
+init_all_protocols(void)
+{
+ g_slist_foreach(init_routines, &call_init_routine, NULL);
}
/* this routine checks the frame type from the cf structure */
void
-dissect_packet(const u_char *pd, frame_data *fd, proto_tree *tree)
+dissect_packet(union wtap_pseudo_header *pseudo_header, const u_char *pd,
+ frame_data *fd, proto_tree *tree)
{
proto_tree *fh_tree;
proto_item *ti;
struct timeval tv;
+ tvbuff_t *tvb;
/* Put in frame header information. */
if (tree) {
- ti = proto_tree_add_item_format(tree, proto_frame, 0, fd->cap_len,
- NULL, "Frame (%d on wire, %d captured)", fd->pkt_len, fd->cap_len);
+ ti = proto_tree_add_protocol_format(tree, proto_frame, NullTVB, 0, fd->cap_len,
+ "Frame %u (%u on wire, %u captured)", fd->num,
+ fd->pkt_len, fd->cap_len);
- fh_tree = proto_item_add_subtree(ti, ETT_FRAME);
+ fh_tree = proto_item_add_subtree(ti, ett_frame);
tv.tv_sec = fd->abs_secs;
tv.tv_usec = fd->abs_usecs;
- proto_tree_add_item(fh_tree, hf_frame_arrival_time,
+ proto_tree_add_item(fh_tree, hf_frame_arrival_time, NullTVB,
0, 0, &tv);
tv.tv_sec = fd->del_secs;
tv.tv_usec = fd->del_usecs;
- proto_tree_add_item(fh_tree, hf_frame_time_delta,
+ proto_tree_add_item(fh_tree, hf_frame_time_delta, NullTVB,
0, 0, &tv);
- proto_tree_add_item(fh_tree, hf_frame_number,
+ proto_tree_add_item(fh_tree, hf_frame_number, NullTVB,
0, 0, fd->num);
- proto_tree_add_item_format(fh_tree, hf_frame_packet_len,
+ proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, NullTVB,
0, 0, fd->pkt_len, "Packet Length: %d byte%s", fd->pkt_len,
plurality(fd->pkt_len, "", "s"));
- proto_tree_add_item_format(fh_tree, hf_frame_capture_len,
+ proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, NullTVB,
0, 0, fd->cap_len, "Capture Length: %d byte%s", fd->cap_len,
plurality(fd->cap_len, "", "s"));
}
pi.len = fd->pkt_len;
pi.captured_len = fd->cap_len;
- switch (fd->lnk_t) {
- case WTAP_ENCAP_ETHERNET :
- dissect_eth(pd, 0, fd, tree);
- break;
- case WTAP_ENCAP_FDDI :
- dissect_fddi(pd, fd, tree, FALSE);
- break;
- case WTAP_ENCAP_FDDI_BITSWAPPED :
- dissect_fddi(pd, fd, tree, TRUE);
- break;
- case WTAP_ENCAP_TR :
- dissect_tr(pd, 0, fd, tree);
- break;
- case WTAP_ENCAP_NULL :
- dissect_null(pd, fd, tree);
- break;
- case WTAP_ENCAP_PPP :
- dissect_ppp(pd, fd, tree);
- break;
- case WTAP_ENCAP_LAPB :
- dissect_lapb(pd, fd, tree);
- break;
- case WTAP_ENCAP_RAW_IP :
- dissect_raw(pd, fd, tree);
- break;
- case WTAP_ENCAP_LINUX_ATM_CLIP :
- dissect_clip(pd, fd, tree);
- break;
- case WTAP_ENCAP_ATM_SNIFFER :
- dissect_atm(pd, fd, tree);
- break;
- case WTAP_ENCAP_ASCEND :
- dissect_ascend(pd, fd, tree);
- break;
+ tvb = tvb_new_real_data(pd, fd->cap_len, fd->pkt_len);
+ pi.fd = fd;
+ pi.compat_top_tvb = tvb;
+ pi.pseudo_header = pseudo_header;
+
+ TRY {
+ switch (fd->lnk_t) {
+ case WTAP_ENCAP_ETHERNET :
+ dissect_eth(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_FDDI :
+ dissect_fddi(tvb, &pi, tree, FALSE);
+ break;
+ case WTAP_ENCAP_FDDI_BITSWAPPED :
+ dissect_fddi(tvb, &pi, tree, TRUE);
+ break;
+ case WTAP_ENCAP_TR :
+ dissect_tr(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_NULL :
+ dissect_null(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_PPP :
+ dissect_ppp(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_LAPB :
+ dissect_lapb(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_RAW_IP :
+ dissect_raw(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_LINUX_ATM_CLIP :
+ dissect_clip(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_ATM_SNIFFER :
+ dissect_atm(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_ASCEND :
+ dissect_ascend(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_LAPD :
+ dissect_lapd(tvb, &pi, tree);
+ break;
+ case WTAP_ENCAP_V120 :
+ dissect_v120(tvb, &pi, tree);
+ break;
+ }
+ }
+ CATCH(BoundsError) {
+ proto_tree_add_protocol_format(tree, proto_short, NullTVB, 0, 0,
+ "[Short Frame: %s]", pi.current_proto );
}
+ CATCH(ReportedBoundsError) {
+ proto_tree_add_protocol_format(tree, proto_malformed, NullTVB, 0, 0,
+ "[Malformed Frame: %s]", pi.current_proto );
+ }
+ ENDTRY;
+
+ /* Free all tvb's created from this tvb, unless dissector
+ * wanted to store the pointer (in which case, the dissector
+ * would have incremented the usage count on that tvbuff_t*) */
+ tvb_free_chain(tvb);
+
+ fd->flags.visited = 1;
+}
+
+gint p_compare(gconstpointer a, gconstpointer b)
+{
+
+ if (((frame_proto_data *)a) -> proto > ((frame_proto_data *)b) -> proto)
+ return 1;
+ else if (((frame_proto_data *)a) -> proto == ((frame_proto_data *)b) -> proto)
+ return 0;
+ else
+ return -1;
+
+}
+
+void
+p_add_proto_data(frame_data *fd, int proto, void *proto_data)
+{
+ frame_proto_data *p1 = g_mem_chunk_alloc(frame_proto_data_area);
+
+ g_assert(p1 != NULL);
+
+ p1 -> proto = proto;
+ p1 -> proto_data = proto_data;
+
+ /* Add it to the GSLIST */
+
+ fd -> pfd = g_slist_insert_sorted(fd -> pfd,
+ (gpointer *)p1,
+ p_compare);
+
+}
+
+void *
+p_get_proto_data(frame_data *fd, int proto)
+{
+ frame_proto_data temp;
+ GSList *item;
+
+ temp.proto = proto;
+ temp.proto_data = NULL;
+
+ item = g_slist_find_custom(fd->pfd, (gpointer *)&temp, p_compare);
+
+ if (item) return (void *)item->data;
+
+ return NULL;
+
+}
+
+void
+p_rem_proto_data(frame_data *fd, int proto)
+{
+
+
}
void
{ "Capture Frame Length", "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
"" }},
};
+ static gint *ett[] = {
+ &ett_frame,
+ };
proto_frame = proto_register_protocol("Frame", "frame");
proto_register_field_array(proto_frame, hf, array_length(hf));
+ proto_register_subtree_array(ett, array_length(ett));
+
+ proto_short = proto_register_protocol("Short Frame", "short");
+ proto_malformed = proto_register_protocol("Malformed Frame", "malformed");
+ register_init_routine(&packet_init_protocol);
+
+}
+
+/*********************** code added for sub-dissector lookup *********************/
+
+static GHashTable *dissector_tables = NULL;
+
+/* Finds a dissector table by field name. */
+static dissector_table_t
+find_dissector_table(const char *name)
+{
+ g_assert(dissector_tables);
+ return g_hash_table_lookup( dissector_tables, name );
+}
+
+/* lookup a dissector based upon pattern. */
+dissector_t
+dissector_lookup( dissector_table_t table, guint32 pattern)
+{
+ return g_hash_table_lookup( table, GUINT_TO_POINTER( pattern));
+}
+
+
+/* add an entry, lookup the dissector table for the specified field name, */
+/* if a valid table found, add the subdissector */
+void
+dissector_add(const char *name, guint32 pattern, dissector_t dissector)
+{
+ dissector_table_t sub_dissectors = find_dissector_table( name);
+
+/* sanity check */
+ g_assert( sub_dissectors);
+
+/* do the table insertion */
+ g_hash_table_insert( sub_dissectors, GUINT_TO_POINTER( pattern),
+ (gpointer)dissector);
+}
+
+/* delete the entry for this dissector at this pattern */
+
+/* NOTE: this doesn't use the dissector call variable. It is included to */
+/* be consistant with the dissector_add and more importantly to be used */
+/* if the technique of adding a temporary dissector is implemented. */
+/* If temporary dissectors are deleted, then the original dissector must */
+/* be available. */
+void
+dissector_delete(const char *name, guint32 pattern, dissector_t dissector)
+{
+ dissector_table_t sub_dissectors = find_dissector_table( name);
+
+/* sanity check */
+ g_assert( sub_dissectors);
+
+/* remove the hash table entry */
+ g_hash_table_remove( sub_dissectors, GUINT_TO_POINTER( pattern));
+}
+
+/* Look for a given port in a given dissector table and, if found, call
+ the dissector with the arguments supplied, and return TRUE, otherwise
+ return FALSE. */
+gboolean
+dissector_try_port(dissector_table_t sub_dissectors, guint32 port,
+ const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+{
+ dissector_t subdissector;
+
+ subdissector = dissector_lookup(sub_dissectors, port);
+ if (subdissector != NULL) {
+ pi.match_port = port;
+ (subdissector)(pd, offset, fd, tree);
+ return TRUE;
+ } else
+ return FALSE;
+}
+
+dissector_table_t
+register_dissector_table(const char *name)
+{
+ dissector_table_t sub_dissectors;
+
+ /* Create our hash-of-hashes if it doesn't already exist */
+ if (!dissector_tables) {
+ dissector_tables = g_hash_table_new( g_str_hash, g_str_equal );
+ g_assert(dissector_tables);
+ }
+
+ /* Make sure the registration is unique */
+ g_assert(!g_hash_table_lookup( dissector_tables, name ));
+
+ /* Create and register the dissector table for this name; returns */
+ /* a pointer to the dissector table. */
+ sub_dissectors = g_hash_table_new( g_direct_hash, g_direct_equal );
+ g_hash_table_insert( dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
+ return sub_dissectors;
+}
+
+static GHashTable *heur_dissector_lists = NULL;
+
+/* Finds a heuristic dissector table by field name. */
+static heur_dissector_list_t *
+find_heur_dissector_list(const char *name)
+{
+ g_assert(heur_dissector_lists != NULL);
+ return g_hash_table_lookup(heur_dissector_lists, name);
+}
+
+void
+heur_dissector_add(const char *name, heur_dissector_t dissector)
+{
+ heur_dissector_list_t *sub_dissectors = find_heur_dissector_list(name);
+
+ /* sanity check */
+ g_assert(sub_dissectors != NULL);
+
+ /* do the table insertion */
+ *sub_dissectors = g_slist_append(*sub_dissectors, (gpointer)dissector);
+}
+
+gboolean
+dissector_try_heuristic(heur_dissector_list_t sub_dissectors,
+ const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+{
+ heur_dissector_t subdissector;
+ GSList *entry;
+
+ for (entry = sub_dissectors; entry != NULL; entry = g_slist_next(entry)) {
+ subdissector = (heur_dissector_t)entry->data;
+ if ((subdissector)(pd, offset, fd, tree))
+ return TRUE;
+ }
+ return FALSE;
+}
+
+void
+register_heur_dissector_list(const char *name, heur_dissector_list_t *sub_dissectors)
+{
+ /* Create our hash-of-hashes if it doesn't already exist */
+ if (heur_dissector_lists == NULL) {
+ heur_dissector_lists = g_hash_table_new(g_str_hash, g_str_equal);
+ g_assert(heur_dissector_lists != NULL);
+ }
+
+ /* Make sure the registration is unique */
+ g_assert(g_hash_table_lookup(heur_dissector_lists, name) == NULL);
+
+ *sub_dissectors = NULL; /* initially empty */
+ g_hash_table_insert(heur_dissector_lists, (gpointer)name,
+ (gpointer) sub_dissectors);
}