NLPID's of 0x08 and 0x09 should be labeled as Q.933 and LMI,
[obnox/wireshark/wip.git] / packet-wcp.c
1 /* packet-wcp.c
2  * Routines for Wellfleet Compression frame disassembly
3  * Copyright 2001, Jeffrey C. Foster <jfoste@woodward.com>
4  *
5  * $Id: packet-wcp.c,v 1.8 2001/03/30 10:51:50 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * ToDo:
27  *      Add preference to allow/disallow decompression
28  *      Calculate and verify check byte (last byte), if only we knew how!
29  *      Handle Wellfleet compression over PPP links.
30  *              - This will require changing the sub-dissector call
31  *                routine to determine if layer 2 is frame relay or
32  *                or PPP and different sub-dissector routines for each.
33  *
34  * Based upon information in the Nortel TCL based Pcaptap code.
35  *http://www.mynetworkforum.com/tools/PCAPTAP/pcaptap-Win32-3.00.exe
36  *
37  * And lzss algorithm
38  *http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzss.html
39  */
40
41 /*
42  * Wellfleet compression is a variation on LZSS encoding.
43  *
44  * Compression is done by keeping a sliding window of previous 
45  * data transmited. The sender will use a pattern match to 
46  * encode repeated data as a data pointer field. Then a stream
47  * of pointers and actual data bytes. The pointer values include
48  * an offset to previous data in the stream and the length of the
49  *  matching data.
50  *
51  * The data pattern matching is done on the octects. 
52  *
53  * The data is encoded as 8 field blocks with a compression flag
54  * byte at the beginning.  If the bit is set in the compression
55  * flag, then that field has a compression field. If it isn't set
56  * then the byte is raw data.
57  *
58  * The compression field is either 2 or 3 bytes long. The length
59  * is determined by the length of the matching data, for short
60  * matches the match length is encoded in the high nibble of the
61  * first byte. Otherwise the third byte of the field contains 
62  * the match length.
63  *
64  * First byte -
65  * lower 4 bits:
66  *              High order nibble of the offset
67  *
68  * upper 4 bits:
69  *              1   = length is in 3rd byte 
70  *              2-F = length of matching data - 1
71  *              
72  * Second byte - 
73  *  Lower byte of the source offset.
74  *
75  * Third byte - 
76  *  Length of match - 1 if First byte upper nibble = 1, otherwise
77  *  this byte isn't added to data stream.
78  *
79  * Example:
80  *      Uncompressed data (hex):  11 22 22 22 22 33 44 55 66 77
81  *
82  *
83  *      Compression data :
84  *                      Flag bits:      0x20 (third field is compressed)
85  *                      Data:   11 22 20 00 33 44 55
86  *                              /  /  /  /
87  *              raw data ------+--+  /  /               
88  *              (Comp length - 1)<<4+  /
89  *              Data offset ----------+
90  *
91  *      Output data (hex):  20 11 22 20 00 33 44 55 66 77
92  *
93  * In this example the copy src is one byte behind the copy destination
94  * so if appears as if output is being loaded with the source byte.
95  *
96  */
97
98
99
100 #ifdef HAVE_CONFIG_H
101 # include "config.h"
102 #endif
103
104 #ifdef HAVE_SYS_TYPES_H
105 # include <sys/types.h>
106 #endif
107
108 #include <stdio.h>
109 #include <glib.h>
110 #include <string.h>
111 #include "packet.h"
112 #include "packet-frame.h"
113 #include "packet-fr.h"
114 #include "conversation.h"
115 #include "etypes.h"
116 #include "nlpid.h"
117
118 #define MAX_WIN_BUF_LEN 0x7fff          /* storage size for decompressed data */
119 #define MAX_WCP_BUF_LEN 2048            /* storage size for decompressed data */
120 #define FROM_DCE        0x80            /* for direction setting */
121
122 typedef struct {
123
124         guint8  *buf_cur;
125         guint8  buffer[MAX_WIN_BUF_LEN];
126
127 }wcp_window_t;
128
129 /*XXX do I really want the length in here  */
130 typedef struct {
131
132         guint16  len;
133         guint8  buffer[MAX_WCP_BUF_LEN];
134
135 }wcp_pdata_t;
136
137
138 #define wcp_win_init_count 4
139 #define wcp_packet_init_count 10
140
141 #define wcp_win_length (sizeof(wcp_window_t))
142 #define wcp_packet_length (sizeof(wcp_pdata_t))
143
144 static GMemChunk *wcp_window = NULL;
145 static GMemChunk *wcp_pdata = NULL;
146
147 extern dissector_table_t fr_subdissector_table;
148
149 static int proto_wcp = -1;
150 static int hf_wcp_cmd = -1;
151 static int hf_wcp_ext_cmd = -1;
152 static int hf_wcp_seq = -1;
153 static int hf_wcp_chksum = -1;
154 static int hf_wcp_tid = -1;
155 static int hf_wcp_rev = -1;
156 static int hf_wcp_init = -1;
157 static int hf_wcp_seq_size = -1;
158 static int hf_wcp_alg = -1;
159 static int hf_wcp_alg_cnt = -1;
160 static int hf_wcp_alg_a = -1;
161 static int hf_wcp_alg_b = -1;
162 static int hf_wcp_alg_c = -1;
163 static int hf_wcp_alg_d = -1;
164 static int hf_wcp_rexmit = -1;
165
166 static int hf_wcp_hist_size = -1;
167 static int hf_wcp_ppc = -1;
168 static int hf_wcp_pib = -1;
169
170 static int hf_wcp_comp_bits = -1;
171 static int hf_wcp_comp_marker = -1;
172 static int hf_wcp_short_len = -1;
173 static int hf_wcp_long_len = -1;
174 static int hf_wcp_short_run = -1;
175 static int hf_wcp_long_run = -1;
176 static int hf_wcp_offset = -1;
177
178 static gint ett_wcp = -1;
179 static gint ett_wcp_field = -1;
180
181 /*
182  * Bits in the address field.
183  */
184 #define WCP_CMD                 0xf0    /* WCP Command */
185 #define WCP_EXT_CMD             0x0f    /* WCP Extended Command */
186 #define WCP_SEQ                 0x0fff  /* WCP Sequence number */
187 #define WCP_OFFSET_MASK         0x0fff  /* WCP Pattern source offset */
188
189 #define PPC_COMPRESSED_IND              0x0
190 #define PPC_UNCOMPRESSED_IND            0x1
191 #define PPC_TPPC_COMPRESSED_IND         0x2
192 #define PPC_TPPC_UNCOMPRESSED_IND       0x3
193 #define CONNECT_REQ                     0x4
194 #define CONNECT_ACK                     0x5
195 #define CONNECT_NAK                     0x6
196 #define DISCONNECT_REQ                  0x7
197 #define DISCONNECT_ACK                  0x8
198 #define INIT_REQ                        0x9
199 #define INIT_ACK                        0xa
200 #define RESET_REQ                       0xb
201 #define RESET_ACK                       0xc
202 #define REXMIT_NAK                      0xd
203
204
205 static const value_string cmd_string[] = {
206         {0, "Compressed Data"},
207         {1, "Uncompressed Data"},
208         {15, "Extended"},
209         { 0,       NULL }
210         };
211
212 static const value_string ext_cmd_string[] = {
213         {0, "Per Packet Compression"},
214         {4, "Connect Req"},
215         {5, "Connect Ack"},
216         {9, "Init Req"},
217         {0x0a, "Init Ack"},
218
219         { 0,       NULL }
220         };
221
222
223
224 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree);
225 static wcp_window_t *get_wcp_window_ptr( packet_info *pinfo);
226
227 static void
228 dissect_wcp_con_req(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree) {
229
230 /* WCP connector request message */
231
232         guint alg_cnt = tvb_get_guint8(tvb, 5);
233
234         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
235         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
236         proto_tree_add_uint(tree, hf_wcp_init, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
237         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
238         proto_tree_add_uint(tree, hf_wcp_alg_cnt, tvb, offset + 5, 1, alg_cnt);
239         proto_tree_add_uint(tree, hf_wcp_alg_a, tvb, offset + 6, 1, tvb_get_guint8(tvb, offset + 6));
240         if ( alg_cnt > 1)
241                 proto_tree_add_uint(tree, hf_wcp_alg_b, tvb, offset + 7, 1, tvb_get_guint8(tvb, offset + 7));
242         if ( alg_cnt > 2)
243                 proto_tree_add_uint(tree, hf_wcp_alg_c, tvb, offset + 8, 1, tvb_get_guint8(tvb, offset + 8));
244         if ( alg_cnt > 3)
245                 proto_tree_add_uint(tree, hf_wcp_alg_d, tvb, offset + 9, 1, tvb_get_guint8(tvb, offset + 9));
246 }
247
248 static void
249 dissect_wcp_con_ack( tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree){
250
251 /* WCP connector ack message */
252
253         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
254         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
255         proto_tree_add_uint(tree, hf_wcp_seq_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
256         proto_tree_add_uint(tree, hf_wcp_alg, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
257 }
258
259 static void
260 dissect_wcp_init( tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree){
261
262 /* WCP Initiate Request/Ack message */
263
264         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
265         proto_tree_add_uint(tree, hf_wcp_rev, tvb, offset + 2, 1, tvb_get_guint8(tvb, offset + 2));
266         proto_tree_add_uint(tree, hf_wcp_hist_size, tvb, offset + 3, 1, tvb_get_guint8(tvb, offset + 3));
267         proto_tree_add_uint(tree, hf_wcp_ppc, tvb, offset + 4, 1, tvb_get_guint8(tvb, offset + 4));
268         proto_tree_add_uint(tree, hf_wcp_pib, tvb, offset + 5, 1, tvb_get_guint8(tvb, offset + 5));
269 }
270
271
272 static void
273 dissect_wcp_reset( tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree){
274
275 /* Process WCP Reset Request/Ack message */
276
277         proto_tree_add_uint(tree, hf_wcp_tid, tvb, offset, 2, tvb_get_ntohs(tvb, offset));
278 }
279
280
281 void wcp_save_data( tvbuff_t *tvb, packet_info *pinfo){
282
283         wcp_window_t *buf_ptr = 0;
284         int len; 
285
286         /* discard first 2 bytes, header and last byte (check byte) */
287         len = tvb_reported_length( tvb)-3;
288         buf_ptr = get_wcp_window_ptr( pinfo); 
289
290         if (( buf_ptr->buf_cur + len) <= (buf_ptr->buffer + MAX_WIN_BUF_LEN)){
291                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, len);
292                 buf_ptr->buf_cur = buf_ptr->buf_cur + len;
293         
294         } else {
295                 guint8 *buf_end = buf_ptr->buffer + MAX_WIN_BUF_LEN; 
296                 tvb_memcpy( tvb, buf_ptr->buf_cur, 2, buf_end - buf_ptr->buf_cur);
297                 tvb_memcpy( tvb, buf_ptr->buffer, buf_end - buf_ptr->buf_cur-2,
298                         len - (int)(buf_end - buf_ptr->buf_cur));
299                 buf_ptr->buf_cur = buf_ptr->buf_cur + len - MAX_WIN_BUF_LEN;
300         }
301
302 }
303
304
305 void dissect_wcp( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
306
307         proto_tree      *wcp_tree;
308         proto_item      *ti;
309         int             wcp_header_len;
310         guint16         temp, cmd, ext_cmd, seq;
311         tvbuff_t        *next_tvb;
312
313         pinfo->current_proto = "WCP";
314
315         if (check_col(pinfo->fd, COL_PROTOCOL))
316                 col_set_str(pinfo->fd, COL_PROTOCOL, "WCP");
317         if (check_col(pinfo->fd, COL_INFO))
318                 col_clear(pinfo->fd, COL_INFO);
319
320         temp =tvb_get_ntohs(tvb, 0); 
321
322         cmd = (temp & 0xf000) >> 12;
323         ext_cmd = (temp & 0x0f00) >> 8;
324
325         if ( cmd == 0xf)
326                 wcp_header_len= 1;
327         else 
328                 wcp_header_len= 2;
329
330         seq = temp & 0x0fff;
331
332 /*XXX should test seq to be sure it the last + 1 !! */
333
334         if (check_col(pinfo->fd, COL_INFO)){
335                 col_add_str(pinfo->fd, COL_INFO, val_to_str(cmd, cmd_string, "Unknown"));
336                 if ( cmd == 0xf)
337                         col_append_fstr(pinfo->fd, COL_INFO, ", %s",
338                                 val_to_str(ext_cmd, ext_cmd_string, "Unknown"));
339         }       
340
341         if (tree) {
342                 ti = proto_tree_add_item(tree, proto_wcp, tvb, 0, wcp_header_len, FALSE);
343
344                 wcp_tree = proto_item_add_subtree(ti, ett_wcp);
345
346                 proto_tree_add_uint(wcp_tree, hf_wcp_cmd, tvb, 0, 1, tvb_get_guint8( tvb, 0));
347                 if ( cmd == 0xf){
348                         proto_tree_add_uint(wcp_tree, hf_wcp_ext_cmd, tvb, 1, 1,
349                                         tvb_get_guint8( tvb, 0));
350                         switch (ext_cmd){
351                         case CONNECT_REQ:
352                                 dissect_wcp_con_req( tvb, 1, pinfo, wcp_tree);
353                                 break;
354
355                         case CONNECT_ACK:
356                                 dissect_wcp_con_ack( tvb, 1, pinfo, wcp_tree);
357                                 break;
358                         case INIT_REQ:
359                         case INIT_ACK:
360                                 dissect_wcp_init( tvb, 1, pinfo, wcp_tree);
361                                 break;
362                         case RESET_REQ:
363                         case RESET_ACK:
364                                 dissect_wcp_reset( tvb, 1, pinfo, wcp_tree);
365                                 break;
366                         default:
367                                 break;
368                         }
369                 }else { 
370                         proto_tree_add_uint(wcp_tree, hf_wcp_seq,  tvb, 0, 2, seq);
371                 }
372         }
373         else {
374                 wcp_tree = NULL;
375         }
376
377
378                                         /* exit if done */
379         if ( cmd != 1 && cmd != 0 && !(cmd == 0xf && ext_cmd == 0)) 
380                 return;
381
382         if ( cmd == 1) {                /* uncompressed data */
383                 if ( !pinfo->fd->flags.visited){        /* if first pass */
384                         wcp_save_data( tvb, pinfo);
385                 }
386                 next_tvb = tvb_new_subset(tvb, wcp_header_len, -1, -1);
387         }
388         else {          /* cmd == 0 || (cmd == 0xf && ext_cmd == 0) */
389
390                 next_tvb = wcp_uncompress( tvb, wcp_header_len, pinfo, wcp_tree);
391
392                 if ( !next_tvb){
393                         proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
394                                         "[Malformed Frame: Bad WCP compressed data]" );
395                         return;
396                 }
397         }
398
399         if ( tree)                      /* add the check byte */
400                 proto_tree_add_uint(wcp_tree, hf_wcp_chksum, tvb,
401                         tvb_reported_length( tvb)-1, 1,
402                         tvb_get_guint8( tvb, tvb_reported_length(tvb)-1));
403
404         dissect_fr_uncompressed(next_tvb, pinfo, tree);
405
406         return;
407 }
408
409
410 guint8 *decompressed_entry( guint8 *src, guint8 *dst, int *len, guint8 * buf_start, guint8 *buf_end){
411
412 /* do the decompression for one field */
413
414         guint16 data_offset, data_cnt;
415         guint8 tmp = *src;
416
417         data_offset = (*(src++) & 0xf) << 8;    /* get high byte */
418         data_offset += *(src++);                /* add next byte */
419
420         if (( tmp & 0xf0) == 0x10){             /* 2 byte count */
421                 data_cnt = *src;
422                 data_cnt++;
423
424         }else {                                 /* one byte count */
425                 data_cnt = tmp >> 4;
426                 data_cnt++;
427         }               
428
429         
430         src = (dst - 1 - data_offset);
431         if ( src < buf_start)
432                 src += MAX_WIN_BUF_LEN;
433
434
435 /*XXX could do some fancy memory moves, later if speed is problem */
436
437         while( data_cnt--){
438                 *dst = *src;
439                 if ( ++(*len) >MAX_WCP_BUF_LEN){
440                         printf("decomp failed, len = %d\n",  *len);
441
442                         return NULL;    /* end of buffer error */
443                 }
444                 if ( dst++ == buf_end)
445                         dst = buf_start;
446                 if ( src++ == buf_end)
447                         src = buf_start;
448
449         }
450         return dst;
451 }
452
453
454 static 
455 wcp_window_t *get_wcp_window_ptr( packet_info *pinfo){
456
457 /* find the conversation for this side of the DLCI, create one if needed */
458 /* and return the wcp_window data structure pointer */
459
460         conversation_t *conv = find_conversation( &pinfo->dl_src, &pinfo->dl_src, PT_NONE, 
461                 ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 
462                 ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 0);
463
464         if ( !conv){
465
466                 conv = conversation_new( &pinfo->dl_src, &pinfo->dl_src, PT_NONE, 
467                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0), 
468                         ((pinfo->pseudo_header->x25.flags & FROM_DCE)? 1:0),
469                         g_mem_chunk_alloc( wcp_window), 0);
470                         
471                 ((wcp_window_t*)conv->data)->buf_cur = ((wcp_window_t*)conv->data)->buffer;
472         }
473
474         return (wcp_window_t*)conv->data;
475 }
476
477
478 static tvbuff_t *wcp_uncompress( tvbuff_t *src_tvb, int offset, packet_info *pinfo, proto_tree *tree) {
479
480 /* do the packet data uncompression and load it into the dst buffer */
481
482         proto_tree      *sub_tree;
483         proto_item      *ti;
484
485         int len=0, i = -1;
486         int cnt = tvb_reported_length( src_tvb)-1;      /* don't include check byte */
487
488         guint8 *buf = 0, *dst, *src, *buf_start, *buf_end,  *tmp, comp_flag_bits = 0;
489         guint8 src_buf[ MAX_WCP_BUF_LEN];
490         tvbuff_t *volatile tvb = 0;
491         wcp_window_t *buf_ptr = 0;
492         wcp_pdata_t *volatile pdata_ptr;
493
494         buf_ptr = get_wcp_window_ptr( pinfo); 
495
496         buf_start = buf_ptr->buffer;
497         buf_end = buf_start + MAX_WIN_BUF_LEN;
498         tmp = buf_ptr->buf_cur;
499
500         src = tvb_memcpy(src_tvb, src_buf, offset, cnt - offset);
501         dst = buf_ptr->buf_cur;
502
503         while( offset++ < cnt){
504
505                 if ( --i >= 0){
506                         if ( comp_flag_bits & 0x80){    /* if this is a compressed entry */
507
508                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
509                                         dst = decompressed_entry( src, dst, &len, buf_start, buf_end);
510                                 }
511                                 if ((*src & 0xf0) == 0x10){
512                                         if ( tree) {
513                                                 ti = proto_tree_add_item( tree, hf_wcp_long_run, src_tvb,
514                                                          offset-1, 3, 0);
515                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
516                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
517                                                          offset-1, 2, pntohs(src));
518
519                                                 proto_tree_add_item( sub_tree, hf_wcp_long_len, src_tvb,
520                                                          offset+1, 1, pntohs(src));
521                                         }
522                                         src += 3;
523                                         offset += 2;
524                                 }else{
525                                         if ( tree) {
526                                                 ti = proto_tree_add_item( tree, hf_wcp_short_run, src_tvb,
527                                                          offset - 1, 2, *src);
528                                                 sub_tree = proto_item_add_subtree(ti, ett_wcp_field);
529                                                 proto_tree_add_item( sub_tree, hf_wcp_short_len, src_tvb,
530                                                          offset-1, 1, *src);
531                                                 proto_tree_add_uint(sub_tree, hf_wcp_offset, src_tvb,
532                                                          offset-1, 2, pntohs(src));
533                                         }
534                                         src += 2;
535                                         offset += 1;
536                                 }
537                         }else {
538                                 if ( !pinfo->fd->flags.visited){        /* if first pass */
539                                         *dst = *src;
540                                         if ( dst++ == buf_end)
541                                                 dst = buf_start;
542                                 }
543                                 ++src;
544                                 ++len;
545
546                         }
547
548                         if ( len >MAX_WCP_BUF_LEN){
549                                 return NULL;
550                         }
551
552                         comp_flag_bits <<= 1;
553
554                 }else { /* compressed data flag */
555
556                         comp_flag_bits = *src++;
557                         if (tree)
558                                 proto_tree_add_uint( tree, hf_wcp_comp_bits,  src_tvb, offset-1, 1,
559                                         comp_flag_bits);
560                                 
561                         i = 8;
562                 }
563         }
564
565         if ( pinfo->fd->flags.visited){ /* if not first pass */
566                                         /* get uncompressed data */
567                 pdata_ptr = p_get_proto_data( pinfo->fd, proto_wcp);
568
569                 if ( !pdata_ptr)        /* exit if no data */
570                         return NULL;
571                 len = pdata_ptr->len;   
572         } else {
573
574         /* save the new data as per packet data */
575                 pdata_ptr = g_mem_chunk_alloc( wcp_pdata);
576                 memcpy( &pdata_ptr->buffer, buf_ptr->buf_cur,  len);
577                 pdata_ptr->len = len;
578
579                 p_add_proto_data( pinfo->fd, proto_wcp, (void*)pdata_ptr);      
580
581                 buf_ptr->buf_cur = dst;
582         }
583
584
585         TRY {
586                 tvb = tvb_new_real_data( pdata_ptr->buffer, pdata_ptr->len, pdata_ptr->len, "uncompressed");
587
588         }
589         CATCH(BoundsError) {
590                 g_assert_not_reached();
591                 g_free(buf);
592                 return NULL;
593         }
594         CATCH(ReportedBoundsError) {
595                 g_free(buf);
596                return NULL;
597         }
598         ENDTRY; 
599
600         /* link new tvbuff into tvbuff chain so cleanup is done later */
601         tvb_set_child_real_data_tvbuff( src_tvb, tvb);
602
603         /* Add new data to the data source list */
604         pinfo->fd->data_src = g_slist_append( pinfo->fd->data_src, tvb);
605         return tvb;
606
607 }
608
609
610 static void wcp_reinit( void){
611
612 /* Do the cleanup work when a new pass through the packet list is       */
613 /* performed. re-initialize the  memory chunks.                         */
614
615         if (wcp_window)
616                 g_mem_chunk_destroy(wcp_window);
617
618         wcp_window = g_mem_chunk_new("wcp_window", wcp_win_length,
619                 wcp_win_init_count * wcp_win_length,
620                 G_ALLOC_AND_FREE);
621
622         if (wcp_pdata)
623                 g_mem_chunk_destroy(wcp_pdata);
624
625         wcp_pdata = g_mem_chunk_new("wcp_pdata", wcp_packet_length,
626                 wcp_packet_init_count * wcp_packet_length,
627                 G_ALLOC_AND_FREE);
628
629 }
630
631
632 void
633 proto_register_wcp(void)
634 {
635     static hf_register_info hf[] = {
636         { &hf_wcp_cmd,
637           { "Command", "wcp.cmd", FT_UINT8, BASE_HEX, VALS(cmd_string), WCP_CMD, 
638                 "Compression Command" }},
639         { &hf_wcp_ext_cmd,
640           { "Extended Command", "wcp.ext_cmd", FT_UINT8, BASE_HEX, VALS(ext_cmd_string), WCP_EXT_CMD, 
641                 "Extended Compression Command" }},
642         { &hf_wcp_seq,
643           { "SEQ", "wcp.seq", FT_UINT16, BASE_HEX, NULL, WCP_SEQ, 
644                 "Sequence Number" }},
645         { &hf_wcp_chksum,
646           { "Checksum", "wcp.checksum", FT_UINT8, BASE_DEC, NULL, 0,
647                 "Packet Checksum" }},
648         { &hf_wcp_tid,
649           { "TID", "wcp.tid", FT_UINT16, BASE_DEC, NULL, 0,
650                 "TID" }},
651         { &hf_wcp_rev,
652           { "Revision", "wcp.rev", FT_UINT8, BASE_DEC, NULL, 0,
653                 "Revision" }},
654         { &hf_wcp_init,
655           { "Initiator", "wcp.init", FT_UINT8, BASE_DEC, NULL, 0,
656                 "Initiator" }},
657         { &hf_wcp_seq_size,
658           { "Seq Size", "wcp.seq_size", FT_UINT8, BASE_DEC, NULL, 0,
659                 "Sequence Size"}},
660         { &hf_wcp_alg_cnt,
661           { "Alg Count", "wcp.alg_cnt", FT_UINT8, BASE_DEC, NULL, 0,
662                 "Algorithm Count"}},
663         { &hf_wcp_alg_a,
664           { "Alg 1", "wcp.alg1", FT_UINT8, BASE_DEC, NULL, 0,
665                 "Algorithm #1"}},
666         { &hf_wcp_alg_b,
667           { "Alg 2", "wcp.alg2", FT_UINT8, BASE_DEC, NULL, 0,
668                 "Algorithm #2"}},
669         { &hf_wcp_alg_c,
670           { "Alg 3", "wcp.alg3", FT_UINT8, BASE_DEC, NULL, 0,
671                 "Algorithm #3"}},
672         { &hf_wcp_alg_d,
673           { "Alg 4", "wcp.alg4", FT_UINT8, BASE_DEC, NULL, 0,
674                 "Algorithm #4"}},
675         { &hf_wcp_alg,
676           { "Alg", "wcp.alg", FT_UINT8, BASE_DEC, NULL, 0,
677                 "Algorithm"}},
678         { &hf_wcp_rexmit,
679           { "Rexmit", "wcp.rexmit", FT_UINT8, BASE_DEC, NULL, 0,
680                 "Retransmit"}},
681         { &hf_wcp_hist_size,
682           { "History", "wcp.hist", FT_UINT8, BASE_DEC, NULL, 0,
683                 "History Size"}},
684         { &hf_wcp_ppc,
685           { "PerPackComp", "wcp.ppc", FT_UINT8, BASE_DEC, NULL, 0,
686                 "Per Packet Compression"}},
687         { &hf_wcp_pib,
688           { "PIB", "wcp.pib", FT_UINT8, BASE_DEC, NULL, 0,
689                 "PIB"}},
690         { &hf_wcp_comp_bits,
691           { "Compress Flag", "wcp.flag", FT_UINT8, BASE_HEX, NULL, 0,
692                 "Compressed byte flag"}},
693         { &hf_wcp_comp_marker,
694           { "Compress Marker", "wcp.mark", FT_UINT8, BASE_BIN, NULL, 0,
695                 "Compressed marker"}},
696         { &hf_wcp_offset,
697           { "Source offset", "wcp.off", FT_UINT16, BASE_HEX, NULL, WCP_OFFSET_MASK,
698                 "Data source offset"}},
699         { &hf_wcp_short_len,
700           { "Compress Length", "wcp.short_len", FT_UINT8, BASE_HEX, NULL, 0xf0,
701                 "Compressed length"}},
702         { &hf_wcp_long_len,
703           { "Compress Length", "wcp.long_len", FT_UINT8, BASE_HEX, NULL, 0,
704                 "Compressed length"}},
705         { &hf_wcp_long_run,
706           { "Long Compression", "wcp.long_comp", FT_UINT16, BASE_HEX, NULL, 0,
707                 "Long Compression type"}},
708         { &hf_wcp_short_run,
709           { "Short Compression", "wcp.short_comp", FT_UINT8, BASE_HEX, NULL, 0,
710                 "Short Compression type"}},
711
712    };
713
714
715     static gint *ett[] = {
716         &ett_wcp,
717         &ett_wcp_field,
718     };
719
720     proto_wcp = proto_register_protocol ("Wellfleet Compression", "WCP", "wcp");
721     proto_register_field_array (proto_wcp, hf, array_length(hf));
722     proto_register_subtree_array(ett, array_length(ett));
723     register_init_routine(&wcp_reinit);
724 }
725
726
727 void
728 proto_reg_handoff_wcp(void) {
729
730     dissector_add("fr.ietf", NLPID_COMPRESSED, dissect_wcp, proto_wcp);
731     dissector_add("ethertype",  ETHERTYPE_WCP, dissect_wcp, proto_wcp);
732 }