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