Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / packet-cops.c
1 /* packet-cops.c
2  * Routines for the COPS (Common Open Policy Service) protocol dissection
3  * RFC2748 & COPS-PR extension RFC3084
4  *
5  * Copyright 2000, Heikki Vatiainen <hessu@cs.tut.fi>
6  *
7  * $Id: packet-cops.c,v 1.31 2002/05/10 23:20:37 guy Exp $
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
11  * Copyright 1998 Gerald Combs
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "packet-ipv6.h"
40 #include "packet-tcp.h"
41
42 #include "asn1.h"
43 #include "format-oid.h"
44 #include "prefs.h"
45
46 #define TCP_PORT_COPS 3288
47
48 /* Variable to hold the tcp port preference */
49 static guint global_cops_tcp_port = TCP_PORT_COPS;
50
51 /* desegmentation of COPS */
52 static gboolean cops_desegment = TRUE;
53
54 /* Variable to allow for proper deletion of dissector registration 
55  * when the user changes port from the gui
56  */
57
58 static guint cops_tcp_port = 0;
59
60 static gchar *last_decoded_prid=NULL;
61
62 #define COPS_OBJECT_HDR_SIZE 4
63
64 /* Null string of type "guchar[]". */
65 static const guchar nullstring[] = "";
66
67 #define SAFE_STRING(s)  (((s) != NULL) ? (s) : nullstring)
68
69 /* COPS PR Tags */
70
71 #define COPS_IPA    0           /* IP Address */
72 #define COPS_U32    2           /* Unsigned 32*/
73 #define COPS_TIT    3           /* TimeTicks */
74 #define COPS_OPQ    4           /* Opaque */
75 #define COPS_I64    10          /* Integer64 */
76 #define COPS_U64    11          /* Uinteger64 */
77
78 /* COPS PR Types */
79
80 #define COPS_NULL                0
81 #define COPS_INTEGER             1    /* l  */
82 #define COPS_OCTETSTR            2    /* c  */
83 #define COPS_OBJECTID            3    /* ul */
84 #define COPS_IPADDR              4    /* uc */
85 #define COPS_UNSIGNED32          5    /* ul */
86 #define COPS_TIMETICKS           7    /* ul */
87 #define COPS_OPAQUE              8    /* c  */
88 #define COPS_INTEGER64           10   /* ll */
89 #define COPS_UNSIGNED64          11   /* ull  */
90
91
92 typedef struct _COPS_CNV COPS_CNV;
93
94 struct _COPS_CNV
95 {
96   guint class;
97   guint tag;
98   gint  syntax;
99   gchar *name;
100 };
101
102 static COPS_CNV CopsCnv [] =
103 {
104   {ASN1_UNI, ASN1_NUL, COPS_NULL,      "NULL"},
105   {ASN1_UNI, ASN1_INT, COPS_INTEGER,   "INTEGER"},
106   {ASN1_UNI, ASN1_OTS, COPS_OCTETSTR,  "OCTET STRING"},
107   {ASN1_UNI, ASN1_OJI, COPS_OBJECTID,  "OBJECTID"},
108   {ASN1_APL, COPS_IPA, COPS_IPADDR,    "IPADDR"},
109   {ASN1_APL, COPS_U32, COPS_UNSIGNED32,"UNSIGNED32"},
110   {ASN1_APL, COPS_TIT, COPS_TIMETICKS, "TIMETICKS"},
111   {ASN1_APL, COPS_OPQ, COPS_OPAQUE,    "OPAQUE"},
112   {ASN1_APL, COPS_I64, COPS_INTEGER64, "INTEGER64"},
113   {ASN1_APL, COPS_U64, COPS_UNSIGNED64, "UNSIGNED64"},
114   {0,       0,         -1,                  NULL}
115 };
116
117 static gchar *
118 cops_tag_cls2syntax ( guint tag, guint cls, gushort *syntax)
119 {
120     COPS_CNV *cnv;
121
122     cnv = CopsCnv;
123     while (cnv->syntax != -1)
124     {
125         if (cnv->tag == tag && cnv->class == cls)
126         {
127             *syntax = cnv->syntax;
128             return cnv->name;
129         }
130         cnv++;
131     }
132     return NULL;
133 }
134
135 static const value_string cops_flags_vals[] = {
136         { 0x00,          "None" },
137         { 0x01,          "Solicited Message Flag Bit" },
138         { 0, NULL },
139 };
140
141 /* The different COPS message types */
142 enum cops_op_code {
143         COPS_NO_MSG,          /* Not a COPS Message type     */ 
144
145         COPS_MSG_REQ,         /* Request (REQ)               */
146         COPS_MSG_DEC,         /* Decision (DEC)              */
147         COPS_MSG_RPT,         /* Report State (RPT)          */
148         COPS_MSG_DRQ,         /* Delete Request State (DRQ)  */
149         COPS_MSG_SSQ,         /* Synchronize State Req (SSQ) */
150         COPS_MSG_OPN,         /* Client-Open (OPN)           */
151         COPS_MSG_CAT,         /* Client-Accept (CAT)         */
152         COPS_MSG_CC,          /* Client-Close (CC)           */
153         COPS_MSG_KA,          /* Keep-Alive (KA)             */
154         COPS_MSG_SSC,         /* Synchronize Complete (SSC)  */
155
156         COPS_LAST_OP_CODE     /* For error checking          */
157 };
158
159 static const value_string cops_op_code_vals[] = {
160         { COPS_MSG_REQ,          "Request (REQ)" },
161         { COPS_MSG_DEC,          "Decision (DEC)" },
162         { COPS_MSG_RPT,          "Report State (RPT)" },
163         { COPS_MSG_DRQ,          "Delete Request State (DRQ)" },
164         { COPS_MSG_SSQ,          "Synchronize State Req (SSQ)" },
165         { COPS_MSG_OPN,          "Client-Open (OPN)" },
166         { COPS_MSG_CAT,          "Client-Accept (CAT)" },
167         { COPS_MSG_CC,           "Client-Close (CC)" },
168         { COPS_MSG_KA,           "Keep-Alive (KA)" },
169         { COPS_MSG_SSC,          "Synchronize Complete (SSC)" },
170         { 0, NULL },
171 };
172
173
174 /* The different objects in COPS messages */
175 enum cops_c_num {
176         COPS_NO_OBJECT,        /* Not a COPS Object type               */
177
178         COPS_OBJ_HANDLE,       /* Handle Object (Handle)               */
179         COPS_OBJ_CONTEXT,      /* Context Object (Context)             */
180         COPS_OBJ_IN_INT,       /* In-Interface Object (IN-Int)         */
181         COPS_OBJ_OUT_INT,      /* Out-Interface Object (OUT-Int)       */
182         COPS_OBJ_REASON,       /* Reason Object (Reason)               */
183         COPS_OBJ_DECISION,     /* Decision Object (Decision)           */
184         COPS_OBJ_LPDPDECISION, /* LPDP Decision Object (LPDPDecision)  */
185         COPS_OBJ_ERROR,        /* Error Object (Error)                 */
186         COPS_OBJ_CLIENTSI,     /* Client Specific Information Object (ClientSI) */
187         COPS_OBJ_KATIMER,      /* Keep-Alive Timer Object (KATimer)    */
188         COPS_OBJ_PEPID,        /* PEP Identification Object (PEPID)    */
189         COPS_OBJ_REPORT_TYPE,  /* Report-Type Object (Report-Type)     */
190         COPS_OBJ_PDPREDIRADDR, /* PDP Redirect Address Object (PDPRedirAddr) */
191         COPS_OBJ_LASTPDPADDR,  /* Last PDP Address (LastPDPaddr)       */
192         COPS_OBJ_ACCTTIMER,    /* Accounting Timer Object (AcctTimer)  */
193         COPS_OBJ_INTEGRITY,    /* Message Integrity Object (Integrity) */       
194         COPS_LAST_C_NUM        /* For error checking                   */
195 };
196
197 static const value_string cops_c_num_vals[] = {
198         { COPS_OBJ_HANDLE,       "Handle Object (Handle)" },
199         { COPS_OBJ_CONTEXT,      "Context Object (Context)" },
200         { COPS_OBJ_IN_INT,       "In-Interface Object (IN-Int)" },
201         { COPS_OBJ_OUT_INT,      "Out-Interface Object (OUT-Int)" },
202         { COPS_OBJ_REASON,       "Reason Object (Reason)" },
203         { COPS_OBJ_DECISION,     "Decision Object (Decision)" },
204         { COPS_OBJ_LPDPDECISION, "LPDP Decision Object (LPDPDecision)" },
205         { COPS_OBJ_ERROR,        "Error Object (Error)" },
206         { COPS_OBJ_CLIENTSI,     "Client Specific Information Object (ClientSI)" },
207         { COPS_OBJ_KATIMER,      "Keep-Alive Timer Object (KATimer)" },
208         { COPS_OBJ_PEPID,        "PEP Identification Object (PEPID)" },
209         { COPS_OBJ_REPORT_TYPE,  "Report-Type Object (Report-Type)" },
210         { COPS_OBJ_PDPREDIRADDR, "PDP Redirect Address Object (PDPRedirAddr)" },
211         { COPS_OBJ_LASTPDPADDR,  "Last PDP Address (LastPDPaddr)" },
212         { COPS_OBJ_ACCTTIMER,    "Accounting Timer Object (AcctTimer)" },
213         { COPS_OBJ_INTEGRITY,    "Message Integrity Object (Integrity)" },
214         { 0, NULL },
215 };
216
217
218 /* The different objects in COPS-PR messages */
219 enum cops_s_num {
220         COPS_NO_PR_OBJECT,     /* Not a COPS-PR Object type               */
221         COPS_OBJ_PRID,         /* Provisioning Instance Identifier (PRID) */
222         COPS_OBJ_PPRID,        /* Prefix Provisioning Instance Identifier (PPRID) */
223         COPS_OBJ_EPD,          /* Encoded Provisioning Instance Data (EPD) */
224         COPS_OBJ_GPERR,        /* Global Provisioning Error Object (GPERR) */
225         COPS_OBJ_CPERR,        /* PRC Class Provisioning Error Object (CPERR) */
226         COPS_OBJ_ERRPRID,      /* Error Provisioning Instance Identifier (ErrorPRID)*/
227         
228         COPS_LAST_S_NUM        /* For error checking                   */
229 };
230
231
232 static const value_string cops_s_num_vals[] = {
233         { COPS_OBJ_PRID,         "Provisioning Instance Identifier (PRID)" },
234         { COPS_OBJ_PPRID,        "Prefix Provisioning Instance Identifier (PPRID)" },
235         { COPS_OBJ_EPD,          "Encoded Provisioning Instance Data (EPD)" },
236         { COPS_OBJ_GPERR,        "Global Provisioning Error Object (GPERR)" },
237         { COPS_OBJ_CPERR,        "PRC Class Provisioning Error Object (CPERR)" },
238         { COPS_OBJ_ERRPRID,      "Error Provisioning Instance Identifier (ErrorPRID)" },
239         { 0, NULL },
240
241 };
242
243 /* R-Type is carried within the Context Object */
244 static const value_string cops_r_type_vals[] = {
245         { 0x01, "Incoming-Message/Admission Control request" },
246         { 0x02, "Resource-Allocation request" },
247         { 0x04, "Outgoing-Message request" },
248         { 0x08, "Configuration request" },
249         { 0, NULL },
250 };
251 /* S-Type is carried within the ClientSI Object for COPS-PR*/
252 static const value_string cops_s_type_vals[] = {
253         { 0x01, "BER" },
254         { 0, NULL },
255 };
256
257 /* Reason-Code is carried within the Reason object */
258 static const value_string cops_reason_vals[] = {
259         { 1,  "Unspecified" },
260         { 2,  "Management" },
261         { 3,  "Preempted (Another request state takes precedence)" },
262         { 4,  "Tear (Used to communicate a signaled state removal)" },
263         { 5,  "Timeout (Local state has timed-out)" },
264         { 6,  "Route Change (Change invalidates request state)" },
265         { 7,  "Insufficient Resources (No local resource available)" },
266         { 8,  "PDP's Directive (PDP decision caused the delete)" },
267         { 9,  "Unsupported decision (PDP decision not supported)" },
268         { 10, "Synchronize Handle Unknown" },
269         { 11, "Transient Handle (stateless event)" },
270         { 12, "Malformed Decision (could not recover)" },
271         { 13, "Unknown COPS Object from PDP" },
272         { 0, NULL },
273 };
274
275 /* Command-Code is carried within the Decision object if C-Type is 1 */
276 static const value_string cops_dec_cmd_code_vals[] = {
277         { 0, "NULL Decision (No configuration data available)" },
278         { 1, "Install (Admit request/Install configuration)" },
279         { 2, "Remove (Remove request/Remove configuration)" },
280         { 0, NULL },
281 };
282
283 /* Decision flags are also carried with the Decision object if C-Type is 1 */
284 static const value_string cops_dec_cmd_flag_vals[] = {
285         { 0x00, "<None set>" },
286         { 0x01, "Trigger Error (Trigger error message if set)" },
287         { 0, NULL },
288 };
289
290 /* Error-Code from Error object */
291 static const value_string cops_error_vals[] = {
292         {1,  "Bad handle" },
293         {2,  "Invalid handle reference" },
294         {3,  "Bad message format (Malformed Message)" },
295         {4,  "Unable to process (server gives up on query)" },
296         {5,  "Mandatory client-specific info missing" },
297         {6,  "Unsupported client" },
298         {7,  "Mandatory COPS object missing" },
299         {8,  "Client Failure" },
300         {9,  "Communication Failure" },
301         {10, "Unspecified" },
302         {11, "Shutting down" },
303         {12, "Redirect to Preferred Server" },
304         {13, "Unknown COPS Object" },
305         {14, "Authentication Failure" },
306         {15, "Authentication Required" },
307         {0,  NULL },
308 };
309 /* Error-Code from GPERR object */
310 static const value_string cops_gperror_vals[] = {
311         {1,  "AvailMemLow" },
312         {2,  "AvailMemExhausted" },
313         {3,  "unknownASN.1Tag" },
314         {4,  "maxMsgSizeExceeded" },
315         {5,  "unknownError" },
316         {6,  "maxRequestStatesOpen" },
317         {7,  "invalidASN.1Length" },
318         {8,  "invalidObjectPad" },
319         {9,  "unknownPIBData" },
320         {10, "unknownCOPSPRObject" },
321         {11, "malformedDecision" },
322         {0,  NULL },
323 };
324
325 /* Error-Code from CPERR object */
326 static const value_string cops_cperror_vals[] = {
327         {1,  "priSpaceExhausted" },
328         {2,  "priInstanceInvalid" },
329         {3,  "attrValueInvalid" },
330         {4,  "attrValueSupLimited" },
331         {5,  "attrEnumSupLimited" },
332         {6,  "attrMaxLengthExceeded" },
333         {7,  "attrReferenceUnknown" },
334         {8,  "priNotifyOnly" },
335         {9,  "unknownPrc" },
336         {10, "tooFewAttrs" },
337         {11, "invalidAttrType" },
338         {12, "deletedInRef" },
339         {13, "priSpecificError" },
340         {0,  NULL },     
341 };
342         
343
344 /* Report-Type from Report-Type object */
345 static const value_string cops_report_type_vals[] = {
346         {1, " Success   : Decision was successful at the PEP" },
347         {2, " Failure   : Decision could not be completed by PEP" },
348         {3, " Accounting: Accounting update for an installed state" },
349         {0, NULL },
350 };
351
352 /* Initialize the protocol and registered fields */
353 static gint proto_cops = -1;
354 static gint hf_cops_ver_flags = -1;
355 static gint hf_cops_version = -1;
356 static gint hf_cops_flags = -1;
357
358 static gint hf_cops_op_code = -1;
359 static gint hf_cops_client_type = -1;
360 static gint hf_cops_msg_len = -1;
361
362 static gint hf_cops_obj_len = -1;
363 static gint hf_cops_obj_c_num = -1;
364 static gint hf_cops_obj_c_type = -1;
365
366 static gint hf_cops_obj_s_num = -1;
367 static gint hf_cops_obj_s_type = -1;
368
369 static gint hf_cops_r_type_flags = -1;
370 static gint hf_cops_m_type_flags = -1;
371
372 static gint hf_cops_in_int_ipv4 = -1;
373 static gint hf_cops_in_int_ipv6 = -1;
374 static gint hf_cops_out_int_ipv4 = -1;
375 static gint hf_cops_out_int_ipv6 = -1;
376 static gint hf_cops_int_ifindex = -1;
377
378 static gint hf_cops_reason = -1;
379 static gint hf_cops_reason_sub = -1;
380
381 static gint hf_cops_dec_cmd_code = -1;
382 static gint hf_cops_dec_flags = -1;
383
384 static gint hf_cops_error = -1;
385 static gint hf_cops_error_sub = -1;
386
387 static gint hf_cops_gperror = -1;
388 static gint hf_cops_gperror_sub = -1;
389
390 static gint hf_cops_cperror = -1;
391 static gint hf_cops_cperror_sub = -1;
392
393 static gint hf_cops_katimer = -1;
394
395 static gint hf_cops_pepid = -1;
396
397 static gint hf_cops_report_type = -1;
398
399 static gint hf_cops_pdprediraddr_ipv4 = -1;
400 static gint hf_cops_pdprediraddr_ipv6 = -1;
401 static gint hf_cops_lastpdpaddr_ipv4 = -1;
402 static gint hf_cops_lastpdpaddr_ipv6 = -1;
403 static gint hf_cops_pdp_tcp_port = -1;
404
405 static gint hf_cops_accttimer = -1;
406
407 static gint hf_cops_key_id = -1;
408 static gint hf_cops_seq_num = -1;
409
410 /* Initialize the subtree pointers */
411 static gint ett_cops = -1;
412 static gint ett_cops_ver_flags = -1;
413 static gint ett_cops_obj = -1;
414 static gint ett_cops_pr_obj = -1;
415 static gint ett_cops_obj_data = -1;
416 static gint ett_cops_r_type_flags = -1;
417 static gint ett_cops_itf = -1;
418 static gint ett_cops_reason = -1;
419 static gint ett_cops_decision = -1;
420 static gint ett_cops_error = -1;
421 static gint ett_cops_clientsi = -1;
422 static gint ett_cops_asn1 = -1;
423 static gint ett_cops_gperror = -1;
424 static gint ett_cops_cperror = -1;
425 static gint ett_cops_pdp = -1;
426
427 void proto_reg_handoff_cops(void);
428
429 static guint get_cops_pdu_len(tvbuff_t *tvb, int offset);
430 static void dissect_cops_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
431
432 static int dissect_cops_object(tvbuff_t *tvb, guint32 offset, proto_tree *tree);
433 static int dissect_cops_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
434                                     guint8 c_num, guint8 c_type, guint16 len);
435
436 static int dissect_cops_pr_objects(tvbuff_t *tvb, guint32 offset, proto_tree *tree, guint16 pr_len);
437 static int dissect_cops_pr_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
438                                        guint8 s_num, guint8 s_type, guint16 len);
439
440 /* Code to actually dissect the packets */
441 static void
442 dissect_cops(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
443 {
444         tcp_dissect_pdus(tvb, pinfo, tree, cops_desegment, 8,
445             get_cops_pdu_len, dissect_cops_pdu);
446 }
447
448 static guint
449 get_cops_pdu_len(tvbuff_t *tvb, int offset)
450 {
451         /*
452          * Get the length of the COPS message.
453          */
454         return tvb_get_ntohl(tvb, offset + 4);
455 }
456
457 static void
458 dissect_cops_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
459 {
460         guint8 op_code;
461
462         if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
463                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "COPS");
464         if (check_col(pinfo->cinfo, COL_INFO)) 
465                 col_clear(pinfo->cinfo, COL_INFO);
466     
467         op_code = tvb_get_guint8(tvb, 1);
468         if (check_col(pinfo->cinfo, COL_INFO))
469                 col_add_fstr(pinfo->cinfo, COL_INFO, "COPS %s",
470                              val_to_str(op_code, cops_op_code_vals, "Unknown Op Code"));
471
472         if (tree) {
473                 proto_item *ti, *tv;
474                 proto_tree *cops_tree, *ver_flags_tree;
475                 guint32 msg_len;
476                 guint32 offset = 0;
477                 guint8 ver_flags;
478                 gint garbage;
479
480                 ti = proto_tree_add_item(tree, proto_cops, tvb, offset, -1, FALSE);
481                 cops_tree = proto_item_add_subtree(ti, ett_cops);
482
483                 /* Version and flags share the same byte, put them in a subtree */
484                 ver_flags = tvb_get_guint8(tvb, offset);
485                 tv = proto_tree_add_uint_format(cops_tree, hf_cops_ver_flags, tvb, offset, 1,
486                                                   ver_flags, "Version: %u, Flags: %s",
487                                                   hi_nibble(ver_flags),
488                                                   val_to_str(lo_nibble(ver_flags), cops_flags_vals, "Unknown"));
489                 ver_flags_tree = proto_item_add_subtree(tv, ett_cops_ver_flags);
490                 proto_tree_add_uint(ver_flags_tree, hf_cops_version, tvb, offset, 1, ver_flags);
491                 proto_tree_add_uint(ver_flags_tree, hf_cops_flags, tvb, offset, 1, ver_flags);
492                 offset++;
493
494                 proto_tree_add_uint(cops_tree, hf_cops_op_code, tvb, offset, 1, tvb_get_guint8(tvb, offset));
495                 offset ++;
496                 proto_tree_add_uint(cops_tree, hf_cops_client_type, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
497                 offset += 2;
498
499                 msg_len = tvb_get_ntohl(tvb, offset);
500                 proto_tree_add_uint(cops_tree, hf_cops_msg_len, tvb, offset, 4, tvb_get_ntohl(tvb, offset));
501                 offset += 4;
502
503                 while (tvb_reported_length_remaining(tvb, offset) >= COPS_OBJECT_HDR_SIZE)
504                         offset += dissect_cops_object(tvb, offset, cops_tree);
505
506                 garbage = tvb_length_remaining(tvb, offset);
507                 if (garbage > 0)
508                         proto_tree_add_text(cops_tree, tvb, offset, garbage,
509                                             "Trailing garbage: %d byte%s", garbage,
510                                             plurality(garbage, "", "s"));
511         }
512
513         return;
514 }
515
516 static char *cops_c_type_to_str(guint8 c_num, guint8 c_type)
517 {
518         switch (c_num) {
519         case COPS_OBJ_HANDLE:
520                 if (c_type == 1)
521                         return "Client Handle";
522                 break;
523         case COPS_OBJ_IN_INT:
524         case COPS_OBJ_OUT_INT:
525                 if (c_type == 1)
526                         return "IPv4 Address + Interface";
527                 else if (c_type == 2)
528                         return "IPv6 Address + Interface";
529                 break;
530         case COPS_OBJ_DECISION:
531         case COPS_OBJ_LPDPDECISION:
532                 if (c_type == 1)
533                         return "Decision Flags (Mandatory)";
534                 else if (c_type == 2)
535                         return "Stateless Data";
536                 else if (c_type == 3)
537                         return "Replacement Data";
538                 else if (c_type == 4)
539                         return "Client Specific Decision Data";
540                 else if (c_type == 5)
541                         return "Named Decision Data";
542                 break;
543         case COPS_OBJ_CLIENTSI:
544                 if (c_type == 1)
545                         return "Signaled ClientSI";
546                 else if (c_type == 2)
547                         return "Named ClientSI";
548                 break;
549         case COPS_OBJ_KATIMER:
550                 if (c_type == 1)
551                         return "Keep-alive timer value";
552                 break;
553         case COPS_OBJ_PDPREDIRADDR:
554         case COPS_OBJ_LASTPDPADDR:
555                 if (c_type == 1)
556                         return "IPv4 Address + TCP Port";
557                 else if (c_type == 2)
558                         return "IPv6 Address + TCP Port";
559                 break;
560         case COPS_OBJ_ACCTTIMER:
561                 if (c_type == 1)
562                         return "Accounting timer value";
563                 break;
564         case COPS_OBJ_INTEGRITY:
565                 if (c_type == 1)
566                         return "HMAC digest";
567                 break;
568         }
569
570         return "";
571 }
572
573 static int dissect_cops_object(tvbuff_t *tvb, guint32 offset, proto_tree *tree)
574 {
575         guint16 object_len, contents_len;
576         guint8 c_num, c_type;
577         proto_item *ti;
578         proto_tree *obj_tree;
579         char *type_str;
580         int ret;
581
582         object_len = tvb_get_ntohs(tvb, offset);
583         c_num = tvb_get_guint8(tvb, offset + 2);
584         c_type = tvb_get_guint8(tvb, offset + 3);
585
586         ti = proto_tree_add_uint_format(tree, hf_cops_obj_c_num, tvb, offset, object_len, c_num,
587                                         "%s: %s", val_to_str(c_num, cops_c_num_vals, "Unknown"),
588                                         cops_c_type_to_str(c_num, c_type));
589         obj_tree = proto_item_add_subtree(ti, ett_cops_obj);
590
591         proto_tree_add_uint(obj_tree, hf_cops_obj_len, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
592         offset += 2;
593
594         proto_tree_add_uint(obj_tree, hf_cops_obj_c_num, tvb, offset, 1, c_num);
595         offset++;
596
597         type_str = cops_c_type_to_str(c_num, c_type);
598         proto_tree_add_text(obj_tree, tvb, offset, 1, "C-Type: %s%s%u%s",
599                             type_str,
600                             strlen(type_str) ? " (" : "",
601                             c_type,
602                             strlen(type_str) ? ")" : "");
603         offset++;
604
605         contents_len = object_len - COPS_OBJECT_HDR_SIZE;
606         ret = dissect_cops_object_data(tvb, offset, obj_tree, c_num, c_type, contents_len);
607         if (ret < 0) return 0;
608
609         /* Pad to 32bit boundary */
610         if (object_len % sizeof (guint32))
611                 object_len += (sizeof (guint32) - object_len % sizeof (guint32));
612         
613         return object_len;        
614 }
615
616 static int dissect_cops_pr_objects(tvbuff_t *tvb, guint32 offset, proto_tree *tree, guint16 pr_len)
617 {
618         guint16 object_len, contents_len;
619         guint8 s_num, s_type;
620         char *type_str;
621         int ret;
622         proto_tree *cops_pr_tree, *obj_tree;
623         proto_item *ti;
624
625         cops_pr_tree = proto_item_add_subtree(tree, ett_cops_pr_obj);
626         
627         while (pr_len >= COPS_OBJECT_HDR_SIZE) { 
628                 object_len = tvb_get_ntohs(tvb, offset);
629                 s_num = tvb_get_guint8(tvb, offset + 2);
630
631                 ti = proto_tree_add_uint_format(cops_pr_tree, hf_cops_obj_s_num, tvb, offset, object_len, s_num,
632                                         "%s", val_to_str(s_num, cops_s_num_vals, "Unknown"));
633                 obj_tree = proto_item_add_subtree(cops_pr_tree, ett_cops_pr_obj);
634
635                 proto_tree_add_uint(obj_tree, hf_cops_obj_len, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
636                 offset += 2;
637                 pr_len -= 2;
638  
639                 proto_tree_add_uint(obj_tree, hf_cops_obj_s_num, tvb, offset, 1, s_num);
640                 offset++;
641                 pr_len--;
642
643                 s_type = tvb_get_guint8(tvb, offset);
644                 type_str = val_to_str(s_type, cops_s_type_vals, "Unknown");
645                 proto_tree_add_text(obj_tree, tvb, offset, 1, "S-Type: %s%s%u%s",
646                             type_str,
647                             strlen(type_str) ? " (" : "",
648                             s_type,
649                             strlen(type_str) ? ")" : "");
650                 offset++;
651                 pr_len--;
652
653                 contents_len = object_len - COPS_OBJECT_HDR_SIZE;
654                 ret = dissect_cops_pr_object_data(tvb, offset, obj_tree, s_num, s_type, contents_len);
655                 if (ret < 0)
656                         break;
657
658                 /*Pad to 32bit boundary */
659                 if (object_len % sizeof (guint32))
660                         object_len += (sizeof (guint32) - object_len % sizeof (guint32));
661            
662                 pr_len -= object_len - COPS_OBJECT_HDR_SIZE;
663                 offset += object_len - COPS_OBJECT_HDR_SIZE;
664         }
665
666         return 0;
667 }
668
669 static int dissect_cops_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
670                                     guint8 c_num, guint8 c_type, guint16 len)
671 {
672         proto_item *ti;
673         proto_tree *r_type_tree, *itf_tree, *reason_tree, *dec_tree, *error_tree, *clientsi_tree, *pdp_tree;
674         guint16 r_type, m_type, reason, reason_sub, cmd_code, cmd_flags, error, error_sub, tcp_port;
675         guint32 ipv4addr, ifindex;
676         struct e_in6_addr ipv6addr;
677
678         switch (c_num) {
679         case COPS_OBJ_CONTEXT:
680                 r_type = tvb_get_ntohs(tvb, offset);
681                 m_type = tvb_get_ntohs(tvb, offset + 2);
682                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: R-Type: %s, M-Type: %u",
683                                          val_to_str(r_type, cops_r_type_vals, "Unknown"),
684                                          m_type);
685
686                 r_type_tree = proto_item_add_subtree(ti, ett_cops_r_type_flags);
687                 proto_tree_add_uint(r_type_tree, hf_cops_r_type_flags, tvb, offset, 2, r_type);
688                 offset += 2;
689                 proto_tree_add_uint(r_type_tree, hf_cops_m_type_flags, tvb, offset, 2, m_type);
690
691                 break;
692         case COPS_OBJ_IN_INT:
693         case COPS_OBJ_OUT_INT:
694                 if (c_type == 1) {          /* IPv4 */
695                         tvb_memcpy(tvb, (guint8 *)&ipv4addr, offset, 4);
696                         ifindex = tvb_get_ntohl(tvb, offset + 4);
697                         ti = proto_tree_add_text(tree, tvb, offset, 8, "Contents: IPv4 address %s, ifIndex: %u",
698                                                  ip_to_str((guint8 *)&ipv4addr), ifindex);
699                         itf_tree = proto_item_add_subtree(ti, ett_cops_itf);
700                         proto_tree_add_ipv4(itf_tree,
701                                             (c_num == COPS_OBJ_IN_INT) ? hf_cops_in_int_ipv4 : hf_cops_out_int_ipv4,
702                                             tvb, offset, 4, ipv4addr);
703                         offset += 4;
704                 } else if (c_type == 2) {   /* IPv6 */
705                         tvb_memcpy(tvb, (guint8 *)&ipv6addr, offset, sizeof ipv6addr);
706                         ifindex = tvb_get_ntohl(tvb, offset + sizeof ipv6addr);
707                         ti = proto_tree_add_text(tree, tvb, offset, 20, "Contents: IPv6 address %s, ifIndex: %u",
708                                                  ip6_to_str(&ipv6addr), ifindex);
709                         itf_tree = proto_item_add_subtree(ti, ett_cops_itf);
710                         proto_tree_add_ipv6(itf_tree,
711                                             (c_num == COPS_OBJ_IN_INT) ? hf_cops_in_int_ipv6 : hf_cops_out_int_ipv6,
712                                             tvb, offset, 16, (guint8 *)&ipv6addr);
713                         offset += 16;
714                 } else {
715                         break;
716                 }
717                 proto_tree_add_uint(itf_tree, hf_cops_int_ifindex, tvb, offset, 4, ifindex);
718
719                 break;
720         case COPS_OBJ_REASON:
721                 reason = tvb_get_ntohs(tvb, offset);
722                 reason_sub = tvb_get_ntohs(tvb, offset + 2);
723                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Reason-Code: %s, Reason Sub-code: 0x%04x",
724                                          val_to_str(reason, cops_reason_vals, "<Unknown value>"), reason_sub);
725                 reason_tree = proto_item_add_subtree(ti, ett_cops_reason);
726                 proto_tree_add_uint(reason_tree, hf_cops_reason, tvb, offset, 2, reason);
727                 offset += 2;
728                 if (reason == 13) {
729                         proto_tree_add_text(reason_tree, tvb, offset, 2, "Reason Sub-code: "
730                                             "Unknown object's C-Num %u, C-Type %u",
731                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
732                 } else 
733                         proto_tree_add_uint(reason_tree, hf_cops_reason_sub, tvb, offset, 2, reason_sub);
734
735                 break;
736         case COPS_OBJ_DECISION:
737         case COPS_OBJ_LPDPDECISION:
738                 if (c_type == 1) {
739                         cmd_code = tvb_get_ntohs(tvb, offset);
740                         cmd_flags = tvb_get_ntohs(tvb, offset + 2);
741                         ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Command-Code: %s, Flags: %s",
742                                          val_to_str(cmd_code, cops_dec_cmd_code_vals, "<Unknown value>"),
743                                          val_to_str(cmd_flags, cops_dec_cmd_flag_vals, "<Unknown flag>"));
744                         dec_tree = proto_item_add_subtree(ti, ett_cops_decision);
745                         proto_tree_add_uint(dec_tree, hf_cops_dec_cmd_code, tvb, offset, 2, cmd_code);
746                         offset += 2;
747                         proto_tree_add_uint(dec_tree, hf_cops_dec_flags, tvb, offset, 2, cmd_flags);
748                 } else if (c_type == 5) { /*COPS-PR Data*/
749                         ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: %u bytes", len);
750                         dec_tree = proto_item_add_subtree(ti, ett_cops_decision);
751                         dissect_cops_pr_objects(tvb, offset, dec_tree, len);
752                 }
753
754                 break;
755         case COPS_OBJ_ERROR:
756                 if (c_type != 1)
757                         break;
758                 
759                 error = tvb_get_ntohs(tvb, offset);
760                 error_sub = tvb_get_ntohs(tvb, offset + 2);
761                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
762                                          val_to_str(error, cops_error_vals, "<Unknown value>"), error_sub);
763                 error_tree = proto_item_add_subtree(ti, ett_cops_error);
764                 proto_tree_add_uint(error_tree, hf_cops_error, tvb, offset, 2, error);
765                 offset += 2;
766                 if (error == 13) {
767                         proto_tree_add_text(error_tree, tvb, offset, 2, "Error Sub-code: "
768                                             "Unknown object's C-Num %u, C-Type %u",
769                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
770                 } else 
771                         proto_tree_add_uint(error_tree, hf_cops_error_sub, tvb, offset, 2, error_sub);
772
773                 break;
774         case COPS_OBJ_CLIENTSI:
775           
776                 if (c_type != 2) /*Not COPS-PR data*/
777                       break;
778
779                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: %u bytes", len);
780                 clientsi_tree = proto_item_add_subtree(ti, ett_cops_clientsi);
781
782                 dissect_cops_pr_objects(tvb, offset, clientsi_tree, len);
783
784                 break;
785         case COPS_OBJ_KATIMER:
786                 if (c_type != 1)
787                         break;
788
789                 proto_tree_add_item(tree, hf_cops_katimer, tvb, offset + 2, 2, FALSE);
790                 if (tvb_get_ntohs(tvb, offset + 2) == 0)
791                         proto_tree_add_text(tree, tvb, offset, 0, "Value of zero implies infinity.");
792                 
793                 break;
794         case COPS_OBJ_PEPID:
795                 if (c_type != 1)
796                         break;
797
798                 if (tvb_strnlen(tvb, offset, len) == -1)
799                         proto_tree_add_text(tree, tvb, offset, len, "<PEP Id is not a NUL terminated ASCII string>");
800                 else
801                         proto_tree_add_item(tree, hf_cops_pepid, tvb, offset,
802                                             tvb_strnlen(tvb, offset, len) + 1, FALSE);
803
804                 break;
805         case COPS_OBJ_REPORT_TYPE:
806                 if (c_type != 1)
807                         break;
808
809                 proto_tree_add_item(tree, hf_cops_report_type, tvb, offset, 2, FALSE);
810
811                 break;
812         case COPS_OBJ_PDPREDIRADDR:
813         case COPS_OBJ_LASTPDPADDR:
814                 if (c_type == 1) {          /* IPv4 */
815                         tvb_memcpy(tvb, (guint8 *)&ipv4addr, offset, 4);
816                         tcp_port = tvb_get_ntohs(tvb, offset + 4 + 2);
817                         ti = proto_tree_add_text(tree, tvb, offset, 8, "Contents: IPv4 address %s, TCP Port Number: %u",
818                                                  ip_to_str((guint8 *)&ipv4addr), tcp_port);
819                         pdp_tree = proto_item_add_subtree(ti, ett_cops_pdp);
820                         proto_tree_add_ipv4(pdp_tree,
821                                             (c_num == COPS_OBJ_PDPREDIRADDR) ? hf_cops_pdprediraddr_ipv4 : hf_cops_lastpdpaddr_ipv4,
822                                             tvb, offset, 4, ipv4addr);
823                         offset += 4;
824                 } else if (c_type == 2) {   /* IPv6 */
825                         tvb_memcpy(tvb, (guint8 *)&ipv6addr, offset, sizeof ipv6addr);
826                         tcp_port = tvb_get_ntohs(tvb, offset + sizeof ipv6addr + 2);
827                         ti = proto_tree_add_text(tree, tvb, offset, 20, "Contents: IPv6 address %s, TCP Port Number: %u",
828                                                  ip6_to_str(&ipv6addr), tcp_port);
829                         pdp_tree = proto_item_add_subtree(ti, ett_cops_pdp);
830                         proto_tree_add_ipv6(pdp_tree,
831                                             (c_num == COPS_OBJ_PDPREDIRADDR) ? hf_cops_pdprediraddr_ipv6 : hf_cops_lastpdpaddr_ipv6,
832                                             tvb, offset, 16, (guint8 *)&ipv6addr);
833                         offset += 16;
834                 } else {
835                         break;
836                 }
837                 offset += 2;
838                 proto_tree_add_uint(pdp_tree, hf_cops_pdp_tcp_port, tvb, offset, 2, tcp_port);
839
840                 break;
841         case COPS_OBJ_ACCTTIMER:
842                 if (c_type != 1)
843                         break;
844
845                 proto_tree_add_item(tree, hf_cops_accttimer, tvb, offset + 2, 2, FALSE);
846                 if (tvb_get_ntohs(tvb, offset + 2) == 0)
847                         proto_tree_add_text(tree, tvb, offset, 0, "Value of zero means "
848                                             "there SHOULD be no unsolicited accounting updates.");
849
850                 break;
851         case COPS_OBJ_INTEGRITY:
852                 if (c_type != 1)
853                         break;      /* Not HMAC digest */
854
855                 proto_tree_add_item(tree, hf_cops_key_id, tvb, offset, 4, FALSE);
856                 proto_tree_add_item(tree, hf_cops_seq_num, tvb, offset + 4, 4, FALSE);
857                 proto_tree_add_text(tree, tvb, offset + 8 , len - 8, "Contents: Keyed Message Digest");
858
859                 break;
860         default:
861                 proto_tree_add_text(tree, tvb, offset, len, "Contents: %u bytes", len);
862
863                 break;
864         }
865
866         return 0;
867 }
868
869
870 /*convert hex to binary string (1010....)*/
871 static gchar* xtobstr(guint8 *hex, guint len) {
872
873   guint i=0,j=0,k=0, bit=0;
874   gchar *binstr=NULL;
875   guint8 mask = 0x80;
876      
877   binstr = g_malloc(8*sizeof(gchar)*len);
878      
879   for (i=0; i < len; i++) {
880     for ( j=0; j<8; j++ ) {   /* for each bit     */
881       bit = (mask & hex[i]) ? 1 : 0;  /* bit is 1 or 0    */
882       sprintf(&binstr[k++],"%d",bit); /*put it in string */
883       mask >>= 1;    /* shift mask right */
884     }
885     mask = 0x80;
886   }
887   return binstr;
888 }
889
890 /*
891  * XXX - we should perhaps support reading PIBs, as we support reading
892  * MIBs, and use the PIBs we read to understand how to display COPS data
893  * just as we use the MIBs we read to understand how to display SNMP
894  * data.
895  */
896 static int decode_cops_pr_asn1_data(tvbuff_t *tvb, guint32 offset,
897     proto_tree *tree, guint epdlen, gboolean inepd)
898 {
899         ASN1_SCK asn1; 
900         int start;
901         gboolean def;
902         guint length;
903
904         guint vb_length;
905         gushort vb_type;
906         gchar *vb_type_name;
907
908         int ret;
909         guint cls, con, tag;
910
911         gint32 vb_integer_value;
912         guint32 vb_uinteger_value;
913
914         guint8 *vb_octet_string;
915
916         subid_t *vb_oid;
917         guint vb_oid_length;
918
919         gchar *vb_display_string;
920
921         unsigned int i=0;
922         gchar *buf;
923         int len;
924
925         while (epdlen > 0) { /*while there is stuff to be decoded*/
926                 asn1_open(&asn1, tvb, offset);
927
928                 /* parse the type of the object */
929
930                 start = asn1.offset;
931
932                 ret = asn1_header_decode (&asn1, &cls, &con, &tag, &def,
933                     &vb_length);
934                 if (ret != ASN1_ERR_NOERROR)
935                         return 0;
936                 if (!def)
937                         return ASN1_ERR_LENGTH_NOT_DEFINITE;
938
939                 /* Convert the class, constructed flag, and tag to a type. */
940                 vb_type_name = cops_tag_cls2syntax(tag, cls, &vb_type);
941                 if (vb_type_name == NULL) {
942                         /*
943                          * Unsupported type.
944                          * Dissect the value as an opaque string of octets.
945                          */
946                         vb_type_name = "unsupported type";
947                         vb_type = COPS_OPAQUE;
948                 }
949
950                 /* parse the value */
951
952                 switch (vb_type) {
953
954                 case COPS_INTEGER:
955                         ret = asn1_int32_value_decode(&asn1, vb_length,
956                             &vb_integer_value);
957                         if (ret != ASN1_ERR_NOERROR)
958                                 return ret;
959                         length = asn1.offset - start;
960                         if (tree) {
961                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
962                                     "Value: %s: %d (%#x)", vb_type_name,
963                                     vb_integer_value, vb_integer_value);
964                         }
965                         break;
966
967                 case COPS_UNSIGNED32:
968                 case COPS_TIMETICKS:
969                         ret = asn1_uint32_value_decode(&asn1, vb_length,
970                             &vb_uinteger_value);
971                         if (ret != ASN1_ERR_NOERROR)
972                                 return ret;
973                         length = asn1.offset - start;
974                         if (tree) {
975                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
976                                     "Value: %s: %u (%#x)", vb_type_name,
977                                     vb_uinteger_value, vb_uinteger_value);
978                         }
979                         break;
980
981                 case COPS_OCTETSTR:
982                 case COPS_IPADDR:
983                 case COPS_OPAQUE:
984                 case COPS_UNSIGNED64:
985                 case COPS_INTEGER64:
986                         ret = asn1_string_value_decode (&asn1, vb_length,
987                             &vb_octet_string);
988                         if (ret != ASN1_ERR_NOERROR)
989                                 return ret;
990                         length = asn1.offset - start;
991                         if (tree) {
992                                 /* if this EPD belongs to ipFilter or
993                                    frwkPrcSupport Entries print it correctly) */
994                                 if ((strncmp(last_decoded_prid,"1.3.6.1.2.2.2.3.2.1",19)==0) || (strncmp(last_decoded_prid,"1.3.6.1.2.2.2.1.1.1",19)==0)) {
995                                         if(strncmp(last_decoded_prid,"1.3.6.1.2.2.2.3.2.1",19)==0) {
996                                                 i=0;/* EPD belongs to
997                                                        IpFilters, print as
998                                                        bytes (IPv6 not printed
999                                                        ok - yet)*/   
1000                                         } else {
1001                                                 /* EPD belongs to
1002                                                    frwkPrcSupportEntry,
1003                                                    convert hex byte(s) to
1004                                                    binary string*/ 
1005                                                 vb_display_string =
1006                                                     xtobstr(vb_octet_string,
1007                                                       vb_length);
1008                           
1009                                                 proto_tree_add_text(tree, asn1.tvb,
1010                                                     offset, length,
1011                                                     "Value: %s: %s", vb_type_name,
1012                                                     vb_display_string);
1013                       
1014                                                 g_free(vb_octet_string);
1015                                                 g_free(vb_display_string);
1016                       
1017                                                 break;
1018                                         }
1019                                 } else {
1020                                         /* EPD doesn't belong to ipFilter or
1021                                            frwkPrcSupport Entries;
1022                                            check for unprintable characters*/
1023
1024                                         for (i = 0; i < vb_length; i++) {
1025                                                 if (!(isprint(vb_octet_string[i])
1026                                                     ||isspace(vb_octet_string[i])))
1027                                                         break;
1028                                         }
1029                                 }
1030
1031                                 /*
1032                                  * If some characters are not printable,
1033                                  * display the string as bytes.
1034                                  */     
1035                                 if (i < vb_length) {
1036                                         /*
1037                                          * We stopped, due to a non-printable
1038                                          * character, before we got to the end
1039                                          * of the string.
1040                                          */
1041                                         vb_display_string =
1042                                             g_malloc(4*vb_length);
1043                                         buf = &vb_display_string[0];
1044                                         len = sprintf(buf, "%03u",
1045                                             vb_octet_string[0]);
1046                                         buf += len;
1047                                         for (i = 1; i < vb_length; i++) {
1048                                                 len = sprintf(buf, ".%03u",
1049                                                     vb_octet_string[i]);
1050                                                 buf += len;
1051                                         }
1052                                         proto_tree_add_text(tree,
1053                                             asn1.tvb, offset, length,
1054                                             "Value: %s: %s", vb_type_name,
1055                                             vb_display_string);
1056                                         g_free(vb_display_string);
1057                                 } else {
1058                                         proto_tree_add_text(tree,
1059                                             asn1.tvb, offset, length,
1060                                             "Value: %s: %.*s", vb_type_name,
1061                                             (int)vb_length,
1062                                             SAFE_STRING(vb_octet_string));
1063                                 }
1064                         }
1065                         g_free(vb_octet_string);
1066                         break;
1067
1068                 case COPS_NULL:
1069                         ret = asn1_null_decode (&asn1, vb_length);
1070                         if (ret != ASN1_ERR_NOERROR)
1071                                 return ret;
1072                         length = asn1.offset - start;
1073                         if (tree) {
1074                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
1075                                     "Value: %s", vb_type_name);
1076                         }
1077                         break;
1078
1079                 case COPS_OBJECTID:
1080                         ret = asn1_oid_value_decode (&asn1, vb_length, &vb_oid,
1081                             &vb_oid_length);
1082                         if (ret != ASN1_ERR_NOERROR)
1083                                 return ret;
1084                         length = asn1.offset - start;
1085
1086                         if (tree) {
1087                                 vb_display_string = format_oid(vb_oid,
1088                                     vb_oid_length);
1089                                 proto_tree_add_text(tree, asn1.tvb, offset, length,
1090                                     "Value: %s: %s", vb_type_name,
1091                                     vb_display_string);
1092                 
1093                                 if (inepd) {
1094                                         /* we're decoding EPD */
1095                                         g_free(vb_display_string);
1096                                 } else {
1097                                         /* we're decoding PRID, so let's store
1098                                            the OID of the PRID so that later
1099                                            when we're decoding this PRID's EPD
1100                                            we can finetune the output. */
1101                                         if (last_decoded_prid)
1102                                                 g_free(last_decoded_prid);
1103                                         last_decoded_prid = vb_display_string;
1104                                 }
1105                         }
1106                         g_free(vb_oid);
1107                         break;
1108
1109                 default:
1110                         g_assert_not_reached();
1111                         return ASN1_ERR_WRONG_TYPE;
1112                 }
1113   
1114                 asn1_close(&asn1,&offset);
1115  
1116                 epdlen -= length;
1117         }
1118         return 0;
1119 }
1120
1121 static int dissect_cops_pr_object_data(tvbuff_t *tvb, guint32 offset, proto_tree *tree,
1122                                     guint8 s_num, guint8 s_type, guint16 len)
1123 {
1124         proto_item *ti;
1125         proto_tree *asn1_object_tree, *gperror_tree, *cperror_tree;
1126         guint16 gperror=0, gperror_sub=0, cperror=0, cperror_sub=0;
1127
1128         switch (s_num){
1129         case COPS_OBJ_PRID:
1130                if (s_type != 1) /* Not Provisioning Instance Identifier (PRID) */
1131                         break; 
1132
1133                 ti=proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1134                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1135
1136                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1137
1138                 break;
1139         case COPS_OBJ_PPRID: 
1140                 if (s_type != 1) /* Not Prefix Provisioning Instance Identifier (PPRID) */
1141                         break; 
1142
1143                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1144                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1145
1146                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1147
1148                 break;
1149         case COPS_OBJ_EPD:
1150                 if (s_type != 1) /* Not  Encoded Provisioning Instance Data (EPD) */
1151                         break; 
1152
1153                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1154                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1155
1156                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, TRUE);
1157                         
1158                 break;
1159         case COPS_OBJ_GPERR:
1160                 if (s_type != 1) /* Not Global Provisioning Error Object (GPERR) */
1161                         break;
1162                 
1163                 gperror = tvb_get_ntohs(tvb, offset);
1164                 gperror_sub = tvb_get_ntohs(tvb, offset + 2);
1165                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
1166                                          val_to_str(gperror, cops_gperror_vals, "<Unknown value>"), gperror_sub);
1167                 gperror_tree = proto_item_add_subtree(ti, ett_cops_gperror);
1168                 proto_tree_add_uint(gperror_tree, hf_cops_gperror, tvb, offset, 2, gperror);
1169                 offset += 2;
1170                 if (cperror == 13) {
1171                         proto_tree_add_text(gperror_tree, tvb, offset, 2, "Error Sub-code: "
1172                                             "Unknown object's C-Num %u, C-Type %u",
1173                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
1174                 } else 
1175                         proto_tree_add_uint(gperror_tree, hf_cops_gperror_sub, tvb, offset, 2, gperror_sub);
1176
1177                 break;
1178         case COPS_OBJ_CPERR:
1179                 if (s_type != 1) /*Not PRC Class Provisioning Error Object (CPERR) */
1180                         break;
1181                 
1182                 break;
1183
1184                 cperror = tvb_get_ntohs(tvb, offset);
1185                 cperror_sub = tvb_get_ntohs(tvb, offset + 2);
1186                 ti = proto_tree_add_text(tree, tvb, offset, 4, "Contents: Error-Code: %s, Error Sub-code: 0x%04x",
1187                                          val_to_str(cperror, cops_cperror_vals, "<Unknown value>"), cperror_sub);
1188                 cperror_tree = proto_item_add_subtree(ti, ett_cops_cperror);
1189                 proto_tree_add_uint(cperror_tree, hf_cops_cperror, tvb, offset, 2, cperror);
1190                 offset += 2;
1191                 if (cperror == 13) {
1192                         proto_tree_add_text(cperror_tree, tvb, offset, 2, "Error Sub-code: "
1193                                             "Unknown object's S-Num %u, C-Type %u",
1194                                             tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
1195                 } else 
1196                         proto_tree_add_uint(cperror_tree, hf_cops_cperror_sub, tvb, offset, 2, cperror_sub);
1197
1198                 break;
1199         case COPS_OBJ_ERRPRID:
1200                 if (s_type != 1) /*Not  Error Provisioning Instance Identifier (ErrorPRID)*/
1201                         break;
1202
1203                 ti = proto_tree_add_text(tree, tvb, offset, len, "Contents:");
1204                 asn1_object_tree = proto_item_add_subtree(ti, ett_cops_asn1);
1205
1206                 decode_cops_pr_asn1_data(tvb, offset, asn1_object_tree, len, FALSE);
1207
1208                 break;
1209         default:
1210                 proto_tree_add_text(tree, tvb, offset, len, "Contents: %u bytes", len);
1211                 break;
1212         }
1213
1214         return 0;
1215 }
1216
1217
1218 /* Register the protocol with Ethereal */
1219 void proto_register_cops(void)
1220 {                 
1221         /* Setup list of header fields */
1222         static hf_register_info hf[] = {
1223                 { &hf_cops_ver_flags,
1224                         { "Version and Flags",           "cops.ver_flags",
1225                         FT_UINT8, BASE_HEX, NULL, 0x0,
1226                         "Version and Flags in COPS Common Header", HFILL }
1227                 },
1228                 { &hf_cops_version,
1229                         { "Version",           "cops.version",
1230                         FT_UINT8, BASE_DEC, NULL, 0xF0,
1231                         "Version in COPS Common Header", HFILL }
1232                 },
1233                 { &hf_cops_flags,
1234                         { "Flags",           "cops.flags",
1235                         FT_UINT8, BASE_HEX, VALS(cops_flags_vals), 0x0F,
1236                         "Flags in COPS Common Header", HFILL }
1237                 },
1238                 { &hf_cops_op_code,
1239                         { "Op Code",           "cops.op_code",
1240                         FT_UINT8, BASE_DEC, VALS(cops_op_code_vals), 0x0,
1241                         "Op Code in COPS Common Header", HFILL }
1242                 },
1243                 { &hf_cops_client_type,
1244                         { "Client Type",           "cops.client_type",
1245                         FT_UINT16, BASE_DEC, NULL, 0x0,
1246                         "Client Type in COPS Common Header", HFILL }
1247                 },
1248                 { &hf_cops_msg_len,
1249                         { "Message Length",           "cops.msg_len",
1250                         FT_UINT32, BASE_DEC, NULL, 0x0,
1251                         "Message Length in COPS Common Header", HFILL }
1252                 },
1253                 { &hf_cops_obj_len,
1254                         { "Object Length",           "cops.obj.len",
1255                         FT_UINT32, BASE_DEC, NULL, 0x0,
1256                         "Object Length in COPS Object Header", HFILL }
1257                 },
1258                 { &hf_cops_obj_c_num,
1259                         { "C-Num",           "cops.c_num",
1260                         FT_UINT8, BASE_DEC, VALS(cops_c_num_vals), 0x0,
1261                         "C-Num in COPS Object Header", HFILL }
1262                 },
1263                 { &hf_cops_obj_c_type,
1264                         { "C-Type",           "cops.c_type",
1265                         FT_UINT8, BASE_DEC, NULL, 0x0,
1266                         "C-Type in COPS Object Header", HFILL }
1267                 },
1268
1269                 { &hf_cops_obj_s_num,
1270                         { "S-Num",           "cops.s_num",
1271                         FT_UINT8, BASE_DEC, VALS(cops_s_num_vals), 0x0,
1272                         "S-Num in COPS-PR Object Header", HFILL }
1273                 },
1274                 { &hf_cops_obj_s_type,
1275                         { "S-Type",           "cops.s_type",
1276                         FT_UINT8, BASE_DEC, NULL, 0x0,
1277                         "S-Type in COPS-PR Object Header", HFILL }
1278                 },
1279
1280                 { &hf_cops_r_type_flags,
1281                         { "R-Type",           "cops.context.r_type",
1282                         FT_UINT16, BASE_HEX, VALS(cops_r_type_vals), 0xFFFF,
1283                         "R-Type in COPS Context Object", HFILL }
1284                 },
1285                 { &hf_cops_m_type_flags,
1286                         { "M-Type",           "cops.context.m_type",
1287                         FT_UINT16, BASE_HEX, NULL, 0xFFFF,
1288                         "M-Type in COPS Context Object", HFILL }
1289                 },
1290                 { &hf_cops_in_int_ipv4,
1291                         { "IPv4 address",           "cops.in-int.ipv4",
1292                         FT_IPv4, 0, NULL, 0xFFFF,
1293                         "IPv4 address in COPS IN-Int object", HFILL }
1294                 },
1295                 { &hf_cops_in_int_ipv6,
1296                         { "IPv6 address",           "cops.in-int.ipv6",
1297                         FT_IPv6, 0, NULL, 0xFFFF,
1298                         "IPv6 address in COPS IN-Int object", HFILL }
1299                 },
1300                 { &hf_cops_out_int_ipv4,
1301                         { "IPv4 address",           "cops.out-int.ipv4",
1302                         FT_IPv4, 0, NULL, 0xFFFF,
1303                         "IPv4 address in COPS OUT-Int object", HFILL }
1304                 },
1305                 { &hf_cops_out_int_ipv6,
1306                         { "IPv6 address",           "cops.out-int.ipv6",
1307                         FT_IPv6, 0, NULL, 0xFFFF,
1308                         "IPv6 address in COPS OUT-Int", HFILL }
1309                 },
1310                 { &hf_cops_int_ifindex,
1311                         { "ifIndex",           "cops.in-out-int.ifindex",
1312                         FT_UINT32, BASE_DEC, NULL, 0x0,
1313                         "If SNMP is supported, corresponds to MIB-II ifIndex", HFILL }
1314                 },
1315                 { &hf_cops_reason,
1316                         { "Reason",           "cops.reason",
1317                         FT_UINT16, BASE_DEC, VALS(cops_reason_vals), 0,
1318                         "Reason in Reason object", HFILL }
1319                 },
1320                 { &hf_cops_reason_sub,
1321                         { "Reason Sub-code",           "cops.reason_sub",
1322                         FT_UINT16, BASE_HEX, NULL, 0,
1323                         "Reason Sub-code in Reason object", HFILL }
1324                 },
1325                 { &hf_cops_dec_cmd_code,
1326                         { "Command-Code",           "cops.decision.cmd",
1327                         FT_UINT16, BASE_DEC, VALS(cops_dec_cmd_code_vals), 0,
1328                         "Command-Code in Decision/LPDP Decision object", HFILL }
1329                 },
1330                 { &hf_cops_dec_flags,
1331                         { "Flags",           "cops.decision.flags",
1332                         FT_UINT16, BASE_HEX, VALS(cops_dec_cmd_flag_vals), 0xffff,
1333                         "Flags in Decision/LPDP Decision object", HFILL }
1334                 },
1335                 { &hf_cops_error,
1336                         { "Error",           "cops.error",
1337                         FT_UINT16, BASE_DEC, VALS(cops_error_vals), 0,
1338                         "Error in Error object", HFILL }
1339                 },
1340                 { &hf_cops_error_sub,
1341                         { "Error Sub-code",           "cops.error_sub",
1342                         FT_UINT16, BASE_HEX, NULL, 0,
1343                         "Error Sub-code in Error object", HFILL }
1344                 },
1345                 { &hf_cops_katimer,
1346                         { "Contents: KA Timer Value",           "cops.katimer.value",
1347                         FT_UINT16, BASE_DEC, NULL, 0,
1348                         "Keep-Alive Timer Value in KATimer object", HFILL }
1349                 },
1350                 { &hf_cops_pepid,
1351                         { "Contents: PEP Id",           "cops.pepid.id",
1352                         FT_STRING, BASE_NONE, NULL, 0,
1353                         "PEP Id in PEPID object", HFILL }
1354                 },
1355                 { &hf_cops_report_type,
1356                         { "Contents: Report-Type",           "cops.report_type",
1357                         FT_UINT16, BASE_DEC, VALS(cops_report_type_vals), 0,
1358                         "Report-Type in Report-Type object", HFILL }
1359                 },
1360                 { &hf_cops_pdprediraddr_ipv4,
1361                         { "IPv4 address",           "cops.pdprediraddr.ipv4",
1362                         FT_IPv4, 0, NULL, 0xFFFF,
1363                         "IPv4 address in COPS PDPRedirAddr object", HFILL }
1364                 },
1365                 { &hf_cops_pdprediraddr_ipv6,
1366                         { "IPv6 address",           "cops.pdprediraddr.ipv6",
1367                         FT_IPv6, 0, NULL, 0xFFFF,
1368                         "IPv6 address in COPS PDPRedirAddr object", HFILL }
1369                 },
1370                 { &hf_cops_lastpdpaddr_ipv4,
1371                         { "IPv4 address",           "cops.lastpdpaddr.ipv4",
1372                         FT_IPv4, 0, NULL, 0xFFFF,
1373                         "IPv4 address in COPS LastPDPAddr object", HFILL }
1374                 },
1375                 { &hf_cops_lastpdpaddr_ipv6,
1376                         { "IPv6 address",           "cops.lastpdpaddr.ipv6",
1377                         FT_IPv6, 0, NULL, 0xFFFF,
1378                         "IPv6 address in COPS LastPDPAddr object", HFILL }
1379                 },
1380                 { &hf_cops_pdp_tcp_port,
1381                         { "TCP Port Number",           "cops.pdp.tcp_port",
1382                         FT_UINT32, BASE_DEC, NULL, 0x0,
1383                          "TCP Port Number of PDP in PDPRedirAddr/LastPDPAddr object", HFILL }
1384                 },
1385                 { &hf_cops_accttimer,
1386                         { "Contents: ACCT Timer Value",           "cops.accttimer.value",
1387                         FT_UINT16, BASE_DEC, NULL, 0,
1388                         "Accounting Timer Value in AcctTimer object", HFILL }
1389                 },
1390                 { &hf_cops_key_id,
1391                         { "Contents: Key ID",           "cops.integrity.key_id",
1392                         FT_UINT32, BASE_DEC, NULL, 0,
1393                         "Key ID in Integrity object", HFILL }
1394                 },
1395                 { &hf_cops_seq_num,
1396                         { "Contents: Sequence Number",           "cops.integrity.seq_num",
1397                         FT_UINT32, BASE_DEC, NULL, 0,
1398                         "Sequence Number in Integrity object", HFILL }
1399                 },
1400                 { &hf_cops_gperror,
1401                         { "Error",           "cops.gperror",
1402                         FT_UINT16, BASE_DEC, VALS(cops_gperror_vals), 0,
1403                         "Error in Error object", HFILL }
1404                 },
1405                 { &hf_cops_gperror_sub,
1406                         { "Error Sub-code",           "cops.gperror_sub",
1407                         FT_UINT16, BASE_HEX, NULL, 0,
1408                         "Error Sub-code in Error object", HFILL }
1409                 },
1410                 { &hf_cops_cperror,
1411                         { "Error",           "cops.cperror",
1412                         FT_UINT16, BASE_DEC, VALS(cops_cperror_vals), 0,
1413                         "Error in Error object", HFILL }
1414                 },
1415                 { &hf_cops_cperror_sub,
1416                         { "Error Sub-code",           "cops.cperror_sub",
1417                         FT_UINT16, BASE_HEX, NULL, 0,
1418                         "Error Sub-code in Error object", HFILL }
1419                 },
1420
1421         };
1422
1423         /* Setup protocol subtree array */
1424         static gint *ett[] = {
1425                 &ett_cops,
1426                 &ett_cops_ver_flags,
1427                 &ett_cops_obj,
1428                 &ett_cops_pr_obj,
1429                 &ett_cops_obj_data,
1430                 &ett_cops_r_type_flags,
1431                 &ett_cops_itf,
1432                 &ett_cops_reason,
1433                 &ett_cops_decision,
1434                 &ett_cops_error,
1435                 &ett_cops_clientsi,
1436                 &ett_cops_asn1,
1437                 &ett_cops_gperror,
1438                 &ett_cops_cperror,
1439                 &ett_cops_pdp,
1440         };
1441
1442         module_t* cops_module;
1443
1444         /* Register the protocol name and description */
1445         proto_cops = proto_register_protocol("Common Open Policy Service",
1446             "COPS", "cops");
1447
1448         /* Required function calls to register the header fields and subtrees used */
1449         proto_register_field_array(proto_cops, hf, array_length(hf));
1450         proto_register_subtree_array(ett, array_length(ett));
1451         
1452         /* Register our configuration options for cops, 
1453          * particularly our ports
1454          */
1455         cops_module = prefs_register_protocol(proto_cops,
1456                                               proto_reg_handoff_cops);
1457         prefs_register_uint_preference(cops_module,"tcp.cops_port",
1458                                        "COPS TCP Port",
1459                                        "Set the TCP port for COPS messages",
1460                                        10,&global_cops_tcp_port);
1461         prefs_register_bool_preference(cops_module, "desegment",
1462             "Desegment all COPS messages spanning multiple TCP segments",
1463             "Whether the COPS dissector should desegment all messages spanning multiple TCP segments",
1464             &cops_desegment);
1465 };
1466
1467 void
1468 proto_reg_handoff_cops(void)
1469 {
1470         static int cops_prefs_initialized = FALSE;
1471         static dissector_handle_t cops_handle;
1472
1473         if (!cops_prefs_initialized) {
1474                 cops_handle = create_dissector_handle(dissect_cops, proto_cops);
1475                 cops_prefs_initialized = TRUE;
1476         } else
1477                 dissector_delete("tcp.port",cops_tcp_port,cops_handle);
1478         
1479         /* Set our port numbers for future use */
1480         cops_tcp_port = global_cops_tcp_port;
1481
1482         dissector_add("tcp.port", cops_tcp_port, cops_handle);
1483 }