2 * Routines for dsi packet dissection
3 * Copyright 2001, Randy McEoin <rmceoin@pe.com>
5 * $Id: packet-dsi.c,v 1.3 2001/09/03 10:33:05 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-pop.c
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.
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.
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.
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
46 #include "conversation.h"
48 /* The information in this module (DSI) comes from:
50 AFP 2.1 & 2.2.pdf contained in AppleShare_IP_6.3_SDK
51 available from http://www.apple.com
53 The netatalk source code by Wesley Craig & Adrian Sun
55 * What a Data Stream Interface packet looks like:
57 * |-------------------------------|
58 * |flags |command| requestID |
59 * |-------------------------------|
60 * |error code/enclosed data offset|
61 * |-------------------------------|
62 * |total data length |
63 * |-------------------------------|
65 * |-------------------------------|
68 static int proto_dsi = -1;
69 static int hf_dsi_flags = -1;
70 static int hf_dsi_command = -1;
71 static int hf_dsi_requestid = -1;
72 static int hf_dsi_code = -1;
73 static int hf_dsi_length = -1;
74 static int hf_dsi_reserved = -1;
76 static gint ett_dsi = -1;
78 #define TCP_PORT_DSI 548
81 #define DSIFL_REQUEST 0x00
82 #define DSIFL_REPLY 0x01
83 #define DSIFL_MAX 0x01
86 #define DSIFUNC_CLOSE 1 /* DSICloseSession */
87 #define DSIFUNC_CMD 2 /* DSICommand */
88 #define DSIFUNC_STAT 3 /* DSIGetStatus */
89 #define DSIFUNC_OPEN 4 /* DSIOpenSession */
90 #define DSIFUNC_TICKLE 5 /* DSITickle */
91 #define DSIFUNC_WRITE 6 /* DSIWrite */
92 #define DSIFUNC_ATTN 8 /* DSIAttention */
93 #define DSIFUNC_MAX 8 /* largest command */
95 static const value_string flag_vals[] = {
96 {DSIFL_REQUEST, "Request" },
97 {DSIFL_REPLY, "Reply" },
100 static const value_string func_vals[] = {
101 {DSIFUNC_CLOSE, "CloseSession" },
102 {DSIFUNC_CMD, "Command" },
103 {DSIFUNC_STAT, "GetStatus" },
104 {DSIFUNC_OPEN, "OpenSession" },
105 {DSIFUNC_TICKLE, "Tickle" },
106 {DSIFUNC_WRITE, "Write" },
107 {DSIFUNC_ATTN, "Attention" },
111 static GMemChunk *vals = NULL;
115 guint8 flags,command;
118 guint32 length; /* total length of this DSI request/reply */
120 guint32 seen; /* bytes seen so far */
123 enum {NONE,FIRSTDATA,MOREDATA,DONE};
125 #define hash_init_count 20
126 #define hash_val_length (sizeof(hash_entry_t))
128 static guint32 last_abs_sec = 0;
129 static guint32 last_abs_usec= 0;
130 static guint32 highest_num = 0;
133 gint dsi_equal (gconstpointer v, gconstpointer v2);
134 guint dsi_hash (gconstpointer v);
136 static guint dsi_packet_init_count = 200;
147 guint32 seen; /* bytes seen so far, including this packet */
150 static GHashTable *dsi_request_hash = NULL;
151 static GMemChunk *dsi_request_keys = NULL;
152 static GMemChunk *dsi_request_records = NULL;
155 gint dsi_equal (gconstpointer v, gconstpointer v2)
157 dsi_request_key *val1 = (dsi_request_key*)v;
158 dsi_request_key *val2 = (dsi_request_key*)v2;
160 if (val1->packetnum == val2->packetnum) {
166 guint dsi_hash (gconstpointer v)
168 dsi_request_key *dsi_key = (dsi_request_key*)v;
169 return GPOINTER_TO_UINT(dsi_key->packetnum);
173 dsi_hash_insert(guint32 packetnum, guint8 flags, guint8 command,
174 guint16 requestid, guint32 length, guint32 seen)
176 dsi_request_val *request_val;
177 dsi_request_key *request_key;
179 /* Now remember info about this continuation packet */
181 request_key = g_mem_chunk_alloc(dsi_request_keys);
182 request_key->packetnum = packetnum;
184 request_val = g_mem_chunk_alloc(dsi_request_records);
185 request_val->flags = flags;
186 request_val->command = command;
187 request_val->requestid = requestid;
188 request_val->length = length;
189 request_val->seen = seen;
191 g_hash_table_insert(dsi_request_hash, request_key, request_val);
194 /* Returns TRUE or FALSE. If TRUE, the record was found */
197 dsi_hash_lookup(guint32 packetnum, guint8 *flags, guint8 *command,
198 guint16 *requestid, guint32 *length, guint32 *seen)
200 dsi_request_val *request_val;
201 dsi_request_key request_key;
203 request_key.packetnum = packetnum;
205 request_val = (dsi_request_val*)
206 g_hash_table_lookup(dsi_request_hash, &request_key);
209 *flags = request_val->flags;
210 *command = request_val->command;
211 *requestid = request_val->requestid;
212 *length = request_val->length;
213 *seen = request_val->seen;
221 /* The state_machine remembers information about continuation packets */
222 /* returns TRUE if it found a previously known continuation packet */
224 dsi_state_machine( hash_entry_t *hash_info, tvbuff_t *tvb, packet_info *pinfo,
229 guint8 flags,command;
237 found_hash=dsi_hash_lookup(fd->num, &flags, &command, &requestid,
239 if (found_hash==TRUE)
241 hash_info->flags = flags;
242 hash_info->command = command;
243 hash_info->requestid = requestid;
244 hash_info->length = length;
245 hash_info->seen = seen;
249 /* is this sequentially the next packet? */
250 if (highest_num > fd->num)
252 hash_info->state = NONE;
256 highest_num = fd->num;
258 if ((hash_info->state == NONE) || (hash_info->state == DONE))
260 hash_info->state = NONE;
261 hash_info->length = tvb_get_ntohl(tvb, offset+8);
262 data_here = tvb_length_remaining(tvb, offset+16);
263 if (data_here < hash_info->length)
265 hash_info->flags = tvb_get_guint8(tvb, offset);
266 hash_info->command = tvb_get_guint8(tvb, offset+1);
267 hash_info->requestid = tvb_get_ntohs(tvb, offset+2);
268 hash_info->code = tvb_get_ntohl(tvb, offset+4);
269 hash_info->reserved = tvb_get_ntohl(tvb, offset+12);
270 hash_info->seen = data_here;
271 hash_info->state = FIRSTDATA;
277 if (hash_info->state == FIRSTDATA)
278 hash_info->state = MOREDATA;
280 /* we must be receiving more data */
281 data_here = tvb_length_remaining(tvb, offset);
282 hash_info->seen += data_here;
283 if (hash_info->seen >= hash_info->length)
284 hash_info->state = DONE;
286 dsi_hash_insert(fd->num, hash_info->flags,
287 hash_info->command, hash_info->requestid,
288 hash_info->length,hash_info->seen);
294 dissect_dsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
296 proto_tree *dsi_tree;
298 conversation_t *conversation;
299 hash_entry_t *hash_info;
301 gboolean prev_cont; /* TRUE if a previously known
302 * continuation packet */
307 guint8 dsi_flags,dsi_command;
308 guint16 dsi_requestid;
311 guint32 dsi_reserved;
313 if (check_col(pinfo->fd, COL_PROTOCOL))
314 col_set_str(pinfo->fd, COL_PROTOCOL, "DSI");
315 if (check_col(pinfo->fd, COL_INFO))
316 col_clear(pinfo->fd, COL_INFO);
318 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_TCP,
319 pinfo->srcport, pinfo->destport, 0);
320 if (conversation == NULL)
322 conversation = conversation_new(&pinfo->src, &pinfo->dst,
323 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
325 conversation_set_dissector(conversation, dissect_dsi);
326 hash_info = conversation_get_proto_data(conversation, proto_dsi);
327 if (hash_info == NULL)
329 hash_info = g_mem_chunk_alloc(vals);
330 hash_info->state = NONE;
331 conversation_add_proto_data(conversation, proto_dsi,
335 prev_cont=dsi_state_machine( hash_info, tvb, pinfo, offset);
337 if ((hash_info->state == NONE) && (prev_cont!=TRUE))
339 dsi_flags = tvb_get_guint8(tvb, offset);
340 dsi_command = tvb_get_guint8(tvb, offset+1);
341 dsi_requestid = tvb_get_ntohs(tvb, offset+2);
342 dsi_code = tvb_get_ntohl(tvb, offset+4);
343 dsi_length = tvb_get_ntohl(tvb, offset+8);
344 dsi_reserved = tvb_get_ntohl(tvb, offset+12);
347 dsi_flags = hash_info->flags;
348 dsi_command = hash_info->command;
349 dsi_requestid = hash_info->requestid;
350 dsi_code = hash_info->code;
351 dsi_length = hash_info->length;
352 dsi_reserved = hash_info->reserved;
355 if (check_col(pinfo->fd, COL_INFO)) {
356 if ((func_str = match_strval(dsi_command, func_vals)))
358 flag_str = match_strval(dsi_flags, flag_vals);
359 if ((hash_info->state == MOREDATA) ||
360 (hash_info->state == DONE) ||
363 sprintf(cont_str,"Continued: %d/%d",
364 hash_info->seen,hash_info->length);
369 col_add_fstr(pinfo->fd, COL_INFO, "%s %s (%d) %s",
370 flag_str,func_str,dsi_requestid,
378 ti = proto_tree_add_item(tree, proto_dsi, tvb, offset,
379 tvb_length_remaining(tvb, offset), FALSE);
380 dsi_tree = proto_item_add_subtree(ti, ett_dsi);
382 if (prev_cont == TRUE)
384 proto_tree_add_uint(dsi_tree, hf_dsi_requestid, tvb,
385 0, 0, dsi_requestid);
386 dissect_data(tvb, 0, pinfo, dsi_tree);
389 proto_tree_add_uint(dsi_tree, hf_dsi_flags, tvb,
390 offset, 1, dsi_flags);
391 proto_tree_add_uint(dsi_tree, hf_dsi_command, tvb,
392 offset+1, 1, dsi_command);
393 proto_tree_add_uint(dsi_tree, hf_dsi_requestid, tvb,
394 offset+2, 2, dsi_requestid);
395 proto_tree_add_uint(dsi_tree, hf_dsi_code, tvb,
396 offset+4, 4, dsi_code);
397 proto_tree_add_uint_format(dsi_tree, hf_dsi_length, tvb,
398 offset+8, 4, dsi_length,
399 "Length: %d bytes", dsi_length);
400 proto_tree_add_uint(dsi_tree, hf_dsi_reserved, tvb,
401 offset+12, 4, dsi_reserved);
402 dissect_data(tvb, 16, pinfo, dsi_tree);
408 static void dsi_reinit( void){
415 g_mem_chunk_destroy(vals);
416 if (dsi_request_hash)
417 g_hash_table_destroy(dsi_request_hash);
418 if (dsi_request_keys)
419 g_mem_chunk_destroy(dsi_request_keys);
420 if (dsi_request_records)
421 g_mem_chunk_destroy(dsi_request_records);
423 dsi_request_hash = g_hash_table_new(dsi_hash, dsi_equal);
425 dsi_request_keys = g_mem_chunk_new("dsi_request_keys",
426 sizeof(dsi_request_key),
427 dsi_packet_init_count * sizeof(dsi_request_key),
429 dsi_request_records = g_mem_chunk_new("dsi_request_records",
430 sizeof(dsi_request_val),
431 dsi_packet_init_count * sizeof(dsi_request_val),
434 vals = g_mem_chunk_new("dsi_vals", hash_val_length,
435 hash_init_count * hash_val_length,
440 proto_register_dsi(void)
443 static hf_register_info hf[] = {
445 { "Flags", "dsi.flags",
446 FT_UINT8, BASE_HEX, VALS(flag_vals), 0x0,
447 "Indicates request or reply.", HFILL }},
450 { "Command", "dsi.command",
451 FT_UINT8, BASE_DEC, VALS(func_vals), 0x0,
452 "Represents a DSI command.", HFILL }},
455 { "Request ID", "dsi.requestid",
456 FT_UINT16, BASE_DEC, NULL, 0x0,
457 "Keeps track of which request this is. Replies must match a Request. IDs must be generated in sequential order.", HFILL }},
460 { "Code", "dsi.code",
461 FT_UINT32, BASE_HEX, NULL, 0x0,
462 "In Reply packets this is an error code. In Request Write packets this is a data offset.", HFILL }},
465 { "Length", "dsi.length",
466 FT_UINT32, BASE_DEC, NULL, 0x0,
467 "Total length of the data that follows the DSI header.", HFILL }},
470 { "Reserved", "dsi.reserved",
471 FT_UINT32, BASE_HEX, NULL, 0x0,
472 "Reserved for future use. Should be set to zero.", HFILL }},
475 static gint *ett[] = {
479 proto_dsi = proto_register_protocol("Data Stream Interface", "DSI", "dsi");
480 proto_register_field_array(proto_dsi, hf, array_length(hf));
481 proto_register_subtree_array(ett, array_length(ett));
483 register_init_routine( &dsi_reinit);
487 proto_reg_handoff_dsi(void)
489 dissector_add("tcp.port", TCP_PORT_DSI, dissect_dsi, proto_dsi);