2 * Routines for FastCGI dissection
3 * Copyright 2010, Tom Hughes <tom@compton.nu>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include <epan/packet.h>
15 #include "packet-tcp.h"
17 void proto_register_fcgi(void);
18 void proto_reg_handoff_fcgi(void);
20 static int proto_fcgi
;
22 static int hf_fcgi_version
;
23 static int hf_fcgi_type
;
24 static int hf_fcgi_id
;
25 static int hf_fcgi_content_length
;
26 static int hf_fcgi_padding_length
;
27 static int hf_fcgi_content_data
;
28 static int hf_fcgi_padding_data
;
29 static int hf_fcgi_begin_request_role
;
30 static int hf_fcgi_begin_request_flags
;
31 static int hf_fcgi_begin_request_keep_conn
;
32 static int hf_fcgi_end_request_app_status
;
33 static int hf_fcgi_end_request_protocol_status
;
34 static int hf_fcgi_nv_name
;
37 static int ett_fcgi_begin_request
;
38 static int ett_fcgi_abort_request
;
39 static int ett_fcgi_end_request
;
40 static int ett_fcgi_params
;
42 static dissector_handle_t fcgi_handle
;
44 #define FCGI_BEGIN_REQUEST 1
45 #define FCGI_ABORT_REQUEST 2
46 #define FCGI_END_REQUEST 3
52 #define FCGI_GET_VALUES 9
53 #define FCGI_GET_VALUES_RESULT 10
54 #define FCGI_UNKNOWN_TYPE 11
56 static const value_string record_types
[] = {
57 { 1, "FCGI_BEGIN_REQUEST" },
58 { 2, "FCGI_ABORT_REQUEST" },
59 { 3, "FCGI_END_REQUEST" },
65 { 9, "FCGI_GET_VALUES" },
66 { 10, "FCGI_GET_VALUES_RESULT" },
67 { 11, "FCGI_UNKNOWN_TYPE" },
71 static const value_string application_roles
[] = {
72 { 1, "FCGI_RESPONDER" },
73 { 2, "FCGI_AUTHORIZER" },
78 #define FCGI_KEEP_CONN 1
80 static const value_string protocol_statuses
[] = {
81 { 0, "FCGI_REQUEST_COMPLETE" },
82 { 1, "FCGI_CANT_MPX_CONN" },
83 { 2, "FCGI_OVERLOADED" },
84 { 3, "FCGI_UNKNOWN_ROLE" },
89 dissect_nv_pairs(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
91 int end_offset
= offset
+ len
;
93 while (offset
< end_offset
) {
94 int start_offset
= offset
;
100 namelen
= tvb_get_uint8(tvb
, offset
);
101 if ((namelen
& 0x80) == 0) {
104 namelen
= tvb_get_ntohl(tvb
, offset
) & 0x7FFFFFFF;
108 valuelen
= tvb_get_uint8(tvb
, offset
);
109 if ((valuelen
& 0x80) == 0) {
112 valuelen
= tvb_get_ntohl(tvb
, offset
) & 0x7FFFFFFF;
116 name
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, namelen
, ENC_ASCII
);
120 value
= tvb_get_string_enc(pinfo
->pool
, tvb
, offset
, valuelen
, ENC_ASCII
);
123 proto_tree_add_string_format(fcgi_tree
, hf_fcgi_nv_name
, tvb
, start_offset
, offset
- start_offset
,
124 name
, "%s = %s", name
, value
);
126 proto_tree_add_string_format(fcgi_tree
, hf_fcgi_nv_name
, tvb
, start_offset
, offset
- start_offset
,
133 dissect_begin_request(tvbuff_t
*tvb
, packet_info
*pinfo _U_
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
137 br_tree
= proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_begin_request
, NULL
, "Begin Request:");
139 proto_tree_add_item(br_tree
, hf_fcgi_begin_request_role
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
142 proto_tree_add_item(br_tree
, hf_fcgi_begin_request_flags
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
143 proto_tree_add_item(br_tree
, hf_fcgi_begin_request_keep_conn
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
152 dissect_abort_request(tvbuff_t
*tvb
, packet_info
*pinfo _U_
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
154 proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_abort_request
, NULL
, "Abort Request:");
160 dissect_end_request(tvbuff_t
*tvb
, packet_info
*pinfo _U_
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
164 er_tree
= proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_end_request
, NULL
, "End Request:");
166 proto_tree_add_item(er_tree
, hf_fcgi_end_request_app_status
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
169 proto_tree_add_item(er_tree
, hf_fcgi_end_request_protocol_status
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
178 dissect_params(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
182 p_tree
= proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_params
, NULL
, "Params:");
184 dissect_nv_pairs(tvb
, pinfo
, p_tree
, offset
, len
);
190 dissect_get_values(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
194 gv_tree
= proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_params
, NULL
, "Get Values:");
196 dissect_nv_pairs(tvb
, pinfo
, gv_tree
, offset
, len
);
202 dissect_get_values_result(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*fcgi_tree
, int offset
, uint16_t len
)
204 proto_tree
*gvr_tree
;
206 gvr_tree
= proto_tree_add_subtree(fcgi_tree
, tvb
, offset
, len
, ett_fcgi_params
, NULL
, "Get Values:");
208 dissect_nv_pairs(tvb
, pinfo
, gvr_tree
, offset
, len
);
214 dissect_fcgi_record(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
219 type
= tvb_get_uint8(tvb
, 1);
221 /* When there are multiple FCGI records in a TCP frame the following code */
222 /* will append the type for each record to COL_INFO. */
223 /* XXX: Unfortunately, something in the tcp_dissect_pdus() code is broken */
224 /* such that only the type for the first FCGI record appears in the */
225 /* INFO column. (All write attempts to COL_INFO after the first fail */
226 /* because pinfo->cinfo->writable is false). */
227 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "FCGI");
228 col_clear(pinfo
->cinfo
, COL_INFO
);
229 col_append_sep_str(pinfo
->cinfo
, COL_INFO
, NULL
,
230 val_to_str(type
, record_types
, "Unknown (%u)"));
231 col_set_fence(pinfo
->cinfo
, COL_INFO
);
233 if (tree
) { /* we are being asked for details */
235 proto_tree
*fcgi_tree
;
239 ti
= proto_tree_add_item(tree
, proto_fcgi
, tvb
, 0, -1, ENC_NA
);
240 proto_item_append_text(ti
, " (%s)",
241 val_to_str(type
, record_types
, "Unknown (%u)"));
242 fcgi_tree
= proto_item_add_subtree(ti
, ett_fcgi
);
244 proto_tree_add_item(fcgi_tree
, hf_fcgi_version
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
247 proto_tree_add_item(fcgi_tree
, hf_fcgi_type
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
250 proto_tree_add_item(fcgi_tree
, hf_fcgi_id
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
253 clen
= tvb_get_ntohs(tvb
, offset
);
254 proto_tree_add_item(fcgi_tree
, hf_fcgi_content_length
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
257 plen
= tvb_get_uint8(tvb
, offset
);
258 proto_tree_add_item(fcgi_tree
, hf_fcgi_padding_length
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
265 case FCGI_BEGIN_REQUEST
:
266 dissect_begin_request(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
269 case FCGI_ABORT_REQUEST
:
270 dissect_abort_request(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
273 case FCGI_END_REQUEST
:
274 dissect_end_request(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
278 dissect_params(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
281 case FCGI_GET_VALUES
:
282 dissect_get_values(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
285 case FCGI_GET_VALUES_RESULT
:
286 dissect_get_values_result(tvb
, pinfo
, fcgi_tree
, offset
, clen
);
291 proto_tree_add_item(fcgi_tree
, hf_fcgi_content_data
, tvb
, offset
, clen
, ENC_NA
);
298 proto_tree_add_item(fcgi_tree
, hf_fcgi_padding_data
, tvb
, offset
, plen
, ENC_NA
);
303 return tvb_captured_length(tvb
);
307 get_fcgi_record_len(packet_info
*pinfo _U_
, tvbuff_t
*tvb
, int offset
, void *data _U_
)
309 return 8 + tvb_get_ntohs(tvb
, offset
+ 4) + tvb_get_uint8(tvb
, offset
+ 6);
313 dissect_fcgi(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data
)
315 tcp_dissect_pdus(tvb
, pinfo
, tree
, true, 8, get_fcgi_record_len
, dissect_fcgi_record
, data
);
316 return tvb_captured_length(tvb
);
320 proto_register_fcgi(void)
322 static hf_register_info hf
[] = {
324 { "Version", "fcgi.version",
325 FT_UINT8
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
327 { "Type", "fcgi.type",
328 FT_UINT8
, BASE_DEC
, VALS(record_types
), 0x0, NULL
, HFILL
} },
330 { "Request ID", "fcgi.id",
331 FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
332 { &hf_fcgi_content_length
,
333 { "Content Length", "fcgi.content.length",
334 FT_UINT16
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
335 { &hf_fcgi_padding_length
,
336 { "Padding Length", "fcgi.padding.length",
337 FT_UINT8
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
338 { &hf_fcgi_content_data
,
339 { "Content Data", "fcgi.content.data",
340 FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
} },
341 { &hf_fcgi_padding_data
,
342 { "Padding Data", "fcgi.padding.data",
343 FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
} },
344 { &hf_fcgi_begin_request_role
,
345 { "Role", "fcgi.begin_request.role",
346 FT_UINT16
, BASE_DEC
, VALS(application_roles
), 0x0, NULL
, HFILL
} },
347 { &hf_fcgi_begin_request_flags
,
348 { "Flags", "fcgi.begin_request.flags",
349 FT_UINT8
, BASE_HEX
, NULL
, 0x0, NULL
, HFILL
} },
350 { &hf_fcgi_begin_request_keep_conn
,
351 { "FCGI_KEEP_CONN", "fcgi.begin_request.keep_conn",
352 FT_BOOLEAN
, 8, NULL
, FCGI_KEEP_CONN
, NULL
, HFILL
} },
353 { &hf_fcgi_end_request_app_status
,
354 { "Application Status", "fcgi.end_request.app_status",
355 FT_UINT32
, BASE_DEC
, NULL
, 0x0, NULL
, HFILL
} },
356 { &hf_fcgi_end_request_protocol_status
,
357 { "Protocol Status", "fcgi.end_request.protocol_status",
358 FT_UINT32
, BASE_DEC
, VALS(protocol_statuses
), 0x0, NULL
, HFILL
} },
360 { "NV Pair name", "fcgi.nv_name",
361 FT_STRING
, BASE_NONE
, NULL
, 0x0, NULL
, HFILL
} },
363 static int *ett
[] = {
365 &ett_fcgi_begin_request
,
366 &ett_fcgi_abort_request
,
367 &ett_fcgi_end_request
,
371 proto_fcgi
= proto_register_protocol("FastCGI", "FCGI", "fcgi");
373 proto_register_field_array(proto_fcgi
, hf
, array_length(hf
));
374 proto_register_subtree_array(ett
, array_length(ett
));
376 fcgi_handle
= register_dissector("fcgi", dissect_fcgi
, proto_fcgi
);
380 proto_reg_handoff_fcgi(void)
382 dissector_add_for_decode_as_with_preference("tcp.port", fcgi_handle
);
386 * Editor modelines - https://www.wireshark.org/tools/modelines.html
391 * indent-tabs-mode: nil
394 * ex: set shiftwidth=3 tabstop=8 expandtab:
395 * :indentSize=3:tabSize=8:noTabs=true: