removed some gcc warnings (hopefully)
[obnox/wireshark/wip.git] / epan / dissectors / packet-rpl.c
1 /* packet-rpl.c
2  * Routines for RPL
3  * Jochen Friedrich <jochen@scram.de>
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 <glib.h>
31 #include <epan/packet.h>
32
33 #include "llcsaps.h"
34 #include "packet-llc.h"
35
36 static int proto_rpl          = -1;
37
38 static int hf_rpl_type        = -1;
39 static int hf_rpl_corrval     = -1;
40 static int hf_rpl_respval     = -1;
41 static int hf_rpl_maxframe    = -1;
42 static int hf_rpl_connclass   = -1;
43 static int hf_rpl_lmac        = -1;
44 static int hf_rpl_smac        = -1;
45 static int hf_rpl_sap         = -1;
46 static int hf_rpl_equipment   = -1;
47 static int hf_rpl_memsize     = -1;
48 static int hf_rpl_bsmversion  = -1;
49 static int hf_rpl_adapterid   = -1;
50 static int hf_rpl_shortname   = -1;
51 static int hf_rpl_laddress    = -1;
52 static int hf_rpl_xaddress    = -1;
53 static int hf_rpl_sequence    = -1;
54 static int hf_rpl_config      = -1;
55 static int hf_rpl_flags       = -1;
56 static int hf_rpl_data        = -1;
57 static int hf_rpl_ec          = -1;
58
59 static gint ett_rpl           = -1;
60 static gint ett_rpl_0004      = -1;
61 static gint ett_rpl_0008      = -1;
62 static gint ett_rpl_4003      = -1;
63 static gint ett_rpl_4006      = -1;
64 static gint ett_rpl_4007      = -1;
65 static gint ett_rpl_4009      = -1;
66 static gint ett_rpl_400a      = -1;
67 static gint ett_rpl_400b      = -1;
68 static gint ett_rpl_400c      = -1;
69 static gint ett_rpl_4011      = -1;
70 static gint ett_rpl_4018      = -1;
71 static gint ett_rpl_c005      = -1;
72 static gint ett_rpl_c014      = -1;
73 static gint ett_rpl_unkn      = -1;
74
75 static const value_string rpl_type_vals[] = {
76         { 1,            "FIND Command" },
77         { 2,            "FOUND Frame" },
78         { 4,            "Search Vector" },
79         { 8,            "Connect Info Vector" },
80         { 0x10,         "Send File Request" },
81         { 0x20,         "File Data Response" },
82         { 0x4003,       "Correlator Vector" },
83         { 0x4006,       "Loader Address Vector" },
84         { 0x4007,       "Loader SAP Vector" },
85         { 0x4009,       "Frame Size Sub-Vector" },
86         { 0x400a,       "Connect Class Sub-Vector" },
87         { 0x400b,       "Response Correlator" },
88         { 0x400c,       "Set Address Vector" },
89         { 0x4011,       "Sequence Header" },
90         { 0x4018,       "File Data Vector" },
91         { 0xc005,       "Loader Info Sub-Vector" },
92         { 0xc014,       "Loader Header" },
93         { 0x0,  NULL }
94 };
95
96 static dissector_handle_t data_handle;
97
98 static void
99 dissect_rpl_container(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
100 {
101         guint16 len, type, sublen, subtyp;
102         proto_item *ti;
103         proto_tree *rpl_container_tree;
104         guint16 offset;
105         gint ett_type;
106         gint length, reported_length;
107
108         len = tvb_get_ntohs(tvb, 0);
109         proto_tree_add_text(tree, tvb, 0, 2, "Length: %u", len);
110
111         type = tvb_get_ntohs(tvb, 2);
112         proto_tree_add_text(tree, tvb, 2, 2, "Type: %s", 
113                 val_to_str(type, rpl_type_vals, "Unknown Type"));
114         offset = 4;
115
116         switch (type) {
117                 case 1:
118                 case 2:
119                 case 4:
120                 case 8:
121                 case 0x10:
122                 case 0x20:
123                         while (len >= offset+4) {
124                                 sublen = tvb_get_ntohs(tvb, offset);
125                                 subtyp = tvb_get_ntohs(tvb, offset+2);
126                                 ett_type = ett_rpl_unkn;
127                                 if(subtyp == 0x0004) ett_type = ett_rpl_0004;
128                                 if(subtyp == 0x0008) ett_type = ett_rpl_0008;
129                                 if(subtyp == 0x4003) ett_type = ett_rpl_4003;
130                                 if(subtyp == 0x4006) ett_type = ett_rpl_4006;
131                                 if(subtyp == 0x4007) ett_type = ett_rpl_4007;
132                                 if(subtyp == 0x4009) ett_type = ett_rpl_4009;
133                                 if(subtyp == 0x400a) ett_type = ett_rpl_400a;
134                                 if(subtyp == 0x400b) ett_type = ett_rpl_400b;
135                                 if(subtyp == 0x400c) ett_type = ett_rpl_400c;
136                                 if(subtyp == 0x4011) ett_type = ett_rpl_4011;
137                                 if(subtyp == 0x4018) ett_type = ett_rpl_4018;
138                                 if(subtyp == 0xc005) ett_type = ett_rpl_c005;
139                                 if(subtyp == 0xc014) ett_type = ett_rpl_c014;
140                                 ti = proto_tree_add_text(tree, tvb,
141                                         offset, sublen, val_to_str(subtyp, 
142                                         rpl_type_vals, "Unknown Type"));
143                                 rpl_container_tree = proto_item_add_subtree(ti, 
144                                         ett_type);
145                                 length = tvb_length_remaining(tvb, offset);
146                                 if (length > sublen)
147                                         length = sublen;
148                                 reported_length = tvb_reported_length_remaining(tvb, offset);
149                                 if (reported_length > sublen)
150                                         reported_length = sublen;
151                                 dissect_rpl_container(tvb_new_subset(tvb, 
152                                         offset, length, reported_length),
153                                         pinfo, rpl_container_tree);
154
155                                 offset += sublen;
156                         }       
157                         break;
158
159                 case 0x4003:
160                         proto_tree_add_item(tree, hf_rpl_corrval, 
161                                 tvb, offset, 4, FALSE);
162                         offset += 4;
163                         break;
164
165                 case 0x4006:
166                         proto_tree_add_item(tree, hf_rpl_lmac, 
167                                 tvb, offset, 6, FALSE);
168                         offset += 6;
169                         break;
170
171                 case 0x4007:
172                         proto_tree_add_item(tree, hf_rpl_sap, 
173                                 tvb, offset, 1, FALSE);
174                         offset ++;
175                         break;
176
177                 case 0x4009:
178                         proto_tree_add_item(tree, hf_rpl_maxframe, 
179                                 tvb, offset, 2, FALSE);
180                         offset += 2;
181                         break;
182
183                 case 0x400a:
184                         proto_tree_add_item(tree, hf_rpl_connclass, 
185                                 tvb, offset, 2, FALSE);
186                         offset += 2;
187                         break;
188
189                 case 0x400b:
190                         proto_tree_add_item(tree, hf_rpl_respval, 
191                                 tvb, offset, 1, FALSE);
192                         offset ++;
193                         break;
194
195                 case 0x400c:
196                         proto_tree_add_item(tree, hf_rpl_smac, 
197                                 tvb, offset, 6, FALSE);
198                         offset += 6;
199                         break;
200
201                 case 0x4011:
202                         proto_tree_add_item(tree, hf_rpl_sequence, 
203                                 tvb, offset, 4, FALSE);
204                         offset += 4;
205                         break;
206
207                 case 0x4018:
208                         proto_tree_add_item(tree, hf_rpl_data, 
209                                 tvb, offset, len-4, FALSE);
210                         offset += len - 4;
211                         break;
212
213                 case 0xc005:
214                         proto_tree_add_item(tree, hf_rpl_config, 
215                                 tvb, offset, 8, FALSE);
216                         offset += 8;
217                         proto_tree_add_item(tree, hf_rpl_equipment, 
218                                 tvb, offset, 2, FALSE);
219                         offset += 2;
220                         proto_tree_add_item(tree, hf_rpl_memsize, 
221                                 tvb, offset, 2, FALSE);
222                         offset += 2;
223                         proto_tree_add_item(tree, hf_rpl_bsmversion, 
224                                 tvb, offset, 2, FALSE);
225                         offset += 2;
226                         proto_tree_add_item(tree, hf_rpl_ec, 
227                                 tvb, offset, 6, FALSE);
228                         offset += 6;
229                         proto_tree_add_item(tree, hf_rpl_adapterid, 
230                                 tvb, offset, 2, FALSE);
231                         offset += 2;
232                         proto_tree_add_item(tree, hf_rpl_shortname, 
233                                 tvb, offset, 10, FALSE);
234                         offset += 10;
235                         break;
236
237                 case 0xc014:
238                         proto_tree_add_item(tree, hf_rpl_laddress, 
239                                 tvb, offset, 4, FALSE);
240                         offset += 4;
241                         proto_tree_add_item(tree, hf_rpl_xaddress, 
242                                 tvb, offset, 4, FALSE);
243                         offset += 4;
244                         proto_tree_add_item(tree, hf_rpl_flags, 
245                                 tvb, offset, 1, FALSE);
246                         offset ++;
247                         break;
248
249                 default:
250                         call_dissector(data_handle, 
251                                 tvb_new_subset(tvb, 4, -1, -1), pinfo,
252                                 tree);
253                         break;
254         }
255         if (tvb_reported_length(tvb) > offset)
256                 call_dissector(data_handle, 
257                         tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
258 }
259
260 static void
261 dissect_rpl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
262 {
263         guint16 rpl_len, rpl_type;
264         proto_item *ti;
265         proto_tree *rpl_tree;
266         tvbuff_t *next_tvb;
267
268         if (check_col(pinfo->cinfo, COL_PROTOCOL))
269                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPL");
270
271         rpl_len  = tvb_get_ntohs(tvb, 0);
272         rpl_type = tvb_get_ntohs(tvb, 2);
273
274         if (check_col(pinfo->cinfo, COL_INFO)) {
275                 col_set_str(pinfo->cinfo, COL_INFO,
276                     val_to_str(rpl_type, rpl_type_vals, "Unknown Type"));
277         }
278         if (tree) {
279                 ti = proto_tree_add_item(tree, proto_rpl, tvb, 0, 
280                         rpl_len, FALSE);
281                 rpl_tree = proto_item_add_subtree(ti, ett_rpl);
282                 proto_tree_add_uint_hidden(rpl_tree, hf_rpl_type, tvb, 2, 2,
283                         rpl_type);
284                 next_tvb = tvb_new_subset(tvb, 0, -1, -1);
285                 set_actual_length(next_tvb, rpl_len);
286                 dissect_rpl_container(next_tvb, pinfo, rpl_tree);
287         
288                 if (tvb_reported_length(tvb) > rpl_len)
289                         call_dissector(data_handle, 
290                                 tvb_new_subset(tvb, rpl_len, -1, -1), pinfo,
291                                     tree);
292         }
293 }
294
295 void
296 proto_register_rpl(void)
297 {
298         static hf_register_info hf[] = {
299                 { &hf_rpl_type,
300                         { "Type", "rpl.type", 
301                                 FT_UINT16, BASE_DEC, NULL, 0x0,
302                                 "RPL Packet Type", HFILL }},
303                 { &hf_rpl_corrval,
304                         { "Correlator Value", "rpl.corrval", 
305                                 FT_UINT32, BASE_HEX, NULL, 0x0,
306                                 "RPL Correlator Value", HFILL }},
307                 { &hf_rpl_respval,
308                         { "Response Code", "rpl.respval", 
309                                 FT_UINT8, BASE_DEC, NULL, 0x0,
310                                 "RPL Response Code", HFILL }},
311                 { &hf_rpl_maxframe,
312                         { "Maximum Frame Size", "rpl.maxframe", 
313                                 FT_UINT16, BASE_DEC, NULL, 0x0,
314                                 "RPL Maximum Frame Size", HFILL }},
315                 { &hf_rpl_connclass,
316                         { "Connection Class", "rpl.connclass", 
317                                 FT_UINT16, BASE_HEX, NULL, 0x0,
318                                 "RPL Connection Class", HFILL }},
319                 { &hf_rpl_lmac,
320                         { "Loader MAC Address", "rpl.lmac", 
321                                 FT_ETHER, BASE_NONE, NULL, 0x0,
322                                 "RPL Loader MAC Address", HFILL }},
323                 { &hf_rpl_smac,
324                         { "Set MAC Address", "rpl.smac", 
325                                 FT_ETHER, BASE_NONE, NULL, 0x0,
326                                 "RPL Set MAC Address", HFILL }},
327                 { &hf_rpl_sap,
328                         { "SAP", "rpl.sap", 
329                                 FT_UINT8, BASE_HEX, VALS(sap_vals), 0x0,
330                                 "RPL SAP", HFILL }},
331                 { &hf_rpl_equipment,
332                         { "Equipment", "rpl.equipment", 
333                                 FT_UINT16, BASE_HEX, NULL, 0x0,
334                                 "RPL Equipment - AX from INT 11h", HFILL }},
335                 { &hf_rpl_memsize,
336                         { "Memory Size", "rpl.memsize", 
337                                 FT_UINT16, BASE_DEC, NULL, 0x0,
338                                 "RPL Memory Size - AX from INT 12h MINUS 32k"
339                                 " MINUS the Boot ROM Size", HFILL }},
340                 { &hf_rpl_bsmversion,
341                         { "BSM Version", "rpl.bsmversion", 
342                                 FT_UINT16, BASE_HEX, NULL, 0x0,
343                                 "RPL Version of BSM.obj", HFILL }},
344                 { &hf_rpl_adapterid,
345                         { "Adapter ID", "rpl.adapterid", 
346                                 FT_UINT16, BASE_HEX, NULL, 0x0,
347                                 "RPL Adapter ID", HFILL }},
348                 { &hf_rpl_shortname,
349                         { "Short Name", "rpl.shortname", 
350                                 FT_BYTES, BASE_HEX, NULL, 0x0,
351                                 "RPL BSM Short Name", HFILL }},
352                 { &hf_rpl_laddress,
353                         { "Locate Address", "rpl.laddress", 
354                                 FT_UINT32, BASE_HEX, NULL, 0x0,
355                                 "RPL Locate Address", HFILL }},
356                 { &hf_rpl_xaddress,
357                         { "XFER Address", "rpl.xaddress", 
358                                 FT_UINT32, BASE_HEX, NULL, 0x0,
359                                 "RPL Transfer Control Address", HFILL }},
360                 { &hf_rpl_sequence,
361                         { "Sequence Number", "rpl.sequence", 
362                                 FT_UINT32, BASE_HEX, NULL, 0x0,
363                                 "RPL Sequence Number", HFILL }},
364                 { &hf_rpl_config,
365                         { "Configuration", "rpl.config", 
366                                 FT_BYTES, BASE_HEX, NULL, 0x0,
367                                  "RPL Configuration", HFILL }},
368                 { &hf_rpl_flags,
369                         { "Flags", "rpl.flags", 
370                                 FT_UINT8, BASE_HEX, NULL, 0x0,
371                                 "RPL Bit Significant Option Flags", HFILL }},
372                 { &hf_rpl_data,
373                         { "Data", "rpl.data", 
374                                 FT_BYTES, BASE_HEX, NULL, 0x0,
375                                 "RPL Binary File Data", HFILL }},
376                 { &hf_rpl_ec,
377                         { "EC", "rpl.ec",
378                                 FT_BYTES, BASE_HEX, NULL, 0x0,
379                                 "RPL EC", HFILL }},
380         };
381
382         static gint *ett[] = {
383                 &ett_rpl,
384                 &ett_rpl_0004,
385                 &ett_rpl_0008,
386                 &ett_rpl_4003,
387                 &ett_rpl_4006,
388                 &ett_rpl_4007,
389                 &ett_rpl_4009,
390                 &ett_rpl_400a,
391                 &ett_rpl_400b,
392                 &ett_rpl_400c,
393                 &ett_rpl_4011,
394                 &ett_rpl_4018,
395                 &ett_rpl_c005,
396                 &ett_rpl_c014,
397                 &ett_rpl_unkn
398         };
399
400         proto_rpl = proto_register_protocol("Remote Program Load",
401             "RPL", "rpl");
402         proto_register_field_array(proto_rpl, hf, array_length(hf));
403         proto_register_subtree_array(ett, array_length(ett));
404         register_dissector("rpl", dissect_rpl, proto_rpl);
405 }
406
407 void
408 proto_reg_handoff_rpl(void)
409 {
410         dissector_handle_t rpl_handle;
411
412         data_handle = find_dissector("data");
413         rpl_handle = find_dissector("rpl");
414         dissector_add("llc.dsap", SAP_RPL, rpl_handle);
415 }