0fa20d4f1f6839200bfa2b99cce9e86093046e22
[obnox/wireshark/wip.git] / epan / dissectors / packet-componentstatus.c
1 /* packet-componentstatus.c
2  * Routines for the Component Status Protocol of the rsplib RSerPool implementation
3  * http://tdrwww.exp-math.uni-essen.de/dreibholz/rserpool/
4  *
5  * Copyright 2006 by Thomas Dreibholz <dreibh [AT] exp-math.uni-essen.de>
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from README.developer
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <epan/packet.h>
35
36
37 #define CSP_VERSION 0x0200
38
39 #define CID_GROUP(id)  (((uint64_t) id >> 56) & (0xffffULL)
40 #define CID_OBJECT(id) (((uint64_t) id & 0xffffffffffffffULL)
41
42 #define CID_GROUP_REGISTRAR 0x0001
43 #define CID_GROUP_POOLELEMENT 0x0002
44 #define CID_GROUP_POOLUSER 0x0003
45
46 #define CID_COMPOUND(group, object) (((uint64_t) (group & 0xffff) << 56) | CID_OBJECT((uint64_t)object))
47
48 #define CSPT_REPORT
49
50
51 /* Initialize the protocol and registered fields */
52 static int proto_componentstatusprotocol             = -1;
53 static int hf_message_type                           = -1;
54 static int hf_message_flags                          = -1;
55 static int hf_message_length                         = -1;
56 static int hf_message_version                        = -1;
57 static int hf_message_senderid                       = -1;
58 static int hf_message_sendertimestamp                = -1;
59 static int hf_componentstatusreport_reportinterval   = -1;
60 static int hf_componentstatusreport_location         = -1;
61 static int hf_componentstatusreport_status           = -1;
62 static int hf_componentstatusreport_workload         = -1;
63 static int hf_componentstatusreport_associations     = -1;
64 static int hf_componentstatusreport_associationarray = -1;
65 static int hf_componentassociation_receiverid        = -1;
66 static int hf_componentassociation_duration          = -1;
67 static int hf_componentassociation_flags             = -1;
68 static int hf_componentassociation_protocolid        = -1;
69 static int hf_componentassociation_ppid              = -1;
70
71
72 /* Initialize the subtree pointers */
73 static gint ett_componentstatusprotocol = -1;
74 static gint ett_association             = -1;
75
76
77 #define COMPONENTSTATUSPROTOCOL_PORT    2960
78 #define COMPONENTSTATUSPROTOCOL_VERSION 0x0200
79
80
81 /* Dissectors for messages. This is specific to ComponentStatusProtocol */
82 #define MESSAGE_TYPE_LENGTH             1
83 #define MESSAGE_FLAGS_LENGTH            1
84 #define MESSAGE_LENGTH_LENGTH           2
85 #define MESSAGE_VERSION_LENGTH          4
86 #define MESSAGE_SENDERID_LENGTH         8
87 #define MESSAGE_SENDERTIMESTAMP_LENGTH  8
88
89
90 #define MESSAGE_TYPE_OFFSET             0
91 #define MESSAGE_FLAGS_OFFSET            (MESSAGE_TYPE_OFFSET            + MESSAGE_TYPE_LENGTH)
92 #define MESSAGE_LENGTH_OFFSET           (MESSAGE_FLAGS_OFFSET           + MESSAGE_FLAGS_LENGTH)
93 #define MESSAGE_VERSION_OFFSET          (MESSAGE_LENGTH_OFFSET          + MESSAGE_LENGTH_LENGTH)
94 #define MESSAGE_SENDERID_OFFSET         (MESSAGE_VERSION_OFFSET         + MESSAGE_VERSION_LENGTH)
95 #define MESSAGE_SENDERTIMESTAMP_OFFSET  (MESSAGE_SENDERID_OFFSET        + MESSAGE_SENDERID_LENGTH)
96 #define MESSAGE_VALUE_OFFSET            (MESSAGE_SENDERTIMESTAMP_OFFSET + MESSAGE_SENDERTIMESTAMP_LENGTH)
97
98
99 #define COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH   4
100 #define COMPONENTSTATUSREPORT_WORKLOAD_LENGTH         2
101 #define COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH     2
102 #define COMPONENTSTATUSREPORT_LOCATION_LENGTH       128
103 #define COMPONENTSTATUSREPORT_STATUS_LENGTH         128
104
105
106 #define COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET   MESSAGE_VALUE_OFFSET
107 #define COMPONENTSTATUSREPORT_LOCATION_OFFSET         (COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET + COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH)
108 #define COMPONENTSTATUSREPORT_STATUS_OFFSET           (COMPONENTSTATUSREPORT_LOCATION_OFFSET       + COMPONENTSTATUSREPORT_LOCATION_LENGTH)
109 #define COMPONENTSTATUSREPORT_WORKLOAD_OFFSET         (COMPONENTSTATUSREPORT_STATUS_OFFSET         + COMPONENTSTATUSREPORT_STATUS_LENGTH)
110 #define COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET     (COMPONENTSTATUSREPORT_WORKLOAD_OFFSET       + COMPONENTSTATUSREPORT_WORKLOAD_LENGTH)
111 #define COMPONENTSTATUSREPORT_ASSOCIATIONARRAY_OFFSET (COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET   + COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH)
112
113
114 #define COMPONENTASSOCIATION_RECEIVERID_LENGTH  8
115 #define COMPONENTASSOCIATION_DURATION_LENGTH    8
116 #define COMPONENTASSOCIATION_FLAGS_LENGTH       2
117 #define COMPONENTASSOCIATION_PROTOCOLID_LENGTH  2
118 #define COMPONENTASSOCIATION_PPID_LENGTH        4
119
120 #define COMPONENTASSOCIATION_RECEIVERID_OFFSET 0
121 #define COMPONENTASSOCIATION_DURATION_OFFSET   (COMPONENTASSOCIATION_RECEIVERID_OFFSET + COMPONENTASSOCIATION_RECEIVERID_LENGTH)
122 #define COMPONENTASSOCIATION_FLAGS_OFFSET      (COMPONENTASSOCIATION_DURATION_OFFSET   + COMPONENTASSOCIATION_DURATION_LENGTH)
123 #define COMPONENTASSOCIATION_PROTOCOLID_OFFSET (COMPONENTASSOCIATION_FLAGS_OFFSET      + COMPONENTASSOCIATION_FLAGS_LENGTH)
124 #define COMPONENTASSOCIATION_PPID_OFFSET       (COMPONENTASSOCIATION_PROTOCOLID_OFFSET + COMPONENTASSOCIATION_PROTOCOLID_LENGTH)
125 #define COMPONENTASSOCIATION_LENGTH            (COMPONENTASSOCIATION_PPID_OFFSET       + COMPONENTASSOCIATION_PPID_LENGTH)
126
127
128 #define COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE       0x01
129
130
131
132
133 static const value_string message_type_values[] = {
134   { COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE,             "ComponentStatus Report" },
135   { 0, NULL }
136 };
137
138
139 static void
140 dissect_componentstatusprotocol_componentassociation_message(tvbuff_t *message_tvb, proto_tree *message_tree)
141 {
142   proto_tree_add_item(message_tree, hf_componentassociation_receiverid, message_tvb, COMPONENTASSOCIATION_RECEIVERID_OFFSET, COMPONENTASSOCIATION_RECEIVERID_LENGTH, ENC_BIG_ENDIAN);
143   proto_tree_add_item(message_tree, hf_componentassociation_duration,   message_tvb, COMPONENTASSOCIATION_DURATION_OFFSET,   COMPONENTASSOCIATION_DURATION_LENGTH,   ENC_BIG_ENDIAN);
144   proto_tree_add_item(message_tree, hf_componentassociation_flags,      message_tvb, COMPONENTASSOCIATION_FLAGS_OFFSET,      COMPONENTASSOCIATION_FLAGS_LENGTH,      ENC_BIG_ENDIAN);
145   proto_tree_add_item(message_tree, hf_componentassociation_protocolid, message_tvb, COMPONENTASSOCIATION_PROTOCOLID_OFFSET, COMPONENTASSOCIATION_PROTOCOLID_LENGTH, ENC_BIG_ENDIAN);
146   proto_tree_add_item(message_tree, hf_componentassociation_ppid,       message_tvb, COMPONENTASSOCIATION_PPID_OFFSET,       COMPONENTASSOCIATION_PPID_LENGTH,       ENC_BIG_ENDIAN);
147 }
148
149
150 static void
151 dissect_componentstatusprotocol_componentstatusreport_message(tvbuff_t *message_tvb, proto_tree *message_tree)
152 {
153   tvbuff_t   *association_tvb;
154   proto_item *association_item;
155   proto_tree *association_tree;
156   /* gint        associations; - variable set but not used, so commented out */
157   int         i;
158   gint        offset;
159
160   proto_tree_add_item(message_tree, hf_componentstatusreport_reportinterval, message_tvb, COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET, COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH, ENC_BIG_ENDIAN);
161   proto_tree_add_item(message_tree, hf_componentstatusreport_location,       message_tvb, COMPONENTSTATUSREPORT_LOCATION_OFFSET,       COMPONENTSTATUSREPORT_LOCATION_LENGTH,       ENC_ASCII|ENC_NA);
162   proto_tree_add_item(message_tree, hf_componentstatusreport_status,         message_tvb, COMPONENTSTATUSREPORT_STATUS_OFFSET,         COMPONENTSTATUSREPORT_STATUS_LENGTH,         ENC_ASCII|ENC_NA);
163   proto_tree_add_item(message_tree, hf_componentstatusreport_workload,       message_tvb, COMPONENTSTATUSREPORT_WORKLOAD_OFFSET,       COMPONENTSTATUSREPORT_WORKLOAD_LENGTH,       ENC_BIG_ENDIAN);
164   proto_tree_add_item(message_tree, hf_componentstatusreport_associations,   message_tvb, COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET,   COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH,   ENC_BIG_ENDIAN);
165
166   /* associations = tvb_get_ntohs(message_tvb, COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET); */
167   offset = COMPONENTSTATUSREPORT_ASSOCIATIONARRAY_OFFSET;
168   i = 1;
169   while(tvb_reported_length_remaining(message_tvb, offset) >= COMPONENTASSOCIATION_LENGTH) {
170      association_item = proto_tree_add_text(message_tree, message_tvb, offset, COMPONENTASSOCIATION_LENGTH,
171          "Association #%d", i++);
172      association_tree = proto_item_add_subtree(association_item, ett_association);
173      association_tvb  = tvb_new_subset(message_tvb, offset, 
174                                        MIN(COMPONENTASSOCIATION_LENGTH, tvb_length_remaining(message_tvb, offset)),
175                                        COMPONENTASSOCIATION_LENGTH);
176
177      dissect_componentstatusprotocol_componentassociation_message(association_tvb, association_tree);
178      offset += COMPONENTASSOCIATION_LENGTH;
179   }
180 }
181
182
183 static void
184 dissect_componentstatusprotocol_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *componentstatusprotocol_tree)
185 {
186   guint8 type;
187
188   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
189   col_add_str(pinfo->cinfo, COL_INFO, val_to_str(type, message_type_values, "Unknown ComponentStatusProtocol type"));
190   proto_tree_add_item(componentstatusprotocol_tree, hf_message_type,            message_tvb, MESSAGE_TYPE_OFFSET,     MESSAGE_TYPE_LENGTH,     ENC_BIG_ENDIAN);
191   proto_tree_add_item(componentstatusprotocol_tree, hf_message_flags,           message_tvb, MESSAGE_FLAGS_OFFSET,    MESSAGE_FLAGS_LENGTH,    ENC_BIG_ENDIAN);
192   proto_tree_add_item(componentstatusprotocol_tree, hf_message_length,          message_tvb, MESSAGE_LENGTH_OFFSET,   MESSAGE_LENGTH_LENGTH,   ENC_BIG_ENDIAN);
193   proto_tree_add_item(componentstatusprotocol_tree, hf_message_version,         message_tvb, MESSAGE_VERSION_OFFSET,  MESSAGE_VERSION_LENGTH,  ENC_BIG_ENDIAN);
194   proto_tree_add_item(componentstatusprotocol_tree, hf_message_senderid,        message_tvb, MESSAGE_SENDERID_OFFSET, MESSAGE_SENDERID_LENGTH, ENC_BIG_ENDIAN);
195   proto_tree_add_item(componentstatusprotocol_tree, hf_message_sendertimestamp, message_tvb, MESSAGE_SENDERTIMESTAMP_OFFSET, MESSAGE_SENDERTIMESTAMP_LENGTH, ENC_BIG_ENDIAN);
196   switch (type) {
197     case COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE:
198       dissect_componentstatusprotocol_componentstatusreport_message(message_tvb, componentstatusprotocol_tree);
199      break;
200   }
201 }
202
203
204 static int
205 dissect_componentstatusprotocol(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
206 {
207   proto_item *componentstatusprotocol_item;
208   proto_tree *componentstatusprotocol_tree;
209   gint8 type;
210   gint32 version;
211
212   if (tvb_length(message_tvb) < (MESSAGE_VERSION_OFFSET + MESSAGE_VERSION_LENGTH))
213     return(0);
214
215   /* Check, if this packet really contains a ComponentStatusProtocol message */
216   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
217   if (type != COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE) {
218     return(0);
219   }
220   version = tvb_get_ntohl(message_tvb, MESSAGE_VERSION_OFFSET);
221   if (version != COMPONENTSTATUSPROTOCOL_VERSION) {
222     return(0);
223   }
224
225   /* pinfo is NULL only if dissect_componentstatusprotocol_message is called from dissect_error cause */
226   if (pinfo) {
227     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ComponentStatusProtocol");
228   }
229   /* In the interest of speed, if "tree" is NULL, don't do any work not
230      necessary to generate protocol tree items. */
231   if (tree) {
232     /* create the componentstatusprotocol protocol tree */
233     componentstatusprotocol_item = proto_tree_add_item(tree, proto_componentstatusprotocol, message_tvb, 0, -1, ENC_BIG_ENDIAN);
234     componentstatusprotocol_tree = proto_item_add_subtree(componentstatusprotocol_item, ett_componentstatusprotocol);
235   } else {
236     componentstatusprotocol_tree = NULL;
237   };
238   /* dissect the message */
239   dissect_componentstatusprotocol_message(message_tvb, pinfo, componentstatusprotocol_tree);
240   return(tvb_length(message_tvb));
241 }
242
243
244 /* Register the protocol with Wireshark */
245 void
246 proto_register_componentstatusprotocol(void)
247 {
248
249   /* Setup list of header fields */
250   static hf_register_info hf[] = {
251     { &hf_message_type,                           { "Type",             "componentstatusprotocol.message_type",                           FT_UINT8,  BASE_DEC, VALS(message_type_values), 0x0, NULL, HFILL } },
252     { &hf_message_flags,                          { "Flags",            "componentstatusprotocol.message_flags",                          FT_UINT8,  BASE_DEC, NULL,                      0x0, NULL, HFILL } },
253     { &hf_message_length,                         { "Length",           "componentstatusprotocol.message_length",                         FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
254     { &hf_message_version,                        { "Version",          "componentstatusprotocol.message_version",                        FT_UINT32, BASE_HEX, NULL,                      0x0, NULL, HFILL } },
255     { &hf_message_senderid,                       { "SenderID",         "componentstatusprotocol.message_senderid",                       FT_UINT64, BASE_HEX, NULL,                      0x0, NULL, HFILL } },
256     { &hf_message_sendertimestamp,                { "SenderTimeStamp",  "componentstatusprotocol.message_sendertimestamp",                FT_UINT64, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
257     { &hf_componentstatusreport_reportinterval,   { "ReportInterval",   "componentstatusprotocol.componentstatusreport_reportinterval",   FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
258     { &hf_componentstatusreport_location,         { "Location",         "componentstatusprotocol.componentstatusreport_location",         FT_STRING, BASE_NONE, NULL,                     0x0, NULL, HFILL } },
259     { &hf_componentstatusreport_status,           { "Status",           "componentstatusprotocol.componentstatusreport_status",           FT_STRING, BASE_NONE, NULL,                     0x0, NULL, HFILL } },
260     { &hf_componentstatusreport_workload,         { "Workload",         "componentstatusprotocol.componentstatusreport_workload",         FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
261     { &hf_componentstatusreport_associations,     { "Associations",     "componentstatusprotocol.componentstatusreport_associations",     FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
262     { &hf_componentstatusreport_associationarray, { "AssociationArray", "componentstatusprotocol.componentstatusreport_AssociationArray", FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
263     { &hf_componentassociation_receiverid,        { "ReceiverID",       "componentstatusprotocol.componentassociation_receiverid",        FT_UINT64, BASE_HEX, NULL,                      0x0, NULL, HFILL } },
264     { &hf_componentassociation_duration,          { "Duration",         "componentstatusprotocol.componentassociation_duration",          FT_UINT64, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
265     { &hf_componentassociation_flags,             { "Flags",            "componentstatusprotocol.componentassociation_flags",             FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
266     { &hf_componentassociation_protocolid,        { "ProtocolID",       "componentstatusprotocol.componentassociation_protocolid",        FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
267     { &hf_componentassociation_ppid,              { "PPID",             "componentstatusprotocol.componentassociation_ppid",              FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
268   };
269
270   /* Setup protocol subtree array */
271   static gint *ett[] = {
272     &ett_componentstatusprotocol,
273     &ett_association
274   };
275
276   /* Register the protocol name and description */
277   proto_componentstatusprotocol = proto_register_protocol("Component Status Protocol", "ComponentStatusProtocol", "componentstatusprotocol");
278
279   /* Required function calls to register the header fields and subtrees used */
280   proto_register_field_array(proto_componentstatusprotocol, hf, array_length(hf));
281   proto_register_subtree_array(ett, array_length(ett));
282 }
283
284 void
285 proto_reg_handoff_componentstatusprotocol(void)
286 {
287   dissector_handle_t componentstatusprotocol_handle;
288
289   componentstatusprotocol_handle = new_create_dissector_handle(dissect_componentstatusprotocol, proto_componentstatusprotocol);
290   dissector_add_uint("udp.port", COMPONENTSTATUSPROTOCOL_PORT, componentstatusprotocol_handle);
291 }