change the signature that asn2wrs generates for functions to marm all parameters...
[obnox/wireshark/wip.git] / epan / dissectors / packet-wccp.c
1 /* packet-wccp.c
2  * Routines for Web Cache Coordination Protocol dissection
3  * Jerry Talkington <jtalkington@users.sourceforge.net>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/strutil.h>
34 #include <epan/emem.h>
35 #include "packet-wccp.h"
36
37 static int proto_wccp = -1;
38 static int hf_wccp_message_type = -1;   /* the message type */
39 static int hf_wccp_version = -1;        /* protocol version */
40 static int hf_hash_revision = -1;       /* the version of the hash */
41 static int hf_change_num = -1;          /* change number */
42 static int hf_recvd_id = -1;
43 static int hf_cache_ip = -1;
44
45 static gint ett_wccp = -1;
46 static gint ett_cache_count = -1;
47 static gint ett_buckets = -1;
48 static gint ett_flags = -1;
49 static gint ett_cache_info = -1;
50 static gint ett_security_info = -1;
51 static gint ett_service_info = -1;
52 static gint ett_service_flags = -1;
53 static gint ett_router_identity_element = -1;
54 static gint ett_router_identity_info = -1;
55 static gint ett_wc_identity_element = -1;
56 static gint ett_wc_identity_info = -1;
57 static gint ett_router_view_info = -1;
58 static gint ett_wc_view_info = -1;
59 static gint ett_router_assignment_element = -1;
60 static gint ett_router_assignment_info = -1;
61 static gint ett_query_info = -1;
62 static gint ett_capabilities_info = -1;
63 static gint ett_capability_element = -1;
64 static gint ett_capability_forwarding_method = -1;
65 static gint ett_capability_assignment_method = -1;
66 static gint ett_capability_return_method = -1;
67 static gint ett_unknown_info = -1;
68
69 /*
70  * At
71  *
72  *      http://www.alternic.org/drafts/drafts-f-g/draft-forster-wrec-wccp-v1-00.html
73  *
74  * is a copy of the now-expired Internet-Draft for WCCP 1.0.
75  *
76  * At
77  *
78  *      http://search.ietf.org/internet-drafts/draft-wilson-wrec-wccp-v2-01.txt
79  *
80  * is an Internet-Draft for WCCP 2.0.
81  */
82
83 #define UDP_PORT_WCCP   2048
84
85 #define WCCPv1                  4
86 #define WCCPv2                  0x0200
87 #define WCCP_HERE_I_AM          7
88 #define WCCP_I_SEE_YOU          8
89 #define WCCP_ASSIGN_BUCKET      9
90 #define WCCP2_HERE_I_AM         10
91 #define WCCP2_I_SEE_YOU         11
92 #define WCCP2_REDIRECT_ASSIGN   12
93 #define WCCP2_REMOVAL_QUERY     13
94
95 static const value_string wccp_type_vals[] = {
96     { WCCP_HERE_I_AM,        "1.0 Here I am" },
97     { WCCP_I_SEE_YOU,        "1.0 I see you" },
98     { WCCP_ASSIGN_BUCKET,    "1.0 Assign bucket" },
99     { WCCP2_HERE_I_AM,       "2.0 Here I am" },
100     { WCCP2_I_SEE_YOU,       "2.0 I see you" },
101     { WCCP2_REDIRECT_ASSIGN, "2.0 Redirect assign" },
102     { WCCP2_REMOVAL_QUERY,   "2.0 Removal query" },
103     { 0,                     NULL }
104 };
105
106 static const value_string wccp_version_val[] = {
107         { WCCPv1, "1"},
108         { WCCPv2, "2"},
109         { 0, NULL}
110 };
111
112 #define HASH_INFO_SIZE  (4*(1+8+1))
113
114 #define WCCP_U_FLAG     0x80000000
115
116 #define WCCP2_SECURITY_INFO             0
117 #define WCCP2_SERVICE_INFO              1
118 #define WCCP2_ROUTER_ID_INFO            2
119 #define WCCP2_WC_ID_INFO                3
120 #define WCCP2_RTR_VIEW_INFO             4
121 #define WCCP2_WC_VIEW_INFO              5
122 #define WCCP2_REDIRECT_ASSIGNMENT       6
123 #define WCCP2_QUERY_INFO                7
124 #define WCCP2_CAPABILITIES_INFO         8
125 #define WCCP2_ALT_ASSIGNMENT            13
126 #define WCCP2_ASSIGN_MAP                14
127 #define WCCP2_COMMAND_EXTENSION         15
128
129 static const value_string info_type_vals[] = {
130         { WCCP2_SECURITY_INFO,       "Security Info" },
131         { WCCP2_SERVICE_INFO,        "Service Info" },
132         { WCCP2_ROUTER_ID_INFO,      "Router Identity Info" },
133         { WCCP2_WC_ID_INFO,          "Web-Cache Identity Info" },
134         { WCCP2_RTR_VIEW_INFO,       "Router View Info" },
135         { WCCP2_WC_VIEW_INFO,        "Web-Cache View Info" },
136         { WCCP2_REDIRECT_ASSIGNMENT, "Assignment Info" },
137         { WCCP2_QUERY_INFO,          "Query Info" },
138         { WCCP2_CAPABILITIES_INFO,   "Capabilities Info" },
139         { WCCP2_COMMAND_EXTENSION,   "Command Extension" },
140         { 0,                         NULL }
141 };
142
143 const value_string service_id_vals[] = {
144     { 0x00, "HTTP" },
145     { 0,    NULL }
146 };
147
148 typedef struct capability_flag {
149         guint32 value;
150         const char *short_name;
151         const char *long_name;
152 } capability_flag;
153
154 static void dissect_hash_data(tvbuff_t *tvb, int offset,
155     proto_tree *wccp_tree);
156 static void dissect_web_cache_list_entry(tvbuff_t *tvb, int offset,
157     int index, proto_tree *wccp_tree);
158 static int wccp_bucket_info(guint8 bucket_info, proto_tree *bucket_tree,
159     guint32 start, tvbuff_t *tvb, int offset);
160 static gchar *bucket_name(guint8 bucket);
161 static guint16 dissect_wccp2_header(tvbuff_t *tvb, int offset,
162     proto_tree *wccp_tree);
163 static void dissect_wccp2_info(tvbuff_t *tvb, int offset, guint16 length,
164     proto_tree *wccp_tree);
165 static gboolean dissect_wccp2_security_info(tvbuff_t *tvb, int offset,
166     int length, proto_tree *info_tree);
167 static gboolean dissect_wccp2_service_info(tvbuff_t *tvb, int offset,
168     int length, proto_tree *info_tree);
169 static gboolean dissect_wccp2_router_identity_info(tvbuff_t *tvb,
170     int offset, int length, proto_tree *info_tree);
171 static gboolean dissect_wccp2_wc_identity_info(tvbuff_t *tvb, int offset,
172     int length, proto_tree *info_tree);
173 static gboolean dissect_wccp2_router_view_info(tvbuff_t *tvb, int offset,
174     int length, proto_tree *info_tree);
175 static gboolean dissect_wccp2_wc_view_info(tvbuff_t *tvb, int offset,
176     int length, proto_tree *info_tree);
177 static gboolean dissect_wccp2_assignment_info(tvbuff_t *tvb, int offset,
178     int length, proto_tree *info_tree);
179 static gboolean dissect_wccp2_router_query_info(tvbuff_t *tvb, int offset,
180     int length, proto_tree *info_tree);
181 static gboolean dissect_wccp2_capability_info(tvbuff_t *tvb, int offset,
182     int length, proto_tree *info_tree);
183 static void dissect_32_bit_capability_flags(tvbuff_t *tvb, int curr_offset,
184     guint16 capability_val_len, gint ett, const capability_flag *flags,
185     proto_tree *element_tree);
186
187 static void
188 dissect_wccp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
189 {
190         int offset = 0;
191         proto_tree *wccp_tree = NULL;
192         proto_item *wccp_tree_item;
193         guint32 wccp_message_type;
194         guint16 length;
195         guint32 cache_count;
196         guint32 ipaddr;
197         guint i;
198
199         if(check_col(pinfo->cinfo, COL_PROTOCOL)) {
200                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "WCCP");
201         }
202         if(check_col(pinfo->cinfo, COL_INFO)) {
203                 col_clear(pinfo->cinfo, COL_INFO);
204         }
205
206         wccp_message_type = tvb_get_ntohl(tvb, offset);
207
208         if(check_col(pinfo->cinfo, COL_INFO)) {
209                 col_add_str(pinfo->cinfo, COL_INFO, val_to_str(wccp_message_type,
210                     wccp_type_vals, "Unknown WCCP message (%u)"));
211         }
212
213         if(tree != NULL) {
214                 wccp_tree_item = proto_tree_add_item(tree, proto_wccp, tvb, offset,
215                     -1, FALSE);
216                 wccp_tree = proto_item_add_subtree(wccp_tree_item, ett_wccp);
217
218                 proto_tree_add_uint(wccp_tree, hf_wccp_message_type, tvb, offset,
219                     sizeof(wccp_message_type), wccp_message_type);
220                 offset += sizeof(wccp_message_type);
221
222                 switch (wccp_message_type) {
223
224                 case WCCP_HERE_I_AM:
225                         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb,
226                             offset, 4, FALSE);
227                         offset += 4;
228                         dissect_hash_data(tvb, offset, wccp_tree);
229                         offset += HASH_INFO_SIZE;
230                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
231                             4, FALSE);
232                         offset += 4;
233                         break;
234
235                 case WCCP_I_SEE_YOU:
236                         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb,
237                             offset, 4, FALSE);
238                         offset += 4;
239                         proto_tree_add_item(wccp_tree, hf_change_num, tvb, offset,
240                             4, FALSE);
241                         offset += 4;
242                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
243                             4, FALSE);
244                         offset += 4;
245                         cache_count = tvb_get_ntohl(tvb, offset);
246                         proto_tree_add_text(wccp_tree, tvb, offset, 4,
247                             "Number of Web Caches: %u", cache_count);
248                         offset += 4;
249                         for (i = 0; i < cache_count; i++) {
250                                 dissect_web_cache_list_entry(tvb, offset, i,
251                                     wccp_tree);
252                                 offset += 4 + HASH_INFO_SIZE;
253                         }
254                         break;
255
256                 case WCCP_ASSIGN_BUCKET:
257                         /*
258                          * This hasn't been tested, since I don't have any
259                          * traces with this in it.
260                          *
261                          * The V1 spec claims that this does, indeed,
262                          * have a Received ID field after the type,
263                          * rather than a Version field.
264                          */
265                         proto_tree_add_item(wccp_tree, hf_recvd_id, tvb, offset,
266                             4, FALSE);
267                         offset += 4;
268                         cache_count = tvb_get_ntohl(tvb, offset);
269                         proto_tree_add_text(wccp_tree, tvb, offset, 4,
270                             "Number of Web Caches: %u", cache_count);
271                         offset += 4;
272                         for (i = 0; i < cache_count; i++) {
273                                 ipaddr = tvb_get_ipv4(tvb, offset);
274                                 proto_tree_add_ipv4_format(wccp_tree,
275                                     hf_cache_ip, tvb, offset, 4,
276                                     ipaddr,
277                                     "Web Cache %d IP Address: %s", i,
278                                     ip_to_str((guint8 *)&ipaddr));
279                                 offset += 4;
280                         }
281                         for (i = 0; i < 256; i += 4) {
282                                 proto_tree_add_text(wccp_tree, tvb, offset, 4,
283                                     "Buckets %d - %d: %10s %10s %10s %10s",
284                                     i, i + 3,
285                                     bucket_name(tvb_get_guint8(tvb, offset)),
286                                     bucket_name(tvb_get_guint8(tvb, offset+1)),
287                                     bucket_name(tvb_get_guint8(tvb, offset+2)),
288                                     bucket_name(tvb_get_guint8(tvb, offset+3)));
289                                 offset += 4;
290                         }
291                         break;
292
293                 case WCCP2_HERE_I_AM:
294                 case WCCP2_I_SEE_YOU:
295                 case WCCP2_REMOVAL_QUERY:
296                 case WCCP2_REDIRECT_ASSIGN:
297                 default:        /* assume unknown packets are v2 */
298                         length = dissect_wccp2_header(tvb, offset, wccp_tree);
299                         offset += 4;
300                         dissect_wccp2_info(tvb, offset, length, wccp_tree);
301                         break;
302                 }
303         }
304 }
305
306 static void
307 dissect_hash_data(tvbuff_t *tvb, int offset, proto_tree *wccp_tree)
308 {
309         proto_item *bucket_item;
310         proto_tree *bucket_tree;
311         proto_item *tf;
312         proto_tree *field_tree;
313         int i;
314         guint8 bucket_info;
315         int n;
316         guint32 flags;
317
318         proto_tree_add_item(wccp_tree, hf_hash_revision, tvb, offset, 4,
319             FALSE);
320         offset += 4;
321
322         bucket_item = proto_tree_add_text(wccp_tree, tvb, offset, 32,
323             "Hash information");
324         bucket_tree = proto_item_add_subtree(bucket_item, ett_buckets);
325
326         for (i = 0, n = 0; i < 32; i++) {
327                 bucket_info = tvb_get_guint8(tvb, offset);
328                 n = wccp_bucket_info(bucket_info, bucket_tree, n, tvb, offset);
329                 offset += 1;
330         }
331         flags = tvb_get_ntohl(tvb, offset);
332         tf = proto_tree_add_text(wccp_tree, tvb, offset, 4,
333             "Flags: 0x%08X (%s)", flags,
334             ((flags & WCCP_U_FLAG) ?
335               "Hash information is historical" :
336               "Hash information is current"));
337         field_tree = proto_item_add_subtree(tf, ett_flags);
338         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
339             decode_boolean_bitfield(flags, WCCP_U_FLAG,
340               sizeof (flags)*8,
341               "Hash information is historical",
342               "Hash information is current"));
343 }
344
345 static void
346 dissect_web_cache_list_entry(tvbuff_t *tvb, int offset, int index,
347     proto_tree *wccp_tree)
348 {
349         proto_item *tl;
350         proto_tree *list_entry_tree;
351
352         tl = proto_tree_add_text(wccp_tree, tvb, offset, 4 + HASH_INFO_SIZE,
353             "Web-Cache List Entry(%d)", index);
354         list_entry_tree = proto_item_add_subtree(tl, ett_cache_info);
355         proto_tree_add_item(list_entry_tree, hf_cache_ip, tvb, offset, 4,
356             FALSE);
357         dissect_hash_data(tvb, offset + 4, list_entry_tree);
358 }
359
360 /*
361  * wccp_bucket_info()
362  * takes an integer representing a "Hash Information" bitmap, and spits out
363  * the corresponding proto_tree entries, returning the next bucket number.
364  */
365 static int
366 wccp_bucket_info(guint8 bucket_info, proto_tree *bucket_tree, guint32 start,
367     tvbuff_t *tvb, int offset)
368 {
369         guint32 i;
370
371         for(i = 0; i < 8; i++) {
372                 proto_tree_add_text(bucket_tree, tvb, offset, sizeof(bucket_info), "Bucket %3d: %s", start, (bucket_info & 1<<i ? "Assigned" : "Not Assigned") );
373                 start++;
374         }
375         return(start);
376 }
377
378 static gchar *
379 bucket_name(guint8 bucket)
380 {
381         gchar *cur=NULL;
382
383         if (bucket == 0xff) {
384                 cur="Unassigned";
385         } else {
386                 cur=ep_alloc(10+1);
387                 g_snprintf(cur, 10+1, "%u", bucket);
388         }
389         return cur;
390 }
391
392 static guint16
393 dissect_wccp2_header(tvbuff_t *tvb, int offset, proto_tree *wccp_tree)
394 {
395         guint16 length;
396
397         proto_tree_add_item(wccp_tree, hf_wccp_version, tvb, offset, 2,
398             FALSE);
399         offset += 2;
400         length = tvb_get_ntohs(tvb, offset);
401         proto_tree_add_text(wccp_tree, tvb, offset, 2, "Length: %u",
402             length);
403         return length;
404 }
405
406 static void
407 dissect_wccp2_info(tvbuff_t *tvb, int offset, guint16 length,
408     proto_tree *wccp_tree)
409 {
410         guint16 type;
411         guint16 item_length;
412         proto_item *ti;
413         proto_tree *info_tree;
414         gint ett;
415         gboolean (*dissector)(tvbuff_t *, int, int, proto_tree *);
416
417         while (length != 0) {
418                 type = tvb_get_ntohs(tvb, offset);
419                 item_length = tvb_get_ntohs(tvb, offset+2);
420
421                 switch (type) {
422
423                 case WCCP2_SECURITY_INFO:
424                         ett = ett_security_info;
425                         dissector = dissect_wccp2_security_info;
426                         break;
427
428                 case WCCP2_SERVICE_INFO:
429                         ett = ett_service_info;
430                         dissector = dissect_wccp2_service_info;
431                         break;
432
433                 case WCCP2_ROUTER_ID_INFO:
434                         ett = ett_router_identity_info;
435                         dissector = dissect_wccp2_router_identity_info;
436                         break;
437
438                 case WCCP2_WC_ID_INFO:
439                         ett = ett_wc_identity_info;
440                         dissector = dissect_wccp2_wc_identity_info;
441                         break;
442
443                 case WCCP2_RTR_VIEW_INFO:
444                         ett = ett_router_view_info;
445                         dissector = dissect_wccp2_router_view_info;
446                         break;
447
448                 case WCCP2_WC_VIEW_INFO:
449                         ett = ett_wc_view_info;
450                         dissector = dissect_wccp2_wc_view_info;
451                         break;
452
453                 case WCCP2_REDIRECT_ASSIGNMENT:
454                         ett = ett_router_assignment_info;
455                         dissector = dissect_wccp2_assignment_info;
456                         break;
457
458                 case WCCP2_QUERY_INFO:
459                         ett = ett_query_info;
460                         dissector = dissect_wccp2_router_query_info;
461                         break;
462
463                 case WCCP2_CAPABILITIES_INFO:
464                         ett = ett_capabilities_info;
465                         dissector = dissect_wccp2_capability_info;
466                         break;
467
468                 default:
469                         ett = ett_unknown_info;
470                         dissector = NULL;
471                         break;
472                 }
473
474                 ti = proto_tree_add_text(wccp_tree, tvb, offset, item_length + 4,
475                     val_to_str(type, info_type_vals, "Unknown info type (%u)"));
476                 info_tree = proto_item_add_subtree(ti, ett);
477                 proto_tree_add_text(info_tree, tvb, offset, 2,
478                     "Type: %s",
479                     val_to_str(type, info_type_vals, "Unknown info type (%u)"));
480                 proto_tree_add_text(info_tree, tvb, offset+2, 2,
481                     "Length: %u", item_length);
482                 offset += 4;
483                 length -= 4;
484
485                 /*
486                  * XXX - pass in "length" and check for that as well.
487                  */
488                 if (dissector != NULL) {
489                         if (!(*dissector)(tvb, offset, item_length, info_tree))
490                                 return; /* ran out of data */
491                 } else {
492                         proto_tree_add_text(info_tree, tvb, offset, item_length,
493                             "Data: %u byte%s", item_length,
494                             plurality(item_length, "", "s"));
495                 }
496                 offset += item_length;
497                 length -= item_length;
498         }
499 }
500
501 #define SECURITY_INFO_LEN               4
502
503 #define WCCP2_NO_SECURITY               0
504 #define WCCP2_MD5_SECURITY              1
505
506 static gboolean
507 dissect_wccp2_security_info(tvbuff_t *tvb, int offset, int length,
508     proto_tree *info_tree)
509 {
510         guint32 security_option;
511
512         if (length < SECURITY_INFO_LEN) {
513                 proto_tree_add_text(info_tree, tvb, offset, 0,
514                     "Item length is %u, should be %u", length,
515                     SECURITY_INFO_LEN);
516                 return TRUE;
517         }
518
519         security_option = tvb_get_ntohl(tvb, offset);
520         switch (security_option) {
521
522         case WCCP2_NO_SECURITY:
523                 proto_tree_add_text(info_tree, tvb, offset, 4,
524                     "Security Option: None");
525                 break;
526
527         case WCCP2_MD5_SECURITY:
528                 proto_tree_add_text(info_tree, tvb, offset, 4,
529                     "Security Option: MD5");
530                 offset += 4;
531                 if (length > 4) {
532                         proto_tree_add_text(info_tree, tvb, offset,
533                             length - 4, "MD5 checksum: %s",
534                             tvb_bytes_to_str(tvb, offset, length - 4));
535                 }
536                 break;
537
538         default:
539                 proto_tree_add_text(info_tree, tvb, offset, 4,
540                     "Security Option: Unknown (%u)", security_option);
541                 break;
542         }
543         return TRUE;
544 }
545
546 #define SERVICE_INFO_LEN                (4+4+8*2)
547
548 #define WCCP2_SERVICE_STANDARD          0
549 #define WCCP2_SERVICE_DYNAMIC           1
550
551 /*
552  * Service flags.
553  */
554 #define WCCP2_SI_SRC_IP_HASH            0x0001
555 #define WCCP2_SI_DST_IP_HASH            0x0002
556 #define WCCP2_SI_SRC_PORT_HASH          0x0004
557 #define WCCP2_SI_DST_PORT_HASH          0x0008
558 #define WCCP2_SI_PORTS_DEFINED          0x0010
559 #define WCCP2_SI_PORTS_SOURCE           0x0020
560 #define WCCP2_SI_SRC_IP_ALT_HASH        0x0100
561 #define WCCP2_SI_DST_IP_ALT_HASH        0x0200
562 #define WCCP2_SI_SRC_PORT_ALT_HASH      0x0400
563 #define WCCP2_SI_DST_PORT_ALT_HASH      0x0800
564
565 static gboolean
566 dissect_wccp2_service_info(tvbuff_t *tvb, int offset, int length,
567     proto_tree *info_tree)
568 {
569         guint8 service_type;
570         guint32 flags;
571         proto_item *tf;
572         proto_tree *field_tree;
573         int i;
574
575         if (length != SERVICE_INFO_LEN) {
576                 proto_tree_add_text(info_tree, tvb, offset, 0,
577                     "Item length is %u, should be %u", length,
578                     SERVICE_INFO_LEN);
579                 return TRUE;
580         }
581
582         service_type = tvb_get_guint8(tvb, offset);
583         switch (service_type) {
584
585         case WCCP2_SERVICE_STANDARD:
586                 proto_tree_add_text(info_tree, tvb, offset, 1,
587                     "Service Type: Well-known service");
588                 proto_tree_add_text(info_tree, tvb, offset+1, 1,
589                     "Service ID: %s",
590                     val_to_str(tvb_get_guint8(tvb, offset+1), service_id_vals,
591                             "Unknown (0x%02X)"));
592
593                 break;
594
595         case WCCP2_SERVICE_DYNAMIC:
596                 proto_tree_add_text(info_tree, tvb, offset, 1,
597                     "Service Type: Dynamic service");
598                 proto_tree_add_text(info_tree, tvb, offset+1, 1,
599                     "Service ID: %s",
600                     val_to_str(tvb_get_guint8(tvb, offset+1), service_id_vals,
601                             "Unknown (0x%02X)"));
602                 proto_tree_add_text(info_tree, tvb, offset+2, 1,
603                     "Priority: %u", tvb_get_guint8(tvb, offset+2));
604                 /*
605                  * XXX - does "IP protocol identifier" mean this is a
606                  * protocol type of the sort you get in IP headers?
607                  * If so, we should get a table of those from the
608                  * IP dissector, and use that.
609                  */
610                 proto_tree_add_text(info_tree, tvb, offset+3, 1,
611                     "Protocol: %u", tvb_get_guint8(tvb, offset+3));     /* IP protocol identifier */
612                 break;
613
614         default:
615                 proto_tree_add_text(info_tree, tvb, offset, 1,
616                     "Service Type: Unknown (%u)", service_type);
617                 break;
618         }
619         offset += 4;
620
621         flags = tvb_get_ntohl(tvb, offset);
622         tf = proto_tree_add_text(info_tree, tvb, offset, 4,
623             "Flags: 0x%08X", flags);
624         field_tree = proto_item_add_subtree(tf, ett_service_flags);
625         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
626             decode_boolean_bitfield(flags, WCCP2_SI_SRC_IP_HASH,
627               sizeof (flags)*8,
628               "Use source IP address in primary hash",
629               "Don't use source IP address in primary hash"));
630         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
631             decode_boolean_bitfield(flags, WCCP2_SI_DST_IP_HASH,
632               sizeof (flags)*8,
633               "Use destination IP address in primary hash",
634               "Don't use destination IP address in primary hash"));
635         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
636             decode_boolean_bitfield(flags, WCCP2_SI_SRC_PORT_HASH,
637               sizeof (flags)*8,
638               "Use source port in primary hash",
639               "Don't use source port in primary hash"));
640         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
641             decode_boolean_bitfield(flags, WCCP2_SI_DST_PORT_HASH,
642               sizeof (flags)*8,
643               "Use destination port in primary hash",
644               "Don't use destination port in primary hash"));
645         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
646             decode_boolean_bitfield(flags, WCCP2_SI_PORTS_DEFINED,
647               sizeof (flags)*8,
648               "Ports defined",
649               "Ports not defined"));
650         if (flags & WCCP2_SI_PORTS_DEFINED) {
651                 proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
652                     decode_boolean_bitfield(flags, WCCP2_SI_PORTS_SOURCE,
653                       sizeof (flags)*8,
654                       "Ports refer to source port",
655                       "Ports refer to destination port"));
656         }
657         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
658             decode_boolean_bitfield(flags, WCCP2_SI_SRC_IP_ALT_HASH,
659               sizeof (flags)*8,
660               "Use source IP address in secondary hash",
661               "Don't use source IP address in secondary hash"));
662         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
663             decode_boolean_bitfield(flags, WCCP2_SI_DST_IP_ALT_HASH,
664               sizeof (flags)*8,
665               "Use destination IP address in secondary hash",
666               "Don't use destination IP address in secondary hash"));
667         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
668             decode_boolean_bitfield(flags, WCCP2_SI_SRC_PORT_ALT_HASH,
669               sizeof (flags)*8,
670               "Use source port in secondary hash",
671               "Don't use source port in secondary hash"));
672         proto_tree_add_text(field_tree, tvb, offset, 4, "%s",
673             decode_boolean_bitfield(flags, WCCP2_SI_DST_PORT_ALT_HASH,
674               sizeof (flags)*8,
675               "Use destination port in secondary hash",
676               "Don't use destination port in secondary hash"));
677         offset += 4;
678
679         if (flags & WCCP2_SI_PORTS_DEFINED) {
680                 for (i = 0; i < 8; i++) {
681                         proto_tree_add_text(info_tree, tvb, offset, 2,
682                             "Port %d: %u", i, tvb_get_ntohs(tvb, offset));
683                         offset += 2;
684                 }
685         }
686
687         return TRUE;
688 }
689
690 #define ROUTER_ID_INFO_MIN_LEN          (8+4+4)
691
692 static void
693 dissect_wccp2_router_identity_element(tvbuff_t *tvb, int offset,
694     proto_tree *tree)
695 {
696         proto_tree_add_text(tree, tvb, offset, 4,
697             "IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
698         proto_tree_add_text(tree, tvb, offset + 4, 4,
699             "Receive ID: %u", tvb_get_ntohl(tvb, offset + 4));
700 }
701
702 static gboolean
703 dissect_wccp2_router_identity_info(tvbuff_t *tvb, int offset, int length,
704     proto_tree *info_tree)
705 {
706         guint32 n_received_from;
707         guint i;
708         proto_item *te;
709         proto_tree *element_tree;
710
711         if (length < ROUTER_ID_INFO_MIN_LEN) {
712                 proto_tree_add_text(info_tree, tvb, offset, 0,
713                     "Item length is %u, should be >= %u", length,
714                     ROUTER_ID_INFO_MIN_LEN);
715                 return TRUE;
716         }
717
718         te = proto_tree_add_text(info_tree, tvb, offset, 8,
719             "Router Identity Element: IP address %s",
720             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
721         element_tree = proto_item_add_subtree(te,
722             ett_router_identity_element);
723         dissect_wccp2_router_identity_element(tvb, offset, element_tree);
724         offset += 8;
725
726         proto_tree_add_text(info_tree, tvb, offset, 4,
727             "Sent To IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
728         offset += 4;
729
730         n_received_from = tvb_get_ntohl(tvb, offset);
731         proto_tree_add_text(info_tree, tvb, offset, 4,
732             "Number of Received From IP addresses: %u", n_received_from);
733         offset += 4;
734
735         for (i = 0; i < n_received_from; i++) {
736                 proto_tree_add_text(info_tree, tvb, offset, 4,
737                     "Received From IP Address %d: %s", i,
738                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
739                 offset += 4;
740         }
741
742         return TRUE;
743 }
744
745 #define WC_ID_INFO_LEN                  (4+4+8*4+4)
746
747 static gboolean
748 dissect_wccp2_web_cache_identity_element(tvbuff_t *tvb, int offset,
749     proto_tree *tree)
750 {
751         proto_item *bucket_item;
752         proto_tree *bucket_tree;
753         proto_item *tf;
754         proto_tree *field_tree;
755         guint16 flags;
756         int i;
757         guint8 bucket_info;
758         int n;
759
760         proto_tree_add_text(tree, tvb, offset, 4,
761             "Web-Cache IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
762         offset += 4;
763
764         proto_tree_add_text(tree, tvb, offset, 2,
765             "Hash Revision %u", tvb_get_ntohs(tvb, offset));
766         offset += 2;
767
768         flags = tvb_get_ntohs(tvb, offset);
769         tf = proto_tree_add_text(tree, tvb, offset, 2,
770             "Flags: 0x%04X (%s)", flags,
771             ((flags & 0x8000) ?
772               "Hash information is historical" :
773               "Hash information is current"));
774         field_tree = proto_item_add_subtree(tf, ett_flags);
775         proto_tree_add_text(field_tree, tvb, offset, 2, "%s",
776             decode_boolean_bitfield(flags, 0x8000,
777               sizeof (flags)*8,
778               "Hash information is historical",
779               "Hash information is current"));
780         offset += 2;
781
782         bucket_item = proto_tree_add_text(tree, tvb, offset, 8*4,
783             "Hash information");
784         bucket_tree = proto_item_add_subtree(bucket_item, ett_buckets);
785         for (i = 0, n = 0; i < 32; i++) {
786                 bucket_info = tvb_get_guint8(tvb, offset);
787                 n = wccp_bucket_info(bucket_info, bucket_tree, n, tvb, offset);
788                 offset += 1;
789         }
790
791         proto_tree_add_text(tree, tvb, offset, 2,
792             "Assignment Weight: %u", tvb_get_ntohs(tvb, offset));
793         offset += 2;
794
795         proto_tree_add_text(tree, tvb, offset, 2,
796             "Status: 0x%04X", tvb_get_ntohs(tvb, offset));
797         offset += 2;
798
799         return TRUE;
800 }
801
802 static gboolean
803 dissect_wccp2_wc_identity_info(tvbuff_t *tvb, int offset, int length,
804     proto_tree *info_tree)
805 {
806         proto_item *te;
807         proto_tree *element_tree;
808
809         if (length != WC_ID_INFO_LEN) {
810                 proto_tree_add_text(info_tree, tvb, offset, 0,
811                     "Item length is %u, should be %u", length, WC_ID_INFO_LEN);
812                 return TRUE;
813         }
814
815         te = proto_tree_add_text(info_tree, tvb, offset, 4+2+2+32+2+2,
816             "Web-Cache Identity Element: IP address %s",
817             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
818         element_tree = proto_item_add_subtree(te, ett_wc_identity_element);
819         if (!dissect_wccp2_web_cache_identity_element(tvb, offset,
820             element_tree))
821                 return FALSE;   /* ran out of data */
822
823         return TRUE;
824 }
825
826 #define ROUTER_VIEW_INFO_MIN_LEN        (4+8+4)
827
828 static void
829 dissect_wccp2_assignment_key(tvbuff_t *tvb, int offset,
830     proto_tree *info_tree)
831 {
832         proto_tree_add_text(info_tree, tvb, offset, 4,
833             "Assignment Key IP Address: %s",
834             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
835         proto_tree_add_text(info_tree, tvb, offset + 4, 4,
836             "Assignment Key Change Number: %u", tvb_get_ntohl(tvb, offset + 4));
837 }
838
839 static gboolean
840 dissect_wccp2_router_view_info(tvbuff_t *tvb, int offset, int length,
841     proto_tree *info_tree)
842 {
843         guint32 n_routers;
844         guint32 n_web_caches;
845         guint i;
846         proto_item *te;
847         proto_tree *element_tree;
848
849         if (length < ROUTER_VIEW_INFO_MIN_LEN) {
850                 proto_tree_add_text(info_tree, tvb, offset, 0,
851                     "Item length is %u, should be >= %u", length,
852                     ROUTER_VIEW_INFO_MIN_LEN);
853                 return TRUE;
854         }
855
856         proto_tree_add_text(info_tree, tvb, offset, 4,
857             "Member Change Number: %u", tvb_get_ntohl(tvb, offset));
858         offset += 4;
859
860         dissect_wccp2_assignment_key(tvb, offset, info_tree);
861         offset += 8;
862
863         n_routers = tvb_get_ntohl(tvb, offset);
864         proto_tree_add_text(info_tree, tvb, offset, 4,
865             "Number of Routers: %u", n_routers);
866         offset += 4;
867
868         for (i = 0; i < n_routers; i++) {
869                 proto_tree_add_text(info_tree, tvb, offset, 4,
870                     "Router %d IP Address: %s", i,
871                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
872                 offset += 4;
873         }
874
875         n_web_caches = tvb_get_ntohl(tvb, offset);
876         proto_tree_add_text(info_tree, tvb, offset, 4,
877             "Number of Web Caches: %u", n_web_caches);
878         offset += 4;
879
880         for (i = 0; i < n_web_caches; i++) {
881                 te = proto_tree_add_text(info_tree, tvb, offset, WC_ID_INFO_LEN,
882                     "Web-Cache Identity Element %d: IP address %s", i,
883                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
884                 element_tree = proto_item_add_subtree(te,
885                     ett_wc_identity_element);
886                 if (!dissect_wccp2_web_cache_identity_element(tvb,
887                     offset, element_tree))
888                         return FALSE;   /* ran out of data */
889                 offset += WC_ID_INFO_LEN;
890         }
891
892         return TRUE;
893 }
894
895 #define WC_VIEW_INFO_MIN_LEN            (4+4)
896
897 static gboolean
898 dissect_wccp2_wc_view_info(tvbuff_t *tvb, int offset, int length,
899     proto_tree *info_tree)
900 {
901         guint32 n_routers;
902         guint32 n_web_caches;
903         guint i;
904         proto_item *te;
905         proto_tree *element_tree;
906
907         if (length < WC_VIEW_INFO_MIN_LEN) {
908                 proto_tree_add_text(info_tree, tvb, offset, 0,
909                     "Item length is %u, should be >= %u", length,
910                     WC_VIEW_INFO_MIN_LEN);
911                 return TRUE;
912         }
913
914         proto_tree_add_text(info_tree, tvb, offset, 4,
915             "Change Number: %u", tvb_get_ntohl(tvb, offset));
916         offset += 4;
917
918         n_routers = tvb_get_ntohl(tvb, offset);
919         proto_tree_add_text(info_tree, tvb, offset, 4,
920             "Number of Routers: %u", n_routers);
921         offset += 4;
922
923         for (i = 0; i < n_routers; i++) {
924                 te = proto_tree_add_text(info_tree, tvb, offset, 8,
925                     "Router %d Identity Element: IP address %s", i,
926                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
927                 element_tree = proto_item_add_subtree(te,
928                     ett_router_identity_element);
929                 dissect_wccp2_router_identity_element(tvb, offset, element_tree);
930                 offset += 8;
931         }
932
933         n_web_caches = tvb_get_ntohl(tvb, offset);
934         proto_tree_add_text(info_tree, tvb, offset, 4,
935             "Number of Web Caches: %u", n_web_caches);
936         offset += 4;
937
938         for (i = 0; i < n_web_caches; i++) {
939                 proto_tree_add_text(info_tree, tvb, offset, 4,
940                     "Web-Cache %d: IP address %s", i,
941                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
942                 offset += 4;
943         }
944
945         return TRUE;
946 }
947
948 #define ASSIGNMENT_INFO_MIN_LEN         (8+4)
949
950 static void
951 dissect_wccp2_router_assignment_element(tvbuff_t *tvb, int offset,
952     proto_tree *tree)
953 {
954         proto_tree_add_text(tree, tvb, offset, 4,
955             "IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
956         proto_tree_add_text(tree, tvb, offset + 4, 4,
957             "Receive ID: %u", tvb_get_ntohl(tvb, offset + 4));
958         proto_tree_add_text(tree, tvb, offset + 8, 4,
959             "Change Number: %u", tvb_get_ntohl(tvb, offset + 8));
960 }
961
962 static gchar *
963 assignment_bucket_name(guint8 bucket)
964 {
965         gchar *cur=NULL;
966
967         if (bucket == 0xff) {
968                 cur="Unassigned";
969         } else {
970                 cur=ep_alloc(10+1);
971                 g_snprintf(cur, 10+1, "%u%s", bucket >> 1,
972                     (bucket & 0x01) ? " (Alt)" : "");
973         }
974         return cur;
975 }
976
977 static gboolean
978 dissect_wccp2_assignment_info(tvbuff_t *tvb, int offset, int length,
979     proto_tree *info_tree)
980 {
981         guint32 n_routers;
982         guint32 n_web_caches;
983         guint i;
984         proto_item *te;
985         proto_tree *element_tree;
986
987         if (length < ASSIGNMENT_INFO_MIN_LEN) {
988                 proto_tree_add_text(info_tree, tvb, offset, 0,
989                     "Item length is %u, should be >= %u", length,
990                     ASSIGNMENT_INFO_MIN_LEN);
991                 return TRUE;
992         }
993
994         dissect_wccp2_assignment_key(tvb, offset, info_tree);
995         offset += 8;
996
997         n_routers = tvb_get_ntohl(tvb, offset);
998         proto_tree_add_text(info_tree, tvb, offset, 4,
999             "Number of Routers: %u", n_routers);
1000         offset += 4;
1001
1002         for (i = 0; i < n_routers; i++) {
1003                 te = proto_tree_add_text(info_tree, tvb, offset, 4,
1004                     "Router %d Assignment Element: IP address %s", i,
1005                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1006                 element_tree = proto_item_add_subtree(te,
1007                     ett_router_assignment_element);
1008                 dissect_wccp2_router_assignment_element(tvb, offset,
1009                     element_tree);
1010                 offset += 12;
1011         }
1012
1013         n_web_caches = tvb_get_ntohl(tvb, offset);
1014         proto_tree_add_text(info_tree, tvb, offset, 4,
1015             "Number of Web Caches: %u", n_web_caches);
1016         offset += 4;
1017
1018         for (i = 0; i < n_web_caches; i++) {
1019                 proto_tree_add_text(info_tree, tvb, offset, 4,
1020                     "Web-Cache %d: IP address %s", i,
1021                     ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1022                 offset += 4;
1023         }
1024
1025         for (i = 0; i < 256; i += 4) {
1026                 proto_tree_add_text(info_tree, tvb, offset, 4,
1027                     "Buckets %d - %d: %10s %10s %10s %10s",
1028                     i, i + 3,
1029                     assignment_bucket_name(tvb_get_guint8(tvb, offset)),
1030                     assignment_bucket_name(tvb_get_guint8(tvb, offset+1)),
1031                     assignment_bucket_name(tvb_get_guint8(tvb, offset+2)),
1032                     assignment_bucket_name(tvb_get_guint8(tvb, offset+3)));
1033                 offset += 4;
1034         }
1035
1036         return TRUE;
1037 }
1038
1039 #define QUERY_INFO_LEN                  (4+4+4+4)
1040
1041 static gboolean
1042 dissect_wccp2_router_query_info(tvbuff_t *tvb, int offset, int length,
1043     proto_tree *info_tree)
1044 {
1045         if (length != QUERY_INFO_LEN) {
1046                 proto_tree_add_text(info_tree, tvb, offset, 0,
1047                     "Item length is %u, should be %u", length, QUERY_INFO_LEN);
1048                 return TRUE;
1049         }
1050
1051         proto_tree_add_text(info_tree, tvb, offset, 4,
1052             "Router IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1053         offset += 4;
1054
1055         proto_tree_add_text(info_tree, tvb, offset, 4,
1056             "Receive ID: %u", tvb_get_ntohl(tvb, offset));
1057         offset += 4;
1058
1059         proto_tree_add_text(info_tree, tvb, offset, 4,
1060             "Sent To IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1061         offset += 4;
1062
1063         proto_tree_add_text(info_tree, tvb, offset, 4,
1064             "Target IP Address: %s", ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1065         offset += 4;
1066
1067         return TRUE;
1068 }
1069
1070 #define WCCP2_FORWARDING_METHOD         0x01
1071 #define WCCP2_ASSIGNMENT_METHOD         0x02
1072 #define WCCP2_PACKET_RETURN_METHOD      0x03
1073
1074 static const value_string capability_type_vals[] = {
1075         { WCCP2_FORWARDING_METHOD,    "Forwarding Method" },
1076         { WCCP2_ASSIGNMENT_METHOD,    "Assignment Method" },
1077         { WCCP2_PACKET_RETURN_METHOD, "Return Method" },
1078         { 0,                          NULL }
1079 };
1080
1081 #define WCCP2_FORWARDING_METHOD_GRE     0x00000001
1082 #define WCCP2_FORWARDING_METHOD_L2      0x00000002
1083
1084 static const capability_flag forwarding_method_flags[] = {
1085         { WCCP2_FORWARDING_METHOD_GRE,
1086           "IP-GRE", "GRE-encapsulated" },
1087         { WCCP2_FORWARDING_METHOD_L2,
1088           "L2", "L2 rewrite" },
1089         { 0,
1090           NULL, NULL }
1091 };
1092
1093 #define WCCP2_ASSIGNMENT_METHOD_HASH    0x00000001
1094 #define WCCP2_ASSIGNMENT_METHOD_MASK    0x00000002
1095
1096 static const capability_flag assignment_method_flags[] = {
1097         { WCCP2_ASSIGNMENT_METHOD_HASH, "Hash", "Hash" },
1098         { WCCP2_ASSIGNMENT_METHOD_MASK, "Mask", "Mask" },
1099         { 0,                            NULL,   NULL }
1100 };
1101
1102 #define WCCP2_PACKET_RETURN_METHOD_GRE  0x00000001
1103 #define WCCP2_PACKET_RETURN_METHOD_L2   0x00000002
1104
1105 static const capability_flag packet_return_method_flags[] = {
1106         { WCCP2_PACKET_RETURN_METHOD_GRE,
1107           "IP-GRE", "GRE-encapsulated" },
1108         { WCCP2_PACKET_RETURN_METHOD_L2,
1109           "L2", "L2 rewrite" },
1110         { 0,
1111           NULL, NULL }
1112 };
1113
1114 static gboolean
1115 dissect_wccp2_capability_info(tvbuff_t *tvb, int offset, int length,
1116     proto_tree *info_tree)
1117 {
1118         guint16 capability_type;
1119         guint16 capability_val_len;
1120         int curr_offset;
1121         proto_item *te;
1122         proto_tree *element_tree;
1123
1124         for (curr_offset = offset; curr_offset < (length + offset);
1125             curr_offset += (capability_val_len + 4)) {
1126                 capability_type = tvb_get_ntohs(tvb, curr_offset);
1127                 capability_val_len = tvb_get_ntohs(tvb, curr_offset + 2);
1128                 te = proto_tree_add_text(info_tree, tvb, curr_offset,
1129                     capability_val_len + 4, "%s",
1130                     val_to_str(capability_type,
1131                       capability_type_vals, "Unknown Capability Element (0x%08X)"));
1132                 element_tree = proto_item_add_subtree(te,
1133                     ett_capability_element);
1134
1135                 proto_tree_add_text(element_tree, tvb, curr_offset, 2,
1136                     "Type: %s",
1137                     val_to_str(capability_type,
1138                       capability_type_vals, "Unknown (0x%08X)"));
1139
1140                 if (capability_val_len < 4) {
1141                         proto_tree_add_text(element_tree, tvb, curr_offset+2, 2,
1142                             "Value Length: %u (illegal, must be >= 4)",
1143                             capability_val_len);
1144                         break;
1145                 }
1146                 proto_tree_add_text(element_tree, tvb, curr_offset+2, 2,
1147                     "Value Length: %u", capability_val_len);
1148
1149                 switch (capability_type) {
1150
1151                 case WCCP2_FORWARDING_METHOD:
1152                         dissect_32_bit_capability_flags(tvb, curr_offset,
1153                             capability_val_len,
1154                             ett_capability_forwarding_method,
1155                             forwarding_method_flags, element_tree);
1156                         break;
1157
1158                 case WCCP2_ASSIGNMENT_METHOD:
1159                         dissect_32_bit_capability_flags(tvb, curr_offset,
1160                             capability_val_len,
1161                             ett_capability_assignment_method,
1162                             assignment_method_flags, element_tree);
1163                         break;
1164
1165                 case WCCP2_PACKET_RETURN_METHOD:
1166                         dissect_32_bit_capability_flags(tvb, curr_offset,
1167                             capability_val_len,
1168                             ett_capability_return_method,
1169                             packet_return_method_flags, element_tree);
1170                         break;
1171
1172                 default:
1173                         proto_tree_add_text(element_tree, tvb,
1174                             curr_offset+4, capability_val_len,
1175                             "Value: %s",
1176                             tvb_bytes_to_str(tvb, curr_offset+4,
1177                                              capability_val_len));
1178                         break;
1179                 }
1180
1181         }
1182         return TRUE;
1183 }
1184
1185 static void
1186 dissect_32_bit_capability_flags(tvbuff_t *tvb, int curr_offset,
1187     guint16 capability_val_len, gint ett, const capability_flag *flags,
1188     proto_tree *element_tree)
1189 {
1190         guint32 capability_val;
1191         proto_item *tm;
1192         proto_tree *method_tree;
1193         int i;
1194         char *flags_string;
1195         char *p;
1196         size_t returned_length, str_index = 0;
1197         char *buf;
1198
1199         if (capability_val_len != 4) {
1200                 proto_tree_add_text(element_tree, tvb,
1201                     curr_offset+4, capability_val_len,
1202                     "Illegal length (must be 4)");
1203                 return;
1204         }
1205
1206         capability_val = tvb_get_ntohl(tvb, curr_offset + 4);
1207 #define FLAGS_STRING_LEN (128+1)
1208         flags_string=ep_alloc(FLAGS_STRING_LEN);
1209         flags_string[0] = 0;
1210         for (i = 0; flags[i].short_name != NULL; i++) {
1211                 if (capability_val & flags[i].value) {
1212                         if (str_index != 0) {
1213                                 returned_length = g_snprintf(&flags_string[str_index],
1214                                         FLAGS_STRING_LEN-str_index, ",");
1215                                 str_index += MIN(returned_length, FLAGS_STRING_LEN-str_index);
1216                         }
1217                         returned_length = g_snprintf(&flags_string[str_index],
1218                                 FLAGS_STRING_LEN-str_index, "%s", flags[i].short_name);
1219                         str_index += MIN(returned_length, FLAGS_STRING_LEN-str_index);
1220                 }
1221         }
1222         tm = proto_tree_add_text(element_tree, tvb, curr_offset+4, 4,
1223             "Value: 0x%08X (%s)", capability_val, flags_string);
1224
1225         method_tree = proto_item_add_subtree(tm, ett);
1226 #define BUF_SIZE 1024
1227         buf=ep_alloc(BUF_SIZE);
1228         buf[0]=0;
1229         for (i = 0; flags[i].long_name != NULL; i++) {
1230                 p = decode_bitfield_value(buf, capability_val,
1231                       flags[i].value, 32);
1232                 str_index = MIN(p-buf, BUF_SIZE);
1233                 returned_length = g_snprintf(&flags_string[str_index], BUF_SIZE-str_index,
1234                         "%s: %s", flags[i].long_name,
1235                                 (capability_val & flags[i].value)?
1236                                         "Supported":
1237                                         "Not supported"
1238                                 );
1239                 str_index += MIN(returned_length, BUF_SIZE-str_index);
1240                 proto_tree_add_text(method_tree, tvb, curr_offset+4, 4,
1241                     "%s", buf);
1242         }
1243 }
1244
1245 void
1246 proto_register_wccp(void)
1247 {
1248         static hf_register_info hf[] = {
1249                 { &hf_wccp_message_type,
1250                         { "WCCP Message Type", "wccp.message", FT_UINT32, BASE_DEC, VALS(wccp_type_vals), 0x0,
1251                                 "The WCCP message that was sent", HFILL }
1252                 },
1253                 { &hf_wccp_version,
1254                         { "WCCP Version", "wccp.version", FT_UINT32, BASE_HEX, VALS(wccp_version_val), 0x0,
1255                                 "The WCCP version", HFILL }
1256                 },
1257                 { &hf_hash_revision,
1258                         { "Hash Revision", "wccp.hash_revision", FT_UINT32, BASE_DEC, 0x0, 0x0,
1259                                 "The cache hash revision", HFILL }
1260                 },
1261                 { &hf_change_num,
1262                         { "Change Number", "wccp.change_num", FT_UINT32, BASE_DEC, 0x0, 0x0,
1263                                 "The Web-Cache list entry change number", HFILL }
1264                 },
1265                 { &hf_recvd_id,
1266                         { "Received ID", "wccp.recvd_id", FT_UINT32, BASE_DEC, 0x0, 0x0,
1267                                 "The number of I_SEE_YOU's that have been sent", HFILL }
1268                 },
1269                 { &hf_cache_ip,
1270                         { "Web Cache IP address", "wccp.cache_ip", FT_IPv4, BASE_NONE, NULL, 0x0,
1271                                 "The IP address of a Web cache", HFILL }
1272                 },
1273         };
1274         static gint *ett[] = {
1275                 &ett_wccp,
1276                 &ett_cache_count,
1277                 &ett_buckets,
1278                 &ett_flags,
1279                 &ett_cache_info,
1280                 &ett_security_info,
1281                 &ett_service_info,
1282                 &ett_service_flags,
1283                 &ett_router_identity_element,
1284                 &ett_router_identity_info,
1285                 &ett_wc_identity_element,
1286                 &ett_wc_identity_info,
1287                 &ett_router_view_info,
1288                 &ett_wc_view_info,
1289                 &ett_query_info,
1290                 &ett_router_assignment_element,
1291                 &ett_router_assignment_info,
1292                 &ett_capabilities_info,
1293                 &ett_capability_element,
1294                 &ett_capability_forwarding_method,
1295                 &ett_capability_assignment_method,
1296                 &ett_capability_return_method,
1297                 &ett_unknown_info,
1298         };
1299
1300         proto_wccp = proto_register_protocol("Web Cache Coordination Protocol",
1301             "WCCP", "wccp");
1302         proto_register_field_array(proto_wccp, hf, array_length(hf));
1303         proto_register_subtree_array(ett, array_length(ett));
1304 }
1305
1306 void
1307 proto_reg_handoff_wccp(void)
1308 {
1309         dissector_handle_t wccp_handle;
1310
1311         wccp_handle = create_dissector_handle(dissect_wccp, proto_wccp);
1312         dissector_add("udp.port", UDP_PORT_WCCP, wccp_handle);
1313 }