attr_dissector_fn_t
[wireshark-sm.git] / epan / osi-utils.c
blob7e551e04114e4acece3b788d502d273af0ca2707
1 /* osi-utils.c
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
15 #include "config.h"
17 #include <stdio.h>
18 #include <string.h>
19 #include <glib.h>
21 #include "tvbuff.h"
22 #include "osi-utils.h"
23 #include "address.h"
24 #include "address_types.h"
26 static void print_nsap_net_buf( const uint8_t *, int, char *, int);
27 static void print_area_buf ( const uint8_t *, int, char *, int);
28 static void print_address_prefix_buf ( const uint8_t *, int, char *, 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.
35 char *
36 print_nsap_net( wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int length )
38 char *cur;
40 cur = (char *)wmem_alloc(scope, MAX_NSAP_LEN * 3 + 50);
41 print_nsap_net_buf( tvb_get_ptr(tvb, offset, length), length, cur, MAX_NSAP_LEN * 3 + 50);
42 return( cur );
45 static void
46 print_nsap_net_buf( const uint8_t *ad, int length, char *buf, int buf_len)
48 char *cur;
50 /* to do : NSAP / NET decoding */
52 if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
53 (void) g_strlcpy(buf, "<Invalid length of NSAP>", buf_len);
54 return;
56 cur = buf;
57 if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
58 print_area_buf(ad, RFC1237_FULLAREA_LEN, cur, buf_len);
59 cur += strlen( cur );
60 print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur, (int) (buf_len-(cur-buf)));
61 cur += strlen( cur );
62 cur += snprintf(cur, buf_len-(cur-buf), "[%02x]",
63 ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
64 if ( length == RFC1237_NSAP_LEN + 1 ) {
65 snprintf(cur, (int) (buf_len-(cur-buf)), "-%02x", ad[ length -1 ] );
68 else { /* probably format as standard */
69 /* XXX - this is an NSAP, not an area address/address prefix */
70 print_area_buf( ad, length, buf, buf_len);
72 } /* print_nsap */
74 char *
75 print_system_id(wmem_allocator_t* scope, const uint8_t *ad, int length )
77 char *cur;
79 cur = (char *)wmem_alloc(scope, MAX_SYSTEMID_LEN * 3 + 5);
80 print_system_id_buf(ad, length, cur, MAX_SYSTEMID_LEN * 3 + 5);
81 return( cur );
84 char *
85 tvb_print_system_id( wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int length )
87 return( print_system_id(scope, tvb_get_ptr(tvb, offset, length), length) );
90 void
91 print_system_id_buf( const uint8_t *ad, int length, char *buf, int buf_len)
93 char *cur;
94 int tmp;
96 if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
97 (void) g_strlcpy(buf, "<Invalid length of SYSTEM ID>", buf_len);
98 return;
101 cur = buf;
102 if ( ( 6 == length ) || /* System-ID */
103 ( 7 == length ) || /* LAN-ID */
104 ( 8 == length )) { /* LSP-ID */
105 cur += snprintf(cur, buf_len, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
106 ad[2], ad[3], ad[4], ad[5] );
107 if ( ( 7 == length ) ||
108 ( 8 == length )) {
109 cur += snprintf(cur, buf_len-(cur-buf), ".%02x", ad[6] );
111 if ( 8 == length ) {
112 snprintf(cur, buf_len-(cur-buf), "-%02x", ad[7] );
115 else {
116 tmp = 0;
117 while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
118 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
119 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
120 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
121 cur += snprintf(cur, buf_len-(cur-buf), "%02x.", ad[tmp++] );
123 if ( 1 == tmp ) { /* Special case for Designated IS */
124 cur--;
125 snprintf(cur, buf_len-(cur-buf), ".%02x", ad[tmp] );
127 else {
128 for ( ; tmp < length; ) { /* print the rest without dot */
129 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
135 char *
136 print_area(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int length)
138 char *cur;
140 cur = (char *)wmem_alloc(scope, MAX_AREA_LEN * 3 + 20);
141 print_area_buf(tvb_get_ptr(tvb, offset, length), length, cur, MAX_AREA_LEN * 3 + 20);
142 return cur;
146 * Note: length is in units of half-octets.
148 char *
149 print_address_prefix(wmem_allocator_t *scope, tvbuff_t *tvb, const int offset, int length)
151 char *cur;
153 cur = (char *)wmem_alloc(scope, MAX_AREA_LEN * 3 + 20);
154 print_address_prefix_buf(tvb_get_ptr(tvb, offset, (length+1)/2), length, cur, MAX_AREA_LEN * 3 + 20);
155 return cur;
159 * Note: length is in units of octets.
161 static void
162 print_area_buf(const uint8_t *ad, int length, char *buf, int buf_len)
164 print_address_prefix_buf(ad, length*2, buf, buf_len);
168 * Note: length is in units of half-octets.
170 static void
171 print_address_prefix_buf(const uint8_t *ad, int length, char *buf, int buf_len)
173 char *cur;
174 int tmp = 0;
176 /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
178 if (length <= 0 || length > MAX_AREA_LEN*2) {
179 (void) g_strlcpy(buf, "<Invalid length of AREA>", buf_len);
180 return;
183 cur = buf;
184 /* Check the AFI and length. */
185 if ( ( ( NSAP_IDI_ISO_DCC_BIN == *ad )
186 || ( NSAP_IDI_ISO_6523_ICD_BIN == *ad )
189 ( ( RFC1237_FULLAREA_LEN*2 == length )
190 || ( (RFC1237_FULLAREA_LEN + 1)*2 == length )
192 ) { /* AFI is good and length is long enough */
194 /* The AFI is either ISO DCC, binary or ISO 6523-ICD, binary,
195 * and the area length corresponds either to the area length
196 * for RFC 1237 (GOSIP) addresses or that length + 1.
198 * XXX - RFC 1237 doesn't mention ISO DCC, binary, as a valid
199 * AFI; is that from GOSIP Version 1? If it's ISO DCC, binary,
200 * the IDI is 3 digits, i.e. 1 1/2 octets.
202 /* there used to be a check for (length > RFC1237_FULLAREA_LEN + 1) here,
203 * in order to report an invalied length of AREA for DCC / ISO 6523 AFI,
204 * but that can *never* be the case because the if() test above explicitly
205 * tests for (length == RFC1237_FULLAREA_LEN) or (length == RFC1237_FULLAREA_LEN + 1)
208 /* Show the one-octet AFI, the two-octet IDI, the one-octet DFI, the
209 * 3-octet AA, and the 2 reserved octets.
211 cur += snprintf(cur, buf_len-(cur-buf), "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
212 ad[0], ad[1], ad[2], ad[3], ad[4],
213 ad[5], ad[6], ad[7], ad[8] );
214 /* Show the 2-octet RD and the 2-octet Area. */
215 cur += snprintf(cur, buf_len-(cur-buf), "[%02x:%02x|%02x:%02x]",
216 ad[9], ad[10], ad[11], ad[12] );
217 /* Show whatever the heck this is; it's not specified by RFC 1237,
218 * but we also handle 14-octet areas. Is it the "Designated IS"
219 * stuff mentioned below? (I didn't find anything in the IS-IS
220 * spec about that.)
222 if ( (RFC1237_FULLAREA_LEN + 1)*2 == length )
223 snprintf(cur, buf_len-(cur-buf), "-[%02x]", ad[13] );
225 else {
226 /* This doesn't look like a full RFC 1237 IS-IS area, so all we know
227 * is that the first octet is an AFI. Print it separately from all
228 * the other octets.
230 if ( length == RFC1237_AREA_LEN*2 ) {
231 /* XXX - RFC1237_AREA_LEN, which is 3 octets, doesn't seem to
232 * correspond to anything in RFC 1237. Where did it come from?
234 snprintf(buf, buf_len, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
235 return;
237 if ( length == 4*2 ) {
238 snprintf(buf, buf_len, "%02x%02x%02x%02x", ad[0], ad[1], ad[2], ad[3] );
239 return;
241 while ( tmp < length / 8 ) { /* 32/8==4 > four Octets left to print */
242 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
243 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
244 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
245 cur += snprintf(cur, buf_len-(cur-buf), "%02x.", ad[tmp++] );
247 if ( 2 == tmp ) { /* Special case for Designated IS */
248 cur--;
249 snprintf(cur, buf_len-(cur-buf), "-%02x", ad[tmp] );
251 else {
252 for ( ; tmp < length / 2; ) { /* print the rest without dot or dash */
253 cur += snprintf(cur, buf_len-(cur-buf), "%02x", ad[tmp++] );
255 /* Odd half-octet? */
256 if (length & 1) {
257 /* Yes - print it (it's the upper half-octet) */
258 snprintf(cur, buf_len-(cur-buf), "%x", (ad[tmp] & 0xF0)>>4 );
262 } /* print_address_prefix_buf */
264 /******************************************************************************
265 * OSI Address Type
266 ******************************************************************************/
267 static int osi_address_type = -1;
269 static int osi_address_to_str(const address* addr, char *buf, int buf_len)
271 print_nsap_net_buf((const uint8_t *)addr->data, addr->len, buf, buf_len);
272 return (int)strlen(buf)+1;
275 static int osi_address_str_len(const address* addr _U_)
277 return MAX_NSAP_LEN * 3 + 50;
280 int get_osi_address_type(void)
282 return osi_address_type;
285 void register_osi_address_type(void)
287 if (osi_address_type != -1)
288 return;
290 osi_address_type = address_type_dissector_register("AT_OSI", "OSI Address", osi_address_to_str, osi_address_str_len, NULL, NULL, NULL, NULL, NULL);
295 * Editor modelines
297 * Local Variables:
298 * c-basic-offset: 2
299 * tab-width: 8
300 * indent-tabs-mode: nil
301 * End:
303 * ex: set shiftwidth=2 tabstop=8 expandtab:
304 * :indentSize=2:tabSize=8:noTabs=true: