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