Enable Lua tcp tap userdata.
[obnox/wireshark/wip.git] / epan / dissectors / packet-acap.c
1 /* packet-acap.c
2  * Routines for ACAP packet dissection
3  * Copyright 2003, Brad Hards <bradh@frogmouth.net>
4  * Heavily based in packet-imap.c, Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-imap.c
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39
40 static int proto_acap = -1;
41 static int hf_acap_response = -1;
42 static int hf_acap_request = -1;
43
44 static gint ett_acap = -1;
45 static gint ett_acap_reqresp = -1;
46
47 #define TCP_PORT_ACAP                   674
48
49 static void
50 dissect_acap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
51 {
52     gboolean        is_request;
53     proto_tree      *acap_tree, *reqresp_tree;
54     proto_item      *ti, *hidden_item;
55         gint                    offset = 0;
56         const guchar    *line;
57         gint                    next_offset;
58         int                             linelen;
59         int                             tokenlen;
60         const guchar    *next_token;
61
62         if (check_col(pinfo->cinfo, COL_PROTOCOL))
63                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ACAP");
64
65         /*
66          * Find the end of the first line.
67          *
68          * Note that "tvb_find_line_end()" will return a value that is
69          * not longer than what's in the buffer, so the "tvb_get_ptr()"
70          * call won't throw an exception.
71          */
72         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
73         line = tvb_get_ptr(tvb, offset, linelen);
74
75         if (pinfo->match_port == pinfo->destport)
76                 is_request = TRUE;
77         else
78                 is_request = FALSE;
79
80         if (check_col(pinfo->cinfo, COL_INFO)) {
81                 /*
82                  * Put the first line from the buffer into the summary
83                  * (but leave out the line terminator).
84                  */
85                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
86                     is_request ? "Request" : "Response",
87                     format_text(line, linelen));
88         }
89
90         if (tree) {
91                 ti = proto_tree_add_item(tree, proto_acap, tvb, offset, -1,
92                     FALSE);
93                 acap_tree = proto_item_add_subtree(ti, ett_acap);
94
95                 if (is_request) {
96                         hidden_item = proto_tree_add_boolean(acap_tree,
97                             hf_acap_request, tvb, 0, 0, TRUE);
98                         PROTO_ITEM_SET_HIDDEN(hidden_item);
99                 } else {
100                         hidden_item = proto_tree_add_boolean(acap_tree,
101                             hf_acap_response, tvb, 0, 0, TRUE);
102                         PROTO_ITEM_SET_HIDDEN(hidden_item);
103                 }
104
105                 /*
106                  * Put the line into the protocol tree.
107                  */
108                 ti = proto_tree_add_text(acap_tree, tvb, offset,
109                     next_offset - offset, "%s",
110                     tvb_format_text(tvb, offset, next_offset - offset));
111                 reqresp_tree = proto_item_add_subtree(ti, ett_acap_reqresp);
112
113                 /*
114                  * Show the first line as tags + requests or replies.
115                  */
116
117                 /*
118                  * Extract the first token, and, if there is a first
119                  * token, add it as the request or reply tag.
120                  */
121                 tokenlen = get_token_len(line, line + linelen, &next_token);
122                 if (tokenlen != 0) {
123                         if (is_request) {
124                                 proto_tree_add_text(reqresp_tree, tvb, offset,
125                                     tokenlen, "Request Tag: %s",
126                                     format_text(line, tokenlen));
127                         } else {
128                                 proto_tree_add_text(reqresp_tree, tvb, offset,
129                                     tokenlen, "Response Tag: %s",
130                                     format_text(line, tokenlen));
131                         }
132                         offset += (int)(next_token - line);
133                         linelen -= (int)(next_token - line);
134                         line = next_token;
135                 }
136
137                 /*
138                  * Add the rest of the line as request or reply data.
139                  */
140                 if (linelen != 0) {
141                         if (is_request) {
142                                 proto_tree_add_text(reqresp_tree, tvb, offset,
143                                     linelen, "Request: %s",
144                                     format_text(line, linelen));
145                         } else {
146                                 proto_tree_add_text(reqresp_tree, tvb, offset,
147                                     linelen, "Response: %s",
148                                     format_text(line, linelen));
149                         }
150                 }
151
152                 /*
153                  * XXX - show the rest of the frame; this requires that
154                  * we handle literals, quoted strings, continuation
155                  * responses, etc..
156                  *
157                  * This involves a state machine, and attaching
158                  * state information to the packets.
159                  */
160         }
161 }
162
163 void
164 proto_register_acap(void)
165 {
166   static hf_register_info hf[] = {
167     { &hf_acap_response,
168       { "Response",           "acap.response",
169         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
170         "TRUE if ACAP response", HFILL }},
171
172     { &hf_acap_request,
173       { "Request",            "acap.request",
174         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
175         "TRUE if ACAP request", HFILL }}
176   };
177   static gint *ett[] = {
178     &ett_acap,
179     &ett_acap_reqresp,
180   };
181
182   proto_acap = proto_register_protocol("Application Configuration Access Protocol",
183                                        "ACAP", "acap");
184   proto_register_field_array(proto_acap, hf, array_length(hf));
185   proto_register_subtree_array(ett, array_length(ett));
186 }
187
188 void
189 proto_reg_handoff_acap(void)
190 {
191   dissector_handle_t acap_handle;
192
193   acap_handle = create_dissector_handle(dissect_acap, proto_acap);
194   dissector_add("tcp.port", TCP_PORT_ACAP, acap_handle);
195 }