Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / epan / frame_data.c
1 /* frame_data.c
2  * Routines for packet disassembly
3  *
4  * $Id$
5  *
6  * Ethereal - 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
33 #include <glib.h>
34
35 /* Protocol-specific data attached to a frame_data structure - protocol
36    index and opaque pointer. */
37 typedef struct _frame_proto_data {
38   int proto;
39   void *proto_data;
40 } frame_proto_data;
41
42 static GMemChunk *frame_proto_data_area = NULL;
43
44 /*
45  * Free up any space allocated for frame proto data areas and then
46  * allocate a new area.
47  *
48  * We can free the area, as the structures it contains are pointed to by
49  * frames, that will be freed as well.
50  */
51 static void
52 packet_init_protocol(void)
53 {
54
55   if (frame_proto_data_area)
56     g_mem_chunk_destroy(frame_proto_data_area);
57
58   frame_proto_data_area = g_mem_chunk_new("frame_proto_data_area",
59                                           sizeof(frame_proto_data),
60                                           20 * sizeof(frame_proto_data), /* FIXME*/
61                                           G_ALLOC_ONLY);
62
63 }
64
65 void
66 frame_data_init(void)
67 {
68         register_init_routine(&packet_init_protocol);
69 }
70
71 void
72 frame_data_cleanup(void)
73 {
74   /* this function intentionally left blank :) */
75 }
76
77 /* XXX - I declared this static, because it only seems to be used by
78  * p_get_proto_data and p_add_proto_data
79  */
80 static gint p_compare(gconstpointer a, gconstpointer b)
81 {
82   const frame_proto_data *ap = (const frame_proto_data *)a;
83   const frame_proto_data *bp = (const frame_proto_data *)b;
84
85   if (ap -> proto > bp -> proto)
86     return 1;
87   else if (ap -> proto == bp -> proto)
88     return 0;
89   else
90     return -1;
91
92 }
93
94
95 void
96 p_add_proto_data(frame_data *fd, int proto, void *proto_data)
97 {
98   frame_proto_data *p1 = g_mem_chunk_alloc(frame_proto_data_area);
99
100   g_assert(p1 != NULL);
101
102   p1 -> proto = proto;
103   p1 -> proto_data = proto_data;
104
105   /* Add it to the GSLIST */
106
107   fd -> pfd = g_slist_insert_sorted(fd -> pfd,
108                                     (gpointer *)p1,
109                                     p_compare);
110
111 }
112
113 void *
114 p_get_proto_data(frame_data *fd, int proto)
115 {
116   frame_proto_data temp, *p1;
117   GSList *item;
118
119   temp.proto = proto;
120   temp.proto_data = NULL;
121
122   item = g_slist_find_custom(fd->pfd, (gpointer *)&temp, p_compare);
123
124   if (item) {
125     p1 = (frame_proto_data *)item->data;
126     return p1->proto_data;
127   }
128
129   return NULL;
130
131 }
132
133 void
134 p_rem_proto_data(frame_data *fd, int proto)
135 {
136   frame_proto_data temp;
137   GSList *item;
138
139   temp.proto = proto;
140   temp.proto_data = NULL;
141
142   item = g_slist_find_custom(fd->pfd, (gpointer *)&temp, p_compare);
143
144   if (item) {
145     fd->pfd = g_slist_remove(fd->pfd, item->data);
146   }
147 }