Zbee ZCL se: fix typo found by conflict hf
[metze/wireshark/wip.git] / epan / proto_data.c
1 /* proto_data.c
2  * Protocol-specific data
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #if 0
28 #include <epan/epan.h>
29 #include <wiretap/wtap.h>
30 #endif
31 #include <epan/wmem/wmem.h>
32 #include <epan/packet_info.h>
33 #include <epan/proto_data.h>
34 #include <epan/proto.h>
35 #if 0
36 #include <epan/packet.h>
37 #endif
38 #if 0
39 #include <epan/timestamp.h>
40 #endif
41
42 /* Protocol-specific data attached to a frame_data structure - protocol
43    index and opaque pointer. */
44 typedef struct _proto_data {
45   int   proto;
46   guint32 key;
47   void *proto_data;
48 } proto_data_t;
49
50 static gint
51 p_compare(gconstpointer a, gconstpointer b)
52 {
53   const proto_data_t *ap = (const proto_data_t *)a;
54   const proto_data_t *bp = (const proto_data_t *)b;
55
56   if (ap -> proto > bp -> proto) {
57     return 1;
58   } else if (ap -> proto == bp -> proto) {
59     if (ap->key > bp->key){
60       return 1;
61     } else if (ap -> key == bp -> key) {
62       return 0;
63     }
64     return -1;
65   } else {
66     return -1;
67   }
68 }
69
70 void
71 p_add_proto_data(wmem_allocator_t *tmp_scope, struct _packet_info* pinfo, int proto, guint32 key, void *proto_data)
72 {
73   proto_data_t     *p1;
74   GSList          **proto_list;
75   wmem_allocator_t *scope;
76
77   if (tmp_scope == pinfo->pool) {
78     scope = tmp_scope;
79     proto_list = &pinfo->proto_data;
80   } else if (tmp_scope == wmem_file_scope()) {
81     scope = wmem_file_scope();
82     proto_list = &pinfo->fd->pfd;
83   } else {
84     DISSECTOR_ASSERT(!"invalid wmem scope");
85   }
86
87   p1 = (proto_data_t *)wmem_alloc(scope, sizeof(proto_data_t));
88
89   p1->proto = proto;
90   p1->key = key;
91   p1->proto_data = proto_data;
92
93   /* Add it to the GSLIST */
94   *proto_list = g_slist_prepend(*proto_list, p1);
95 }
96
97 void *
98 p_get_proto_data(wmem_allocator_t *scope, struct _packet_info* pinfo, int proto, guint32 key)
99 {
100   proto_data_t  temp, *p1;
101   GSList       *item;
102
103   temp.proto = proto;
104   temp.key = key;
105   temp.proto_data = NULL;
106
107   if (scope == pinfo->pool) {
108     item = g_slist_find_custom(pinfo->proto_data, &temp, p_compare);
109   } else if (scope == wmem_file_scope()) {
110     item = g_slist_find_custom(pinfo->fd->pfd, &temp, p_compare);
111   } else {
112     DISSECTOR_ASSERT(!"invalid wmem scope");
113   }
114
115   if (item) {
116     p1 = (proto_data_t *)item->data;
117     return p1->proto_data;
118   }
119
120   return NULL;
121 }
122
123 void
124 p_remove_proto_data(wmem_allocator_t *scope, struct _packet_info* pinfo, int proto, guint32 key)
125 {
126   proto_data_t  temp;
127   GSList       *item;
128   GSList      **proto_list;
129
130   temp.proto = proto;
131   temp.key = key;
132   temp.proto_data = NULL;
133
134   if (scope == pinfo->pool) {
135     item = g_slist_find_custom(pinfo->fd->pfd, &temp, p_compare);
136     proto_list = &pinfo->proto_data;
137   } else if (scope == wmem_file_scope()) {
138     item = g_slist_find_custom(pinfo->fd->pfd, &temp, p_compare);
139     proto_list = &pinfo->fd->pfd;
140   } else {
141     DISSECTOR_ASSERT(!"invalid wmem scope");
142   }
143
144   if (item) {
145     *proto_list = g_slist_remove(*proto_list, item->data);
146   }
147 }
148
149 gchar *
150 p_get_proto_name_and_key(wmem_allocator_t *scope, struct _packet_info* pinfo, guint pfd_index){
151   proto_data_t  *temp;
152
153   if (scope == pinfo->pool) {
154     temp = (proto_data_t *)g_slist_nth_data(pinfo->proto_data, pfd_index);
155   } else if (scope == wmem_file_scope()) {
156     temp = (proto_data_t *)g_slist_nth_data(pinfo->fd->pfd, pfd_index);
157   } else {
158     DISSECTOR_ASSERT(!"invalid wmem scope");
159   }
160
161   return wmem_strdup_printf(wmem_packet_scope(),"[%s, key %u]",proto_get_protocol_name(temp->proto), temp->key);
162 }
163
164 /*
165  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
166  *
167  * Local variables:
168  * c-basic-offset: 2
169  * tab-width: 8
170  * indent-tabs-mode: nil
171  * End:
172  *
173  * vi: set shiftwidth=2 tabstop=8 expandtab:
174  * :indentSize=2:tabSize=8:noTabs=true:
175  */