The function pointer in a "per_choice_t" or a "per_sequence_t" is to a
[obnox/wireshark/wip.git] / packet-laplink.c
1 /* packet-laplink.c
2  * Routines for laplink dissection
3  * Copyright 2003, Brad Hards <bradh@frogmouth.net>
4  *
5  * $Id: packet-laplink.c,v 1.4 2003/10/20 23:05:41 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <glib.h>
35
36 #ifdef NEED_SNPRINTF_H
37 # include "snprintf.h"
38 #endif
39
40 #include <epan/packet.h>
41 #include <epan/strutil.h>
42 #include <epan/conversation.h>
43
44 #include "packet-tcp.h"
45 #include "prefs.h"
46
47 #define TCP_PORT_LAPLINK 1547
48 #define UDP_PORT_LAPLINK 1547
49
50 /* Initialize the protocol and registered fields */
51 static int proto_laplink = -1;
52 static int hf_laplink_udp_ident = -1;
53 static int hf_laplink_udp_name = -1;
54 static int hf_laplink_tcp_ident = -1;
55 static int hf_laplink_tcp_length = -1;
56 static int hf_laplink_tcp_data = -1;
57
58 /* Initialize the subtree pointers */
59 static gint ett_laplink = -1;
60
61 static const value_string laplink_udp_magic[] = {
62         { 0x0f010000, "Name Solicitation" },
63         { 0xf0000200, "Name Reply" },
64         { 0, NULL }
65 };
66
67 static const value_string laplink_tcp_magic[] = {
68         { 0xff08c000, "Unknown TCP query - connection?" },
69         { 0xff08c200, "Unknown TCP query - connection?" },
70         { 0xff0bc000, "Unknown TCP query - connection?" },
71         { 0xff0bc200, "Unknown TCP query - connection?" },
72         { 0xff10c000, "Unknown TCP response - connection?" },
73         { 0xff10c200, "Unknown TCP response - connection?" },
74         { 0xff11c000, "Unknown TCP query/response - directory list or file transfer?" },
75         { 0xff11c200, "Unknown TCP query - directory list or file request?" },
76         { 0xff13c000, "Unknown TCP response - connection?" },
77         { 0xff13c200, "Unknown TCP response - connection?" },
78         { 0xff14c000, "Unknown TCP response - directory list or file transfer?" },
79         { 0, NULL }
80 };
81
82 static gboolean laplink_desegment = TRUE;
83
84 /* Code to actually dissect the packets - UDP */
85 static gint
86 dissect_laplink_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
87 {
88         int offset = 0;
89         proto_item *ti;
90         proto_tree *laplink_tree;
91         guint32 udp_ident;
92         gchar *udp_ident_string;
93
94         /*
95          * Make sure the identifier is reasonable.
96          */
97         if (!tvb_bytes_exist(tvb, offset, 4))
98                 return 0;       /* not enough bytes to check */
99         udp_ident = tvb_get_ntohl(tvb, offset);
100         udp_ident_string = match_strval(udp_ident, laplink_udp_magic);
101         if (udp_ident_string == NULL)
102                 return 0;       /* unknown */
103
104 /* Make entries in Protocol column and Info column on summary display */
105         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
106                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Laplink");
107
108         if (check_col(pinfo->cinfo, COL_INFO))
109                 col_add_str(pinfo->cinfo, COL_INFO, udp_ident_string);
110     
111         if (tree){
112                 ti = proto_tree_add_item(tree, proto_laplink, tvb, 0, -1, FALSE);
113                 laplink_tree = proto_item_add_subtree(ti, ett_laplink);
114
115                 proto_tree_add_uint(laplink_tree, hf_laplink_udp_ident, tvb, offset, 4, udp_ident);
116                 offset += 4;
117
118                 proto_tree_add_item(laplink_tree, hf_laplink_udp_name, tvb, offset, -1, FALSE);
119         }
120         return tvb_length(tvb);
121 }
122
123 /* Code to actually dissect the packets - TCP aspects*/
124 static void
125 dissect_laplink_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
126 {
127         int offset = 0;
128         int length = 0;
129         proto_item *ti;
130         proto_tree *laplink_tree;
131         guint32 tcp_ident;
132
133 /* Make entries in Protocol column and Info column on summary display */
134         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
135                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Laplink");
136
137         tcp_ident = tvb_get_ntohl(tvb, offset);
138         if (check_col(pinfo->cinfo, COL_INFO)) {
139                 col_add_str(pinfo->cinfo, COL_INFO,
140                             val_to_str(tcp_ident, laplink_tcp_magic, "TCP TBA (%u)"));
141         }
142     
143         if (tree){
144                 ti = proto_tree_add_item(tree, proto_laplink, tvb, 0, -1, FALSE);
145
146
147                 laplink_tree = proto_item_add_subtree(ti, ett_laplink);
148
149                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_ident, tvb, offset, 4, FALSE);
150                 offset += 4;
151
152                 length = tvb_get_ntohs(tvb, offset);
153                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_length, tvb, offset, 2, FALSE);
154                 offset += 2;
155
156                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_data, tvb, offset, length, FALSE);
157
158 /* Continue adding tree items to process the packet here */
159
160         }
161
162 /* If this protocol has a sub-dissector call it here, see section 1.8 */
163 }
164
165 static guint
166 get_laplink_pdu_len(tvbuff_t *tvb, int offset)
167 {
168         guint plen;
169         /*
170          * The length doesn't include the length or ident fields; add those in.
171          */
172         plen = (tvb_get_ntohs(tvb, offset+4) + 2 + 4);
173         return plen;
174 }
175
176 static void
177 dissect_laplink_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
178 {
179         tcp_dissect_pdus(tvb, pinfo, tree, laplink_desegment,
180                          6, get_laplink_pdu_len, 
181                          dissect_laplink_tcp_pdu);
182 }
183
184
185 /* Register the protocol with Ethereal */
186
187 void
188 proto_register_laplink(void)
189 {                 
190
191 /* Setup list of header fields  See Section 1.6.1 for details*/
192         static hf_register_info hf[] = {
193                 { &hf_laplink_udp_ident,
194                         { "UDP Ident", "laplink.udp_ident",
195                         FT_UINT32, BASE_HEX, VALS(laplink_udp_magic), 0x0,          
196                         "Unknown magic", HFILL }
197                 },
198                 { &hf_laplink_udp_name,
199                         { "UDP Name", "laplink.udp_name",
200                         FT_STRINGZ, BASE_NONE, NULL, 0x0,          
201                         "Machine name", HFILL }
202                 },
203                 { &hf_laplink_tcp_ident,
204                         { "TCP Ident", "laplink.tcp_ident",
205                         FT_UINT32, BASE_HEX, VALS(laplink_tcp_magic), 0x0,          
206                         "Unknown magic", HFILL }
207                 },
208                 { &hf_laplink_tcp_length,
209                         { "TCP Data payload length", "laplink.tcp_length",
210                         FT_UINT16, BASE_DEC, NULL, 0x0,          
211                         "Length of remaining payload", HFILL }
212                 },
213                 { &hf_laplink_tcp_data,
214                         { "Unknown TCP data", "laplink.tcp_data",
215                         FT_BYTES, BASE_HEX, NULL, 0x0,          
216                         "TCP data", HFILL }
217                 },
218         };
219
220 /* Setup protocol subtree array */
221         static gint *ett[] = {
222                 &ett_laplink,
223         };
224
225         module_t *laplink_module;
226
227 /* Register the protocol name and description */
228         proto_laplink = proto_register_protocol("Laplink",
229             "Laplink", "laplink");
230
231 /* Required function calls to register the header fields and subtrees used */
232         proto_register_field_array(proto_laplink, hf, array_length(hf));
233         proto_register_subtree_array(ett, array_length(ett));
234
235         laplink_module = prefs_register_protocol(proto_laplink, NULL);
236         prefs_register_bool_preference(laplink_module, "desegment_laplink_over_tcp",
237                                        "Desegment all Laplink-over-TCP messages",
238                                        "Whether the Laplink dissector should desegment all Laplink-over-TCP messages",
239                                        &laplink_desegment);
240 }
241
242
243 /* If this dissector uses sub-dissector registration add a registration routine.
244    This format is required because a script is used to find these routines and
245    create the code that calls these routines.
246 */
247 void
248 proto_reg_handoff_laplink(void)
249 {
250         dissector_handle_t laplink_udp_handle;
251         dissector_handle_t laplink_tcp_handle;
252
253         laplink_tcp_handle = create_dissector_handle(dissect_laplink_tcp,
254             proto_laplink);
255         dissector_add("tcp.port", TCP_PORT_LAPLINK, laplink_tcp_handle);
256
257         laplink_udp_handle = new_create_dissector_handle(dissect_laplink_udp,
258             proto_laplink);
259         dissector_add("udp.port", UDP_PORT_LAPLINK, laplink_udp_handle);
260 }
261