3967940d20d65a7f8eee56c1fdd3294546b48867
[obnox/wireshark/wip.git] / packet-klm.c
1 /* packet-klm.c    2001 Ronnie Sahlberg <rsahlber@bigpond.net.au>
2  * Routines for klm dissection
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@zing.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31
32
33 #include "packet-rpc.h"
34 #include "packet-nfs.h"
35 #include "packet-klm.h"
36
37 static int proto_klm = -1;
38 static int hf_klm_exclusive = -1;
39 static int hf_klm_lock = -1;
40 static int hf_klm_servername = -1;
41 static int hf_klm_pid = -1;
42 static int hf_klm_offset = -1;
43 static int hf_klm_len = -1;
44 static int hf_klm_stats = -1;
45 static int hf_klm_holder = -1;
46 static int hf_klm_block = -1;
47
48 static gint ett_klm = -1;
49 static gint ett_klm_lock = -1;
50 static gint ett_klm_holder = -1;
51
52 const value_string names_klm_stats[] =
53 {
54 #define KLM_GRANTED             0
55                 {       KLM_GRANTED,    "KLM_GRANTED"   },
56 #define KLM_DENIED              1
57                 {       KLM_DENIED,     "KLM_DENIED"    },
58 #define KLM_DENIED_NOLOCKS      2
59                 {       KLM_DENIED_NOLOCKS,     "KLM_DENIED_NOLOCKS"    },
60 #define KLM_WORKING             3
61                 {       KLM_WORKING,    "KLM_WORKING"   },
62 };
63
64 static int
65 dissect_holder(tvbuff_t *tvb, packet_info *pinfo, 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, tvb_length_remaining(tvb, offset), FALSE);
72
73         lock_tree = proto_item_add_subtree(lock_item, ett_klm_holder);
74
75         offset = dissect_rpc_bool_tvb( tvb, pinfo, lock_tree, 
76                         hf_klm_exclusive, offset);
77
78         offset = dissect_rpc_uint32_tvb(tvb, pinfo, lock_tree, 
79                         hf_klm_pid, offset);
80
81         offset = dissect_rpc_uint32_tvb(tvb, pinfo, lock_tree, 
82                         hf_klm_offset, offset);
83
84         offset = dissect_rpc_uint32_tvb(tvb, pinfo, 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, tvb_length_remaining(tvb, offset), FALSE);
98
99         lock_tree = proto_item_add_subtree(lock_item, ett_klm_lock);
100
101         offset = dissect_rpc_string_tvb(tvb, pinfo, lock_tree,
102                         hf_klm_servername, offset, NULL);
103
104         offset = dissect_nfs_fh3(tvb, offset, pinfo, lock_tree,"fh");
105
106         offset = dissect_rpc_uint32_tvb(tvb, pinfo, lock_tree, 
107                         hf_klm_pid, offset);
108
109         offset = dissect_rpc_uint32_tvb(tvb, pinfo, lock_tree, 
110                         hf_klm_offset, offset);
111
112         offset = dissect_rpc_uint32_tvb(tvb, pinfo, 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, proto_tree *tree)
128 {
129
130         offset = dissect_rpc_uint32_tvb(tvb, pinfo, 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( tvb, pinfo, tree, 
140                         hf_klm_block, offset);
141
142         offset = dissect_rpc_bool_tvb( tvb, pinfo, 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, proto_tree *tree)
152 {
153         gint32  stats;
154
155         stats = tvb_get_ntohl(tvb, offset);
156
157         offset = dissect_rpc_uint32_tvb(tvb, pinfo, tree, 
158                         hf_klm_stats, offset);
159
160         if (stats == KLM_DENIED) {
161                 offset = dissect_holder(tvb, pinfo, 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( tvb, pinfo, 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
193 void
194 proto_register_klm(void)
195 {
196         static struct true_false_string tfs_exclusive = { "Exclusive", "Not exclusive" };
197         static struct true_false_string tfs_block = { "Block", "Do not block" };
198
199         static hf_register_info hf[] = {
200                 { &hf_klm_exclusive, {
201                         "exclusive", "klm.exclusive", FT_BOOLEAN, BASE_NONE,
202                         &tfs_exclusive, 0, "Exclusive lock" }},
203
204                 { &hf_klm_lock, {
205                         "lock", "klm.lock", FT_NONE, BASE_NONE,
206                         NULL, 0, "KLM lock structure" }},
207
208                 { &hf_klm_servername, {
209                         "server name", "klm.servername", FT_STRING, BASE_NONE,
210                         NULL, 0, "Server name" }},
211
212                 { &hf_klm_pid, {
213                         "pid", "klm.pid", FT_UINT32, BASE_DEC,
214                         NULL, 0, "ProcessID" }},
215
216                 { &hf_klm_offset, {
217                         "offset", "klm.offset", FT_UINT32, BASE_DEC,
218                         NULL, 0, "File offset" }},
219
220                 { &hf_klm_len, {
221                         "length", "klm.len", FT_UINT32, BASE_DEC,
222                         NULL, 0, "Length of lock region" }},
223
224                 { &hf_klm_stats, {
225                         "stats", "klm.stats", FT_UINT32, BASE_DEC,
226                         VALS(names_klm_stats), 0, "stats" }},
227
228                 { &hf_klm_holder, {
229                         "holder", "klm.holder", FT_NONE, BASE_NONE,
230                         NULL, 0, "KLM lock holder" }},
231
232                 { &hf_klm_block, {
233                         "block", "klm.block", FT_BOOLEAN, BASE_NONE,
234                         &tfs_block, 0, "Block" }},
235
236         };
237
238         static gint *ett[] = {
239                 &ett_klm,
240                 &ett_klm_lock,
241                 &ett_klm_holder,
242         };
243
244         proto_klm = proto_register_protocol("Kernel Lock Manager",
245             "KLM", "klm");
246         proto_register_field_array(proto_klm, hf, array_length(hf));
247         proto_register_subtree_array(ett, array_length(ett));
248 }
249
250 void
251 proto_reg_handoff_klm(void)
252 {
253         /* Register the protocol as RPC */
254         rpc_init_prog(proto_klm, KLM_PROGRAM, ett_klm);
255         /* Register the procedure tables */
256         rpc_init_proc_table(KLM_PROGRAM, 1, klm1_proc);
257 }
258