From Mike Frisch: Win32 systems don't have "strptime()", so we need to
[obnox/wireshark/wip.git] / packet-tftp.c
1 /* packet-tftp.c
2  * Routines for tftp packet dissection
3  *
4  * Richard Sharpe <rsharpe@ns.aus.com>
5  * Craig Newell <CraigN@cheque.uq.edu.au>
6  *      RFC2347 TFTP Option Extension
7  *
8  * $Id: packet-tftp.c,v 1.27 2001/09/03 10:33:07 guy Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * Copied from packet-bootp.c
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * 
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
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 <glib.h>
44 #include "packet.h"
45 #include "conversation.h"
46
47 static int proto_tftp = -1;
48 static int hf_tftp_opcode = -1;
49 static int hf_tftp_source_file = -1;
50 static int hf_tftp_destination_file = -1;
51 static int hf_tftp_transfer_type = -1;
52 static int hf_tftp_blocknum = -1;
53 static int hf_tftp_error_code = -1;
54 static int hf_tftp_error_string = -1;
55
56 static gint ett_tftp = -1;
57
58 #define UDP_PORT_TFTP    69
59
60 #define TFTP_RRQ        1
61 #define TFTP_WRQ        2
62 #define TFTP_DATA       3
63 #define TFTP_ACK        4
64 #define TFTP_ERROR      5
65 #define TFTP_OACK       6
66
67 static const value_string tftp_opcode_vals[] = {
68   { TFTP_RRQ,   "Read Request" },
69   { TFTP_WRQ,   "Write Request" },
70   { TFTP_DATA,  "Data Packet" },
71   { TFTP_ACK,   "Acknowledgement" },
72   { TFTP_ERROR, "Error Code" },
73   { TFTP_OACK,  "Option Acknowledgement" },
74   { 0,          NULL }
75 };
76
77 static const value_string tftp_error_code_vals[] = {
78   { 0, "Not defined" },
79   { 1, "File not found" },
80   { 2, "Access violation" },
81   { 3, "Disk full or allocation exceeded" },
82   { 4, "Illegal TFTP Operation" },
83   { 5, "Unknown transfer ID" },
84   { 6, "File already exists" },
85   { 7, "No such user" },
86   { 0, NULL }
87 };
88
89 static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
90
91 static void
92 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
93 {
94         proto_tree      *tftp_tree;
95         proto_item      *ti;
96         conversation_t  *conversation;
97         gint            offset = 0;
98         guint16         opcode;
99         u_int           i1;
100
101         CHECK_DISPLAY_AS_DATA(proto_tftp, tvb, pinfo, tree);
102
103         pinfo->current_proto = "TFTP";
104
105         /*
106          * The first TFTP packet goes to the TFTP port; the second one
107          * comes from some *other* port, but goes back to the same
108          * IP address and port as the ones from which the first packet
109          * came; all subsequent packets go between those two IP addresses
110          * and ports.
111          *
112          * If this packet went to the TFTP port, we check to see if
113          * there's already a conversation with one address/port pair
114          * matching the source IP address and port of this packet,
115          * the other address matching the destination IP address of this
116          * packet, and any destination port.
117          *
118          * If not, we create one, with its address 1/port 1 pair being
119          * the source address/port of this packet, its address 2 being
120          * the destination address of this packet, and its port 2 being
121          * wildcarded, and give it the TFTP dissector as a dissector.
122          */
123         if (pinfo->destport == UDP_PORT_TFTP) {
124           conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
125                                            pinfo->srcport, 0, NO_PORT_B);
126           if (conversation == NULL) {
127             conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
128                                             pinfo->srcport, 0, NO_PORT2);
129             conversation_set_dissector(conversation, dissect_tftp);
130           }
131         }
132
133         if (check_col(pinfo->fd, COL_PROTOCOL))
134                 col_set_str(pinfo->fd, COL_PROTOCOL, "TFTP");
135
136         opcode = tvb_get_ntohs(tvb, offset);
137
138         if (check_col(pinfo->fd, COL_INFO)) {
139
140           col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
141             val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
142
143         }
144
145         if (tree) {
146
147           ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
148                             tvb_length_remaining(tvb, offset), FALSE);
149           tftp_tree = proto_item_add_subtree(ti, ett_tftp);
150
151           proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
152                             offset, 2, opcode);
153           offset += 2;
154             
155           switch (opcode) {
156           case TFTP_RRQ:
157             i1 = tvb_strsize(tvb, offset);
158             proto_tree_add_item(tftp_tree, hf_tftp_source_file,
159                             tvb, offset, i1, FALSE);
160             offset += i1;
161
162             i1 = tvb_strsize(tvb, offset);
163             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
164                             tvb, offset, i1, FALSE);
165             offset += i1;
166
167             tftp_dissect_options(tvb, offset, tftp_tree);
168             break;
169           case TFTP_WRQ:
170             i1 = tvb_strsize(tvb, offset);
171             proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
172                             tvb, offset, i1, FALSE);
173             offset += i1;
174
175             i1 = tvb_strsize(tvb, offset);
176             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
177                             tvb, offset, i1, FALSE);
178             offset += i1;
179
180             tftp_dissect_options(tvb, offset, tftp_tree);
181             break;
182           case TFTP_DATA:
183             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
184                             FALSE);
185             offset += 2;
186
187             proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
188                 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
189             break;
190           case TFTP_ACK:
191             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
192                             FALSE);
193             break;
194           case TFTP_ERROR:
195             proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
196                             FALSE);
197             offset += 2;
198
199             i1 = tvb_strsize(tvb, offset);
200             proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
201                 i1, FALSE);
202             break;
203           case TFTP_OACK:
204             tftp_dissect_options(tvb, offset, tftp_tree);
205             break;
206           default:
207             proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
208                 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
209             break;
210           }
211
212         }
213 }
214
215 static void
216 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
217 {
218         int option_len, value_len;
219         int value_offset;
220
221         while (tvb_offset_exists(tvb, offset)) {
222           option_len = tvb_strsize(tvb, offset);        /* length of option */
223           value_offset = offset + option_len;
224           value_len = tvb_strsize(tvb, value_offset);   /* length of value */
225           proto_tree_add_text(tree, tvb, offset, option_len+value_len,
226                   "Option: %s = %s",
227                   tvb_get_ptr(tvb, offset, option_len),
228                   tvb_get_ptr(tvb, value_offset, value_len));
229           offset += option_len + value_len;
230         }
231 }
232
233 void
234 proto_register_tftp(void)
235 {
236   static hf_register_info hf[] = {
237     { &hf_tftp_opcode,
238       { "Opcode",             "tftp.opcode",
239         FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
240         "TFTP message type", HFILL }},
241
242     { &hf_tftp_source_file,
243       { "Source File",        "tftp.source_file",
244         FT_STRINGZ, BASE_DEC, NULL, 0x0,
245         "TFTP source file name", HFILL }},
246
247     { &hf_tftp_destination_file,
248       { "DESTINATION File",   "tftp.destination_file",
249         FT_STRINGZ, BASE_DEC, NULL, 0x0,
250         "TFTP source file name", HFILL }},
251
252     { &hf_tftp_transfer_type,
253       { "Type",               "tftp.type",
254         FT_STRINGZ, BASE_DEC, NULL, 0x0,
255         "TFTP transfer type", HFILL }},
256
257     { &hf_tftp_blocknum,
258       { "Block",              "tftp.block",
259         FT_UINT16, BASE_DEC, NULL, 0x0,
260         "Block number", HFILL }},
261
262     { &hf_tftp_error_code,
263       { "Error code",         "tftp.error.code",
264         FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
265         "Error code in case of TFTP error message", HFILL }},
266
267     { &hf_tftp_error_string,
268       { "Error message",      "tftp.error.message",
269         FT_STRINGZ, BASE_DEC, NULL, 0x0,
270         "Error string in case of TFTP error message", HFILL }}
271   };
272   static gint *ett[] = {
273     &ett_tftp,
274   };
275
276   proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
277                                        "TFTP", "tftp");
278   proto_register_field_array(proto_tftp, hf, array_length(hf));
279   proto_register_subtree_array(ett, array_length(ett));
280 }
281
282 void
283 proto_reg_handoff_tftp(void)
284 {
285   dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp, proto_tftp);
286 }