Make "epan_init()" take, as additional arguments, pointers to routines
[obnox/wireshark/wip.git] / packet-nntp.c
1 /* packet-nntp.c
2  * Routines for nntp packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  *
5  * $Id: packet-nntp.c,v 1.27 2002/08/28 21:00:24 jmayer Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/strutil.h>
36
37 static int proto_nntp = -1;
38 static int hf_nntp_response = -1;
39 static int hf_nntp_request = -1;
40
41 static gint ett_nntp = -1;
42
43 #define TCP_PORT_NNTP                   119
44
45 static void
46 dissect_nntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
47 {
48         gchar           *type;
49         proto_tree      *nntp_tree;
50         proto_item      *ti;
51         gint            offset = 0;
52         gint            next_offset;
53         int             linelen;
54
55         if (pinfo->match_port == pinfo->destport)
56                 type = "Request";
57         else
58                 type = "Response";
59
60         if (check_col(pinfo->cinfo, COL_PROTOCOL))
61                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "NNTP");
62
63         if (check_col(pinfo->cinfo, COL_INFO)) {
64                 /*
65                  * Put the first line from the buffer into the summary
66                  * (but leave out the line terminator).
67                  *
68                  * Note that "tvb_find_line_end()" will return a value that
69                  * is not longer than what's in the buffer, so the
70                  * "tvb_get_ptr()" call won't throw an exception.
71                  */
72                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset,
73                     FALSE);
74                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s", type,
75                     tvb_format_text(tvb, offset, linelen));
76         }
77
78         if (tree) {
79                 ti = proto_tree_add_item(tree, proto_nntp, tvb, offset, -1,
80                     FALSE);
81                 nntp_tree = proto_item_add_subtree(ti, ett_nntp);
82
83                 if (pinfo->match_port == pinfo->destport) {
84                         proto_tree_add_boolean_hidden(nntp_tree,
85                             hf_nntp_request, tvb, 0, 0, TRUE);
86                 } else {
87                         proto_tree_add_boolean_hidden(nntp_tree,
88                             hf_nntp_response, tvb, 0, 0, TRUE);
89                 }
90
91                 /*
92                  * Show the request or response as text, a line at a time.
93                  * XXX - for requests, we could display the stuff after the
94                  * first line, if any, based on what the request was, and
95                  * for responses, we could display it based on what the
96                  * matching request was, although the latter requires us to
97                  * know what the matching request was....
98                  */
99                 while (tvb_offset_exists(tvb, offset)) {
100                         /*
101                          * Find the end of the line.
102                          */
103                         tvb_find_line_end(tvb, offset, -1, &next_offset,
104                             FALSE);
105
106                         /*
107                          * Put this line.
108                          */
109                         proto_tree_add_text(nntp_tree, tvb, offset,
110                             next_offset - offset, "%s",
111                             tvb_format_text(tvb, offset, next_offset - offset));
112                         offset = next_offset;
113                 }
114         }
115 }
116
117 void
118 proto_register_nntp(void)
119 {
120         static hf_register_info hf[] = {
121             { &hf_nntp_response,
122               { "Response",           "nntp.response",
123                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
124                 "TRUE if NNTP response", HFILL }},
125
126             { &hf_nntp_request,
127               { "Request",            "nntp.request",
128                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
129                 "TRUE if NNTP request", HFILL }}
130         };
131         static gint *ett[] = {
132                 &ett_nntp,
133         };
134
135         proto_nntp = proto_register_protocol("Network News Transfer Protocol",
136             "NNTP", "nntp");
137         proto_register_field_array(proto_nntp, hf, array_length(hf));
138         proto_register_subtree_array(ett, array_length(ett));
139 }
140
141 void
142 proto_reg_handoff_nntp(void)
143 {
144         dissector_handle_t nntp_handle;
145
146         nntp_handle = create_dissector_handle(dissect_nntp, proto_nntp);
147         dissector_add("tcp.port", TCP_PORT_NNTP, nntp_handle);
148 }