Use Ashok's IEEE-float-to-long code as the basis for
[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.36 2002/01/24 09:20:52 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 <epan/packet.h>
45 #include <epan/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 static dissector_handle_t tftp_handle;
59
60 #define UDP_PORT_TFTP    69
61
62 #define TFTP_RRQ        1
63 #define TFTP_WRQ        2
64 #define TFTP_DATA       3
65 #define TFTP_ACK        4
66 #define TFTP_ERROR      5
67 #define TFTP_OACK       6
68
69 static const value_string tftp_opcode_vals[] = {
70   { TFTP_RRQ,   "Read Request" },
71   { TFTP_WRQ,   "Write Request" },
72   { TFTP_DATA,  "Data Packet" },
73   { TFTP_ACK,   "Acknowledgement" },
74   { TFTP_ERROR, "Error Code" },
75   { TFTP_OACK,  "Option Acknowledgement" },
76   { 0,          NULL }
77 };
78
79 static const value_string tftp_error_code_vals[] = {
80   { 0, "Not defined" },
81   { 1, "File not found" },
82   { 2, "Access violation" },
83   { 3, "Disk full or allocation exceeded" },
84   { 4, "Illegal TFTP Operation" },
85   { 5, "Unknown transfer ID" },
86   { 6, "File already exists" },
87   { 7, "No such user" },
88   { 0, NULL }
89 };
90
91 static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
92
93 static void
94 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
95 {
96         proto_tree      *tftp_tree = NULL;
97         proto_item      *ti;
98         conversation_t  *conversation;
99         gint            offset = 0;
100         guint16         opcode;
101         u_int           i1;
102         guint16         error;
103
104         /*
105          * The first TFTP packet goes to the TFTP port; the second one
106          * comes from some *other* port, but goes back to the same
107          * IP address and port as the ones from which the first packet
108          * came; all subsequent packets go between those two IP addresses
109          * and ports.
110          *
111          * If this packet went to the TFTP port, we check to see if
112          * there's already a conversation with one address/port pair
113          * matching the source IP address and port of this packet,
114          * the other address matching the destination IP address of this
115          * packet, and any destination port.
116          *
117          * If not, we create one, with its address 1/port 1 pair being
118          * the source address/port of this packet, its address 2 being
119          * the destination address of this packet, and its port 2 being
120          * wildcarded, and give it the TFTP dissector as a dissector.
121          */
122         if (pinfo->destport == UDP_PORT_TFTP) {
123           conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
124                                            pinfo->srcport, 0, NO_PORT_B);
125           if (conversation == NULL) {
126             conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
127                                             pinfo->srcport, 0, NO_PORT2);
128             conversation_set_dissector(conversation, tftp_handle);
129           }
130         }
131
132         if (check_col(pinfo->cinfo, COL_PROTOCOL))
133                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");
134
135         opcode = tvb_get_ntohs(tvb, offset);
136
137         if (check_col(pinfo->cinfo, COL_INFO)) {
138
139           col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
140             val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
141
142         }
143
144         if (tree) {
145
146           ti = proto_tree_add_item(tree, proto_tftp, tvb, offset, -1, FALSE);
147           tftp_tree = proto_item_add_subtree(ti, ett_tftp);
148
149           proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
150                             offset, 2, opcode);
151         }
152         offset += 2;
153             
154         switch (opcode) {
155
156         case TFTP_RRQ:
157           i1 = tvb_strsize(tvb, offset);
158           if (tree) {
159             proto_tree_add_item(tftp_tree, hf_tftp_source_file,
160                             tvb, offset, i1, FALSE);
161           }
162           if (check_col(pinfo->cinfo, COL_INFO)) {
163             col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
164                             tvb_get_ptr(tvb, offset, i1));
165           }
166           offset += i1;
167
168           i1 = tvb_strsize(tvb, offset);
169           if (tree) {
170             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
171                             tvb, offset, i1, FALSE);
172           }
173           if (check_col(pinfo->cinfo, COL_INFO)) {
174             col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
175                             tvb_get_ptr(tvb, offset, i1));
176           }
177           offset += i1;
178
179           if (tree)
180             tftp_dissect_options(tvb, offset, tftp_tree);
181           break;
182
183         case TFTP_WRQ:
184           i1 = tvb_strsize(tvb, offset);
185           if (tree) {
186             proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
187                             tvb, offset, i1, FALSE);
188           }
189           if (check_col(pinfo->cinfo, COL_INFO)) {
190             col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
191                             tvb_get_ptr(tvb, offset, i1));
192           }
193           offset += i1;
194
195           i1 = tvb_strsize(tvb, offset);
196           if (tree) {
197             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
198                             tvb, offset, i1, FALSE);
199           }
200           if (check_col(pinfo->cinfo, COL_INFO)) {
201             col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
202                             tvb_get_ptr(tvb, offset, i1));
203           }
204           offset += i1;
205
206           if (tree)
207             tftp_dissect_options(tvb, offset, tftp_tree);
208           break;
209
210         case TFTP_DATA:
211           if (tree) {
212             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
213                             FALSE);
214           }
215           if (check_col(pinfo->cinfo, COL_INFO)) {
216             col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
217                             tvb_get_ntohs(tvb, offset));
218           }
219           offset += 2;
220
221           if (tree) {
222             proto_tree_add_text(tftp_tree, tvb, offset, -1,
223                 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
224           }
225           break;
226
227         case TFTP_ACK:
228           if (tree) {
229             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
230                             FALSE);
231           }
232           if (check_col(pinfo->cinfo, COL_INFO)) {
233             col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
234                             tvb_get_ntohs(tvb, offset));
235           }
236           break;
237
238         case TFTP_ERROR:
239           error = tvb_get_ntohs(tvb, offset);
240           if (tree) {
241             proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
242                             error);
243           }
244           if (check_col(pinfo->cinfo, COL_INFO)) {
245             col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
246                             val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));
247           }
248           offset += 2;
249
250           i1 = tvb_strsize(tvb, offset);
251           if (tree) {
252             proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
253                 i1, FALSE);
254           }
255           if (check_col(pinfo->cinfo, COL_INFO)) {
256             col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
257                             tvb_get_ptr(tvb, offset, i1));
258           }
259           break;
260
261         case TFTP_OACK:
262           if (tree)
263             tftp_dissect_options(tvb, offset, tftp_tree);
264           break;
265
266         default:
267           if (tree) {
268             proto_tree_add_text(tftp_tree, tvb, offset, -1,
269                 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
270           }
271           break;
272
273         }
274 }
275
276 static void
277 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
278 {
279         int option_len, value_len;
280         int value_offset;
281
282         while (tvb_offset_exists(tvb, offset)) {
283           option_len = tvb_strsize(tvb, offset);        /* length of option */
284           value_offset = offset + option_len;
285           value_len = tvb_strsize(tvb, value_offset);   /* length of value */
286           proto_tree_add_text(tree, tvb, offset, option_len+value_len,
287                   "Option: %s = %s",
288                   tvb_get_ptr(tvb, offset, option_len),
289                   tvb_get_ptr(tvb, value_offset, value_len));
290           offset += option_len + value_len;
291         }
292 }
293
294 void
295 proto_register_tftp(void)
296 {
297   static hf_register_info hf[] = {
298     { &hf_tftp_opcode,
299       { "Opcode",             "tftp.opcode",
300         FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
301         "TFTP message type", HFILL }},
302
303     { &hf_tftp_source_file,
304       { "Source File",        "tftp.source_file",
305         FT_STRINGZ, BASE_DEC, NULL, 0x0,
306         "TFTP source file name", HFILL }},
307
308     { &hf_tftp_destination_file,
309       { "DESTINATION File",   "tftp.destination_file",
310         FT_STRINGZ, BASE_DEC, NULL, 0x0,
311         "TFTP source file name", HFILL }},
312
313     { &hf_tftp_transfer_type,
314       { "Type",               "tftp.type",
315         FT_STRINGZ, BASE_DEC, NULL, 0x0,
316         "TFTP transfer type", HFILL }},
317
318     { &hf_tftp_blocknum,
319       { "Block",              "tftp.block",
320         FT_UINT16, BASE_DEC, NULL, 0x0,
321         "Block number", HFILL }},
322
323     { &hf_tftp_error_code,
324       { "Error code",         "tftp.error.code",
325         FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
326         "Error code in case of TFTP error message", HFILL }},
327
328     { &hf_tftp_error_string,
329       { "Error message",      "tftp.error.message",
330         FT_STRINGZ, BASE_DEC, NULL, 0x0,
331         "Error string in case of TFTP error message", HFILL }}
332   };
333   static gint *ett[] = {
334     &ett_tftp,
335   };
336
337   proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
338                                        "TFTP", "tftp");
339   proto_register_field_array(proto_tftp, hf, array_length(hf));
340   proto_register_subtree_array(ett, array_length(ett));
341
342   tftp_handle = create_dissector_handle(dissect_tftp, proto_tftp);
343 }
344
345 void
346 proto_reg_handoff_tftp(void)
347 {
348   dissector_add("udp.port", UDP_PORT_TFTP, tftp_handle);
349 }