Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-newmail.c
1 /* packet-newmail.c
2  * Routines for Exchange New Mail Notification dissection
3  * Copyright 2006, Stephen Fisher (see AUTHORS file)
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 WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
12  * is a dissector file; if you just copied this from README.developer,
13  * don't bother with the "Copied from" - you don't even need to put
14  * in a "Copied from" if you copied an existing dissector, especially
15  * if the bulk of the code in the new dissector is your code)
16  * 
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * 
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdlib.h>
37
38 #include <glib.h>
39
40 #include <epan/packet.h>
41 #include <epan/prefs.h>
42
43 /* Forward declaration we need below */
44 void proto_reg_handoff_newmail(void);
45
46 /* Variables for preferences */
47 static guint preference_default_port = 0;
48
49 /* Initialize the protocol and registered fields */
50 static int proto_newmail = -1;
51 static int hf_newmail_payload = -1;
52
53 /* Initialize the subtree pointers */
54 static gint ett_newmail = -1;
55
56 /* Code to actually dissect the packets */
57 static void
58 dissect_newmail(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
59 {
60         /* Set up structures needed to add the protocol subtree and manage it */
61         proto_item *ti;
62         proto_tree *newmail_tree;
63
64         /* Make entries in Protocol column and Info column on summary display */
65         col_set_str(pinfo->cinfo, COL_PROTOCOL, "NEWMAIL");
66
67         col_set_str(pinfo->cinfo, COL_INFO, "Microsoft Exchange new mail notification");
68
69         if (tree) {
70                 ti = proto_tree_add_item(tree, proto_newmail, tvb, 0, -1, FALSE);
71
72                 newmail_tree = proto_item_add_subtree(ti, ett_newmail);
73
74                 proto_tree_add_item(newmail_tree, hf_newmail_payload, tvb, 0, 8, FALSE);
75         }
76 }
77
78
79 /* Register the protocol with Wireshark */
80 void
81 proto_register_newmail(void)
82 {                 
83
84         /* Setup list of header fields  See Section 1.6.1 for details*/
85         static hf_register_info hf[] = {
86                 { &hf_newmail_payload,
87                   { "Notification payload", "newmail.notification_payload",
88                     FT_BYTES, BASE_NONE, NULL, 0x0,          
89                     "Payload requested by client in the MAPI register push notification packet", HFILL }
90                 },
91         };
92
93         /* Setup protocol subtree array */
94         static gint *ett[] = {
95                 &ett_newmail,
96         };
97
98         module_t *newmail_module;
99
100         /* Register the protocol name and description */
101         proto_newmail = proto_register_protocol("Microsoft Exchange New Mail Notification",
102                                                 "NEWMAIL", "newmail");
103
104         /* Required function calls to register the header fields and subtrees used */
105         proto_register_field_array(proto_newmail, hf, array_length(hf));
106         proto_register_subtree_array(ett, array_length(ett));
107
108         /* Register the dissector without a port yet */
109         register_dissector("newmail", dissect_newmail, proto_newmail);
110         
111         /* Register preferences module */
112         newmail_module = prefs_register_protocol(proto_newmail,
113                                                  proto_reg_handoff_newmail);
114
115         prefs_register_uint_preference(newmail_module,
116                                        "default_port",
117                                        "Default UDP port (optional)",
118                                        "Always dissect this port's traffic as newmail notifications.  Additional ports will be dynamically registered as they are seen in MAPI register push notification packets.",
119                                        10, &preference_default_port);
120         
121 }
122
123 void
124 proto_reg_handoff_newmail(void)
125 {
126         static gboolean inited = FALSE;
127         static dissector_handle_t newmail_handle;
128         static guint preference_default_port_last;
129
130         if(!inited) {
131                 newmail_handle = find_dissector("newmail");
132                 dissector_add_handle("udp.port", newmail_handle); /* for 'decode-as' */
133                 inited = TRUE;
134         } else {
135                 if (preference_default_port_last != 0) {
136                         dissector_delete("udp.port", preference_default_port_last, newmail_handle);
137                 }
138         }       
139
140         if(preference_default_port != 0) {      
141                 dissector_add("udp.port", preference_default_port, newmail_handle);
142         }
143         preference_default_port_last = preference_default_port;
144 }