Include <time.h> to declare "gmtime()".
[obnox/wireshark/wip.git] / packet-ftp.c
1 /* packet-ftp.c
2  * Routines for ftp packet dissection
3  * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
4  *
5  * $Id: packet-ftp.c,v 1.10 1999/11/16 11:42:30 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@unicom.net>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-pop.c
12  * 
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.
17  * 
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.
22  * 
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42
43 #include <string.h>
44 #include <glib.h>
45 #include "packet.h"
46
47 static int proto_ftp = -1;
48 static int hf_ftp_response = -1;
49 static int hf_ftp_request = -1;
50 static int hf_ftp_request_command = -1;
51 static int hf_ftp_request_data = -1;
52 static int hf_ftp_response_code = -1;
53 static int hf_ftp_response_data = -1;
54
55 static gint ett_ftp = -1;
56
57 void
58 dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
59 {
60         proto_tree      *ftp_tree, *ti;
61         gchar          rr[50], rd[1500];
62         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
63         int i2;
64         int max_data = pi.captured_len - offset;
65
66         memset(rr, '\0', sizeof(rr));
67         memset(rd, '\0', sizeof(rd));
68
69         if (i1 >= 0) {
70
71           /* Hmmm, check if there was no space in there ... */
72
73           if (i1 > max_data) {
74
75             i1 = max_data;  /* Make things below work */
76             strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
77
78           }
79           else {
80
81             strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
82             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
83             if (i2 > max_data - i1 - 1 || i2 < 0) {
84               i2 = max_data - i1 - 1;
85             }
86
87             strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
88           }
89         }
90         else { 
91
92           i1 = max_data;
93           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));  /* Lazy, CRLF */
94
95         }
96
97         if (check_col(fd, COL_PROTOCOL))
98                 col_add_str(fd, COL_PROTOCOL, "FTP");
99
100         if (check_col(fd, COL_INFO)) {
101
102           col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);
103
104         }
105
106         if (tree) {
107
108           ti = proto_tree_add_item(tree, proto_ftp, offset, END_OF_FRAME, NULL);
109           ftp_tree = proto_item_add_subtree(ti, ett_ftp);
110
111           if (pi.match_port == pi.destport) { /* Request */
112
113             proto_tree_add_item_hidden(ftp_tree, hf_ftp_request,
114                                        offset, i1, TRUE);
115             proto_tree_add_item_hidden(ftp_tree, hf_ftp_response,
116                                        offset, i1, FALSE);
117             proto_tree_add_item_format(ftp_tree, hf_ftp_request_command,
118                                        offset, i1, rr, "Request: %s", rr);
119             proto_tree_add_item_format(ftp_tree, hf_ftp_request_data,
120                                        offset + i1 + 1, END_OF_FRAME, 
121                                        rd, "Request Arg: %s", rd);
122           }
123           else {
124
125             proto_tree_add_item_hidden(ftp_tree, hf_ftp_request,
126                                        offset, i1, FALSE);
127             proto_tree_add_item_hidden(ftp_tree, hf_ftp_response,
128                                        offset, i1, TRUE);
129             proto_tree_add_item_format(ftp_tree, hf_ftp_response_code, 
130                                        offset, i1, 
131                                        atoi(rr), "Response: %s", rr);
132             proto_tree_add_item_format(ftp_tree, hf_ftp_response_data,
133                                        offset + i1 + 1, END_OF_FRAME,
134                                        rd, "Response Arg: %s", rd);
135           }
136
137         }
138 }
139
140 void
141 dissect_ftpdata(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
142 {
143         proto_tree      *ti;
144
145         if (check_col(fd, COL_PROTOCOL))
146                 col_add_str(fd, COL_PROTOCOL, "FTP DATA");
147
148         if (check_col(fd, COL_INFO)) {
149
150           col_add_fstr(fd, COL_INFO, "FTP Data ...");
151
152         }
153
154         if (tree) {
155
156           ti = proto_tree_add_text(tree, offset, END_OF_FRAME,
157                                 "File Transfer Protocol Data");
158
159         }
160 }
161
162 void
163 proto_register_ftp(void)
164 {
165   static hf_register_info hf[] = {
166     { &hf_ftp_response,
167       { "Response",           "ftp.response",           FT_BOOLEAN, BASE_NONE, NULL, 0x0,
168         "" }},
169
170     { &hf_ftp_request,
171       { "Request",            "ftp.request",            FT_BOOLEAN, BASE_NONE, NULL, 0x0,
172         "" }},
173
174     { &hf_ftp_request_command,
175       { "Request command",    "ftp.request.command",    FT_STRING,  BASE_NONE, NULL, 0x0,
176         "" }},
177
178     { &hf_ftp_request_data,
179       { "Request data",       "ftp.request.data",       FT_STRING,  BASE_NONE, NULL, 0x0,
180         "" }},
181
182     { &hf_ftp_response_code,
183       { "Response code",      "ftp.response.code",      FT_UINT8,   BASE_DEC, NULL, 0x0,
184         "" }},
185
186     { &hf_ftp_response_data,
187       { "Response data",      "ftp.reponse.data",       FT_STRING,  BASE_NONE, NULL, 0x0,
188         "" }}
189   };
190   static gint *ett[] = {
191     &ett_ftp,
192   };
193
194   proto_ftp = proto_register_protocol("File Transfer Protocol", "ftp");
195   proto_register_field_array(proto_ftp, hf, array_length(hf));
196   proto_register_subtree_array(ett, array_length(ett));
197 }