change a whole bunch of ethereal into wireshark
[obnox/wireshark/wip.git] / epan / dissectors / packet-smb-mailslot.c
1 /* packet-smb-mailslot.c
2  * Routines for SMB mailslot packet dissection
3  * Copyright 2000, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-pop.c
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include <epan/dissectors/packet-smb.h>
38 #include "packet-smb-mailslot.h"
39 #include "packet-smb-browse.h"
40 #include "packet-smb-pipe.h"
41
42 static int proto_smb_msp = -1;
43 static int hf_opcode = -1;
44 static int hf_priority = -1;
45 static int hf_class = -1;
46 static int hf_size = -1;
47 static int hf_name = -1;
48
49 static int ett_smb_msp = -1;
50
51 static dissector_handle_t mailslot_browse_handle;
52 static dissector_handle_t mailslot_lanman_handle;
53 static dissector_handle_t netlogon_handle;
54 static dissector_handle_t data_handle;
55
56 #define MAILSLOT_UNKNOWN              0
57 #define MAILSLOT_BROWSE               1
58 #define MAILSLOT_LANMAN               2
59 #define MAILSLOT_NET                  3
60 #define MAILSLOT_TEMP_NETLOGON        4
61 #define MAILSLOT_MSSP                 5
62
63 static const value_string opcode_vals[] = {
64         {1,     "Write Mail Slot"},
65         {0,     NULL}
66 };
67
68 static const value_string class_vals[] = {
69         {1,     "Reliable"},
70         {2,     "Unreliable & Broadcast"},
71         {0,     NULL}
72 };
73
74 /* decode the SMB mail slot protocol
75    for requests
76      mailslot is the name of the mailslot, e.g. BROWSE
77      si->trans_subcmd is set to the symbolic constant matching the mailslot name.
78    for responses
79      mailslot is NULL
80      si->trans_subcmd gives us which mailslot this response refers to.
81 */
82
83 gboolean
84 dissect_mailslot_smb(tvbuff_t *mshdr_tvb, tvbuff_t *setup_tvb,
85                      tvbuff_t *tvb, const char *mailslot, packet_info *pinfo,
86                      proto_tree *parent_tree)
87 {
88         smb_info_t *smb_info;
89         smb_transact_info_t *tri;
90         int             trans_subcmd;
91         proto_tree      *tree = NULL;
92         proto_item      *item = NULL;
93         guint16         opcode;
94         int             offset = 0;
95         int             len;
96
97         if (!proto_is_protocol_enabled(find_protocol_by_id(proto_smb_msp))) {
98                 return FALSE;
99         }
100         pinfo->current_proto = "SMB Mailslot";
101
102         if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
103                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SMB Mailslot");
104         }
105
106         if ((tvb==NULL) || (tvb_reported_length(tvb)==0)) {
107                 /* Interim reply */
108                 if (check_col (pinfo->cinfo, COL_INFO)) {
109                         col_set_str(pinfo->cinfo, COL_INFO, "Interim reply");
110                 }
111                 return TRUE;
112         }
113
114         if (check_col(pinfo->cinfo, COL_INFO)) {
115                 col_clear(pinfo->cinfo, COL_INFO);
116         }
117
118         smb_info = pinfo->private_data;
119         if (smb_info->sip != NULL && smb_info->sip->extra_info_type == SMB_EI_TRI)
120                 tri = smb_info->sip->extra_info;
121         else
122                 tri = NULL;
123
124         /* check which mailslot this is about */
125         trans_subcmd=MAILSLOT_UNKNOWN;
126         if(smb_info->request){
127                 if(strncmp(mailslot,"BROWSE",6) == 0){
128                         trans_subcmd=MAILSLOT_BROWSE;
129                 } else if(strncmp(mailslot,"LANMAN",6) == 0){
130                         trans_subcmd=MAILSLOT_LANMAN;
131                 } else if(strncmp(mailslot,"NET",3) == 0){
132                         trans_subcmd=MAILSLOT_NET;
133                 } else if(strncmp(mailslot,"TEMP\\NETLOGON",13) == 0){
134                         trans_subcmd=MAILSLOT_TEMP_NETLOGON;
135                 } else if(strncmp(mailslot,"MSSP",4) == 0){
136                         trans_subcmd=MAILSLOT_MSSP;
137                 }
138                 if (!pinfo->fd->flags.visited) {
139                         if (tri != NULL)
140                                 tri->trans_subcmd = trans_subcmd;
141                 }
142         } else {
143                 if(!tri){
144                         return FALSE;
145                 } else {
146                         trans_subcmd = tri->trans_subcmd;
147                 }
148         }
149
150         /* Only do these ones if we have them. For fragmented SMB Transactions
151            we may only have the setup area for the first fragment
152         */
153         if(mshdr_tvb && setup_tvb){
154                 if (parent_tree) {
155                         item = proto_tree_add_item(parent_tree, proto_smb_msp,
156                                                    mshdr_tvb, 0, -1, FALSE);
157                         tree = proto_item_add_subtree(item, ett_smb_msp);
158                 }
159
160                 /* do the opcode field */
161                 opcode = tvb_get_letohs(setup_tvb, offset);
162
163                 if (check_col(pinfo->cinfo, COL_INFO)) {
164                         col_add_str(pinfo->cinfo, COL_INFO,
165                                     val_to_str(opcode, opcode_vals, "Unknown opcode: 0x%04x"));
166                 }
167
168
169                 /* These are in the setup words; use "setup_tvb". */
170
171                 /* opcode */
172                 proto_tree_add_uint(tree, hf_opcode, setup_tvb, offset, 2,
173                     opcode);
174                 offset += 2;
175
176                 /* priority */
177                 proto_tree_add_item(tree, hf_priority, setup_tvb, offset, 2,
178                     TRUE);
179                 offset += 2;
180
181                 /* class */
182                 proto_tree_add_item(tree, hf_class, setup_tvb, offset, 2, TRUE);
183                 offset += 2;
184
185                 /* These are in the rest of the data; use "mshdr_tvb", which
186                    starts at the same place "setup_tvb" does. */
187
188                 /* size */
189                 /* this is actually bytecount in the SMB Transaction command */
190                 proto_tree_add_item(tree, hf_size, mshdr_tvb, offset, 2, TRUE);
191                 offset += 2;
192
193                 /* mailslot name */
194                 len = tvb_strsize(mshdr_tvb, offset);
195                 proto_tree_add_item(tree, hf_name, mshdr_tvb, offset, len, TRUE);
196                 offset += len;
197                 proto_item_set_len(item, offset);
198         }
199
200         switch(trans_subcmd){
201         case MAILSLOT_BROWSE:
202                 call_dissector(mailslot_browse_handle, tvb, pinfo,
203                     parent_tree);
204                 break;
205
206         case MAILSLOT_LANMAN:
207                 call_dissector(mailslot_lanman_handle, tvb, pinfo,
208                     parent_tree);
209                 break;
210
211         case MAILSLOT_NET:
212         case MAILSLOT_TEMP_NETLOGON:
213         case MAILSLOT_MSSP:
214                 call_dissector(netlogon_handle, tvb, pinfo,
215                     parent_tree);
216                 break;
217
218         default:
219                 /*
220                  * We dissected the mailslot header, but we don't know
221                  * how to dissect the message; dissect the latter as data,
222                  * but indicate that we successfully dissected the mailslot
223                  * stuff.
224                  */
225                 call_dissector(data_handle ,tvb, pinfo, parent_tree);
226                 break;
227         }
228         return TRUE;
229 }
230
231 void
232 proto_register_smb_mailslot(void)
233 {
234         static hf_register_info hf[] = {
235                 { &hf_opcode,
236                         { "Opcode", "mailslot.opcode", FT_UINT16, BASE_DEC,
237                         VALS(opcode_vals), 0, "MAILSLOT OpCode", HFILL }},
238
239                 { &hf_priority,
240                         { "Priority", "mailslot.priority", FT_UINT16, BASE_DEC,
241                         NULL, 0, "MAILSLOT Priority of transaction", HFILL }},
242
243                 { &hf_class,
244                         { "Class", "mailslot.class", FT_UINT16, BASE_DEC,
245                         VALS(class_vals), 0, "MAILSLOT Class of transaction", HFILL }},
246
247                 { &hf_size,
248                         { "Size", "mailslot.size", FT_UINT16, BASE_DEC,
249                         NULL, 0, "MAILSLOT Total size of mail data", HFILL }},
250
251                 { &hf_name,
252                         { "Mailslot Name", "mailslot.name", FT_STRING, BASE_NONE,
253                         NULL, 0, "MAILSLOT Name of mailslot", HFILL }},
254
255         };
256
257         static gint *ett[] = {
258                 &ett_smb_msp
259         };
260
261         proto_smb_msp = proto_register_protocol(
262                 "SMB MailSlot Protocol", "SMB Mailslot", "mailslot");
263
264         proto_register_field_array(proto_smb_msp, hf, array_length(hf));
265         proto_register_subtree_array(ett, array_length(ett));
266 }
267
268 void
269 proto_reg_handoff_smb_mailslot(void)
270 {
271         mailslot_browse_handle = find_dissector("mailslot_browse");
272         mailslot_lanman_handle = find_dissector("mailslot_lanman");
273         netlogon_handle = find_dissector("smb_netlogon");
274         data_handle = find_dissector("data");
275 }