Ralf Schneider's changes to enhance to OSI CLNP, CLTP, and ISIS support
[metze/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.30 2000/04/15 22:11:12 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  * To do:
30  *
31  * - add other network protocols (IS-IS)
32  * - add NSAP decoding & resolution
33  * 
34  *
35  */
36
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <glib.h>
49 #include "packet.h"
50 #include "packet-h1.h"
51 #include "nlpid.h"
52 #include "packet-osi.h"
53 #include "packet-clnp.h"
54 #include "packet-isis.h"
55 #include "packet-esis.h"
56
57
58 gchar *print_system_id( const u_char *buffer, int length ) {
59   int           tmp;
60   u_char       *cur; 
61   static gchar  str[MAX_SYSTEMID_LEN * 3 + 5]; /* Don't trust exact matching */  
62  
63   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
64     sprintf( str, "<Invalid length of SYSTEM ID>");
65     return( str );
66   }  
67  
68   cur = str;
69   if ( ( 6 == length ) || ( 7 == length ) ) { /* Special case, print as MAC */
70     cur += sprintf(str, "[%02x:%02x:%02x_%02x:%02x:%02x]", buffer[0], buffer[1],
71                     buffer[2], buffer[3], buffer[4], buffer[5] );
72     if ( 7 == length ) {
73       sprintf( cur, "-%02x", buffer[6] );
74     }
75   }
76   else {
77     tmp = 0;
78     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
79       cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
80                       buffer[tmp++], buffer[tmp++] );
81     }
82     if ( 1 == tmp ) {   /* Special case for Designated IS */
83       sprintf( --cur, "-%02x", buffer[tmp] );
84     }
85     else {
86       for ( ; tmp < length; ) {  /* print the rest without dot */
87         cur += sprintf( cur, "%02x", buffer[tmp++] );
88       }
89     }
90   }
91   return( str );
92 }
93
94 gchar *print_area(const u_char *buffer, int length)
95 {
96   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
97    * and take away all these stupid resource consuming local statics
98    */
99   
100   static gchar  str[MAX_AREA_LEN * 3 + 20]; /* reserve space for nice layout */
101   gchar *cur;
102   u_int  tmp  = 0;
103
104   cur = str;
105
106   if (length <= 0 || length > MAX_AREA_LEN) {
107     sprintf( str, "<Invalid length of AREA>");
108     return( str );
109   }
110   
111   if ( (  ( NSAP_IDI_ISODCC          == *buffer )      
112        || ( NSAP_IDI_GOSIP2          == *buffer )
113        )
114        && 
115        (  ( RFC1237_FULLAREA_LEN     ==  length ) 
116        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
117        ) 
118      ) {    /* AFI is good and length is long enough  */
119   
120     if ( length > RFC1237_FULLAREA_LEN + 1 ) {  /* Special Case Designated IS */
121       sprintf( str, "<Invalid length of AREA for DCC / GOSIP AFI>");
122       return( str );
123     }
124  
125     cur += sprintf( cur, "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]", 
126                     buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], 
127                     buffer[5], buffer[6], buffer[7], buffer[8] );
128     cur += sprintf( cur, "[%02x:%02x|%02x:%02x]",
129                     buffer[9], buffer[10],  buffer[11], buffer[12] );
130     if ( RFC1237_FULLAREA_LEN + 1 == length ) {
131       sprintf( cur, "-[%02x]", buffer[20] );
132     }
133     return str;
134   }
135   else { /* print standard format */
136     if ( 4 < length ) { 
137       while ( tmp < length / 4 ) {      /* 16/4==4  four Octets left to print */
138         cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
139                         buffer[tmp++], buffer[tmp++] );
140       }
141       if ( 1 == tmp ) {                     /* Special case for Designated IS */
142         sprintf( --cur, "-%02x", buffer[tmp] );
143       }
144       else {
145         for ( ; tmp < length; ) {  /* print the rest without dot */ 
146           cur += sprintf( cur, "%02x", buffer[tmp++] );
147         }
148       } 
149     }
150     return( str );
151   }
152 } /* print_area */
153
154
155 gchar *print_nsap_net( const u_char *buffer, int length)
156 {
157   /* to do : NSAP / NET decoding */
158
159   static gchar  str[MAX_NSAP_LEN * 3 + 50]; /* reserve space for nice layout */
160   gchar *cur;
161
162   cur = str;
163
164   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
165     sprintf( str, "<Invalid length of NSAP>");
166     return( str );
167   }
168   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
169     cur += sprintf( cur, "%s", print_area( buffer, RFC1237_FULLAREA_LEN ) );
170     cur += sprintf( cur, "%s", print_system_id( buffer + RFC1237_FULLAREA_LEN, 
171                     RFC1237_SYSTEMID_LEN ) );
172     cur += sprintf( cur, "[%02x]", 
173                     buffer[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
174     if ( length == RFC1237_NSAP_LEN + 1 ) {
175       cur += sprintf( cur, "-%02x", buffer[ length -1 ] );
176     }
177     return ( str );
178   }
179   else {    /* probably format as standard */
180     return( print_area( buffer, length ) );
181   }
182 } /* print_nsap */
183
184
185 gchar *calc_checksum( const u_char *buffer, u_int len, u_int checksum) {
186   u_int   calc_sum = 0;
187   u_int   count    = 0;
188
189   static char *checksum_string[] = { "Not Used", 
190                                      "Is good",
191                                      "Is wrong" }; 
192   if ( 0 == checksum )
193     return( checksum_string[0] );
194
195   for ( count = 0; count < len; count++ ) {
196     calc_sum += (u_int) buffer[count];
197   }
198   calc_sum %= 255;  /* modulo 255 divison */
199   
200   if ( 0 == calc_sum )
201     return( checksum_string[1] );
202   else
203     return( checksum_string[2] );
204 }
205
206
207 /* main entry point */
208
209 const value_string nlpid_vals[] = {
210         { NLPID_NULL,            "NULL" },
211         { NLPID_T_70,            "T.70" },
212         { NLPID_X_633,           "X.633" },
213         { NLPID_Q_931,           "Q.931" },
214         { NLPID_Q_2931,          "Q.2931" },
215         { NLPID_Q_2119,          "Q.2119" },
216         { NLPID_SNAP,            "SNAP" },
217         { NLPID_ISO8473_CLNP,    "CLNP" },
218         { NLPID_ISO9542_ESIS,    "ESIS" },
219         { NLPID_ISO10589_ISIS,   "ISIS" },
220         { NLPID_ISO10747_IDRP,   "IDRP" },
221         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
222         { NLPID_ISO10030,        "ISO 10030" },
223         { NLPID_ISO11577,        "ISO 11577" },
224         { NLPID_IP,              "IP" },
225         { NLPID_PPP,             "PPP" },
226         { 0,                     NULL },
227 };
228
229 void dissect_osi(const u_char *pd, int offset, frame_data *fd, 
230                  proto_tree *tree) 
231 {
232   switch (pd[offset]) {
233
234     /* ESIS (X.25) is not currently decoded */
235
236     case NLPID_ISO8473_CLNP:
237     case NLPID_NULL:    /* "Inactive Subset" of ISO 8473 CLNP */
238       dissect_clnp(pd, offset, fd, tree);
239       break;
240     case NLPID_ISO9542_ESIS:
241       dissect_esis(pd, offset, fd, tree);
242       break;
243     case NLPID_ISO9542X25_ESIS:
244       if (check_col(fd, COL_PROTOCOL)) {
245         col_add_str(fd, COL_PROTOCOL, "ESIS (X.25)");
246       }
247       dissect_data(pd, offset, fd, tree);
248       break;
249     case NLPID_ISO10589_ISIS:
250       dissect_isis(pd, offset, fd, tree);
251       break;
252     case NLPID_ISO10747_IDRP:
253       if (check_col(fd, COL_PROTOCOL)) {
254         col_add_str(fd, COL_PROTOCOL, "IDRP");
255       }
256       dissect_data(pd, offset, fd, tree);
257       break;
258     default:
259       if (check_col(fd, COL_PROTOCOL)) {
260         col_add_str(fd, COL_PROTOCOL, "ISO");
261       }
262       if (check_col(fd, COL_INFO)) {
263         col_add_fstr(fd, COL_INFO, "Unknown ISO protocol (%02x)", pd[offset]);
264       }
265       dissect_data(pd, offset, fd, tree);
266       break;
267   }
268 } /* dissect_osi */