Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-scriptingservice.c
1 /* packet-scriptingservice.c
2  * Routines for the Scripting Service Protocol, a load distribution application
3  * of the rsplib RSerPool implementation
4  * http://tdrwww.iem.uni-due.de/dreibholz/rserpool/
5  *
6  * Copyright 2008-2010 by Thomas Dreibholz <dreibh [AT] iem.uni-due.de>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <epan/packet.h>
34
35
36 #define SSPROTOCOL_PAYLOAD_PROTOCOL_ID 0x29097604
37
38
39 /* Initialize the protocol and registered fields */
40 static int proto_ssprotocol     = -1;
41 static int hf_message_type      = -1;
42 static int hf_message_flags     = -1;
43 static int hf_message_length    = -1;
44 static int hf_message_status    = -1;
45 static int hf_message_data      = -1;
46 static int hf_message_reason    = -1;
47 static int hf_message_info      = -1;
48 static int hf_message_hash      = -1;
49 static int hf_environment_u_bit = -1;
50
51 /* Initialize the subtree pointers */
52 static gint ett_ssprotocol        = -1;
53 static gint ett_environment_flags = -1;
54
55 static guint
56 dissect_ssprotocol_message(tvbuff_t *, packet_info *, proto_tree *);
57
58
59 /* Dissectors for messages. This is specific to ScriptingServiceProtocol */
60 #define MESSAGE_TYPE_LENGTH          1
61 #define MESSAGE_FLAGS_LENGTH         1
62 #define MESSAGE_LENGTH_LENGTH        2
63 #define MESSAGE_STATUS_LENGTH        4
64 #define MESSAGE_NOTRDY_REASON_LENGTH 4
65 #define MESSAGE_ENVIRON_HASH_LENGTH  20
66
67 #define MESSAGE_TYPE_OFFSET          0
68 #define MESSAGE_FLAGS_OFFSET         (MESSAGE_TYPE_OFFSET   + MESSAGE_TYPE_LENGTH)
69 #define MESSAGE_LENGTH_OFFSET        (MESSAGE_FLAGS_OFFSET  + MESSAGE_FLAGS_LENGTH)
70 #define MESSAGE_STATUS_OFFSET        (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
71 #define MESSAGE_DATA_OFFSET          (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
72 #define MESSAGE_RDY_INFO_OFFSET      (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
73 #define MESSAGE_NOTRDY_REASON_OFFSET (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
74 #define MESSAGE_NOTRDY_INFO_OFFSET   (MESSAGE_NOTRDY_REASON_OFFSET + MESSAGE_NOTRDY_REASON_LENGTH)
75 #define MESSAGE_ENVIRON_HASH_OFFSET  (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
76
77
78 #define SS_NOTREADY_TYPE       0
79 #define SS_READY_TYPE          1
80 #define SS_UPLOAD_TYPE         2
81 #define SS_DOWNLOAD_TYPE       3
82 #define SS_KEEPALIVE_TYPE      4
83 #define SS_KEEPALIVE_ACK_TYPE  5
84 #define SS_STATUS_TYPE         6
85 #define SS_ENVIRONMENT_TYPE    7
86
87
88 static const value_string message_type_values[] = {
89   { SS_NOTREADY_TYPE,       "Not Ready" },
90   { SS_READY_TYPE,          "Ready" },
91   { SS_UPLOAD_TYPE,         "Upload" },
92   { SS_DOWNLOAD_TYPE,       "Download" },
93   { SS_KEEPALIVE_TYPE,      "Keep-Alive" },
94   { SS_KEEPALIVE_ACK_TYPE,  "Keep-Alive Ack" },
95   { SS_STATUS_TYPE,         "Status" },
96   { SS_ENVIRONMENT_TYPE,    "Environment" },
97   { 0, NULL }
98 };
99
100
101 static const value_string notrdy_reason_values[] = {
102   { 0x00000001, "Fully Loaded" },
103   { 0x00000002, "Out of Resources" },
104   { 0, NULL }
105 };
106
107
108 #define SSP_ENVIRONMENT_U_BIT 0x01
109 static const true_false_string environment_u_bit = {
110   "Upload needed",
111   "Upload not needed"
112 };
113
114
115 static guint
116 dissect_ssprotocol_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *ssprotocol_tree)
117 {
118   proto_item* flags_item;
119   proto_tree* flags_tree;
120   guint8      type;
121   guint16     data_length;
122   guint16     info_length;
123   guint       total_length;
124
125   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
126   if (pinfo && (check_col(pinfo->cinfo, COL_INFO))) {
127     col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(type, message_type_values, "Unknown SSP type: %u"));
128   }
129   proto_tree_add_item(ssprotocol_tree, hf_message_type,   message_tvb, MESSAGE_TYPE_OFFSET,   MESSAGE_TYPE_LENGTH,   FALSE);
130   flags_item = proto_tree_add_item(ssprotocol_tree, hf_message_flags,  message_tvb, MESSAGE_FLAGS_OFFSET,  MESSAGE_FLAGS_LENGTH,  FALSE);
131   proto_tree_add_item(ssprotocol_tree, hf_message_length, message_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH, FALSE);
132   total_length = MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH;
133   switch (type) {
134     case SS_KEEPALIVE_ACK_TYPE:
135     case SS_STATUS_TYPE:
136       info_length = tvb_get_ntohs(message_tvb, MESSAGE_LENGTH_OFFSET) - MESSAGE_STATUS_OFFSET;
137       if (info_length == MESSAGE_STATUS_LENGTH) {
138         proto_tree_add_item(ssprotocol_tree, hf_message_status, message_tvb, MESSAGE_STATUS_OFFSET, MESSAGE_STATUS_LENGTH, FALSE);
139         total_length += MESSAGE_STATUS_LENGTH;
140       }
141       break;
142     case SS_UPLOAD_TYPE:
143     case SS_DOWNLOAD_TYPE:
144       data_length = tvb_get_ntohs(message_tvb, MESSAGE_LENGTH_OFFSET) - MESSAGE_DATA_OFFSET;
145       if (data_length > 0) {
146         proto_tree_add_item(ssprotocol_tree, hf_message_data, message_tvb, MESSAGE_DATA_OFFSET, data_length, FALSE);
147         total_length += data_length;
148       }
149       break;
150     case SS_READY_TYPE:
151       info_length = tvb_get_ntohs(message_tvb, MESSAGE_LENGTH_OFFSET) - MESSAGE_RDY_INFO_OFFSET;
152       if (info_length > 0) {
153         proto_tree_add_item(ssprotocol_tree, hf_message_info, message_tvb, MESSAGE_RDY_INFO_OFFSET, info_length, FALSE);
154         total_length += info_length;
155       }
156       break;
157     case SS_NOTREADY_TYPE:
158       info_length = tvb_get_ntohs(message_tvb, MESSAGE_LENGTH_OFFSET) - MESSAGE_NOTRDY_INFO_OFFSET;
159       if (info_length > 0) {
160         proto_tree_add_item(ssprotocol_tree, hf_message_reason, message_tvb, MESSAGE_NOTRDY_REASON_OFFSET, MESSAGE_NOTRDY_REASON_LENGTH, FALSE);
161         proto_tree_add_item(ssprotocol_tree, hf_message_info,   message_tvb, MESSAGE_NOTRDY_INFO_OFFSET, info_length, FALSE);
162         total_length += info_length;
163       }
164       break;
165     case SS_ENVIRONMENT_TYPE:
166         flags_tree = proto_item_add_subtree(flags_item, ett_environment_flags);
167         proto_tree_add_item(flags_tree, hf_environment_u_bit, message_tvb, MESSAGE_FLAGS_OFFSET, MESSAGE_FLAGS_LENGTH, ENC_BIG_ENDIAN);
168         proto_tree_add_item(ssprotocol_tree, hf_message_hash, message_tvb, MESSAGE_ENVIRON_HASH_OFFSET, MESSAGE_ENVIRON_HASH_LENGTH, FALSE);
169       break;
170     default:
171       break;
172   }
173
174   return total_length;
175 }
176
177
178 static int
179 dissect_ssprotocol(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
180 {
181   proto_item *ssprotocol_item;
182   proto_tree *ssprotocol_tree;
183
184   /* pinfo is NULL only if dissect_ssprotocol_message is called from dissect_error cause */
185   if (pinfo)
186     col_set_str(pinfo->cinfo, COL_PROTOCOL, "SSP");
187
188   /* In the interest of speed, if "tree" is NULL, don't do any work not
189      necessary to generate protocol tree items. */
190   if (tree) {
191     /* create the ssprotocol protocol tree */
192     ssprotocol_item = proto_tree_add_item(tree, proto_ssprotocol, message_tvb, 0, -1, FALSE);
193     ssprotocol_tree = proto_item_add_subtree(ssprotocol_item, ett_ssprotocol);
194   } else {
195     ssprotocol_tree = NULL;
196   };
197   /* dissect the message */
198   return dissect_ssprotocol_message(message_tvb, pinfo, ssprotocol_tree);
199 }
200
201
202 /* Register the protocol */
203 void
204 proto_register_ssprotocol(void)
205 {
206
207   /* Setup list of header fields */
208   static hf_register_info hf[] = {
209     { &hf_message_type,      { "Type",   "ssprotocol.message_type",   FT_UINT8,  BASE_DEC,  VALS(message_type_values),  0x0, NULL, HFILL } },
210     { &hf_message_flags,     { "Flags",  "ssprotocol.message_flags",  FT_UINT8,  BASE_DEC,  NULL,                       0x0, NULL, HFILL } },
211     { &hf_message_length,    { "Length", "ssprotocol.message_length", FT_UINT16, BASE_DEC,  NULL,                       0x0, NULL, HFILL } },
212     { &hf_message_status,    { "Status", "ssprotocol.message_status", FT_UINT32, BASE_DEC,  NULL,                       0x0, NULL, HFILL } },
213     { &hf_message_reason,    { "Reason", "ssprotocol.message_reason", FT_UINT32, BASE_DEC,  VALS(notrdy_reason_values), 0x0, NULL, HFILL } },
214     { &hf_message_info,      { "Info",   "ssprotocol.message_info",   FT_STRING, BASE_NONE, NULL,                       0x0, NULL, HFILL } },
215     { &hf_message_data,      { "Data",   "ssprotocol.message_data",   FT_BYTES,  BASE_NONE, NULL,                       0x0, NULL, HFILL } },
216     { &hf_message_hash,      { "Hash",   "ssprotocol.message_hash",   FT_BYTES,  BASE_NONE, NULL,                       0x0, NULL, HFILL } },
217     { &hf_environment_u_bit, { "U-Bit",  "ssprotocol.environment_u_bit", FT_BOOLEAN, 8,TFS(&environment_u_bit), SSP_ENVIRONMENT_U_BIT, NULL, HFILL } }
218   };
219
220   /* Setup protocol subtree array */
221   static gint *ett[] = {
222     &ett_ssprotocol,
223     &ett_environment_flags
224   };
225
226   /* Register the protocol name and description */
227   proto_ssprotocol = proto_register_protocol("Scripting Service Protocol", "SSP", "ssp");
228
229   /* Required function calls to register the header fields and subtrees used */
230   proto_register_field_array(proto_ssprotocol, hf, array_length(hf));
231   proto_register_subtree_array(ett, array_length(ett));
232 }
233
234 void
235 proto_reg_handoff_ssprotocol(void)
236 {
237   dissector_handle_t ssprotocol_handle;
238
239   ssprotocol_handle = new_create_dissector_handle(dissect_ssprotocol, proto_ssprotocol);
240   dissector_add("sctp.ppi", SSPROTOCOL_PAYLOAD_PROTOCOL_ID, ssprotocol_handle);
241 }