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