Update Anand Narwani's email address.
[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: packet-rngrsp.c,v 1.5 2003/05/28 14:52:52 gerald Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 "plugins/plugin_api.h"
31 #include "plugins/plugin_api_defs.h"
32 #include "moduleinfo.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <gmodule.h>
39
40 #include <epan/packet.h>
41
42 #define RNGRSP_TIMING 1
43 #define RNGRSP_PWR_LEVEL_ADJ 2
44 #define RNGRSP_OFFSET_FREQ_ADJ 3
45 #define RNGRSP_TRANSMIT_EQ_ADJ 4
46 #define RNGRSP_RANGING_STATUS 5
47 #define RNGRSP_DOWN_FREQ_OVER 6
48 #define RNGRSP_UP_CHID_OVER 7
49
50 /* Initialize the protocol and registered fields */
51 static int proto_docsis_rngrsp = -1;
52 static int hf_docsis_rngrsp = -1;
53 static int hf_docsis_rngrsp_upstream_chid = -1;
54 static int hf_docsis_rngrsp_sid = -1;
55 static int hf_docsis_rngrsp_timing_adj = -1;
56 static int hf_docsis_rngrsp_power_adj = -1;
57 static int hf_docsis_rngrsp_freq_adj = -1;
58 static int hf_docsis_rngrsp_xmit_eq_adj = -1;
59 static int hf_docsis_rngrsp_ranging_status = -1;
60 static int hf_docsis_rngrsp_down_freq_over = -1;
61 static int hf_docsis_rngrsp_upstream_ch_over = -1;
62
63 static const value_string rng_stat_vals[] = {
64   {1, "Continue"},
65   {2, "Abort"},
66   {3, "Success"},
67   {0, NULL}
68 };
69
70 /* Initialize the subtree pointers */
71 static gint ett_docsis_rngrsp = -1;
72
73 /* These Routines convert the specified type to a signed integer
74  * using a Two's Compliment Format */
75
76 gint8
77 byte_to_signed (guint8 i)
78 {
79   gint16 val;
80   if (i & 0x80)
81     {
82       val = ((~i) + 1);
83       val = -val;
84     }
85   else
86     {
87       val = i;
88     }
89   return (val);
90 }
91
92 gint16
93 short_to_signed (guint16 i)
94 {
95   gint16 val;
96
97   if (i & 0x8000)
98     {
99       val = (gint16) ((~i) + 1);
100       val = -val;
101     }
102   else
103     {
104       val = (gint16) i;
105     }
106
107   return (val);
108 }
109
110 gint32
111 long_to_signed (guint32 i)
112 {
113   gint32 val;
114
115   if (i & 0x80000000)
116     {
117       val = (gint32) ((~i) + 1);
118       val = -val;
119     }
120   else
121     {
122       val = (gint16) i;
123     }
124
125   return (val);
126
127 }
128
129 /* Code to actually dissect the packets */
130 static void
131 dissect_rngrsp (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
132 {
133   proto_item *it;
134   proto_tree *rngrsp_tree;
135   guint8 tlvtype, tlvlen;
136   guint16 pos, length;
137   guint8 upchid;
138   guint16 sid;
139   gint8 pwr;
140   gint32 tim;
141
142   sid = tvb_get_ntohs (tvb, 0);
143   upchid = tvb_get_guint8 (tvb, 2);
144
145   if (check_col (pinfo->cinfo, COL_INFO))
146     {
147       col_clear (pinfo->cinfo, COL_INFO);
148       if (upchid > 0)
149         col_add_fstr (pinfo->cinfo, COL_INFO,
150                       "Ranging Response: SID = %u, Upstream Channel = %u (U%u)",
151                       sid, upchid, upchid - 1);
152       else
153         col_add_fstr (pinfo->cinfo, COL_INFO,
154                       "Ranging Response: SID = %u, Telephony Return", sid);
155
156     }
157
158   if (tree)
159     {
160       it =
161         proto_tree_add_protocol_format (tree, proto_docsis_rngrsp, tvb, 0,
162                                         tvb_length_remaining (tvb, 0),
163                                         "Ranging Response");
164       rngrsp_tree = proto_item_add_subtree (it, ett_docsis_rngrsp);
165       proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_sid, tvb, 0, 2,
166                            FALSE);
167       proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_upstream_chid, tvb,
168                            2, 1, FALSE);
169
170       length = tvb_length_remaining (tvb, 0);
171       pos = 3;
172       while (pos < length)
173         {
174           tlvtype = tvb_get_guint8 (tvb, pos++);
175           tlvlen = tvb_get_guint8 (tvb, pos++);
176           switch (tlvtype)
177             {
178             case RNGRSP_TIMING:
179               if (tlvlen == 4)
180                 {
181                   tim = long_to_signed (tvb_get_ntohl (tvb, pos));
182                   proto_tree_add_int (rngrsp_tree,
183                                       hf_docsis_rngrsp_timing_adj, tvb, pos,
184                                       tlvlen, tim);
185                 }
186               else
187                 {
188                   THROW (ReportedBoundsError);
189                 }
190               break;
191             case RNGRSP_PWR_LEVEL_ADJ:
192               if (tlvlen == 1)
193                 {
194                   pwr = byte_to_signed (tvb_get_guint8 (tvb, pos));
195                   proto_tree_add_int (rngrsp_tree, hf_docsis_rngrsp_power_adj,
196                                       tvb, pos, tlvlen, pwr);
197                 }
198               else
199                 {
200                   THROW (ReportedBoundsError);
201                 }
202               break;
203             case RNGRSP_OFFSET_FREQ_ADJ:
204               if (tlvlen == 2)
205                 {
206                   proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_freq_adj,
207                                        tvb, pos, tlvlen, FALSE);
208                 }
209               else
210                 {
211                   THROW (ReportedBoundsError);
212                 }
213               break;
214             case RNGRSP_TRANSMIT_EQ_ADJ:
215               proto_tree_add_item (rngrsp_tree, hf_docsis_rngrsp_xmit_eq_adj,
216                                    tvb, pos, tlvlen, FALSE);
217               break;
218             case RNGRSP_RANGING_STATUS:
219               if (tlvlen == 1)
220                 proto_tree_add_item (rngrsp_tree,
221                                      hf_docsis_rngrsp_ranging_status, tvb,
222                                      pos, tlvlen, FALSE);
223               else
224                 {
225                   THROW (ReportedBoundsError);
226                 }
227               break;
228             case RNGRSP_DOWN_FREQ_OVER:
229               if (tlvlen == 4)
230                 proto_tree_add_item (rngrsp_tree,
231                                      hf_docsis_rngrsp_down_freq_over, tvb,
232                                      pos, tlvlen, FALSE);
233               else
234                 {
235                   THROW (ReportedBoundsError);
236                 }
237               break;
238             case RNGRSP_UP_CHID_OVER:
239               if (tlvlen == 1)
240                 proto_tree_add_item (rngrsp_tree,
241                                      hf_docsis_rngrsp_upstream_ch_over, tvb,
242                                      pos, tlvlen, FALSE);
243               else
244                 {
245                   THROW (ReportedBoundsError);
246                 }
247               break;
248
249             }                   /* switch(tlvtype) */
250           pos = pos + tlvlen;
251         }                       /* while (pos < length) */
252     }                           /* if (tree) */
253 }
254
255
256
257
258 /* Register the protocol with Ethereal */
259
260 /* this format is require because a script is used to build the C function
261    that calls all the protocol registration.
262 */
263
264
265 void
266 proto_register_docsis_rngrsp (void)
267 {
268
269 /* Setup list of header fields  See Section 1.6.1 for details*/
270   static hf_register_info hf[] = {
271     {&hf_docsis_rngrsp,
272      {"RNG-RSP Message", "docsis.rngrsp",
273       FT_BYTES, BASE_HEX, NULL, 0x0,
274       "Ranging Response Message", HFILL}
275      },
276     {&hf_docsis_rngrsp_sid,
277      {"Service Identifier", "docsis.rngrsp.sid",
278       FT_UINT16, BASE_DEC, NULL, 0x0,
279       "Service Identifier", HFILL}
280      },
281     {&hf_docsis_rngrsp_upstream_chid,
282      {"Upstream Channel ID", "docsis.rngrsp.upchid",
283       FT_UINT8, BASE_DEC, NULL, 0x0,
284       "Upstream Channel ID", HFILL}
285      },
286     {&hf_docsis_rngrsp_timing_adj,
287      {"Timing Adjust (6.25us/64)", "docsis.rngrsp.timingadj",
288       FT_INT32, BASE_DEC, NULL, 0x0,
289       "Timing Adjust", HFILL}
290      },
291     {&hf_docsis_rngrsp_power_adj,
292      {"Power Level Adjust (0.25dB units)", "docsis.rngrsp.poweradj",
293       FT_INT8, BASE_DEC, NULL, 0x0,
294       "Power Level Adjust", HFILL}
295      },
296     {&hf_docsis_rngrsp_freq_adj,
297      {"Offset Freq Adjust (Hz)", "docsis.rngrsp.freqadj",
298       FT_INT16, BASE_DEC, NULL, 0x0,
299       "Frequency Adjust", HFILL}
300      },
301     {&hf_docsis_rngrsp_xmit_eq_adj,
302      {"Transmit Equalisation Adjust", "docsis.rngrsp.xmit_eq_adj",
303       FT_BYTES, BASE_HEX, NULL, 0x0,
304       "Timing Equalisation Adjust", HFILL}
305      },
306     {&hf_docsis_rngrsp_ranging_status,
307      {"Ranging Status", "docsis.rngrsp.rng_stat",
308       FT_UINT8, BASE_DEC, VALS (rng_stat_vals), 0x0,
309       "Ranging Status", HFILL}
310      },
311     {&hf_docsis_rngrsp_down_freq_over,
312      {"Downstream Frequency Override (Hz)", "docsis.rngrsp.freq_over",
313       FT_UINT32, BASE_DEC, NULL, 0x0,
314       "Downstream Frequency Override", HFILL}
315      },
316     {&hf_docsis_rngrsp_upstream_ch_over,
317      {"Upstream Channel ID Override", "docsis.rngrsp.chid_override",
318       FT_UINT8, BASE_DEC, NULL, 0x0,
319       "Upstream Channel ID Override", HFILL}
320      },
321
322   };
323
324 /* Setup protocol subtree array */
325   static gint *ett[] = {
326     &ett_docsis_rngrsp,
327   };
328
329 /* Register the protocol name and description */
330   proto_docsis_rngrsp = proto_register_protocol ("DOCSIS Ranging Response",
331                                                  "DOCSIS RNG-RSP",
332                                                  "docsis_rngrsp");
333
334 /* Required function calls to register the header fields and subtrees used */
335   proto_register_field_array (proto_docsis_rngrsp, hf, array_length (hf));
336   proto_register_subtree_array (ett, array_length (ett));
337
338   register_dissector ("docsis_rngrsp", dissect_rngrsp, proto_docsis_rngrsp);
339 }
340
341
342 /* If this dissector uses sub-dissector registration add a registration routine.
343    This format is required because a script is used to find these routines and
344    create the code that calls these routines.
345 */
346 void
347 proto_reg_handoff_docsis_rngrsp (void)
348 {
349   dissector_handle_t docsis_rngrsp_handle;
350
351   docsis_rngrsp_handle = find_dissector ("docsis_rngrsp");
352   dissector_add ("docsis_mgmt", 0x05, docsis_rngrsp_handle);
353
354 }