From Chris Wilson:
[obnox/wireshark/wip.git] / packet-dsi.c
1 /* packet-dsi.c
2  * Routines for dsi packet dissection
3  * Copyright 2001, Randy McEoin <rmceoin@pe.com>
4  *
5  * $Id: packet-dsi.c,v 1.29 2004/01/13 21:49:52 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from packet-pop.c
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33
34 #include <glib.h>
35 #include <epan/packet.h>
36
37 #include "prefs.h"
38 #include "packet-tcp.h"
39 #include "packet-afp.h"
40
41 /* The information in this module (DSI) comes from:
42
43   AFP 2.1 & 2.2.pdf contained in AppleShare_IP_6.3_SDK
44   available from http://www.apple.com
45
46   The netatalk source code by Wesley Craig & Adrian Sun
47
48  * What a Data Stream Interface packet looks like:
49  * 0                               32
50  * |-------------------------------|
51  * |flags  |command| requestID     |
52  * |-------------------------------|
53  * |error code/enclosed data offset|
54  * |-------------------------------|
55  * |total data length              |
56  * |-------------------------------|
57  * |reserved field                 |
58  * |-------------------------------|
59  */
60
61 static int proto_dsi = -1;
62 static int hf_dsi_flags = -1;
63 static int hf_dsi_command = -1;
64 static int hf_dsi_requestid = -1;
65 static int hf_dsi_offset = -1;
66 static int hf_dsi_error = -1;
67 static int hf_dsi_length = -1;
68 static int hf_dsi_reserved = -1;
69
70 static gint ett_dsi = -1;
71
72 static int hf_dsi_open_type     = -1;
73 static int hf_dsi_open_len      = -1;
74 static int hf_dsi_open_quantum  = -1;
75 static int hf_dsi_open_option   = -1;
76
77 static int hf_dsi_attn_flag             = -1;
78 static int hf_dsi_attn_flag_shutdown    = -1;
79 static int hf_dsi_attn_flag_crash       = -1;
80 static int hf_dsi_attn_flag_msg         = -1;
81 static int hf_dsi_attn_flag_reconnect   = -1;
82 static int hf_dsi_attn_flag_time        = -1;
83 static int hf_dsi_attn_flag_bitmap      = -1;
84
85 static gint ett_dsi_open        = -1;
86 static gint ett_dsi_attn        = -1;
87 static gint ett_dsi_attn_flag   = -1;
88
89 static const value_string dsi_attn_flag_vals[] = {
90   {0x0, "Reserved" },                                           /* 0000 */
91   {0x1, "Reserved" },                                           /* 0001 */
92   {0x2, "Server message" },                                     /* 0010 */
93   {0x3, "Server notification, cf. extended bitmap" },           /* 0011 */
94   {0x4, "Server is shutting down, internal error" },            /* 0100 */
95   {0x8, "Server is shutting down" },                            /* 1000 */
96   {0x9, "Server disconnects user" },                            /* 1001 */
97   {0x10,"Server is shutting down, message" },                   /* 1010 */
98   {0x11,"Server is shutting down, message,no reconnect"},       /* 1011 */
99   {0,                   NULL } };
100
101 static const value_string dsi_open_type_vals[] = {
102   {0,   "Server quantum" },
103   {1,   "Attention quantum" },
104   {0,                   NULL } };
105
106 /* status stuff same for asp and afp */
107 static int hf_dsi_server_name = -1;
108 static int hf_dsi_utf8_server_name_len = -1;
109 static int hf_dsi_utf8_server_name = -1;
110 static int hf_dsi_server_type = -1;
111 static int hf_dsi_server_vers = -1;
112 static int hf_dsi_server_uams = -1;
113 static int hf_dsi_server_icon = -1;
114 static int hf_dsi_server_directory = -1;
115
116 static int hf_dsi_server_flag = -1;
117 static int hf_dsi_server_flag_copyfile = -1;
118 static int hf_dsi_server_flag_passwd   = -1;
119 static int hf_dsi_server_flag_no_save_passwd = -1;
120 static int hf_dsi_server_flag_srv_msg   = -1;
121 static int hf_dsi_server_flag_srv_sig   = -1;
122 static int hf_dsi_server_flag_tcpip     = -1;
123 static int hf_dsi_server_flag_notify    = -1;
124 static int hf_dsi_server_flag_reconnect = -1;
125 static int hf_dsi_server_flag_directory = -1;
126 static int hf_dsi_server_flag_utf8_name = -1;
127 static int hf_dsi_server_flag_fast_copy = -1;
128 static int hf_dsi_server_signature      = -1;
129
130 static int hf_dsi_server_addr_len       = -1;
131 static int hf_dsi_server_addr_type      = -1;
132 static int hf_dsi_server_addr_value     = -1;
133
134 static gint ett_dsi_status = -1;
135 static gint ett_dsi_uams   = -1;
136 static gint ett_dsi_vers   = -1;
137 static gint ett_dsi_addr   = -1;
138 static gint ett_dsi_addr_line = -1;
139 static gint ett_dsi_directory = -1;
140 static gint ett_dsi_utf8_name = -1;
141 static gint ett_dsi_status_server_flag = -1;
142
143 const value_string afp_server_addr_type_vals[] = {
144   {1,   "IP address" },
145   {2,   "IP+port address" },
146   {3,   "DDP address" },
147   {4,   "DNS name" },
148   {5,   "IP+port ssh tunnel" },
149   {0,                   NULL } };
150
151 /* end status stuff */
152
153 /* desegmentation of DSI */
154 static gboolean dsi_desegment = TRUE;
155
156 static dissector_handle_t data_handle;
157 static dissector_handle_t afp_handle;
158
159 #define TCP_PORT_DSI                    548
160
161 #define DSI_BLOCKSIZ            16
162
163 /* DSI flags */
164 #define DSIFL_REQUEST    0x00
165 #define DSIFL_REPLY      0x01
166 #define DSIFL_MAX        0x01
167
168 /* DSI Commands */
169 #define DSIFUNC_CLOSE   1       /* DSICloseSession */
170 #define DSIFUNC_CMD     2       /* DSICommand */
171 #define DSIFUNC_STAT    3       /* DSIGetStatus */
172 #define DSIFUNC_OPEN    4       /* DSIOpenSession */
173 #define DSIFUNC_TICKLE  5       /* DSITickle */
174 #define DSIFUNC_WRITE   6       /* DSIWrite */
175 #define DSIFUNC_ATTN    8       /* DSIAttention */
176 #define DSIFUNC_MAX     8       /* largest command */
177
178 static const value_string flag_vals[] = {
179   {DSIFL_REQUEST,       "Request" },
180   {DSIFL_REPLY,         "Reply" },
181   {0,                   NULL } };
182
183 static const value_string func_vals[] = {
184   {DSIFUNC_CLOSE,       "CloseSession" },
185   {DSIFUNC_CMD,         "Command" },
186   {DSIFUNC_STAT,        "GetStatus" },
187   {DSIFUNC_OPEN,        "OpenSession" },
188   {DSIFUNC_TICKLE,      "Tickle" },
189   {DSIFUNC_WRITE,       "Write" },
190   {DSIFUNC_ATTN,        "Attention" },
191   {0,                   NULL } };
192
193 static gint
194 dissect_dsi_open_session(tvbuff_t *tvb, proto_tree *dsi_tree, gint offset)
195 {
196         proto_tree      *tree;
197         proto_item      *ti;
198         guint8          type;
199         guint8          len;
200
201         ti = proto_tree_add_text(dsi_tree, tvb, offset, -1, "Open Session");
202         tree = proto_item_add_subtree(ti, ett_dsi_open);
203         type = tvb_get_guint8(tvb, offset);
204         proto_tree_add_item(tree, hf_dsi_open_type, tvb, offset, 1, FALSE);
205         offset++;
206         len = tvb_get_guint8(tvb, offset);
207         proto_tree_add_item(tree, hf_dsi_open_len, tvb, offset, 1, FALSE);
208         offset++;
209         if (type <= 1) {
210                 proto_tree_add_item(tree, hf_dsi_open_quantum, tvb, offset, 4, FALSE);
211         }
212         else {
213                 proto_tree_add_item(tree, hf_dsi_open_option, tvb, offset, len, FALSE);
214         }
215         offset += len;
216         return offset;
217 }
218
219 static gint
220 dissect_dsi_attention(tvbuff_t *tvb, proto_tree *dsi_tree, gint offset)
221 {
222         proto_tree      *tree;
223         proto_item      *ti;
224         guint16         flag;
225
226         if (!tvb_reported_length_remaining(tvb,offset))
227                 return offset;
228
229         flag = tvb_get_ntohs(tvb, offset);
230         ti = proto_tree_add_text(dsi_tree, tvb, offset, -1, "Attention");
231         tree = proto_item_add_subtree(ti, ett_dsi_attn);
232
233         ti = proto_tree_add_item(tree, hf_dsi_attn_flag, tvb, offset, 2, FALSE);
234         tree = proto_item_add_subtree(ti, ett_dsi_attn_flag);
235         proto_tree_add_item(tree, hf_dsi_attn_flag_shutdown, tvb, offset, 2, FALSE);
236         proto_tree_add_item(tree, hf_dsi_attn_flag_crash, tvb, offset, 2, FALSE);
237         proto_tree_add_item(tree, hf_dsi_attn_flag_msg, tvb, offset, 2, FALSE);
238         proto_tree_add_item(tree, hf_dsi_attn_flag_reconnect, tvb, offset, 2, FALSE);
239         /* FIXME */
240         if ((flag & 0xf000) != 0x3000)
241                 proto_tree_add_item(tree, hf_dsi_attn_flag_time, tvb, offset, 2, FALSE);
242         else
243                 proto_tree_add_item(tree, hf_dsi_attn_flag_bitmap, tvb, offset, 2, FALSE);
244         offset += 2;
245         return offset;
246 }
247
248 /* -----------------------------
249         from netatalk/etc/afpd/status.c
250 */
251 static gint
252 dissect_dsi_reply_get_status(tvbuff_t *tvb, proto_tree *tree, gint offset)
253 {
254         proto_tree      *sub_tree;
255         proto_item      *ti;
256
257         guint16 ofs;
258         guint16 flag;
259         guint16 sign_ofs = 0;
260         guint16 adr_ofs = 0;
261         guint16 dir_ofs = 0;
262         guint16 utf_ofs = 0;
263         guint8  nbe;
264         guint8  len;
265         guint8  i;
266
267         if (!tree)
268                 return offset;
269
270         ti = proto_tree_add_text(tree, tvb, offset, -1, "Get Status");
271         tree = proto_item_add_subtree(ti, ett_dsi_status);
272
273         ofs = tvb_get_ntohs(tvb, offset +AFPSTATUS_MACHOFF);
274         proto_tree_add_text(tree, tvb, offset +AFPSTATUS_MACHOFF, 2, "Machine offset: %d", ofs);
275
276         ofs = tvb_get_ntohs(tvb, offset +AFPSTATUS_VERSOFF);
277         proto_tree_add_text(tree, tvb, offset +AFPSTATUS_VERSOFF, 2, "Version offset: %d", ofs);
278
279         ofs = tvb_get_ntohs(tvb, offset +AFPSTATUS_UAMSOFF);
280         proto_tree_add_text(tree, tvb, offset +AFPSTATUS_UAMSOFF, 2, "UAMS offset: %d", ofs);
281
282         ofs = tvb_get_ntohs(tvb, offset +AFPSTATUS_ICONOFF);
283         proto_tree_add_text(tree, tvb, offset +AFPSTATUS_ICONOFF, 2, "Icon offset: %d", ofs);
284
285         ofs = offset +AFPSTATUS_FLAGOFF;
286         ti = proto_tree_add_item(tree, hf_dsi_server_flag, tvb, ofs, 2, FALSE);
287         sub_tree = proto_item_add_subtree(ti, ett_dsi_status_server_flag);
288         proto_tree_add_item(sub_tree, hf_dsi_server_flag_copyfile      , tvb, ofs, 2, FALSE);
289         proto_tree_add_item(sub_tree, hf_dsi_server_flag_passwd        , tvb, ofs, 2, FALSE);
290         proto_tree_add_item(sub_tree, hf_dsi_server_flag_no_save_passwd, tvb, ofs, 2, FALSE);
291         proto_tree_add_item(sub_tree, hf_dsi_server_flag_srv_msg       , tvb, ofs, 2, FALSE);
292         proto_tree_add_item(sub_tree, hf_dsi_server_flag_srv_sig       , tvb, ofs, 2, FALSE);
293         proto_tree_add_item(sub_tree, hf_dsi_server_flag_tcpip         , tvb, ofs, 2, FALSE);
294         proto_tree_add_item(sub_tree, hf_dsi_server_flag_notify        , tvb, ofs, 2, FALSE);
295         proto_tree_add_item(sub_tree, hf_dsi_server_flag_reconnect     , tvb, ofs, 2, FALSE);
296         proto_tree_add_item(sub_tree, hf_dsi_server_flag_directory     , tvb, ofs, 2, FALSE);
297         proto_tree_add_item(sub_tree, hf_dsi_server_flag_utf8_name     , tvb, ofs, 2, FALSE);
298         proto_tree_add_item(sub_tree, hf_dsi_server_flag_fast_copy     , tvb, ofs, 2, FALSE);
299
300         proto_tree_add_item(tree, hf_dsi_server_name, tvb, offset +AFPSTATUS_PRELEN, 1, FALSE);
301
302         flag = tvb_get_ntohs(tvb, ofs);
303         if ((flag & AFPSRVRINFO_SRVSIGNATURE)) {
304                 ofs = offset +AFPSTATUS_PRELEN +tvb_get_guint8(tvb, offset +AFPSTATUS_PRELEN) +1;
305                 if ((ofs & 1))
306                         ofs++;
307
308                 sign_ofs = tvb_get_ntohs(tvb, ofs);
309                 proto_tree_add_text(tree, tvb, ofs, 2, "Signature offset: %d", sign_ofs);
310                 sign_ofs += offset;
311
312                 if ((flag & AFPSRVRINFO_TCPIP)) {
313                         ofs += 2;
314                         adr_ofs =  tvb_get_ntohs(tvb, ofs);
315                         proto_tree_add_text(tree, tvb, ofs, 2, "Network address offset: %d", adr_ofs);
316                         adr_ofs += offset;
317                 }
318
319                 if ((flag & AFPSRVRINFO_SRVDIRECTORY)) {
320                         ofs += 2;
321                         dir_ofs =  tvb_get_ntohs(tvb, ofs);
322                         proto_tree_add_text(tree, tvb, ofs, 2, "Directory services offset: %d", dir_ofs);
323                         dir_ofs += offset;
324                 }
325                 if ((flag & AFPSRVRINFO_SRVUTF8)) {
326                         ofs += 2;
327                         utf_ofs =  tvb_get_ntohs(tvb, ofs);
328                         proto_tree_add_text(tree, tvb, ofs, 2, "UTF8 server name offset: %d", utf_ofs);
329                         utf_ofs += offset;
330                 }
331         }
332
333         ofs = offset +tvb_get_ntohs(tvb, offset +AFPSTATUS_MACHOFF);
334         if (ofs)
335                 proto_tree_add_item(tree, hf_dsi_server_type, tvb, ofs, 1, FALSE);
336
337         ofs = offset +tvb_get_ntohs(tvb, offset +AFPSTATUS_VERSOFF);
338         if (ofs) {
339                 nbe = tvb_get_guint8(tvb, ofs);
340                 ti = proto_tree_add_text(tree, tvb, ofs, 1, "Version list: %d", nbe);
341                 ofs++;
342                 sub_tree = proto_item_add_subtree(ti, ett_dsi_vers);
343                 for (i = 0; i < nbe; i++) {
344                         len = tvb_get_guint8(tvb, ofs);
345                         proto_tree_add_item(sub_tree, hf_dsi_server_vers, tvb, ofs, 1, FALSE);
346                         ofs += len + 1;
347                 }
348         }
349
350         ofs = offset +tvb_get_ntohs(tvb, offset +AFPSTATUS_UAMSOFF);
351         if (ofs) {
352                 nbe = tvb_get_guint8(tvb, ofs);
353                 ti = proto_tree_add_text(tree, tvb, ofs, 1, "UAMS list: %d", nbe);
354                 ofs++;
355                 sub_tree = proto_item_add_subtree(ti, ett_dsi_uams);
356                 for (i = 0; i < nbe; i++) {
357                         len = tvb_get_guint8(tvb, ofs);
358                         proto_tree_add_item(sub_tree, hf_dsi_server_uams, tvb, ofs, 1, FALSE);
359                         ofs += len + 1;
360                 }
361         }
362
363         ofs = offset +tvb_get_ntohs(tvb, offset +AFPSTATUS_ICONOFF);
364         if (ofs)
365                 proto_tree_add_item(tree, hf_dsi_server_icon, tvb, ofs, 256, FALSE);
366
367         if (sign_ofs) {
368                 proto_tree_add_item(tree, hf_dsi_server_signature, tvb, sign_ofs, 16, FALSE);
369         }
370
371         if (adr_ofs) {
372                 proto_tree *adr_tree;
373                 char *tmp;
374                 const guint8 *ip;
375                 guint16 net;
376                 guint8  node;
377                 guint16 port;
378
379                 ofs = adr_ofs;
380                 nbe = tvb_get_guint8(tvb, ofs);
381                 ti = proto_tree_add_text(tree, tvb, ofs, 1, "Address list: %d", nbe);
382                 ofs++;
383                 adr_tree = proto_item_add_subtree(ti, ett_dsi_addr);
384                 for (i = 0; i < nbe; i++) {
385                         guint8 type;
386
387                         len = tvb_get_guint8(tvb, ofs);
388                         type =  tvb_get_guint8(tvb, ofs +1);
389                         switch (type) {
390                         case 1: /* IP */
391                                 ip = tvb_get_ptr(tvb, ofs+2, 4);
392                                 ti = proto_tree_add_text(adr_tree, tvb, ofs, len, "ip: %s", ip_to_str(ip));
393                                 break;
394                         case 2: /* IP + port */
395                                 ip = tvb_get_ptr(tvb, ofs+2, 4);
396                                 port = tvb_get_ntohs(tvb, ofs+6);
397                                 ti = proto_tree_add_text(adr_tree, tvb, ofs, len, "ip %s:%d",ip_to_str(ip),port);
398                                 break;
399                         case 3: /* DDP, atalk_addr_to_str want host order not network */
400                                 net  = tvb_get_ntohs(tvb, ofs+2);
401                                 node = tvb_get_guint8(tvb, ofs +4);
402                                 port = tvb_get_guint8(tvb, ofs +5);
403                                 ti = proto_tree_add_text(adr_tree, tvb, ofs, len, "ddp: %u.%u:%u",
404                                         net, node, port);
405                                 break;
406                         case 4: /* DNS */
407                         case 5: /* SSH tunnel */
408                                 if (len > 2) {
409                                         tmp = g_malloc( len -1);
410                                         tvb_memcpy(tvb, tmp, ofs +2, len -2);
411                                         tmp[len -2] = 0;
412                                         ti = proto_tree_add_text(adr_tree, tvb, ofs, len, "%s: %s", 
413                                                                 (type==4)?"dns":"ssh tunnel", tmp);
414                                         g_free(tmp);
415                                         break;
416                                 }
417                                 else {
418                                         ti = proto_tree_add_text(adr_tree, tvb, ofs, len,"Malformed address type %d", type);
419                                 }
420                                 break;
421                         default:
422                                 ti = proto_tree_add_text(adr_tree, tvb, ofs, len,"Unknow type : %d", type);
423                                 break;
424                         }
425                         len -= 2;
426                         sub_tree = proto_item_add_subtree(ti,ett_dsi_addr_line);
427                         proto_tree_add_item(sub_tree, hf_dsi_server_addr_len, tvb, ofs, 1, FALSE);
428                         ofs++;
429                         proto_tree_add_item(sub_tree, hf_dsi_server_addr_type, tvb, ofs, 1, FALSE);
430                         ofs++;
431                         proto_tree_add_item(sub_tree, hf_dsi_server_addr_value,tvb, ofs, len, FALSE);
432                         ofs += len;
433                 }
434         }
435
436         if (dir_ofs) {
437                 ofs = dir_ofs;
438                 nbe = tvb_get_guint8(tvb, ofs);
439                 ti = proto_tree_add_text(tree, tvb, ofs, 1, "Directory services list: %d", nbe);
440                 ofs++;
441                 sub_tree = proto_item_add_subtree(ti, ett_dsi_directory);
442                 for (i = 0; i < nbe; i++) {
443                         len = tvb_get_guint8(tvb, ofs);
444                         proto_tree_add_item(sub_tree, hf_dsi_server_directory, tvb, ofs, 1, FALSE);
445                         ofs += len + 1;
446                 }
447         }
448         if (utf_ofs) {
449                 guint16 ulen;
450                 char *tmp;
451
452                 ofs = utf_ofs;
453                 ulen = tvb_get_ntohs(tvb, ofs);
454                 tmp = tvb_get_string(tvb, ofs + 2, ulen);
455                 ti = proto_tree_add_text(tree, tvb, ofs, ulen + 2, "UTF8 server name: %s", tmp);
456                 sub_tree = proto_item_add_subtree(ti, ett_dsi_utf8_name);
457                 proto_tree_add_uint(sub_tree, hf_dsi_utf8_server_name_len, tvb, ofs, 2, ulen);
458                 ofs += 2;
459                 proto_tree_add_string(sub_tree, hf_dsi_utf8_server_name, tvb, ofs, ulen, tmp);
460                 ofs += ulen;
461                 g_free(tmp);
462         }
463
464         return offset;
465 }
466
467 static void
468 dissect_dsi_packet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
469 {
470         proto_tree      *dsi_tree;
471         proto_item      *ti;
472         guint8          dsi_flags,dsi_command;
473         guint16         dsi_requestid;
474         gint32          dsi_code;
475         guint32         dsi_length;
476         guint32         dsi_reserved;
477         struct          aspinfo aspinfo;
478         gint            col_info;
479         
480
481         if (check_col(pinfo->cinfo, COL_PROTOCOL))
482                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "DSI");
483         col_info = check_col(pinfo->cinfo, COL_INFO);
484         if (col_info)
485                 col_clear(pinfo->cinfo, COL_INFO);
486
487         dsi_flags = tvb_get_guint8(tvb, 0);
488         dsi_command = tvb_get_guint8(tvb, 1);
489         dsi_requestid = tvb_get_ntohs(tvb, 2);
490         dsi_code = tvb_get_ntohl(tvb, 4);
491         dsi_length = tvb_get_ntohl(tvb, 8);
492         dsi_reserved = tvb_get_ntohl(tvb, 12);
493
494         if (col_info) {
495                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s %s (%u)",
496                         val_to_str(dsi_flags, flag_vals,
497                                    "Unknown flag (0x%02x)"),
498                         val_to_str(dsi_command, func_vals,
499                                    "Unknown function (0x%02x)"),
500                         dsi_requestid);
501         }
502
503
504         if (tree) {
505                 ti = proto_tree_add_item(tree, proto_dsi, tvb, 0, -1, FALSE);
506                 dsi_tree = proto_item_add_subtree(ti, ett_dsi);
507
508                 proto_tree_add_uint(dsi_tree, hf_dsi_flags, tvb,
509                         0, 1, dsi_flags);
510                 proto_tree_add_uint(dsi_tree, hf_dsi_command, tvb,
511                         1, 1, dsi_command);
512                 proto_tree_add_uint(dsi_tree, hf_dsi_requestid, tvb,
513                         2, 2, dsi_requestid);
514                 switch (dsi_flags) {
515
516                 case DSIFL_REQUEST:
517                         proto_tree_add_int(dsi_tree, hf_dsi_offset, tvb,
518                                 4, 4, dsi_code);
519                         break;
520
521                 case DSIFL_REPLY:
522                         proto_tree_add_int(dsi_tree, hf_dsi_error, tvb,
523                                 4, 4, dsi_code);
524                         break;
525                 }
526                 proto_tree_add_uint_format(dsi_tree, hf_dsi_length, tvb,
527                         8, 4, dsi_length,
528                         "Length: %u bytes", dsi_length);
529                 proto_tree_add_uint(dsi_tree, hf_dsi_reserved, tvb,
530                         12, 4, dsi_reserved);
531         }
532         else
533                 dsi_tree = tree;
534         switch (dsi_command) {
535         case DSIFUNC_OPEN:
536                 if (tree) {
537                         dissect_dsi_open_session(tvb, dsi_tree, DSI_BLOCKSIZ);
538                 }
539                 break;
540         case DSIFUNC_ATTN:
541                 if (tree) {
542                         dissect_dsi_attention(tvb, dsi_tree, DSI_BLOCKSIZ);
543                 }
544                 break;
545         case DSIFUNC_STAT:
546                 if (tree && (dsi_flags == DSIFL_REPLY)) {
547                         dissect_dsi_reply_get_status(tvb, dsi_tree, DSI_BLOCKSIZ);
548                 }
549                 break;
550         case DSIFUNC_CMD:
551         case DSIFUNC_WRITE:
552                 {
553                         tvbuff_t   *new_tvb;
554                         int len = tvb_reported_length_remaining(tvb,DSI_BLOCKSIZ);
555
556                         aspinfo.reply = (dsi_flags == DSIFL_REPLY);
557                         aspinfo.command = dsi_command;
558                         aspinfo.seq = dsi_requestid;
559                         aspinfo.code = dsi_code;
560                         pinfo->private_data = &aspinfo;
561                         proto_item_set_len(dsi_tree, DSI_BLOCKSIZ);
562
563                         new_tvb = tvb_new_subset(tvb, DSI_BLOCKSIZ,-1,len);
564                         call_dissector(afp_handle, new_tvb, pinfo, tree);
565                 }
566                 break;
567         default:
568                 if (tree) {
569                         call_dissector(data_handle,
570                             tvb_new_subset(tvb, DSI_BLOCKSIZ, -1, -1),
571                             pinfo, dsi_tree);
572                 }
573                 break;
574         }
575 }
576
577 static guint
578 get_dsi_pdu_len(tvbuff_t *tvb, int offset)
579 {
580         guint32 plen;
581         guint8  dsi_flags,dsi_command;
582
583         dsi_flags = tvb_get_guint8(tvb, offset);
584         dsi_command = tvb_get_guint8(tvb, offset+ 1);
585         if ( dsi_flags > DSIFL_MAX || !dsi_command || dsi_command > DSIFUNC_MAX) 
586         {
587             /* it's not a known dsi pdu start sequence */
588             return tvb_length_remaining(tvb, offset);
589         }
590
591         /*
592          * Get the length of the DSI packet.
593          */
594         plen = tvb_get_ntohl(tvb, offset+8);
595
596         /*
597          * That length doesn't include the length of the header itself;
598          * add that in.
599          */
600         return plen + 16;
601 }
602
603 static void
604 dissect_dsi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
605 {
606         tcp_dissect_pdus(tvb, pinfo, tree, dsi_desegment, 12,
607             get_dsi_pdu_len, dissect_dsi_packet);
608 }
609
610 void
611 proto_register_dsi(void)
612 {
613
614   static hf_register_info hf[] = {
615     { &hf_dsi_flags,
616       { "Flags",            "dsi.flags",
617         FT_UINT8, BASE_HEX, VALS(flag_vals), 0x0,
618         "Indicates request or reply.", HFILL }},
619
620     { &hf_dsi_command,
621       { "Command",          "dsi.command",
622         FT_UINT8, BASE_DEC, VALS(func_vals), 0x0,
623         "Represents a DSI command.", HFILL }},
624
625     { &hf_dsi_requestid,
626       { "Request ID",       "dsi.requestid",
627         FT_UINT16, BASE_DEC, NULL, 0x0,
628         "Keeps track of which request this is.  Replies must match a Request.  IDs must be generated in sequential order.", HFILL }},
629
630     { &hf_dsi_offset,
631       { "Data offset",      "dsi.data_offset",
632         FT_INT32, BASE_DEC, NULL, 0x0,
633         "Data offset", HFILL }},
634
635     { &hf_dsi_error,
636       { "Error code",       "dsi.error_code",
637         FT_INT32, BASE_DEC, VALS(asp_error_vals), 0x0,
638         "Error code", HFILL }},
639
640     { &hf_dsi_length,
641       { "Length",           "dsi.length",
642         FT_UINT32, BASE_DEC, NULL, 0x0,
643         "Total length of the data that follows the DSI header.", HFILL }},
644
645     { &hf_dsi_reserved,
646       { "Reserved",         "dsi.reserved",
647         FT_UINT32, BASE_HEX, NULL, 0x0,
648         "Reserved for future use.  Should be set to zero.", HFILL }},
649         /* asp , afp */
650     { &hf_dsi_utf8_server_name_len,
651       { "Length",          "dsi.utf8_server_name_len",
652         FT_UINT16, BASE_DEC, NULL, 0x0,
653         "UTF8 server name length.", HFILL }},
654     { &hf_dsi_utf8_server_name,
655       { "UTF8 Server name",         "dsi.utf8_server_name",
656         FT_STRING, BASE_NONE, NULL, 0x0,
657         "UTF8 Server name", HFILL }},
658
659     { &hf_dsi_server_name,
660       { "Server name",         "dsi.server_name",
661         FT_UINT_STRING, BASE_NONE, NULL, 0x0,
662         "Server name", HFILL }},
663
664     { &hf_dsi_server_type,
665       { "Server type",         "dsi.server_type",
666         FT_UINT_STRING, BASE_NONE, NULL, 0x0,
667         "Server type", HFILL }},
668
669     { &hf_dsi_server_vers,
670       { "AFP version",         "dsi.server_vers",
671         FT_UINT_STRING, BASE_NONE, NULL, 0x0,
672         "AFP version", HFILL }},
673
674     { &hf_dsi_server_uams,
675       { "UAM",         "dsi.server_uams",
676         FT_UINT_STRING, BASE_NONE, NULL, 0x0,
677         "UAM", HFILL }},
678
679     { &hf_dsi_server_icon,
680       { "Icon bitmap",         "dsi.server_icon",
681         FT_BYTES, BASE_HEX, NULL, 0x0,
682         "Server icon bitmap", HFILL }},
683
684     { &hf_dsi_server_directory,
685       { "Directory service",         "dsi.server_directory",
686         FT_UINT_STRING, BASE_NONE, NULL, 0x0,
687         "Server directory service", HFILL }},
688
689     { &hf_dsi_server_signature,
690       { "Server signature",         "dsi.server_signature",
691         FT_BYTES, BASE_HEX, NULL, 0x0,
692         "Server signature", HFILL }},
693
694     { &hf_dsi_server_flag,
695       { "Flag",         "dsi.server_flag",
696         FT_UINT16, BASE_HEX, NULL, 0x0,
697         "Server capabilities flag", HFILL }},
698     { &hf_dsi_server_flag_copyfile,
699       { "Support copyfile",      "dsi.server_flag.copyfile",
700                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_COPY,
701         "Server support copyfile", HFILL }},
702     { &hf_dsi_server_flag_passwd,
703       { "Support change password",      "dsi.server_flag.passwd",
704                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_PASSWD,
705         "Server support change password", HFILL }},
706     { &hf_dsi_server_flag_no_save_passwd,
707       { "Don't allow save password",      "dsi.server_flag.no_save_passwd",
708                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_NOSAVEPASSWD,
709         "Don't allow save password", HFILL }},
710     { &hf_dsi_server_flag_srv_msg,
711       { "Support server message",      "dsi.server_flag.srv_msg",
712                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVMSGS,
713         "Support server message", HFILL }},
714     { &hf_dsi_server_flag_srv_sig,
715       { "Support server signature",      "dsi.server_flag.srv_sig",
716                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVSIGNATURE,
717         "Support server signature", HFILL }},
718     { &hf_dsi_server_flag_tcpip,
719       { "Support TCP/IP",      "dsi.server_flag.tcpip",
720                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_TCPIP,
721         "Server support TCP/IP", HFILL }},
722     { &hf_dsi_server_flag_notify,
723       { "Support server notifications",      "dsi.server_flag.notify",
724                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVNOTIFY,
725         "Server support notifications", HFILL }},
726     { &hf_dsi_server_flag_reconnect,
727       { "Support server reconnect",      "dsi.server_flag.reconnect",
728                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVRECONNECT,
729         "Server support reconnect", HFILL }},
730     { &hf_dsi_server_flag_directory,
731       { "Support directory services",      "dsi.server_flag.directory",
732                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVDIRECTORY,
733         "Server support directory services", HFILL }},
734     { &hf_dsi_server_flag_utf8_name,
735       { "Support UTF8 server name",      "dsi.server_flag.utf8_name",
736                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_SRVUTF8,
737         "Server support UTF8 server name", HFILL }},
738     { &hf_dsi_server_flag_fast_copy,
739       { "Support fast copy",      "dsi.server_flag.fast_copy",
740                 FT_BOOLEAN, 16, NULL, AFPSRVRINFO_FASTBOZO,
741         "Server support fast copy", HFILL }},
742
743
744     { &hf_dsi_server_addr_len,
745       { "Length",          "dsi.server_addr.len",
746         FT_UINT8, BASE_DEC, NULL, 0x0,
747         "Address length.", HFILL }},
748
749     { &hf_dsi_server_addr_type,
750       { "Type",          "dsi.server_addr.type",
751         FT_UINT8, BASE_DEC, VALS(afp_server_addr_type_vals), 0x0,
752         "Address type.", HFILL }},
753
754     { &hf_dsi_server_addr_value,
755       { "Value",          "dsi.server_addr.value",
756         FT_BYTES, BASE_HEX, NULL, 0x0,
757         "Address value", HFILL }},
758
759     { &hf_dsi_open_type,
760       { "Flags",          "dsi.open_type",
761         FT_UINT8, BASE_DEC, VALS(dsi_open_type_vals), 0x0,
762         "Open session option type.", HFILL }},
763
764     { &hf_dsi_open_len,
765       { "Length",          "dsi.open_len",
766         FT_UINT8, BASE_DEC, NULL, 0x0,
767         "Open session option len", HFILL }},
768
769     { &hf_dsi_open_quantum,
770       { "Quantum",       "dsi.open_quantum",
771         FT_UINT32, BASE_DEC, NULL, 0x0,
772         "Server/Attention quantum", HFILL }},
773
774     { &hf_dsi_open_option,
775       { "Option",          "dsi.open_option",
776         FT_BYTES, BASE_HEX, NULL, 0x0,
777         "Open session options (undecoded)", HFILL }},
778
779     { &hf_dsi_attn_flag,
780       { "Flags",          "dsi.attn_flag",
781         FT_UINT16, BASE_HEX, VALS(dsi_attn_flag_vals), 0xf000,
782         "Server attention flag", HFILL }},
783     { &hf_dsi_attn_flag_shutdown,
784       { "Shutdown",      "dsi.attn_flag.shutdown",
785                 FT_BOOLEAN, 16, NULL, 1<<15,
786         "Attention flag, server is shutting down", HFILL }},
787     { &hf_dsi_attn_flag_crash,
788       { "Crash",      "dsi.attn_flag.crash",
789                 FT_BOOLEAN, 16, NULL, 1<<14,
790         "Attention flag, server crash bit", HFILL }},
791     { &hf_dsi_attn_flag_msg,
792       { "Message",      "dsi.attn_flag.msg",
793                 FT_BOOLEAN, 16, NULL, 1<<13,
794         "Attention flag, server message bit", HFILL }},
795     { &hf_dsi_attn_flag_reconnect,
796       { "Don't reconnect",      "dsi.attn_flag.reconnect",
797                 FT_BOOLEAN, 16, NULL, 1<<12,
798         "Attention flag, don't reconnect bit", HFILL }},
799     { &hf_dsi_attn_flag_time,
800      { "Minutes",          "dsi.attn_flag.time",
801         FT_UINT16, BASE_DEC, NULL, 0xfff,
802         "Number of minutes", HFILL }},
803     { &hf_dsi_attn_flag_bitmap,
804      { "Bitmap",          "dsi.attn_flag.time",
805         FT_UINT16, BASE_HEX, NULL, 0xfff,
806         "Attention extended bitmap", HFILL }},
807
808   };
809
810   static gint *ett[] = {
811     &ett_dsi,
812     &ett_dsi_open,
813     &ett_dsi_attn,
814     &ett_dsi_attn_flag,
815     /* asp afp */
816     &ett_dsi_status,
817     &ett_dsi_status_server_flag,
818     &ett_dsi_vers,
819     &ett_dsi_uams,
820     &ett_dsi_addr,
821     &ett_dsi_addr_line,
822     &ett_dsi_directory,
823     &ett_dsi_utf8_name,
824   };
825   module_t *dsi_module;
826
827   proto_dsi = proto_register_protocol("Data Stream Interface", "DSI", "dsi");
828   proto_register_field_array(proto_dsi, hf, array_length(hf));
829   proto_register_subtree_array(ett, array_length(ett));
830
831   dsi_module = prefs_register_protocol(proto_dsi, NULL);
832   prefs_register_bool_preference(dsi_module, "desegment",
833     "Desegment all DSI messages spanning multiple TCP segments",
834     "Whether the DSI dissector should desegment all messages spanning multiple TCP segments",
835     &dsi_desegment);
836 }
837
838 void
839 proto_reg_handoff_dsi(void)
840 {
841   static dissector_handle_t dsi_handle;
842
843   dsi_handle = create_dissector_handle(dissect_dsi, proto_dsi);
844   dissector_add("tcp.port", TCP_PORT_DSI, dsi_handle);
845
846   data_handle = find_dissector("data");
847   afp_handle = find_dissector("afp");
848 }