Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-nntp.c
blob8fb391dfae3c3c66d05237282d69446195e7862f
1 /* packet-nntp.c
2 * Routines for nntp packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
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
12 #include "config.h"
14 #include <epan/packet.h>
16 #include "packet-tls-utils.h"
18 void proto_register_nntp(void);
19 void proto_reg_handoff_nntp(void);
21 static int proto_nntp;
22 static int hf_nntp_response;
23 static int hf_nntp_request;
25 static int ett_nntp;
27 static dissector_handle_t nntp_handle;
28 static dissector_handle_t tls_handle;
30 #define TCP_PORT_NNTP 119
32 /* State of NNTP conversation */
33 typedef struct nntp_conversation_t {
34 bool tls_requested;
35 } nntp_conversation_t;
37 static int
38 dissect_nntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
40 const char *type;
41 proto_tree *nntp_tree;
42 proto_item *ti;
43 int offset = 0;
44 int next_offset;
45 const unsigned char *line;
46 int linelen;
47 conversation_t *conversation;
48 nntp_conversation_t *session_state;
50 conversation = find_or_create_conversation(pinfo);
51 session_state = (nntp_conversation_t *)conversation_get_proto_data(conversation, proto_nntp);
52 if (!session_state) {
53 session_state = wmem_new0(wmem_file_scope(), nntp_conversation_t);
54 session_state->tls_requested = false;
55 conversation_add_proto_data(conversation, proto_nntp, session_state);
58 if (pinfo->match_uint == pinfo->destport)
59 type = "Request";
60 else
61 type = "Response";
63 col_set_str(pinfo->cinfo, COL_PROTOCOL, "NNTP");
66 * Put the first line from the buffer into the summary
67 * (but leave out the line terminator).
69 * Note that "tvb_find_line_end()" will return a value that
70 * is not longer than what's in the buffer, so the
71 * "tvb_get_ptr()" call won't throw an exception.
73 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, false);
74 line = tvb_get_ptr(tvb, offset, linelen);
75 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s", type,
76 tvb_format_text(pinfo->pool, tvb, offset, linelen));
78 ti = proto_tree_add_item(tree, proto_nntp, tvb, offset, -1, ENC_NA);
79 nntp_tree = proto_item_add_subtree(ti, ett_nntp);
81 if (pinfo->match_uint == pinfo->destport) {
82 ti = proto_tree_add_boolean(nntp_tree, hf_nntp_request, tvb, 0, 0, true);
84 if (line && g_ascii_strncasecmp(line, "STARTTLS", 8) == 0) {
85 session_state->tls_requested = true;
87 } else {
88 ti = proto_tree_add_boolean(nntp_tree, hf_nntp_response, tvb, 0, 0, true);
90 if (session_state->tls_requested) {
91 if (line && g_ascii_strncasecmp(line, "382", 3) == 0) {
92 /* STARTTLS command accepted */
93 ssl_starttls_ack(tls_handle, pinfo, nntp_handle);
95 session_state->tls_requested = false;
98 proto_item_set_hidden(ti);
101 * Show the request or response as text, a line at a time.
102 * XXX - for requests, we could display the stuff after the
103 * first line, if any, based on what the request was, and
104 * for responses, we could display it based on what the
105 * matching request was, although the latter requires us to
106 * know what the matching request was....
108 while (tvb_offset_exists(tvb, offset)) {
110 * Find the end of the line.
112 tvb_find_line_end(tvb, offset, -1, &next_offset, false);
115 * Put this line.
117 proto_tree_add_format_text(nntp_tree, tvb, offset, next_offset - offset);
118 offset = next_offset;
121 return tvb_captured_length(tvb);
124 void
125 proto_register_nntp(void)
127 static hf_register_info hf[] = {
128 { &hf_nntp_response,
129 { "Response", "nntp.response",
130 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
131 "true if NNTP response", HFILL }},
133 { &hf_nntp_request,
134 { "Request", "nntp.request",
135 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
136 "true if NNTP request", HFILL }}
138 static int *ett[] = {
139 &ett_nntp,
142 proto_nntp = proto_register_protocol("Network News Transfer Protocol",
143 "NNTP", "nntp");
144 proto_register_field_array(proto_nntp, hf, array_length(hf));
145 proto_register_subtree_array(ett, array_length(ett));
148 void
149 proto_reg_handoff_nntp(void)
151 nntp_handle = register_dissector("nntp", dissect_nntp, proto_nntp);
152 dissector_add_uint_with_preference("tcp.port", TCP_PORT_NNTP, nntp_handle);
154 tls_handle = find_dissector("tls");
158 * Editor modelines - https://www.wireshark.org/tools/modelines.html
160 * Local variables:
161 * c-basic-offset: 8
162 * tab-width: 8
163 * indent-tabs-mode: t
164 * End:
166 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
167 * :indentSize=8:tabSize=8:noTabs=false: