create_dissector_handle -> new_create_dissector_handle
[metze/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  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <epan/packet.h>
29 #include <epan/expert.h>
30
31 void proto_register_icp(void);
32 void proto_reg_handoff_icp(void);
33
34 static int proto_icp = -1;
35 static int hf_icp_length = -1;
36 static int hf_icp_opcode = -1;
37 static int hf_icp_version = -1;
38 static int hf_icp_request_nr = -1;
39
40 /* Generated from convert_proto_tree_add_text.pl */
41 static int hf_icp_url = -1;
42 static int hf_icp_rtt = -1;
43 static int hf_icp_object_data = -1;
44 static int hf_icp_requester_host_address = -1;
45 static int hf_icp_sender_host_ip_address = -1;
46 static int hf_icp_option_src_rtt = -1;
47 static int hf_icp_object_length = -1;
48 static int hf_icp_option_hit_obj = -1;
49
50 static gint ett_icp = -1;
51 static gint ett_icp_payload = -1;
52
53 /* Generated from convert_proto_tree_add_text.pl */
54 static expert_field ei_icp_fragmented_packet = EI_INIT;
55
56 #define UDP_PORT_ICP    3130
57
58 #define CODE_ICP_OP_QUERY 1
59 #define CODE_ICP_OP_INVALID 0
60 #define CODE_ICP_OP_HIT 2
61 #define CODE_ICP_OP_MISS 3
62 #define CODE_ICP_OP_ERR 4
63 #define CODE_ICP_OP_SEND 5
64 #define CODE_ICP_OP_SENDA 6
65 #define CODE_ICP_OP_DATABEG 7
66 #define CODE_ICP_OP_DATA 8
67 #define CODE_ICP_OP_DATAEND 9
68 #define CODE_ICP_OP_SECHO 10
69 #define CODE_ICP_OP_DECHO 11
70 #define CODE_ICP_OP_MISS_NOFETCH 21
71 #define CODE_ICP_OP_DENIED 22
72 #define CODE_ICP_OP_HIT_OBJ 23
73
74 static const value_string opcode_vals[] = {
75 { CODE_ICP_OP_INVALID ,      "ICP_INVALID" },
76 { CODE_ICP_OP_QUERY ,        "ICP_QUERY" },
77 { CODE_ICP_OP_HIT ,          "ICP_HIT" },
78 { CODE_ICP_OP_MISS ,         "ICP_MISS" },
79 { CODE_ICP_OP_ERR ,          "ICP_ERR" },
80 { CODE_ICP_OP_SEND,          "ICP_SEND" },
81 { CODE_ICP_OP_SENDA,         "ICP_SENDA"},
82 { CODE_ICP_OP_DATABEG,       "ICP_DATABEG"},
83 { CODE_ICP_OP_DATA,          "ICP_DATA"},
84 { CODE_ICP_OP_DATAEND,       "ICP_DATA_END"},
85 { CODE_ICP_OP_SECHO ,        "ICP_SECHO"},
86 { CODE_ICP_OP_DECHO ,        "ICP_DECHO"},
87 { CODE_ICP_OP_MISS_NOFETCH , "ICP_MISS_NOFETCH"},
88 { CODE_ICP_OP_DENIED ,       "ICP_DENIED"},
89 { CODE_ICP_OP_HIT_OBJ ,      "ICP_HIT_OBJ"},
90 { 0,     NULL}
91 };
92
93 static void dissect_icp_payload(tvbuff_t *tvb, packet_info *pinfo, int offset,
94                                 proto_tree *pload_tree, guint8 opcode)
95 {
96         gint stringlength;
97         guint16 objectlength;
98         proto_item* object_item;
99
100         switch(opcode)
101         {
102         case CODE_ICP_OP_QUERY:
103                 /* 4 byte requester host address */
104                 proto_tree_add_item(pload_tree, hf_icp_requester_host_address, tvb, offset, 4, ENC_BIG_ENDIAN);
105                 offset += 4;
106
107                 /* null terminated URL */
108                 stringlength = tvb_strsize(tvb, offset);
109                 proto_tree_add_item(pload_tree, hf_icp_url, tvb, offset, stringlength, ENC_ASCII|ENC_NA);
110                 break;
111
112         case CODE_ICP_OP_SECHO:
113         case CODE_ICP_OP_DECHO:
114         case CODE_ICP_OP_HIT:
115         case CODE_ICP_OP_MISS:
116         case CODE_ICP_OP_ERR:
117         case CODE_ICP_OP_MISS_NOFETCH:
118         case CODE_ICP_OP_DENIED:
119                 stringlength = tvb_strsize(tvb, offset);
120                 proto_tree_add_item(pload_tree, hf_icp_url, tvb, offset, stringlength, ENC_ASCII|ENC_NA);
121                 break;
122
123         case CODE_ICP_OP_HIT_OBJ:
124                 /* null terminated URL */
125                 stringlength = tvb_strsize(tvb, offset);
126                 proto_tree_add_item(pload_tree, hf_icp_url, tvb, offset, stringlength, ENC_ASCII|ENC_NA);
127                 offset += stringlength;
128
129                 /* 2 byte object size */
130                 /* object data not recommended by standard*/
131                 objectlength=tvb_get_ntohs(tvb, offset);
132                 proto_tree_add_item(pload_tree, hf_icp_object_length, tvb, offset, 2, ENC_BIG_ENDIAN);
133                 offset += 2;
134
135                 /* object data not recommended by standard*/
136                 object_item = proto_tree_add_item(pload_tree, hf_icp_object_data, tvb, offset, objectlength, ENC_NA);
137                 if (objectlength > tvb_reported_length_remaining(tvb, offset))
138                 {
139                         expert_add_info(pinfo, object_item, &ei_icp_fragmented_packet);
140                 }
141                 break;
142         default:
143                 break;
144         }
145 }
146
147 static int dissect_icp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
148 {
149         proto_tree *icp_tree , *payload_tree;
150         proto_item *ti;
151         guint8 opcode;
152         guint16 message_length;
153         guint32 request_number;
154         guint32 options;
155
156         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ICP");
157         col_clear(pinfo->cinfo, COL_INFO);
158
159         opcode=tvb_get_guint8(tvb, 0);
160         message_length=tvb_get_ntohs(tvb, 2);
161         request_number=tvb_get_ntohl(tvb, 4);
162
163         col_add_fstr(pinfo->cinfo,COL_INFO,"Opcode: %s (%u), Req Nr: %u",
164                      val_to_str_const(opcode, opcode_vals, "Unknown"), opcode,
165                      request_number);
166
167         ti = proto_tree_add_item(tree,proto_icp, tvb, 0, message_length, ENC_NA);
168         icp_tree = proto_item_add_subtree(ti, ett_icp);
169
170         if (tree)
171         {
172                 proto_tree_add_uint(icp_tree,hf_icp_opcode, tvb, 0, 1, opcode);
173
174                 proto_tree_add_item(icp_tree,hf_icp_version, tvb, 1, 1, ENC_BIG_ENDIAN);
175
176                 proto_tree_add_uint(icp_tree,hf_icp_length, tvb, 2, 2, message_length);
177
178                 proto_tree_add_uint(icp_tree,hf_icp_request_nr, tvb, 4, 4,
179                                     request_number);
180
181                 options=tvb_get_ntohl(tvb, 8);
182                 if ( (opcode == CODE_ICP_OP_QUERY) && ((options & 0x80000000 ) != 0) )
183                 {
184                         proto_tree_add_item(icp_tree, hf_icp_option_hit_obj, tvb, 8, 4, ENC_NA);
185                 }
186                 if ( (opcode == CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0) )
187                 {
188                         proto_tree_add_item(icp_tree, hf_icp_option_src_rtt, tvb, 8, 4, ENC_NA);
189                 }
190                 if ((opcode != CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0))
191                 {
192                         proto_tree_add_item(icp_tree, hf_icp_option_src_rtt, tvb, 8, 4, ENC_NA);
193             proto_tree_add_item(icp_tree, hf_icp_rtt, tvb, 12, 4, ENC_BIG_ENDIAN);
194                 }
195
196                 proto_tree_add_item(icp_tree, hf_icp_sender_host_ip_address, tvb, 16, 4, ENC_BIG_ENDIAN);
197         }
198
199         payload_tree = proto_tree_add_subtree(icp_tree, tvb,
200                                                       20, message_length - 20,
201                                                       ett_icp_payload, NULL, "Payload");
202         dissect_icp_payload(tvb, pinfo, 20, payload_tree, opcode);
203
204         return tvb_captured_length(tvb);
205 }
206
207 void
208 proto_register_icp(void)
209 {
210         static hf_register_info hf[] = {
211                 { &hf_icp_opcode,
212                   { "Opcode", "icp.opcode", FT_UINT8, BASE_HEX, VALS(opcode_vals),
213                     0x0, NULL, HFILL }},
214
215                 { &hf_icp_version,
216                   { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL,
217                     0x0, NULL, HFILL }},
218
219                 { &hf_icp_length,
220                   { "Length", "icp.length", FT_UINT16, BASE_DEC, NULL,
221                     0x0, NULL, HFILL }},
222
223                 { &hf_icp_request_nr,
224                   { "Request Number", "icp.nr", FT_UINT32, BASE_DEC, NULL,
225                     0x0, NULL, HFILL }},
226
227       { &hf_icp_requester_host_address, { "Requester Host Address", "icp.requester_host_address", FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
228       { &hf_icp_url, { "URL", "icp.url", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
229       { &hf_icp_object_length, { "Object length", "icp.object_length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
230       { &hf_icp_object_data, { "Object data", "icp.object_data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
231       { &hf_icp_option_hit_obj, { "Option: ICP_FLAG_HIT_OBJ", "icp.option.hit_obj", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
232       { &hf_icp_option_src_rtt, { "Option: ICP_FLAG_SRC_RTT", "icp.option.src_rtt", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
233       { &hf_icp_rtt, { "RTT", "icp.rtt", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
234       { &hf_icp_sender_host_ip_address, { "Sender Host IP address", "icp.sender_host_ip_address", FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
235         };
236         static gint *ett[] = {
237                 &ett_icp,
238                 &ett_icp_payload,
239         };
240
241         static ei_register_info ei[] = {
242                 { &ei_icp_fragmented_packet, { "icp.fragmented_packet", PI_PROTOCOL, PI_WARN, "Packet is fragmented", EXPFILL }},
243         };
244
245         expert_module_t* expert_icp;
246
247         proto_icp = proto_register_protocol("Internet Cache Protocol", "ICP", "icp");
248
249         proto_register_field_array(proto_icp, hf, array_length(hf));
250         proto_register_subtree_array(ett, array_length(ett));
251         expert_icp = expert_register_protocol(proto_icp);
252         expert_register_field_array(expert_icp, ei, array_length(ei));
253 }
254
255 void
256 proto_reg_handoff_icp(void)
257 {
258         dissector_handle_t icp_handle;
259
260         icp_handle = new_create_dissector_handle(dissect_icp, proto_icp);
261         dissector_add_uint("udp.port", UDP_PORT_ICP, icp_handle);
262 }
263
264 /*
265  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
266  *
267  * Local variables:
268  * c-basic-offset: 8
269  * tab-width: 8
270  * indent-tabs-mode: t
271  * End:
272  *
273  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
274  * :indentSize=8:tabSize=8:noTabs=false:
275  */