2 * Routines for ISO/OSI network and transport protocol packet disassembly
3 * Main entrance point and common functions
5 * Laurent Deniel <laurent.deniel@free.fr>
6 * Ralf Schneider <Ralf.Schneider@t-online.de>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
22 #include "osi-utils.h"
24 #include "address_types.h"
26 static void print_nsap_net_buf( const guint8 *, int, gchar *, int);
27 static void print_area_buf ( const guint8 *, int, gchar *, int);
28 static void print_address_prefix_buf ( const guint8 *, int, gchar *, int);
31 * XXX - shouldn't there be a centralized routine for dissecting NSAPs?
32 * See also "dissect_atm_nsap()" in epan/dissectors/packet-arp.c and
33 * "dissect_nsap()" in epan/dissectors/packet-isup.c.
36 print_nsap_net( tvbuff_t *tvb, const gint offset, int length )
40 cur = (gchar *)wmem_alloc(wmem_packet_scope(), MAX_NSAP_LEN * 3 + 50);
41 print_nsap_net_buf( tvb_get_ptr(tvb, offset, length), length, cur, MAX_NSAP_LEN * 3 + 50);
46 print_nsap_net_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
50 /* to do : NSAP / NET decoding */
52 if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
53 g_strlcpy(buf, "<Invalid length of NSAP>", buf_len);
57 if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
58 print_area_buf(ad, RFC1237_FULLAREA_LEN, cur, buf_len);
60 print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur, (int) (buf_len-(cur-buf)));
62 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x]",
63 ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
64 if ( length == RFC1237_NSAP_LEN + 1 ) {
65 g_snprintf(cur, (int) (buf_len-(cur-buf)), "-%02x", ad[ length -1 ] );
68 else { /* probably format as standard */
69 print_area_buf( ad, length, buf, buf_len);
74 print_system_id(wmem_allocator_t* scope, const guint8 *ad, int length )
78 cur = (gchar *)wmem_alloc(scope, MAX_SYSTEMID_LEN * 3 + 5);
79 print_system_id_buf(ad, length, cur, MAX_SYSTEMID_LEN * 3 + 5);
84 tvb_print_system_id( tvbuff_t *tvb, const gint offset, int length )
86 return( print_system_id(wmem_packet_scope(), tvb_get_ptr(tvb, offset, length), length) );
90 print_system_id_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
95 if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
96 g_strlcpy(buf, "<Invalid length of SYSTEM ID>", buf_len);
101 if ( ( 6 == length ) || /* System-ID */
102 ( 7 == length ) || /* LAN-ID */
103 ( 8 == length )) { /* LSP-ID */
104 cur += g_snprintf(cur, buf_len, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
105 ad[2], ad[3], ad[4], ad[5] );
106 if ( ( 7 == length ) ||
108 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[6] );
111 g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[7] );
116 while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
117 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
118 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
119 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
120 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
122 if ( 1 == tmp ) { /* Special case for Designated IS */
124 g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[tmp] );
127 for ( ; tmp < length; ) { /* print the rest without dot */
128 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
135 print_area(tvbuff_t *tvb, const gint offset, int length)
139 cur = (gchar *)wmem_alloc(wmem_packet_scope(), MAX_AREA_LEN * 3 + 20);
140 print_area_buf(tvb_get_ptr(tvb, offset, length), length, cur, MAX_AREA_LEN * 3 + 20);
145 * Note: length is in units of half-octets.
148 print_address_prefix(tvbuff_t *tvb, const gint offset, int length)
152 cur = (gchar *)wmem_alloc(wmem_packet_scope(), MAX_AREA_LEN * 3 + 20);
153 print_address_prefix_buf(tvb_get_ptr(tvb, offset, (length+1)/2), length, cur, MAX_AREA_LEN * 3 + 20);
158 * Note: length is in units of octets.
161 print_area_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
163 print_address_prefix_buf(ad, length*2, buf, buf_len);
167 * Note: length is in units of half-octets.
170 print_address_prefix_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
175 /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
177 if (length <= 0 || length > MAX_AREA_LEN*2) {
178 g_strlcpy(buf, "<Invalid length of AREA>", buf_len);
183 if ( ( ( NSAP_IDI_ISO_DCC_BIN == *ad )
184 || ( NSAP_IDI_ISO_6523_ICD_BIN == *ad )
187 ( ( RFC1237_FULLAREA_LEN*2 == length )
188 || ( (RFC1237_FULLAREA_LEN + 1)*2 == length )
190 ) { /* AFI is good and length is long enough */
192 /* there used to be a check for (length > RFC1237_FULLAREA_LEN + 1) here,
193 * in order to report an invalied length of AREA for DCC / ISO 6523 AFI,
194 * but that can *never* be the case because the if() test above explicitly
195 * tests for (length == RFC1237_FULLAREA_LEN) or (length == RFC1237_FULLAREA_LEN + 1)
198 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
199 ad[0], ad[1], ad[2], ad[3], ad[4],
200 ad[5], ad[6], ad[7], ad[8] );
201 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x:%02x|%02x:%02x]",
202 ad[9], ad[10], ad[11], ad[12] );
203 if ( (RFC1237_FULLAREA_LEN + 1)*2 == length )
204 g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-[%02x]", ad[20] );
206 else { /* print standard format */
207 if ( length == RFC1237_AREA_LEN*2 ) {
208 g_snprintf(buf, buf_len, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
211 if ( length == 4*2 ) {
212 g_snprintf(buf, buf_len, "%02x%02x%02x%02x", ad[0], ad[1], ad[2], ad[3] );
215 while ( tmp < length / 8 ) { /* 32/8==4 > four Octets left to print */
216 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
217 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
218 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
219 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
221 if ( 2 == tmp ) { /* Special case for Designated IS */
223 g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[tmp] );
226 for ( ; tmp < length / 2; ) { /* print the rest without dot or dash */
227 cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
229 /* Odd half-octet? */
231 /* Yes - print it (it's the upper half-octet) */
232 g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%x", (ad[tmp] & 0xF0)>>4 );
236 } /* print_address_prefix_buf */
238 /******************************************************************************
240 ******************************************************************************/
241 static int osi_address_type = -1;
243 static int osi_address_to_str(const address* addr, gchar *buf, int buf_len)
245 print_nsap_net_buf((const guint8 *)addr->data, addr->len, buf, buf_len);
246 return (int)strlen(buf)+1;
249 static int osi_address_str_len(const address* addr _U_)
251 return MAX_NSAP_LEN * 3 + 50;
254 int get_osi_address_type(void)
256 return osi_address_type;
259 void register_osi_address_type(void)
261 if (osi_address_type != -1)
264 osi_address_type = address_type_dissector_register("AT_OSI", "OSI Address", osi_address_to_str, osi_address_str_len, NULL, NULL, NULL, NULL, NULL);
274 * indent-tabs-mode: nil
277 * ex: set shiftwidth=2 tabstop=8 expandtab:
278 * :indentSize=2:tabSize=8:noTabs=true: