From Kovarththanan Rajaratnam via bug 3548:
[obnox/wireshark/wip.git] / plugins / docsis / packet-rngrsp.c
1 /* packet-rngrsp.c
2  * Routines for Ranging Response Message dissection
3  * Copyright 2002, Anand V. Narwani <anand[AT]narwani.org>
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 #include "moduleinfo.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <gmodule.h>
37
38 #include <epan/packet.h>
39
40 #define RNGRSP_TIMING 1
41 #define RNGRSP_PWR_LEVEL_ADJ 2
42 #define RNGRSP_OFFSET_FREQ_ADJ 3
43 #define RNGRSP_TRANSMIT_EQ_ADJ 4
44 #define RNGRSP_RANGING_STATUS 5
45 #define RNGRSP_DOWN_FREQ_OVER 6
46 #define RNGRSP_UP_CHID_OVER 7
47
48 /* Initialize the protocol and registered fields */
49 static int proto_docsis_rngrsp = -1;
50 static int hf_docsis_rngrsp = -1;
51 static int hf_docsis_rngrsp_upstream_chid = -1;
52 static int hf_docsis_rngrsp_sid = -1;
53 static int hf_docsis_rngrsp_timing_adj = -1;
54 static int hf_docsis_rngrsp_power_adj = -1;
55 static int hf_docsis_rngrsp_freq_adj = -1;
56 static int hf_docsis_rngrsp_xmit_eq_adj = -1;
57 static int hf_docsis_rngrsp_ranging_status = -1;
58 static int hf_docsis_rngrsp_down_freq_over = -1;
59 static int hf_docsis_rngrsp_upstream_ch_over = -1;
60
61 static const value_string rng_stat_vals[] = {
62   {1, "Continue"},
63   {2, "Abort"},
64   {3, "Success"},
65   {0, NULL}
66 };
67
68 /* Initialize the subtree pointers */
69 static gint ett_docsis_rngrsp = -1;
70
71 /* Code to actually dissect the packets */
72 static void
73 dissect_rngrsp (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
74 {
75   proto_item *it;
76   proto_tree *rngrsp_tree;
77   guint8 tlvtype, tlvlen;
78   int pos;
79   gint length;
80   guint8 upchid;
81   guint16 sid;
82   gint8 pwr;
83   gint32 tim;
84
85   sid = tvb_get_ntohs (tvb, 0);
86   upchid = tvb_get_guint8 (tvb, 2);
87
88   if (check_col (pinfo->cinfo, COL_INFO))
89     {
90       col_clear (pinfo->cinfo, COL_INFO);
91       if (upchid > 0)
92         col_add_fstr (pinfo->cinfo, COL_INFO,
93                       "Ranging Response: SID = %u, Upstream Channel = %u (U%u)",
94                       sid, upchid, upchid - 1);
95       else
96         col_add_fstr (pinfo->cinfo, COL_INFO,
97                       "Ranging Response: SID = %u, Telephony Return", sid);
98
99     }
100
101   if (tree)
102     {
103       it =
104         proto_tree_add_protocol_format (tree, proto_docsis_rngrsp, tvb, 0, -1,
105                                         "Ranging Response");
106       rngrsp_tree = proto_item_add_subtree (it, ett_docsis_rngrsp);
107       proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_sid, tvb, 0, 2,
108                            FALSE);
109       proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_upstream_chid, tvb,
110                            2, 1, FALSE);
111
112       length = tvb_reported_length_remaining (tvb, 0);
113       pos = 3;
114       while (pos < length)
115         {
116           tlvtype = tvb_get_guint8 (tvb, pos++);
117           tlvlen = tvb_get_guint8 (tvb, pos++);
118           switch (tlvtype)
119             {
120             case RNGRSP_TIMING:
121               if (tlvlen == 4)
122                 {
123                   tim = tvb_get_ntohl (tvb, pos);
124                   proto_tree_add_int (rngrsp_tree,
125                                       hf_docsis_rngrsp_timing_adj, tvb, pos,
126                                       tlvlen, tim);
127                 }
128               else
129                 {
130                   THROW (ReportedBoundsError);
131                 }
132               break;
133             case RNGRSP_PWR_LEVEL_ADJ:
134               if (tlvlen == 1)
135                 {
136                   pwr = tvb_get_guint8 (tvb, pos);
137                   proto_tree_add_int (rngrsp_tree, hf_docsis_rngrsp_power_adj,
138                                       tvb, pos, tlvlen, pwr);
139                 }
140               else
141                 {
142                   THROW (ReportedBoundsError);
143                 }
144               break;
145             case RNGRSP_OFFSET_FREQ_ADJ:
146               if (tlvlen == 2)
147                 {
148                   proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_freq_adj,
149                                        tvb, pos, tlvlen, FALSE);
150                 }
151               else
152                 {
153                   THROW (ReportedBoundsError);
154                 }
155               break;
156             case RNGRSP_TRANSMIT_EQ_ADJ:
157               proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_xmit_eq_adj,
158                                    tvb, pos, tlvlen, FALSE);
159               break;
160             case RNGRSP_RANGING_STATUS:
161               if (tlvlen == 1)
162                 proto_tree_add_item (rngrsp_tree,
163                                      hf_docsis_rngrsp_ranging_status, tvb,
164                                      pos, tlvlen, FALSE);
165               else
166                 {
167                   THROW (ReportedBoundsError);
168                 }
169               break;
170             case RNGRSP_DOWN_FREQ_OVER:
171               if (tlvlen == 4)
172                 proto_tree_add_item (rngrsp_tree,
173                                      hf_docsis_rngrsp_down_freq_over, tvb,
174                                      pos, tlvlen, FALSE);
175               else
176                 {
177                   THROW (ReportedBoundsError);
178                 }
179               break;
180             case RNGRSP_UP_CHID_OVER:
181               if (tlvlen == 1)
182                 proto_tree_add_item (rngrsp_tree,
183                                      hf_docsis_rngrsp_upstream_ch_over, tvb,
184                                      pos, tlvlen, FALSE);
185               else
186                 {
187                   THROW (ReportedBoundsError);
188                 }
189               break;
190
191             }                   /* switch(tlvtype) */
192           pos = pos + tlvlen;
193         }                       /* while (pos < length) */
194     }                           /* if (tree) */
195 }
196
197
198
199
200 /* Register the protocol with Wireshark */
201
202 /* this format is require because a script is used to build the C function
203    that calls all the protocol registration.
204 */
205
206
207 void
208 proto_register_docsis_rngrsp (void)
209 {
210
211 /* Setup list of header fields  See Section 1.6.1 for details*/
212   static hf_register_info hf[] = {
213     {&hf_docsis_rngrsp,
214      {"RNG-RSP Message", "docsis_rngrsp",
215       FT_BYTES, BASE_NONE, NULL, 0x0,
216       "Ranging Response Message", HFILL}
217      },
218     {&hf_docsis_rngrsp_sid,
219      {"Service Identifier", "docsis_rngrsp.sid",
220       FT_UINT16, BASE_DEC, NULL, 0x0,
221       NULL, HFILL}
222      },
223     {&hf_docsis_rngrsp_upstream_chid,
224      {"Upstream Channel ID", "docsis_rngrsp.upchid",
225       FT_UINT8, BASE_DEC, NULL, 0x0,
226       NULL, HFILL}
227      },
228     {&hf_docsis_rngrsp_timing_adj,
229      {"Timing Adjust (6.25us/64)", "docsis_rngrsp.timingadj",
230       FT_INT32, BASE_DEC, NULL, 0x0,
231       "Timing Adjust", HFILL}
232      },
233     {&hf_docsis_rngrsp_power_adj,
234      {"Power Level Adjust (0.25dB units)", "docsis_rngrsp.poweradj",
235       FT_INT8, BASE_DEC, NULL, 0x0,
236       "Power Level Adjust", HFILL}
237      },
238     {&hf_docsis_rngrsp_freq_adj,
239      {"Offset Freq Adjust (Hz)", "docsis_rngrsp.freqadj",
240       FT_INT16, BASE_DEC, NULL, 0x0,
241       "Frequency Adjust", HFILL}
242      },
243     {&hf_docsis_rngrsp_xmit_eq_adj,
244      {"Transmit Equalisation Adjust", "docsis_rngrsp.xmit_eq_adj",
245       FT_BYTES, BASE_NONE, NULL, 0x0,
246       "Timing Equalisation Adjust", HFILL}
247      },
248     {&hf_docsis_rngrsp_ranging_status,
249      {"Ranging Status", "docsis_rngrsp.rng_stat",
250       FT_UINT8, BASE_DEC, VALS (rng_stat_vals), 0x0,
251       NULL, HFILL}
252      },
253     {&hf_docsis_rngrsp_down_freq_over,
254      {"Downstream Frequency Override (Hz)", "docsis_rngrsp.freq_over",
255       FT_UINT32, BASE_DEC, NULL, 0x0,
256       "Downstream Frequency Override", HFILL}
257      },
258     {&hf_docsis_rngrsp_upstream_ch_over,
259      {"Upstream Channel ID Override", "docsis_rngrsp.chid_override",
260       FT_UINT8, BASE_DEC, NULL, 0x0,
261       NULL, HFILL}
262      },
263
264   };
265
266 /* Setup protocol subtree array */
267   static gint *ett[] = {
268     &ett_docsis_rngrsp,
269   };
270
271 /* Register the protocol name and description */
272   proto_docsis_rngrsp = proto_register_protocol ("DOCSIS Ranging Response",
273                                                  "DOCSIS RNG-RSP",
274                                                  "docsis_rngrsp");
275
276 /* Required function calls to register the header fields and subtrees used */
277   proto_register_field_array (proto_docsis_rngrsp, hf, array_length (hf));
278   proto_register_subtree_array (ett, array_length (ett));
279
280   register_dissector ("docsis_rngrsp", dissect_rngrsp, proto_docsis_rngrsp);
281 }
282
283
284 /* If this dissector uses sub-dissector registration add a registration routine.
285    This format is required because a script is used to find these routines and
286    create the code that calls these routines.
287 */
288 void
289 proto_reg_handoff_docsis_rngrsp (void)
290 {
291   dissector_handle_t docsis_rngrsp_handle;
292
293   docsis_rngrsp_handle = find_dissector ("docsis_rngrsp");
294   dissector_add ("docsis_mgmt", 0x05, docsis_rngrsp_handle);
295
296 }