ad4ab682257ecff8e2a9ade12745ddb11f16a597
[obnox/wireshark/wip.git] / epan / dissectors / packet-calcappprotocol.c
1 /* packet-calcappprotocol.c
2  * Routines for the Calculation Application Protocol, a test application of the
3  * rsplib RSerPool implementation
4  * http://tdrwww.exp-math.uni-essen.de/dreibholz/rserpool/
5  *
6  * Copyright 2006 by Thomas Dreibholz <dreibh [AT] exp-math.uni-essen.de>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied from README.developer
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <epan/packet.h>
36 #include <epan/sctpppids.h>
37
38
39 #define CALCAPPPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY 0x29097603
40
41
42 /* Initialize the protocol and registered fields */
43 static int proto_calcappprotocol = -1;
44 static int hf_message_type       = -1;
45 static int hf_message_flags      = -1;
46 static int hf_message_length     = -1;
47 static int hf_message_jobid      = -1;
48 static int hf_message_jobsize    = -1;
49 static int hf_message_completed  = -1;
50
51 /* Initialize the subtree pointers */
52 static gint ett_calcappprotocol = -1;
53
54 static void
55 dissect_calcappprotocol_message(tvbuff_t *, packet_info *, proto_tree *);
56
57
58 /* Dissectors for messages. This is specific to CalcAppProtocol */
59 #define MESSAGE_TYPE_LENGTH      1
60 #define MESSAGE_FLAGS_LENGTH     1
61 #define MESSAGE_LENGTH_LENGTH    2
62 #define MESSAGE_JOBID_LENGTH     4
63 #define MESSAGE_JOBSIZE_LENGTH   8
64 #define MESSAGE_COMPLETED_LENGTH 8
65
66 #define MESSAGE_TYPE_OFFSET      0
67 #define MESSAGE_FLAGS_OFFSET     (MESSAGE_TYPE_OFFSET    + MESSAGE_TYPE_LENGTH)
68 #define MESSAGE_LENGTH_OFFSET    (MESSAGE_FLAGS_OFFSET   + MESSAGE_FLAGS_LENGTH)
69 #define MESSAGE_JOBID_OFFSET     (MESSAGE_LENGTH_OFFSET  + MESSAGE_LENGTH_LENGTH)
70 #define MESSAGE_JOBSIZE_OFFSET   (MESSAGE_JOBID_OFFSET   + MESSAGE_JOBID_OFFSET)
71 #define MESSAGE_COMPLETED_OFFSET (MESSAGE_JOBSIZE_OFFSET + MESSAGE_JOBSIZE_OFFSET)
72
73
74 #define CALCAPP_REQUEST_MESSAGE_TYPE       1
75 #define CALCAPP_ACCEPT_MESSAGE_TYPE        2
76 #define CALCAPP_REJECT_MESSAGE_TYPE        3
77 #define CALCAPP_ABORT_MESSAGE_TYPE         4
78 #define CALCAPP_COMPLETE_MESSAGE_TYPE      5
79 #define CALCAPP_KEEPALIVE_MESSAGE_TYPE     6
80 #define CALCAPP_KEEPALIVE_ACK_MESSAGE_TYPE 7
81
82
83 static const value_string message_type_values[] = {
84   { CALCAPP_REQUEST_MESSAGE_TYPE,        "CalcApp Request" },
85   { CALCAPP_ACCEPT_MESSAGE_TYPE,         "CalcApp Accept" },
86   { CALCAPP_REJECT_MESSAGE_TYPE,         "CalcApp Reject" },
87   { CALCAPP_ABORT_MESSAGE_TYPE,          "CalcApp Abort" },
88   { CALCAPP_COMPLETE_MESSAGE_TYPE,       "CalcApp Complete" },
89   { CALCAPP_KEEPALIVE_MESSAGE_TYPE,      "CalcApp Keep-Alive" },
90   { CALCAPP_KEEPALIVE_ACK_MESSAGE_TYPE,  "CalcApp Keep-Alive Ack" },
91   { 0, NULL }
92 };
93
94
95 static void
96 dissect_calcappprotocol_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *calcappprotocol_tree)
97 {
98   guint8 type;
99
100   type = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
101   if (pinfo) {
102     col_add_fstr(pinfo->cinfo, COL_INFO, "%s ", val_to_str(type, message_type_values, "Unknown CalcAppProtocol type"));
103   }
104   proto_tree_add_item(calcappprotocol_tree, hf_message_type,      message_tvb, MESSAGE_TYPE_OFFSET,      MESSAGE_TYPE_LENGTH,      ENC_BIG_ENDIAN);
105   proto_tree_add_item(calcappprotocol_tree, hf_message_flags,     message_tvb, MESSAGE_FLAGS_OFFSET,     MESSAGE_FLAGS_LENGTH,     ENC_BIG_ENDIAN);
106   proto_tree_add_item(calcappprotocol_tree, hf_message_length,    message_tvb, MESSAGE_LENGTH_OFFSET,    MESSAGE_LENGTH_LENGTH,    ENC_BIG_ENDIAN);
107   proto_tree_add_item(calcappprotocol_tree, hf_message_jobid,     message_tvb, MESSAGE_JOBID_OFFSET,     MESSAGE_JOBID_LENGTH,     ENC_BIG_ENDIAN);
108   proto_tree_add_item(calcappprotocol_tree, hf_message_jobsize,   message_tvb, MESSAGE_JOBSIZE_OFFSET,   MESSAGE_JOBSIZE_LENGTH,   ENC_BIG_ENDIAN);
109   proto_tree_add_item(calcappprotocol_tree, hf_message_completed, message_tvb, MESSAGE_COMPLETED_OFFSET, MESSAGE_COMPLETED_LENGTH, ENC_BIG_ENDIAN);
110 }
111
112
113 static int
114 dissect_calcappprotocol(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
115 {
116   proto_item *calcappprotocol_item;
117   proto_tree *calcappprotocol_tree;
118
119   /* pinfo is NULL only if dissect_calcappprotocol_message is called from dissect_error cause */
120   if (pinfo)
121     col_set_str(pinfo->cinfo, COL_PROTOCOL, "CalcAppProtocol");
122
123   /* In the interest of speed, if "tree" is NULL, don't do any work not
124      necessary to generate protocol tree items. */
125   if (tree) {
126     /* create the calcappprotocol protocol tree */
127     calcappprotocol_item = proto_tree_add_item(tree, proto_calcappprotocol, message_tvb, 0, -1, ENC_BIG_ENDIAN);
128     calcappprotocol_tree = proto_item_add_subtree(calcappprotocol_item, ett_calcappprotocol);
129   } else {
130     calcappprotocol_tree = NULL;
131   };
132   /* dissect the message */
133   dissect_calcappprotocol_message(message_tvb, pinfo, calcappprotocol_tree);
134   return(TRUE);
135 }
136
137
138 /* Register the protocol with Wireshark */
139 void
140 proto_register_calcappprotocol(void)
141 {
142
143   /* Setup list of header fields */
144   static hf_register_info hf[] = {
145     { &hf_message_type,      { "Type",      "calcappprotocol.message_type",      FT_UINT8,  BASE_DEC, VALS(message_type_values), 0x0, NULL, HFILL } },
146     { &hf_message_flags,     { "Flags",     "calcappprotocol.message_flags",     FT_UINT8,  BASE_DEC, NULL,                      0x0, NULL, HFILL } },
147     { &hf_message_length,    { "Length",    "calcappprotocol.message_length",    FT_UINT16, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
148     { &hf_message_jobid,     { "JobID",     "calcappprotocol.message_jobid",     FT_UINT32, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
149     { &hf_message_jobsize,   { "JobSize",   "calcappprotocol.message_jobsize",   FT_UINT64, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
150     { &hf_message_completed, { "Completed", "calcappprotocol.message_completed", FT_UINT64, BASE_DEC, NULL,                      0x0, NULL, HFILL } },
151   };
152
153   /* Setup protocol subtree array */
154   static gint *ett[] = {
155     &ett_calcappprotocol
156   };
157
158   /* Register the protocol name and description */
159   proto_calcappprotocol = proto_register_protocol("Calculation Application Protocol", "CalcAppProtocol", "calcappprotocol");
160
161   /* Required function calls to register the header fields and subtrees used */
162   proto_register_field_array(proto_calcappprotocol, hf, array_length(hf));
163   proto_register_subtree_array(ett, array_length(ett));
164 }
165
166 void
167 proto_reg_handoff_calcappprotocol(void)
168 {
169   dissector_handle_t calcappprotocol_handle;
170
171   calcappprotocol_handle = new_create_dissector_handle(dissect_calcappprotocol, proto_calcappprotocol);
172   dissector_add_uint("sctp.ppi", CALCAPPPROTOCOL_PAYLOAD_PROTOCOL_ID_LEGACY, calcappprotocol_handle);
173   dissector_add_uint("sctp.ppi", CALCAPP_PAYLOAD_PROTOCOL_ID, calcappprotocol_handle);
174 }