There's no need to keep a "FILE *" for the file being printed to in a
[obnox/wireshark/wip.git] / 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: packet-jabber.c,v 1.1 2003/07/07 22:48:55 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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
50 static void
51 dissect_jabber(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
52 {
53         gboolean        is_request;
54         proto_tree      *jabber_tree;
55         proto_item      *ti;
56         gint            offset = 0;
57         const guchar    *line;
58         gint            next_offset;
59         int             linelen;
60
61         if (check_col(pinfo->cinfo, COL_PROTOCOL))
62                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "Jabber");
63
64         /*
65          * Find the end of the first line.
66          *
67          * Note that "tvb_find_line_end()" will return a value that is
68          * not longer than what's in the buffer, so the "tvb_get_ptr()"
69          * call won't throw an exception.
70          */
71         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
72         line = tvb_get_ptr(tvb, offset, linelen);
73
74         if (pinfo->match_port == pinfo->destport)
75                 is_request = TRUE;
76         else
77                 is_request = FALSE;
78
79         if (check_col(pinfo->cinfo, COL_INFO)) {
80                 /*
81                  * Put the first line from the buffer into the summary
82                  * (but leave out the line terminator).
83                  */
84                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
85                     is_request ? "Request" : "Response",
86                     format_text(line, linelen));
87         }
88
89         if (tree) {
90                 ti = proto_tree_add_item(tree, proto_jabber, tvb, offset, -1,
91                     FALSE);
92                 jabber_tree = proto_item_add_subtree(ti, ett_jabber);
93
94                 if (is_request) {
95                         proto_tree_add_boolean_hidden(jabber_tree,
96                             hf_jabber_request, tvb, 0, 0, TRUE);
97                 } else {
98                         proto_tree_add_boolean_hidden(jabber_tree,
99                             hf_jabber_response, tvb, 0, 0, TRUE);
100                 }
101
102                 /*
103                  * Put the line into the protocol tree.
104                  */
105                 ti = proto_tree_add_text(jabber_tree, tvb, offset,
106                     next_offset - offset, "%s",
107                     tvb_format_text(tvb, offset, next_offset - offset));
108         }
109 }
110
111 void
112 proto_register_jabber(void)
113 {
114   static hf_register_info hf[] = {
115     { &hf_jabber_response,
116       { "Response",           "jabber.response",
117         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
118         "TRUE if Jabber response", HFILL }},
119
120     { &hf_jabber_request,
121       { "Request",            "jabber.request",
122         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
123         "TRUE if Jabber request", HFILL }}
124   };
125   static gint *ett[] = {
126     &ett_jabber,
127     &ett_jabber_reqresp,
128   };
129
130   proto_jabber = proto_register_protocol("Jabber XML Messaging",
131                                        "Jabber", "jabber");
132   proto_register_field_array(proto_jabber, hf, array_length(hf));
133   proto_register_subtree_array(ett, array_length(ett));
134 }
135
136 void
137 proto_reg_handoff_jabber(void)
138 {
139   dissector_handle_t jabber_handle;
140
141   jabber_handle = create_dissector_handle(dissect_jabber, proto_jabber);
142   dissector_add("tcp.port", TCP_PORT_JABBER, jabber_handle);
143 }