Ensure that all value_string arrays end in {0, NULL}. Dissectors got away
[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.22 2001/01/03 06:55:34 guy Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@zing.org>
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 RRQ     1
61 #define WRQ     2
62 #define DATA    3
63 #define ACK     4
64 #define ERROR   5
65 #define OACK    6
66
67 static const value_string tftp_opcode_vals[] = {
68   { RRQ,   "Read Request" },
69   { WRQ,   "Write Request" },
70   { DATA,  "Data Packet" },
71   { ACK,   "Acknowledgement" },
72   { ERROR, "Error Code" },
73   { 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 the source IP address
114          * and port of this packet, the destination IP address of this
115          * packet, and any destination UDP port.  If not, we create
116          * one, with a wildcard UDP port, and give it the TFTP dissector
117          * as a dissector.
118          */
119         if (pinfo->destport == UDP_PORT_TFTP) {
120           conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
121                                            pinfo->srcport, 0, NO_DST_PORT);
122           if (conversation == NULL) {
123             conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
124                                             pinfo->srcport, 0, NULL,
125                                             NO_DST_PORT);
126             conversation_set_dissector(conversation, dissect_tftp);
127           }
128         }
129
130         if (check_col(pinfo->fd, COL_PROTOCOL))
131                 col_set_str(pinfo->fd, COL_PROTOCOL, "TFTP");
132
133         opcode = tvb_get_ntohs(tvb, offset);
134
135         if (check_col(pinfo->fd, COL_INFO)) {
136
137           col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
138             val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
139
140         }
141
142         if (tree) {
143
144           ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
145                             tvb_length_remaining(tvb, offset), FALSE);
146           tftp_tree = proto_item_add_subtree(ti, ett_tftp);
147
148           proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
149                             offset, 2, opcode);
150           offset += 2;
151             
152           switch (opcode) {
153           case RRQ:
154             i1 = tvb_strsize(tvb, offset);
155             proto_tree_add_item(tftp_tree, hf_tftp_source_file,
156                             tvb, offset, i1, FALSE);
157             offset += i1;
158
159             i1 = tvb_strsize(tvb, offset);
160             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
161                             tvb, offset, i1, FALSE);
162             offset += i1;
163
164             tftp_dissect_options(tvb, offset, tftp_tree);
165             break;
166           case WRQ:
167             i1 = tvb_strsize(tvb, offset);
168             proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
169                             tvb, offset, i1, FALSE);
170             offset += i1;
171
172             i1 = tvb_strsize(tvb, offset);
173             ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
174                             tvb, offset, i1, FALSE);
175             offset += i1;
176
177             tftp_dissect_options(tvb, offset, tftp_tree);
178             break;
179           case DATA:
180             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
181                             FALSE);
182             offset += 2;
183
184             proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
185                 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
186             break;
187           case ACK:
188             proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
189                             FALSE);
190             break;
191           case ERROR:
192             proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
193                             FALSE);
194             offset += 2;
195
196             i1 = tvb_strsize(tvb, offset);
197             proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
198                 i1, FALSE);
199             break;
200           case OACK:
201             tftp_dissect_options(tvb, offset, tftp_tree);
202             break;
203           default:
204             proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
205                 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
206             break;
207           }
208
209         }
210 }
211
212 static void
213 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
214 {
215         int option_len, value_len;
216         int value_offset;
217
218         while (tvb_offset_exists(tvb, offset)) {
219           option_len = tvb_strsize(tvb, offset);        /* length of option */
220           value_offset = offset + option_len;
221           value_len = tvb_strsize(tvb, value_offset);   /* length of value */
222           proto_tree_add_text(tree, tvb, offset, option_len+value_len,
223                   "Option: %s = %s",
224                   tvb_get_ptr(tvb, offset, option_len),
225                   tvb_get_ptr(tvb, value_offset, value_len));
226           offset += option_len + value_len;
227         }
228 }
229
230 void
231 proto_register_tftp(void)
232 {
233   static hf_register_info hf[] = {
234     { &hf_tftp_opcode,
235       { "Opcode",             "tftp.opcode",
236         FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
237         "TFTP message type" }},
238
239     { &hf_tftp_source_file,
240       { "Source File",        "tftp.source_file",
241         FT_STRINGZ, BASE_DEC, NULL, 0x0,
242         "TFTP source file name" }},
243
244     { &hf_tftp_destination_file,
245       { "DESTINATION File",   "tftp.destination_file",
246         FT_STRINGZ, BASE_DEC, NULL, 0x0,
247         "TFTP source file name" }},
248
249     { &hf_tftp_transfer_type,
250       { "Type",               "tftp.type",
251         FT_STRINGZ, BASE_DEC, NULL, 0x0,
252         "TFTP transfer type" }},
253
254     { &hf_tftp_blocknum,
255       { "Block",              "tftp.block",
256         FT_UINT16, BASE_DEC, NULL, 0x0,
257         "Block number" }},
258
259     { &hf_tftp_error_code,
260       { "Error code",         "tftp.error.code",
261         FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
262         "Error code in case of TFTP error message" }},
263
264     { &hf_tftp_error_string,
265       { "Error message",      "tftp.error.message",
266         FT_STRINGZ, BASE_DEC, NULL, 0x0,
267         "Error string in case of TFTP error message" }}
268   };
269   static gint *ett[] = {
270     &ett_tftp,
271   };
272
273   proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
274                                        "TFTP", "tftp");
275   proto_register_field_array(proto_tftp, hf, array_length(hf));
276   proto_register_subtree_array(ett, array_length(ett));
277 }
278
279 void
280 proto_reg_handoff_tftp(void)
281 {
282   dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp);
283 }