There's no need to keep a "FILE *" for the file being printed to in a
[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.13 2003/08/17 21:34:22 sahlberg 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_procedure_v1 = -1;
37 static int hf_klm_exclusive = -1;
38 static int hf_klm_lock = -1;
39 static int hf_klm_servername = -1;
40 static int hf_klm_pid = -1;
41 static int hf_klm_offset = -1;
42 static int hf_klm_len = -1;
43 static int hf_klm_stats = -1;
44 static int hf_klm_holder = -1;
45 static int hf_klm_block = -1;
46
47 static gint ett_klm = -1;
48 static gint ett_klm_lock = -1;
49 static gint ett_klm_holder = -1;
50
51 static const value_string names_klm_stats[] =
52 {
53 #define KLM_GRANTED             0
54                 {       KLM_GRANTED,    "KLM_GRANTED"   },
55 #define KLM_DENIED              1
56                 {       KLM_DENIED,     "KLM_DENIED"    },
57 #define KLM_DENIED_NOLOCKS      2
58                 {       KLM_DENIED_NOLOCKS,     "KLM_DENIED_NOLOCKS"    },
59 #define KLM_WORKING             3
60                 {       KLM_WORKING,    "KLM_WORKING"   },
61                 {       0,              NULL }
62 };
63
64 static int
65 dissect_holder(tvbuff_t *tvb, proto_tree *tree, int offset)
66 {
67         proto_item* lock_item = NULL;
68         proto_tree* lock_tree = NULL;
69
70         lock_item = proto_tree_add_item(tree, hf_klm_holder, tvb,
71                         offset, -1, FALSE);
72
73         lock_tree = proto_item_add_subtree(lock_item, ett_klm_holder);
74
75         offset = dissect_rpc_bool( tvb, lock_tree,
76                         hf_klm_exclusive, offset);
77
78         offset = dissect_rpc_uint32(tvb, lock_tree,
79                         hf_klm_pid, offset);
80
81         offset = dissect_rpc_uint32(tvb, lock_tree,
82                         hf_klm_offset, offset);
83
84         offset = dissect_rpc_uint32(tvb, lock_tree,
85                         hf_klm_len, offset);
86
87         return offset;
88 }
89
90 static int
91 dissect_lock(tvbuff_t *tvb, packet_info* pinfo, proto_tree *tree, int offset)
92 {
93         proto_item* lock_item = NULL;
94         proto_tree* lock_tree = NULL;
95
96         lock_item = proto_tree_add_item(tree, hf_klm_lock, tvb,
97                         offset, -1, FALSE);
98
99         lock_tree = proto_item_add_subtree(lock_item, ett_klm_lock);
100
101         offset = dissect_rpc_string(tvb, lock_tree,
102                         hf_klm_servername, offset, NULL);
103
104         offset = dissect_nfs_fh3(tvb, offset, pinfo, lock_tree,"fh", NULL);
105
106         offset = dissect_rpc_uint32(tvb, lock_tree,
107                         hf_klm_pid, offset);
108
109         offset = dissect_rpc_uint32(tvb, lock_tree,
110                         hf_klm_offset, offset);
111
112         offset = dissect_rpc_uint32(tvb, lock_tree,
113                         hf_klm_len, offset);
114
115         return offset;
116 }
117
118 static int
119 dissect_klm_unlock_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
120 {
121         offset = dissect_lock(tvb, pinfo, tree, offset);
122
123         return offset;
124 }
125
126 static int
127 dissect_klm_stat_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
128 {
129
130         offset = dissect_rpc_uint32(tvb, tree,
131                         hf_klm_stats, offset);
132
133         return offset;
134 }
135
136 static int
137 dissect_klm_lock_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
138 {
139         offset = dissect_rpc_bool( tvb, tree,
140                         hf_klm_block, offset);
141
142         offset = dissect_rpc_bool( tvb, tree,
143                         hf_klm_exclusive, offset);
144
145         offset = dissect_lock(tvb, pinfo, tree, offset);
146
147         return offset;
148 }
149
150 static int
151 dissect_klm_test_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
152 {
153         gint32  stats;
154
155         stats = tvb_get_ntohl(tvb, offset);
156
157         offset = dissect_rpc_uint32(tvb, tree,
158                         hf_klm_stats, offset);
159
160         if (stats == KLM_DENIED) {
161                 offset = dissect_holder(tvb, tree, offset);
162         }
163
164         return offset;
165 }
166
167 static int
168 dissect_klm_test_call(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
169 {
170         offset = dissect_rpc_bool( tvb, tree,
171                         hf_klm_exclusive, offset);
172
173         offset = dissect_lock(tvb, pinfo, tree, offset);
174
175         return offset;
176 }
177
178
179 /* proc number, "proc name", dissect_request, dissect_reply */
180 /* NULL as function pointer means: type of arguments is "void". */
181 static const vsff klm1_proc[] = {
182         { KLMPROC_TEST, "TEST",
183                 dissect_klm_test_call,  dissect_klm_test_reply },
184         { KLMPROC_LOCK, "LOCK",
185                 dissect_klm_lock_call,  dissect_klm_stat_reply },
186         { KLMPROC_CANCEL,       "CANCEL",
187                 dissect_klm_lock_call,  dissect_klm_stat_reply },
188         { KLMPROC_UNLOCK,       "UNLOCK",
189                 dissect_klm_unlock_call,        dissect_klm_stat_reply },
190         { 0,    NULL,           NULL,                           NULL }
191 };
192 static const value_string klm1_proc_vals[] = {
193         { KLMPROC_TEST, "TEST" },
194         { KLMPROC_LOCK, "LOCK" },
195         { KLMPROC_CANCEL,       "CANCEL" },
196         { KLMPROC_UNLOCK,       "UNLOCK" },
197         { 0,    NULL}
198 };
199
200 void
201 proto_register_klm(void)
202 {
203         static struct true_false_string tfs_exclusive = { "Exclusive", "Not exclusive" };
204         static struct true_false_string tfs_block = { "Block", "Do not block" };
205
206         static hf_register_info hf[] = {
207                 { &hf_klm_procedure_v1, {
208                         "V1 Procedure", "klm.procedure_v1", FT_UINT32, BASE_DEC,
209                         VALS(klm1_proc_vals), 0, "V1 Procedure", HFILL }},
210                 { &hf_klm_exclusive, {
211                         "exclusive", "klm.exclusive", FT_BOOLEAN, BASE_NONE,
212                         &tfs_exclusive, 0, "Exclusive lock", HFILL }},
213
214                 { &hf_klm_lock, {
215                         "lock", "klm.lock", FT_NONE, BASE_NONE,
216                         NULL, 0, "KLM lock structure", HFILL }},
217
218                 { &hf_klm_servername, {
219                         "server name", "klm.servername", FT_STRING, BASE_NONE,
220                         NULL, 0, "Server name", HFILL }},
221
222                 { &hf_klm_pid, {
223                         "pid", "klm.pid", FT_UINT32, BASE_DEC,
224                         NULL, 0, "ProcessID", HFILL }},
225
226                 { &hf_klm_offset, {
227                         "offset", "klm.offset", FT_UINT32, BASE_DEC,
228                         NULL, 0, "File offset", HFILL }},
229
230                 { &hf_klm_len, {
231                         "length", "klm.len", FT_UINT32, BASE_DEC,
232                         NULL, 0, "Length of lock region", HFILL }},
233
234                 { &hf_klm_stats, {
235                         "stats", "klm.stats", FT_UINT32, BASE_DEC,
236                         VALS(names_klm_stats), 0, "stats", HFILL }},
237
238                 { &hf_klm_holder, {
239                         "holder", "klm.holder", FT_NONE, BASE_NONE,
240                         NULL, 0, "KLM lock holder", HFILL }},
241
242                 { &hf_klm_block, {
243                         "block", "klm.block", FT_BOOLEAN, BASE_NONE,
244                         &tfs_block, 0, "Block", HFILL }},
245
246         };
247
248         static gint *ett[] = {
249                 &ett_klm,
250                 &ett_klm_lock,
251                 &ett_klm_holder,
252         };
253
254         proto_klm = proto_register_protocol("Kernel Lock Manager",
255             "KLM", "klm");
256         proto_register_field_array(proto_klm, hf, array_length(hf));
257         proto_register_subtree_array(ett, array_length(ett));
258 }
259
260 void
261 proto_reg_handoff_klm(void)
262 {
263         /* Register the protocol as RPC */
264         rpc_init_prog(proto_klm, KLM_PROGRAM, ett_klm);
265         /* Register the procedure tables */
266         rpc_init_proc_table(KLM_PROGRAM, 1, klm1_proc, hf_klm_procedure_v1);
267 }