2 * Routines for ICP (internet cache protocol) packet disassembly
5 * $Id: packet-icp.c,v 1.4 1999/12/23 09:53:53 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 CODE_ICP_OP_QUERY 1
55 #define CODE_ICP_OP_INVALID 0
56 #define CODE_ICP_OP_HIT 2
57 #define CODE_ICP_OP_MISS 3
58 #define CODE_ICP_OP_ERR 4
59 #define CODE_ICP_OP_SEND 5
60 #define CODE_ICP_OP_SENDA 6
61 #define CODE_ICP_OP_DATABEG 7
62 #define CODE_ICP_OP_DATA 8
63 #define CODE_ICP_OP_DATAEND 9
64 #define CODE_ICP_OP_SECHO 10
65 #define CODE_ICP_OP_DECHO 11
66 #define CODE_ICP_OP_MISS_NOFETCH 21
67 #define CODE_ICP_OP_DENIED 22
68 #define CODE_ICP_OP_HIT_OBJ 23
70 static value_string opcode_vals[] = {
71 { CODE_ICP_OP_INVALID , "ICP_INVALID" },
72 { CODE_ICP_OP_QUERY , "ICP_QUERY" },
73 { CODE_ICP_OP_HIT , "ICP_HIT" },
74 { CODE_ICP_OP_MISS , "ICP_MISS" },
75 { CODE_ICP_OP_ERR , "ICP_ERR" },
76 { CODE_ICP_OP_SEND, "ICP_SEND" },
77 { CODE_ICP_OP_SENDA, "ICP_SENDA"},
78 { CODE_ICP_OP_DATABEG, "ICP_DATABEG"},
79 { CODE_ICP_OP_DATA, "ICP_DATA"},
80 { CODE_ICP_OP_DATAEND, "ICP_DATA_END"},
81 { CODE_ICP_OP_SECHO , "ICP_SECHO"},
82 { CODE_ICP_OP_DECHO , "ICP_DECHO"},
83 { CODE_ICP_OP_MISS_NOFETCH , "ICP_MISS_NOFETCH"},
84 { CODE_ICP_OP_DENIED , "ICP_DENIED"},
85 { CODE_ICP_OP_HIT_OBJ , "ICP_HIT_OBJ"},
91 typedef struct _e_icphdr
95 guint16 message_length;
96 guint32 request_number;
99 gchar sender_address[4];
102 static gchar textbuf[MAX_TEXTBUF_LENGTH];
103 void dissect_icp_payload( const u_char *pd, int offset,
104 frame_data *fd,proto_tree *pload_tree, e_icphdr *icph)
106 /* To Be Done take care of fragmentation*/
107 guint32 maxlength=fd->pkt_len-offset;
109 guint16 objectlength;
112 case CODE_ICP_OP_QUERY:
113 /* 4 byte requester host address */
114 proto_tree_add_text(pload_tree,offset,4,
115 "Requester Host Address %u.%u.%u.%u",
117 (guint8)pd[offset+1],
118 (guint8)pd[offset+2],
119 (guint8)pd[offset+3]);
121 /* null terminated URL */
122 for (i=0; i < maxlength &&
123 pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
125 textbuf[i]=pd[offset+4+i];
129 proto_tree_add_text(pload_tree, offset+4,i,
132 case CODE_ICP_OP_HIT_OBJ:
133 /* null terminated url */
134 for (i=0; i < maxlength &&
135 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
137 textbuf[i]=pd[offset+i];
141 proto_tree_add_text(pload_tree, offset,i,
143 /* 2 byte object size */
144 /* object data not recommended by standard*/
145 objectlength=pntohs(&pd[offset]);
146 proto_tree_add_text(pload_tree,offset,2,"object length: %u", objectlength);
147 /* object data not recommended by standard*/
148 proto_tree_add_text(pload_tree,offset+2, maxlength-2,"object data");
149 if (objectlength > maxlength-2)
151 proto_tree_add_text(pload_tree,offset,0,
152 "Packet is fragmented, rest of object is in next udp packet");
154 case CODE_ICP_OP_MISS:
155 case CODE_ICP_OP_HIT:
156 for (i=0; i < maxlength &&
157 pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
159 textbuf[i]=pd[offset+i];
163 proto_tree_add_text(pload_tree, offset,i,
166 /* check for fragmentation and add message if next part
167 of payload in next fragment*/
172 void dissect_icp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
174 proto_tree *icp_tree , *payload_tree;
175 proto_item *ti , *payloadtf;
180 /* TBD: check if this is a fragment or first part of udp packet */
181 icph.opcode=pd[offset];
182 icph.version=pd[offset+1];
183 icph.message_length=pntohs(&(pd[offset+2]));
184 icph.request_number=pntohl(&(pd[offset+4]));
185 memcpy(&icph.options,&pd[offset+8],sizeof(guint32));
186 memcpy(&icph.option_data,&pd[offset+12],sizeof(guint32));
187 memcpy(icph.sender_address,&pd[offset+16],4);
190 opcodestrval = match_strval(icph.opcode,opcode_vals);
192 if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
194 sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
195 (guint16)icph.opcode,icph.request_number);
197 if (check_col(fd, COL_PROTOCOL))
198 col_add_str(fd, COL_PROTOCOL, "ICP");
200 if (check_col(fd, COL_INFO))
202 col_add_fstr(fd,COL_INFO,textbuf);
208 ti = proto_tree_add_item(tree,proto_icp ,offset,fd->pkt_len-offset,
211 icp_tree = proto_item_add_subtree(ti, ett_icp);
212 proto_tree_add_item_format(icp_tree,hf_icp_opcode, offset, 1,
213 icph.opcode, "Opcode:0x%01x (%s)",icph.opcode, opcodestrval);
215 proto_tree_add_item_format(icp_tree,hf_icp_version, offset+1, 1,
216 icph.version,"Version: 0x%01x (%d)", icph.version, (int)icph.version);
218 proto_tree_add_item_format(icp_tree,hf_icp_length, offset+2, 2,
220 "Length: 0x%02x (%d)", icph.message_length,(int)icph.message_length);
221 proto_tree_add_item_format(icp_tree,hf_icp_request_nr, offset+4, 4,
223 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
225 if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
227 proto_tree_add_text(icp_tree,offset+8,4,
228 "option: ICP_FLAG_HIT_OBJ");
230 if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
232 proto_tree_add_text(icp_tree,offset+8,4,
233 "option:ICP_FLAG_SRC_RTT");
235 if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
237 proto_tree_add_text(icp_tree,offset+8,8,
238 "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
241 proto_tree_add_text(icp_tree,offset+16, 4,
242 "Sender Host IP address %u.%u.%u.%u",
243 (guint8)icph.sender_address[0],
244 (guint8)icph.sender_address[1],
245 (guint8)icph.sender_address[2],
246 (guint8)icph.sender_address[3]);
248 payloadtf = proto_tree_add_text(icp_tree,
249 offset+20,icph.message_length - 20,
251 payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
253 if (payload_tree !=NULL)
255 dissect_icp_payload( pd,
256 20+offset,fd,payload_tree,&icph);
261 proto_register_icp(void)
263 static hf_register_info hf[] = {
265 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
269 { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
273 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
276 { &hf_icp_request_nr,
277 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
280 static gint *ett[] = {
285 proto_icp = proto_register_protocol ("Internet Cache Protocol", "icp");
286 proto_register_field_array(proto_icp, hf, array_length(hf));
287 proto_register_subtree_array(ett, array_length(ett));