Propset eol-style and keywords
[obnox/wireshark/wip.git] / epan / dissectors / packet-daap.c
1 /* packet-daap.c
2  * Routines for Digital Audio Access Protocol dissection
3  * Copyright 2004, Kelly Byrd <kbyrd@memcpy.com>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31
32 #include <epan/packet.h>
33 #include <epan/dissectors/packet-http.h>
34
35 #define TCP_PORT_DAAP 3689
36
37 /* DAAP tags */
38 /* Container tags */
39 #define daap_mcon       0x6d636f6e
40 #define daap_msrv       0x6d737276
41 #define daap_mccr       0x6d636372
42 #define daap_mdcl       0x6d64636c
43 #define daap_mlog       0x6d6c6f67
44 #define daap_mupd       0x6d757064
45 #define daap_avdb       0x61766462
46 #define daap_mlcl       0x6d6c636c
47 #define daap_mlit       0x6d6c6974
48 #define daap_mbcl       0x6d62636c
49 #define daap_adbs       0x61646273
50 #define daap_aply       0x61706c79
51 #define daap_apso       0x6170736f
52 #define daap_mudl       0x6d75646c
53 #define daap_abro       0x6162726f
54 #define daap_abal       0x6162616c
55 #define daap_abcp       0x61626370
56 #define daap_abgn       0x6162676e
57 #define daap_prsv       0x70727376
58 #define daap_arif       0x61726966
59 /* String tags */
60 #define daap_minm       0x6d696e6d
61 #define daap_msts       0x6d737473
62 #define daap_mcnm       0x6d636e6d
63 #define daap_mcna       0x6d636e61
64 #define daap_asal       0x6173616c
65 #define daap_asar       0x61736172
66 #define daap_ascm       0x6173636d
67 #define daap_asfm       0x6173666d
68 #define daap_aseq       0x61736571
69 #define daap_asgn       0x6173676e
70 #define daap_asdt       0x61736474
71 #define daap_asul       0x6173756c
72 /* uint64 tags */
73 #define daap_mper       0x6d706572
74 /* uint32 tags */
75 #define daap_mstt       0x6d737474
76 #define daap_musr       0x6d757372
77 #define daap_miid       0x6d696964
78 #define daap_mcti       0x6d637469
79 #define daap_mpco       0x6d70636f
80 #define daap_mimc       0x6d696d63
81 #define daap_mrco       0x6d72636f
82 #define daap_mtco       0x6d74636f
83 #define daap_mstm       0x6d73746d
84 #define daap_msdc       0x6d736463
85 #define daap_mlid       0x6d6c6964
86 #define daap_msur       0x6d737572
87 #define daap_asda       0x61736461
88 #define daap_asdm       0x6173646d
89 #define daap_assr       0x61737372
90 #define daap_assz       0x6173737a
91 #define daap_asst       0x61737374
92 #define daap_assp       0x61737370
93 #define daap_astm       0x6173746d
94 #define daap_aeNV       0x61654e56
95 /* uint16 tags */
96 #define daap_mcty       0x6d637479
97 #define daap_asbt       0x61736274
98 #define daap_asbr       0x61736272
99 #define daap_asdc       0x61736463
100 #define daap_asdn       0x6173646e
101 #define daap_astc       0x61737463
102 #define daap_astn       0x6173746e
103 #define daap_asyr       0x61737972
104 /* byte  tags */
105 #define daap_mikd       0x6d696b64
106 #define daap_msau       0x6d736175
107 #define daap_msty       0x6d737479
108 #define daap_asrv       0x61737276
109 #define daap_asur       0x61737572
110 #define daap_asdk       0x6173646b
111 /* boolean  tags */
112 #define daap_mslr       0x6d736c72
113 #define daap_msal       0x6d73616c
114 #define daap_msup       0x6d737570
115 #define daap_mspi       0x6d737069
116 #define daap_msex       0x6d736578
117 #define daap_msbr       0x6d736272
118 #define daap_msqy       0x6d737179
119 #define daap_msix       0x6d736978
120 #define daap_msrs       0x6d737273
121 #define daap_asco       0x6173636f
122 #define daap_asdb       0x61736462
123 #define daap_abpl       0x6162706c
124 #define daap_aeSP       0x61655350
125 /* version (32-bit)*/
126 #define daap_mpro       0x6d70726f
127 #define daap_apro       0x6170726f
128
129
130 /* Initialize the protocol and registered fields */
131 static int proto_daap = -1;
132 static int hf_daap_name = -1;
133 static int hf_daap_size = -1;
134
135 /* Initialize the subtree pointers */
136 static gint ett_daap = -1;
137 static gint ett_daap_sub = -1;
138
139 /* Forward declarations */
140 static int dissect_daap_one_tag(proto_tree *tree, tvbuff_t *tvb, int offset, int length);
141
142 static void
143 dissect_daap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
144 {
145
146    proto_item *ti;
147    proto_tree *daap_tree;
148    int offset = 0;
149    gboolean is_request = (pinfo->destport == TCP_PORT_DAAP);
150    
151    
152    if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
153       col_set_str(pinfo->cinfo, COL_PROTOCOL, "DAAP");
154    
155    
156    if (check_col(pinfo->cinfo, COL_INFO)) {
157       if (is_request) {
158          col_add_str(pinfo->cinfo, COL_INFO, "DAAP Request");
159       } else {
160          /* This is done in two functions on purpose. If the tvb_get_xxx()
161           * functions fail, at least something will be in the info column
162           */
163          col_add_str(pinfo->cinfo, COL_INFO, "DAAP Response");
164          col_append_fstr(pinfo->cinfo, COL_INFO, " [tag: %s, size: %d]",
165                          tvb_get_string(tvb, offset, 4), 
166                          tvb_get_ntohl(tvb, offset+4));
167       }
168    }
169
170    
171    
172    if (tree) {
173       ti = proto_tree_add_item(tree, proto_daap, tvb, 0, -1, FALSE);
174       daap_tree = proto_item_add_subtree(ti, ett_daap);     
175       dissect_daap_one_tag(daap_tree, tvb, offset, 0);
176    }
177 }
178
179 static int
180 dissect_daap_one_tag(proto_tree *tree, tvbuff_t *tvb, int offset, int length)
181 {
182    unsigned int tagname;
183    int tagsize;
184    int new_offset;
185    proto_item *ti = NULL;
186    proto_item *ti2 = NULL;
187    proto_tree *new_tree = NULL;
188
189    do {
190       if (!tvb_offset_exists(tvb, offset)) 
191          break;
192
193       tagname = tvb_get_ntohl(tvb, offset);
194       tagsize = tvb_get_ntohl(tvb, offset+4);
195       ti = proto_tree_add_text(tree, tvb, offset, tagsize+8, 
196                                "Tag: %c%c%c%c, Size: %d", 
197                                tvb_get_guint8(tvb, offset),
198                                tvb_get_guint8(tvb, offset+1),
199                                tvb_get_guint8(tvb, offset+2),
200                                tvb_get_guint8(tvb, offset+3),
201                                tagsize);
202
203       ti2 = proto_tree_add_item(tree, hf_daap_name, tvb, offset, 4, FALSE);
204       PROTO_ITEM_SET_HIDDEN(ti2);
205       ti2 = proto_tree_add_item(tree, hf_daap_size, tvb, offset+4, 4, FALSE);
206       PROTO_ITEM_SET_HIDDEN(ti2);
207       offset += 8;
208       length -= 8;
209
210       switch (tagname) {
211       case daap_mcon:
212       case daap_msrv:
213       case daap_mccr:
214       case daap_mdcl:
215       case daap_mlog:
216       case daap_mupd:
217       case daap_avdb:
218       case daap_mlcl:
219       case daap_mlit:
220       case daap_mbcl:
221       case daap_adbs:
222       case daap_aply:
223       case daap_apso:
224       case daap_mudl:
225       case daap_abro:
226       case daap_abal:
227       case daap_abcp:
228       case daap_abgn:
229       case daap_prsv:
230       case daap_arif:
231          /* Container tags */
232          new_tree = proto_item_add_subtree(ti, ett_daap_sub);
233          new_offset = dissect_daap_one_tag(new_tree, tvb, offset, 
234                                            tagsize);
235          break;
236       case daap_minm:
237       case daap_msts:
238       case daap_mcnm:
239       case daap_mcna:
240       case daap_asal:
241       case daap_asar:
242       case daap_ascm:
243       case daap_asfm:
244       case daap_aseq:
245       case daap_asgn:
246       case daap_asdt:
247       case daap_asul:
248          /* Tags contain strings */
249          proto_item_append_text(ti, ", Data: %s", 
250                                 tvb_get_string(tvb, offset, tagsize));
251          break;
252       case daap_mper:
253          /* Tags conain uint64 */
254          proto_item_append_text(ti, ", Persistent Id: %" PRIu64, 
255                                 tvb_get_ntoh64(tvb, offset));
256          break;
257       case daap_mstt:
258          proto_item_append_text(ti, ", Status: %d", 
259                                 tvb_get_ntohl(tvb, offset));
260          break;
261       case daap_musr:
262       case daap_msur:
263          proto_item_append_text(ti, ", Revision: %d", 
264                                 tvb_get_ntohl(tvb, offset));
265          break;
266       case daap_miid:
267       case daap_mcti:
268       case daap_mpco:
269       case daap_mlid:
270          proto_item_append_text(ti, ", Id: %d", 
271                                 tvb_get_ntohl(tvb, offset));
272          break;
273       case daap_mrco:
274       case daap_mtco:
275       case daap_mimc:
276       case daap_msdc:
277          proto_item_append_text(ti, ", Count: %d", 
278                                 tvb_get_ntohl(tvb, offset));
279          break;
280       case daap_mstm:
281          proto_item_append_text(ti, ", Timeout: %d seconds", 
282                                 tvb_get_ntohl(tvb, offset));
283          break;
284       case daap_asda:
285       case daap_asdm:
286       case daap_assr:
287       case daap_assz:
288       case daap_asst:
289       case daap_assp:
290       case daap_astm:
291       case daap_aeNV:
292          /* Tags conain uint32 */
293          proto_item_append_text(ti, ", Data: %d", 
294                                 tvb_get_ntohl(tvb, offset));
295          break;
296
297       case daap_mcty:
298       case daap_asbt:
299       case daap_asbr:
300       case daap_asdc:
301       case daap_asdn:
302       case daap_astc:
303       case daap_astn:
304       case daap_asyr:
305          /* Tags conain uint16 */
306          proto_item_append_text(ti, ", Data: %d", 
307                                 tvb_get_ntohs(tvb, offset));
308          break;
309
310       case daap_mikd:
311       case daap_msau:
312       case daap_msty:
313       case daap_asrv:
314       case daap_asur:
315       case daap_asdk:
316          /* Tags conain uint8 */
317          proto_item_append_text(ti, ", Data: %d", 
318                                 tvb_get_guint8(tvb, offset));
319
320          break;
321
322       case daap_mslr:
323       case daap_msal:
324       case daap_msup:
325       case daap_mspi:
326       case daap_msex:
327       case daap_msbr:
328       case daap_msqy:
329       case daap_msix:
330       case daap_msrs:
331       case daap_asco:
332       case daap_asdb:
333       case daap_abpl:
334       case daap_aeSP:
335          /* Tags ARE boolean. Data is (uint8), but it seems
336           * the value is always zero. So, if the tag is present
337           * the "bool" is true. 
338           */
339          proto_item_append_text(ti, ", Data: True");
340          break;
341
342       case daap_mpro:
343       case daap_apro:
344          /* Tags conain version (uint32) */
345          proto_item_append_text(ti, ", Version: %d.%d.%d.%d",
346                                 tvb_get_guint8(tvb, offset),
347                                 tvb_get_guint8(tvb, offset+1),
348                                 tvb_get_guint8(tvb, offset+2),
349                                 tvb_get_guint8(tvb, offset+3));
350          break;
351
352       default: 
353          break;
354       }
355       offset += tagsize;
356       length -= tagsize;
357    } while (length > 0);
358    return offset;
359 }
360
361
362 /* Register the protocol with Ethereal */
363 void
364 proto_register_daap(void)
365 {        
366    
367    static hf_register_info hf[] = {
368       { &hf_daap_name,
369         { "Name", "daap.name", FT_STRING, BASE_NONE, NULL, 0x0, 
370           "Tag Name", HFILL}
371       },
372       { &hf_daap_size,
373         { "Size", "daap.size", FT_UINT32, BASE_DEC, NULL, 0x0, 
374           "Tag Size", HFILL }
375       }
376    };
377
378    static gint *ett[] = {
379       &ett_daap,
380       &ett_daap_sub,
381    };
382
383    proto_daap = proto_register_protocol("Digital Audio Access Protocol",
384                                         "DAAP", "daap");
385
386    proto_register_field_array(proto_daap, hf, array_length(hf));
387    proto_register_subtree_array(ett, array_length(ett));
388 }
389
390
391 /* If this dissector uses sub-dissector registration add a registration routine.
392    This format is required because a script is used to find these routines and
393    create the code that calls these routines.
394 */
395 void
396 proto_reg_handoff_daap(void)
397 {
398     dissector_handle_t daap_handle;
399
400     daap_handle = create_dissector_handle(dissect_daap, proto_daap);
401     http_dissector_add(TCP_PORT_DAAP, daap_handle);
402 }