Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / epan / dissectors / packet-jabber.c
1 /* packet-jabber.c
2  * Routines for Jabber packet dissection
3  * Copyright 2003, Brad Hards <bradh@frogmouth.net>
4  * Heavily based in packet-acap.c, which in turn is heavily based on 
5  * packet-imap.c, Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-acap.c
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35
36 #include <string.h>
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include <epan/strutil.h>
40
41 static int proto_jabber = -1;
42 static int hf_jabber_response = -1;
43 static int hf_jabber_request = -1;
44
45 static gint ett_jabber = -1;
46 static gint ett_jabber_reqresp = -1;
47
48 #define TCP_PORT_JABBER                 5222
49 static dissector_handle_t xml_handle=NULL;
50
51 static void
52 dissect_jabber(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
53 {
54         gboolean        is_request;
55         proto_tree      *jabber_tree;
56         proto_item      *ti;
57         gint            offset = 0;
58         const guchar    *line;
59         gint            next_offset;
60         int             linelen;
61         tvbuff_t *xmltvb;
62
63         if (check_col(pinfo->cinfo, COL_PROTOCOL))
64                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Jabber");
65
66         /*
67          * Find the end of the first line.
68          *
69          * Note that "tvb_find_line_end()" will return a value that is
70          * not longer than what's in the buffer, so the "tvb_get_ptr()"
71          * call won't throw an exception.
72          */
73         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
74         line = tvb_get_ptr(tvb, offset, linelen);
75
76         if (pinfo->match_port == pinfo->destport)
77                 is_request = TRUE;
78         else
79                 is_request = FALSE;
80
81         if (check_col(pinfo->cinfo, COL_INFO)) {
82                 /*
83                  * Put the first line from the buffer into the summary
84                  * (but leave out the line terminator).
85                  */
86                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
87                     is_request ? "Request" : "Response",
88                     format_text(line, linelen));
89         }
90
91         if (tree) {
92                 ti = proto_tree_add_item(tree, proto_jabber, tvb, offset, -1,
93                     FALSE);
94                 jabber_tree = proto_item_add_subtree(ti, ett_jabber);
95
96                 if (is_request) {
97                         proto_tree_add_boolean_hidden(jabber_tree,
98                             hf_jabber_request, tvb, 0, 0, TRUE);
99                 } else {
100                         proto_tree_add_boolean_hidden(jabber_tree,
101                             hf_jabber_response, tvb, 0, 0, TRUE);
102                 }
103
104                 xmltvb = tvb_new_subset(tvb, offset, -1, -1);
105                 call_dissector(xml_handle, xmltvb, pinfo, jabber_tree);
106         }
107 }
108
109 void
110 proto_register_jabber(void)
111 {
112   static hf_register_info hf[] = {
113     { &hf_jabber_response,
114       { "Response",           "jabber.response",
115         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
116         "TRUE if Jabber response", HFILL }},
117
118     { &hf_jabber_request,
119       { "Request",            "jabber.request",
120         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
121         "TRUE if Jabber request", HFILL }}
122   };
123   static gint *ett[] = {
124     &ett_jabber,
125     &ett_jabber_reqresp,
126   };
127
128   proto_jabber = proto_register_protocol("Jabber XML Messaging",
129                                        "Jabber", "jabber");
130   proto_register_field_array(proto_jabber, hf, array_length(hf));
131   proto_register_subtree_array(ett, array_length(ett));
132 }
133
134 void
135 proto_reg_handoff_jabber(void)
136 {
137   dissector_handle_t jabber_handle;
138
139   xml_handle = find_dissector("xml");
140
141   jabber_handle = create_dissector_handle(dissect_jabber, proto_jabber);
142   dissector_add("tcp.port", TCP_PORT_JABBER, jabber_handle);
143 }