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