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