Update from Kojak to dissect ICQ login packets and text messages.
[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.9 1999/10/12 06:20:05 gram 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 void
56 dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
57 {
58         proto_tree      *ftp_tree, *ti;
59         gchar          rr[50], rd[1500];
60         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
61         int i2;
62         int max_data = pi.captured_len - offset;
63
64         memset(rr, '\0', sizeof(rr));
65         memset(rd, '\0', sizeof(rd));
66
67         if (i1 >= 0) {
68
69           /* Hmmm, check if there was no space in there ... */
70
71           if (i1 > max_data) {
72
73             i1 = max_data;  /* Make things below work */
74             strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
75
76           }
77           else {
78
79             strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
80             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
81             if (i2 > max_data - i1 - 1 || i2 < 0) {
82               i2 = max_data - i1 - 1;
83             }
84
85             strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
86           }
87         }
88         else { 
89
90           i1 = max_data;
91           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));  /* Lazy, CRLF */
92
93         }
94
95         if (check_col(fd, COL_PROTOCOL))
96                 col_add_str(fd, COL_PROTOCOL, "FTP");
97
98         if (check_col(fd, COL_INFO)) {
99
100           col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);
101
102         }
103
104         if (tree) {
105
106           ti = proto_tree_add_item(tree, proto_ftp, offset, END_OF_FRAME, NULL);
107           ftp_tree = proto_item_add_subtree(ti, ETT_FTP);
108
109           if (pi.match_port == pi.destport) { /* Request */
110
111             proto_tree_add_item_hidden(ftp_tree, hf_ftp_request,
112                                        offset, i1, TRUE);
113             proto_tree_add_item_hidden(ftp_tree, hf_ftp_response,
114                                        offset, i1, FALSE);
115             proto_tree_add_item_format(ftp_tree, hf_ftp_request_command,
116                                        offset, i1, rr, "Request: %s", rr);
117             proto_tree_add_item_format(ftp_tree, hf_ftp_request_data,
118                                        offset + i1 + 1, END_OF_FRAME, 
119                                        rd, "Request Arg: %s", rd);
120           }
121           else {
122
123             proto_tree_add_item_hidden(ftp_tree, hf_ftp_request,
124                                        offset, i1, FALSE);
125             proto_tree_add_item_hidden(ftp_tree, hf_ftp_response,
126                                        offset, i1, TRUE);
127             proto_tree_add_item_format(ftp_tree, hf_ftp_response_code, 
128                                        offset, i1, 
129                                        atoi(rr), "Response: %s", rr);
130             proto_tree_add_item_format(ftp_tree, hf_ftp_response_data,
131                                        offset + i1 + 1, END_OF_FRAME,
132                                        rd, "Response Arg: %s", rd);
133           }
134
135         }
136 }
137
138 void
139 dissect_ftpdata(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
140 {
141         proto_tree      *ti;
142
143         if (check_col(fd, COL_PROTOCOL))
144                 col_add_str(fd, COL_PROTOCOL, "FTP DATA");
145
146         if (check_col(fd, COL_INFO)) {
147
148           col_add_fstr(fd, COL_INFO, "FTP Data ...");
149
150         }
151
152         if (tree) {
153
154           ti = proto_tree_add_text(tree, offset, END_OF_FRAME,
155                                 "File Transfer Protocol Data");
156
157         }
158 }
159
160 void
161 proto_register_ftp(void)
162 {
163   static hf_register_info hf[] = {
164     { &hf_ftp_response,
165       { "Response",           "ftp.response",           FT_BOOLEAN, BASE_NONE, NULL, 0x0,
166         "" }},
167
168     { &hf_ftp_request,
169       { "Request",            "ftp.request",            FT_BOOLEAN, BASE_NONE, NULL, 0x0,
170         "" }},
171
172     { &hf_ftp_request_command,
173       { "Request command",    "ftp.request.command",    FT_STRING,  BASE_NONE, NULL, 0x0,
174         "" }},
175
176     { &hf_ftp_request_data,
177       { "Request data",       "ftp.request.data",       FT_STRING,  BASE_NONE, NULL, 0x0,
178         "" }},
179
180     { &hf_ftp_response_code,
181       { "Response code",      "ftp.response.code",      FT_UINT8,   BASE_DEC, NULL, 0x0,
182         "" }},
183
184     { &hf_ftp_response_data,
185       { "Response data",      "ftp.reponse.data",       FT_STRING,  BASE_NONE, NULL, 0x0,
186         "" }}
187   };
188
189   proto_ftp = proto_register_protocol("File Transfer Protocol", "ftp");
190   proto_register_field_array(proto_ftp, hf, array_length(hf));
191
192 }