Add tvbuff class.
[obnox/wireshark/wip.git] / packet-sap.c
1 /* packet-tftp.c
2  * Routines for sap packet dissection
3  * <draft-ietf-mmusic-sap-v2-03.txt>
4  *
5  * Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-sap.c,v 1.7 2000/05/11 08:15:43 gram Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@zing.org>
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 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <string.h>
43 #include <glib.h>
44 #include "packet.h"
45 #include "packet-ipv6.h"
46 #include "packet-sdp.h"
47
48 #define UDP_PORT_SAP    9875
49
50 #define MCAST_SAP_VERSION_MASK 0xE0 /* 3 bits for  SAP version*/
51 #define MCAST_SAP_VERSION_SHIFT 5   /* Right shift 5 bits to get the version */
52 #define MCAST_SAP_VER0 0            /* Version 0 */
53 #define MCAST_SAP_VER1PLUS 1        /* Version 1 or later */
54 static const value_string mcast_sap_ver[] = {
55                   { MCAST_SAP_VER0,     "SAPv0"},
56                   { MCAST_SAP_VER1PLUS, "SAPv1 or later"},
57                   { 0,                  NULL} };
58
59 static const true_false_string mcast_sap_address_type = {
60         "IPv6",
61         "IPv4"
62 };
63
64 static const true_false_string flags_set_truth = {
65         "Set",
66         "Not set"
67 };
68
69 static const true_false_string mcast_sap_message_type = {
70         "Deletion",
71         "Announcement"
72 };
73
74 static const true_false_string mcast_sap_crypt_type = {
75         "Payload encrypted",
76         "Payload not encrypted "
77 };
78
79 static const true_false_string mcast_sap_comp_type = {
80         "Payload compressed",
81         "Payload not compressed"
82 };
83
84 static const value_string mcast_sap_auth_ver[] = {
85                   { 1, "SAP authentication header v1"},
86                   { 0,                  NULL} };
87
88 static const true_false_string mcast_sap_auth_pad = {
89         "Authentication subheader padded to 32 bits",
90         "No padding required for the authentication subheader"
91 };
92
93 #define MCAST_SAP_AUTH_TYPE_MASK 0x0F /* 4 bits for the type of the authentication header */
94 #define MCAST_SAP_AUTH_TYPE_PGP 0
95 #define MCAST_SAP_AUTH_TYPE_CMS 1
96 static const value_string mcast_sap_auth_type[] = {
97                   { MCAST_SAP_AUTH_TYPE_PGP,  "PGP"},
98                   { MCAST_SAP_AUTH_TYPE_CMS,  "CMS"},
99                   { 0,                   NULL} };
100
101 #define MCAST_SAP_BIT_A 0x10 /* Address type: 0 IPv4, 1 IPv6 */
102 #define MCAST_SAP_BIT_R 0x08 /* Reserved: Must be 0 */
103 #define MCAST_SAP_BIT_T 0x04 /* Message Type: 0 announcement, 1 deletion */
104 #define MCAST_SAP_BIT_E 0x02 /* Encryption Bit: 1 payload encrypted */
105 #define MCAST_SAP_BIT_C 0x01 /* Compressed Bit: 1 payload zlib compressed */
106
107 #define MCAST_SAP_AUTH_BIT_P 0x10 /* Padding required for the authentication header */
108
109
110 static int proto_sap = -1;
111 static int hf_sap_flags = -1;
112 static int hf_sap_flags_v = -1;
113 static int hf_sap_flags_a = -1;
114 static int hf_sap_flags_r = -1;
115 static int hf_sap_flags_t = -1;
116 static int hf_sap_flags_e = -1;
117 static int hf_sap_flags_c = -1;
118 static int hf_auth_data = -1;
119 static int hf_auth_flags = -1;
120 static int hf_auth_flags_v = -1;
121 static int hf_auth_flags_p = -1;
122 static int hf_auth_flags_t = -1;
123
124 static gint ett_sap = -1;
125 static gint ett_sap_flags = -1;
126 static gint ett_sap_auth = -1;
127 static gint ett_sap_authf = -1;
128
129 static void
130 dissect_sap(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
131 {
132         int sap_version, is_ipv6, is_del, is_enc, is_comp, addr_len;
133         guint8 auth_len;
134         guint16 tmp1;
135         guint32 tmp2;
136
137         proto_item *si, *sif;
138         proto_tree *sap_tree, *sap_flags_tree;
139
140         is_ipv6 = pd[offset]&MCAST_SAP_BIT_A;
141         is_del = pd[offset]&MCAST_SAP_BIT_T;
142         is_enc = pd[offset]&MCAST_SAP_BIT_E;
143         is_comp = pd[offset]&MCAST_SAP_BIT_C;
144
145         sap_version = (pd[offset]&MCAST_SAP_VERSION_MASK)>>MCAST_SAP_VERSION_SHIFT;
146         addr_len = (is_ipv6) ? sizeof(struct e_in6_addr) : 4;
147
148         if (check_col(fd, COL_PROTOCOL))
149                 col_add_str(fd, COL_PROTOCOL, "SAP");
150         
151         if (check_col(fd, COL_INFO)) {
152                 col_add_fstr(fd, COL_INFO, "%s (v%u)",
153                              (is_del) ? "Deletion" : "Announcement", sap_version);
154         }
155
156         if (tree) {
157           si = proto_tree_add_item(tree, proto_sap, NullTVB, offset, END_OF_FRAME, NULL);
158           sap_tree = proto_item_add_subtree(si, ett_sap);
159
160           sif = proto_tree_add_item(sap_tree, hf_sap_flags, NullTVB, offset, 1, pd[offset]);
161           sap_flags_tree = proto_item_add_subtree(sif, ett_sap_flags);
162           proto_tree_add_item(sap_flags_tree, hf_sap_flags_v, NullTVB, offset, 1, pd[offset]);
163           proto_tree_add_item(sap_flags_tree, hf_sap_flags_a, NullTVB, offset, 1, pd[offset]);
164           proto_tree_add_item(sap_flags_tree, hf_sap_flags_r, NullTVB, offset, 1, pd[offset]);
165           proto_tree_add_item(sap_flags_tree, hf_sap_flags_t, NullTVB, offset, 1, pd[offset]);
166           proto_tree_add_item(sap_flags_tree, hf_sap_flags_e, NullTVB, offset, 1, pd[offset]);
167           proto_tree_add_item(sap_flags_tree, hf_sap_flags_c, NullTVB, offset, 1, pd[offset]);
168           offset++;
169
170           proto_tree_add_text(sap_tree, NullTVB, offset, 1, "Authentication Length: %u", pd[offset]);
171           auth_len = pd[offset];
172           offset++;
173
174           tmp1 = pntohs(pd+offset);
175           proto_tree_add_text(sap_tree, NullTVB, offset, 2, "Message Identifier Hash: 0x%x", tmp1);
176           offset +=2;
177
178           proto_tree_add_text(sap_tree, NullTVB, offset, addr_len, "Originating Source: %s",
179                               (is_ipv6) ? ip6_to_str((struct e_in6_addr*)(pd+offset)) : ip_to_str(pd+offset));
180           offset += addr_len;
181
182           /* Authentication data lives in its own subtree */
183           if (auth_len > 0) {
184                   guint32 auth_data_len;
185                   proto_item *sdi, *sai;
186                   proto_tree *sa_tree, *saf_tree;
187                   int has_pad;
188                   guint8 pad_len = 0;
189
190                   auth_data_len = auth_len * sizeof(guint32);
191
192                   sdi = proto_tree_add_item(sap_tree, hf_auth_data, NullTVB, offset, auth_data_len, pd[offset]);
193                   sa_tree = proto_item_add_subtree(sdi, ett_sap_auth);
194
195                   sai = proto_tree_add_item(sa_tree, hf_auth_flags, NullTVB, offset, 1, pd[offset]);
196                   saf_tree = proto_item_add_subtree(sai, ett_sap_authf);
197                   proto_tree_add_item(saf_tree, hf_auth_flags_v, NullTVB, offset, 1, pd[offset]);
198                   proto_tree_add_item(saf_tree, hf_auth_flags_p, NullTVB, offset, 1, pd[offset]);
199                   proto_tree_add_item(saf_tree, hf_auth_flags_t, NullTVB, offset, 1, pd[offset]);
200
201                   has_pad = pd[offset]&MCAST_SAP_AUTH_BIT_P;
202                   if (has_pad) pad_len = *(pd+offset+auth_data_len-1);
203
204                   proto_tree_add_text(sa_tree, NullTVB, offset+1, auth_data_len-pad_len-1,
205                                       "Authentication subheader: (%u byte%s)",
206                                       auth_data_len-1, plurality(auth_data_len-1, "", "s"));
207                   if (has_pad) {
208                           proto_tree_add_text(sa_tree, NullTVB, offset+auth_data_len-pad_len, pad_len,
209                                               "Authentication data padding: (%u byte%s)",
210                                               pad_len, plurality(pad_len, "", "s"));
211                           proto_tree_add_text(sa_tree, NullTVB, offset+auth_data_len-1, 1,
212                                               "Authentication data pad count: %u byte%s",
213                                               pad_len, plurality(pad_len, "", "s"));
214                   }
215
216                   offset += auth_data_len;
217           }
218
219           if (is_enc) { /* Encrypted payload implies valid timeout in the SAP header */
220                   tmp2 = pntohl(pd+offset);
221                   proto_tree_add_text(sap_tree, NullTVB, offset, 4, "Timeout: %u", tmp2);
222                   offset += sizeof(guint32);
223           }
224
225           if (is_enc || is_comp) {
226                   proto_tree_add_text(sap_tree, NullTVB, offset, END_OF_FRAME,
227                                       "Rest of the packet is encrypted or compressed");
228                   return;
229           }
230
231           /* Do we have the optional payload type aka. MIME content specifier */
232           if (strncasecmp(pd+offset, "v=", strlen("v="))) {
233                   guint32 pt_len = strlen(pd+offset); /* BUG: should use strnlen */
234                   proto_tree_add_text(sap_tree, NullTVB, offset, pt_len, "Payload type: %s", pd+offset);
235                   offset += pt_len;
236                   if (pd[offset] == '\0')
237                           offset++; /* Skip possible '\0' */
238           }
239           
240           /* Done with SAP */
241           dissect_sdp(pd, offset, fd, tree);
242         }
243
244         return;
245 }
246
247 void proto_register_sap(void)
248 {
249
250   static hf_register_info hf[] = {
251     { &hf_sap_flags,
252       { "Flags",         "sap.flags",
253         FT_UINT8, BASE_HEX, NULL, 0x0,
254         "Bits in the beginning of the SAP header" }},
255
256     { &hf_sap_flags_v,
257       { "Version Number",         "sap.flags.v",
258         FT_UINT8, BASE_DEC, VALS(mcast_sap_ver), MCAST_SAP_VERSION_MASK,
259         "3 bit version field in the SAP header" }},
260
261     { &hf_sap_flags_a,
262       { "Address Type",           "sap.flags.a",
263         FT_BOOLEAN, 8, TFS(&mcast_sap_address_type), MCAST_SAP_BIT_A,
264         "Originating source address type" }},
265
266     { &hf_sap_flags_r,
267       { "Reserved",               "sap.flags.r",
268         FT_BOOLEAN, 8, TFS(&flags_set_truth), MCAST_SAP_BIT_R,
269         "Reserved" }},
270
271     { &hf_sap_flags_t,
272       { "Message Type",           "sap.flags.t",
273         FT_BOOLEAN, 8, TFS(&mcast_sap_message_type), MCAST_SAP_BIT_T,
274         "Announcement type" }},
275
276     { &hf_sap_flags_e,
277       { "Encryption Bit",         "sap.flags.e",
278         FT_BOOLEAN, 8, TFS(&mcast_sap_crypt_type), MCAST_SAP_BIT_E,
279         "Encryption" }},
280
281     { &hf_sap_flags_c,
282       { "Compression Bit",         "sap.flags.c",
283         FT_BOOLEAN, 8, TFS(&mcast_sap_comp_type), MCAST_SAP_BIT_C,
284         "Compression" }},
285
286     { &hf_auth_data,
287       { "Authentication data",     "sap.auth",
288         FT_NONE, BASE_NONE, NULL, 0x0,
289         "Auth data" }},
290
291     { &hf_auth_flags,
292       { "Authentication data flags", "sap.auth.flags",
293         FT_UINT8, BASE_HEX, NULL, 0x0,
294         "Auth flags" }},
295
296     { &hf_auth_flags_v,
297       { "Version Number",         "sap.auth.flags.v",
298         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_ver), MCAST_SAP_VERSION_MASK,
299         "Version" }},
300
301     { &hf_auth_flags_p,
302       { "Padding Bit",            "sap.auth.flags.p",
303         FT_BOOLEAN, 8, TFS(&mcast_sap_auth_pad), MCAST_SAP_AUTH_BIT_P,
304         "Compression" }},
305
306     { &hf_auth_flags_t,
307       { "Authentication Type",         "sap.auth.flags.t",
308         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_type), MCAST_SAP_AUTH_TYPE_MASK,
309         "Auth type" }}
310   };
311   static gint *ett[] = {
312     &ett_sap,
313     &ett_sap_flags,
314     &ett_sap_auth,
315     &ett_sap_authf,
316   };
317
318   proto_sap = proto_register_protocol("Session Announcement Protocol", "sap");
319   proto_register_field_array(proto_sap, hf, array_length(hf));
320   proto_register_subtree_array(ett, array_length(ett));
321 }
322
323 void
324 proto_reg_handoff_sap(void)
325 {
326   dissector_add("udp.port", UDP_PORT_SAP, dissect_sap);
327 }