aboutsummaryrefslogtreecommitdiffstats
path: root/packet-bxxp.c
blob: d5e4c5b3b240d0277a1894e89878f2aa0e35c705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
/* packet-bxxp.c
 * Routines for BXXP packet disassembly
 *
 * $Id: packet-bxxp.c,v 1.3 2000/09/12 02:24:19 sharpe Exp $
 *
 * Copyright (c) 2000 by Richard Sharpe <rsharpe@ns.aus.com>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs
 * Copyright 1999 Gerald Combs
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <glib.h>
#include <string.h>
#include "packet.h"
#include "resolv.h"
#include "prefs.h"
#include "conversation.h"

#define TCP_PORT_BXXP 10288
void proto_reg_handoff_bxxp(void);

static int proto_bxxp = -1;

static int hf_bxxp_req = -1;
static int hf_bxxp_rsp = -1;
static int hf_bxxp_seq = -1;
static int hf_bxxp_end = -1;    /* Do we need this one?        */
static int hf_bxxp_complete = -1;   /* More data follows */
static int hf_bxxp_intermediate = -1; /* No More data follows */
static int hf_bxxp_serial = -1;
static int hf_bxxp_seqno = -1;
static int hf_bxxp_size = -1;
static int hf_bxxp_channel = -1;
static int hf_bxxp_positive = -1;
static int hf_bxxp_negative = -1;
static int hf_bxxp_ackno = -1;
static int hf_bxxp_window = -1;

static int ett_bxxp = -1;
static int ett_mime_header = -1;
static int ett_header = -1;
static int ett_trailer = -1;

static int tcp_port = 0;

/* Get the state of the more flag ... */

#define BXXP_VIOL         0
#define BXXP_INTERMEDIATE 1
#define BXXP_COMPLETE     2

/*
 * Per-frame data
 *
 * pl_left is the amount of data in this packet that belongs to another
 * frame ...
 * 
 * It relies on TCP segments not being re-ordered too much ...
 */
struct bxxp_proto_data {
  int pl_left;   /* Payload at beginning of frame */
  int pl_size;   /* Payload in current message ...*/
};

/*
 * Conversation stuff
 */
static int bxxp_packet_init_count = 100;

struct bxxp_request_key {
  guint32 conversation;
};

struct bxxp_request_val {
  guint16 processed;     /* Have we processed this conversation? */
  int size;              /* Size of the message                  */
};

GHashTable *bxxp_request_hash = NULL;
GMemChunk  *bxxp_request_keys = NULL;
GMemChunk  *bxxp_request_vals = NULL;
GMemChunk  *bxxp_packet_infos = NULL;

/* Hash Functions */
gint
bxxp_equal(gconstpointer v, gconstpointer w)
{
  struct bxxp_request_key *v1 = (struct bxxp_request_key *)v;
  struct bxxp_request_key *v2 = (struct bxxp_request_key *)w;

#if defined(DEBUG_BXXP_HASH)
  printf("Comparing %08X\n      and %08X\n",
	 v1->conversation, v2->conversation);
#endif

  if (v1->conversation == v2->conversation)
    return 1;

  return 0;

}

static guint
bxxp_hash(gconstpointer v)
{
  struct bxxp_request_key *key = (struct bxxp_request_key *)v;
  guint val;

  val = key->conversation;

#if defined(DEBUG_BXXP_HASH)
  printf("BXXP Hash calculated as %u\n", val);
#endif

  return val;

}

static void
bxxp_init_protocol(void)
{
#if defined(DEBUG_BXXP_HASH)
  fprintf(stderr, "Initializing BXXP hashtable area\n");
#endif

  if (bxxp_request_hash)
    g_hash_table_destroy(bxxp_request_hash);
  if (bxxp_request_keys)
    g_mem_chunk_destroy(bxxp_request_keys);
  if (bxxp_request_vals)
    g_mem_chunk_destroy(bxxp_request_vals);
  if (bxxp_packet_infos)
    g_mem_chunk_destroy(bxxp_packet_infos);

  bxxp_request_hash = g_hash_table_new(bxxp_hash, bxxp_equal);
  bxxp_request_keys = g_mem_chunk_new("bxxp_request_keys",
				       sizeof(struct bxxp_request_key),
				       bxxp_packet_init_count * sizeof(struct bxxp_request_key), G_ALLOC_AND_FREE);
  bxxp_request_vals = g_mem_chunk_new("bxxp_request_vals", 
				      sizeof(struct bxxp_request_val),
				      bxxp_packet_init_count * sizeof(struct bxxp_request_val), G_ALLOC_AND_FREE);
  bxxp_packet_infos = g_mem_chunk_new("bxxp_packet_infos",
				      sizeof(struct bxxp_proto_data),
				      bxxp_packet_init_count * sizeof(struct bxxp_proto_data), G_ALLOC_AND_FREE);
}

/*
 * BXXP routines
 */

int bxxp_get_more(char more)
{

  if (more == '.')
    return BXXP_COMPLETE;
  else if (more == '*')
    return BXXP_INTERMEDIATE;

  return BXXP_VIOL;
}

void
dissect_bxxp_more(tvbuff_t *tvb, int offset, frame_data *fd, 
		  proto_tree *tree)
{

  switch (bxxp_get_more(tvb_get_guint8(tvb, offset))) {

  case BXXP_COMPLETE:

    proto_tree_add_boolean_hidden(tree, hf_bxxp_complete, tvb, offset, 1, TRUE);
    proto_tree_add_text(tree, tvb, offset, 1, "More: Complete");

    break;

  case BXXP_INTERMEDIATE:
	
    proto_tree_add_boolean_hidden(tree, hf_bxxp_intermediate, tvb, offset, 1, TRUE);
    proto_tree_add_text(tree, tvb, offset, 1, "More: Intermediate");

    break;

  default:  /* FIXME: Add code for this case ... */

    fprintf(stderr, "Error from bxxp_get_more ...\n");
    break;
  }

}

void dissect_bxxp_status(tvbuff_t *tvb, int offset, frame_data *fd,
			 proto_tree *tree)
{
  
  switch(tvb_get_guint8(tvb, offset)) {

  case '+':
  
    proto_tree_add_boolean_hidden(tree, hf_bxxp_positive, tvb, offset, 1, TRUE);
    proto_tree_add_text(tree, tvb, offset, 1, "Status: Positive");

    break;

  case '-':

    proto_tree_add_boolean_hidden(tree, hf_bxxp_negative, tvb, offset, 1, TRUE);
    proto_tree_add_text(tree, tvb, offset, 1, "Status: Negative");

    break;

  default:  /* Proto violation: FIXME */

    break;

  }

}

int num_len(tvbuff_t *tvb, int offset)
{
  int i = 0;

  while (isdigit(tvb_get_guint8(tvb, offset + i))) i++;

  return i;

}

/* Get the MIME header length */
int header_len(tvbuff_t *tvb, int offset)
{
  int i = 0;

  while (tvb_get_guint8(tvb, offset + i) != 0x0d 
	 && tvb_get_guint8(tvb, offset + i + 1) != 0x0a) i++;

  return i;

}

int
dissect_bxxp_mime_header(tvbuff_t *tvb, int offset, frame_data *fd,
			 proto_tree *tree)
{
  proto_tree    *ti, *mime_tree;
  int           mime_length = header_len(tvb, offset);

  ti = proto_tree_add_text(tree, tvb, offset, mime_length + 2, "Mime header: %s", tvb_format_text(tvb, offset, mime_length + 2));
  mime_tree = proto_item_add_subtree(ti, ett_mime_header);

  if (mime_length == 0) { /* Default header */

    proto_tree_add_text(mime_tree, tvb, offset, 2, "Default values");

  }
  else {  /* FIXME: Process the headers */


  }

  return mime_length + 2;  /* FIXME: Check that the CRLF is there */

}

int
dissect_bxxp_int(tvbuff_t *tvb, int offset, frame_data *fd,
		    proto_tree *tree, int hf, int *val)
{
  int ival, i = num_len(tvb, offset);
  guint8 int_buff[100];

  memset(int_buff, '\0', sizeof(int_buff));

  tvb_memcpy(tvb, int_buff, offset, MIN(sizeof(int_buff), i));

  sscanf(int_buff, "%d", &ival);  /* FIXME: Dangerous */

  proto_tree_add_uint(tree, hf, tvb, offset, i, ival);

  *val = ival;  /* Return the value */

  return i;

}

int 
check_crlf(tvbuff_t *tvb, int offset)
{

  return (tvb_get_guint8(tvb, offset) == 0x0d
	  && tvb_get_guint8(tvb, offset + 1) == 0x0a);

}

static int global_bxxp_tcp_port = TCP_PORT_BXXP;

/* Build the tree */

int
dissect_bxxp_tree(tvbuff_t *tvb, int offset, packet_info *pinfo, 
		  proto_tree *tree, struct bxxp_request_val *request_val, 
		  struct bxxp_proto_data *frame_data)
{
  proto_tree     *ti, *hdr;
  int            st_offset, serial, seqno, size, channel, ackno, window;

  st_offset = offset;

  if (tvb_strneql(tvb, offset, "REQ ", 4) == 0) {

    ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");

    hdr = proto_item_add_subtree(ti, ett_header);

    proto_tree_add_boolean_hidden(hdr, hf_bxxp_req, tvb, offset, 3, TRUE);
    proto_tree_add_text(hdr, NullTVB, offset, 3, "Command: REQ");

    offset += 3;

#if 0
    if (tvb_get_guint8(tvb, offset) != ' ') { /* Protocol violation */

      /* Hmm, FIXME ... Add some code here ... */

    }
#endif

    offset += 1;  /* Skip the space */

    /* Insert the more elements ... */

    dissect_bxxp_more(tvb, offset, pinfo->fd, hdr);
    offset += 1;
      
    /* Check the space ... */

    offset += 1;

    /* Dissect the serial */

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial);
    /* skip the space */

    offset += 1;

    /* now for the seqno */

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno);

    /* skip the space */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size);
    if (request_val)
      request_val -> size = size;  /* Stash this away */
    else {
      frame_data->pl_size = size;
      if (frame_data->pl_size < 0) frame_data->pl_size = 0;
    }

    /* Check the space */

    offset += 1;

    /* Get the channel */

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_channel, &channel);
      
    if (check_crlf(tvb, offset)) {

      proto_tree_add_text(hdr, tvb, offset, 2, "Terminator: CRLF");

    }
    else {  /* Protocol violation */

      /* FIXME: implement */

    }

    offset += 2;
    
    /* Insert MIME header ... */

    offset += dissect_bxxp_mime_header(tvb, offset, pinfo->fd, hdr);

    /* Now for the payload, if any */

    if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */

      int pl_size = MIN(size, tvb_length_remaining(tvb, offset));

      /* Except, check the payload length, and only dissect that much */

      /* We need to keep track, in the conversation, of how much is left 
       * so in the next packet, we can figure out what is part of the payload
       * and what is the next message
       */

      proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));

      offset += pl_size;

      if (request_val) {
	request_val->size -= pl_size;
	if (request_val->size < 0) request_val->size = 0;
      }
      else {
	frame_data->pl_size -= pl_size;
	if (frame_data->pl_size < 0) frame_data->pl_size = 0;
      }
    }
      
    /* If anything else left, dissect it ... As what? */

    if (tvb_length_remaining(tvb, offset) > 0)
      offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);

  } else if (tvb_strneql(tvb, offset, "RSP ", 4) == 0) {

    /* FIXME: Fix the header length */

    ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");

    hdr = proto_item_add_subtree(ti, ett_header);

    proto_tree_add_boolean_hidden(hdr, hf_bxxp_rsp, NullTVB, offset, 3, TRUE);
    proto_tree_add_text(hdr, tvb, offset, 3, "Command: RSP");

    offset += 3;

    /* Now check the space: FIXME */

    offset += 1;

    /* Next, the 'more' flag ... */

    dissect_bxxp_more(tvb, offset, pinfo->fd, hdr);
    offset += 1;

    /* Check the space */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial);
    /* skip the space */

    offset += 1;

    /* now for the seqno */

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno);

    /* skip the space */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size);
    if (request_val)
      request_val->size = size;
    else
      frame_data->pl_size = size;

    /* Check the space ... */

    offset += 1;

    dissect_bxxp_status(tvb, offset, pinfo->fd, hdr);

    offset += 1;

    if (check_crlf(tvb, offset)) {

      proto_tree_add_text(hdr, tvb, offset, 2, "Terminator: CRLF");

    }
    else {  /* Protocol violation */

      /* FIXME: implement */

    }

    offset += 2;
    
    /* Insert MIME header ... */

    offset += dissect_bxxp_mime_header(tvb, offset, pinfo->fd, hdr);

    /* Now for the payload, if any */

    if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */

      int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
      
      /* Except, check the payload length, and only dissect that much */

      proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));

      offset += pl_size;

      if (request_val) {
	request_val->size -= pl_size;
	if (request_val->size < 0) request_val->size = 0;
      }
      else {
	frame_data->pl_size -= pl_size;
	if (frame_data->pl_size < 0) frame_data->pl_size = 0;
      }
    }

    /* If anything else left, dissect it ... As what? */

    if (tvb_length_remaining(tvb, offset) > 0)
      offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);

  } else if (tvb_strneql(tvb, offset, "SEQ ", 4) == 0) {

    proto_tree_add_boolean_hidden(tree, hf_bxxp_seq, NullTVB, offset, 3, TRUE);
    proto_tree_add_text(tree, tvb, offset, 3, "Command: SEQ");
      
    offset += 3;

    /* Now check the space: FIXME */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_channel, &channel);

    /* Check the space: FIXME */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_ackno, &ackno);

    /* Check the space: FIXME */

    offset += 1;

    offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_window, &window);

    if (check_crlf(tvb, offset)) {

      proto_tree_add_text(tree, tvb, offset, 2, "Terminator: CRLF");

    }
    else {  /* Protocol violation */

      /* FIXME: implement */

    }

    offset += 2;

  } else if (tvb_strneql(tvb, offset, "END", 3) == 0) {

    proto_tree *tr;

    ti = proto_tree_add_text(tree, tvb, offset, MIN(5, tvb_length_remaining(tvb, offset)), "Trailer");

    tr = proto_item_add_subtree(ti, ett_trailer);

    proto_tree_add_boolean_hidden(tr, hf_bxxp_end, NullTVB, offset, 3, TRUE);
    proto_tree_add_text(tr, tvb, offset, 3, "Command: END");

    offset += 3;

    if (check_crlf(tvb, offset)) {

      proto_tree_add_text(tr, tvb, offset, 2, "Terminator: CRLF");

    }
    else {  /* Protocol violation */

      /* FIXME: implement */

    }

    offset += 2;

  }

  if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect anything left over */

    int pl_size = 0;

    if (request_val) {

      pl_size = MIN(request_val->size, tvb_length_remaining(tvb, offset));

      if (pl_size == 0) { /* The whole of the rest must be payload */
      
	pl_size = tvb_length_remaining(tvb, offset); /* Right place ? */
      
      }

    } else if (frame_data) {
      pl_size = MIN(frame_data->pl_size, tvb_length_remaining(tvb, offset));
    } else { /* Just in case */
      pl_size = tvb_length_remaining(tvb, offset);
    }

    /* Take care here to handle the payload correctly, and if there is 
     * another message here, then handle it correctly as well.
     */

    /* If the pl_size == 0 and the offset == 0, then we have not processed
     * anything in this frame above, so we better treat all this data as 
     * payload to avoid recursion loops
     */

    if (pl_size == 0 && offset == 0)
      pl_size = tvb_length_remaining(tvb, offset);

    if (pl_size > 0) {

      proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s",
			  tvb_format_text(tvb, offset, pl_size));

      offset += pl_size;            /* Advance past the payload */

      if (request_val){
	request_val->size -= pl_size; /* Reduce payload by what we added */
	if (request_val->size < 0) request_val->size = 0;
      }
      else {
	frame_data->pl_size -= pl_size;
	if (frame_data->pl_size < 0) frame_data->pl_size = 0;
      }
    }

    if (tvb_length_remaining(tvb, offset) > 0) {
      offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
    }
  }

  return offset - st_offset;

}

static void
dissect_bxxp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
  int offset;
  struct bxxp_proto_data  *frame_data = NULL;
  proto_tree              *bxxp_tree, *ti;
  conversation_t          *conversation = NULL;
  struct bxxp_request_key request_key, *new_request_key;
  struct bxxp_request_val *request_val = NULL;

  CHECK_DISPLAY_AS_DATA(proto_bxxp, tvb, pinfo, tree);

  offset = 0;

  /* If we have per frame data, use that, else, we must have lost the per-
   * frame data, and we have to do a full dissect pass again.
   *
   * The per-frame data tells us how much of this frame is left over from a
   * previous frame, so we dissect it as payload and then try to dissect the
   * rest.
   * 
   * We use the conversation to build up info on the first pass over the
   * packets of type BXXP, and record anything that is needed if the user
   * does random dissects of packets in per packet data.
   *
   * Once we have per-packet data, we don't need the conversation stuff 
   * anymore, but if per-packet data and conversation stuff gets deleted, as 
   * it does under some circumstances when a rescan is done, it all gets 
   * rebuilt.
   */

  /* Find out what conversation this packet is part of ... but only
   * if we have no information on this packet, so find the per-frame 
   * info first.
   */

  frame_data = p_get_proto_data(pinfo->fd, proto_bxxp);

  if (!frame_data) {

    conversation = find_conversation(&pinfo->src, &pinfo->dst, pi.ptype,
				       pinfo->srcport, pinfo->destport);
    if (conversation == NULL) { /* No conversation, create one */
	conversation = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
					pinfo->srcport, pinfo->destport, NULL);

      }

      /* 
       * Check for and insert an entry in the request table if does not exist
       */
      request_key.conversation = conversation->index;

      request_val = (struct bxxp_request_val *)g_hash_table_lookup(bxxp_request_hash, &request_key);
      
      if (!request_val) { /* Create one */

	new_request_key = g_mem_chunk_alloc(bxxp_request_keys);
	new_request_key->conversation = conversation->index;

	request_val = g_mem_chunk_alloc(bxxp_request_vals);
	request_val->processed = 0;
	request_val->size = 0;

	g_hash_table_insert(bxxp_request_hash, new_request_key, request_val);

      }
    }

  if (check_col(pinfo->fd, COL_PROTOCOL))
    col_add_str(pinfo->fd, COL_PROTOCOL, "BXXP");

  if (check_col(pinfo->fd, COL_INFO)) {  /* Check the type ... */

    col_add_fstr(pinfo->fd, COL_INFO, "%s", tvb_format_text(tvb, offset, tvb_length_remaining(tvb, offset)));

  }

  if (tree) {  /* Build the tree info ... */

    /* Check the per-frame data and the conversation for any left-over 
     * payload from the previous frame 
     */

    ti = proto_tree_add_item(tree, proto_bxxp, tvb, offset, tvb_length(tvb), FALSE);

    bxxp_tree = proto_item_add_subtree(ti, ett_bxxp);

    /* FIXME: This conditional is not correct */
    if ((frame_data && frame_data->pl_left > 0) ||
	(request_val && request_val->size > 0)) {
      int pl_left = 0;

      if (frame_data) {
	pl_left = frame_data->pl_left;
      }
      else {
	pl_left = request_val->size;

	request_val->size = 0;

	/* We create the frame data here for this case, and 
	 * elsewhere for other frames
	 */

	frame_data = g_mem_chunk_alloc(bxxp_packet_infos);

	frame_data->pl_left = pl_left;
	frame_data->pl_size = 0;

	p_add_proto_data(pinfo->fd, proto_bxxp, frame_data);

      }

      pl_left = MIN(pl_left, tvb_length_remaining(tvb, offset));

      /* Add the payload bit */
      proto_tree_add_text(bxxp_tree, tvb, offset, pl_left, "Payload: %s",
			  tvb_format_text(tvb, offset, pl_left));
      offset += pl_left;
    }

    if (tvb_length_remaining(tvb, offset) > 0) {

      offset += dissect_bxxp_tree(tvb, offset, pinfo, bxxp_tree, request_val, frame_data);

    }

    /* Set up the per-frame data here if not already done so */

    if (frame_data == NULL) { 

      frame_data = g_mem_chunk_alloc(bxxp_packet_infos);

      frame_data->pl_left = 0;
      frame_data->pl_size = 0;

      p_add_proto_data(pinfo->fd, proto_bxxp, frame_data);
	
    }

  }

}

/* Register all the bits needed with the filtering engine */

void 
proto_register_bxxp(void)
{
  static hf_register_info hf[] = {
    { &hf_bxxp_req,
      { "Request", "bxxp.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_rsp,
      { "Response", "bxxp.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_seq,
      { "Sequence", "bxxp.seq", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_end,
      { "End", "bxxp.end", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_complete,
      { "Complete", "bxxp.more.complete", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_intermediate,
      { "Intermediate", "bxxp.more.intermediate", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},

    { &hf_bxxp_serial,
      { "Serial", "bxxp.serial", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},

    { &hf_bxxp_seqno,
      { "Seqno", "bxxp.seqno", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},

    { &hf_bxxp_size,
      { "Size", "bxxp.size", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},

    { &hf_bxxp_channel,
      { "Channel", "bxxp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},

    { &hf_bxxp_negative,
      { "Negative", "bxxp.status.negative", FT_BOOLEAN, BASE_NONE, NULL, 0x0, ""}},

    { &hf_bxxp_positive,
      { "Positive", "bxxp.status.positive", FT_BOOLEAN, BASE_NONE, NULL, 0x0, ""}},

    { &hf_bxxp_ackno,
      { "Ackno", "bxxp.seq.ackno", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},

    { &hf_bxxp_window,
      { "Window", "bxxp.seq.window", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},

  };
  static gint *ett[] = {
    &ett_bxxp,
    &ett_mime_header,
    &ett_header,
    &ett_trailer,
  };
  module_t *bxxp_module; 

  /* Register our configuration options for BXXP, particularly out port */

  bxxp_module = prefs_register_module("bxxp", "BXXP", proto_reg_handoff_bxxp);

  prefs_register_uint_preference(bxxp_module, "tcp.port", "BXXP TCP Port",
				 "Set the port for BXXP messages (if other"
				 " than the default of 10288)",
				 10, &global_bxxp_tcp_port);

  proto_bxxp = proto_register_protocol("Blocks eXtensible eXchange Protocol",
				       "bxxp");

  proto_register_field_array(proto_bxxp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  register_init_routine(&bxxp_init_protocol);

}

/* The registration hand-off routine */
void
proto_reg_handoff_bxxp(void)
{
  static int bxxp_prefs_initialized = FALSE;

  if (bxxp_prefs_initialized) {

    dissector_delete("tcp.port", tcp_port, dissect_bxxp);

  }
  else {

    bxxp_prefs_initialized = TRUE;

  }

  /* Set our port number for future use */

  tcp_port = global_bxxp_tcp_port;

  dissector_add("tcp.port", global_bxxp_tcp_port, dissect_bxxp);

}