2a4092f8c25932b31975632584220f77aeafc1ad
[metze/wireshark/wip.git] / epan / dissectors / packet-laplink.c
1 /* packet-laplink.c
2  * Routines for laplink dissection
3  * Copyright 2003, Brad Hards <bradh@frogmouth.net>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 #include "packet-tcp.h"
40 #include <epan/prefs.h>
41
42 #define TCP_PORT_LAPLINK 1547
43 #define UDP_PORT_LAPLINK 1547
44
45 /* Initialize the protocol and registered fields */
46 static int proto_laplink = -1;
47 static int hf_laplink_udp_ident = -1;
48 static int hf_laplink_udp_name = -1;
49 static int hf_laplink_tcp_ident = -1;
50 static int hf_laplink_tcp_length = -1;
51 static int hf_laplink_tcp_data = -1;
52
53 /* Initialize the subtree pointers */
54 static gint ett_laplink = -1;
55
56 static const value_string laplink_udp_magic[] = {
57         { 0x0f010000, "Name Solicitation" },
58         { 0xf0000200, "Name Reply" },
59         { 0, NULL }
60 };
61
62 static const value_string laplink_tcp_magic[] = {
63         { 0xff08c000, "Unknown TCP query - connection?" },
64         { 0xff08c200, "Unknown TCP query - connection?" },
65         { 0xff0bc000, "Unknown TCP query - connection?" },
66         { 0xff0bc200, "Unknown TCP query - connection?" },
67         { 0xff10c000, "Unknown TCP response - connection?" },
68         { 0xff10c200, "Unknown TCP response - connection?" },
69         { 0xff11c000, "Unknown TCP query/response - directory list or file transfer?" },
70         { 0xff11c200, "Unknown TCP query - directory list or file request?" },
71         { 0xff13c000, "Unknown TCP response - connection?" },
72         { 0xff13c200, "Unknown TCP response - connection?" },
73         { 0xff14c000, "Unknown TCP response - directory list or file transfer?" },
74         { 0, NULL }
75 };
76
77 static gboolean laplink_desegment = TRUE;
78
79 /* Code to actually dissect the packets - UDP */
80 static gint
81 dissect_laplink_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
82 {
83         int offset = 0;
84         proto_item *ti;
85         proto_tree *laplink_tree;
86         guint32 udp_ident;
87         const gchar *udp_ident_string;
88
89         /*
90          * Make sure the identifier is reasonable.
91          */
92         if (!tvb_bytes_exist(tvb, offset, 4))
93                 return 0;       /* not enough bytes to check */
94         udp_ident = tvb_get_ntohl(tvb, offset);
95         udp_ident_string = match_strval(udp_ident, laplink_udp_magic);
96         if (udp_ident_string == NULL)
97                 return 0;       /* unknown */
98
99 /* Make entries in Protocol column and Info column on summary display */
100         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
101                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Laplink");
102
103         if (check_col(pinfo->cinfo, COL_INFO))
104                 col_add_str(pinfo->cinfo, COL_INFO, udp_ident_string);
105     
106         if (tree){
107                 ti = proto_tree_add_item(tree, proto_laplink, tvb, 0, -1, FALSE);
108                 laplink_tree = proto_item_add_subtree(ti, ett_laplink);
109
110                 proto_tree_add_uint(laplink_tree, hf_laplink_udp_ident, tvb, offset, 4, udp_ident);
111                 offset += 4;
112
113                 proto_tree_add_item(laplink_tree, hf_laplink_udp_name, tvb, offset, -1, FALSE);
114         }
115         return tvb_length(tvb);
116 }
117
118 /* Code to actually dissect the packets - TCP aspects*/
119 static void
120 dissect_laplink_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
121 {
122         int offset = 0;
123         int length = 0;
124         proto_item *ti;
125         proto_tree *laplink_tree;
126         guint32 tcp_ident;
127
128 /* Make entries in Protocol column and Info column on summary display */
129         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
130                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Laplink");
131
132         tcp_ident = tvb_get_ntohl(tvb, offset);
133         if (check_col(pinfo->cinfo, COL_INFO)) {
134                 col_add_str(pinfo->cinfo, COL_INFO,
135                             val_to_str(tcp_ident, laplink_tcp_magic, "TCP TBA (%u)"));
136         }
137     
138         if (tree){
139                 ti = proto_tree_add_item(tree, proto_laplink, tvb, 0, -1, FALSE);
140
141
142                 laplink_tree = proto_item_add_subtree(ti, ett_laplink);
143
144                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_ident, tvb, offset, 4, FALSE);
145                 offset += 4;
146
147                 length = tvb_get_ntohs(tvb, offset);
148                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_length, tvb, offset, 2, FALSE);
149                 offset += 2;
150
151                 proto_tree_add_item(laplink_tree, hf_laplink_tcp_data, tvb, offset, length, FALSE);
152
153 /* Continue adding tree items to process the packet here */
154
155         }
156
157 /* If this protocol has a sub-dissector call it here, see section 1.8 */
158 }
159
160 static guint
161 get_laplink_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
162 {
163         guint plen;
164         /*
165          * The length doesn't include the length or ident fields; add those in.
166          */
167         plen = (tvb_get_ntohs(tvb, offset+4) + 2 + 4);
168         return plen;
169 }
170
171 static void
172 dissect_laplink_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
173 {
174         tcp_dissect_pdus(tvb, pinfo, tree, laplink_desegment,
175                          6, get_laplink_pdu_len, 
176                          dissect_laplink_tcp_pdu);
177 }
178
179
180 /* Register the protocol with Wireshark */
181
182 void
183 proto_register_laplink(void)
184 {                 
185
186 /* Setup list of header fields  See Section 1.6.1 for details*/
187         static hf_register_info hf[] = {
188                 { &hf_laplink_udp_ident,
189                         { "UDP Ident", "laplink.udp_ident",
190                         FT_UINT32, BASE_HEX, VALS(laplink_udp_magic), 0x0,          
191                         "Unknown magic", HFILL }
192                 },
193                 { &hf_laplink_udp_name,
194                         { "UDP Name", "laplink.udp_name",
195                         FT_STRINGZ, BASE_NONE, NULL, 0x0,          
196                         "Machine name", HFILL }
197                 },
198                 { &hf_laplink_tcp_ident,
199                         { "TCP Ident", "laplink.tcp_ident",
200                         FT_UINT32, BASE_HEX, VALS(laplink_tcp_magic), 0x0,          
201                         "Unknown magic", HFILL }
202                 },
203                 { &hf_laplink_tcp_length,
204                         { "TCP Data payload length", "laplink.tcp_length",
205                         FT_UINT16, BASE_DEC, NULL, 0x0,          
206                         "Length of remaining payload", HFILL }
207                 },
208                 { &hf_laplink_tcp_data,
209                         { "Unknown TCP data", "laplink.tcp_data",
210                         FT_BYTES, BASE_NONE, NULL, 0x0,          
211                         "TCP data", HFILL }
212                 },
213         };
214
215 /* Setup protocol subtree array */
216         static gint *ett[] = {
217                 &ett_laplink,
218         };
219
220         module_t *laplink_module;
221
222 /* Register the protocol name and description */
223         proto_laplink = proto_register_protocol("Laplink",
224             "Laplink", "laplink");
225
226 /* Required function calls to register the header fields and subtrees used */
227         proto_register_field_array(proto_laplink, hf, array_length(hf));
228         proto_register_subtree_array(ett, array_length(ett));
229
230         laplink_module = prefs_register_protocol(proto_laplink, NULL);
231         prefs_register_bool_preference(laplink_module, "desegment_laplink_over_tcp",
232                                        "Reassemble Laplink over TCP messages spanning multiple TCP segments",
233                                        "Whether the Laplink dissector should reassemble messages spanning multiple TCP segments."
234                                        " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
235                                        &laplink_desegment);
236 }
237
238
239 /* If this dissector uses sub-dissector registration add a registration routine.
240    This format is required because a script is used to find these routines and
241    create the code that calls these routines.
242 */
243 void
244 proto_reg_handoff_laplink(void)
245 {
246         dissector_handle_t laplink_udp_handle;
247         dissector_handle_t laplink_tcp_handle;
248
249         laplink_tcp_handle = create_dissector_handle(dissect_laplink_tcp,
250             proto_laplink);
251         dissector_add("tcp.port", TCP_PORT_LAPLINK, laplink_tcp_handle);
252
253         laplink_udp_handle = new_create_dissector_handle(dissect_laplink_udp,
254             proto_laplink);
255         dissector_add("udp.port", UDP_PORT_LAPLINK, laplink_udp_handle);
256 }
257