Stuff to IPX socket 0x0455 (NetBIOS), and to sockets 0x0551 and 0x0553
[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.7 1999/08/24 17:26:11 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
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45
46 static int proto_ftp = -1;
47
48 void
49 dissect_ftp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
50 {
51         proto_tree      *ftp_tree, *ti;
52         gchar          rr[50], rd[1500];
53         int i1 = (u_char *)strchr(pd + offset, ' ') - (pd + offset); /* Where is that space */
54         int i2;
55         int max_data = pi.captured_len - offset;
56
57         memset(rr, '\0', sizeof(rr));
58         memset(rd, '\0', sizeof(rd));
59
60         if (i1 > 0) {
61
62           /* Hmmm, check if there was no space in there ... */
63
64           if (i1 > max_data) {
65
66             i1 = max_data;  /* Make things below work */
67             strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));
68
69           }
70           else {
71
72             strncpy(rr, pd + offset, MIN(i1, sizeof(rr) - 1));
73             i2 = ((u_char *)strchr(pd + offset + i1 + 1, '\r') - (pd + offset)) - i1 - 1;
74             strncpy(rd, pd + offset + i1 + 1, MIN(i2, sizeof(rd) - 1));
75           }
76         }
77         else { 
78
79           i1 = max_data;
80           strncpy(rr, pd + offset, MIN(max_data - 2, sizeof(rr) - 1));  /* Lazy, CRLF */
81
82         }
83
84         if (check_col(fd, COL_PROTOCOL))
85                 col_add_str(fd, COL_PROTOCOL, "FTP");
86
87         if (check_col(fd, COL_INFO)) {
88
89           col_add_fstr(fd, COL_INFO, "%s: %s %s", (pi.match_port == pi.destport)? "Request" : "Response", rr, rd);
90
91         }
92
93         if (tree) {
94
95           ti = proto_tree_add_item(tree, proto_ftp, offset, END_OF_FRAME, NULL);
96           ftp_tree = proto_item_add_subtree(ti, ETT_FTP);
97
98           if (pi.match_port == pi.destport) { /* Request */
99
100             proto_tree_add_text(ftp_tree, offset, i1, "Request: %s", rr);
101
102             proto_tree_add_text(ftp_tree, offset + i1 + 1, END_OF_FRAME, "Request Arg: %s", rd);
103
104           }
105           else {
106
107             proto_tree_add_text(ftp_tree, offset, i1, "Response: %s", rr);
108
109             proto_tree_add_text(ftp_tree, offset + i1 + 1, END_OF_FRAME, "Response Arg: %s", rd);
110           }
111
112         }
113 }
114
115 void
116 dissect_ftpdata(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
117 {
118         proto_tree      *ti;
119
120         if (check_col(fd, COL_PROTOCOL))
121                 col_add_str(fd, COL_PROTOCOL, "FTP DATA");
122
123         if (check_col(fd, COL_INFO)) {
124
125           col_add_fstr(fd, COL_INFO, "FTP Data ...");
126
127         }
128
129         if (tree) {
130
131           ti = proto_tree_add_text(tree, offset, END_OF_FRAME,
132                                 "File Transfer Protocol Data");
133
134         }
135 }
136
137 void
138 proto_register_ftp(void)
139 {
140 /*        static hf_register_info hf[] = {
141                 { &variable,
142                 { "Name",           "ftp.abbreviation", TYPE, VALS_POINTER }},
143         };*/
144
145         proto_ftp = proto_register_protocol("File Transfer Protocol", "ftp");
146  /*       proto_register_field_array(proto_ftp, hf, array_length(hf));*/
147 }