make dissect_ber_choice take a guint* that will return the
[obnox/wireshark/wip.git] / epan / dissectors / packet-sap.c
1 /* packet-sap.c
2  * Routines for sap packet dissection
3  * RFC 2974
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id$
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  *
13  * Copied from packet-tftp.c
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <string.h>
35 #include <glib.h>
36 #include <epan/packet.h>
37 #include "packet-ipv6.h"
38
39 #define UDP_PORT_SAP    9875
40
41 #define MCAST_SAP_VERSION_MASK 0xE0 /* 3 bits for  SAP version*/
42 #define MCAST_SAP_VERSION_SHIFT 5   /* Right shift 5 bits to get the version */
43 #define MCAST_SAP_VER0 0            /* Version 0 */
44 #define MCAST_SAP_VER1PLUS 1        /* Version 1 or later */
45 static const value_string mcast_sap_ver[] = {
46                   { MCAST_SAP_VER0,     "SAPv0"},
47                   { MCAST_SAP_VER1PLUS, "SAPv1 or later"},
48                   { 0,                  NULL} };
49
50 static const true_false_string mcast_sap_address_type = {
51         "IPv6",
52         "IPv4"
53 };
54
55 static const true_false_string mcast_sap_message_type = {
56         "Deletion",
57         "Announcement"
58 };
59
60 static const true_false_string mcast_sap_crypt_type = {
61         "Payload encrypted",
62         "Payload not encrypted "
63 };
64
65 static const true_false_string mcast_sap_comp_type = {
66         "Payload compressed",
67         "Payload not compressed"
68 };
69
70 static const value_string mcast_sap_auth_ver[] = {
71                   { 1, "SAP authentication header v1"},
72                   { 0,                  NULL} };
73
74 static const true_false_string mcast_sap_auth_pad = {
75         "Authentication subheader padded to 32 bits",
76         "No padding required for the authentication subheader"
77 };
78
79 #define MCAST_SAP_AUTH_TYPE_MASK 0x0F /* 4 bits for the type of the authentication header */
80 #define MCAST_SAP_AUTH_TYPE_PGP 0
81 #define MCAST_SAP_AUTH_TYPE_CMS 1
82 static const value_string mcast_sap_auth_type[] = {
83                   { MCAST_SAP_AUTH_TYPE_PGP,  "PGP"},
84                   { MCAST_SAP_AUTH_TYPE_CMS,  "CMS"},
85                   { 0,                   NULL} };
86
87 #define MCAST_SAP_BIT_A 0x10 /* Address type: 0 IPv4, 1 IPv6 */
88 #define MCAST_SAP_BIT_R 0x08 /* Reserved: Must be 0 */
89 #define MCAST_SAP_BIT_T 0x04 /* Message Type: 0 announcement, 1 deletion */
90 #define MCAST_SAP_BIT_E 0x02 /* Encryption Bit: 1 payload encrypted */
91 #define MCAST_SAP_BIT_C 0x01 /* Compressed Bit: 1 payload zlib compressed */
92
93 #define MCAST_SAP_AUTH_BIT_P 0x10 /* Padding required for the authentication header */
94
95
96 static int proto_sap = -1;
97 static int hf_sap_flags = -1;
98 static int hf_sap_flags_v = -1;
99 static int hf_sap_flags_a = -1;
100 static int hf_sap_flags_r = -1;
101 static int hf_sap_flags_t = -1;
102 static int hf_sap_flags_e = -1;
103 static int hf_sap_flags_c = -1;
104 static int hf_auth_data = -1;
105 static int hf_auth_flags = -1;
106 static int hf_auth_flags_v = -1;
107 static int hf_auth_flags_p = -1;
108 static int hf_auth_flags_t = -1;
109
110 static gint ett_sap = -1;
111 static gint ett_sap_flags = -1;
112 static gint ett_sap_auth = -1;
113 static gint ett_sap_authf = -1;
114
115 static dissector_handle_t sdp_handle;
116
117 static void
118 dissect_sap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
119 {
120         int offset = 0;
121         int sap_version, is_ipv6, is_del, is_enc, is_comp, addr_len;
122         guint8 vers_flags;
123         guint8 auth_len;
124         guint16 tmp1;
125         const guint8 *addr;
126         guint8 auth_flags;
127         tvbuff_t *next_tvb;
128
129         proto_item *si, *sif;
130         proto_tree *sap_tree, *sap_flags_tree;
131
132         if (check_col(pinfo->cinfo, COL_PROTOCOL))
133                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SAP");
134         if (check_col(pinfo->cinfo, COL_INFO))
135                 col_clear(pinfo->cinfo, COL_INFO);
136
137         vers_flags = tvb_get_guint8(tvb, offset);
138         is_ipv6 = vers_flags&MCAST_SAP_BIT_A;
139         is_del = vers_flags&MCAST_SAP_BIT_T;
140         is_enc = vers_flags&MCAST_SAP_BIT_E;
141         is_comp = vers_flags&MCAST_SAP_BIT_C;
142
143         sap_version = (vers_flags&MCAST_SAP_VERSION_MASK)>>MCAST_SAP_VERSION_SHIFT;
144         addr_len = (is_ipv6) ? sizeof(struct e_in6_addr) : 4;
145
146         if (check_col(pinfo->cinfo, COL_INFO)) {
147                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s (v%u)",
148                              (is_del) ? "Deletion" : "Announcement", sap_version);
149         }
150
151         if (tree) {
152           si = proto_tree_add_item(tree, proto_sap, tvb, offset, -1, FALSE);
153           sap_tree = proto_item_add_subtree(si, ett_sap);
154
155           sif = proto_tree_add_uint(sap_tree, hf_sap_flags, tvb, offset, 1, vers_flags);
156           sap_flags_tree = proto_item_add_subtree(sif, ett_sap_flags);
157           proto_tree_add_uint(sap_flags_tree, hf_sap_flags_v, tvb, offset, 1, vers_flags);
158           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_a, tvb, offset, 1, vers_flags);
159           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_r, tvb, offset, 1, vers_flags);
160           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_t, tvb, offset, 1, vers_flags);
161           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_e, tvb, offset, 1, vers_flags);
162           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_c, tvb, offset, 1, vers_flags);
163           offset++;
164
165           auth_len = tvb_get_guint8(tvb, offset);
166           proto_tree_add_text(sap_tree, tvb, offset, 1, "Authentication Length: %u", auth_len);
167           offset++;
168
169           tmp1 = tvb_get_ntohs(tvb, offset);
170           proto_tree_add_text(sap_tree, tvb, offset, 2, "Message Identifier Hash: 0x%x", tmp1);
171           offset +=2;
172
173           addr = tvb_get_ptr(tvb, offset, addr_len);
174           proto_tree_add_text(sap_tree, tvb, offset, addr_len, "Originating Source: %s",
175               (is_ipv6) ? ip6_to_str((const struct e_in6_addr*)addr) : ip_to_str(addr));
176           offset += addr_len;
177
178           /* Authentication data lives in its own subtree */
179           if (auth_len > 0) {
180                   guint32 auth_data_len;
181                   proto_item *sdi, *sai;
182                   proto_tree *sa_tree, *saf_tree;
183                   int has_pad;
184                   guint8 pad_len = 0;
185
186                   auth_data_len = auth_len * sizeof(guint32);
187
188                   sdi = proto_tree_add_item(sap_tree, hf_auth_data, tvb, offset, auth_data_len, FALSE);
189                   sa_tree = proto_item_add_subtree(sdi, ett_sap_auth);
190
191                   auth_flags = tvb_get_guint8(tvb, offset);
192                   sai = proto_tree_add_uint(sa_tree, hf_auth_flags, tvb, offset, 1, auth_flags);
193                   saf_tree = proto_item_add_subtree(sai, ett_sap_authf);
194                   proto_tree_add_uint(saf_tree, hf_auth_flags_v, tvb, offset, 1, auth_flags);
195                   proto_tree_add_boolean(saf_tree, hf_auth_flags_p, tvb, offset, 1, auth_flags);
196                   proto_tree_add_uint(saf_tree, hf_auth_flags_t, tvb, offset, 1, auth_flags);
197
198                   has_pad = auth_flags&MCAST_SAP_AUTH_BIT_P;
199                   if (has_pad)
200                           pad_len = tvb_get_guint8(tvb, offset+auth_data_len-1);
201
202                   if ((int) auth_data_len - pad_len - 1 < 0) {
203                           proto_tree_add_text(sa_tree, tvb, 0, 0,
204                                               "Bogus authentication length (%d) or pad length (%d)",
205                                               auth_len, pad_len);
206                           return;
207                   }
208                   
209
210                   proto_tree_add_text(sa_tree, tvb, offset+1, auth_data_len-pad_len-1,
211                                       "Authentication subheader: (%u byte%s)",
212                                       auth_data_len-1, plurality(auth_data_len-1, "", "s"));
213                   if (has_pad) {
214                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-pad_len, pad_len,
215                                               "Authentication data padding: (%u byte%s)",
216                                               pad_len, plurality(pad_len, "", "s"));
217                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-1, 1,
218                                               "Authentication data pad count: %u byte%s",
219                                               pad_len, plurality(pad_len, "", "s"));
220                   }
221
222                   offset += auth_data_len;
223           }
224           if (is_enc || is_comp) {
225                   char *mangle;
226                   if (is_enc && is_comp) mangle = "compressed and encrypted";
227                   else if (is_enc) mangle = "encrypted";
228                   else mangle = "compressed";
229                   proto_tree_add_text(sap_tree, tvb, offset, -1,
230                                       "The rest of the packet is %s", mangle);
231                   return;
232           }
233
234           /* Do we have the optional payload type aka. MIME content specifier */
235           if (tvb_strneql(tvb, offset, "v=", strlen("v="))) {
236                   gint remaining_len;
237                   guint32 pt_len;
238                   int pt_string_len;
239
240                   remaining_len = tvb_length_remaining(tvb, offset);
241                   if (remaining_len == 0) {
242                       /*
243                        * "tvb_strneql()" failed because there was no
244                        * data left in the packet.
245                        *
246                        * Set the remaining length to 1, so that
247                        * we throw the appropriate exception in
248                        * "tvb_get_ptr()", rather than displaying
249                        * the payload type.
250                        */
251                       remaining_len = 1;
252                   }
253                   pt_string_len = tvb_strnlen(tvb, offset, remaining_len);
254                   if (pt_string_len == -1) {
255                       /*
256                        * We didn't find a terminating '\0'; run to the
257                        * end of the buffer.
258                        */
259                       pt_string_len = remaining_len;
260                       pt_len = pt_string_len;
261                   } else {
262                       /*
263                        * Include the '\0' in the total item length.
264                        */
265                       pt_len = pt_string_len + 1;
266                   }
267                   proto_tree_add_text(sap_tree, tvb, offset, pt_len,
268                       "Payload type: %.*s", pt_string_len,
269                       tvb_get_ptr(tvb, offset, pt_string_len));
270                   offset += pt_len;
271           }
272         }
273
274         /* Done with SAP */
275         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
276         call_dissector(sdp_handle, next_tvb, pinfo, tree);
277
278         return;
279 }
280
281 void proto_register_sap(void)
282 {
283
284   static hf_register_info hf[] = {
285     { &hf_sap_flags,
286       { "Flags",         "sap.flags",
287         FT_UINT8, BASE_HEX, NULL, 0x0,
288         "Bits in the beginning of the SAP header", HFILL }},
289
290     { &hf_sap_flags_v,
291       { "Version Number",         "sap.flags.v",
292         FT_UINT8, BASE_DEC, VALS(mcast_sap_ver), MCAST_SAP_VERSION_MASK,
293         "3 bit version field in the SAP header", HFILL }},
294
295     { &hf_sap_flags_a,
296       { "Address Type",           "sap.flags.a",
297         FT_BOOLEAN, 8, TFS(&mcast_sap_address_type), MCAST_SAP_BIT_A,
298         "Originating source address type", HFILL }},
299
300     { &hf_sap_flags_r,
301       { "Reserved",               "sap.flags.r",
302         FT_BOOLEAN, 8, TFS(&flags_set_truth), MCAST_SAP_BIT_R,
303         "Reserved", HFILL }},
304
305     { &hf_sap_flags_t,
306       { "Message Type",           "sap.flags.t",
307         FT_BOOLEAN, 8, TFS(&mcast_sap_message_type), MCAST_SAP_BIT_T,
308         "Announcement type", HFILL }},
309
310     { &hf_sap_flags_e,
311       { "Encryption Bit",         "sap.flags.e",
312         FT_BOOLEAN, 8, TFS(&mcast_sap_crypt_type), MCAST_SAP_BIT_E,
313         "Encryption", HFILL }},
314
315     { &hf_sap_flags_c,
316       { "Compression Bit",         "sap.flags.c",
317         FT_BOOLEAN, 8, TFS(&mcast_sap_comp_type), MCAST_SAP_BIT_C,
318         "Compression", HFILL }},
319
320     { &hf_auth_data,
321       { "Authentication data",     "sap.auth",
322         FT_NONE, BASE_NONE, NULL, 0x0,
323         "Auth data", HFILL }},
324
325     { &hf_auth_flags,
326       { "Authentication data flags", "sap.auth.flags",
327         FT_UINT8, BASE_HEX, NULL, 0x0,
328         "Auth flags", HFILL }},
329
330     { &hf_auth_flags_v,
331       { "Version Number",         "sap.auth.flags.v",
332         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_ver), MCAST_SAP_VERSION_MASK,
333         "Version", HFILL }},
334
335     { &hf_auth_flags_p,
336       { "Padding Bit",            "sap.auth.flags.p",
337         FT_BOOLEAN, 8, TFS(&mcast_sap_auth_pad), MCAST_SAP_AUTH_BIT_P,
338         "Compression", HFILL }},
339
340     { &hf_auth_flags_t,
341       { "Authentication Type",         "sap.auth.flags.t",
342         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_type), MCAST_SAP_AUTH_TYPE_MASK,
343         "Auth type", HFILL }}
344   };
345   static gint *ett[] = {
346     &ett_sap,
347     &ett_sap_flags,
348     &ett_sap_auth,
349     &ett_sap_authf,
350   };
351
352   proto_sap = proto_register_protocol("Session Announcement Protocol", "SAP",
353                                       "sap");
354   proto_register_field_array(proto_sap, hf, array_length(hf));
355   proto_register_subtree_array(ett, array_length(ett));
356 }
357
358 void
359 proto_reg_handoff_sap(void)
360 {
361   dissector_handle_t sap_handle;
362
363   sap_handle = create_dissector_handle(dissect_sap, proto_sap);
364   dissector_add("udp.port", UDP_PORT_SAP, sap_handle);
365
366   /*
367    * Get a handle for the SDP dissector.
368    */
369   sdp_handle = find_dissector("sdp");
370 }