Replace the types from sys/types.h and netinet/in.h by their glib.h
[obnox/wireshark/wip.git] / 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: packet-sap.c,v 1.28 2002/08/02 23:36:00 jmayer Exp $
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 flags_set_truth = {
56         "Set",
57         "Not set"
58 };
59
60 static const true_false_string mcast_sap_message_type = {
61         "Deletion",
62         "Announcement"
63 };
64
65 static const true_false_string mcast_sap_crypt_type = {
66         "Payload encrypted",
67         "Payload not encrypted "
68 };
69
70 static const true_false_string mcast_sap_comp_type = {
71         "Payload compressed",
72         "Payload not compressed"
73 };
74
75 static const value_string mcast_sap_auth_ver[] = {
76                   { 1, "SAP authentication header v1"},
77                   { 0,                  NULL} };
78
79 static const true_false_string mcast_sap_auth_pad = {
80         "Authentication subheader padded to 32 bits",
81         "No padding required for the authentication subheader"
82 };
83
84 #define MCAST_SAP_AUTH_TYPE_MASK 0x0F /* 4 bits for the type of the authentication header */
85 #define MCAST_SAP_AUTH_TYPE_PGP 0
86 #define MCAST_SAP_AUTH_TYPE_CMS 1
87 static const value_string mcast_sap_auth_type[] = {
88                   { MCAST_SAP_AUTH_TYPE_PGP,  "PGP"},
89                   { MCAST_SAP_AUTH_TYPE_CMS,  "CMS"},
90                   { 0,                   NULL} };
91
92 #define MCAST_SAP_BIT_A 0x10 /* Address type: 0 IPv4, 1 IPv6 */
93 #define MCAST_SAP_BIT_R 0x08 /* Reserved: Must be 0 */
94 #define MCAST_SAP_BIT_T 0x04 /* Message Type: 0 announcement, 1 deletion */
95 #define MCAST_SAP_BIT_E 0x02 /* Encryption Bit: 1 payload encrypted */
96 #define MCAST_SAP_BIT_C 0x01 /* Compressed Bit: 1 payload zlib compressed */
97
98 #define MCAST_SAP_AUTH_BIT_P 0x10 /* Padding required for the authentication header */
99
100
101 static int proto_sap = -1;
102 static int hf_sap_flags = -1;
103 static int hf_sap_flags_v = -1;
104 static int hf_sap_flags_a = -1;
105 static int hf_sap_flags_r = -1;
106 static int hf_sap_flags_t = -1;
107 static int hf_sap_flags_e = -1;
108 static int hf_sap_flags_c = -1;
109 static int hf_auth_data = -1;
110 static int hf_auth_flags = -1;
111 static int hf_auth_flags_v = -1;
112 static int hf_auth_flags_p = -1;
113 static int hf_auth_flags_t = -1;
114
115 static gint ett_sap = -1;
116 static gint ett_sap_flags = -1;
117 static gint ett_sap_auth = -1;
118 static gint ett_sap_authf = -1;
119
120 static dissector_handle_t sdp_handle;
121
122 static void
123 dissect_sap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
124 {
125         int offset = 0;
126         int sap_version, is_ipv6, is_del, is_enc, is_comp, addr_len;
127         guint8 vers_flags;
128         guint8 auth_len;
129         guint16 tmp1;
130         const guint8 *addr;
131         guint8 auth_flags;
132         tvbuff_t *next_tvb;
133
134         proto_item *si, *sif;
135         proto_tree *sap_tree, *sap_flags_tree;
136
137         if (check_col(pinfo->cinfo, COL_PROTOCOL))
138                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SAP");
139         if (check_col(pinfo->cinfo, COL_INFO))
140                 col_clear(pinfo->cinfo, COL_INFO);
141         
142         vers_flags = tvb_get_guint8(tvb, offset);
143         is_ipv6 = vers_flags&MCAST_SAP_BIT_A;
144         is_del = vers_flags&MCAST_SAP_BIT_T;
145         is_enc = vers_flags&MCAST_SAP_BIT_E;
146         is_comp = vers_flags&MCAST_SAP_BIT_C;
147
148         sap_version = (vers_flags&MCAST_SAP_VERSION_MASK)>>MCAST_SAP_VERSION_SHIFT;
149         addr_len = (is_ipv6) ? sizeof(struct e_in6_addr) : 4;
150
151         if (check_col(pinfo->cinfo, COL_INFO)) {
152                 col_add_fstr(pinfo->cinfo, 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, tvb, offset, -1, FALSE);
158           sap_tree = proto_item_add_subtree(si, ett_sap);
159
160           sif = proto_tree_add_uint(sap_tree, hf_sap_flags, tvb, offset, 1, vers_flags);
161           sap_flags_tree = proto_item_add_subtree(sif, ett_sap_flags);
162           proto_tree_add_uint(sap_flags_tree, hf_sap_flags_v, tvb, offset, 1, vers_flags);
163           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_a, tvb, offset, 1, vers_flags);
164           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_r, tvb, offset, 1, vers_flags);
165           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_t, tvb, offset, 1, vers_flags);
166           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_e, tvb, offset, 1, vers_flags);
167           proto_tree_add_boolean(sap_flags_tree, hf_sap_flags_c, tvb, offset, 1, vers_flags);
168           offset++;
169
170           auth_len = tvb_get_guint8(tvb, offset);
171           proto_tree_add_text(sap_tree, tvb, offset, 1, "Authentication Length: %u", auth_len);
172           offset++;
173
174           tmp1 = tvb_get_ntohs(tvb, offset);
175           proto_tree_add_text(sap_tree, tvb, offset, 2, "Message Identifier Hash: 0x%x", tmp1);
176           offset +=2;
177
178           addr = tvb_get_ptr(tvb, offset, addr_len);
179           proto_tree_add_text(sap_tree, tvb, offset, addr_len, "Originating Source: %s",
180               (is_ipv6) ? ip6_to_str((struct e_in6_addr*)addr) : ip_to_str(addr));
181           offset += addr_len;
182
183           /* Authentication data lives in its own subtree */
184           if (auth_len > 0) {
185                   guint32 auth_data_len;
186                   proto_item *sdi, *sai;
187                   proto_tree *sa_tree, *saf_tree;
188                   int has_pad;
189                   guint8 pad_len = 0;
190
191                   auth_data_len = auth_len * sizeof(guint32);
192
193                   sdi = proto_tree_add_item(sap_tree, hf_auth_data, tvb, offset, auth_data_len, FALSE);
194                   sa_tree = proto_item_add_subtree(sdi, ett_sap_auth);
195
196                   auth_flags = tvb_get_guint8(tvb, offset);
197                   sai = proto_tree_add_uint(sa_tree, hf_auth_flags, tvb, offset, 1, auth_flags);
198                   saf_tree = proto_item_add_subtree(sai, ett_sap_authf);
199                   proto_tree_add_uint(saf_tree, hf_auth_flags_v, tvb, offset, 1, auth_flags);
200                   proto_tree_add_boolean(saf_tree, hf_auth_flags_p, tvb, offset, 1, auth_flags);
201                   proto_tree_add_uint(saf_tree, hf_auth_flags_t, tvb, offset, 1, auth_flags);
202
203                   has_pad = auth_flags&MCAST_SAP_AUTH_BIT_P;
204                   if (has_pad)
205                          pad_len = tvb_get_guint8(tvb, offset+auth_data_len-1);
206
207                   proto_tree_add_text(sa_tree, tvb, offset+1, auth_data_len-pad_len-1,
208                                       "Authentication subheader: (%u byte%s)",
209                                       auth_data_len-1, plurality(auth_data_len-1, "", "s"));
210                   if (has_pad) {
211                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-pad_len, pad_len,
212                                               "Authentication data padding: (%u byte%s)",
213                                               pad_len, plurality(pad_len, "", "s"));
214                           proto_tree_add_text(sa_tree, tvb, offset+auth_data_len-1, 1,
215                                               "Authentication data pad count: %u byte%s",
216                                               pad_len, plurality(pad_len, "", "s"));
217                   }
218
219                   offset += auth_data_len;
220           }
221           if (is_enc || is_comp) {
222                   char *mangle;
223                   if (is_enc && is_comp) mangle = "compressed and encrypted";
224                   else if (is_enc) mangle = "encrypted";
225                   else mangle = "compressed";
226                   proto_tree_add_text(sap_tree, tvb, offset, -1,
227                                       "The rest of the packet is %s", mangle);
228                   return;
229           }
230
231           /* Do we have the optional payload type aka. MIME content specifier */
232           if (tvb_strneql(tvb, offset, "v=", strlen("v="))) {
233                   gint remaining_len;
234                   guint32 pt_len;
235                   int pt_string_len;
236
237                   remaining_len = tvb_length_remaining(tvb, offset);
238                   if (remaining_len == 0) {
239                       /*
240                        * "tvb_strneql()" failed because there was no
241                        * data left in the packet.
242                        *
243                        * Set the remaining length to 1, so that
244                        * we throw the appropriate exception in
245                        * "tvb_get_ptr()", rather than displaying
246                        * the payload type.
247                        */
248                       remaining_len = 1;
249                   }
250                   pt_string_len = tvb_strnlen(tvb, offset, remaining_len);
251                   if (pt_string_len == -1) {
252                       /*
253                        * We didn't find a terminating '\0'; run to the
254                        * end of the buffer.
255                        */
256                       pt_string_len = remaining_len;
257                       pt_len = pt_string_len;
258                   } else {
259                       /*
260                        * Include the '\0' in the total item length.
261                        */
262                       pt_len = pt_string_len + 1;
263                   }
264                   proto_tree_add_text(sap_tree, tvb, offset, pt_len,
265                       "Payload type: %.*s", pt_string_len,
266                       tvb_get_ptr(tvb, offset, pt_string_len));
267                   offset += pt_len;
268           }
269         }
270
271         /* Done with SAP */
272         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
273         call_dissector(sdp_handle, next_tvb, pinfo, tree);
274
275         return;
276 }
277
278 void proto_register_sap(void)
279 {
280
281   static hf_register_info hf[] = {
282     { &hf_sap_flags,
283       { "Flags",         "sap.flags",
284         FT_UINT8, BASE_HEX, NULL, 0x0,
285         "Bits in the beginning of the SAP header", HFILL }},
286
287     { &hf_sap_flags_v,
288       { "Version Number",         "sap.flags.v",
289         FT_UINT8, BASE_DEC, VALS(mcast_sap_ver), MCAST_SAP_VERSION_MASK,
290         "3 bit version field in the SAP header", HFILL }},
291
292     { &hf_sap_flags_a,
293       { "Address Type",           "sap.flags.a",
294         FT_BOOLEAN, 8, TFS(&mcast_sap_address_type), MCAST_SAP_BIT_A,
295         "Originating source address type", HFILL }},
296
297     { &hf_sap_flags_r,
298       { "Reserved",               "sap.flags.r",
299         FT_BOOLEAN, 8, TFS(&flags_set_truth), MCAST_SAP_BIT_R,
300         "Reserved", HFILL }},
301
302     { &hf_sap_flags_t,
303       { "Message Type",           "sap.flags.t",
304         FT_BOOLEAN, 8, TFS(&mcast_sap_message_type), MCAST_SAP_BIT_T,
305         "Announcement type", HFILL }},
306
307     { &hf_sap_flags_e,
308       { "Encryption Bit",         "sap.flags.e",
309         FT_BOOLEAN, 8, TFS(&mcast_sap_crypt_type), MCAST_SAP_BIT_E,
310         "Encryption", HFILL }},
311
312     { &hf_sap_flags_c,
313       { "Compression Bit",         "sap.flags.c",
314         FT_BOOLEAN, 8, TFS(&mcast_sap_comp_type), MCAST_SAP_BIT_C,
315         "Compression", HFILL }},
316
317     { &hf_auth_data,
318       { "Authentication data",     "sap.auth",
319         FT_NONE, BASE_NONE, NULL, 0x0,
320         "Auth data", HFILL }},
321
322     { &hf_auth_flags,
323       { "Authentication data flags", "sap.auth.flags",
324         FT_UINT8, BASE_HEX, NULL, 0x0,
325         "Auth flags", HFILL }},
326
327     { &hf_auth_flags_v,
328       { "Version Number",         "sap.auth.flags.v",
329         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_ver), MCAST_SAP_VERSION_MASK,
330         "Version", HFILL }},
331
332     { &hf_auth_flags_p,
333       { "Padding Bit",            "sap.auth.flags.p",
334         FT_BOOLEAN, 8, TFS(&mcast_sap_auth_pad), MCAST_SAP_AUTH_BIT_P,
335         "Compression", HFILL }},
336
337     { &hf_auth_flags_t,
338       { "Authentication Type",         "sap.auth.flags.t",
339         FT_UINT8, BASE_DEC, VALS(&mcast_sap_auth_type), MCAST_SAP_AUTH_TYPE_MASK,
340         "Auth type", HFILL }}
341   };
342   static gint *ett[] = {
343     &ett_sap,
344     &ett_sap_flags,
345     &ett_sap_auth,
346     &ett_sap_authf,
347   };
348
349   proto_sap = proto_register_protocol("Session Announcement Protocol", "SAP",
350                                       "sap");
351   proto_register_field_array(proto_sap, hf, array_length(hf));
352   proto_register_subtree_array(ett, array_length(ett));
353 }
354
355 void
356 proto_reg_handoff_sap(void)
357 {
358   dissector_handle_t sap_handle;
359
360   sap_handle = create_dissector_handle(dissect_sap, proto_sap);
361   dissector_add("udp.port", UDP_PORT_SAP, sap_handle);
362
363   /*
364    * Get a handle for the SDP dissector.
365    */
366   sdp_handle = find_dissector("sdp");
367 }