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