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