From Ishraq Ibne Ashraf: Tinkerforge protocol dissector
[metze/wireshark/wip.git] / epan / h225-persistentdata.c
1 /*
2  * h225-persistentdata.c
3  * Source for lists and hash tables used in wireshark's h225 dissector
4  * for calculation of delays in h225-calls
5  *
6  * Copyright 2003 Lars Roland
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27  */
28
29 #include "config.h"
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/emem.h>
34
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "h225-persistentdata.h"
39
40 /* Global Memory Chunks for lists and Global hash tables*/
41
42 static GHashTable *ras_calls[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
43
44 /*
45  * Functions needed for Ras-Hash-Table
46  */
47
48 /* compare 2 keys */
49 static gint h225ras_call_equal(gconstpointer k1, gconstpointer k2)
50 {
51         const h225ras_call_info_key* key1 = (const h225ras_call_info_key*) k1;
52         const h225ras_call_info_key* key2 = (const h225ras_call_info_key*) k2;
53
54         return (key1->reqSeqNum == key2->reqSeqNum &&
55             key1->conversation == key2->conversation);
56 }
57
58 /* calculate a hash key */
59 static guint h225ras_call_hash(gconstpointer k)
60 {
61         const h225ras_call_info_key* key = (const h225ras_call_info_key*) k;
62
63         return key->reqSeqNum + GPOINTER_TO_UINT(key->conversation);
64 }
65
66
67 h225ras_call_t * find_h225ras_call(h225ras_call_info_key *h225ras_call_key ,int category)
68 {
69         h225ras_call_t *h225ras_call = NULL;
70         h225ras_call = (h225ras_call_t *)g_hash_table_lookup(ras_calls[category], h225ras_call_key);
71
72         return h225ras_call;
73 }
74
75 h225ras_call_t * new_h225ras_call(h225ras_call_info_key *h225ras_call_key, packet_info *pinfo, e_guid_t *guid, int category)
76 {
77         h225ras_call_info_key *new_h225ras_call_key;
78         h225ras_call_t *h225ras_call = NULL;
79
80
81         /* Prepare the value data.
82            "req_num" and "rsp_num" are frame numbers;
83            frame numbers are 1-origin, so we use 0
84            to mean "we don't yet know in which frame
85            the reply for this call appears". */
86         new_h225ras_call_key = se_new(h225ras_call_info_key);
87         new_h225ras_call_key->reqSeqNum = h225ras_call_key->reqSeqNum;
88         new_h225ras_call_key->conversation = h225ras_call_key->conversation;
89         h225ras_call = se_new(h225ras_call_t);
90         h225ras_call->req_num = pinfo->fd->num;
91         h225ras_call->rsp_num = 0;
92         h225ras_call->requestSeqNum = h225ras_call_key->reqSeqNum;
93         h225ras_call->responded = FALSE;
94         h225ras_call->next_call = NULL;
95         h225ras_call->req_time=pinfo->fd->abs_ts;
96         h225ras_call->guid=*guid;
97         /* store it */
98         g_hash_table_insert(ras_calls[category], new_h225ras_call_key, h225ras_call);
99
100         return h225ras_call;
101 }
102
103 h225ras_call_t * append_h225ras_call(h225ras_call_t *prev_call, packet_info *pinfo, e_guid_t *guid, int category _U_)
104 {
105         h225ras_call_t *h225ras_call = NULL;
106
107         /* Prepare the value data.
108            "req_num" and "rsp_num" are frame numbers;
109            frame numbers are 1-origin, so we use 0
110            to mean "we don't yet know in which frame
111            the reply for this call appears". */
112         h225ras_call = se_new(h225ras_call_t);
113         h225ras_call->req_num = pinfo->fd->num;
114         h225ras_call->rsp_num = 0;
115         h225ras_call->requestSeqNum = prev_call->requestSeqNum;
116         h225ras_call->responded = FALSE;
117         h225ras_call->next_call = NULL;
118         h225ras_call->req_time=pinfo->fd->abs_ts;
119         h225ras_call->guid=*guid;
120
121         prev_call->next_call = h225ras_call;
122         return h225ras_call;
123 }
124
125
126 /* Init routine for hash tables and delay calculation
127    This routine will be called by Wireshark, before it
128    is (re-)dissecting a trace file from beginning.
129    We need to discard and init any state we've saved */
130
131 void
132 h225_init_routine(void)
133 {
134         int i;
135
136         /* free hash-tables for RAS SRT */
137         for(i=0;i<7;i++) {
138                 if (ras_calls[i] != NULL) {
139                         g_hash_table_destroy(ras_calls[i]);
140                         ras_calls[i] = NULL;
141                 }
142         }
143
144         /* create new hash-tables for RAS SRT */
145
146         for(i=0;i<7;i++) {
147                 ras_calls[i] = g_hash_table_new(h225ras_call_hash, h225ras_call_equal);
148         }
149
150 }