In the final scene of the horror movie, just when you think the monster
[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.10 2000/05/31 05:07:26 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
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 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <string.h>
41 #include <glib.h>
42 #include "packet.h"
43
44 static int proto_nntp = -1;
45 static int hf_nntp_response = -1;
46 static int hf_nntp_request = -1;
47
48 static gint ett_nntp = -1;
49
50 #define TCP_PORT_NNTP                   119
51
52 static void
53 dissect_nntp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
54 {
55         gchar           *type;
56         proto_tree      *nntp_tree, *ti;
57         const u_char    *data, *dataend;
58         const u_char    *lineend, *eol;
59         int             linelen;
60         int             max_data = pi.captured_len - offset;
61
62         data = &pd[offset];
63         dataend = data + END_OF_FRAME;
64         if (dataend > data + max_data)
65                 dataend = data + max_data;
66
67         if (pi.match_port == pi.destport)
68                 type = "Request";
69         else
70                 type = "Response";
71
72         if (check_col(fd, COL_PROTOCOL))
73                 col_add_str(fd, COL_PROTOCOL, "NNTP");
74
75         if (check_col(fd, COL_INFO)) {
76                 /*
77                  * Put the first line from the buffer into the summary.
78                  */
79                 lineend = find_line_end(data, dataend, &eol);
80                 linelen = eol - data;
81                 col_add_fstr(fd, COL_INFO, "%s: %s", type,
82                     format_text(data, linelen));
83         }
84
85         if (tree) {
86
87           ti = proto_tree_add_item(tree, proto_nntp, NullTVB, offset, END_OF_FRAME, FALSE);
88           nntp_tree = proto_item_add_subtree(ti, ett_nntp);
89
90           if (pi.match_port == pi.destport) {
91             proto_tree_add_boolean_hidden(nntp_tree, hf_nntp_request, NullTVB, 0, 0, TRUE);
92           } else {
93             proto_tree_add_boolean_hidden(nntp_tree, hf_nntp_response, NullTVB, 0, 0, TRUE);
94           }
95
96           /*
97            * Show the request or response as text, a line at a time.
98            * XXX - for requests, we could display the stuff after the
99            * first line, if any, based on what the request was, and
100            * for responses, we could display it based on what the
101            * matching request was, although the latter requires us to
102            * know what the matching request was....
103            */
104           while (data < dataend) {
105                 /*
106                  * Find the end of the line.
107                  */
108                 lineend = find_line_end(data, dataend, &eol);
109                 linelen = lineend - data;
110
111                 /*
112                  * Put this line.
113                  */
114                 proto_tree_add_text(nntp_tree, NullTVB, offset, linelen, "%s",
115                     format_text(data, linelen));
116                 offset += linelen;
117                 data = lineend;
118           }
119         }
120 }
121
122 void
123 proto_register_nntp(void)
124 {
125   
126   static hf_register_info hf[] = {
127     { &hf_nntp_response,
128       { "Response",           "nntp.response",
129         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
130         "TRUE if NNTP response" }},
131
132     { &hf_nntp_request,
133       { "Request",            "nntp.request",
134         FT_BOOLEAN, BASE_NONE, NULL, 0x0,
135         "TRUE if NNTP request" }}
136   };
137   static gint *ett[] = {
138     &ett_nntp,
139   };
140
141   proto_nntp = proto_register_protocol("Network News Transfer Protocol", 
142                                        "nntp");
143   proto_register_field_array(proto_nntp, hf, array_length(hf));
144   proto_register_subtree_array(ett, array_length(ett));
145 }
146
147 void
148 proto_reg_handoff_nntp(void)
149 {
150   dissector_add("tcp.port", TCP_PORT_NNTP, dissect_nntp);
151 }