From Chris Maynard <christopher.maynard@gtech.com>:
[obnox/wireshark/wip.git] / plugins / docsis / packet-dccrsp.c
1 /* packet-dccrsp.c
2  * Routines for DCC Response Message  dissection
3  * Copyright 2004, Darryl Hymel <darryl.hymel[AT]arrisi.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30
31 #include "moduleinfo.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <gmodule.h>
38
39 #include <epan/packet.h>
40
41 #define DCCRSP_CM_JUMP_TIME 1
42 #define DCCRSP_KEY_SEQ_NUM 31
43 #define DCCRSP_HMAC_DIGEST 27
44
45 /* Define DCC-RSP CM Jump Time subtypes
46  * These are subtype of DCCRSP_CM_JUMP_TIME (1)
47  */
48 #define DCCRSP_CM_JUMP_TIME_LENGTH 1
49 #define DCCRSP_CM_JUMP_TIME_START 2
50
51 /* Initialize the protocol and registered fields */
52 static int proto_docsis_dccrsp = -1;
53
54 static int hf_docsis_dccrsp_tran_id = -1;
55 static int hf_docsis_dccrsp_conf_code = -1;
56 static int hf_docsis_dccrsp_cm_jump_time_length = -1;
57 static int hf_docsis_dccrsp_cm_jump_time_start = -1;
58 static int hf_docsis_dccrsp_key_seq_num = -1;
59 static int hf_docsis_dccrsp_hmac_digest = -1;
60
61 /* Initialize the subtree pointers */
62 static gint ett_docsis_dccrsp = -1;
63 static gint ett_docsis_dccrsp_cm_jump_time = -1;
64
65
66 /* Code to actually dissect the packets */
67 static void
68 dissect_dccrsp_cm_jump_time (tvbuff_t * tvb, proto_tree * tree, int start, guint16 len)
69 {
70   guint8 type, length;
71   proto_item *dcc_item;
72   proto_tree *dcc_tree;
73   int pos;
74    
75   pos = start;
76   dcc_item = proto_tree_add_text ( tree, tvb, start, len, "2 DCC-RSP CM Time Jump Encodings (Length = %u)", len);
77   dcc_tree = proto_item_add_subtree ( dcc_item , ett_docsis_dccrsp_cm_jump_time);
78   
79   while ( pos < ( start + len) ) 
80     {
81         type = tvb_get_guint8 (tvb, pos++);
82         length = tvb_get_guint8 (tvb, pos++);
83         
84         switch (type)
85           {
86             case DCCRSP_CM_JUMP_TIME_LENGTH:
87               if (length == 4)
88                 {
89                   proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_cm_jump_time_length, tvb,
90                                        pos, length, FALSE);
91                 }
92               else
93                 {
94                   THROW (ReportedBoundsError);
95                 }
96               break;
97             case DCCRSP_CM_JUMP_TIME_START:
98               if (length == 8)
99                 {
100                   proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_cm_jump_time_start, tvb,
101                                    pos, length, FALSE);
102                 }
103               else 
104                 {
105                   THROW (ReportedBoundsError);
106                 }
107               break;
108             }
109           pos = pos + length;
110       }
111 }
112 static void
113 dissect_dccrsp (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
114 {
115   guint16 pos;
116   guint8 type, length;
117   proto_tree *dcc_tree;
118   proto_item *dcc_item;
119   guint16 len;
120
121   len = tvb_length_remaining (tvb, 0);
122
123   if (check_col (pinfo->cinfo, COL_INFO))
124     {
125       col_clear (pinfo->cinfo, COL_INFO);
126       col_add_fstr (pinfo->cinfo, COL_INFO,"DCC-RSP Message: ");
127     }
128
129   if (tree)
130     {
131       dcc_item =
132         proto_tree_add_protocol_format (tree, proto_docsis_dccrsp, tvb, 0,
133                                         tvb_length_remaining (tvb, 0),
134                                         "DCC-RSP Message");
135       dcc_tree = proto_item_add_subtree (dcc_item, ett_docsis_dccrsp);
136       proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_tran_id, tvb, 0, 2, FALSE);
137       proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_conf_code, tvb, 2, 1, FALSE);
138
139       pos = 3;
140       while (pos < len)
141         {
142           type = tvb_get_guint8 (tvb, pos++);
143           length = tvb_get_guint8 (tvb, pos++);
144           switch (type)
145             {
146             case DCCRSP_CM_JUMP_TIME:
147               dissect_dccrsp_cm_jump_time (tvb , dcc_tree , pos , length );
148               break;
149             case DCCRSP_KEY_SEQ_NUM:
150               if (length == 1)
151                 {
152                   proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_key_seq_num, tvb,
153                                        pos, length, FALSE);
154                 }
155               else
156                 {
157                   THROW (ReportedBoundsError);
158                 }
159               break;
160             case DCCRSP_HMAC_DIGEST:
161               if (length == 20)
162                 {
163                   proto_tree_add_item (dcc_tree, hf_docsis_dccrsp_hmac_digest, tvb,
164                                        pos, length, FALSE);
165                 }
166               else
167                 {
168                   THROW (ReportedBoundsError);
169                 }
170               break;
171             }                   /* switch(type) */
172           pos = pos + length;
173         }                       /* while (pos < len) */
174     }                           /* if (tree) */
175
176 }
177 /* Register the protocol with Wireshark */
178
179 /* this format is require because a script is used to build the C function
180    that calls all the protocol registration.
181 */
182
183
184 void
185 proto_register_docsis_dccrsp (void)
186 {
187 /* Setup list of header fields  See Section 1.6.1 for details*/
188   static hf_register_info hf[] = {
189     {&hf_docsis_dccrsp_tran_id ,
190       {
191       "Transaction ID ", 
192       "docsis_dccrsp.tran_id",
193       FT_UINT16, BASE_DEC, NULL, 0x0,
194       "Transaction ID ", 
195       HFILL
196       }
197     },
198     {&hf_docsis_dccrsp_conf_code ,
199       {
200       "Confirmation Code ", 
201       "docsis_dccrsp.conf_code",
202       FT_UINT8, BASE_DEC, NULL, 0x0,
203       "Confirmation Code ", 
204       HFILL
205       }
206     },
207     {&hf_docsis_dccrsp_cm_jump_time_length ,
208       {
209       "Jump Time Length ", 
210       "docsis_dccrsp.cm_jump_time_length",
211       FT_UINT32, BASE_DEC, NULL, 0x0,
212       "Jump Time Length ", 
213       HFILL
214       }
215     },
216     {&hf_docsis_dccrsp_cm_jump_time_start ,
217       {
218       "Jump Time Start ", 
219       "docsis_dccrsp.cm_jump_time_start",
220       FT_UINT64, BASE_DEC, NULL, 0x0,
221       "Jump Time Start ", 
222       HFILL
223       }
224     },
225     {&hf_docsis_dccrsp_key_seq_num ,
226       {
227       "Auth Key Sequence Number ",
228       "docsis_dccrsp.key_seq_num",
229       FT_UINT8, BASE_DEC, NULL, 0x0,
230       "Auth Key Sequence Number ",
231       HFILL
232       }
233     },
234     {&hf_docsis_dccrsp_hmac_digest ,
235       {
236       "HMAC-DigestNumber ",
237       "docsis_dccrsp.hmac_digest",
238       FT_BYTES, BASE_DEC, NULL, 0x0,
239       "HMAC-DigestNumber ",
240       HFILL
241       }
242     },
243
244   };
245
246 /* Setup protocol subtree array */
247   static gint *ett[] = {
248     &ett_docsis_dccrsp,
249     &ett_docsis_dccrsp_cm_jump_time,
250   };
251
252 /* Register the protocol name and description */
253   proto_docsis_dccrsp =
254     proto_register_protocol ("DOCSIS Downstream Channel Change Response ",
255                              "DOCSIS DCC-RSP", "docsis_dccrsp");
256
257 /* Required function calls to register the header fields and subtrees used */
258   proto_register_field_array (proto_docsis_dccrsp, hf, array_length (hf));
259   proto_register_subtree_array (ett, array_length (ett));
260
261   register_dissector ("docsis_dccrsp", dissect_dccrsp, proto_docsis_dccrsp);
262 }
263
264
265 /* If this dissector uses sub-dissector registration add a registration routine.
266    This format is required because a script is used to find these routines and
267    create the code that calls these routines.
268 */
269 void
270 proto_reg_handoff_docsis_dccrsp (void)
271 {
272   dissector_handle_t docsis_dccrsp_handle;
273
274   docsis_dccrsp_handle = find_dissector ("docsis_dccrsp");
275   dissector_add ("docsis_mgmt", 0x18, docsis_dccrsp_handle);
276
277 }