Don't show "Text" as one of the available fields.
[obnox/wireshark/wip.git] / packet-osi.c
1 /* packet-osi.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly
3  * Main entrance point and common functions
4  *
5  * $Id: packet-osi.c,v 1.38 2000/11/19 08:54:00 guy Exp $
6  * Laurent Deniel <deniel@worldnet.fr>
7  * Ralf Schneider <Ralf.Schneider@t-online.de>
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 <ctype.h>
41 #include <glib.h>
42 #include "packet.h"
43 #include "llcsaps.h"
44 #include "aftypes.h"
45 #include "nlpid.h"
46 #include "packet-osi.h"
47 #include "packet-isis.h"
48 #include "packet-esis.h"
49
50
51 gchar *print_system_id( const u_char *buffer, int length ) {
52   int           tmp;
53   u_char       *cur; 
54   static gchar  str[MAX_SYSTEMID_LEN * 3 + 5]; /* Don't trust exact matching */  
55  
56   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
57     sprintf( str, "<Invalid length of SYSTEM ID>");
58     return( str );
59   }  
60  
61   cur = str;
62   if ( ( 6 == length ) || ( 7 == length ) ) { /* Special case, print as MAC */
63     cur += sprintf(str, "[%02x:%02x:%02x_%02x:%02x:%02x]", buffer[0], buffer[1],
64                     buffer[2], buffer[3], buffer[4], buffer[5] );
65     if ( 7 == length ) {
66       sprintf( cur, "-%02x", buffer[6] );
67     }
68   }
69   else {
70     tmp = 0;
71     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
72       cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
73                       buffer[tmp++], buffer[tmp++] );
74     }
75     if ( 1 == tmp ) {   /* Special case for Designated IS */
76       sprintf( --cur, "-%02x", buffer[tmp] );
77     }
78     else {
79       for ( ; tmp < length; ) {  /* print the rest without dot */
80         cur += sprintf( cur, "%02x", buffer[tmp++] );
81       }
82     }
83   }
84   return( str );
85 }
86
87 gchar *print_area(const u_char *buffer, int length)
88 {
89   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
90    * and take away all these stupid resource consuming local statics
91    */
92   
93   static gchar  str[MAX_AREA_LEN * 3 + 20]; /* reserve space for nice layout */
94   gchar *cur;
95   u_int  tmp  = 0;
96
97   cur = str;
98
99   if (length <= 0 || length > MAX_AREA_LEN) {
100     sprintf( str, "<Invalid length of AREA>");
101     return( str );
102   }
103   
104   if ( (  ( NSAP_IDI_ISODCC          == *buffer )      
105        || ( NSAP_IDI_GOSIP2          == *buffer )
106        )
107        && 
108        (  ( RFC1237_FULLAREA_LEN     ==  length ) 
109        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
110        ) 
111      ) {    /* AFI is good and length is long enough  */
112   
113     if ( length > RFC1237_FULLAREA_LEN + 1 ) {  /* Special Case Designated IS */
114       sprintf( str, "<Invalid length of AREA for DCC / GOSIP AFI>");
115       return( str );
116     }
117  
118     cur += sprintf( cur, "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]", 
119                     buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], 
120                     buffer[5], buffer[6], buffer[7], buffer[8] );
121     cur += sprintf( cur, "[%02x:%02x|%02x:%02x]",
122                     buffer[9], buffer[10],  buffer[11], buffer[12] );
123     if ( RFC1237_FULLAREA_LEN + 1 == length ) {
124       sprintf( cur, "-[%02x]", buffer[20] );
125     }
126     return str;
127   }
128   else { /* print standard format */
129     if ( 4 < length ) { 
130       while ( tmp < length / 4 ) {      /* 16/4==4  four Octets left to print */
131         cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
132                         buffer[tmp++], buffer[tmp++] );
133       }
134       if ( 1 == tmp ) {                     /* Special case for Designated IS */
135         sprintf( --cur, "-%02x", buffer[tmp] );
136       }
137       else {
138         for ( ; tmp < length; ) {  /* print the rest without dot */ 
139           cur += sprintf( cur, "%02x", buffer[tmp++] );
140         }
141       } 
142     }
143     return( str );
144   }
145 } /* print_area */
146
147
148 gchar *print_nsap_net( const u_char *buffer, int length)
149 {
150   /* to do : NSAP / NET decoding */
151
152   static gchar  str[MAX_NSAP_LEN * 3 + 50]; /* reserve space for nice layout */
153   gchar *cur;
154
155   cur = str;
156
157   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
158     sprintf( str, "<Invalid length of NSAP>");
159     return( str );
160   }
161   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
162     cur += sprintf( cur, "%s", print_area( buffer, RFC1237_FULLAREA_LEN ) );
163     cur += sprintf( cur, "%s", print_system_id( buffer + RFC1237_FULLAREA_LEN, 
164                     RFC1237_SYSTEMID_LEN ) );
165     cur += sprintf( cur, "[%02x]", 
166                     buffer[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
167     if ( length == RFC1237_NSAP_LEN + 1 ) {
168       cur += sprintf( cur, "-%02x", buffer[ length -1 ] );
169     }
170     return ( str );
171   }
172   else {    /* probably format as standard */
173     return( print_area( buffer, length ) );
174   }
175 } /* print_nsap */
176
177
178 gchar *calc_checksum( tvbuff_t *tvb, int offset, u_int len, u_int checksum) {
179   u_int   calc_sum = 0;
180   u_int   count    = 0;
181   const gchar *buffer;
182   guint   available_len;
183
184   if ( 0 == checksum )
185     return( "Not Used" );
186
187   available_len = tvb_length_remaining( tvb, offset );
188   if ( available_len < len )
189     return( "Not checkable - not all of packet was captured" );
190
191   buffer = tvb_get_ptr( tvb, offset, len );
192   for ( count = 0; count < len; count++ ) {
193     calc_sum += (u_int) buffer[count];
194   }
195   calc_sum %= 255;  /* modulo 255 divison */
196   
197   if ( 0 == calc_sum )
198     return( "Is good" );
199   else
200     return( "Is wrong" );       /* XXX - what should the checksum be? */
201 }
202
203
204 /* main entry point */
205
206 const value_string nlpid_vals[] = {
207         { NLPID_NULL,            "NULL" },
208         { NLPID_T_70,            "T.70" },
209         { NLPID_X_633,           "X.633" },
210         { NLPID_Q_931,           "Q.931" },
211         { NLPID_Q_2931,          "Q.2931" },
212         { NLPID_Q_2119,          "Q.2119" },
213         { NLPID_SNAP,            "SNAP" },
214         { NLPID_ISO8473_CLNP,    "CLNP" },
215         { NLPID_ISO9542_ESIS,    "ESIS" },
216         { NLPID_ISO10589_ISIS,   "ISIS" },
217         { NLPID_ISO10747_IDRP,   "IDRP" },
218         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
219         { NLPID_ISO10030,        "ISO 10030" },
220         { NLPID_ISO11577,        "ISO 11577" },
221         { NLPID_IP,              "IP" },
222         { NLPID_PPP,             "PPP" },
223         { 0,                     NULL },
224 };
225
226 static dissector_table_t subdissector_table;
227
228 void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
229 {
230   guint8 nlpid;
231
232   pinfo->current_proto = "OSI";
233
234   nlpid = tvb_get_guint8(tvb, 0);
235
236   /* do lookup with the subdissector table */
237   if (dissector_try_port(subdissector_table, nlpid, tvb, pinfo, tree))
238       return;
239
240   switch (nlpid) {
241
242     /* ESIS (X.25) is not currently decoded */
243
244     case NLPID_ISO9542X25_ESIS:
245       if (check_col(pinfo->fd, COL_PROTOCOL)) {
246         col_set_str(pinfo->fd, COL_PROTOCOL, "ESIS (X.25)");
247       }
248       dissect_data(tvb, 0, pinfo, tree);
249       break;
250     case NLPID_ISO10747_IDRP:
251       if (check_col(pinfo->fd, COL_PROTOCOL)) {
252         col_set_str(pinfo->fd, COL_PROTOCOL, "IDRP");
253       }
254       dissect_data(tvb, 0, pinfo, tree);
255       break;
256     default:
257       if (check_col(pinfo->fd, COL_PROTOCOL)) {
258         col_set_str(pinfo->fd, COL_PROTOCOL, "ISO");
259       }
260       if (check_col(pinfo->fd, COL_INFO)) {
261         col_add_fstr(pinfo->fd, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
262       }
263       dissect_data(tvb, 0, pinfo, tree);
264       break;
265   }
266 } /* dissect_osi */
267
268 void
269 proto_register_osi(void)
270 {
271         /* There's no "OSI" protocol *per se*, but we do register a
272            dissector table so various protocols running at the
273            network layer can register themselves. */
274         subdissector_table = register_dissector_table("osinl");
275 }
276
277 void
278 proto_reg_handoff_osi(void)
279 {
280         dissector_add("llc.dsap", SAP_OSINL, dissect_osi);
281         dissector_add("null.type", BSD_AF_ISO, dissect_osi);
282 }