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