summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-beep.c
blob: e0306a830238f21f79ad06dfa1ae12770568a2bb (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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
/* packet-beep.c
 * Routines for BEEP packet disassembly
 *
 * Copyright (c) 2000 by Richard Sharpe <rsharpe@ns.aus.com>
 * Modified 2001 Darren New <dnew@invisible.net> for BEEP.
 *
 * Original BXXP dissector developed with funding from InvisibleWorlds
 * (www.invisibleworlds.com) via Collab.Net.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs
 * Copyright 1999 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdlib.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/conversation.h>
#include <epan/expert.h>
#include <epan/proto_data.h>
#if defined(DEBUG_BEEP_HASH)
#include <epan/ws_printf.h>
#endif

#define TCP_PORT_BEEP 10288 /* Don't think this is IANA registered */

void proto_register_beep(void);
void proto_reg_handoff_beep(void);

static dissector_handle_t beep_handle;

static range_t *global_beep_tcp_ports;
static bool global_beep_strict_term = true;

static int proto_beep;

static int hf_beep_req;
static int hf_beep_cmd;
static int hf_beep_req_chan;
/* static int hf_beep_rsp_chan; */
static int hf_beep_seq_chan;
/* static int hf_beep_rsp; */
static int hf_beep_more;
static int hf_beep_msgno;
static int hf_beep_ansno;
static int hf_beep_seqno;
static int hf_beep_size;
static int hf_beep_channel;
static int hf_beep_mime_header;
static int hf_beep_header;
#if 0
static int hf_beep_status;
#endif
static int hf_beep_ackno;
static int hf_beep_window;
static int hf_beep_payload;
static int hf_beep_payload_undissected;
static int hf_beep_crlf_terminator;

#if 0
static const value_string beep_status_vals[] = {
   { '+',        "Positive" },
   { '-',        "Negative"  },

   { 0,        NULL   }
};
#endif

static const value_string beep_more_vals[] = {
   { '.',        "Complete" },
   { '*',        "Intermediate"  },

   { 0,        NULL   }
};



/* Arrays of hf entry pointers for some routines to use. If you want more
 * hidden items added for a field, add them to the list before the NULL,
 * and the various routines that these are passed to will add them.
 */

static int *req_msgno_hfa[] = { &hf_beep_msgno, NULL };
static int *req_ansno_hfa[] = { &hf_beep_ansno, NULL };
static int *req_seqno_hfa[]  = { &hf_beep_seqno, NULL };
static int *req_size_hfa[]   = { &hf_beep_size, NULL };
static int *req_chan_hfa[]   = { &hf_beep_channel, &hf_beep_req_chan, NULL };
/*
static int *rsp_msgno_hfa[] = { &hf_beep_msgno, NULL };
static int *rsp_seqno_hfa[]  = { &hf_beep_seqno, NULL };
static int *rsp_size_hfa[]   = { &hf_beep_size, NULL };
*/
static int *seq_chan_hfa[]   = { &hf_beep_channel, &hf_beep_seq_chan, NULL };
static int *seq_ackno_hfa[]  = { &hf_beep_ackno, NULL };
static int *seq_window_hfa[] = { &hf_beep_window, NULL };

static int ett_beep;
static int ett_mime_header;
static int ett_header;
static int ett_trailer;

static expert_field ei_beep_more;
static expert_field ei_beep_cr_terminator;
static expert_field ei_beep_lf_terminator;
static expert_field ei_beep_invalid_terminator;

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

#define BEEP_VIOL         0
#define BEEP_INTERMEDIATE 1
#define BEEP_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 beep_proto_data {
  int pl_left;   /* Payload at beginning of frame */
  int pl_size;   /* Payload in current message ...*/
  int mime_hdr;  /* Whether we expect a mime header. 1 on first, 0 on rest
                  * in a message
                  */
};

/*
 * Conversation stuff
 */

struct beep_request_key {
  uint32_t conversation;
};

struct beep_request_val {
  uint16_t processed;     /* Have we processed this conversation? */
  int size;              /* Size of the message                  */
                         /* We need an indication in each dirn of
                          * whether on not a mime header is expected
                          */
  int c_mime_hdr, s_mime_hdr;
};

static wmem_map_t *beep_request_hash;

/* Hash Functions */
static int
beep_equal(const void *v, const void *w)
{
  const struct beep_request_key *v1 = (const struct beep_request_key *)v;
  const struct beep_request_key *v2 = (const struct beep_request_key *)w;

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

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

  return 0;

}

static unsigned
beep_hash(const void *v)
{
  const struct beep_request_key *key = (const struct beep_request_key *)v;
  unsigned val;

  val = key->conversation;

#if defined(DEBUG_BEEP_HASH)
  ws_debug_printf("BEEP Hash calculated as %u\n", val);
#endif

  return val;

}


/* dissect the more flag, and return a value of:
 *  1 -> more
 *  0 -> no more
 *  -1 -> Proto violation
 */

static int
dissect_beep_more(tvbuff_t *tvb, packet_info *pinfo, int offset,
                  proto_tree *tree)
{
  proto_item *hidden_item;
  int ret = 0;
  uint8_t more = tvb_get_uint8(tvb, offset);

  hidden_item = proto_tree_add_item(tree, hf_beep_more, tvb, offset, 1, ENC_ASCII|ENC_NA);
  proto_item_set_hidden(hidden_item);

  switch(more) {
  case '.':
     ret = 0;
     break;
  case '*':
     ret = 1;
     break;
  default:
    expert_add_info(pinfo, hidden_item, &ei_beep_more);
    ret = -1;
    break;
  }

  return ret;
}

#if 0
static void dissect_beep_status(tvbuff_t *tvb, int offset,
                                proto_tree *tree)
{

  /* FIXME: We should return a value to indicate all OK. */

  proto_tree_add_item(item_tree, hf_beep_status, tvb, offset, 1, ENC_BIG_ENDIAN);
}
#endif

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

  while (g_ascii_isdigit(tvb_get_uint8(tvb, offset + i))) i++;

  return i;

}

/*
 * We check for a terminator. This can be CRLF, which will be recorded
 * as a terminator, or CR or LF by itself, which will be redorded as
 * an incorrect terminator ... We build the tree at this point
 * However, we depend on the variable beep_strict_term
 */

static int
check_term(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_tree *tree)
{
  /* First, check for CRLF, or, if global_beep_strict_term is false,
   * one of CR or LF ... If neither of these hold, we add an element
   * that complains of a protocol violation, and return -1, else
   * we add a terminator to the tree (possibly non-standard) and return
   * the count of characters we saw ... This may throw off the rest of the
   * dissection ... so-be-it!
   */

  if ((tvb_get_uint8(tvb, offset) == 0x0d &&
       tvb_get_uint8(tvb, offset + 1) == 0x0a)){ /* Correct terminator */

    proto_tree_add_item(tree, hf_beep_crlf_terminator, tvb, offset, 2, ENC_NA);
    return 2;

  }

  if ((tvb_get_uint8(tvb, offset) == 0x0d) && !global_beep_strict_term) {

    proto_tree_add_expert(tree, pinfo, &ei_beep_cr_terminator, tvb, offset, 1);
    return 1;

  }

  if ((tvb_get_uint8(tvb, offset) == 0x0a) && !global_beep_strict_term) {

    proto_tree_add_expert(tree, pinfo, &ei_beep_lf_terminator, tvb, offset, 1);
    return 1;
  }

  proto_tree_add_expert_format(tree, pinfo, &ei_beep_invalid_terminator, tvb,
                                offset, 1, "Terminator: %s", tvb_format_text(pinfo->pool, tvb, offset, 2));
  return -1;
}

/* Get the header length, up to CRLF or CR or LF */
static int header_len(tvbuff_t *tvb, int offset)
{
  int i = 0;
  uint8_t sc;

  /* FIXME: Have to make sure we stop looking at the end of the tvb ... */

  /* We look for CRLF, or CR or LF if global_beep_strict_term is
   * not set.
   */

  while (1) {

    if ((sc = tvb_get_uint8(tvb, offset + i)) == 0x0d
        && tvb_get_uint8(tvb, offset + i + 1) == 0x0a)
      return i;   /* Done here ... */

    if (!global_beep_strict_term && (sc == 0x0d || sc == 0x0a))
      return i;   /* Done here also ... */

    i++;

  }
}

static int
dissect_beep_mime_header(tvbuff_t *tvb, packet_info *pinfo, int offset,
                         struct beep_proto_data *beep_frame_data,
                         proto_tree *tree)
{
  proto_tree    *ti = NULL, *mime_tree = NULL;
  int           mime_length = header_len(tvb, offset), cc = 0;

  if (beep_frame_data && !beep_frame_data->mime_hdr) return 0;

  if (tree) {

    /* FIXME: Should calculate the whole length of the mime headers */
    ti = proto_tree_add_item(tree, hf_beep_mime_header, tvb, offset, mime_length, ENC_NA|ENC_ASCII);
    mime_tree = proto_item_add_subtree(ti, ett_mime_header);
  }

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

    if (tree) {
      proto_tree_add_string_format(mime_tree, hf_beep_header, tvb, offset, 0, "", "Default values");
    }

    if ((cc = check_term(tvb, pinfo, offset, mime_tree)) <= 0) {

      /* Ignore it, it will cause funnies in the rest of the dissect */

    }

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

    if (tree) {
      proto_tree_add_item(mime_tree, hf_beep_header, tvb, offset, mime_length, ENC_NA|ENC_ASCII);
    }

    if ((cc = check_term(tvb, pinfo, offset + mime_length, mime_tree)) <= 0) {

      /* Ignore it, it will cause funnies in the rest of the dissect */

    }

  }

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

}

static int
dissect_beep_int(tvbuff_t *tvb, packet_info *pinfo, int offset,
                    proto_tree *tree, int hf, int *val, int *hfa[])
{
  proto_item  *hidden_item;
  int ival, ind = 0;
  unsigned int len = num_len(tvb, offset);

  ival = (int)strtol(tvb_get_string_enc(pinfo->pool, tvb, offset, len, ENC_ASCII), NULL, 10);
  proto_tree_add_uint(tree, hf, tvb, offset, len, ival);

  while (hfa[ind]) {

    hidden_item = proto_tree_add_uint(tree, *hfa[ind], tvb, offset, len, ival);
        proto_item_set_hidden(hidden_item);
    ind++;

  }

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

  return len;

}

static void
set_mime_hdr_flags(int more, struct beep_request_val *request_val,
                   struct beep_proto_data *beep_frame_data, packet_info *pinfo)
{

  if (!request_val) return; /* Nothing to do ??? */

  if (value_is_in_range(global_beep_tcp_ports, pinfo->destport)) { /* Going to the server ... client */

    if (request_val->c_mime_hdr) {

      beep_frame_data->mime_hdr = 0;

      if (!more) request_val->c_mime_hdr = 0;

    }
    else {

      beep_frame_data->mime_hdr = 1;

      if (more) request_val->c_mime_hdr = 1;

    }

  }
  else {

    if (request_val->s_mime_hdr) {

      beep_frame_data->mime_hdr = 0;

      if (!more) request_val->s_mime_hdr = 0;

    }
    else {

      beep_frame_data->mime_hdr = 1;

      if (more) request_val->s_mime_hdr = 1;

    }

  }

}

/* Build the tree
 *
 * A return value of <= 0 says we bailed out, skip the rest of this message,
 * if any.
 *
 * A return value > 0 is the count of bytes we consumed ...
 */

static int
// NOLINTNEXTLINE(misc-no-recursion)
dissect_beep_tree(tvbuff_t *tvb, int offset, packet_info *pinfo,
                  proto_tree *tree, struct beep_request_val *request_val,
                  struct beep_proto_data *beep_frame_data)
{
  proto_tree     *ti = NULL, *hdr = NULL;
  /*proto_item     *hidden_item;*/
  int            st_offset, msgno, ansno, seqno, size, channel, ackno, window, cc,
                 more;

  const char * cmd_temp = NULL;
  int is_ANS = 0;
  st_offset = offset;

  if (tvb_strneql(tvb, offset, "MSG ", 4) == 0)
    cmd_temp = "Command: MSG";
  if (tvb_strneql(tvb, offset, "RPY ", 4) == 0)
    cmd_temp = "Command: RPY";
  if (tvb_strneql(tvb, offset, "ERR ", 4) == 0)
    cmd_temp = "Command: ERR";
  if (tvb_strneql(tvb, offset, "NUL ", 4) == 0)
    cmd_temp = "Command: NUL";
  if (tvb_strneql(tvb, offset, "ANS ", 4) == 0) {
    cmd_temp = "Command: ANS";
    is_ANS = 1;
  }

  if (cmd_temp != NULL) {

    if (tree) {
      hdr = proto_tree_add_subtree(tree, tvb, offset, header_len(tvb, offset) + 2,
            ett_header, NULL, "Header");

      ti = proto_tree_add_item(hdr, hf_beep_cmd, tvb, offset, 3, ENC_NA|ENC_ASCII);
      /* Include space */
      proto_item_set_len(ti, 4);

      proto_tree_add_boolean(hdr, hf_beep_req, tvb, offset, 3, true);
    }

    offset += 4;

    /* Get the channel */
    offset += dissect_beep_int(tvb, pinfo, offset, hdr, hf_beep_channel, &channel, req_chan_hfa);
    offset += 1; /* Skip the space */

    /* Dissect the message number */
    offset += dissect_beep_int(tvb, pinfo, offset, hdr, hf_beep_msgno, &msgno, req_msgno_hfa);
    offset += 1; /* skip the space */

    /* Insert the more elements ... */
    if ((more = dissect_beep_more(tvb, pinfo, offset, hdr)) >= 0) {
      /* Figure out which direction this is in and what mime_hdr flag to
       * add to the beep_frame_data. If there are missing segments, this code
       * will get it wrong!
       */
      set_mime_hdr_flags(more, request_val, beep_frame_data, pinfo);
    }
    else {  /* Protocol violation, so dissect rest as undissectable */
      if (tree && (tvb_reported_length_remaining(tvb, offset) > 0)) {
        proto_tree_add_item(tree, hf_beep_payload_undissected, tvb, offset,
                            tvb_reported_length_remaining(tvb, offset), ENC_NA|ENC_ASCII);
      }
      return -1;
    }

    offset += 2; /* Skip the flag and the space ... */

    /* now for the seqno */
    offset += dissect_beep_int(tvb, pinfo, offset, hdr, hf_beep_seqno, &seqno, req_seqno_hfa);
    offset += 1; /* skip the space */

    offset += dissect_beep_int(tvb, pinfo, offset, hdr, hf_beep_size, &size, req_size_hfa);
    if (request_val)   /* FIXME, is this the right order ... */
      request_val -> size = size;  /* Stash this away */
    else if (beep_frame_data) {
      beep_frame_data->pl_size = size;
      if (beep_frame_data->pl_size < 0) beep_frame_data->pl_size = 0; /* FIXME: OK? */
    }
    /* offset += 1; skip the space */

    if (is_ANS) { /* We need to put in the ansno */
        offset += 1; /* skip the space */
        /* Dissect the message number */
        offset += dissect_beep_int(tvb, pinfo, offset, hdr, hf_beep_ansno, &ansno, req_ansno_hfa);
    }

    if ((cc = check_term(tvb, pinfo, offset, hdr)) <= 0) {

      /* We dissect the rest as data and bail ... */

      if (tree && (tvb_reported_length_remaining(tvb, offset) > 0)) {
        proto_tree_add_item(tree, hf_beep_payload_undissected, tvb, offset,
                            tvb_reported_length_remaining(tvb, offset), ENC_NA|ENC_ASCII);
      }

      return -1;

    }

    offset += cc;

    /* Insert MIME header ... */

    if (beep_frame_data && beep_frame_data->mime_hdr)
      offset += dissect_beep_mime_header(tvb, pinfo, offset, beep_frame_data, hdr);

    /* Now for the payload, if any */

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

      int pl_size = MIN(size, tvb_reported_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
       */

      if (tree) {
        proto_tree_add_item(tree, hf_beep_payload, tvb, offset, pl_size, ENC_NA|ENC_ASCII);
      }

      offset += pl_size;

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

    /* If anything else left, dissect it ... */

    if (tvb_reported_length_remaining(tvb, offset) > 0) {
      increment_dissection_depth(pinfo);
      offset += dissect_beep_tree(tvb, offset, pinfo, tree, request_val, beep_frame_data);
      decrement_dissection_depth(pinfo);
    }
  } else if (tvb_strneql(tvb, offset, "SEQ ", 4) == 0) {

    if (tree) {
      ti = proto_tree_add_item(hdr, hf_beep_cmd, tvb, offset, 3, ENC_NA|ENC_ASCII);
      /* Include space */
      proto_item_set_len(ti, 4);
    }

    offset += 3;

    /* Now check the space: FIXME */

    offset += 1;

    offset += dissect_beep_int(tvb, pinfo, offset, tree, hf_beep_channel, &channel, seq_chan_hfa);

    /* Check the space: FIXME */

    offset += 1;

    offset += dissect_beep_int(tvb, pinfo, offset, tree, hf_beep_ackno, &ackno, seq_ackno_hfa);

    /* Check the space: FIXME */

    offset += 1;

    offset += dissect_beep_int(tvb, pinfo, offset, tree, hf_beep_window, &window, seq_window_hfa);

    if ((cc = check_term(tvb, pinfo, offset, tree)) <= 0) {

      /* We dissect the rest as data and bail ... */

      if (tree && (tvb_reported_length_remaining(tvb, offset) > 0)) {
        proto_tree_add_item(tree, hf_beep_payload_undissected, tvb, offset,
                            tvb_reported_length_remaining(tvb, offset), ENC_NA|ENC_ASCII);
      }

      return -1;

    }

    offset += cc;

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

    proto_tree *tr = NULL;

    if (tree) {
      tr = proto_tree_add_subtree(tree, tvb, offset, MIN(5, MAX(0, tvb_reported_length_remaining(tvb, offset))),
                                    ett_trailer, NULL, "Trailer");

      proto_tree_add_item(hdr, hf_beep_cmd, tvb, offset, 3, ENC_NA|ENC_ASCII);
    }

    offset += 3;

    if ((cc = check_term(tvb, pinfo, offset, tr)) <= 0) {

      /* We dissect the rest as data and bail ... */

      if (tree && (tvb_reported_length_remaining(tvb, offset) > 0)) {
        proto_tree_add_item(tree, hf_beep_payload_undissected, tvb, offset,
                            tvb_reported_length_remaining(tvb, offset), ENC_NA|ENC_ASCII);
      }

      return -1;

    }

    offset += cc;

  }

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

    int pl_size = 0;

    if (request_val) {

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

      if (pl_size == 0) { /* The whole of the rest must be payload */

        pl_size = tvb_reported_length_remaining(tvb, offset); /* Right place ? */

      }

    } else if (beep_frame_data) {
      pl_size = MIN(beep_frame_data->pl_size, tvb_reported_length_remaining(tvb, offset));
    } else { /* Just in case */
      pl_size = tvb_reported_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 == st_offset)
      pl_size = tvb_reported_length_remaining(tvb, offset);

    if (pl_size > 0) {

      if (tree) {
        proto_tree_add_item(tree, hf_beep_payload, tvb, offset, pl_size, ENC_NA|ENC_ASCII);
      }

      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 if (beep_frame_data) {
        beep_frame_data->pl_size -= pl_size;
        if (beep_frame_data->pl_size < 0) beep_frame_data->pl_size = 0;
      }
    }

    if (tvb_reported_length_remaining(tvb, offset) > 0) {
      offset += dissect_beep_tree(tvb, offset, pinfo, tree, request_val, beep_frame_data);
    }
  }

  return offset - st_offset;

}

static int
dissect_beep(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  int offset;
  struct beep_proto_data  *beep_frame_data;
  proto_tree              *beep_tree = NULL, *ti = NULL;
  conversation_t          *conversation = NULL;
  struct beep_request_key request_key, *new_request_key;
  struct beep_request_val *request_val = NULL;

  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 BEEP, 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.
   */

  beep_frame_data = (struct beep_proto_data *)p_get_proto_data(wmem_file_scope(), pinfo, proto_beep, 0);

  if (!beep_frame_data) {

      conversation = find_or_create_conversation(pinfo);

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

      request_val = (struct beep_request_val *)wmem_map_lookup(beep_request_hash, &request_key);

      if (!request_val) { /* Create one */

        new_request_key = wmem_new(wmem_file_scope(), struct beep_request_key);
        new_request_key->conversation = conversation->conv_index;

        request_val = wmem_new0(wmem_file_scope(), struct beep_request_val);

        wmem_map_insert(beep_request_hash, new_request_key, request_val);

      }
    }

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "BEEP");


  /* "tvb_format_text()" is passed a value that won't go past the end
   * of the packet, so it won't throw an exception.
   */
  if (tvb_reported_length_remaining(tvb, offset) > 0)
    col_add_str(pinfo->cinfo, COL_INFO, tvb_format_text(pinfo->pool, tvb, offset, tvb_reported_length_remaining(tvb, offset)));

  /* Here, we parse the message so we can retrieve the info we need, which
   * is that there is some payload left from a previous segment on the
   * front of this segment ... This all depends on TCP segments not getting
   * out of order ...
   *
   * As a huge kludge, we push the checking for the tree down into the code
   * and process as if we were given a tree but not call the routines that
   * adorn the protocol tree if they were NULL.
   */

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

    ti = proto_tree_add_item(tree, proto_beep, tvb, offset, -1, ENC_NA);

    beep_tree = proto_item_add_subtree(ti, ett_beep);

  }

  /* Check the per-frame data and the conversation for any left-over
   * payload from the previous frame
   *
   * We check that per-frame data exists first, and if so, use it,
   * else we use the conversation data.
   *
   * We create per-frame data here as well, but we must ensure we create it
   * after we have done the check for per-frame or conversation data.
   *
   * We also depend on the first frame in a group having a pl_size of 0.
   */

  if (beep_frame_data != NULL && beep_frame_data->pl_left > 0) {

    int pl_left = beep_frame_data->pl_left;

    pl_left = MIN(pl_left, MAX(0, tvb_reported_length_remaining(tvb, offset)));

    /* Add the payload bit, only if we have a tree */
    if (tree && (pl_left > 0)) {
      proto_tree_add_item(tree, hf_beep_payload, tvb, offset, pl_left, ENC_NA|ENC_ASCII);
    }
    offset += pl_left;
  }
  else if (request_val && request_val->size > 0) {

    int pl_left = request_val->size;

    request_val->size = 0;

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

    beep_frame_data = wmem_new(wmem_file_scope(), struct beep_proto_data);

    beep_frame_data->pl_left = pl_left;
    beep_frame_data->pl_size = 0;
    beep_frame_data->mime_hdr = 0;

    p_add_proto_data(wmem_file_scope(), pinfo, proto_beep, 0, beep_frame_data);

  }

  /* Set up the per-frame data here if not already done so
   * This _must_ come after the checks above ...
   */

  if (beep_frame_data == NULL) {

    beep_frame_data = wmem_new(wmem_file_scope(), struct beep_proto_data);

    beep_frame_data->pl_left = 0;
    beep_frame_data->pl_size = 0;
    beep_frame_data->mime_hdr = 0;

    p_add_proto_data(wmem_file_scope(), pinfo, proto_beep, 0, beep_frame_data);

  }

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

    /*offset += */dissect_beep_tree(tvb, offset, pinfo, beep_tree, request_val, beep_frame_data);

  }

  return tvb_captured_length(tvb);
}

static void
apply_beep_prefs(void)
{
  /* Beep uses the port preference to determine client/server */
  global_beep_tcp_ports = prefs_get_range_value("beep", "tcp.port");
}

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

void
proto_register_beep(void)
{
  static hf_register_info hf[] = {
    { &hf_beep_req,
      { "Request", "beep.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_cmd,
      { "Command", "beep.command", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_req_chan,
      { "Request Channel Number", "beep.req.channel", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

#if 0
    { &hf_beep_rsp,
      { "Response", "beep.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_rsp_chan,
      { "Response Channel Number", "beep.rsp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
#endif

    { &hf_beep_seq_chan,
      { "Sequence Channel Number", "beep.seq.channel", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_more,
      { "More", "beep.more", FT_CHAR, BASE_HEX, VALS(beep_more_vals), 0x0, NULL, HFILL }},

    { &hf_beep_msgno,
      { "Msgno", "beep.msgno", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_ansno,
      { "Ansno", "beep.ansno", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_seqno,
      { "Seqno", "beep.seqno", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_size,
      { "Size", "beep.size", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_channel,
      { "Channel", "beep.channel", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_mime_header,
      { "Mime header", "beep.mime_header", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_header,
      { "Header", "beep.header", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},

#if 0
    { &hf_beep_status,
      { "Status", "beep.status", FT_UINT8, BASE_HEX, VALS(beep_status_vals), 0x0, NULL, HFILL }},
#endif

    { &hf_beep_ackno,
      { "Ackno", "beep.seq.ackno", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_window,
      { "Window", "beep.seq.window", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_payload,
      { "Payload", "beep.payload", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_payload_undissected,
      { "Undissected Payload", "beep.payload_undissected", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},

    { &hf_beep_crlf_terminator,
      { "Terminator: CRLF", "beep.crlf_terminator", FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }},
  };
  static int *ett[] = {
    &ett_beep,
    &ett_mime_header,
    &ett_header,
    &ett_trailer,
  };
  static ei_register_info ei[] = {
     { &ei_beep_more, { "beep.more.expected", PI_PROTOCOL, PI_WARN, "Expected More Flag (* or .)", EXPFILL }},
     { &ei_beep_cr_terminator, { "beep.cr_terminator", PI_PROTOCOL, PI_WARN, "Nonstandard Terminator: CR", EXPFILL }},
     { &ei_beep_lf_terminator, { "beep.lf_terminator", PI_PROTOCOL, PI_WARN, "Nonstandard Terminator: LF", EXPFILL }},
     { &ei_beep_invalid_terminator, { "beep.invalid_terminator", PI_PROTOCOL, PI_WARN, "Invalid Terminator", EXPFILL }},
  };

  module_t *beep_module;
  expert_module_t* expert_beep;

  proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
                                       "BEEP", "beep");

  proto_register_field_array(proto_beep, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_beep = expert_register_protocol(proto_beep);
  expert_register_field_array(expert_beep, ei, array_length(ei));

  beep_request_hash = wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(), beep_hash, beep_equal);

  /* Register our configuration options for BEEP, particularly our port */

  beep_module = prefs_register_protocol(proto_beep, apply_beep_prefs);
  /* For reading older preference files with "bxxp." preferences */
  prefs_register_module_alias("bxxp", beep_module);

  prefs_register_bool_preference(beep_module, "strict_header_terminator",
                                 "BEEP Header Requires CRLF",
                                 "Specifies that BEEP requires CRLF as a "
                                 "terminator, and not just CR or LF",
                                 &global_beep_strict_term);

  beep_handle = register_dissector("beep", dissect_beep, proto_beep);
}

/* The registration hand-off routine */
void
proto_reg_handoff_beep(void)
{
  dissector_add_uint_with_preference("tcp.port", TCP_PORT_BEEP, beep_handle);

  apply_beep_prefs();
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */