name change
[obnox/wireshark/wip.git] / epan / frame_data.c
1 /* frame_data.c
2  * Routines for packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "frame_data.h"
31 #include "packet.h"
32 #include "emem.h"
33
34 #include <glib.h>
35
36 /* Protocol-specific data attached to a frame_data structure - protocol
37    index and opaque pointer. */
38 typedef struct _frame_proto_data {
39   int proto;
40   void *proto_data;
41 } frame_proto_data;
42
43 /* XXX - I declared this static, because it only seems to be used by
44  * p_get_proto_data and p_add_proto_data
45  */
46 static gint p_compare(gconstpointer a, gconstpointer b)
47 {
48   const frame_proto_data *ap = (const frame_proto_data *)a;
49   const frame_proto_data *bp = (const frame_proto_data *)b;
50
51   if (ap -> proto > bp -> proto)
52     return 1;
53   else if (ap -> proto == bp -> proto)
54     return 0;
55   else
56     return -1;
57
58 }
59
60
61 void
62 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
63 {
64   frame_proto_data *p1 = se_alloc(sizeof(frame_proto_data));
65
66   g_assert(p1 != NULL);
67
68   p1 -> proto = proto;
69   p1 -> proto_data = proto_data;
70
71   /* Add it to the GSLIST */
72
73   fd -> pfd = g_slist_insert_sorted(fd -> pfd,
74                                     (gpointer *)p1,
75                                     p_compare);
76
77 }
78
79 void *
80 p_get_proto_data(frame_data *fd, int proto)
81 {
82   frame_proto_data temp, *p1;
83   GSList *item;
84
85   temp.proto = proto;
86   temp.proto_data = NULL;
87
88   item = g_slist_find_custom(fd->pfd, (gpointer *)&temp, p_compare);
89
90   if (item) {
91     p1 = (frame_proto_data *)item->data;
92     return p1->proto_data;
93   }
94
95   return NULL;
96
97 }
98
99 void
100 p_remove_proto_data(frame_data *fd, int proto)
101 {
102   frame_proto_data temp;
103   GSList *item;
104
105   temp.proto = proto;
106   temp.proto_data = NULL;
107
108   item = g_slist_find_custom(fd->pfd, (gpointer *)&temp, p_compare);
109
110   if (item) {
111     fd->pfd = g_slist_remove(fd->pfd, item->data);
112   }
113 }