Moved from using dissect_data to using call_dissector()
[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.47 2001/11/25 22:51:13 hagbard 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@ethereal.com>
11  * Copyright 1998 Gerald Combs
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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <glib.h>
41 #include "packet.h"
42 #include "llcsaps.h"
43 #include "aftypes.h"
44 #include "nlpid.h"
45 #include "ppptypes.h"
46 #include "packet-osi.h"
47 #include "packet-isis.h"
48 #include "packet-esis.h"
49
50
51 cksum_status_t
52 calc_checksum( tvbuff_t *tvb, int offset, u_int len, u_int checksum) {
53   const gchar *buffer;
54   guint   available_len;
55   const guint8 *p;
56   guint32 c0, c1;
57   u_int   seglen;
58   u_int   i;
59
60   if ( 0 == checksum )
61     return( NO_CKSUM );
62
63   available_len = tvb_length_remaining( tvb, offset );
64   if ( available_len < len )
65     return( DATA_MISSING );
66
67   buffer = tvb_get_ptr( tvb, offset, len );
68
69   /*
70    * The maximum values of c0 and c1 will occur if all bytes have the
71    * value 255; if so, then c0 will be len*255 and c1 will be
72    * (len*255 + (len-1)*255 + ... + 255), which is
73    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
74    * This means it can overflow if "len" is 5804 or greater.
75    *
76    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
77    * we can solve this by taking c0 and c1 mod 255 every
78    * 5803 bytes.
79    */
80   p = buffer;
81   c0 = 0;
82   c1 = 0;
83   while (len != 0) {
84     seglen = len;
85     if (seglen > 5803)
86       seglen = 5803;
87     for (i = 0; i < seglen; i++) {
88       c0 = c0 + *(p++);
89       c1 += c0;
90     }
91
92     c0 = c0 % 255;
93     c1 = c1 % 255;
94
95     len -= seglen;
96   }
97   if (c0 != 0 || c1 != 0)
98     return( CKSUM_NOT_OK );     /* XXX - what should the checksum be? */
99   else
100     return( CKSUM_OK );
101 }
102
103
104 /* main entry point */
105
106 const value_string nlpid_vals[] = {
107         { NLPID_NULL,            "NULL" },
108         { NLPID_T_70,            "T.70" },
109         { NLPID_X_633,           "X.633" },
110         { NLPID_Q_931,           "Q.931" },
111         { NLPID_Q_2931,          "Q.2931" },
112         { NLPID_Q_2119,          "Q.2119" },
113         { NLPID_SNAP,            "SNAP" },
114         { NLPID_ISO8473_CLNP,    "CLNP" },
115         { NLPID_ISO9542_ESIS,    "ESIS" },
116         { NLPID_ISO10589_ISIS,   "ISIS" },
117         { NLPID_ISO10747_IDRP,   "IDRP" },
118         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
119         { NLPID_ISO10030,        "ISO 10030" },
120         { NLPID_ISO11577,        "ISO 11577" },
121         { NLPID_COMPRESSED,      "Data compression protocol" },
122         { NLPID_IP,              "IP" },
123         { NLPID_IP6,             "IPv6" },
124         { NLPID_PPP,             "PPP" },
125         { 0,                     NULL },
126 };
127
128 dissector_table_t osinl_subdissector_table;
129 static dissector_handle_t data_handle;
130
131 static void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
132 {
133   guint8 nlpid;
134
135   pinfo->current_proto = "OSI";
136
137   nlpid = tvb_get_guint8(tvb, 0);
138
139   /* do lookup with the subdissector table */
140   if (dissector_try_port(osinl_subdissector_table, nlpid, tvb, pinfo, tree))
141       return;
142
143   switch (nlpid) {
144
145     /* ESIS (X.25) is not currently decoded */
146
147     case NLPID_ISO9542X25_ESIS:
148       if (check_col(pinfo->fd, COL_PROTOCOL)) {
149         col_set_str(pinfo->fd, COL_PROTOCOL, "ESIS (X.25)");
150       }
151       call_dissector(data_handle,tvb, pinfo, tree);
152       break;
153     case NLPID_ISO10747_IDRP:
154       if (check_col(pinfo->fd, COL_PROTOCOL)) {
155         col_set_str(pinfo->fd, COL_PROTOCOL, "IDRP");
156       }
157       call_dissector(data_handle,tvb, pinfo, tree);
158       break;
159     default:
160       if (check_col(pinfo->fd, COL_PROTOCOL)) {
161         col_set_str(pinfo->fd, COL_PROTOCOL, "ISO");
162       }
163       if (check_col(pinfo->fd, COL_INFO)) {
164         col_add_fstr(pinfo->fd, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
165       }
166       call_dissector(data_handle,tvb, pinfo, tree);
167       break;
168   }
169 } /* dissect_osi */
170
171 void
172 proto_register_osi(void)
173 {
174         /* There's no "OSI" protocol *per se*, but we do register a
175            dissector table so various protocols running at the
176            network layer can register themselves. */
177         osinl_subdissector_table = register_dissector_table("osinl");
178 }
179
180 void
181 proto_reg_handoff_osi(void)
182 {
183         dissector_add("llc.dsap", SAP_OSINL, dissect_osi, -1);
184         dissector_add("ppp.protocol", PPP_OSI, dissect_osi, -1);
185         dissector_add("null.type", BSD_AF_ISO, dissect_osi, -1);
186         data_handle = find_dissector("data");
187 }