Get rid of extra blanks in strings.
[obnox/wireshark/wip.git] / packet-esis.c
1 /* packet-esis.c
2  * Routines for ISO/OSI End System to Intermediate System  
3  * Routeing Exchange Protocol ISO 9542.
4  *
5  * $Id: packet-esis.c,v 1.9 2000/11/19 08:53:56 guy Exp $
6  * Ralf Schneider <Ralf.Schneider@t-online.de>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  *
28 */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <glib.h>
41 #include "packet.h"
42 #include "nlpid.h"
43 #include "packet-osi.h"
44 #include "packet-osi-options.h"
45 #include "packet-esis.h"
46
47
48 /* esis base header */
49 static int  proto_esis        = -1;
50
51 static int  hf_esis_nlpi      = -1;
52 static int  hf_esis_length    = -1;
53 static int  hf_esis_version   = -1;
54 static int  hf_esis_reserved  = -1;
55 static int  hf_esis_type      = -1;
56 static int  hf_esis_holdtime  = -1;
57 static int  hf_esis_checksum  = -1;
58
59 static gint ett_esis              = -1;
60 static gint ett_esis_area_addr    = -1;
61
62 static const value_string esis_vals[] = {
63   { ESIS_ESH_PDU, "ES HELLO"},
64   { ESIS_ISH_PDU, "IS HELLO"},
65   { ESIS_RD_PDU,  "RD REQUEST"},
66   { 0,             NULL} };
67
68 /* internal prototypes */
69
70 static void esis_dissect_esh_pdu( u_char len, tvbuff_t *tvb,
71                            packet_info *pinfo, proto_tree *treepd);
72 static void esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb,
73                            packet_info *pinfo, proto_tree *tree);
74 static void esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb,
75                            packet_info *pinfo, proto_tree *tree);
76
77 /* ################## Descriptions ###########################################*/
78 /* Parameters for the ESH PDU
79  * Source Address Parameter:
80  *
81  * Octet:    Length:   Parameter Type:
82  *     10          1   Number of Source Adresses ( NSAPs served by this Network
83  *     11          1   Source Address Length Indicator ( SAL )     #    Entity )
84  * 12-m-1   variable   Source Address ( NSAP )
85  *      m              Options, dissected in osi.c
86  *
87  *
88  * Parameter for the ISH PDU:
89  * Network Entity Title Parameter:
90  *
91  * Octet:    Length:   Parameter Type:
92  *     10          1   Network Entity Title Length Indicator ( NETL )
93  * 11-m-1   variable   Network Entity Title ( NET )
94  *      m              Options, dissected in osi.c
95  *
96  *
97  * Parameter for the RD PDU:
98  * When re-directed to an IS:
99  *
100  *  Octet:   Length:   Parameter Type:
101  *      10         1   Destination Address Length Indicator ( DAL )
102  *  11>m-1  variable   Destination Address ( DA )
103  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
104  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
105  *       n         1   Network Entity Title Length Indicator ( NETL )
106  * n+1>p-1  variable   Network Entity Title ( NET )
107  *       p             Options, dissected in osi.c
108  *
109  *
110  * Parameter for the RD PDU:
111  * When re-directed to an ES:
112  *
113  *  Octet:   Length:   Parameter Type:
114  *      10         1   Destination Address Length Indicator ( DAL )
115  *  11>m-1  variable   Destination Address ( DA )
116  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
117  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
118  *       n         1   Network Entity Title Length Indicator ( NETL ) == 0
119  *     n+1             Options, dissected in osi.c
120  *
121  */
122
123 /* ############################ Tool Functions ############################## */
124
125
126 /* ############################## Dissection Functions ###################### */
127 /*
128  * Name: dissect_esis_unknown()
129  *
130  * Description:
131  *   There was some error in the protocol and we are in unknown space
132  *   here.  Add a tree item to cover the error and go on.  Note
133  *   that we make sure we don't go off the end of the bleedin packet here!
134  *
135  *   This is just a copy of isis.c and isis.h, so I keep the stuff also
136  *   and adapt the names to cover possible protocol errors! Ive really no
137  *   idea wether I need this or not.
138  *  
139  * Input
140  *   int offset      : Current offset into packet data.
141  *   int len         : length of to dump.
142  *   proto_tree *    : tree of display data.  May be NULL.
143  *   frame_data * fd : frame data
144  *   char *          : format text
145  *
146  * Output:
147  *   void (may modify proto tree)
148  */
149 static void
150 esis_dissect_unknown( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
151                       char *fmat, ...){
152   va_list ap;
153
154   va_start(ap, fmat);
155   proto_tree_add_text_valist(tree, tvb, 0, tvb_length(tvb), fmat, ap);
156   va_end(ap);
157 }
158
159
160 static void
161 esis_dissect_esh_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
162                       proto_tree *tree) {
163   proto_tree *esis_area_tree;
164   int         offset  = 0;
165   int         no_sa   = 0;
166   int         sal     = 0;
167   
168   proto_item  *ti;
169   
170   if (tree) {
171     offset += ESIS_HDR_FIXED_LENGTH;
172
173     no_sa  = tvb_get_guint8(tvb, offset);
174     len   -= 1;
175
176     ti = proto_tree_add_text( tree, tvb, offset, tvb_length_remaining(tvb, offset),
177             "Number of Source Addresses (SA, Format: NSAP) : %u", no_sa );
178     offset++;
179     
180     esis_area_tree = proto_item_add_subtree( ti, ett_esis_area_addr );
181     while ( no_sa-- > 0 ) {
182        sal = (int) tvb_get_guint8(tvb, offset);
183        offset++;
184        proto_tree_add_text(esis_area_tree, tvb, offset, 1, "SAL: %2u Octets", sal);
185        proto_tree_add_text(esis_area_tree, tvb, offset + 1, sal,
186                            " SA: %s",
187                            print_nsap_net( tvb_get_ptr(tvb, offset, sal), sal ) );
188        offset += sal;
189        len    -= ( sal + 1 );
190     }
191     dissect_osi_options( PDU_TYPE_ESIS_ESH, len, tvb, offset, pinfo, tree );
192   }  
193 } /* esis_dissect_esh_pdu */ ;
194
195 static void
196 esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
197                       proto_tree *tree) {
198   
199   int   offset  = 0;
200   int   netl    = 0;
201
202   if (tree) {
203     offset += ESIS_HDR_FIXED_LENGTH;
204
205     netl = (int) tvb_get_guint8(tvb, offset);
206     proto_tree_add_text( tree, tvb, offset, netl + 1, 
207                          "### Network Entity Titel Section ###");
208     proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", netl);
209     proto_tree_add_text( tree, tvb, offset, netl,
210                            " NET: %s",
211                            print_nsap_net( tvb_get_ptr(tvb, offset, netl), netl ) );
212     offset += netl;
213     len    -= ( netl + 1 );
214
215     dissect_osi_options( PDU_TYPE_ESIS_ISH, len, tvb, offset, pinfo, tree );
216   }
217 };
218
219 static void
220 esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
221                            proto_tree *tree) {
222
223   int   offset  = 0;
224   int   tmpl    = 0;
225
226   if (tree) {
227     offset += ESIS_HDR_FIXED_LENGTH;
228
229     tmpl = (int) tvb_get_guint8(tvb, offset);
230     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
231                          "### Destination Address Section ###" );
232     proto_tree_add_text( tree, tvb, offset++, 1, "DAL: %2u Octets", tmpl);
233     proto_tree_add_text( tree, tvb, offset, tmpl,
234                          " DA : %s",
235                          print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
236     offset += tmpl;
237     len    -= ( tmpl + 1 );
238     tmpl    = (int) tvb_get_guint8(tvb, offset);
239
240     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
241                          "###  Subnetwork Address Section ###");
242     proto_tree_add_text( tree, tvb, offset++, 1, "BSNPAL: %2u Octets", tmpl);
243     proto_tree_add_text( tree, tvb, offset, tmpl,
244                            " BSNPA: %s",
245                            print_system_id( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
246     offset += tmpl;
247     len    -= ( tmpl + 1 );
248     tmpl    = (int) tvb_get_guint8(tvb, offset);
249
250     if ( 0 == tmpl ) {
251       proto_tree_add_text( tree, tvb, offset, 1, 
252                            "### No Network Entity Title Section ###" );
253       offset++;
254       len--;
255     }
256     else {
257       proto_tree_add_text( tree, tvb, offset, 1,
258                            "### Network Entity Title Section ###" );
259       proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", tmpl );
260       proto_tree_add_text( tree, tvb, offset, tmpl,
261                            " NET: %s",
262                            print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
263       offset += tmpl;
264       len    -= ( tmpl + 1 );
265     }
266     dissect_osi_options( PDU_TYPE_ESIS_RD, len, tvb, offset, pinfo, tree );
267   }
268 }
269
270
271 /*
272  * Name: dissect_esis()
273  * 
274  * Description:
275  *   Main entry area for esis de-mangling.  This will build the
276  *   main esis tree data and call the sub-protocols as needed.
277  *
278  * Input:
279  *   tvbuff *     : tvbuff referring to packet data
280  *   frame_data * : frame data (whole packet with extra info)
281  *   proto_tree * : tree of display data.  May be NULL.
282  *
283  * Output:
284  *   void, but we will add to the proto_tree if it is not NULL.
285  */
286 static void
287 dissect_esis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
288    char       *pdu_type_string        = NULL;
289    char       *pdu_type_format_string = "PDU Type      : %s (R:%s%s%s)";   
290    esis_hdr_t  ehdr;
291    proto_item *ti;
292    proto_tree *esis_tree    = NULL;
293    int         variable_len = 0;
294    u_int       tmp_uint     = 0;
295
296    CHECK_DISPLAY_AS_DATA(proto_esis, tvb, pinfo, tree);
297
298    pinfo->current_proto = "ESIS";
299
300    if (check_col(pinfo->fd, COL_PROTOCOL))
301      col_set_str(pinfo->fd, COL_PROTOCOL, "ESIS");
302
303    tvb_memcpy(tvb, (guint8 *)&ehdr, 0, sizeof ehdr);
304    
305    if (tree) {
306      ti = proto_tree_add_item(tree, proto_esis, tvb, 0, tvb_length(tvb), FALSE);
307      esis_tree = proto_item_add_subtree(ti, ett_esis);
308
309      if (ehdr.esis_version != ESIS_REQUIRED_VERSION){
310        esis_dissect_unknown(tvb, pinfo, esis_tree,
311                           "Unknown ESIS version (%u vs %u)",
312                            ehdr.esis_version, ESIS_REQUIRED_VERSION );
313        return;
314      }
315
316      proto_tree_add_uint( esis_tree, hf_esis_nlpi, tvb, 0, 1, ehdr.esis_nlpi );
317      proto_tree_add_uint( esis_tree, hf_esis_length, tvb,
318                           1, 1, ehdr.esis_length );
319      proto_tree_add_uint( esis_tree, hf_esis_version, tvb, 2, 1, 
320                           ehdr.esis_version );
321      proto_tree_add_uint( esis_tree, hf_esis_reserved, tvb, 3, 1, 
322                           ehdr.esis_reserved );
323
324      pdu_type_string = val_to_str(ehdr.esis_type&OSI_PDU_TYPE_MASK,
325                                   esis_vals, "Unknown (0x%x)");
326
327      proto_tree_add_uint_format( esis_tree, hf_esis_type, tvb, 4, 1, 
328                                  ehdr.esis_type, 
329                                  pdu_type_format_string,
330                                  pdu_type_string,
331                                  (ehdr.esis_type&BIT_8) ? "1" : "0",
332                                  (ehdr.esis_type&BIT_7) ? "1" : "0",
333                                  (ehdr.esis_type&BIT_6) ? "1" : "0");
334
335      tmp_uint = pntohs( ehdr.esis_holdtime );
336      proto_tree_add_uint_format(esis_tree, hf_esis_holdtime, tvb, 5, 2, 
337                                 tmp_uint, "Holding Time  : %u seconds",
338                                 tmp_uint );
339
340      tmp_uint = pntohs( ehdr.esis_checksum );
341      
342      proto_tree_add_uint_format( esis_tree, hf_esis_checksum, tvb, 7, 2,
343                                  tmp_uint, "Checksum      : 0x%x ( %s )", 
344                                  tmp_uint, calc_checksum( tvb, 0,
345                                                           ehdr.esis_length ,
346                                                           tmp_uint ) );
347    }
348
349
350    /*
351     * Let us make sure we use the same names for all our decodes
352     * here.  First, dump the name into info column, and THEN
353     * dispatch the sub-type.
354     */
355    if (check_col(pinfo->fd, COL_INFO)) {
356      col_add_str(pinfo->fd, COL_INFO, 
357                  val_to_str( ehdr.esis_type&OSI_PDU_TYPE_MASK, esis_vals,
358                              "Unknown (0x%x)" ) );
359    } 
360
361    variable_len = ehdr.esis_length - ESIS_HDR_FIXED_LENGTH;
362
363    switch (ehdr.esis_type & OSI_PDU_TYPE_MASK) {
364      case ESIS_ESH_PDU:
365           esis_dissect_esh_pdu( variable_len, tvb, pinfo, esis_tree);
366      break;
367      case ESIS_ISH_PDU:
368           esis_dissect_ish_pdu( variable_len, tvb, pinfo, esis_tree);
369      break;
370      case ESIS_RD_PDU:
371           esis_dissect_redirect_pdu( variable_len, tvb, pinfo, 
372                                      esis_tree);
373      break;
374      default:
375          esis_dissect_unknown(tvb, pinfo, esis_tree,
376                               "Unknown ESIS packet type 0x%x",
377                               ehdr.esis_type & OSI_PDU_TYPE_MASK );
378    }
379 } /* dissect_esis */
380
381
382 /*
383  * Name: proto_register_esis()
384  *
385  * Description:
386  *      main register for esis protocol set.  We register some display
387  *      formats and the protocol module variables.
388  *
389  *      NOTE: this procedure to autolinked by the makefile process that
390  *      builds register.c
391  *
392  * Input: 
393  *      void
394  *
395  * Output:
396  *      void
397  */
398 void 
399 proto_register_esis(void) {
400   static hf_register_info hf[] = {
401     { &hf_esis_nlpi,
402       { "Network Layer Protocol Identifier", "esis.nlpi",       
403         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, ""                       }},
404     { &hf_esis_length,
405       { "PDU Length    ", "esis.length", FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
406     { &hf_esis_version,
407       { "Version (==1) ", "esis.ver",    FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
408     { &hf_esis_reserved,
409       { "Reserved(==0) ", "esis.res",    FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
410     { &hf_esis_type,
411       { "PDU Type      ", "esis.type",   FT_UINT8, BASE_DEC, VALS(esis_vals),
412          0xff, "" }},
413     { &hf_esis_holdtime,
414       { "Holding Time  ", "esis.htime",  FT_UINT16, BASE_DEC, NULL, 0x0, " s"}},
415     { &hf_esis_checksum,
416       { "Checksum      ", "esis.chksum", FT_UINT16, BASE_HEX, NULL, 0x0, "" }}
417   };
418   /*
419    * 
420    * 
421    */
422   static gint *ett[] = {
423     &ett_esis,
424     &ett_esis_area_addr,
425   };
426
427   proto_esis = proto_register_protocol( PROTO_STRING_ESIS, "esis");
428   proto_register_field_array(proto_esis, hf, array_length(hf));
429   proto_register_subtree_array(ett, array_length(ett));
430 };
431
432 void
433 proto_reg_handoff_esis(void)
434 {
435   dissector_add("osinl", NLPID_ISO9542_ESIS, dissect_esis);
436 }