fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / h225-persistentdata.c
1 /*
2  * h225-persistentdata.c
3  * Source for lists and hash tables used in ethereal's h225 dissector
4  * for calculation of delays in h225-calls
5  *
6  * Copyright 2003 Lars Roland
7  *
8  * $Id$
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/conversation.h>
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include "h225-persistentdata.h"
41
42 /* Global Memory Chunks for lists and Global hash tables*/
43
44 static GMemChunk *h225ras_call_info_key_chunk = NULL;
45 static GMemChunk *h225ras_call_info_value_chunk = NULL;
46
47 static GHashTable *ras_calls[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
48
49 /*
50  * Functions needed for Ras-Hash-Table
51  */
52
53 /* compare 2 keys */
54 static gint h225ras_call_equal(gconstpointer k1, gconstpointer k2)
55 {
56         const h225ras_call_info_key* key1 = (const h225ras_call_info_key*) k1;
57         const h225ras_call_info_key* key2 = (const h225ras_call_info_key*) k2;
58
59         return (key1->reqSeqNum == key2->reqSeqNum &&
60             key1->conversation == key2->conversation);
61 }
62
63 /* calculate a hash key */
64 static guint h225ras_call_hash(gconstpointer k)
65 {
66         const h225ras_call_info_key* key = (const h225ras_call_info_key*) k;
67
68         return key->reqSeqNum + GPOINTER_TO_UINT(key->conversation);
69 }
70
71
72 h225ras_call_t * find_h225ras_call(h225ras_call_info_key *h225ras_call_key ,int category)
73 {
74         h225ras_call_t *h225ras_call = NULL;
75         h225ras_call = (h225ras_call_t *)g_hash_table_lookup(ras_calls[category], h225ras_call_key);
76
77         return h225ras_call;
78 }
79
80 h225ras_call_t * new_h225ras_call(h225ras_call_info_key *h225ras_call_key, packet_info *pinfo, guint8 *guid, int category)
81 {
82         h225ras_call_info_key *new_h225ras_call_key;
83         h225ras_call_t *h225ras_call = NULL;
84
85
86         /* Prepare the value data.
87            "req_num" and "rsp_num" are frame numbers;
88            frame numbers are 1-origin, so we use 0
89            to mean "we don't yet know in which frame
90            the reply for this call appears". */
91         new_h225ras_call_key = (h225ras_call_info_key *)g_mem_chunk_alloc(h225ras_call_info_key_chunk);
92         new_h225ras_call_key->reqSeqNum = h225ras_call_key->reqSeqNum;
93         new_h225ras_call_key->conversation = h225ras_call_key->conversation;
94         h225ras_call = (h225ras_call_t *)g_mem_chunk_alloc(h225ras_call_info_value_chunk);
95         h225ras_call->req_num = pinfo->fd->num;
96         h225ras_call->rsp_num = 0;
97         h225ras_call->requestSeqNum = h225ras_call_key->reqSeqNum;
98         h225ras_call->responded = FALSE;
99         h225ras_call->next_call = NULL;
100         h225ras_call->req_time.secs=pinfo->fd->abs_secs;
101         h225ras_call->req_time.nsecs=pinfo->fd->abs_usecs*1000;
102         memcpy(h225ras_call->guid, guid,16);
103         /* store it */
104         g_hash_table_insert(ras_calls[category], new_h225ras_call_key, h225ras_call);
105
106         return h225ras_call;
107 }
108
109 h225ras_call_t * append_h225ras_call(h225ras_call_t *prev_call, packet_info *pinfo, guint8 *guid, int category _U_)
110 {
111         h225ras_call_t *h225ras_call = NULL;
112
113         /* Prepare the value data.
114            "req_num" and "rsp_num" are frame numbers;
115            frame numbers are 1-origin, so we use 0
116            to mean "we don't yet know in which frame
117            the reply for this call appears". */
118         h225ras_call = (h225ras_call_t *)g_mem_chunk_alloc(h225ras_call_info_value_chunk);
119         h225ras_call->req_num = pinfo->fd->num;
120         h225ras_call->rsp_num = 0;
121         h225ras_call->requestSeqNum = prev_call->requestSeqNum;
122         h225ras_call->responded = FALSE;
123         h225ras_call->next_call = NULL;
124         h225ras_call->req_time.secs=pinfo->fd->abs_secs;
125         h225ras_call->req_time.nsecs=pinfo->fd->abs_usecs*1000;
126         memcpy(h225ras_call->guid, guid,16);
127
128         prev_call->next_call = h225ras_call;
129         return h225ras_call;
130 }
131
132
133 /* Init routine for hash tables and delay calculation
134    This routine will be called by Ethereal, before it
135    is (re-)dissecting a trace file from beginning.
136    We need to discard and init any state we've saved */
137
138 void
139 h225_init_routine(void)
140 {
141         int i;
142
143         /* free hash-tables and mem_chunks for RAS SRT */
144         for(i=0;i<7;i++) {
145                 if (ras_calls[i] != NULL) {
146                         g_hash_table_destroy(ras_calls[i]);
147                         ras_calls[i] = NULL;
148                 }
149         }
150
151         if (h225ras_call_info_key_chunk != NULL) {
152                 g_mem_chunk_destroy(h225ras_call_info_key_chunk);
153                 h225ras_call_info_key_chunk = NULL;
154         }
155         if (h225ras_call_info_value_chunk != NULL) {
156                 g_mem_chunk_destroy(h225ras_call_info_value_chunk);
157                 h225ras_call_info_value_chunk = NULL;
158         }
159
160         /* create new hash-tables and mem_chunks for RAS SRT */
161
162         for(i=0;i<7;i++) {
163                 ras_calls[i] = g_hash_table_new(h225ras_call_hash, h225ras_call_equal);
164         }
165
166         h225ras_call_info_key_chunk = g_mem_chunk_new("call_info_key_chunk",
167             sizeof(h225ras_call_info_key),
168             400 * sizeof(h225ras_call_info_key),
169             G_ALLOC_ONLY);
170         h225ras_call_info_value_chunk = g_mem_chunk_new("call_info_value_chunk",
171             sizeof(h225ras_call_t),
172             400 * sizeof(h225ras_call_t),
173             G_ALLOC_ONLY);
174 }