Small patch for the win32 makefile that improves the dependencies and
[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  *
5  * $Id: packet-icp.c,v 1.8 2000/05/31 05:07:06 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Peter Torvals
9  * Copyright 1999 Peter Torvals
10
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.
15  *
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.
20  *
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.
24  */
25
26 #define MAX_TEXTBUF_LENGTH 600
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h> 
41 #include <glib.h>
42 #include "packet.h"
43 #include "resolv.h"
44
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;
50
51 static gint ett_icp = -1;
52 static gint ett_icp_payload = -1;
53
54 #define UDP_PORT_ICP    3130
55
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
71
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"},
88 { 0,     NULL}
89 };
90
91
92
93 typedef struct _e_icphdr
94 {
95   guint8 opcode;
96   guint8 version;
97   guint16 message_length;
98   guint32 request_number;
99   guint32 options;
100   guint32 option_data;
101   gchar sender_address[4];
102 } e_icphdr;
103
104 static gchar textbuf[MAX_TEXTBUF_LENGTH];
105
106 static void dissect_icp_payload( const u_char *pd, int offset,
107         frame_data *fd,proto_tree *pload_tree, e_icphdr *icph)
108 {
109 /* To Be Done take care of fragmentation*/
110 guint32 maxlength=fd->pkt_len-offset;
111 guint32 i;
112 guint16 objectlength;
113   switch(icph->opcode)
114   {
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",
119                         (guint8)pd[offset],
120                         (guint8)pd[offset+1],
121                         (guint8)pd[offset+2],
122                         (guint8)pd[offset+3]);
123
124                 /* null terminated URL */
125                 for (i=0; i < maxlength && 
126                         pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
127                 {
128                         textbuf[i]=pd[offset+4+i];
129                 }
130                 textbuf[i]=0;
131                 i++;
132                 proto_tree_add_text(pload_tree, NullTVB, offset+4,i,
133                         "URL: %s", textbuf);
134                 break;
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++)
139                 {
140                         textbuf[i]=pd[offset+i];
141                 }
142                 textbuf[i]=0;
143                 i++;
144                 proto_tree_add_text(pload_tree, NullTVB, offset,i,
145                         "URL: %s", textbuf);
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)
153                 {
154                         proto_tree_add_text(pload_tree, NullTVB,offset,0,
155                                 "Packet is fragmented, rest of object is in next udp packet");
156                 }
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++)
161                 {
162                         textbuf[i]=pd[offset+i];
163                 }
164                 textbuf[i]=0;
165                 i++;
166                 proto_tree_add_text(pload_tree, NullTVB, offset,i,
167                         "URL: %s", textbuf);    
168         default: 
169                 /* check for fragmentation and add message if next part
170                          of payload in next fragment*/
171                 break;
172   }
173 }
174
175 static void dissect_icp(const u_char *pd, int offset, frame_data *fd,
176         proto_tree *tree)
177 {
178   proto_tree *icp_tree , *payload_tree;
179   proto_item *ti , *payloadtf;
180   e_icphdr icph;
181
182   gchar *opcodestrval;
183
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);
192
193
194   opcodestrval =  match_strval(icph.opcode,opcode_vals);
195
196   if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
197
198   sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
199                 (guint16)icph.opcode,icph.request_number);
200
201   if (check_col(fd, COL_PROTOCOL))
202         col_add_str(fd, COL_PROTOCOL, "ICP");
203
204   if (check_col(fd, COL_INFO))
205   {
206         col_add_fstr(fd,COL_INFO,textbuf);
207   }
208
209   if (tree)
210   {
211
212         ti = proto_tree_add_item(tree,proto_icp, NullTVB,offset,fd->pkt_len-offset, FALSE);
213
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);
217
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);
220
221         proto_tree_add_uint_format(icp_tree,hf_icp_length, NullTVB, offset+2, 2,
222                 icph.message_length,
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,
225                 icph.request_number,
226                 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
227                 
228         if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
229         {
230                 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
231                         "option: ICP_FLAG_HIT_OBJ");
232         }
233         if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
234         {
235                 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
236                         "option:ICP_FLAG_SRC_RTT");
237         }
238         if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
239         {
240                 proto_tree_add_text(icp_tree, NullTVB,offset+8,8,
241                         "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
242         }
243         
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]);
250
251         payloadtf = proto_tree_add_text(icp_tree, NullTVB,
252                         offset+20,icph.message_length - 20,
253                         "Payload");
254         payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
255
256         if (payload_tree !=NULL)
257         {
258                 dissect_icp_payload( pd,
259                                 20+offset,fd,payload_tree,&icph);
260         }
261   }
262 }
263 void
264 proto_register_icp(void)
265 {
266         static hf_register_info hf[] = {
267                 { &hf_icp_opcode,
268                 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
269                         "" }},
270
271                 { &hf_icp_version,
272                 { "Version",    "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
273                         "" }},
274
275                 { &hf_icp_length,
276                 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
277                         "" }},
278
279                 { &hf_icp_request_nr,
280                 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
281                         "" }},
282         };
283         static gint *ett[] = {
284                 &ett_icp,
285                 &ett_icp_payload,
286         };
287
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));
291 }
292
293 void
294 proto_reg_handoff_icp(void)
295 {
296         dissector_add("udp.port", UDP_PORT_ICP, dissect_icp);
297 }