Logcat: Set data-text-lines dissectors for log
[metze/wireshark/wip.git] / epan / osi-utils.c
1 /* osi-utils.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly
3  * Main entrance point and common functions
4  *
5  * Laurent Deniel <laurent.deniel@free.fr>
6  * Ralf Schneider <Ralf.Schneider@t-online.de>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib.h>
32
33 #include "tvbuff.h"
34 #include "osi-utils.h"
35 #include "emem.h"
36
37 /*
38  * XXX - shouldn't there be a centralized routine for dissecting NSAPs?
39  * See also "dissect_atm_nsap()" in epan/dissectors/packet-arp.c and
40  * "dissect_nsap()" in epan/dissectors/packet-isup.c.
41  */
42 gchar *
43 print_nsap_net( const guint8 *ad, int length )
44 {
45   gchar *cur;
46
47   cur = (gchar *)ep_alloc(MAX_NSAP_LEN * 3 + 50);
48   print_nsap_net_buf( ad, length, cur, MAX_NSAP_LEN * 3 + 50);
49   return( cur );
50 }
51
52 /* XXX - Should these be converted to string buffers? */
53 void
54 print_nsap_net_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
55 {
56   gchar *cur;
57
58   /* to do : NSAP / NET decoding */
59
60   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
61     g_snprintf(buf, buf_len, "<Invalid length of NSAP>");
62     return;
63   }
64   cur = buf;
65   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
66     print_area_buf(ad, RFC1237_FULLAREA_LEN, cur, buf_len);
67     cur += strlen( cur );
68     print_system_id_buf( ad + RFC1237_FULLAREA_LEN, RFC1237_SYSTEMID_LEN, cur, (int) (buf_len-(cur-buf)));
69     cur += strlen( cur );
70     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x]",
71                     ad[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
72     if ( length == RFC1237_NSAP_LEN + 1 ) {
73       g_snprintf(cur, (int) (buf_len-(cur-buf)), "-%02x", ad[ length -1 ] );
74     }
75   }
76   else {    /* probably format as standard */
77     print_area_buf( ad, length, buf, buf_len);
78   }
79 } /* print_nsap */
80
81 gchar *
82 print_system_id( const guint8 *ad, int length )
83 {
84   gchar        *cur;
85
86   cur = (gchar *)ep_alloc(MAX_SYSTEMID_LEN * 3 + 5);
87   print_system_id_buf(ad, length, cur, MAX_SYSTEMID_LEN * 3 + 5);
88   return( cur );
89 }
90
91 gchar *
92 tvb_print_system_id( tvbuff_t *tvb, const gint offset, int length )
93 {
94   return( print_system_id(tvb_get_ptr(tvb, offset, length), length) );
95 }
96
97 void
98 print_system_id_buf( const guint8 *ad, int length, gchar *buf, int buf_len)
99 {
100   gchar        *cur;
101   int           tmp;
102
103   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
104     g_snprintf(buf, buf_len, "<Invalid length of SYSTEM ID>");
105     return;
106   }
107
108   cur = buf;
109   if ( ( 6 == length ) || /* System-ID */
110        ( 7 == length ) || /* LAN-ID */
111        ( 8 == length )) { /* LSP-ID */
112     cur += g_snprintf(cur, buf_len, "%02x%02x.%02x%02x.%02x%02x", ad[0], ad[1],
113                     ad[2], ad[3], ad[4], ad[5] );
114     if ( ( 7 == length ) ||
115          ( 8 == length )) {
116         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[6] );
117     }
118     if ( 8 == length ) {
119         g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[7] );
120     }
121   }
122   else {
123     tmp = 0;
124     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
125       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
126       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
127       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
128       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
129     }
130     if ( 1 == tmp ) {   /* Special case for Designated IS */
131       cur--;
132       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), ".%02x", ad[tmp] );
133     }
134     else {
135       for ( ; tmp < length; ) {  /* print the rest without dot */
136         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
137       }
138     }
139   }
140 }
141
142 gchar *
143 print_area(const guint8 *ad, int length)
144 {
145   gchar *cur;
146
147   cur = (gchar *)ep_alloc(MAX_AREA_LEN * 3 + 20);
148   print_area_buf(ad, length, cur, MAX_AREA_LEN * 3 + 20);
149   return cur;
150 }
151
152 void
153 print_area_buf(const guint8 *ad, int length, gchar *buf, int buf_len)
154 {
155   gchar *cur;
156   int  tmp  = 0;
157
158   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
159    * and take away all these stupid resource consuming local statics
160    */
161   if (length <= 0 || length > MAX_AREA_LEN) {
162     g_snprintf(buf, buf_len, "<Invalid length of AREA>");
163     return;
164   }
165
166   cur = buf;
167   if ( (  ( NSAP_IDI_ISODCC          == *ad )
168        || ( NSAP_IDI_GOSIP2          == *ad )
169        )
170        &&
171        (  ( RFC1237_FULLAREA_LEN     ==  length )
172        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
173        )
174      ) {    /* AFI is good and length is long enough  */
175
176     /* there used to be a check for (length > RFC1237_FULLAREA_LEN + 1) here,
177      * in order to report an invalied length of AREA for DCC / GOSIP AFI,
178      * but that can *never* be the case because the if() test above explicitly
179      * tests for (length == RFC1237_FULLAREA_LEN) or (length == RFC1237_FULLAREA_LEN + 1)
180      */
181
182     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]",
183                     ad[0], ad[1], ad[2], ad[3], ad[4],
184                     ad[5], ad[6], ad[7], ad[8] );
185     cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "[%02x:%02x|%02x:%02x]",
186                     ad[9], ad[10],  ad[11], ad[12] );
187     if ( RFC1237_FULLAREA_LEN + 1 == length )
188       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-[%02x]", ad[20] );
189   }
190   else { /* print standard format */
191     if ( length == RFC1237_AREA_LEN ) {
192       g_snprintf(buf, buf_len, "%02x.%02x%02x", ad[0], ad[1], ad[2] );
193       return;
194     }
195     if ( length == 4 ) {
196       g_snprintf(buf, buf_len, "%02x%02x%02x%02x", ad[0], ad[1], ad[2], ad[3] );
197       return;
198     }
199     while ( tmp < length / 4 ) {      /* 16/4==4 > four Octets left to print */
200       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
201       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
202       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
203       cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x.", ad[tmp++] );
204     }
205     if ( 1 == tmp ) {                     /* Special case for Designated IS */
206       cur--;
207       g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "-%02x", ad[tmp] );
208     }
209     else {
210       for ( ; tmp < length; ) {  /* print the rest without dot or dash */
211         cur += g_snprintf(cur, (gulong) (buf_len-(cur-buf)), "%02x", ad[tmp++] );
212       }
213     }
214   }
215 } /* print_area_buf */
216