38804ecf6a5384b7b27edebbbdbb5e22f0e8e23b
[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 #include <epan/emem.h>
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "h225-persistentdata.h"
42
43 /* Global Memory Chunks for lists and Global hash tables*/
44
45 static GHashTable *ras_calls[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
46
47 /*
48  * Functions needed for Ras-Hash-Table
49  */
50
51 /* compare 2 keys */
52 static gint h225ras_call_equal(gconstpointer k1, gconstpointer k2)
53 {
54         const h225ras_call_info_key* key1 = (const h225ras_call_info_key*) k1;
55         const h225ras_call_info_key* key2 = (const h225ras_call_info_key*) k2;
56
57         return (key1->reqSeqNum == key2->reqSeqNum &&
58             key1->conversation == key2->conversation);
59 }
60
61 /* calculate a hash key */
62 static guint h225ras_call_hash(gconstpointer k)
63 {
64         const h225ras_call_info_key* key = (const h225ras_call_info_key*) k;
65
66         return key->reqSeqNum + GPOINTER_TO_UINT(key->conversation);
67 }
68
69
70 h225ras_call_t * find_h225ras_call(h225ras_call_info_key *h225ras_call_key ,int category)
71 {
72         h225ras_call_t *h225ras_call = NULL;
73         h225ras_call = (h225ras_call_t *)g_hash_table_lookup(ras_calls[category], h225ras_call_key);
74
75         return h225ras_call;
76 }
77
78 h225ras_call_t * new_h225ras_call(h225ras_call_info_key *h225ras_call_key, packet_info *pinfo, guint8 *guid, int category)
79 {
80         h225ras_call_info_key *new_h225ras_call_key;
81         h225ras_call_t *h225ras_call = NULL;
82
83
84         /* Prepare the value data.
85            "req_num" and "rsp_num" are frame numbers;
86            frame numbers are 1-origin, so we use 0
87            to mean "we don't yet know in which frame
88            the reply for this call appears". */
89         new_h225ras_call_key = se_alloc(sizeof(h225ras_call_info_key));
90         new_h225ras_call_key->reqSeqNum = h225ras_call_key->reqSeqNum;
91         new_h225ras_call_key->conversation = h225ras_call_key->conversation;
92         h225ras_call = se_alloc(sizeof(h225ras_call_t));
93         h225ras_call->req_num = pinfo->fd->num;
94         h225ras_call->rsp_num = 0;
95         h225ras_call->requestSeqNum = h225ras_call_key->reqSeqNum;
96         h225ras_call->responded = FALSE;
97         h225ras_call->next_call = NULL;
98         h225ras_call->req_time.secs=pinfo->fd->abs_secs;
99         h225ras_call->req_time.nsecs=pinfo->fd->abs_usecs*1000;
100         memcpy(h225ras_call->guid, guid,16);
101         /* store it */
102         g_hash_table_insert(ras_calls[category], new_h225ras_call_key, h225ras_call);
103
104         return h225ras_call;
105 }
106
107 h225ras_call_t * append_h225ras_call(h225ras_call_t *prev_call, packet_info *pinfo, guint8 *guid, int category _U_)
108 {
109         h225ras_call_t *h225ras_call = NULL;
110
111         /* Prepare the value data.
112            "req_num" and "rsp_num" are frame numbers;
113            frame numbers are 1-origin, so we use 0
114            to mean "we don't yet know in which frame
115            the reply for this call appears". */
116         h225ras_call = se_alloc(sizeof(h225ras_call_t));
117         h225ras_call->req_num = pinfo->fd->num;
118         h225ras_call->rsp_num = 0;
119         h225ras_call->requestSeqNum = prev_call->requestSeqNum;
120         h225ras_call->responded = FALSE;
121         h225ras_call->next_call = NULL;
122         h225ras_call->req_time.secs=pinfo->fd->abs_secs;
123         h225ras_call->req_time.nsecs=pinfo->fd->abs_usecs*1000;
124         memcpy(h225ras_call->guid, guid,16);
125
126         prev_call->next_call = h225ras_call;
127         return h225ras_call;
128 }
129
130
131 /* Init routine for hash tables and delay calculation
132    This routine will be called by Ethereal, before it
133    is (re-)dissecting a trace file from beginning.
134    We need to discard and init any state we've saved */
135
136 void
137 h225_init_routine(void)
138 {
139         int i;
140
141         /* free hash-tables for RAS SRT */
142         for(i=0;i<7;i++) {
143                 if (ras_calls[i] != NULL) {
144                         g_hash_table_destroy(ras_calls[i]);
145                         ras_calls[i] = NULL;
146                 }
147         }
148
149         /* create new hash-tables for RAS SRT */
150
151         for(i=0;i<7;i++) {
152                 ras_calls[i] = g_hash_table_new(h225ras_call_hash, h225ras_call_equal);
153         }
154
155 }