Register the WSP dissector, make it static, and have the WTP dissector
[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.14 2001/01/03 06:55:28 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 <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "resolv.h"
45
46 static int proto_icp=-1;
47 static int hf_icp_length=-1;
48 static int hf_icp_opcode=-1;
49 static int hf_icp_version=-1;
50 static int hf_icp_request_nr=-1;
51
52 static gint ett_icp = -1;
53 static gint ett_icp_payload = -1;
54
55 #define UDP_PORT_ICP    3130
56
57 #define CODE_ICP_OP_QUERY 1
58 #define CODE_ICP_OP_INVALID 0
59 #define CODE_ICP_OP_HIT 2
60 #define CODE_ICP_OP_MISS 3
61 #define CODE_ICP_OP_ERR 4
62 #define CODE_ICP_OP_SEND 5
63 #define CODE_ICP_OP_SENDA 6
64 #define CODE_ICP_OP_DATABEG 7
65 #define CODE_ICP_OP_DATA 8
66 #define CODE_ICP_OP_DATAEND 9
67 #define CODE_ICP_OP_SECHO 10
68 #define CODE_ICP_OP_DECHO 11
69 #define CODE_ICP_OP_MISS_NOFETCH 21
70 #define CODE_ICP_OP_DENIED 22
71 #define CODE_ICP_OP_HIT_OBJ 23
72
73 static value_string opcode_vals[] = {
74 { CODE_ICP_OP_INVALID ,    "ICP_INVALID" },
75 { CODE_ICP_OP_QUERY ,    "ICP_QUERY" },
76 { CODE_ICP_OP_HIT ,    "ICP_HIT" },
77 { CODE_ICP_OP_MISS ,    "ICP_MISS" },
78 { CODE_ICP_OP_ERR ,    "ICP_ERR" },
79 { CODE_ICP_OP_SEND,    "ICP_SEND" },
80 { CODE_ICP_OP_SENDA, "ICP_SENDA"},
81 { CODE_ICP_OP_DATABEG, "ICP_DATABEG"},
82 { CODE_ICP_OP_DATA,    "ICP_DATA"},
83 { CODE_ICP_OP_DATAEND, "ICP_DATA_END"}, 
84 { CODE_ICP_OP_SECHO ,    "ICP_SECHO"},
85 { CODE_ICP_OP_DECHO ,    "ICP_DECHO"},
86 { CODE_ICP_OP_MISS_NOFETCH ,    "ICP_MISS_NOFETCH"},
87 { CODE_ICP_OP_DENIED ,    "ICP_DENIED"},
88 { CODE_ICP_OP_HIT_OBJ ,    "ICP_HIT_OBJ"},
89 { 0,     NULL}
90 };
91
92
93
94 typedef struct _e_icphdr
95 {
96   guint8 opcode;
97   guint8 version;
98   guint16 message_length;
99   guint32 request_number;
100   guint32 options;
101   guint32 option_data;
102   gchar sender_address[4];
103 } e_icphdr;
104
105 static gchar textbuf[MAX_TEXTBUF_LENGTH];
106
107 static void dissect_icp_payload( const u_char *pd, int offset,
108         frame_data *fd,proto_tree *pload_tree, e_icphdr *icph)
109 {
110 /* To Be Done take care of fragmentation*/
111 guint32 maxlength=END_OF_FRAME;
112 guint32 i;
113 guint16 objectlength;
114   switch(icph->opcode)
115   {
116         case CODE_ICP_OP_QUERY:
117                 /* 4 byte requester host address */
118                 proto_tree_add_text(pload_tree, NullTVB,offset,4,
119                         "Requester Host Address %u.%u.%u.%u",
120                         (guint8)pd[offset],
121                         (guint8)pd[offset+1],
122                         (guint8)pd[offset+2],
123                         (guint8)pd[offset+3]);
124
125                 /* null terminated URL */
126                 for (i=0; i < maxlength && 
127                         pd[offset+4+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
128                 {
129                         textbuf[i]=pd[offset+4+i];
130                 }
131                 textbuf[i]=0;
132                 i++;
133                 proto_tree_add_text(pload_tree, NullTVB, offset+4,i,
134                         "URL: %s", textbuf);
135                 break;
136         case CODE_ICP_OP_HIT_OBJ:
137                 /* null terminated url */
138                 for (i=0; i < maxlength && 
139                         pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
140                 {
141                         textbuf[i]=pd[offset+i];
142                 }
143                 textbuf[i]=0;
144                 i++;
145                 proto_tree_add_text(pload_tree, NullTVB, offset,i,
146                         "URL: %s", textbuf);
147                 /* 2 byte object size */
148                 /* object data not recommended by standard*/
149                 objectlength=pntohs(&pd[offset]);
150                 proto_tree_add_text(pload_tree, NullTVB,offset,2,"object length: %u", objectlength);
151                 /* object data not recommended by standard*/
152                 proto_tree_add_text(pload_tree, NullTVB,offset+2, maxlength-2,"object data");
153                 if (objectlength > maxlength-2)
154                 {
155                         proto_tree_add_text(pload_tree, NullTVB,offset,0,
156                                 "Packet is fragmented, rest of object is in next udp packet");
157                 }
158         case CODE_ICP_OP_MISS:
159         case CODE_ICP_OP_HIT:
160                 for (i=0; i < maxlength && 
161                         pd[offset+i] != 0 && i<(MAX_TEXTBUF_LENGTH-1);i++)
162                 {
163                         textbuf[i]=pd[offset+i];
164                 }
165                 textbuf[i]=0;
166                 i++;
167                 proto_tree_add_text(pload_tree, NullTVB, offset,i,
168                         "URL: %s", textbuf);    
169         default: 
170                 /* check for fragmentation and add message if next part
171                          of payload in next fragment*/
172                 break;
173   }
174 }
175
176 static void dissect_icp(const u_char *pd, int offset, frame_data *fd,
177         proto_tree *tree)
178 {
179   proto_tree *icp_tree , *payload_tree;
180   proto_item *ti , *payloadtf;
181   e_icphdr icph;
182
183   gchar *opcodestrval;
184
185   OLD_CHECK_DISPLAY_AS_DATA(proto_icp, pd, offset, fd, tree);
186
187 /* TBD: check if this is the first fragment of a fragmented UDP datagram?
188    Or just wait for IP fragment reassembly to be implemented? */
189   icph.opcode=pd[offset];
190   icph.version=pd[offset+1];
191   icph.message_length=pntohs(&(pd[offset+2]));
192   icph.request_number=pntohl(&(pd[offset+4]));
193   memcpy(&icph.options,&pd[offset+8],sizeof(guint32));
194   memcpy(&icph.option_data,&pd[offset+12],sizeof(guint32));
195   memcpy(icph.sender_address,&pd[offset+16],4);
196
197
198   opcodestrval =  match_strval(icph.opcode,opcode_vals);
199
200   if (opcodestrval == NULL ) opcodestrval= "UNKNOWN OPCODE";
201
202   sprintf(textbuf,"opc: %s(%u), Req Nr: %u", opcodestrval,
203                 (guint16)icph.opcode,icph.request_number);
204
205   if (check_col(fd, COL_PROTOCOL))
206         col_set_str(fd, COL_PROTOCOL, "ICP");
207
208   if (check_col(fd, COL_INFO))
209   {
210         col_add_fstr(fd,COL_INFO,textbuf);
211   }
212
213   if (tree)
214   {
215
216         ti = proto_tree_add_item(tree,proto_icp, NullTVB,offset,END_OF_FRAME, FALSE);
217
218         icp_tree = proto_item_add_subtree(ti, ett_icp);
219         proto_tree_add_uint_format(icp_tree,hf_icp_opcode, NullTVB, offset,      1,
220                icph.opcode, "Opcode:0x%01x (%s)",icph.opcode, opcodestrval);
221
222         proto_tree_add_uint_format(icp_tree,hf_icp_version, NullTVB, offset+1, 1,
223                 icph.version,"Version: 0x%01x (%d)", icph.version, (int)icph.version);
224
225         proto_tree_add_uint_format(icp_tree,hf_icp_length, NullTVB, offset+2, 2,
226                 icph.message_length,
227                 "Length: 0x%02x (%d)", icph.message_length,(int)icph.message_length);
228         proto_tree_add_uint_format(icp_tree,hf_icp_request_nr, NullTVB, offset+4, 4,
229                 icph.request_number,
230                 "Request Number: 0x%04x (%u)", icph.request_number,icph.request_number);
231                 
232         if ( (icph.opcode == CODE_ICP_OP_QUERY) && ((icph.options & 0x80000000 ) != 0) )
233         {
234                 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
235                         "option: ICP_FLAG_HIT_OBJ");
236         }
237         if ( (icph.opcode == CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0) )
238         {
239                 proto_tree_add_text(icp_tree, NullTVB,offset+8,4,
240                         "option:ICP_FLAG_SRC_RTT");
241         }
242         if ((icph.opcode != CODE_ICP_OP_QUERY)&& ((icph.options & 0x40000000 ) != 0))
243         {
244                 proto_tree_add_text(icp_tree, NullTVB,offset+8,8,
245                         "option: ICP_FLAG_SCR_RTT RTT=%u", icph.option_data & 0x0000ffff);
246         }
247         
248         proto_tree_add_text(icp_tree, NullTVB,offset+16, 4, 
249                         "Sender Host IP address %u.%u.%u.%u",
250                         (guint8)icph.sender_address[0],
251                         (guint8)icph.sender_address[1],
252                         (guint8)icph.sender_address[2],
253                         (guint8)icph.sender_address[3]);
254
255         payloadtf = proto_tree_add_text(icp_tree, NullTVB,
256                         offset+20,icph.message_length - 20,
257                         "Payload");
258         payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
259
260         if (payload_tree !=NULL)
261         {
262                 dissect_icp_payload( pd,
263                                 20+offset,fd,payload_tree,&icph);
264         }
265   }
266 }
267 void
268 proto_register_icp(void)
269 {
270         static hf_register_info hf[] = {
271                 { &hf_icp_opcode,
272                 { "Opcode","icp.opcode", FT_UINT8, BASE_HEX, NULL, 0x0,
273                         "" }},
274
275                 { &hf_icp_version,
276                 { "Version",    "icp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
277                         "" }},
278
279                 { &hf_icp_length,
280                 { "Length","icp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
281                         "" }},
282
283                 { &hf_icp_request_nr,
284                 { "Request Number","icp.nr", FT_UINT32, BASE_DEC, NULL, 0x0,
285                         "" }},
286         };
287         static gint *ett[] = {
288                 &ett_icp,
289                 &ett_icp_payload,
290         };
291
292         proto_icp = proto_register_protocol("Internet Cache Protocol",
293             "ICP", "icp");
294         proto_register_field_array(proto_icp, hf, array_length(hf));
295         proto_register_subtree_array(ett, array_length(ett));
296 }
297
298 void
299 proto_reg_handoff_icp(void)
300 {
301         old_dissector_add("udp.port", UDP_PORT_ICP, dissect_icp);
302 }