From Hannes Gredler: make the IS-IS dissector more verbose in the INFO
[obnox/wireshark/wip.git] / packet-klm.c
1 /* packet-klm.c    2001 Ronnie Sahlberg <See AUTHORS for email>
2  * Routines for klm dissection
3  *
4  * $Id: packet-klm.c,v 1.10 2002/08/28 21:00:19 jmayer Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29
30
31 #include "packet-rpc.h"
32 #include "packet-nfs.h"
33 #include "packet-klm.h"
34
35 static int proto_klm = -1;
36 static int hf_klm_exclusive = -1;
37 static int hf_klm_lock = -1;
38 static int hf_klm_servername = -1;
39 static int hf_klm_pid = -1;
40 static int hf_klm_offset = -1;
41 static int hf_klm_len = -1;
42 static int hf_klm_stats = -1;
43 static int hf_klm_holder = -1;
44 static int hf_klm_block = -1;
45
46 static gint ett_klm = -1;
47 static gint ett_klm_lock = -1;
48 static gint ett_klm_holder = -1;
49
50 static const value_string names_klm_stats[] =
51 {
52 #define KLM_GRANTED             0
53                 {       KLM_GRANTED,    "KLM_GRANTED"   },
54 #define KLM_DENIED              1
55                 {       KLM_DENIED,     "KLM_DENIED"    },
56 #define KLM_DENIED_NOLOCKS      2
57                 {       KLM_DENIED_NOLOCKS,     "KLM_DENIED_NOLOCKS"    },
58 #define KLM_WORKING             3
59                 {       KLM_WORKING,    "KLM_WORKING"   },
60                 {       0,              NULL }
61 };
62
63 static int
64 dissect_holder(tvbuff_t *tvb, proto_tree *tree, int offset)
65 {
66         proto_item* lock_item = NULL;
67         proto_tree* lock_tree = NULL;
68
69         lock_item = proto_tree_add_item(tree, hf_klm_holder, tvb,
70                         offset, -1, FALSE);
71
72         lock_tree = proto_item_add_subtree(lock_item, ett_klm_holder);
73
74         offset = dissect_rpc_bool( tvb, lock_tree,
75                         hf_klm_exclusive, offset);
76
77         offset = dissect_rpc_uint32(tvb, lock_tree,
78                         hf_klm_pid, offset);
79
80         offset = dissect_rpc_uint32(tvb, lock_tree,
81                         hf_klm_offset, offset);
82
83         offset = dissect_rpc_uint32(tvb, lock_tree,
84                         hf_klm_len, offset);
85
86         return offset;
87 }
88
89 static int
90 dissect_lock(tvbuff_t *tvb, packet_info* pinfo, proto_tree *tree, int offset)
91 {
92         proto_item* lock_item = NULL;
93         proto_tree* lock_tree = NULL;
94
95         lock_item = proto_tree_add_item(tree, hf_klm_lock, tvb,
96                         offset, -1, FALSE);
97
98         lock_tree = proto_item_add_subtree(lock_item, ett_klm_lock);
99
100         offset = dissect_rpc_string(tvb, lock_tree,
101                         hf_klm_servername, offset, NULL);
102
103         offset = dissect_nfs_fh3(tvb, offset, pinfo, lock_tree,"fh");
104
105         offset = dissect_rpc_uint32(tvb, lock_tree,
106                         hf_klm_pid, offset);
107
108         offset = dissect_rpc_uint32(tvb, lock_tree,
109                         hf_klm_offset, offset);
110
111         offset = dissect_rpc_uint32(tvb, lock_tree,
112                         hf_klm_len, offset);
113
114         return offset;
115 }
116
117 static int
118 dissect_klm_unlock_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
119 {
120         offset = dissect_lock(tvb, pinfo, tree, offset);
121
122         return offset;
123 }
124
125 static int
126 dissect_klm_stat_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
127 {
128
129         offset = dissect_rpc_uint32(tvb, tree,
130                         hf_klm_stats, offset);
131
132         return offset;
133 }
134
135 static int
136 dissect_klm_lock_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
137 {
138         offset = dissect_rpc_bool( tvb, tree,
139                         hf_klm_block, offset);
140
141         offset = dissect_rpc_bool( tvb, tree,
142                         hf_klm_exclusive, offset);
143
144         offset = dissect_lock(tvb, pinfo, tree, offset);
145
146         return offset;
147 }
148
149 static int
150 dissect_klm_test_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
151 {
152         gint32  stats;
153
154         stats = tvb_get_ntohl(tvb, offset);
155
156         offset = dissect_rpc_uint32(tvb, tree,
157                         hf_klm_stats, offset);
158
159         if (stats == KLM_DENIED) {
160                 offset = dissect_holder(tvb, tree, offset);
161         }
162
163         return offset;
164 }
165
166 static int
167 dissect_klm_test_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
168 {
169         offset = dissect_rpc_bool( tvb, tree,
170                         hf_klm_exclusive, offset);
171
172         offset = dissect_lock(tvb, pinfo, tree, offset);
173
174         return offset;
175 }
176
177
178 /* proc number, "proc name", dissect_request, dissect_reply */
179 /* NULL as function pointer means: type of arguments is "void". */
180 static const vsff klm1_proc[] = {
181         { KLMPROC_TEST, "TEST",
182                 dissect_klm_test_call,  dissect_klm_test_reply },
183         { KLMPROC_LOCK, "LOCK",
184                 dissect_klm_lock_call,  dissect_klm_stat_reply },
185         { KLMPROC_CANCEL,       "CANCEL",
186                 dissect_klm_lock_call,  dissect_klm_stat_reply },
187         { KLMPROC_UNLOCK,       "UNLOCK",
188                 dissect_klm_unlock_call,        dissect_klm_stat_reply },
189         { 0,    NULL,           NULL,                           NULL }
190 };
191
192 void
193 proto_register_klm(void)
194 {
195         static struct true_false_string tfs_exclusive = { "Exclusive", "Not exclusive" };
196         static struct true_false_string tfs_block = { "Block", "Do not block" };
197
198         static hf_register_info hf[] = {
199                 { &hf_klm_exclusive, {
200                         "exclusive", "klm.exclusive", FT_BOOLEAN, BASE_NONE,
201                         &tfs_exclusive, 0, "Exclusive lock", HFILL }},
202
203                 { &hf_klm_lock, {
204                         "lock", "klm.lock", FT_NONE, BASE_NONE,
205                         NULL, 0, "KLM lock structure", HFILL }},
206
207                 { &hf_klm_servername, {
208                         "server name", "klm.servername", FT_STRING, BASE_NONE,
209                         NULL, 0, "Server name", HFILL }},
210
211                 { &hf_klm_pid, {
212                         "pid", "klm.pid", FT_UINT32, BASE_DEC,
213                         NULL, 0, "ProcessID", HFILL }},
214
215                 { &hf_klm_offset, {
216                         "offset", "klm.offset", FT_UINT32, BASE_DEC,
217                         NULL, 0, "File offset", HFILL }},
218
219                 { &hf_klm_len, {
220                         "length", "klm.len", FT_UINT32, BASE_DEC,
221                         NULL, 0, "Length of lock region", HFILL }},
222
223                 { &hf_klm_stats, {
224                         "stats", "klm.stats", FT_UINT32, BASE_DEC,
225                         VALS(names_klm_stats), 0, "stats", HFILL }},
226
227                 { &hf_klm_holder, {
228                         "holder", "klm.holder", FT_NONE, BASE_NONE,
229                         NULL, 0, "KLM lock holder", HFILL }},
230
231                 { &hf_klm_block, {
232                         "block", "klm.block", FT_BOOLEAN, BASE_NONE,
233                         &tfs_block, 0, "Block", HFILL }},
234
235         };
236
237         static gint *ett[] = {
238                 &ett_klm,
239                 &ett_klm_lock,
240                 &ett_klm_holder,
241         };
242
243         proto_klm = proto_register_protocol("Kernel Lock Manager",
244             "KLM", "klm");
245         proto_register_field_array(proto_klm, hf, array_length(hf));
246         proto_register_subtree_array(ett, array_length(ett));
247 }
248
249 void
250 proto_reg_handoff_klm(void)
251 {
252         /* Register the protocol as RPC */
253         rpc_init_prog(proto_klm, KLM_PROGRAM, ett_klm);
254         /* Register the procedure tables */
255         rpc_init_proc_table(KLM_PROGRAM, 1, klm1_proc);
256 }