checkAPIs.pl: support for new-style dissectors in check_hf_entries
[metze/wireshark/wip.git] / epan / dissectors / packet-acap.c
1 /* packet-acap.c
2  * Routines for ACAP packet dissection
3  * RFC 2244
4  * Copyright 2003, Brad Hards <bradh@frogmouth.net>
5  * Heavily based in packet-imap.c, Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-imap.c
12  *
13  * SPDX-License-Identifier: GPL-2.0-or-later
14  */
15
16 #define NEW_PROTO_TREE_API
17
18 #include "config.h"
19
20 #include <epan/packet.h>
21 #include <epan/strutil.h>
22
23 /* Forward declarations */
24 void proto_register_acap(void);
25 void proto_reg_handoff_acap(void);
26
27 static dissector_handle_t acap_handle;
28
29 static header_field_info *hfi_acap = NULL;
30
31 #define HFI_ACAP HFI_INIT(proto_acap)
32
33 static header_field_info hfi_acap_response HFI_ACAP = {
34     "Response",           "acap.response",
35     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
36     "TRUE if ACAP response", HFILL };
37
38 static header_field_info hfi_acap_request HFI_ACAP = {
39     "Request",            "acap.request",
40     FT_BOOLEAN, BASE_NONE, NULL, 0x0,
41     "TRUE if ACAP request", HFILL };
42
43 static header_field_info hfi_acap_request_tag HFI_ACAP = {
44     "Request Tag",            "acap.request_tag",
45     FT_STRING, BASE_NONE, NULL, 0x0,
46     NULL, HFILL };
47
48 static header_field_info hfi_acap_response_tag HFI_ACAP = {
49     "Response Tag",            "acap.response_tag",
50     FT_STRING, BASE_NONE, NULL, 0x0,
51     NULL, HFILL };
52
53 static header_field_info hfi_acap_request_data HFI_ACAP = {
54     "Request",            "acap.request_data",
55     FT_STRING, BASE_NONE, NULL, 0x0,
56     NULL, HFILL };
57
58 static header_field_info hfi_acap_response_data HFI_ACAP = {
59     "Response",            "acap.response_data",
60     FT_STRING, BASE_NONE, NULL, 0x0,
61     NULL, HFILL };
62
63 static gint ett_acap = -1;
64 static gint ett_acap_reqresp = -1;
65
66 #define TCP_PORT_ACAP           674
67
68 static int
69 dissect_acap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
70 {
71     gboolean      is_request;
72     proto_tree   *acap_tree, *reqresp_tree;
73     proto_item   *ti, *hidden_item;
74     gint          offset = 0;
75     const guchar *line;
76     gint          next_offset;
77     int           linelen;
78     int           tokenlen;
79     const guchar *next_token;
80
81
82     /*
83      * If this should be a request or response, do this quick check to see if
84      * it begins with a string...
85      * Otherwise, looking for the end of line in a binary file can take a long time
86      * and this probably isn't ACAP
87      */
88     if (!g_ascii_isprint(tvb_get_guint8(tvb, offset))) {
89         return 0;
90     }
91
92     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ACAP");
93
94     /*
95      * Find the end of the first line.
96      *
97      * Note that "tvb_find_line_end()" will return a value that is
98      * not longer than what's in the buffer, so the "tvb_get_ptr()"
99      * call won't throw an exception.
100      */
101     linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, FALSE);
102     line = tvb_get_ptr(tvb, offset, linelen);
103
104     if (pinfo->match_uint == pinfo->destport)
105         is_request = TRUE;
106     else
107         is_request = FALSE;
108
109     /*
110      * Put the first line from the buffer into the summary
111      * (but leave out the line terminator).
112      */
113     col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
114         is_request ? "Request" : "Response",
115         format_text(wmem_packet_scope(), line, linelen));
116
117     if (tree) {
118         ti = proto_tree_add_item(tree, hfi_acap, tvb, offset, -1,
119             ENC_NA);
120         acap_tree = proto_item_add_subtree(ti, ett_acap);
121
122         if (is_request) {
123             hidden_item = proto_tree_add_boolean(acap_tree,
124                 &hfi_acap_request, tvb, 0, 0, TRUE);
125             PROTO_ITEM_SET_HIDDEN(hidden_item);
126         } else {
127             hidden_item = proto_tree_add_boolean(acap_tree,
128                 &hfi_acap_response, tvb, 0, 0, TRUE);
129             PROTO_ITEM_SET_HIDDEN(hidden_item);
130         }
131
132         /*
133          * Put the line into the protocol tree.
134          */
135         ti = proto_tree_add_format_text(acap_tree, tvb, offset, next_offset - offset);
136         reqresp_tree = proto_item_add_subtree(ti, ett_acap_reqresp);
137
138         /*
139          * Show the first line as tags + requests or replies.
140          */
141
142         /*
143          * Extract the first token, and, if there is a first
144          * token, add it as the request or reply tag.
145          */
146         tokenlen = get_token_len(line, line + linelen, &next_token);
147         if (tokenlen != 0) {
148             if (is_request) {
149                 proto_tree_add_string(reqresp_tree, &hfi_acap_request_tag, tvb, offset,
150                     tokenlen, format_text(wmem_packet_scope(), line, tokenlen));
151             } else {
152                 proto_tree_add_string(reqresp_tree, &hfi_acap_response_tag, tvb, offset,
153                     tokenlen, format_text(wmem_packet_scope(), line, tokenlen));
154             }
155             offset += (int)(next_token - line);
156             linelen -= (int)(next_token - line);
157             line = next_token;
158         }
159
160         /*
161          * Add the rest of the line as request or reply data.
162          */
163         if (linelen != 0) {
164             if (is_request) {
165                 proto_tree_add_string(reqresp_tree, &hfi_acap_request_data, tvb, offset,
166                     linelen, format_text(wmem_packet_scope(), line, linelen));
167             } else {
168                 proto_tree_add_string(reqresp_tree, &hfi_acap_response_data, tvb, offset,
169                     linelen, format_text(wmem_packet_scope(), line, linelen));
170             }
171         }
172
173         /*
174          * XXX - show the rest of the frame; this requires that
175          * we handle literals, quoted strings, continuation
176          * responses, etc..
177          *
178          * This involves a state machine, and attaching
179          * state information to the packets.
180          */
181     }
182     return tvb_captured_length(tvb);
183 }
184
185 void
186 proto_register_acap(void)
187 {
188 #ifndef HAVE_HFI_SECTION_INIT
189     static header_field_info *hfi[] = {
190         &hfi_acap_response,
191         &hfi_acap_request,
192         &hfi_acap_request_tag,
193         &hfi_acap_response_tag,
194         &hfi_acap_request_data,
195         &hfi_acap_response_data,
196     };
197 #endif
198
199     static gint *ett[] = {
200         &ett_acap,
201         &ett_acap_reqresp,
202     };
203
204     int proto_acap;
205
206     proto_acap = proto_register_protocol("Application Configuration Access Protocol",
207                          "ACAP", "acap");
208     hfi_acap = proto_registrar_get_nth(proto_acap);
209
210     proto_register_fields(proto_acap, hfi, array_length(hfi));
211     proto_register_subtree_array(ett, array_length(ett));
212
213     acap_handle = create_dissector_handle(dissect_acap, proto_acap);
214 }
215
216 void
217 proto_reg_handoff_acap(void)
218 {
219     dissector_add_uint_with_preference("tcp.port", TCP_PORT_ACAP, acap_handle);
220 }
221
222 /*
223  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
224  *
225  * Local variables:
226  * c-basic-offset: 4
227  * tab-width: 8
228  * indent-tabs-mode: nil
229  * End:
230  *
231  * vi: set shiftwidth=4 tabstop=8 expandtab:
232  * :indentSize=4:tabSize=8:noTabs=true:
233  */