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