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