Include <time.h> to declare "gmtime()".
[obnox/wireshark/wip.git] / packet-v120.c
1 /* packet-v120.c
2  * Routines for v120 frame disassembly
3  * Bert Driehuis <driehuis@playbeing.org>
4  *
5  * $Id: packet-v120.c,v 1.3 1999/12/14 06:21:19 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998
10  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <stdio.h>
36 #include <glib.h>
37 #include <string.h>
38 #include "packet.h"
39 #include "xdlc.h"
40
41 #define FROM_DCE        0x80
42
43 static int proto_v120 = -1;
44 static int hf_v120_address = -1;
45 static int hf_v120_control = -1;
46 static int hf_v120_header = -1;
47
48 static gint ett_v120 = -1;
49 static gint ett_v120_address = -1;
50 static gint ett_v120_control = -1;
51 static gint ett_v120_header = -1;
52
53 void dissect_v120_header(const u_char *pd, int offset, frame_data *fd, proto_tree *tree);
54
55 void
56 dissect_v120(const u_char *pd, frame_data *fd, proto_tree *tree)
57 {
58     proto_tree *v120_tree, *ti, *tc, *address_tree;
59     int is_response;
60     int addr;
61     char info[80];
62     int v120len;
63
64     if (check_col(fd, COL_PROTOCOL))
65         col_add_str(fd, COL_PROTOCOL, "V.120");
66
67     if(check_col(fd, COL_RES_DL_SRC))
68         col_add_fstr(fd, COL_RES_DL_SRC, "0x%02X", pd[0]);
69     if ((pd[0] & 0x01) != 0x00 && (pd[1] && 0x01) != 0x01)
70     {
71         if (check_col(fd, COL_INFO))
72             col_add_str(fd, COL_INFO, "Invalid V.120 frame");
73         if (tree)
74             ti = proto_tree_add_item_format(tree, proto_v120, 0, fd->cap_len,
75                                             NULL, "Invalid V.120 frame");
76         return;
77     }
78
79     if (fd->pseudo_header.x25.flags & FROM_DCE) {
80         if(check_col(fd, COL_RES_DL_DST))
81             col_add_str(fd, COL_RES_DL_DST, "DTE");
82         if(check_col(fd, COL_RES_DL_SRC))
83             col_add_str(fd, COL_RES_DL_SRC, "DCE");
84     }
85     else {
86         if(check_col(fd, COL_RES_DL_DST))
87             col_add_str(fd, COL_RES_DL_DST, "DCE");
88         if(check_col(fd, COL_RES_DL_SRC))
89             col_add_str(fd, COL_RES_DL_SRC, "DTE");
90     }
91
92     if (((fd->pseudo_header.x25.flags & FROM_DCE) && pd[0] & 0x02) ||
93        (!(fd->pseudo_header.x25.flags & FROM_DCE) && !(pd[0] & 0x02)))
94         is_response = TRUE;
95     else
96         is_response = FALSE;
97
98     if (fd->pkt_len <= 5)
99         v120len = fd->pkt_len;
100     else
101         v120len = 5;
102     if (tree) {
103         ti = proto_tree_add_item_format(tree, proto_v120, 0, v120len, NULL,
104                                             "V.120");
105         v120_tree = proto_item_add_subtree(ti, ett_v120);
106         addr = pd[1] << 8 | pd[0];
107         sprintf(info, "LLI: %d C/R: %s",
108                         ((pd[0] & 0xfc) << 5) | ((pd[1] & 0xfe) >> 1),
109                         pd[0] & 0x02 ? "R" : "C");
110         tc = proto_tree_add_item_format(v120_tree, ett_v120_address,
111                         0, 2,
112                         "Address field: %s", info);
113         address_tree = proto_item_add_subtree(tc, ett_v120_address);
114         proto_tree_add_text(address_tree, 0, 2,
115                     decode_boolean_bitfield(addr, 0x0002, 2*8,
116                         "Response", "Command"), NULL);
117         sprintf(info, "LLI: %d", ((pd[0] & 0xfc) << 5) | ((pd[1] & 0xfe) >> 1));
118         proto_tree_add_text(address_tree, 0, 2,
119                     decode_numeric_bitfield(addr, 0xfefc, 2*8, info));
120         proto_tree_add_text(address_tree, 0, 2,
121                     decode_boolean_bitfield(addr, 0x0001, 2*8,
122                         "EA0 = 1 (Error)", "EA0 = 0"), NULL);
123         proto_tree_add_text(address_tree, 0, 2,
124                     decode_boolean_bitfield(addr, 0x0100, 2*8,
125                         "EA1 = 1", "EA1 = 0 (Error)"), NULL);
126         if (v120len == 5)
127                 dissect_v120_header(pd, 4, fd, v120_tree);
128     }
129     else
130         v120_tree = NULL;
131     dissect_xdlc_control(pd, 2, fd, v120_tree, hf_v120_control,
132             ett_v120_control, is_response, v120len == 3 ? FALSE : TRUE);
133 }
134
135 void
136 dissect_v120_header(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
137 {
138         char info[80];
139         int header_len, nbits;
140         int header;
141         proto_tree *h_tree, *tc;
142
143         if (pd[offset] & 0x80) {
144                 header_len = 1;
145                 header = pd[offset];
146         } else {
147                 header_len = 2;
148                 header = pd[offset] | pd[offset + 1] << 8;
149         }
150         nbits = header_len * 8;
151         sprintf(info, "Header: B: %d F: %d", pd[offset] & 0x02 ? 1:0,
152                         pd[offset] & 0x01 ? 1:0);
153         tc = proto_tree_add_item_format(tree, ett_v120_header,
154                         offset, header_len,
155                         "Header octet: %s (0x%02X)", info, pd[offset]);
156         h_tree = proto_item_add_subtree(tc, ett_v120_header);
157         proto_tree_add_text(h_tree, offset, header_len,
158                     decode_boolean_bitfield(header, 0x80, nbits,
159                         "No extension octet", "Extension octet follows"), NULL);
160         proto_tree_add_text(h_tree, offset, header_len,
161                     decode_boolean_bitfield(header, 0x40, nbits,
162                         "Break condition", "No break condition"), NULL);
163         sprintf(info, "Error control C1/C2: %d", (header & 0x0c) >> 2);
164         proto_tree_add_text(h_tree, offset, header_len,
165                     decode_numeric_bitfield(header, 0x0c, nbits, info));
166         proto_tree_add_text(h_tree, offset, header_len,
167                     decode_boolean_bitfield(header, 0x02, nbits,
168                         "Segmentation bit B", "No segmentation bit B"), NULL);
169         proto_tree_add_text(h_tree, offset, header_len,
170                     decode_boolean_bitfield(header, 0x01, nbits,
171                         "Segmentation bit F", "No segmentation bit F"), NULL);
172         if (header_len == 2) {
173                 proto_tree_add_text(h_tree, offset, header_len,
174                     decode_boolean_bitfield(header, 0x8000, nbits,
175                         "E", "E bit not set (Error)"), NULL);
176                 proto_tree_add_text(h_tree, offset, header_len,
177                     decode_boolean_bitfield(header, 0x4000, nbits,
178                         "DR", "No DR"), NULL);
179                 proto_tree_add_text(h_tree, offset, header_len,
180                     decode_boolean_bitfield(header, 0x2000, nbits,
181                         "SR", "No SR"), NULL);
182                 proto_tree_add_text(h_tree, offset, header_len,
183                     decode_boolean_bitfield(header, 0x1000, nbits,
184                         "RR", "No RR"), NULL);
185         }
186 }
187 void
188 proto_register_v120(void)
189 {
190     static hf_register_info hf[] = {
191         { &hf_v120_address,
192           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
193                   0x0, "" }},
194         { &hf_v120_control,
195           { "Control Field", "v120.control", FT_STRING, BASE_NONE, NULL, 0x0,
196                 "" }},
197         { &hf_v120_header,
198           { "Header Field", "v120.header", FT_STRING, BASE_NONE, NULL, 0x0,
199                 "" }},
200     };
201     static gint *ett[] = {
202         &ett_v120,
203         &ett_v120_address,
204         &ett_v120_control,
205         &ett_v120_header,
206     };
207
208     proto_v120 = proto_register_protocol ("Async data over ISDN (V.120)", "v120");
209     proto_register_field_array (proto_v120, hf, array_length(hf));
210     proto_register_subtree_array(ett, array_length(ett));
211 }