Added Bert Driehuis <driehuis@playbeing.org>'s I4B wiretap module
[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.1 1999/12/12 22:39:29 gram 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
47 static gint ett_v120 = -1;
48 static gint ett_v120_address = -1;
49 static gint ett_v120_control = -1;
50
51 void
52 dissect_v120(const u_char *pd, frame_data *fd, proto_tree *tree)
53 {
54     proto_tree *v120_tree, *ti, *tc, *address_tree;
55     int is_response;
56     int addr;
57     char info[80];
58     int v120len;
59
60     if (check_col(fd, COL_PROTOCOL))
61         col_add_str(fd, COL_PROTOCOL, "V.120");
62
63     if(check_col(fd, COL_RES_DL_SRC))
64         col_add_fstr(fd, COL_RES_DL_SRC, "0x%02X", pd[0]);
65     if ((pd[0] & 0x01) != 0x00 && (pd[1] && 0x01) != 0x01)
66     {
67         if (check_col(fd, COL_INFO))
68             col_add_str(fd, COL_INFO, "Invalid V.120 frame");
69         if (tree)
70             ti = proto_tree_add_item_format(tree, proto_v120, 0, fd->cap_len,
71                                             NULL, "Invalid V.120 frame");
72         return;
73     }
74
75     if (fd->pseudo_header.x25.flags & FROM_DCE) {
76         if(check_col(fd, COL_RES_DL_DST))
77             col_add_str(fd, COL_RES_DL_DST, "DTE");
78         if(check_col(fd, COL_RES_DL_SRC))
79             col_add_str(fd, COL_RES_DL_SRC, "DCE");
80     }
81     else {
82         if(check_col(fd, COL_RES_DL_DST))
83             col_add_str(fd, COL_RES_DL_DST, "DCE");
84         if(check_col(fd, COL_RES_DL_SRC))
85             col_add_str(fd, COL_RES_DL_SRC, "DTE");
86     }
87
88     if (((fd->pseudo_header.x25.flags & FROM_DCE) && pd[0] & 0x02) ||
89        (!(fd->pseudo_header.x25.flags & FROM_DCE) && !(pd[0] & 0x02)))
90         is_response = TRUE;
91     else
92         is_response = FALSE;
93
94     if (tree) {
95         if (fd->pkt_len <= 5)
96                 v120len = fd->pkt_len;
97         else
98                 v120len = 5;
99         ti = proto_tree_add_item_format(tree, proto_v120, 0, v120len, NULL,
100                                             "V.120");
101         v120_tree = proto_item_add_subtree(ti, ett_v120);
102         addr = pd[0] << 8 | pd[1];
103         sprintf(info, "LLI: %d C/R: %s",
104                         ((pd[0] & 0xfc) << 5) | ((pd[1] & 0xfe) >> 1),
105                         pd[0] & 0x02 ? "R" : "C");
106         tc = proto_tree_add_item_format(v120_tree, ett_v120_address,
107                         0, 2,
108                         "Address field: %s (0x%02X)", info, addr);
109         address_tree = proto_item_add_subtree(tc, ett_v120_address);
110         proto_tree_add_text(address_tree, 0, 2,
111                     decode_boolean_bitfield(addr, 0x0200, 2*8,
112                         "Response", "Command"), NULL);
113         sprintf(info, "LLI: %d", ((pd[0] & 0xfc) << 5) | ((pd[1] & 0xfe) >> 1));
114         proto_tree_add_text(address_tree, 0, 2,
115                     decode_numeric_bitfield(addr, 0xfcfe, 2*8, info));
116         proto_tree_add_text(address_tree, 0, 2,
117                     decode_boolean_bitfield(addr, 0x0100, 2*8,
118                         "EA0 = 1 (Error)", "EA0 = 0"), NULL);
119         proto_tree_add_text(address_tree, 0, 2,
120                     decode_boolean_bitfield(addr, 0x01, 2*8,
121                         "EA1 = 1", "EA1 = 0 (Error)"), NULL);
122         /* TODO: parse octets 4 & 5. Not that they're used in
123            practice, but it looks so professional. */
124     }
125     else
126         v120_tree = NULL;
127     dissect_xdlc_control(pd, 2, fd, v120_tree, hf_v120_control,
128             ett_v120_control, is_response, v120len == 3 ? FALSE : TRUE);
129
130     /* not end of frame ==> X.25 */
131 }
132
133 void
134 proto_register_v120(void)
135 {
136     static hf_register_info hf[] = {
137         { &hf_v120_address,
138           { "Link Address", "v120.address", FT_UINT16, BASE_HEX, NULL,
139                   0x0, "" }},
140         { &hf_v120_control,
141           { "Control Field", "v120.control", FT_STRING, BASE_NONE, NULL, 0x0,
142                 "" }},
143     };
144     static gint *ett[] = {
145         &ett_v120,
146         &ett_v120_address,
147         &ett_v120_control,
148     };
149
150     proto_v120 = proto_register_protocol ("Async data over ISDN (V.120)", "v120");
151     proto_register_field_array (proto_v120, hf, array_length(hf));
152     proto_register_subtree_array(ett, array_length(ett));
153 }