Use "tvb_get_ntohieee_float()" to fetch floating-point numbers.
[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.24 2002/01/24 09:20:50 guy 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 #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 <epan/packet.h>
43 #include <epan/strutil.h>
44
45 static int proto_nntp = -1;
46 static int hf_nntp_response = -1;
47 static int hf_nntp_request = -1;
48
49 static gint ett_nntp = -1;
50
51 #define TCP_PORT_NNTP                   119
52
53 static void
54 dissect_nntp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
55 {
56         gchar           *type;
57         proto_tree      *nntp_tree;
58         proto_item      *ti;
59         gint            offset = 0;
60         gint            next_offset;
61         int             linelen;
62
63         if (pinfo->match_port == pinfo->destport)
64                 type = "Request";
65         else
66                 type = "Response";
67
68         if (check_col(pinfo->cinfo, COL_PROTOCOL))
69                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "NNTP");
70
71         if (check_col(pinfo->cinfo, COL_INFO)) {
72                 /*
73                  * Put the first line from the buffer into the summary
74                  * (but leave out the line terminator).
75                  *
76                  * Note that "tvb_find_line_end()" will return a value that
77                  * is not longer than what's in the buffer, so the
78                  * "tvb_get_ptr()" call won't throw an exception.
79                  */
80                 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
81                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s", type,
82                     tvb_format_text(tvb, offset, linelen));
83         }
84
85         if (tree) {
86                 ti = proto_tree_add_item(tree, proto_nntp, tvb, offset, -1,
87                     FALSE);
88                 nntp_tree = proto_item_add_subtree(ti, ett_nntp);
89
90                 if (pinfo->match_port == pinfo->destport) {
91                         proto_tree_add_boolean_hidden(nntp_tree,
92                             hf_nntp_request, tvb, 0, 0, TRUE);
93                 } else {
94                         proto_tree_add_boolean_hidden(nntp_tree,
95                             hf_nntp_response, tvb, 0, 0, TRUE);
96                 }
97
98                 /*
99                  * Show the request or response as text, a line at a time.
100                  * XXX - for requests, we could display the stuff after the
101                  * first line, if any, based on what the request was, and
102                  * for responses, we could display it based on what the
103                  * matching request was, although the latter requires us to
104                  * know what the matching request was....
105                  */
106                 while (tvb_offset_exists(tvb, offset)) {
107                         /*
108                          * Find the end of the line.
109                          */
110                         tvb_find_line_end(tvb, offset, -1, &next_offset);
111
112                         /*
113                          * Put this line.
114                          */
115                         proto_tree_add_text(nntp_tree, tvb, offset,
116                             next_offset - offset, "%s",
117                             tvb_format_text(tvb, offset, next_offset - offset));
118                         offset = next_offset;
119                 }
120         }
121 }
122
123 void
124 proto_register_nntp(void)
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", HFILL }},
131
132             { &hf_nntp_request,
133               { "Request",            "nntp.request",
134                 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
135                 "TRUE if NNTP request", HFILL }}
136         };
137         static gint *ett[] = {
138                 &ett_nntp,
139         };
140
141         proto_nntp = proto_register_protocol("Network News Transfer Protocol", 
142             "NNTP", "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_handle_t nntp_handle;
151
152         nntp_handle = create_dissector_handle(dissect_nntp, proto_nntp);
153         dissector_add("tcp.port", TCP_PORT_NNTP, nntp_handle);
154 }