Don't include the header file to get the SNMP version unless we're
[obnox/wireshark/wip.git] / packet-asap.c
1 /* packet-asap.c
2  * Routines for Aggregate Server Access Protocol
3  * It is hopefully (needs testing) compilant to
4  * http://www.ietf.org/internet-drafts/draft-ietf-rserpool-common-param-01.txt
5  * http://www.ietf.org/internet-drafts/draft-ietf-rserpool-asap-04.txt
6  *
7  * Copyright 2002, Michael Tuexen <Michael.Tuexen@icn.siemens.de>
8  *
9  * $Id: packet-asap.c,v 1.5 2002/08/28 21:00:07 jmayer Exp $
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@ethereal.com>
13  * Copyright 1998 Gerald Combs
14  *
15  * Copied from README.developer
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <epan/packet.h>
37
38 #define ASAP_PAYLOAD_PROTO_ID 4209948113u /* = 0xFAEEB5D1 */
39
40 /* Initialize the protocol and registered fields */
41 static int proto_asap = -1;
42 static int hf_cause_code = -1;
43 static int hf_cause_length = -1;
44 static int hf_cause_info = -1;
45 static int hf_cause_padding = -1;
46 static int hf_message_type = -1;
47 static int hf_message_flags = -1;
48 static int hf_message_length = -1;
49 static int hf_parameter_type = -1;
50 static int hf_parameter_length = -1;
51 static int hf_parameter_value = -1;
52 static int hf_parameter_padding = -1;
53 static int hf_parameter_ipv4_address = -1;
54 static int hf_parameter_ipv6_address = -1;
55 static int hf_sctp_port = -1;
56 static int hf_sctp_reserved = -1;
57 static int hf_tcp_port = -1;
58 static int hf_tcp_reserved = -1;
59 static int hf_udp_port = -1;
60 static int hf_udp_reserved = -1;
61 static int hf_policy_type = -1;
62 static int hf_policy_value = -1;
63 static int hf_pool_handle = -1;
64 static int hf_pe_pe_identifier = -1;
65 static int hf_home_enrp_id = -1;
66 static int hf_reg_life = -1;
67 static int hf_server_identifier = -1;
68 static int hf_m_bit = -1;
69 static int hf_reserved = -1;
70 static int hf_cookie = -1;
71 static int hf_pe_identifier = -1;
72
73 /* Initialize the subtree pointers */
74 static gint ett_asap = -1;
75 static gint ett_asap_parameter = -1;
76 static gint ett_asap_cause = -1;
77
78 static void
79 dissect_parameters(tvbuff_t *, proto_tree *);
80
81 /* Helper functions */
82
83 static guint
84 nr_of_padding_bytes (guint length)
85 {
86   guint remainder;
87
88   remainder = length % 4;
89
90   if (remainder == 0)
91     return 0;
92   else
93     return 4 - remainder;
94 }
95
96 /* Dissectors for error causes */
97 #define CAUSE_CODE_LENGTH   2
98 #define CAUSE_LENGTH_LENGTH 2
99 #define CAUSE_HEADER_LENGTH (CAUSE_CODE_LENGTH + CAUSE_LENGTH_LENGTH)
100
101 #define CAUSE_HEADER_OFFSET 0
102 #define CAUSE_CODE_OFFSET   CAUSE_HEADER_OFFSET
103 #define CAUSE_LENGTH_OFFSET (CAUSE_CODE_OFFSET + CAUSE_CODE_LENGTH)
104 #define CAUSE_INFO_OFFSET   (CAUSE_LENGTH_OFFSET + CAUSE_LENGTH_LENGTH)
105
106 static void
107 dissect_unknown_cause(tvbuff_t *cause_tvb, proto_tree *cause_tree, proto_item *cause_item)
108 {
109   guint16 code, length, cause_info_length;
110
111   code              = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
112   length            = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
113   cause_info_length = length - CAUSE_HEADER_LENGTH;
114   if (cause_info_length > 0)
115     proto_tree_add_bytes(cause_tree, hf_cause_info, cause_tvb, CAUSE_INFO_OFFSET, cause_info_length,
116                          tvb_get_ptr(cause_tvb, CAUSE_INFO_OFFSET, cause_info_length));
117   proto_item_set_text(cause_item, "Error cause with code %u and %u byte%s information", code, cause_info_length, plurality(cause_info_length, "", "s"));
118 }
119
120 #define UNRECOGNIZED_PARAMETER_CAUSE_CODE      1
121 #define UNRECONGNIZED_MESSAGE_CAUSE_CODE       2
122 #define AUTHORIZATION_FAILURE_CAUSE_CODE       3 /* This is an error in the ID */
123 #define INVALID_VALUES                         4
124 #define NON_UNIQUE_PE_IDENTIFIER               5
125 #define POOLING_POLICY_INCONSISTENT_CAUSE_CODE 6
126
127 static const value_string cause_code_values[] = {
128   { UNRECOGNIZED_PARAMETER_CAUSE_CODE,      "Unrecognized parameter" },
129   { UNRECONGNIZED_MESSAGE_CAUSE_CODE,       "Unrecognized message" },
130   { AUTHORIZATION_FAILURE_CAUSE_CODE,       "Authorization failure" },
131   { INVALID_VALUES,                         "Invalid values" },
132   { NON_UNIQUE_PE_IDENTIFIER,               "Non-unique PE identifier" },
133   { POOLING_POLICY_INCONSISTENT_CAUSE_CODE, "Pooling policy inconsistent" },
134   { 0,                            NULL } };
135
136 static void
137 dissect_error_cause(tvbuff_t *cause_tvb, proto_tree *parameter_tree)
138 {
139   guint16 code, length, padding_length, total_length;
140   proto_item *cause_item;
141   proto_tree *cause_tree;
142
143   code           = tvb_get_ntohs(cause_tvb, CAUSE_CODE_OFFSET);
144   length         = tvb_get_ntohs(cause_tvb, CAUSE_LENGTH_OFFSET);
145   padding_length = nr_of_padding_bytes(length);
146   total_length   = length + padding_length;
147
148   cause_item = proto_tree_add_text(parameter_tree, cause_tvb, CAUSE_HEADER_OFFSET, total_length, "BAD ERROR CAUSE");
149   cause_tree = proto_item_add_subtree(cause_item, ett_asap_cause);
150
151   proto_tree_add_uint(cause_tree, hf_cause_code, cause_tvb, CAUSE_CODE_OFFSET, CAUSE_CODE_LENGTH, code);
152   proto_tree_add_uint(cause_tree, hf_cause_length, cause_tvb, CAUSE_LENGTH_OFFSET, CAUSE_LENGTH_LENGTH, length);
153
154   switch(code) {
155   default:
156     dissect_unknown_cause(cause_tvb, cause_tree, cause_item);
157     break;
158   }
159   if (padding_length > 0)
160     proto_tree_add_bytes(cause_tree, hf_cause_padding, cause_tvb, CAUSE_HEADER_OFFSET + length, padding_length,
161                          tvb_get_ptr(cause_tvb, CAUSE_HEADER_OFFSET + length, padding_length));
162 }
163
164 static void
165 dissect_error_causes(tvbuff_t *error_causes_tvb, proto_tree *parameter_tree)
166 {
167   guint16 length, padding_length, total_length;
168   gint offset;
169   tvbuff_t *error_cause_tvb;
170
171   offset = 0;
172   while(tvb_reported_length_remaining(error_causes_tvb, offset)) {
173     length          = tvb_get_ntohs(error_causes_tvb, offset + CAUSE_LENGTH_OFFSET);
174     padding_length  = nr_of_padding_bytes(length);
175     total_length    = length + padding_length;
176     error_cause_tvb = tvb_new_subset(error_causes_tvb, offset , total_length, total_length);
177     dissect_error_cause(error_cause_tvb, parameter_tree);
178     offset += total_length;
179   }
180 }
181
182 /* Dissectors for parameters */
183
184 #define PARAMETER_TYPE_LENGTH   2
185 #define PARAMETER_LENGTH_LENGTH 2
186 #define PARAMETER_HEADER_LENGTH (PARAMETER_TYPE_LENGTH + PARAMETER_LENGTH_LENGTH)
187
188 #define PARAMETER_HEADER_OFFSET 0
189 #define PARAMETER_TYPE_OFFSET   PARAMETER_HEADER_OFFSET
190 #define PARAMETER_LENGTH_OFFSET (PARAMETER_TYPE_OFFSET + PARAMETER_TYPE_LENGTH)
191 #define PARAMETER_VALUE_OFFSET  (PARAMETER_LENGTH_OFFSET + PARAMETER_LENGTH_LENGTH)
192
193 #define IPV4_ADDRESS_LENGTH 4
194 #define IPV4_ADDRESS_OFFSET PARAMETER_VALUE_OFFSET
195
196 static void
197 dissect_ipv4_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
198 {
199   guint32 ipv4_address;
200
201   tvb_memcpy(parameter_tvb, (guint8 *)&ipv4_address, IPV4_ADDRESS_OFFSET, IPV4_ADDRESS_LENGTH);
202   proto_tree_add_ipv4(parameter_tree, hf_parameter_ipv4_address, parameter_tvb, IPV4_ADDRESS_OFFSET, IPV4_ADDRESS_LENGTH, ipv4_address);
203   proto_item_set_text(parameter_item, "IPV4 address parameter: %s", ip_to_str((const guint8 *)&ipv4_address));
204 }
205
206 #define IPV6_ADDRESS_LENGTH 16
207 #define IPV6_ADDRESS_OFFSET PARAMETER_VALUE_OFFSET
208
209 static void
210 dissect_ipv6_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
211 {
212   guint8 *ip6_address_ptr;
213
214   ip6_address_ptr = (guint8 *)tvb_get_ptr(parameter_tvb, IPV6_ADDRESS_OFFSET, IPV6_ADDRESS_LENGTH);
215   proto_tree_add_ipv6(parameter_tree, hf_parameter_ipv6_address, parameter_tvb, IPV6_ADDRESS_OFFSET, IPV6_ADDRESS_LENGTH,
216                       (const guint8 *)ip6_address_ptr);
217
218   proto_item_set_text(parameter_item, "IPV6 address parameter: %s", ip6_to_str((struct e_in6_addr *)ip6_address_ptr));
219 }
220
221 #define SCTP_PORT_LENGTH     2
222 #define SCTP_RESERVED_LENGTH 2
223 #define SCTP_PORT_OFFSET     PARAMETER_VALUE_OFFSET
224 #define SCTP_RESERVED_OFFSET (SCTP_PORT_OFFSET + SCTP_PORT_LENGTH)
225 #define SCTP_ADDRESS_OFFSET  (SCTP_RESERVED_OFFSET + SCTP_RESERVED_LENGTH)
226
227 static void
228 dissect_sctp_transport_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
229 {
230   tvbuff_t *parameters_tvb;
231   guint16 port, reserved;
232
233   port     = tvb_get_ntohs(parameter_tvb, SCTP_PORT_OFFSET);
234   reserved = tvb_get_ntohs(parameter_tvb, SCTP_RESERVED_OFFSET);
235
236   proto_tree_add_uint(parameter_tree, hf_sctp_port, parameter_tvb, SCTP_PORT_OFFSET, SCTP_PORT_LENGTH, port);
237   proto_tree_add_uint(parameter_tree, hf_sctp_reserved, parameter_tvb, SCTP_RESERVED_OFFSET, SCTP_RESERVED_LENGTH, reserved);
238
239   proto_item_set_text(parameter_item, "SCTP transport parameter");
240
241   parameters_tvb = tvb_new_subset(parameter_tvb, SCTP_ADDRESS_OFFSET, -1, -1);
242   dissect_parameters(parameters_tvb, parameter_tree);
243 }
244
245 #define TCP_PORT_LENGTH     2
246 #define TCP_RESERVED_LENGTH 2
247 #define TCP_PORT_OFFSET     PARAMETER_VALUE_OFFSET
248 #define TCP_RESERVED_OFFSET (TCP_PORT_OFFSET + TCP_PORT_LENGTH)
249 #define TCP_ADDRESS_OFFSET  (TCP_RESERVED_OFFSET + TCP_RESERVED_LENGTH)
250
251 static void
252 dissect_tcp_transport_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
253 {
254   tvbuff_t *parameters_tvb;
255   guint16 port, reserved;
256
257   port     = tvb_get_ntohs(parameter_tvb, TCP_PORT_OFFSET);
258   reserved = tvb_get_ntohs(parameter_tvb, TCP_RESERVED_OFFSET);
259
260   proto_tree_add_uint(parameter_tree, hf_tcp_port, parameter_tvb, TCP_PORT_OFFSET, TCP_PORT_LENGTH, port);
261   proto_tree_add_uint(parameter_tree, hf_tcp_reserved, parameter_tvb, TCP_RESERVED_OFFSET, TCP_RESERVED_LENGTH, reserved);
262
263   proto_item_set_text(parameter_item, "TCP transport parameter");
264
265   parameters_tvb = tvb_new_subset(parameter_tvb, TCP_ADDRESS_OFFSET, -1, -1);
266   dissect_parameters(parameters_tvb, parameter_tree);
267 }
268
269 #define UDP_PORT_LENGTH     2
270 #define UDP_RESERVED_LENGTH 2
271 #define UDP_PORT_OFFSET     PARAMETER_VALUE_OFFSET
272 #define UDP_RESERVED_OFFSET (UDP_PORT_OFFSET + UDP_PORT_LENGTH)
273 #define UDP_ADDRESS_OFFSET  (UDP_RESERVED_OFFSET + UDP_RESERVED_LENGTH)
274
275 static void
276 dissect_udp_transport_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
277 {
278   tvbuff_t *parameters_tvb;
279   guint16 port, reserved;
280
281   port     = tvb_get_ntohs(parameter_tvb, UDP_PORT_OFFSET);
282   reserved = tvb_get_ntohs(parameter_tvb, UDP_RESERVED_OFFSET);
283
284   proto_tree_add_uint(parameter_tree, hf_udp_port, parameter_tvb, UDP_PORT_OFFSET, UDP_PORT_LENGTH, port);
285   proto_tree_add_uint(parameter_tree, hf_udp_reserved, parameter_tvb, UDP_RESERVED_OFFSET, UDP_RESERVED_LENGTH, reserved);
286
287   proto_item_set_text(parameter_item, "UDP transport parameter");
288
289   parameters_tvb = tvb_new_subset(parameter_tvb, UDP_ADDRESS_OFFSET, -1, -1);
290   dissect_parameters(parameters_tvb, parameter_tree);
291 }
292
293 #define POLICY_TYPE_LENGTH   1
294 #define POLICY_VALUE_LENGTH  3
295
296 #define POLICY_TYPE_OFFSET   PARAMETER_VALUE_OFFSET
297 #define POLICY_VALUE_OFFSET  (POLICY_TYPE_OFFSET + POLICY_TYPE_LENGTH)
298
299 #define ROUND_ROBIN_POLICY   1
300 #define LEAST_USED_POLICY    2
301 #define LEAST_USED_WITH_DEG  3
302 #define WEIGHTED_ROUND_ROBIN 4
303
304 static const value_string policy_type_values[] = {
305   { ROUND_ROBIN_POLICY,   "Round robin" },
306   { LEAST_USED_POLICY,    "Least used" },
307   { LEAST_USED_WITH_DEG,  "Least used with degradation" },
308   { WEIGHTED_ROUND_ROBIN, "Weighted round robin" },
309   { 0,                    NULL } };
310
311 static void
312 dissect_pool_member_selection_policy_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
313 {
314   guint8  policy_type;
315   gint32 policy_value;
316
317   policy_type  = tvb_get_guint8(parameter_tvb, POLICY_TYPE_OFFSET);
318   policy_value = tvb_get_ntoh24(parameter_tvb, POLICY_VALUE_OFFSET);
319   proto_tree_add_uint(parameter_tree, hf_policy_type, parameter_tvb, POLICY_TYPE_OFFSET, POLICY_TYPE_LENGTH, policy_type);
320   proto_tree_add_int(parameter_tree, hf_policy_value, parameter_tvb, POLICY_VALUE_OFFSET, POLICY_VALUE_LENGTH, policy_value);
321
322   proto_item_set_text(parameter_item, "Pool member selection policy");
323
324 }
325
326 #define POOL_HANDLE_OFFSET PARAMETER_VALUE_OFFSET
327
328 static void
329 dissect_pool_handle_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
330 {
331   guint16 length, handle_length;
332   char *handle_ptr;
333
334   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
335
336   handle_length = length - PARAMETER_HEADER_LENGTH;
337   handle_ptr    = (char *)tvb_get_ptr(parameter_tvb, POOL_HANDLE_OFFSET, handle_length);
338   proto_tree_add_bytes(parameter_tree, hf_pool_handle, parameter_tvb, POOL_HANDLE_OFFSET, handle_length, handle_ptr);
339   proto_item_set_text(parameter_item, "Pool handle");
340 }
341
342 #define PE_PE_IDENTIFIER_LENGTH         4
343 #define HOME_ENRP_INDENTIFIER_LENGTH    4
344 #define REGISTRATION_LIFE_LENGTH        4
345
346 #define PE_PE_IDENTIFIER_OFFSET         PARAMETER_VALUE_OFFSET
347 #define HOME_ENRP_INDENTIFIER_OFFSET    (PE_PE_IDENTIFIER_OFFSET + PE_PE_IDENTIFIER_LENGTH)
348 #define REGISTRATION_LIFE_OFFSET        (HOME_ENRP_INDENTIFIER_OFFSET + HOME_ENRP_INDENTIFIER_LENGTH)
349 #define USER_TRANSPORT_PARAMETER_OFFSET (REGISTRATION_LIFE_OFFSET + REGISTRATION_LIFE_LENGTH)
350
351 static void
352 dissect_pool_element_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
353 {
354   tvbuff_t *parameters_tvb;
355   guint32 pe_identifier, home_enrp_identifier;
356   gint32 reg_life;
357
358   pe_identifier        = tvb_get_ntohl(parameter_tvb, PE_PE_IDENTIFIER_OFFSET);
359   home_enrp_identifier = tvb_get_ntohl(parameter_tvb, HOME_ENRP_INDENTIFIER_OFFSET);
360   reg_life             = tvb_get_ntohl(parameter_tvb, REGISTRATION_LIFE_OFFSET);
361
362   proto_tree_add_uint(parameter_tree, hf_pe_pe_identifier, parameter_tvb, PE_PE_IDENTIFIER_OFFSET, PE_PE_IDENTIFIER_LENGTH, pe_identifier);
363   proto_tree_add_uint(parameter_tree, hf_home_enrp_id, parameter_tvb, HOME_ENRP_INDENTIFIER_OFFSET, HOME_ENRP_INDENTIFIER_LENGTH, home_enrp_identifier);
364   proto_tree_add_int(parameter_tree, hf_reg_life, parameter_tvb, REGISTRATION_LIFE_OFFSET, REGISTRATION_LIFE_LENGTH, reg_life);
365
366   parameters_tvb = tvb_new_subset(parameter_tvb, USER_TRANSPORT_PARAMETER_OFFSET, -1, -1);
367   dissect_parameters(parameters_tvb, parameter_tree);
368
369   proto_item_set_text(parameter_item, "Pool element");
370 }
371
372 #define SERVER_ID_LENGTH         4
373 #define RESERVED_LENGTH          4
374
375 #define SERVER_ID_OFFSET         PARAMETER_VALUE_OFFSET
376 #define RESERVED_OFFSET          (SERVER_ID_OFFSET + SERVER_ID_LENGTH)
377 #define SERVER_TRANSPORT_OFFSET  (RESERVED_OFFSET + RESERVED_LENGTH)
378
379 #define M_BIT_MASK               0x80000000
380 #define RESERVED_MASK            0x7fffffff
381
382 static void
383 dissect_server_information_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
384 {
385   tvbuff_t *parameters_tvb;
386   guint32 server_identifier, reserved;
387
388   server_identifier  = tvb_get_ntohl(parameter_tvb, SERVER_ID_OFFSET);
389   reserved           = tvb_get_ntohl(parameter_tvb, RESERVED_OFFSET);
390
391   proto_tree_add_uint(parameter_tree, hf_server_identifier, parameter_tvb, SERVER_ID_OFFSET, SERVER_ID_LENGTH, server_identifier);
392   proto_tree_add_boolean(parameter_tree, hf_m_bit, parameter_tvb, RESERVED_OFFSET, RESERVED_LENGTH, reserved);
393   proto_tree_add_uint(parameter_tree, hf_reserved, parameter_tvb, RESERVED_OFFSET, RESERVED_LENGTH, reserved);
394
395   proto_item_set_text(parameter_item, "Server information");
396
397   parameters_tvb = tvb_new_subset(parameter_tvb, SERVER_TRANSPORT_OFFSET, -1, -1);
398   dissect_parameters(parameters_tvb, parameter_tree);
399 }
400
401 #define ERROR_CAUSES_OFFSET PARAMETER_VALUE_OFFSET
402
403 static void
404 dissect_operation_error_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
405 {
406   tvbuff_t *error_causes_tvb;
407
408   error_causes_tvb = tvb_new_subset(parameter_tvb, ERROR_CAUSES_OFFSET, -1,-1);
409   dissect_error_causes(error_causes_tvb, parameter_tree);
410   proto_item_set_text(parameter_item, "Operation Error");
411 }
412
413 #define COOKIE_OFFSET PARAMETER_VALUE_OFFSET
414
415 static void
416 dissect_cookie_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
417 {
418   guint16 cookie_length;
419
420   cookie_length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET) - PARAMETER_HEADER_LENGTH;
421   if (cookie_length > 0)
422     proto_tree_add_bytes(parameter_tree, hf_cookie, parameter_tvb, COOKIE_OFFSET, cookie_length,
423                          tvb_get_ptr(parameter_tvb, COOKIE_OFFSET, cookie_length));
424   proto_item_set_text(parameter_item, "Cookie (%u byte%s)", cookie_length, plurality(cookie_length, "", "s"));
425 }
426
427 #define PE_IDENTIFIER_LENGTH 4
428 #define PE_IDENTIFIER_OFFSET PARAMETER_VALUE_OFFSET
429
430 static void
431 dissect_pe_identifier_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
432 {
433   guint32 pe_identifer;
434
435   pe_identifer = tvb_get_ntohl(parameter_tvb, PE_IDENTIFIER_OFFSET);
436   proto_tree_add_uint(parameter_tree, hf_pe_identifier, parameter_tvb, PE_IDENTIFIER_OFFSET, PE_IDENTIFIER_LENGTH, pe_identifer);
437   proto_item_set_text(parameter_item, "PE identifier: 0x%x", pe_identifer);
438 }
439
440 static void
441 dissect_unknown_parameter(tvbuff_t *parameter_tvb, proto_tree *parameter_tree, proto_item *parameter_item)
442 {
443   guint16 type, length, parameter_value_length;
444
445   type   = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
446   length = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
447
448   parameter_value_length = length - PARAMETER_HEADER_LENGTH;
449
450   if (parameter_value_length > 0)
451     proto_tree_add_bytes(parameter_tree, hf_parameter_value, parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length,
452                          tvb_get_ptr(parameter_tvb, PARAMETER_VALUE_OFFSET, parameter_value_length));
453
454   proto_item_set_text(parameter_item, "Parameter with type %u and %u byte%s value", type, parameter_value_length, plurality(parameter_value_length, "", "s"));
455 }
456
457 #define IPV4_ADDRESS_PARAMETER_TYPE                 0x01
458 #define IPV6_ADDRESS_PARAMETER_TYPE                 0x02
459 #define SCTP_TRANSPORT_PARAMETER_TYPE               0x03
460 #define TCP_TRANSPORT_PARAMETER_TYPE                0x04
461 #define UDP_TRANSPORT_PARAMETER_TYPE                0x05
462 #define POOL_MEMBER_SELECTION_POLICY_PARAMETER_TYPE 0x06
463 #define POOL_HANDLE_PARAMETER_TYPE                  0x07
464 #define POOL_ELEMENT_PARAMETER_TYPE                 0x08
465 #define SERVER_INFORMATION_PARAMETER_TYPE           0x09
466 #define OPERATION_ERROR_PARAMETER_TYPE              0x0a
467 #define COOKIE_PARAMETER_TYPE                       0x0b
468 #define PE_IDENTIFIER_PARAMETER_TYPE                0x0c
469
470 static const value_string parameter_type_values[] = {
471   { IPV4_ADDRESS_PARAMETER_TYPE,                 "IPV4 address" },
472   { IPV6_ADDRESS_PARAMETER_TYPE,                 "IPV6 address" },
473   { SCTP_TRANSPORT_PARAMETER_TYPE,               "SCTP transport address" },
474   { TCP_TRANSPORT_PARAMETER_TYPE,                "TCP transport address" },
475   { UDP_TRANSPORT_PARAMETER_TYPE,                "UDP transport address" },
476   { POOL_MEMBER_SELECTION_POLICY_PARAMETER_TYPE, "Pool member selection policy" },
477   { POOL_HANDLE_PARAMETER_TYPE,                  "Pool handle" },
478   { SERVER_INFORMATION_PARAMETER_TYPE,           "Server Information" },
479   { OPERATION_ERROR_PARAMETER_TYPE,              "Operation error" },
480   { COOKIE_PARAMETER_TYPE,                       "Cookie" },
481   { PE_IDENTIFIER_PARAMETER_TYPE,                "Pool Element identifier" },
482   { 0,                            NULL } };
483
484
485 static void
486 dissect_asap_parameter(tvbuff_t *parameter_tvb, proto_tree *asap_tree)
487 {
488   guint16 type, length, padding_length, total_length;
489   proto_item *parameter_item;
490   proto_tree *parameter_tree;
491
492   /* extract tag and length from the parameter */
493   type           = tvb_get_ntohs(parameter_tvb, PARAMETER_TYPE_OFFSET);
494   length         = tvb_get_ntohs(parameter_tvb, PARAMETER_LENGTH_OFFSET);
495
496   /* calculate padding and total length */
497   padding_length = tvb_length(parameter_tvb) - length;
498   total_length   = length + padding_length;
499
500   /* create proto_tree stuff */
501   parameter_item   = proto_tree_add_text(asap_tree, parameter_tvb, PARAMETER_HEADER_OFFSET, total_length, "Incomplete parameter");
502   parameter_tree   = proto_item_add_subtree(parameter_item, ett_asap_parameter);
503
504   /* add tag and length to the asap tree */
505   proto_tree_add_uint(parameter_tree, hf_parameter_type, parameter_tvb, PARAMETER_TYPE_OFFSET, PARAMETER_TYPE_LENGTH, type);
506   proto_tree_add_uint(parameter_tree, hf_parameter_length, parameter_tvb, PARAMETER_LENGTH_OFFSET, PARAMETER_LENGTH_LENGTH, length);
507
508   switch(type) {
509   case IPV4_ADDRESS_PARAMETER_TYPE:
510     dissect_ipv4_parameter(parameter_tvb, parameter_tree, parameter_item);
511     break;
512   case IPV6_ADDRESS_PARAMETER_TYPE:
513     dissect_ipv6_parameter(parameter_tvb, parameter_tree, parameter_item);
514     break;
515   case SCTP_TRANSPORT_PARAMETER_TYPE:
516     dissect_sctp_transport_parameter(parameter_tvb, parameter_tree, parameter_item);
517     break;
518   case TCP_TRANSPORT_PARAMETER_TYPE:
519     dissect_tcp_transport_parameter(parameter_tvb, parameter_tree, parameter_item);
520     break;
521   case UDP_TRANSPORT_PARAMETER_TYPE:
522     dissect_udp_transport_parameter(parameter_tvb, parameter_tree, parameter_item);
523     break;
524   case POOL_MEMBER_SELECTION_POLICY_PARAMETER_TYPE:
525     dissect_pool_member_selection_policy_parameter(parameter_tvb, parameter_tree, parameter_item);
526     break;
527   case POOL_HANDLE_PARAMETER_TYPE:
528     dissect_pool_handle_parameter(parameter_tvb, parameter_tree, parameter_item);
529     break;
530   case POOL_ELEMENT_PARAMETER_TYPE:
531     dissect_pool_element_parameter(parameter_tvb, parameter_tree, parameter_item);
532     break;
533   case SERVER_INFORMATION_PARAMETER_TYPE:
534     dissect_server_information_parameter(parameter_tvb, parameter_tree, parameter_item);
535     break;
536   case OPERATION_ERROR_PARAMETER_TYPE:
537     dissect_operation_error_parameter(parameter_tvb, parameter_tree, parameter_item);
538     break;
539   case COOKIE_PARAMETER_TYPE:
540     dissect_cookie_parameter(parameter_tvb, parameter_tree, parameter_item);
541     break;
542   case PE_IDENTIFIER_PARAMETER_TYPE:
543     dissect_pe_identifier_parameter(parameter_tvb, parameter_tree, parameter_item);
544     break;
545   default:
546     dissect_unknown_parameter(parameter_tvb, parameter_tree, parameter_item);
547     break;
548   };
549
550   if (padding_length > 0)
551     proto_tree_add_bytes(parameter_tree, hf_parameter_padding, parameter_tvb, PARAMETER_HEADER_OFFSET + length, padding_length,
552                          tvb_get_ptr(parameter_tvb, PARAMETER_HEADER_OFFSET + length, padding_length));
553 }
554
555 static void
556 dissect_parameters(tvbuff_t *parameters_tvb, proto_tree *tree)
557 {
558   gint offset, length, padding_length, total_length, remaining_length;
559   tvbuff_t *parameter_tvb;
560
561   offset = 0;
562   while((remaining_length = tvb_length_remaining(parameters_tvb, offset))) {
563     length         = tvb_get_ntohs(parameters_tvb, offset + PARAMETER_LENGTH_OFFSET);
564     padding_length = nr_of_padding_bytes(length);
565     if (remaining_length >= length)
566       total_length = MIN(length + padding_length, remaining_length);
567     else
568       total_length = length + padding_length;
569     /* create a tvb for the parameter including the padding bytes */
570     parameter_tvb  = tvb_new_subset(parameters_tvb, offset, total_length, total_length);
571     dissect_asap_parameter(parameter_tvb, tree);
572     /* get rid of the handled parameter */
573     offset += total_length;
574   }
575 }
576
577 #define MESSAGE_TYPE_LENGTH   1
578 #define MESSAGE_FLAGS_LENGTH  1
579 #define MESSAGE_LENGTH_LENGTH 2
580
581 #define MESSAGE_TYPE_OFFSET   0
582 #define MESSAGE_FLAGS_OFFSET  (MESSAGE_TYPE_OFFSET + MESSAGE_TYPE_LENGTH)
583 #define MESSAGE_LENGTH_OFFSET (MESSAGE_FLAGS_OFFSET + MESSAGE_FLAGS_LENGTH)
584 #define MESSAGE_VALUE_OFFSET  (MESSAGE_LENGTH_OFFSET + MESSAGE_LENGTH_LENGTH)
585
586 #define REGISTRATION_MESSAGE_TYPE             0x01
587 #define DEREGISTRATION_MESSAGE_TYPE           0x02
588 #define REGISTRATION_RESPONSE_MESSAGE_TYPE    0x03
589 #define DEREGISTRATION_RESPONSE_MESSAGE_TYPE  0x04
590 #define NAME_RESOLUTION_MESSAGE_TYPE          0x05
591 #define NAME_RESOLUTION_RESPONSE_MESSAGE_TYPE 0x06
592 #define NAME_UNKNOWN_MESSAGE_TYPE             0x07
593 #define ENDPOINT_KEEP_ALIVE_MESSAGE_TYPE      0x08
594 #define ENDPOINT_KEEP_ALIVE_ACK_MESSAGE_TYPE  0x09
595 #define ENDPOINT_UNREACHABLE_MESSAGE_TYPE     0x0a
596 #define SERVER_HUNT_MESSAGE_TYPE              0x0b
597 #define SERVER_HUNT_RESPONSE_MESSAGE_TYPE     0x0c
598 #define COOKIE_MESSAGE_TYPE                   0x0d
599 #define COOKIE_ECHO_MESSAGE_TYPE              0x0e
600
601 static const value_string message_type_values[] = {
602   { REGISTRATION_MESSAGE_TYPE,             "Registration" },
603   { DEREGISTRATION_MESSAGE_TYPE,           "Deregistration" },
604   { REGISTRATION_RESPONSE_MESSAGE_TYPE,    "Registration response" },
605   { DEREGISTRATION_RESPONSE_MESSAGE_TYPE,  "Deregistration response" },
606   { NAME_RESOLUTION_MESSAGE_TYPE,          "Name resolution" },
607   { NAME_RESOLUTION_RESPONSE_MESSAGE_TYPE, "Name resolution response" },
608   { NAME_UNKNOWN_MESSAGE_TYPE,             "Name unknown" },
609   { ENDPOINT_KEEP_ALIVE_MESSAGE_TYPE,      "Endpoint keep alive" },
610   { ENDPOINT_KEEP_ALIVE_ACK_MESSAGE_TYPE,  "Endpoint keep alive acknowledgement" },
611   { ENDPOINT_UNREACHABLE_MESSAGE_TYPE,     "Endpoint unreachable" },
612   { SERVER_HUNT_MESSAGE_TYPE,              "Server hunt" },
613   { SERVER_HUNT_RESPONSE_MESSAGE_TYPE,     "Server hunt response" },
614   { COOKIE_MESSAGE_TYPE,                   "Cookie" },
615   { COOKIE_ECHO_MESSAGE_TYPE,              "Cookie echo" },
616   { 0,                                     NULL } };
617
618 static void
619 dissect_asap_message(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *asap_tree)
620 {
621   tvbuff_t *parameters_tvb;
622   guint8  type, flags;
623   guint16 length;
624
625   type   = tvb_get_guint8(message_tvb, MESSAGE_TYPE_OFFSET);
626   flags  = tvb_get_guint8(message_tvb, MESSAGE_FLAGS_OFFSET);
627   length = tvb_get_ntohs (message_tvb, MESSAGE_LENGTH_OFFSET);
628
629   if (check_col(pinfo->cinfo, COL_INFO)) {
630     col_append_str(pinfo->cinfo, COL_INFO, val_to_str(type, message_type_values, "Unknown ASAP type"));
631     col_append_str(pinfo->cinfo, COL_INFO, " ");
632   }
633   if (asap_tree) {
634     proto_tree_add_uint(asap_tree, hf_message_type,   message_tvb, MESSAGE_TYPE_OFFSET,   MESSAGE_TYPE_LENGTH,   type);
635     proto_tree_add_uint(asap_tree, hf_message_flags,  message_tvb, MESSAGE_FLAGS_OFFSET,  MESSAGE_FLAGS_LENGTH,  flags);
636     proto_tree_add_uint(asap_tree, hf_message_length, message_tvb, MESSAGE_LENGTH_OFFSET, MESSAGE_LENGTH_LENGTH, length);
637     parameters_tvb    = tvb_new_subset(message_tvb, MESSAGE_VALUE_OFFSET, -1, -1);
638     dissect_parameters(parameters_tvb, asap_tree);
639     }
640 }
641
642 static void
643 dissect_asap(tvbuff_t *message_tvb, packet_info *pinfo, proto_tree *tree)
644 {
645   proto_item *asap_item;
646   proto_tree *asap_tree;
647
648   /* make entry in the Protocol column on summary display */
649   if (check_col(pinfo->cinfo, COL_PROTOCOL))
650     col_set_str(pinfo->cinfo, COL_PROTOCOL, "ASAP");
651
652   /* In the interest of speed, if "tree" is NULL, don't do any work not
653      necessary to generate protocol tree items. */
654   if (tree) {
655     /* create the asap protocol tree */
656     asap_item = proto_tree_add_item(tree, proto_asap, message_tvb, 0, -1, FALSE);
657     asap_tree = proto_item_add_subtree(asap_item, ett_asap);
658   } else {
659     asap_tree = NULL;
660   };
661   /* dissect the message */
662   dissect_asap_message(message_tvb, pinfo, asap_tree);
663 }
664
665 /* Register the protocol with Ethereal */
666 void
667 proto_register_asap(void)
668 {
669
670   /* Setup list of header fields */
671   static hf_register_info hf[] = {
672     { &hf_message_type,
673       { "Type", "asap.message_type",
674         FT_UINT8, BASE_DEC, VALS(message_type_values), 0x0,
675         "", HFILL }
676     },
677     { &hf_message_flags,
678       { "Flags", "asap.message_flags",
679         FT_UINT8, BASE_HEX, NULL, 0x0,
680         "", HFILL }
681     },
682     { &hf_message_length,
683       { "Length", "asap.message_length",
684         FT_UINT16, BASE_DEC, NULL, 0x0,
685         "", HFILL }
686     },
687     { &hf_cause_code,
688       { "Cause code", "asap.cause.code",
689         FT_UINT16, BASE_HEX, VALS(cause_code_values), 0x0,
690         "", HFILL }
691     },
692     { &hf_cause_length,
693       { "Cause length", "asap.cause.length",
694         FT_UINT16, BASE_DEC, NULL, 0x0,
695         "", HFILL }
696     },
697     { &hf_cause_info,
698       { "Cause info", "asap.cause.info",
699         FT_BYTES, BASE_NONE, NULL, 0x0,
700         "", HFILL }
701     },
702     { &hf_cause_padding,
703       { "Padding", "asap.cause.padding",
704         FT_BYTES, BASE_NONE, NULL, 0x0,
705         "", HFILL }
706     },
707     { &hf_parameter_type,
708       { "Parameter Type", "asap.parameter.type",
709         FT_UINT16, BASE_HEX, VALS(parameter_type_values), 0x0,
710         "", HFILL }
711     },
712     { &hf_parameter_length,
713       { "Parameter length", "asap.parameter.length",
714         FT_UINT16, BASE_DEC, NULL, 0x0,
715         "", HFILL }
716     },
717     { &hf_parameter_value,
718       { "Parameter value", "asap.parameter.value",
719         FT_BYTES, BASE_NONE, NULL, 0x0,
720         "", HFILL }
721     },
722     { &hf_parameter_padding,
723       { "Padding", "asap.parameter.padding",
724         FT_BYTES, BASE_NONE, NULL, 0x0,
725         "", HFILL }
726     },
727     {&hf_parameter_ipv4_address,
728      { "IP Version 4 address", "asap.ipv4_address.ipv4_address",
729        FT_IPv4, BASE_NONE, NULL, 0x0,
730        "", HFILL }
731     },
732     {&hf_parameter_ipv6_address,
733      { "IP Version 6 address", "asap.ipv6_address.ipv6_address",
734        FT_IPv6, BASE_NONE, NULL, 0x0,
735        "", HFILL }
736     },
737     { &hf_sctp_port,
738       { "Port", "asap.sctp_transport.port",
739         FT_UINT16, BASE_DEC, NULL, 0x0,
740         "", HFILL }
741     },
742     { &hf_sctp_reserved,
743       { "Reserved", "asap.sctp_transport.reserved",
744         FT_UINT16, BASE_DEC, NULL, 0x0,
745         "", HFILL }
746     },
747     { &hf_tcp_port,
748       { "Port", "asap.tcp_transport.port",
749         FT_UINT16, BASE_DEC, NULL, 0x0,
750         "", HFILL }
751     },
752     { &hf_tcp_reserved,
753       { "Reserved", "asap.tcp_transport.reserved",
754         FT_UINT16, BASE_DEC, NULL, 0x0,
755         "", HFILL }
756     },
757     { &hf_udp_port,
758       { "Port", "asap.udp_transport.port",
759         FT_UINT16, BASE_DEC, NULL, 0x0,
760         "", HFILL }
761     },
762     { &hf_udp_reserved,
763       { "Reserved", "asap.udp_transport.reserved",
764         FT_UINT16, BASE_DEC, NULL, 0x0,
765         "", HFILL }
766     },
767     { &hf_policy_type,
768       { "Policy type", "asap.pool_member_slection_policy.type",
769         FT_UINT8, BASE_DEC, VALS(policy_type_values), 0x0,
770         "", HFILL }
771     },
772     { &hf_policy_value,
773       { "Policy value", "asap.pool_member_slection_policy.value",
774         FT_INT24, BASE_DEC, NULL, 0x0,
775         "", HFILL }
776     },
777     { &hf_pool_handle,
778       { "Pool handle", "asap.pool_handle.pool_handle",
779         FT_BYTES, BASE_HEX, NULL, 0x0,
780         "", HFILL }
781     },
782     { &hf_pe_pe_identifier,
783       { "PE identifier", "asap.pool_element.pe_identifier",
784         FT_UINT32, BASE_HEX, NULL, 0x0,
785         "", HFILL }
786     },
787     { &hf_home_enrp_id,
788       { "Home ENRP server identifier", "asap.pool_element.home_enrp_server_identifier",
789         FT_UINT32, BASE_HEX, NULL, 0x0,
790         "", HFILL }
791     },
792     { &hf_reg_life,
793       { "Registration life", "asap.pool_element.registration_life",
794         FT_INT32, BASE_DEC, NULL, 0x0,
795         "", HFILL }
796     },
797     { &hf_server_identifier,
798       { "Server identifier", "asap.server_information.server_identifier",
799         FT_UINT32, BASE_HEX, NULL, 0x0,
800         "", HFILL }
801     },
802     { &hf_m_bit,
803       { "M-Bit", "asap.server_information.m_bit",
804         FT_BOOLEAN, 32, NULL, M_BIT_MASK,
805         "", HFILL }
806     },
807     { &hf_reserved,
808       { "Reserved", "asap.server_information.reserved",
809         FT_UINT32, BASE_HEX, NULL, RESERVED_MASK,
810         "", HFILL }
811     },
812     { &hf_cookie,
813       { "Cookie", "asap.cookie.cookie",
814         FT_BYTES, BASE_HEX, NULL, 0x0,
815         "", HFILL }
816     },
817     { &hf_pe_identifier,
818       { "PE identifier", "asap.pe_identifier.pe_identifier",
819         FT_UINT32, BASE_HEX, NULL, 0x0,
820         "", HFILL }
821     },
822   };
823
824   /* Setup protocol subtree array */
825   static gint *ett[] = {
826     &ett_asap,
827     &ett_asap_parameter,
828     &ett_asap_cause,
829   };
830
831   /* Register the protocol name and description */
832   proto_asap = proto_register_protocol("Aggregate Server Access Protocol", "ASAP",  "asap");
833
834   /* Required function calls to register the header fields and subtrees used */
835   proto_register_field_array(proto_asap, hf, array_length(hf));
836   proto_register_subtree_array(ett, array_length(ett));
837
838 };
839
840 void
841 proto_reg_handoff_asap(void)
842 {
843   dissector_handle_t asap_handle;
844
845   asap_handle = create_dissector_handle(dissect_asap, proto_asap);
846   dissector_add("sctp.ppi",  ASAP_PAYLOAD_PROTO_ID, asap_handle);
847 }