For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-icp.c
1 /* packet-icp.c
2  * Routines for ICP (internet cache protocol) packet disassembly
3  * RFC 2186 && RFC 2187
4  * By Peter Torvals
5  * Copyright 1999 Peter Torvals
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998
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 #define MAX_TEXTBUF_LENGTH 600
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include <epan/addr_resolv.h>
37
38 static int proto_icp=-1;
39 static int hf_icp_length=-1;
40 static int hf_icp_opcode=-1;
41 static int hf_icp_version=-1;
42 static int hf_icp_request_nr=-1;
43
44 static gint ett_icp = -1;
45 static gint ett_icp_payload = -1;
46
47 #define UDP_PORT_ICP    3130
48
49 #define CODE_ICP_OP_QUERY 1
50 #define CODE_ICP_OP_INVALID 0
51 #define CODE_ICP_OP_HIT 2
52 #define CODE_ICP_OP_MISS 3
53 #define CODE_ICP_OP_ERR 4
54 #define CODE_ICP_OP_SEND 5
55 #define CODE_ICP_OP_SENDA 6
56 #define CODE_ICP_OP_DATABEG 7
57 #define CODE_ICP_OP_DATA 8
58 #define CODE_ICP_OP_DATAEND 9
59 #define CODE_ICP_OP_SECHO 10
60 #define CODE_ICP_OP_DECHO 11
61 #define CODE_ICP_OP_MISS_NOFETCH 21
62 #define CODE_ICP_OP_DENIED 22
63 #define CODE_ICP_OP_HIT_OBJ 23
64
65 static const value_string opcode_vals[] = {
66 { CODE_ICP_OP_INVALID ,    "ICP_INVALID" },
67 { CODE_ICP_OP_QUERY ,    "ICP_QUERY" },
68 { CODE_ICP_OP_HIT ,    "ICP_HIT" },
69 { CODE_ICP_OP_MISS ,    "ICP_MISS" },
70 { CODE_ICP_OP_ERR ,    "ICP_ERR" },
71 { CODE_ICP_OP_SEND,    "ICP_SEND" },
72 { CODE_ICP_OP_SENDA, "ICP_SENDA"},
73 { CODE_ICP_OP_DATABEG, "ICP_DATABEG"},
74 { CODE_ICP_OP_DATA,    "ICP_DATA"},
75 { CODE_ICP_OP_DATAEND, "ICP_DATA_END"},
76 { CODE_ICP_OP_SECHO ,    "ICP_SECHO"},
77 { CODE_ICP_OP_DECHO ,    "ICP_DECHO"},
78 { CODE_ICP_OP_MISS_NOFETCH ,    "ICP_MISS_NOFETCH"},
79 { CODE_ICP_OP_DENIED ,    "ICP_DENIED"},
80 { CODE_ICP_OP_HIT_OBJ ,    "ICP_HIT_OBJ"},
81 { 0,     NULL}
82 };
83
84 static void dissect_icp_payload(tvbuff_t *tvb, int offset,
85         proto_tree *pload_tree, guint8 opcode)
86 {
87   gint stringlength;
88   guint16 objectlength;
89
90   switch(opcode)
91   {
92         case CODE_ICP_OP_QUERY:
93                 /* 4 byte requester host address */
94                 proto_tree_add_text(pload_tree, tvb,offset,4,
95                         "Requester Host Address %s",
96                         tvb_ip_to_str(tvb, offset));
97                 offset += 4;
98
99                 /* null terminated URL */
100                 stringlength = tvb_strsize(tvb, offset);
101                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
102                         "URL: %s", tvb_get_ephemeral_string(tvb, offset, stringlength));
103                 break;
104
105         case CODE_ICP_OP_SECHO:
106         case CODE_ICP_OP_DECHO:
107         case CODE_ICP_OP_HIT:
108         case CODE_ICP_OP_MISS:
109         case CODE_ICP_OP_ERR:
110         case CODE_ICP_OP_MISS_NOFETCH:
111         case CODE_ICP_OP_DENIED:
112                 stringlength = tvb_strsize(tvb, offset);
113                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
114                         "URL: %s", tvb_get_ephemeral_string(tvb, offset, stringlength));
115                 break;
116
117         case CODE_ICP_OP_HIT_OBJ:
118                 /* null terminated URL */
119                 stringlength = tvb_strsize(tvb, offset);
120                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
121                         "URL: %s", tvb_get_ephemeral_string(tvb, offset, stringlength));
122                 offset += stringlength;
123
124                 /* 2 byte object size */
125                 /* object data not recommended by standard*/
126                 objectlength=tvb_get_ntohs(tvb, offset);
127                 proto_tree_add_text(pload_tree, tvb,offset,2,"Object length: %u", objectlength);
128                 offset += 2;
129
130                 /* object data not recommended by standard*/
131                 proto_tree_add_text(pload_tree, tvb,offset,objectlength,"Object data");
132                 if (objectlength > tvb_reported_length_remaining(tvb, offset))
133                 {
134                         proto_tree_add_text(pload_tree, tvb,offset,0,
135                                 "Packet is fragmented, rest of object is in next udp packet");
136                 }
137                 break;
138         default:
139                 break;
140   }
141 }
142
143 static void dissect_icp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
144 {
145   proto_tree *icp_tree , *payload_tree;
146   proto_item *ti , *payloadtf;
147   guint8 opcode;
148   guint16 message_length;
149   guint32 request_number;
150   guint32 options;
151   guint32 option_data;
152
153   col_set_str(pinfo->cinfo, COL_PROTOCOL, "ICP");
154   col_clear(pinfo->cinfo, COL_INFO);
155
156   opcode=tvb_get_guint8(tvb, 0);
157   message_length=tvb_get_ntohs(tvb, 2);
158   request_number=tvb_get_ntohl(tvb, 4);
159
160   if (check_col(pinfo->cinfo, COL_INFO))
161   {
162         col_add_fstr(pinfo->cinfo,COL_INFO,"Opcode: %s (%u), Req Nr: %u",
163                 val_to_str(opcode, opcode_vals, "Unknown"), opcode,
164                 request_number);
165   }
166
167   if (tree)
168   {
169
170         ti = proto_tree_add_item(tree,proto_icp, tvb, 0, message_length, ENC_NA);
171         icp_tree = proto_item_add_subtree(ti, ett_icp);
172
173         proto_tree_add_uint(icp_tree,hf_icp_opcode, tvb, 0, 1, opcode);
174
175         proto_tree_add_item(icp_tree,hf_icp_version, tvb, 1, 1, ENC_BIG_ENDIAN);
176
177         proto_tree_add_uint(icp_tree,hf_icp_length, tvb, 2, 2, message_length);
178
179         proto_tree_add_uint(icp_tree,hf_icp_request_nr, tvb, 4, 4,
180                 request_number);
181
182         options=tvb_get_ntohl(tvb, 8);
183         if ( (opcode == CODE_ICP_OP_QUERY) && ((options & 0x80000000 ) != 0) )
184         {
185                 proto_tree_add_text(icp_tree, tvb,8,4,
186                         "option: ICP_FLAG_HIT_OBJ");
187         }
188         if ( (opcode == CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0) )
189         {
190                 proto_tree_add_text(icp_tree, tvb,8,4,
191                         "option:ICP_FLAG_SRC_RTT");
192         }
193         if ((opcode != CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0))
194         {
195                 option_data=tvb_get_ntohl(tvb, 12);
196                 proto_tree_add_text(icp_tree, tvb,8,8,
197                         "option: ICP_FLAG_SCR_RTT RTT=%u",
198                         option_data & 0x0000ffff);
199         }
200
201         proto_tree_add_text(icp_tree, tvb, 16, 4,
202                         "Sender Host IP address %s",
203                         tvb_ip_to_str(tvb, 16));
204
205         payloadtf = proto_tree_add_text(icp_tree, tvb,
206                         20, message_length - 20,
207                         "Payload");
208         payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
209         dissect_icp_payload(tvb, 20, payload_tree, opcode);
210   }
211 }
212 void
213 proto_register_icp(void)
214 {
215         static hf_register_info hf[] = {
216                 { &hf_icp_opcode,
217                 { "Opcode", "icp.opcode", FT_UINT8, BASE_HEX, VALS(opcode_vals),
218                         0x0, NULL, HFILL }},
219
220                 { &hf_icp_version,
221                 { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL,
222                         0x0, NULL, HFILL }},
223
224                 { &hf_icp_length,
225                 { "Length", "icp.length", FT_UINT16, BASE_DEC, NULL,
226                         0x0, NULL, HFILL }},
227
228                 { &hf_icp_request_nr,
229                 { "Request Number", "icp.nr", FT_UINT32, BASE_DEC, NULL,
230                         0x0, NULL, HFILL }},
231         };
232         static gint *ett[] = {
233                 &ett_icp,
234                 &ett_icp_payload,
235         };
236
237         proto_icp = proto_register_protocol("Internet Cache Protocol",
238             "ICP", "icp");
239         proto_register_field_array(proto_icp, hf, array_length(hf));
240         proto_register_subtree_array(ett, array_length(ett));
241 }
242
243 void
244 proto_reg_handoff_icp(void)
245 {
246         dissector_handle_t icp_handle;
247
248         icp_handle = create_dissector_handle(dissect_icp, proto_icp);
249         dissector_add_uint("udp.port", UDP_PORT_ICP, icp_handle);
250 }