Get rid of get_ber_last_reated_item() and fix dissection of wIN-TriggerList.
[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 static void
78 dissect_componentstatusprotocol_message(tvbuff_t *, packet_info *, proto_tree *);
79
80
81 #define COMPONENTSTATUSPROTOCOL_PORT    2960
82 #define COMPONENTSTATUSPROTOCOL_VERSION 0x0200
83
84
85 /* Dissectors for messages. This is specific to ComponentStatusProtocol */
86 #define MESSAGE_TYPE_LENGTH             1
87 #define MESSAGE_FLAGS_LENGTH            1
88 #define MESSAGE_LENGTH_LENGTH           2
89 #define MESSAGE_VERSION_LENGTH          4
90 #define MESSAGE_SENDERID_LENGTH         8
91 #define MESSAGE_SENDERTIMESTAMP_LENGTH  8
92
93
94 #define MESSAGE_TYPE_OFFSET             0
95 #define MESSAGE_FLAGS_OFFSET            (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)
96 #define MESSAGE_LENGTH_OFFSET           (MESSAGE_FLAGS_OFFSET + MESSAGE_FLAGS_OFFSET)
97 #define MESSAGE_VERSION_OFFSET          (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_OFFSET)
98 #define MESSAGE_SENDERID_OFFSET         (MESSAGE_VERSION_OFFSET + MESSAGE_VERSION_OFFSET)
99 #define MESSAGE_SENDERTIMESTAMP_OFFSET  (MESSAGE_SENDERID_OFFSET + MESSAGE_SENDERID_OFFSET)
100 #define MESSAGE_VALUE_OFFSET            (MESSAGE_SENDERTIMESTAMP_OFFSET + MESSAGE_SENDERTIMESTAMP_LENGTH)
101
102
103 #define COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH 4
104 #define COMPONENTSTATUSREPORT_WORKLOAD_LENGTH  2
105 #define COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH 2
106 #define COMPONENTSTATUSREPORT_LOCATION_LENGTH 128
107 #define COMPONENTSTATUSREPORT_STATUS_LENGTH 128
108
109
110 #define COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET MESSAGE_VALUE_OFFSET
111 #define COMPONENTSTATUSREPORT_LOCATION_OFFSET (COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET + COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH)
112 #define COMPONENTSTATUSREPORT_STATUS_OFFSET (COMPONENTSTATUSREPORT_LOCATION_OFFSET + COMPONENTSTATUSREPORT_LOCATION_LENGTH)
113 #define COMPONENTSTATUSREPORT_WORKLOAD_OFFSET (COMPONENTSTATUSREPORT_STATUS_OFFSET + COMPONENTSTATUSREPORT_STATUS_LENGTH)
114 #define COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET (COMPONENTSTATUSREPORT_WORKLOAD_OFFSET + COMPONENTSTATUSREPORT_WORKLOAD_LENGTH)
115 #define COMPONENTSTATUSREPORT_ASSOCIATIONARRAY_OFFSET (COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET + COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH)
116
117
118 #define COMPONENTASSOCIATION_RECEIVERID_LENGTH  8
119 #define COMPONENTASSOCIATION_DURATION_LENGTH    8
120 #define COMPONENTASSOCIATION_FLAGS_LENGTH       2
121 #define COMPONENTASSOCIATION_PROTOCOLID_LENGTH  2
122 #define COMPONENTASSOCIATION_PPID_LENGTH        4
123
124 #define COMPONENTASSOCIATION_RECEIVERID_OFFSET  0
125 #define COMPONENTASSOCIATION_DURATION_OFFSET (COMPONENTASSOCIATION_RECEIVERID_OFFSET + COMPONENTASSOCIATION_RECEIVERID_LENGTH)
126 #define COMPONENTASSOCIATION_FLAGS_OFFSET (COMPONENTASSOCIATION_DURATION_OFFSET + COMPONENTASSOCIATION_DURATION_LENGTH)
127 #define COMPONENTASSOCIATION_PROTOCOLID_OFFSET (COMPONENTASSOCIATION_FLAGS_OFFSET + COMPONENTASSOCIATION_FLAGS_LENGTH)
128 #define COMPONENTASSOCIATION_PPID_OFFSET (COMPONENTASSOCIATION_PROTOCOLID_OFFSET + COMPONENTASSOCIATION_PROTOCOLID_LENGTH)
129 #define COMPONENTASSOCIATION_LENGTH (COMPONENTASSOCIATION_PPID_OFFSET + COMPONENTASSOCIATION_PPID_LENGTH)
130
131
132 #define COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE       0x01
133
134
135
136
137 static const value_string message_type_values[] = {
138   { COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE,             "ComponentStatus Report" },
139   { 0, NULL }
140 };
141
142
143 static void
144 dissect_componentstatusprotocol_componentassociation_message(tvbuff_t *message_tvb, proto_tree *message_tree)
145 {
146   proto_tree_add_item(message_tree, hf_componentassociation_receiverid,  message_tvb, COMPONENTASSOCIATION_RECEIVERID_OFFSET,  COMPONENTASSOCIATION_RECEIVERID_LENGTH,  FALSE);
147   proto_tree_add_item(message_tree, hf_componentassociation_duration, message_tvb, COMPONENTASSOCIATION_DURATION_OFFSET, COMPONENTASSOCIATION_DURATION_LENGTH, FALSE);
148   proto_tree_add_item(message_tree, hf_componentassociation_flags, message_tvb, COMPONENTASSOCIATION_FLAGS_OFFSET, COMPONENTASSOCIATION_FLAGS_LENGTH, FALSE);
149   proto_tree_add_item(message_tree, hf_componentassociation_protocolid, message_tvb, COMPONENTASSOCIATION_PROTOCOLID_OFFSET, COMPONENTASSOCIATION_PROTOCOLID_LENGTH, FALSE);
150   proto_tree_add_item(message_tree, hf_componentassociation_ppid, message_tvb, COMPONENTASSOCIATION_PPID_OFFSET, COMPONENTASSOCIATION_PPID_LENGTH, FALSE);
151 }
152
153
154 static void
155 dissect_componentstatusprotocol_componentstatusreport_message(tvbuff_t *message_tvb, proto_tree *message_tree)
156 {
157   tvbuff_t   *association_tvb;
158   proto_item *association_item;
159   proto_tree *association_tree;
160   gint        associations;
161   int         i;
162   gint        offset;
163   gint        remaining_length;
164
165   proto_tree_add_item(message_tree, hf_componentstatusreport_reportinterval, message_tvb, COMPONENTSTATUSREPORT_REPORTINTERVAL_OFFSET, COMPONENTSTATUSREPORT_REPORTINTERVAL_LENGTH, FALSE);
166   proto_tree_add_item(message_tree, hf_componentstatusreport_location, message_tvb,  COMPONENTSTATUSREPORT_LOCATION_OFFSET, COMPONENTSTATUSREPORT_LOCATION_LENGTH, FALSE);
167   proto_tree_add_item(message_tree, hf_componentstatusreport_status, message_tvb,  COMPONENTSTATUSREPORT_STATUS_OFFSET, COMPONENTSTATUSREPORT_STATUS_LENGTH, FALSE);
168   proto_tree_add_item(message_tree, hf_componentstatusreport_workload, message_tvb,  COMPONENTSTATUSREPORT_WORKLOAD_OFFSET, COMPONENTSTATUSREPORT_WORKLOAD_LENGTH, FALSE);
169   proto_tree_add_item(message_tree, hf_componentstatusreport_associations,  message_tvb, COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET, COMPONENTSTATUSREPORT_ASSOCIATIONS_LENGTH, FALSE);
170
171   associations = tvb_get_ntohs(message_tvb, COMPONENTSTATUSREPORT_ASSOCIATIONS_OFFSET);
172   offset = COMPONENTSTATUSREPORT_ASSOCIATIONARRAY_OFFSET;
173   i = 1;
174   while((remaining_length = tvb_length_remaining(message_tvb, offset)) >= COMPONENTASSOCIATION_LENGTH) {
175      association_item = proto_tree_add_text(message_tree, message_tvb, offset, COMPONENTASSOCIATION_LENGTH,
176          "Association #%d", i++);
177      association_tree = proto_item_add_subtree(association_item, ett_association);
178      association_tvb  = tvb_new_subset(message_tvb, offset, COMPONENTASSOCIATION_LENGTH, COMPONENTASSOCIATION_LENGTH);
179
180      dissect_componentstatusprotocol_componentassociation_message(association_tvb, association_tree);
181      offset += COMPONENTASSOCIATION_LENGTH;
182   }
183 }
184
185
186 static void
187 dissect_componentstatusprotocol_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *componentstatusprotocol_tree)
188 {
189   guint8 type;
190
191   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
192   if (pinfo && (check_col(pinfo->cinfo, COL_INFO))) {
193     col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(type, message_type_values, "Unknown ComponentStatusProtocol type"));
194   }
195   proto_tree_add_item(componentstatusprotocol_tree, hf_message_type,            message_tvb, MESSAGE_TYPE_OFFSET,     MESSAGE_TYPE_LENGTH,     FALSE);
196   proto_tree_add_item(componentstatusprotocol_tree, hf_message_flags,           message_tvb, MESSAGE_FLAGS_OFFSET,    MESSAGE_FLAGS_LENGTH,    FALSE);
197   proto_tree_add_item(componentstatusprotocol_tree, hf_message_length,          message_tvb, MESSAGE_LENGTH_OFFSET,   MESSAGE_LENGTH_LENGTH,   FALSE);
198   proto_tree_add_item(componentstatusprotocol_tree, hf_message_version,         message_tvb, MESSAGE_VERSION_OFFSET,  MESSAGE_VERSION_LENGTH,  FALSE);
199   proto_tree_add_item(componentstatusprotocol_tree, hf_message_senderid,        message_tvb, MESSAGE_SENDERID_OFFSET, MESSAGE_SENDERID_LENGTH, FALSE);
200   proto_tree_add_item(componentstatusprotocol_tree, hf_message_sendertimestamp, message_tvb, MESSAGE_SENDERTIMESTAMP_OFFSET, MESSAGE_SENDERTIMESTAMP_LENGTH, FALSE);
201   switch (type) {
202     case COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE:
203       dissect_componentstatusprotocol_componentstatusreport_message(message_tvb, componentstatusprotocol_tree);
204      break;
205   }
206 }
207
208
209 static int
210 dissect_componentstatusprotocol(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
211 {
212   proto_item *componentstatusprotocol_item;
213   proto_tree *componentstatusprotocol_tree;
214   gint8 type;
215   gint32 version;
216
217   /* Check, if this packet really contains a ComponentStatusProtocol message */
218   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
219   if (type != COMPONENTSTATUS_COMPONENTSTATUSREPORT_MESSAGE_TYPE) {
220     return(FALSE);
221   }
222   version = tvb_get_ntohl(message_tvb, MESSAGE_VERSION_OFFSET);
223   if (version != COMPONENTSTATUSPROTOCOL_VERSION) {
224     return(FALSE);
225   }
226
227   /* pinfo is NULL only if dissect_componentstatusprotocol_message is called from dissect_error cause */
228   if (pinfo && (check_col(pinfo->cinfo, COL_PROTOCOL)))
229     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ComponentStatusProtocol");
230
231   /* In the interest of speed, if "tree" is NULL, don't do any work not
232      necessary to generate protocol tree items. */
233   if (tree) {
234     /* create the componentstatusprotocol protocol tree */
235     componentstatusprotocol_item = proto_tree_add_item(tree, proto_componentstatusprotocol, message_tvb, 0, -1, FALSE);
236     componentstatusprotocol_tree = proto_item_add_subtree(componentstatusprotocol_item, ett_componentstatusprotocol);
237   } else {
238     componentstatusprotocol_tree = NULL;
239   };
240   /* dissect the message */
241   dissect_componentstatusprotocol_message(message_tvb, pinfo, componentstatusprotocol_tree);
242   return(TRUE);
243 }
244
245
246 /* Register the protocol with Wireshark */
247 void
248 proto_register_componentstatusprotocol(void)
249 {
250
251   /* Setup list of header fields */
252   static hf_register_info hf[] = {
253     { &hf_message_type,                           { "Type",             "componentstatusprotocol.message_type",                           FT_UINT8,  BASE_DEC, VALS(message_type_values), 0x0, "", HFILL } },
254     { &hf_message_flags,                          { "Flags",            "componentstatusprotocol.message_flags",                          FT_UINT8,  BASE_DEC, NULL,                      0x0, "", HFILL } },
255     { &hf_message_length,                         { "Length",           "componentstatusprotocol.message_length",                         FT_UINT16, BASE_DEC, NULL,                      0x0, "", HFILL } },
256     { &hf_message_version,                        { "Version",          "componentstatusprotocol.message_version",                        FT_UINT32, BASE_HEX, NULL,                      0x0, "", HFILL } },
257     { &hf_message_senderid,                       { "SenderID",         "componentstatusprotocol.message_senderid",                       FT_UINT64, BASE_HEX, NULL,                      0x0, "", HFILL } },
258     { &hf_message_sendertimestamp,                { "SenderTimeStamp",  "componentstatusprotocol.message_sendertimestamp",                FT_UINT64, BASE_DEC, NULL,                      0x0, "", HFILL } },
259     { &hf_componentstatusreport_reportinterval,   { "ReportInterval",   "componentstatusprotocol.componentstatusreport_reportinterval",   FT_UINT32, BASE_DEC, NULL,                      0x0, "", HFILL } },
260     { &hf_componentstatusreport_location,         { "Location",         "componentstatusprotocol.componentstatusreport_location",         FT_STRING, 0, NULL,                             0x0, "", HFILL } },
261     { &hf_componentstatusreport_status,           { "Status",           "componentstatusprotocol.componentstatusreport_status",           FT_STRING, 0, NULL,                             0x0, "", HFILL } },
262     { &hf_componentstatusreport_workload,         { "Workload",         "componentstatusprotocol.componentstatusreport_workload",         FT_UINT16, BASE_DEC, NULL,                      0x0, "", HFILL } },
263     { &hf_componentstatusreport_associations,     { "Associations",     "componentstatusprotocol.componentstatusreport_associations",     FT_UINT16, BASE_DEC, NULL,                      0x0, "", HFILL } },
264     { &hf_componentstatusreport_associationarray, { "AssociationArray", "componentstatusprotocol.componentstatusreport_AssociationArray", FT_UINT32, BASE_DEC, NULL,                      0x0, "", HFILL } },
265     { &hf_componentassociation_receiverid,        { "ReceiverID",       "componentstatusprotocol.componentassociation_receiverid",        FT_UINT64, BASE_HEX, NULL,                      0x0, "", HFILL } },
266     { &hf_componentassociation_duration,          { "Duration",         "componentstatusprotocol.componentassociation_duration",          FT_UINT64, BASE_DEC, NULL,                      0x0, "", HFILL } },
267     { &hf_componentassociation_flags,             { "Flags",            "componentstatusprotocol.componentassociation_flags",             FT_UINT16, BASE_DEC, NULL,                      0x0, "", HFILL } },
268     { &hf_componentassociation_protocolid,        { "ProtocolID",       "componentstatusprotocol.componentassociation_protocolid",        FT_UINT16, BASE_DEC, NULL,                      0x0, "", HFILL } },
269     { &hf_componentassociation_ppid,              { "PPID",             "componentstatusprotocol.componentassociation_ppid",              FT_UINT32, BASE_DEC, NULL,                      0x0, "", HFILL } },
270   };
271
272   /* Setup protocol subtree array */
273   static gint *ett[] = {
274     &ett_componentstatusprotocol,
275     &ett_association
276   };
277
278   /* Register the protocol name and description */
279   proto_componentstatusprotocol = proto_register_protocol("Component Status Protocol", "ComponentStatusProtocol", "componentstatusprotocol");
280
281   /* Required function calls to register the header fields and subtrees used */
282   proto_register_field_array(proto_componentstatusprotocol, hf, array_length(hf));
283   proto_register_subtree_array(ett, array_length(ett));
284 }
285
286 void
287 proto_reg_handoff_componentstatusprotocol(void)
288 {
289   dissector_handle_t componentstatusprotocol_handle;
290
291   componentstatusprotocol_handle = new_create_dissector_handle(dissect_componentstatusprotocol, proto_componentstatusprotocol);
292   dissector_add("udp.port", COMPONENTSTATUSPROTOCOL_PORT, componentstatusprotocol_handle);
293 }