2 * Routines for ICP (internet cache protocol) packet disassembly
5 * $Id: packet-icp.c,v 1.13 2000/11/19 08:53:58 guy Exp $
7 * Ethereal - Network traffic analyzer
9 * Copyright 1999 Peter Torvals
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #define MAX_TEXTBUF_LENGTH 600
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
46 static int proto_icp=-1;
47 static int hf_icp_length=-1;
48 static int hf_icp_opcode=-1;
49 static int hf_icp_version=-1;
50 static int hf_icp_request_nr=-1;
52 static gint ett_icp = -1;
53 static gint ett_icp_payload = -1;
55 #define UDP_PORT_ICP 3130
57 #define CODE_ICP_OP_QUERY 1
58 #define CODE_ICP_OP_INVALID 0
59 #define CODE_ICP_OP_HIT 2
60 #define CODE_ICP_OP_MISS 3
61 #define CODE_ICP_OP_ERR 4
62 #define CODE_ICP_OP_SEND 5
63 #define CODE_ICP_OP_SENDA 6
64 #define CODE_ICP_OP_DATABEG 7
65 #define CODE_ICP_OP_DATA 8
66 #define CODE_ICP_OP_DATAEND 9
67 #define CODE_ICP_OP_SECHO 10
68 #define CODE_ICP_OP_DECHO 11
69 #define CODE_ICP_OP_MISS_NOFETCH 21
70 #define CODE_ICP_OP_DENIED 22
71 #define CODE_ICP_OP_HIT_OBJ 23
73 static value_string opcode_vals[] = {
74 { CODE_ICP_OP_INVALID , "ICP_INVALID" },
75 { CODE_ICP_OP_QUERY , "ICP_QUERY" },
76 { CODE_ICP_OP_HIT , "ICP_HIT" },
77 { CODE_ICP_OP_MISS , "ICP_MISS" },
78 { CODE_ICP_OP_ERR , "ICP_ERR" },
79 { CODE_ICP_OP_SEND, "ICP_SEND" },
80 { CODE_ICP_OP_SENDA, "ICP_SENDA"},
81 { CODE_ICP_OP_DATABEG, "ICP_DATABEG"},
82 { CODE_ICP_OP_DATA, "ICP_DATA"},
83 { CODE_ICP_OP_DATAEND, "ICP_DATA_END"},
84 { CODE_ICP_OP_SECHO , "ICP_SECHO"},
85 { CODE_ICP_OP_DECHO , "ICP_DECHO"},
86 { CODE_ICP_OP_MISS_NOFETCH , "ICP_MISS_NOFETCH"},
87 { CODE_ICP_OP_DENIED , "ICP_DENIED"},
88 { CODE_ICP_OP_HIT_OBJ , "ICP_HIT_OBJ"},
94 typedef struct _e_icphdr
98 guint16 message_length;
99 guint32 request_number;
102 gchar sender_address[4];
105 static gchar textbuf[MAX_TEXTBUF_LENGTH];
107 static void dissect_icp_payload( const u_char *pd, int offset,
108 frame_data *fd,proto_tree *pload_tree, e_icphdr *icph)
110 /* To Be Done take care of fragmentation*/
111 guint32 maxlength=END_OF_FRAME;
113 guint16 objectlength;
116 case CODE_ICP_OP_QUERY:
117 /* 4 byte requester host address */
118 proto_tree_add_text(pload_tree, NullTVB,offset,4,
119 "Requester Host Address %u.%u.%u.%u",
121 (guint8)pd[offset+1],
122 (guint8)pd[offset+2],
123 (guint8)pd[offset+3]);
125 /* null terminated URL */
126 for (i=0; i < maxlength &&
127 pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
129 textbuf[i]=pd[offset+4+i];
133 proto_tree_add_text(pload_tree, NullTVB, offset+4,i,
136 case CODE_ICP_OP_HIT_OBJ:
137 /* null terminated url */
138 for (i=0; i < maxlength &&
139 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
141 textbuf[i]=pd[offset+i];
145 proto_tree_add_text(pload_tree, NullTVB, offset,i,
147 /* 2 byte object size */
148 /* object data not recommended by standard*/
149 objectlength=pntohs(&pd[offset]);
150 proto_tree_add_text(pload_tree, NullTVB,offset,2,"object length: %u", objectlength);
151 /* object data not recommended by standard*/
152 proto_tree_add_text(pload_tree, NullTVB,offset+2, maxlength-2,"object data");
153 if (objectlength > maxlength-2)
155 proto_tree_add_text(pload_tree, NullTVB,offset,0,
156 "Packet is fragmented, rest of object is in next udp packet");
158 case CODE_ICP_OP_MISS:
159 case CODE_ICP_OP_HIT:
160 for (i=0; i < maxlength &&
161 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
163 textbuf[i]=pd[offset+i];
167 proto_tree_add_text(pload_tree, NullTVB, offset,i,
170 /* check for fragmentation and add message if next part
171 of payload in next fragment*/
176 static void dissect_icp(const u_char *pd, int offset, frame_data *fd,
179 proto_tree *icp_tree , *payload_tree;
180 proto_item *ti , *payloadtf;
185 OLD_CHECK_DISPLAY_AS_DATA(proto_icp, pd, offset, fd, tree);
187 /* TBD: check if this is a fragment or first part of udp packet */
188 icph.opcode=pd[offset];
189 icph.version=pd[offset+1];
190 icph.message_length=pntohs(&(pd[offset+2]));
191 icph.request_number=pntohl(&(pd[offset+4]));
192 memcpy(&icph.options,&pd[offset+8],sizeof(guint32));
193 memcpy(&icph.option_data,&pd[offset+12],sizeof(guint32));
194 memcpy(icph.sender_address,&pd[offset+16],4);
197 opcodestrval = match_strval(icph.opcode,opcode_vals);
199 if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
201 sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
202 (guint16)icph.opcode,icph.request_number);
204 if (check_col(fd, COL_PROTOCOL))
205 col_set_str(fd, COL_PROTOCOL, "ICP");
207 if (check_col(fd, COL_INFO))
209 col_add_fstr(fd,COL_INFO,textbuf);
215 ti = proto_tree_add_item(tree,proto_icp, NullTVB,offset,END_OF_FRAME, FALSE);
217 icp_tree = proto_item_add_subtree(ti, ett_icp);
218 proto_tree_add_uint_format(icp_tree,hf_icp_opcode, NullTVB, offset, 1,
219 icph.opcode, "Opcode:0x%01x (%s)",icph.opcode, opcodestrval);
221 proto_tree_add_uint_format(icp_tree,hf_icp_version, NullTVB, offset+1, 1,
222 icph.version,"Version: 0x%01x (%d)", icph.version, (int)icph.version);
224 proto_tree_add_uint_format(icp_tree,hf_icp_length, NullTVB, offset+2, 2,
226 "Length: 0x%02x (%d)", icph.message_length,(int)icph.message_length);
227 proto_tree_add_uint_format(icp_tree,hf_icp_request_nr, NullTVB, offset+4, 4,
229 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
231 if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
233 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
234 "option: ICP_FLAG_HIT_OBJ");
236 if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
238 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
239 "option:ICP_FLAG_SRC_RTT");
241 if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
243 proto_tree_add_text(icp_tree, NullTVB,offset+8,8,
244 "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
247 proto_tree_add_text(icp_tree, NullTVB,offset+16, 4,
248 "Sender Host IP address %u.%u.%u.%u",
249 (guint8)icph.sender_address[0],
250 (guint8)icph.sender_address[1],
251 (guint8)icph.sender_address[2],
252 (guint8)icph.sender_address[3]);
254 payloadtf = proto_tree_add_text(icp_tree, NullTVB,
255 offset+20,icph.message_length - 20,
257 payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
259 if (payload_tree !=NULL)
261 dissect_icp_payload( pd,
262 20+offset,fd,payload_tree,&icph);
267 proto_register_icp(void)
269 static hf_register_info hf[] = {
271 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
275 { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
279 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
282 { &hf_icp_request_nr,
283 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
286 static gint *ett[] = {
291 proto_icp = proto_register_protocol ("Internet Cache Protocol", "icp");
292 proto_register_field_array(proto_icp, hf, array_length(hf));
293 proto_register_subtree_array(ett, array_length(ett));
297 proto_reg_handoff_icp(void)
299 old_dissector_add("udp.port", UDP_PORT_ICP, dissect_icp);