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