2 * Routines for ftp packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-ftp.c,v 1.26 2001/01/09 06:31:35 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-pop.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
48 static int proto_ftp = -1;
49 static int proto_ftp_data = -1;
50 static int hf_ftp_response = -1;
51 static int hf_ftp_request = -1;
52 static int hf_ftp_request_command = -1;
53 static int hf_ftp_request_data = -1;
54 static int hf_ftp_response_code = -1;
55 static int hf_ftp_response_data = -1;
57 static gint ett_ftp = -1;
58 static gint ett_ftp_data = -1;
60 #define TCP_PORT_FTPDATA 20
61 #define TCP_PORT_FTP 21
64 dissect_ftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
74 const u_char *next_token;
76 CHECK_DISPLAY_AS_DATA(proto_ftp, tvb, pinfo, tree);
78 if (pinfo->match_port == pinfo->destport)
83 pinfo->current_proto = "FTP";
85 if (check_col(pinfo->fd, COL_PROTOCOL))
86 col_set_str(pinfo->fd, COL_PROTOCOL, "FTP");
89 * Find the end of the first line.
91 linelen = tvb_find_line_end(tvb, offset, -1, &next_offset);
92 line = tvb_get_ptr(tvb, offset, linelen);
94 if (check_col(pinfo->fd, COL_INFO)) {
96 * Put the first line from the buffer into the summary.
97 * (but leave out the line terminator).
99 col_add_fstr(pinfo->fd, COL_INFO, "%s: %s",
100 is_request ? "Request" : "Response",
101 format_text(line, linelen));
105 ti = proto_tree_add_item(tree, proto_ftp, tvb, offset,
106 tvb_length_remaining(tvb, offset), FALSE);
107 ftp_tree = proto_item_add_subtree(ti, ett_ftp);
110 proto_tree_add_boolean_hidden(ftp_tree,
111 hf_ftp_request, tvb, 0, 0, TRUE);
112 proto_tree_add_boolean_hidden(ftp_tree,
113 hf_ftp_response, tvb, 0, 0, FALSE);
115 proto_tree_add_boolean_hidden(ftp_tree,
116 hf_ftp_request, tvb, 0, 0, FALSE);
117 proto_tree_add_boolean_hidden(ftp_tree,
118 hf_ftp_response, tvb, 0, 0, TRUE);
122 * Extract the first token, and, if there is a first
123 * token, add it as the request or reply code.
125 tokenlen = get_token_len(line, line + linelen, &next_token);
128 proto_tree_add_string_format(ftp_tree,
129 hf_ftp_request_command, tvb, offset,
130 tokenlen, line, "Request: %s",
131 format_text(line, tokenlen));
133 proto_tree_add_uint_format(ftp_tree,
134 hf_ftp_response_code, tvb, offset,
135 tokenlen, atoi(line), "Response: %s",
136 format_text(line, tokenlen));
138 offset += next_token - line;
139 linelen -= next_token - line;
144 * Add the rest of the first line as request or
149 proto_tree_add_string_format(ftp_tree,
150 hf_ftp_request_data, tvb, offset,
151 linelen, line, "Request Arg: %s",
152 format_text(line, linelen));
154 proto_tree_add_string_format(ftp_tree,
155 hf_ftp_response_data, tvb, offset,
156 linelen, line, "Response Arg: %s",
157 format_text(line, linelen));
160 offset = next_offset;
163 * Show the rest of the request or response as text,
166 while (tvb_offset_exists(tvb, offset)) {
168 * Find the end of the line.
170 linelen = tvb_find_line_end(tvb, offset, -1,
176 proto_tree_add_text(ftp_tree, tvb, offset,
177 next_offset - offset, "%s",
178 tvb_format_text(tvb, offset, next_offset - offset));
179 offset = next_offset;
185 dissect_ftpdata(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
187 proto_tree *ti, *ftp_data_tree;
190 CHECK_DISPLAY_AS_DATA(proto_ftp_data, tvb, pinfo, tree);
192 pinfo->current_proto = "FTP-DATA";
194 if (check_col(pinfo->fd, COL_PROTOCOL))
195 col_set_str(pinfo->fd, COL_PROTOCOL, "FTP-DATA");
197 if (check_col(pinfo->fd, COL_INFO)) {
198 col_add_fstr(pinfo->fd, COL_INFO, "FTP Data: %u bytes",
203 data_length = tvb_length(tvb);
205 ti = proto_tree_add_item(tree, proto_ftp_data, tvb, 0,
207 ftp_data_tree = proto_item_add_subtree(ti, ett_ftp_data);
210 * XXX - if this is binary data, it'll produce
211 * a *really* long line.
213 proto_tree_add_text(ftp_data_tree, tvb, 0, data_length,
214 "FTP Data: %s", tvb_format_text(tvb, 0, data_length));
219 proto_register_ftp(void)
221 static hf_register_info hf[] = {
223 { "Response", "ftp.response",
224 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
225 "TRUE if FTP response" }},
228 { "Request", "ftp.request",
229 FT_BOOLEAN, BASE_NONE, NULL, 0x0,
230 "TRUE if FTP request" }},
232 { &hf_ftp_request_command,
233 { "Request command", "ftp.request.command",
234 FT_STRING, BASE_NONE, NULL, 0x0,
237 { &hf_ftp_request_data,
238 { "Request data", "ftp.request.data",
239 FT_STRING, BASE_NONE, NULL, 0x0,
242 { &hf_ftp_response_code,
243 { "Response code", "ftp.response.code",
244 FT_UINT8, BASE_DEC, NULL, 0x0,
247 { &hf_ftp_response_data,
248 { "Response data", "ftp.reponse.data",
249 FT_STRING, BASE_NONE, NULL, 0x0,
252 static gint *ett[] = {
257 proto_ftp = proto_register_protocol("File Transfer Protocol (FTP)", "FTP",
259 proto_ftp_data = proto_register_protocol("FTP Data", "FTP-DATA", "ftp-data");
260 proto_register_field_array(proto_ftp, hf, array_length(hf));
261 proto_register_subtree_array(ett, array_length(ett));
265 proto_reg_handoff_ftp(void)
267 dissector_add("tcp.port", TCP_PORT_FTPDATA, &dissect_ftpdata, proto_ftp);
268 dissector_add("tcp.port", TCP_PORT_FTP, &dissect_ftp, proto_ftp_data);