Rename "proto_alloc_dfilter_string()" to
[obnox/wireshark/wip.git] / 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: packet-acap.c,v 1.1 2003/03/25 22:30:19 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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
46 #define TCP_PORT_ACAP                   674
47
48 static void
49 dissect_acap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
50 {
51         gboolean        is_request;
52         proto_tree      *acap_tree, *ti;
53         gint            offset = 0;
54         const guchar    *line;
55         gint            next_offset;
56         int             linelen;
57         int             tokenlen;
58         const guchar    *next_token;
59
60         if (check_col(pinfo->cinfo, COL_PROTOCOL))
61                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "ACAP");
62
63         /*
64          * Find the end of the first line.
65          *
66          * Note that "tvb_find_line_end()" will return a value that is
67          * not longer than what's in the buffer, so the "tvb_get_ptr()"
68          * call won't throw an exception.
69          */
70         linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
71         line = tvb_get_ptr(tvb, offset, linelen);
72
73         if (pinfo->match_port == pinfo->destport)
74                 is_request = TRUE;
75         else
76                 is_request = FALSE;
77
78         if (check_col(pinfo->cinfo, COL_INFO)) {
79                 /*
80                  * Put the first line from the buffer into the summary
81                  * (but leave out the line terminator).
82                  */
83                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
84                     is_request ? "Request" : "Response",
85                     format_text(line, linelen));
86         }
87
88         if (tree) {
89                 ti = proto_tree_add_item(tree, proto_acap, tvb, offset, -1,
90                     FALSE);
91                 acap_tree = proto_item_add_subtree(ti, ett_acap);
92
93                 if (is_request) {
94                         proto_tree_add_boolean_hidden(acap_tree,
95                             hf_acap_request, tvb, 0, 0, TRUE);
96                 } else {
97                         proto_tree_add_boolean_hidden(acap_tree,
98                             hf_acap_response, tvb, 0, 0, TRUE);
99                 }
100
101                 /*
102                  * Show the first line as tags + requests or replies.
103                  */
104
105                 /*
106                  * Extract the first token, and, if there is a first
107                  * token, add it as the request or reply tag.
108                  */
109                 tokenlen = get_token_len(line, line + linelen, &next_token);
110                 if (tokenlen != 0) {
111                         if (is_request) {
112                                 proto_tree_add_text(acap_tree, tvb, offset,
113                                     tokenlen, "Request Tag: %s",
114                                     format_text(line, tokenlen));
115                         } else {
116                                 proto_tree_add_text(acap_tree, tvb, offset,
117                                     tokenlen, "Response Tag: %s",
118                                     format_text(line, tokenlen));
119                         }
120                         offset += next_token - line;
121                         linelen -= next_token - line;
122                         line = next_token;
123                 }
124
125                 /*
126                  * Add the rest of the line as request or reply data.
127                  */
128                 if (linelen != 0) {
129                         if (is_request) {
130                                 proto_tree_add_text(acap_tree, tvb, offset,
131                                     linelen, "Request: %s",
132                                     format_text(line, linelen));
133                         } else {
134                                 proto_tree_add_text(acap_tree, tvb, offset,
135                                     linelen, "Response: %s",
136                                     format_text(line, linelen));
137                         }
138                 }
139
140                 /*
141                  * XXX - show the rest of the frame; this requires that
142                  * we handle literals, quoted strings, continuation
143                  * responses, etc..
144                  *
145                  * This involves a state machine, and attaching
146                  * state information to the packets.
147                  */
148         }
149 }
150
151 void
152 proto_register_acap(void)
153 {
154   static hf_register_info hf[] = {
155     { &hf_acap_response,
156       { "Response",           "acap.response",
157         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
158         "TRUE if ACAP response", HFILL }},
159
160     { &hf_acap_request,
161       { "Request",            "acap.request",
162         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
163         "TRUE if ACAP request", HFILL }}
164   };
165   static gint *ett[] = {
166     &ett_acap,
167   };
168
169   proto_acap = proto_register_protocol("Application Configuration Access Protocol",
170                                        "ACAP", "acap");
171   proto_register_field_array(proto_acap, hf, array_length(hf));
172   proto_register_subtree_array(ett, array_length(ett));
173 }
174
175 void
176 proto_reg_handoff_acap(void)
177 {
178   dissector_handle_t acap_handle;
179
180   acap_handle = create_dissector_handle(dissect_acap, proto_acap);
181   dissector_add("tcp.port", TCP_PORT_ACAP, acap_handle);
182 }