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