Include <time.h> to declare "gmtime()".
[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.4 1999/12/23 09:53:53 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 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
69
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"},
86 { 0,     NULL}
87 };
88
89
90
91 typedef struct _e_icphdr
92 {
93   guint8 opcode;
94   guint8 version;
95   guint16 message_length;
96   guint32 request_number;
97   guint32 options;
98   guint32 option_data;
99   gchar sender_address[4];
100 } e_icphdr;
101
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)
105 {
106 /* To Be Done take care of fragmentation*/
107 guint32 maxlength=fd->pkt_len-offset;
108 guint32 i;
109 guint16 objectlength;
110   switch(icph->opcode)
111   {
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",
116                         (guint8)pd[offset],
117                         (guint8)pd[offset+1],
118                         (guint8)pd[offset+2],
119                         (guint8)pd[offset+3]);
120
121                 /* null terminated URL */
122                 for (i=0; i < maxlength && 
123                         pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
124                 {
125                         textbuf[i]=pd[offset+4+i];
126                 }
127                 textbuf[i]=0;
128                 i++;
129                 proto_tree_add_text(pload_tree, offset+4,i,
130                         "URL: %s", textbuf);
131                 break;
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++)
136                 {
137                         textbuf[i]=pd[offset+i];
138                 }
139                 textbuf[i]=0;
140                 i++;
141                 proto_tree_add_text(pload_tree, offset,i,
142                         "URL: %s", textbuf);
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)
150                 {
151                         proto_tree_add_text(pload_tree,offset,0,
152                                 "Packet is fragmented, rest of object is in next udp packet");
153                 }
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++)
158                 {
159                         textbuf[i]=pd[offset+i];
160                 }
161                 textbuf[i]=0;
162                 i++;
163                 proto_tree_add_text(pload_tree, offset,i,
164                         "URL: %s", textbuf);    
165         default: 
166                 /* check for fragmentation and add message if next part
167                          of payload in next fragment*/
168                 break;
169   }
170 }
171
172 void dissect_icp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
173 {
174   proto_tree *icp_tree , *payload_tree;
175   proto_item *ti , *payloadtf;
176   e_icphdr icph;
177
178   gchar *opcodestrval;
179
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);
188
189
190   opcodestrval =  match_strval(icph.opcode,opcode_vals);
191
192   if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
193
194   sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
195                 (guint16)icph.opcode,icph.request_number);
196
197   if (check_col(fd, COL_PROTOCOL))
198         col_add_str(fd, COL_PROTOCOL, "ICP");
199
200   if (check_col(fd, COL_INFO))
201   {
202         col_add_fstr(fd,COL_INFO,textbuf);
203   }
204
205   if (tree)
206   {
207
208         ti = proto_tree_add_item(tree,proto_icp ,offset,fd->pkt_len-offset,
209                         NULL);
210
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);
214
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);
217
218         proto_tree_add_item_format(icp_tree,hf_icp_length, offset+2, 2,
219                 icph.message_length,
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,
222                 icph.request_number,
223                 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
224                 
225         if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
226         {
227                 proto_tree_add_text(icp_tree,offset+8,4,
228                         "option: ICP_FLAG_HIT_OBJ");
229         }
230         if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
231         {
232                 proto_tree_add_text(icp_tree,offset+8,4,
233                         "option:ICP_FLAG_SRC_RTT");
234         }
235         if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
236         {
237                 proto_tree_add_text(icp_tree,offset+8,8,
238                         "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
239         }
240         
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]);
247
248         payloadtf = proto_tree_add_text(icp_tree,
249                         offset+20,icph.message_length - 20,
250                         "Payload");
251         payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
252
253         if (payload_tree !=NULL)
254         {
255                 dissect_icp_payload( pd,
256                                 20+offset,fd,payload_tree,&icph);
257         }
258   }
259 }
260 void
261 proto_register_icp(void)
262 {
263         static hf_register_info hf[] = {
264                 { &hf_icp_opcode,
265                 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
266                         "" }},
267
268                 { &hf_icp_version,
269                 { "Version",    "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
270                         "" }},
271
272                 { &hf_icp_length,
273                 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
274                         "" }},
275
276                 { &hf_icp_request_nr,
277                 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
278                         "" }},
279         };
280         static gint *ett[] = {
281                 &ett_icp,
282                 &ett_icp_payload,
283         };
284
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));
288 }