2 * Routines for ICP (internet cache protocol) packet disassembly
5 * $Id: packet-icp.c,v 1.8 2000/05/31 05:07:06 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>
45 static int proto_icp=-1;
46 static int hf_icp_length=-1;
47 static int hf_icp_opcode=-1;
48 static int hf_icp_version=-1;
49 static int hf_icp_request_nr=-1;
51 static gint ett_icp = -1;
52 static gint ett_icp_payload = -1;
54 #define UDP_PORT_ICP 3130
56 #define CODE_ICP_OP_QUERY 1
57 #define CODE_ICP_OP_INVALID 0
58 #define CODE_ICP_OP_HIT 2
59 #define CODE_ICP_OP_MISS 3
60 #define CODE_ICP_OP_ERR 4
61 #define CODE_ICP_OP_SEND 5
62 #define CODE_ICP_OP_SENDA 6
63 #define CODE_ICP_OP_DATABEG 7
64 #define CODE_ICP_OP_DATA 8
65 #define CODE_ICP_OP_DATAEND 9
66 #define CODE_ICP_OP_SECHO 10
67 #define CODE_ICP_OP_DECHO 11
68 #define CODE_ICP_OP_MISS_NOFETCH 21
69 #define CODE_ICP_OP_DENIED 22
70 #define CODE_ICP_OP_HIT_OBJ 23
72 static value_string opcode_vals[] = {
73 { CODE_ICP_OP_INVALID , "ICP_INVALID" },
74 { CODE_ICP_OP_QUERY , "ICP_QUERY" },
75 { CODE_ICP_OP_HIT , "ICP_HIT" },
76 { CODE_ICP_OP_MISS , "ICP_MISS" },
77 { CODE_ICP_OP_ERR , "ICP_ERR" },
78 { CODE_ICP_OP_SEND, "ICP_SEND" },
79 { CODE_ICP_OP_SENDA, "ICP_SENDA"},
80 { CODE_ICP_OP_DATABEG, "ICP_DATABEG"},
81 { CODE_ICP_OP_DATA, "ICP_DATA"},
82 { CODE_ICP_OP_DATAEND, "ICP_DATA_END"},
83 { CODE_ICP_OP_SECHO , "ICP_SECHO"},
84 { CODE_ICP_OP_DECHO , "ICP_DECHO"},
85 { CODE_ICP_OP_MISS_NOFETCH , "ICP_MISS_NOFETCH"},
86 { CODE_ICP_OP_DENIED , "ICP_DENIED"},
87 { CODE_ICP_OP_HIT_OBJ , "ICP_HIT_OBJ"},
93 typedef struct _e_icphdr
97 guint16 message_length;
98 guint32 request_number;
101 gchar sender_address[4];
104 static gchar textbuf[MAX_TEXTBUF_LENGTH];
106 static void dissect_icp_payload( const u_char *pd, int offset,
107 frame_data *fd,proto_tree *pload_tree, e_icphdr *icph)
109 /* To Be Done take care of fragmentation*/
110 guint32 maxlength=fd->pkt_len-offset;
112 guint16 objectlength;
115 case CODE_ICP_OP_QUERY:
116 /* 4 byte requester host address */
117 proto_tree_add_text(pload_tree, NullTVB,offset,4,
118 "Requester Host Address %u.%u.%u.%u",
120 (guint8)pd[offset+1],
121 (guint8)pd[offset+2],
122 (guint8)pd[offset+3]);
124 /* null terminated URL */
125 for (i=0; i < maxlength &&
126 pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
128 textbuf[i]=pd[offset+4+i];
132 proto_tree_add_text(pload_tree, NullTVB, offset+4,i,
135 case CODE_ICP_OP_HIT_OBJ:
136 /* null terminated url */
137 for (i=0; i < maxlength &&
138 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
140 textbuf[i]=pd[offset+i];
144 proto_tree_add_text(pload_tree, NullTVB, offset,i,
146 /* 2 byte object size */
147 /* object data not recommended by standard*/
148 objectlength=pntohs(&pd[offset]);
149 proto_tree_add_text(pload_tree, NullTVB,offset,2,"object length: %u", objectlength);
150 /* object data not recommended by standard*/
151 proto_tree_add_text(pload_tree, NullTVB,offset+2, maxlength-2,"object data");
152 if (objectlength > maxlength-2)
154 proto_tree_add_text(pload_tree, NullTVB,offset,0,
155 "Packet is fragmented, rest of object is in next udp packet");
157 case CODE_ICP_OP_MISS:
158 case CODE_ICP_OP_HIT:
159 for (i=0; i < maxlength &&
160 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
162 textbuf[i]=pd[offset+i];
166 proto_tree_add_text(pload_tree, NullTVB, offset,i,
169 /* check for fragmentation and add message if next part
170 of payload in next fragment*/
175 static void dissect_icp(const u_char *pd, int offset, frame_data *fd,
178 proto_tree *icp_tree , *payload_tree;
179 proto_item *ti , *payloadtf;
184 /* TBD: check if this is a fragment or first part of udp packet */
185 icph.opcode=pd[offset];
186 icph.version=pd[offset+1];
187 icph.message_length=pntohs(&(pd[offset+2]));
188 icph.request_number=pntohl(&(pd[offset+4]));
189 memcpy(&icph.options,&pd[offset+8],sizeof(guint32));
190 memcpy(&icph.option_data,&pd[offset+12],sizeof(guint32));
191 memcpy(icph.sender_address,&pd[offset+16],4);
194 opcodestrval = match_strval(icph.opcode,opcode_vals);
196 if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
198 sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
199 (guint16)icph.opcode,icph.request_number);
201 if (check_col(fd, COL_PROTOCOL))
202 col_add_str(fd, COL_PROTOCOL, "ICP");
204 if (check_col(fd, COL_INFO))
206 col_add_fstr(fd,COL_INFO,textbuf);
212 ti = proto_tree_add_item(tree,proto_icp, NullTVB,offset,fd->pkt_len-offset, FALSE);
214 icp_tree = proto_item_add_subtree(ti, ett_icp);
215 proto_tree_add_uint_format(icp_tree,hf_icp_opcode, NullTVB, offset, 1,
216 icph.opcode, "Opcode:0x%01x (%s)",icph.opcode, opcodestrval);
218 proto_tree_add_uint_format(icp_tree,hf_icp_version, NullTVB, offset+1, 1,
219 icph.version,"Version: 0x%01x (%d)", icph.version, (int)icph.version);
221 proto_tree_add_uint_format(icp_tree,hf_icp_length, NullTVB, offset+2, 2,
223 "Length: 0x%02x (%d)", icph.message_length,(int)icph.message_length);
224 proto_tree_add_uint_format(icp_tree,hf_icp_request_nr, NullTVB, offset+4, 4,
226 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
228 if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
230 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
231 "option: ICP_FLAG_HIT_OBJ");
233 if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
235 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
236 "option:ICP_FLAG_SRC_RTT");
238 if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
240 proto_tree_add_text(icp_tree, NullTVB,offset+8,8,
241 "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
244 proto_tree_add_text(icp_tree, NullTVB,offset+16, 4,
245 "Sender Host IP address %u.%u.%u.%u",
246 (guint8)icph.sender_address[0],
247 (guint8)icph.sender_address[1],
248 (guint8)icph.sender_address[2],
249 (guint8)icph.sender_address[3]);
251 payloadtf = proto_tree_add_text(icp_tree, NullTVB,
252 offset+20,icph.message_length - 20,
254 payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
256 if (payload_tree !=NULL)
258 dissect_icp_payload( pd,
259 20+offset,fd,payload_tree,&icph);
264 proto_register_icp(void)
266 static hf_register_info hf[] = {
268 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
272 { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
276 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
279 { &hf_icp_request_nr,
280 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
283 static gint *ett[] = {
288 proto_icp = proto_register_protocol ("Internet Cache Protocol", "icp");
289 proto_register_field_array(proto_icp, hf, array_length(hf));
290 proto_register_subtree_array(ett, array_length(ett));
294 proto_reg_handoff_icp(void)
296 dissector_add("udp.port", UDP_PORT_ICP, dissect_icp);