Have "dissect_netbios_payload()" take as an argument a tvbuff containing
[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.18 2001/06/18 02:17:46 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 static void dissect_icp_payload(tvbuff_t *tvb, int offset,
93         proto_tree *pload_tree, guint8 opcode)
94 {
95   gint stringlength;
96   guint16 objectlength;
97
98   switch(opcode)
99   {
100         case CODE_ICP_OP_QUERY:
101                 /* 4 byte requester host address */
102                 proto_tree_add_text(pload_tree, tvb,offset,4,
103                         "Requester Host Address %s",
104                         ip_to_str(tvb_get_ptr(tvb, offset, 4)));
105                 offset += 4;
106
107                 /* null terminated URL */
108                 stringlength = tvb_strsize(tvb, offset);
109                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
110                         "URL: %s", tvb_get_ptr(tvb, offset, stringlength));
111                 break;
112
113         case CODE_ICP_OP_SECHO:
114         case CODE_ICP_OP_DECHO:
115         case CODE_ICP_OP_HIT:
116         case CODE_ICP_OP_MISS:
117         case CODE_ICP_OP_ERR:
118         case CODE_ICP_OP_MISS_NOFETCH:
119         case CODE_ICP_OP_DENIED:
120                 stringlength = tvb_strsize(tvb, offset);
121                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
122                         "URL: %s", tvb_get_ptr(tvb, offset, stringlength));
123                 break;
124
125         case CODE_ICP_OP_HIT_OBJ:
126                 /* null terminated URL */
127                 stringlength = tvb_strsize(tvb, offset);
128                 proto_tree_add_text(pload_tree, tvb, offset, stringlength,
129                         "URL: %s", tvb_get_ptr(tvb, offset, stringlength));
130                 offset += stringlength;
131
132                 /* 2 byte object size */
133                 /* object data not recommended by standard*/
134                 objectlength=tvb_get_ntohs(tvb, offset);
135                 proto_tree_add_text(pload_tree, tvb,offset,2,"Object length: %u", objectlength);
136                 offset += 2;
137
138                 /* object data not recommended by standard*/
139                 proto_tree_add_text(pload_tree, tvb,offset,objectlength,"Object data");
140                 if (objectlength > tvb_reported_length_remaining(tvb, offset))
141                 {
142                         proto_tree_add_text(pload_tree, tvb,offset,0,
143                                 "Packet is fragmented, rest of object is in next udp packet");
144                 }
145                 break;
146         default: 
147                 break;
148   }
149 }
150
151 static void dissect_icp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 {
153   proto_tree *icp_tree , *payload_tree;
154   proto_item *ti , *payloadtf;
155   guint8 opcode;
156   guint16 message_length;
157   guint32 request_number;
158   guint32 options;
159   guint32 option_data;
160
161   if (check_col(pinfo->fd, COL_PROTOCOL))
162         col_set_str(pinfo->fd, COL_PROTOCOL, "ICP");
163   if (check_col(pinfo->fd, COL_INFO))
164         col_clear(pinfo->fd, COL_INFO);
165
166   opcode=tvb_get_guint8(tvb, 0);
167   message_length=tvb_get_ntohs(tvb, 2);
168   request_number=tvb_get_ntohl(tvb, 4);
169
170   if (check_col(pinfo->fd, COL_INFO))
171   {
172         col_add_fstr(pinfo->fd,COL_INFO,"Opcode: %s (%u), Req Nr: %u",
173                 val_to_str(opcode, opcode_vals, "Unknown"), opcode,
174                 request_number);
175   }
176
177   if (tree)
178   {
179
180         ti = proto_tree_add_item(tree,proto_icp, tvb, 0, message_length, FALSE);
181         icp_tree = proto_item_add_subtree(ti, ett_icp);
182
183         proto_tree_add_uint(icp_tree,hf_icp_opcode, tvb, 0, 1, opcode);
184
185         proto_tree_add_item(icp_tree,hf_icp_version, tvb, 1, 1, FALSE);
186
187         proto_tree_add_uint(icp_tree,hf_icp_length, tvb, 2, 2, message_length);
188
189         proto_tree_add_uint(icp_tree,hf_icp_request_nr, tvb, 4, 4,
190                 request_number);
191                 
192         options=tvb_get_ntohl(tvb, 8);
193         if ( (opcode == CODE_ICP_OP_QUERY) && ((options & 0x80000000 ) != 0) )
194         {
195                 proto_tree_add_text(icp_tree, tvb,8,4,
196                         "option: ICP_FLAG_HIT_OBJ");
197         }
198         if ( (opcode == CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0) )
199         {
200                 proto_tree_add_text(icp_tree, tvb,8,4,
201                         "option:ICP_FLAG_SRC_RTT");
202         }
203         if ((opcode != CODE_ICP_OP_QUERY)&& ((options & 0x40000000 ) != 0))
204         {
205                 option_data=tvb_get_ntohl(tvb, 12);
206                 proto_tree_add_text(icp_tree, tvb,8,8,
207                         "option: ICP_FLAG_SCR_RTT RTT=%u",
208                         option_data & 0x0000ffff);
209         }
210         
211         proto_tree_add_text(icp_tree, tvb, 16, 4, 
212                         "Sender Host IP address %s",
213                         ip_to_str(tvb_get_ptr(tvb, 16, 4)));
214
215         payloadtf = proto_tree_add_text(icp_tree, tvb,
216                         20, message_length - 20,
217                         "Payload");
218         payload_tree = proto_item_add_subtree(payloadtf, ett_icp_payload);
219         dissect_icp_payload(tvb, 20, payload_tree, opcode);
220   }
221 }
222 void
223 proto_register_icp(void)
224 {
225         static hf_register_info hf[] = {
226                 { &hf_icp_opcode,
227                 { "Opcode", "icp.opcode", FT_UINT8, BASE_HEX, VALS(opcode_vals),
228                         0x0, "", HFILL }},
229
230                 { &hf_icp_version,
231                 { "Version", "icp.version", FT_UINT8, BASE_DEC, NULL,
232                         0x0, "", HFILL }},
233
234                 { &hf_icp_length,
235                 { "Length", "icp.length", FT_UINT16, BASE_DEC, NULL,
236                         0x0, "", HFILL }},
237
238                 { &hf_icp_request_nr,
239                 { "Request Number", "icp.nr", FT_UINT32, BASE_DEC, NULL,
240                         0x0, "", HFILL }},
241         };
242         static gint *ett[] = {
243                 &ett_icp,
244                 &ett_icp_payload,
245         };
246
247         proto_icp = proto_register_protocol("Internet Cache Protocol",
248             "ICP", "icp");
249         proto_register_field_array(proto_icp, hf, array_length(hf));
250         proto_register_subtree_array(ett, array_length(ett));
251 }
252
253 void
254 proto_reg_handoff_icp(void)
255 {
256         dissector_add("udp.port", UDP_PORT_ICP, dissect_icp, proto_icp);
257 }