Remove a bunch of duplicate semicolons.
[obnox/wireshark/wip.git] / packet-beep.c
1 /* packet-beep.c
2  * Routines for BEEP packet disassembly
3  *
4  * $Id: packet-beep.c,v 1.13 2003/07/25 04:17:36 gram Exp $
5  *
6  * Copyright (c) 2000 by Richard Sharpe <rsharpe@ns.aus.com>
7  * Modified 2001 Darren New <dnew@invisible.net> for BEEP.
8  *
9  * Original BXXP dissector developed with funding from InvisibleWorlds
10  * (www.invisibleworlds.com) via Collab.Net.
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs
14  * Copyright 1999 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <time.h>
39 #include <glib.h>
40 #include <string.h>
41 #include <epan/packet.h>
42 #include <epan/resolv.h>
43 #include "prefs.h"
44 #include <epan/conversation.h>
45
46 #define TCP_PORT_BEEP 10288
47 void proto_reg_handoff_beep(void);
48
49
50 static int global_beep_tcp_port = TCP_PORT_BEEP;
51 static int global_beep_strict_term = TRUE;
52
53 static int proto_beep = -1;
54
55 static int hf_beep_req = -1;
56 static int hf_beep_req_chan = -1;
57 static int hf_beep_rsp_chan = -1;
58 static int hf_beep_seq_chan = -1;
59 static int hf_beep_rsp = -1;
60 static int hf_beep_seq = -1;
61 static int hf_beep_end = -1;
62 static int hf_beep_proto_viol = -1;
63 static int hf_beep_complete = -1;   /* No More data follows */
64 static int hf_beep_intermediate = -1; /* More data follows */
65 static int hf_beep_msgno = -1;
66 static int hf_beep_ansno = -1;
67 static int hf_beep_seqno = -1;
68 static int hf_beep_size = -1;
69 static int hf_beep_channel = -1;
70 static int hf_beep_positive = -1;
71 static int hf_beep_negative = -1;
72 static int hf_beep_ackno = -1;
73 static int hf_beep_window = -1;
74
75 /* Arrays of hf entry pointers for some routines to use. If you want more
76  * hidden items added for a field, add them to the list before the NULL,
77  * and the various routines that these are passed to will add them.
78  */
79
80 static int *req_msgno_hfa[] = { &hf_beep_msgno, NULL };
81 static int *req_ansno_hfa[] = { &hf_beep_ansno, NULL };
82 static int *req_seqno_hfa[]  = { &hf_beep_seqno, NULL };
83 static int *req_size_hfa[]   = { &hf_beep_size, NULL };
84 static int *req_chan_hfa[]   = { &hf_beep_channel, &hf_beep_req_chan, NULL };
85 /*
86 static int *rsp_msgno_hfa[] = { &hf_beep_msgno, NULL };
87 static int *rsp_seqno_hfa[]  = { &hf_beep_seqno, NULL };
88 static int *rsp_size_hfa[]   = { &hf_beep_size, NULL };
89 */
90 static int *seq_chan_hfa[]   = { &hf_beep_channel, &hf_beep_seq_chan, NULL };
91 static int *seq_ackno_hfa[]  = { &hf_beep_ackno, NULL };
92 static int *seq_window_hfa[] = { &hf_beep_window, NULL };
93
94 static int ett_beep = -1;
95 static int ett_mime_header = -1;
96 static int ett_header = -1;
97 static int ett_trailer = -1;
98
99 static unsigned int tcp_port = 0;
100
101 /* Get the state of the more flag ... */
102
103 #define BEEP_VIOL         0
104 #define BEEP_INTERMEDIATE 1
105 #define BEEP_COMPLETE     2
106
107 /*
108  * Per-frame data
109  *
110  * pl_left is the amount of data in this packet that belongs to another
111  * frame ...
112  *
113  * It relies on TCP segments not being re-ordered too much ...
114  */
115 struct beep_proto_data {
116   int pl_left;   /* Payload at beginning of frame */
117   int pl_size;   /* Payload in current message ...*/
118   int mime_hdr;  /* Whether we expect a mime header. 1 on first, 0 on rest
119                   * in a message
120                   */
121 };
122
123 /*
124  * Conversation stuff
125  */
126 static int beep_packet_init_count = 100;
127
128 struct beep_request_key {
129   guint32 conversation;
130 };
131
132 struct beep_request_val {
133   guint16 processed;     /* Have we processed this conversation? */
134   int size;              /* Size of the message                  */
135                          /* We need an indication in each dirn of
136                           * whether on not a mime header is expected
137                           */
138   int c_mime_hdr, s_mime_hdr;
139 };
140
141 static GHashTable *beep_request_hash = NULL;
142 static GMemChunk  *beep_request_keys = NULL;
143 static GMemChunk  *beep_request_vals = NULL;
144 static GMemChunk  *beep_packet_infos = NULL;
145
146 /* Hash Functions */
147 static gint
148 beep_equal(gconstpointer v, gconstpointer w)
149 {
150   const struct beep_request_key *v1 = (const struct beep_request_key *)v;
151   const struct beep_request_key *v2 = (const struct beep_request_key *)w;
152
153 #if defined(DEBUG_BEEP_HASH)
154   printf("Comparing %08X\n      and %08X\n",
155          v1->conversation, v2->conversation);
156 #endif
157
158   if (v1->conversation == v2->conversation)
159     return 1;
160
161   return 0;
162
163 }
164
165 static guint
166 beep_hash(gconstpointer v)
167 {
168   const struct beep_request_key *key = (const struct beep_request_key *)v;
169   guint val;
170
171   val = key->conversation;
172
173 #if defined(DEBUG_BEEP_HASH)
174   printf("BEEP Hash calculated as %u\n", val);
175 #endif
176
177   return val;
178
179 }
180
181 static void
182 beep_init_protocol(void)
183 {
184 #if defined(DEBUG_BEEP_HASH)
185   fprintf(stderr, "Initializing BEEP hashtable area\n");
186 #endif
187
188   if (beep_request_hash)
189     g_hash_table_destroy(beep_request_hash);
190   if (beep_request_keys)
191     g_mem_chunk_destroy(beep_request_keys);
192   if (beep_request_vals)
193     g_mem_chunk_destroy(beep_request_vals);
194   if (beep_packet_infos)
195     g_mem_chunk_destroy(beep_packet_infos);
196
197   beep_request_hash = g_hash_table_new(beep_hash, beep_equal);
198   beep_request_keys = g_mem_chunk_new("beep_request_keys",
199                                        sizeof(struct beep_request_key),
200                                        beep_packet_init_count * sizeof(struct beep_request_key), G_ALLOC_AND_FREE);
201   beep_request_vals = g_mem_chunk_new("beep_request_vals",
202                                       sizeof(struct beep_request_val),
203                                       beep_packet_init_count * sizeof(struct beep_request_val), G_ALLOC_AND_FREE);
204   beep_packet_infos = g_mem_chunk_new("beep_packet_infos",
205                                       sizeof(struct beep_proto_data),
206                                       beep_packet_init_count * sizeof(struct beep_proto_data), G_ALLOC_AND_FREE);
207 }
208
209 /*
210  * BEEP routines
211  */
212
213 static int beep_get_more(char more)
214 {
215
216   if (more == '.')
217     return BEEP_COMPLETE;
218   else if (more == '*')
219     return BEEP_INTERMEDIATE;
220
221   return BEEP_VIOL;
222 }
223
224 /* dissect the more flag, and return a value of:
225  *  1 -> more
226  *  0 -> no more
227  *  -1 -> Proto violation
228  */
229
230 static int
231 dissect_beep_more(tvbuff_t *tvb, int offset,
232                   proto_tree *tree)
233 {
234
235
236   switch (beep_get_more(tvb_get_guint8(tvb, offset))) {
237
238   case BEEP_COMPLETE:
239
240     if (tree) {
241       proto_tree_add_boolean_hidden(tree, hf_beep_complete, tvb, offset, 1, TRUE);
242       proto_tree_add_text(tree, tvb, offset, 1, "More: Complete");
243     }
244
245     return 0;
246
247     break;
248
249   case BEEP_INTERMEDIATE:
250
251     if (tree) {
252       proto_tree_add_boolean_hidden(tree, hf_beep_intermediate, tvb, offset, 1, TRUE);
253       proto_tree_add_text(tree, tvb, offset, 1, "More: Intermediate");
254     }
255
256     return 1;
257
258     break;
259
260   default:
261
262     if (tree) {
263       proto_tree_add_boolean_hidden(tree, hf_beep_proto_viol, tvb, offset, 1, TRUE);
264       proto_tree_add_text(tree, tvb, offset, 1, "PROTOCOL VIOLATION: Expected More Flag (* or .)");
265     }
266
267     return -1;
268
269     break;
270   }
271
272 }
273
274 #if 0
275 static void dissect_beep_status(tvbuff_t *tvb, int offset,
276                                 proto_tree *tree)
277 {
278
279   /* FIXME: We should return a value to indicate all OK. */
280
281   switch(tvb_get_guint8(tvb, offset)) {
282
283   case '+':
284
285     if (tree) {
286       proto_tree_add_boolean_hidden(tree, hf_beep_positive, tvb, offset, 1, TRUE);
287       proto_tree_add_text(tree, tvb, offset, 1, "Status: Positive");
288     }
289
290     break;
291
292   case '-':
293
294     if (tree) {
295       proto_tree_add_boolean_hidden(tree, hf_beep_negative, tvb, offset, 1, TRUE);
296       proto_tree_add_text(tree, tvb, offset, 1, "Status: Negative");
297     }
298
299     break;
300
301   default:  /* Proto violation: FIXME */
302
303     break;
304
305   }
306
307 }
308 #endif
309
310 static int num_len(tvbuff_t *tvb, int offset)
311 {
312   unsigned int i = 0;
313
314   while (isdigit(tvb_get_guint8(tvb, offset + i))) i++;
315
316   return i;
317
318 }
319
320 /*
321  * We check for a terminator. This can be CRLF, which will be recorded
322  * as a terminator, or CR or LF by itself, which will be redorded as
323  * an incorrect terminator ... We build the tree at this point
324  * However, we depend on the variable beep_strict_term
325  */
326
327 static int
328 check_term(tvbuff_t *tvb, int offset, proto_tree *tree)
329 {
330
331   /* First, check for CRLF, or, if global_beep_strict_term is false,
332    * one of CR or LF ... If neither of these hold, we add an element
333    * that complains of a protocol violation, and return -1, else
334    * we add a terminator to the tree (possibly non-standard) and return
335    * the count of characters we saw ... This may throw off the rest of the
336    * dissection ... so-be-it!
337    */
338
339   if ((tvb_get_guint8(tvb, offset) == 0x0d &&
340        tvb_get_guint8(tvb, offset + 1) == 0x0a)){ /* Correct terminator */
341
342     if (tree) {
343       proto_tree_add_text(tree, tvb, offset, 2, "Terminator: CRLF");
344     }
345     return 2;
346
347   }
348   else if ((tvb_get_guint8(tvb, offset) == 0x0d) && !global_beep_strict_term) {
349
350     if (tree) {
351       proto_tree_add_text(tree, tvb, offset, 1, "Nonstandard Terminator: CR");
352       proto_tree_add_boolean_hidden(tree, hf_beep_proto_viol, tvb, offset, 1, TRUE);
353     }
354     return 1;
355
356   }
357   else if ((tvb_get_guint8(tvb, offset) == 0x0a) && !global_beep_strict_term) {
358
359     if (tree) {
360       proto_tree_add_text(tree, tvb, offset, 1, "Nonstandard Terminator: LF");
361       proto_tree_add_boolean_hidden(tree, hf_beep_proto_viol, tvb, offset, 1, TRUE);
362     }
363     return 1;
364
365   }
366   else {
367
368     if (tree) {
369       proto_tree_add_text(tree, tvb, offset, 2, "PROTOCOL VIOLATION, Invalid Terminator: %s", tvb_format_text(tvb, offset, 2));
370       proto_tree_add_boolean_hidden(tree, hf_beep_proto_viol, tvb, offset, 2, TRUE);
371     }
372     return -1;
373
374   }
375
376 }
377
378 /* Get the header length, up to CRLF or CR or LF */
379 static int header_len(tvbuff_t *tvb, int offset)
380 {
381   int i = 0;
382   guint8 sc;
383
384   /* FIXME: Have to make sure we stop looking at the end of the tvb ... */
385
386   /* We look for CRLF, or CR or LF if global_beep_strict_term is
387    * not set.
388    */
389
390   while (1) {
391
392     if (tvb_ensure_length_remaining(tvb, offset + i) < 1)
393       return i;   /* Not enough characters left ... */
394
395     if ((sc = tvb_get_guint8(tvb, offset + i)) == 0x0d
396         && tvb_get_guint8(tvb, offset + i + 1) == 0x0a)
397       return i;   /* Done here ... */
398
399     if (!global_beep_strict_term && (sc == 0x0d || sc == 0x0a))
400       return i;   /* Done here also ... */
401
402     i++;
403
404   }
405 }
406
407 static int
408 dissect_beep_mime_header(tvbuff_t *tvb, int offset,
409                          struct beep_proto_data *frame_data,
410                          proto_tree *tree)
411 {
412   proto_tree    *ti = NULL, *mime_tree = NULL;
413   int           mime_length = header_len(tvb, offset), cc = 0;
414
415   if (frame_data && !frame_data->mime_hdr) return 0;
416
417   if (tree) {
418
419     /* FIXME: Should calculate the whole length of the mime headers */
420
421     ti = proto_tree_add_text(tree, tvb, offset, mime_length, "Mime header: %s", tvb_format_text(tvb, offset, mime_length));
422     mime_tree = proto_item_add_subtree(ti, ett_mime_header);
423   }
424
425   if (mime_length == 0) { /* Default header */
426
427     if (tree) {
428       proto_tree_add_text(mime_tree, tvb, offset, 0, "Default values");
429     }
430
431     if ((cc = check_term(tvb, offset, mime_tree)) <= 0) {
432
433       /* Ignore it, it will cause funnies in the rest of the dissect */
434
435     }
436
437   }
438   else {  /* FIXME: Process the headers */
439
440     if (tree) {
441       proto_tree_add_text(mime_tree, tvb, offset, mime_length, "Header: %s",
442                           tvb_format_text(tvb, offset, mime_length));
443     }
444
445     if ((cc = check_term(tvb, offset + mime_length, mime_tree)) <= 0) {
446
447       /* Ignore it, it will cause funnies in the rest of the dissect */
448
449     }
450
451   }
452
453   return mime_length + cc;  /* FIXME: Check that the CRLF is there */
454
455 }
456
457 static int
458 dissect_beep_int(tvbuff_t *tvb, int offset,
459                     proto_tree *tree, int hf, int *val, int *hfa[])
460 {
461   int ival, ind = 0;
462   unsigned int i = num_len(tvb, offset);
463   guint8 int_buff[100];
464
465   memset(int_buff, '\0', sizeof(int_buff));
466
467   tvb_memcpy(tvb, int_buff, offset, MIN(sizeof(int_buff) - 1, i));
468
469   /* XXX - is this still "Dangerous" now that we don't copy to the
470      last byte of "int_buff[]"? */
471   sscanf(int_buff, "%d", &ival);
472
473   if (tree) {
474     proto_tree_add_uint(tree, hf, tvb, offset, i, ival);
475   }
476
477   while (hfa[ind]) {
478
479     proto_tree_add_uint_hidden(tree, *hfa[ind], tvb, offset, i, ival);
480     ind++;
481
482   }
483
484   *val = ival;  /* Return the value */
485
486   return i;
487
488 }
489
490 static void
491 set_mime_hdr_flags(int more, struct beep_request_val *request_val,
492                    struct beep_proto_data *frame_data, packet_info *pinfo)
493 {
494
495   if (!request_val) return; /* Nothing to do ??? */
496
497   if (pinfo->destport == tcp_port) { /* Going to the server ... client */
498
499     if (request_val->c_mime_hdr) {
500
501       frame_data->mime_hdr = 0;
502
503       if (!more) request_val->c_mime_hdr = 0;
504
505     }
506     else {
507
508       frame_data->mime_hdr = 1;
509
510       if (more) request_val->c_mime_hdr = 1;
511
512     }
513
514   }
515   else {
516
517     if (request_val->s_mime_hdr) {
518
519       frame_data->mime_hdr = 0;
520
521       if (!more) request_val->s_mime_hdr = 0;
522
523     }
524     else {
525
526       frame_data->mime_hdr = 1;
527
528       if (more) request_val->s_mime_hdr = 1;
529
530     }
531
532   }
533
534 }
535
536 /* Build the tree
537  *
538  * A return value of <= 0 says we bailed out, skip the rest of this message,
539  * if any.
540  *
541  * A return value > 0 is the count of bytes we consumed ...
542  */
543
544 static int
545 dissect_beep_tree(tvbuff_t *tvb, int offset, packet_info *pinfo,
546                   proto_tree *tree, struct beep_request_val *request_val,
547                   struct beep_proto_data *frame_data)
548 {
549   proto_tree     *ti = NULL, *hdr = NULL;
550   int            st_offset, msgno, ansno, seqno, size, channel, ackno, window, cc,
551                  more;
552
553   char * cmd_temp = NULL;
554   int is_ANS = 0;
555   st_offset = offset;
556
557   if (tvb_strneql(tvb, offset, "MSG ", 4) == 0)
558     cmd_temp = "Command: MSG";
559   if (tvb_strneql(tvb, offset, "RPY ", 4) == 0)
560     cmd_temp = "Command: RPY";
561   if (tvb_strneql(tvb, offset, "ERR ", 4) == 0)
562     cmd_temp = "Command: ERR";
563   if (tvb_strneql(tvb, offset, "NUL ", 4) == 0)
564     cmd_temp = "Command: NUL";
565   if (tvb_strneql(tvb, offset, "ANS ", 4) == 0) {
566     cmd_temp = "Command: ANS";
567     is_ANS = 1;
568   }
569
570   if (cmd_temp != NULL) {
571
572     if (tree) {
573       ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");
574
575       hdr = proto_item_add_subtree(ti, ett_header);
576
577       proto_tree_add_boolean_hidden(hdr, hf_beep_req, tvb, offset, 3, TRUE);
578       proto_tree_add_text(hdr, tvb, offset, 3, cmd_temp);
579     }
580
581     offset += 4;
582
583     /* Get the channel */
584     offset += dissect_beep_int(tvb, offset, hdr, hf_beep_channel, &channel, req_chan_hfa);
585     offset += 1; /* Skip the space */
586
587     /* Dissect the message number */
588     offset += dissect_beep_int(tvb, offset, hdr, hf_beep_msgno, &msgno, req_msgno_hfa);
589     offset += 1; /* skip the space */
590
591     /* Insert the more elements ... */
592     if ((more = dissect_beep_more(tvb, offset, hdr)) >= 0) {
593       /* Figure out which direction this is in and what mime_hdr flag to
594        * add to the frame_data. If there are missing segments, this code
595        * will get it wrong!
596        */
597       set_mime_hdr_flags(more, request_val, frame_data, pinfo);
598     }
599     else {  /* Protocol violation, so dissect rest as undisectable */
600       if (tree) {
601         proto_tree_add_text(hdr, tvb, offset,
602                             tvb_length_remaining(tvb, offset),
603                             "Undissected Payload: %s",
604                             tvb_format_text(tvb, offset,
605                                             tvb_length_remaining(tvb, offset)
606                                             )
607                             );
608
609       }
610       return -1;
611     }
612
613     offset += 2; /* Skip the flag and the space ... */
614
615     /* now for the seqno */
616     offset += dissect_beep_int(tvb, offset, hdr, hf_beep_seqno, &seqno, req_seqno_hfa);
617     offset += 1; /* skip the space */
618
619     offset += dissect_beep_int(tvb, offset, hdr, hf_beep_size, &size, req_size_hfa);
620     if (request_val)   /* FIXME, is this the right order ... */
621       request_val -> size = size;  /* Stash this away */
622     else {
623       frame_data->pl_size = size;
624       if (frame_data->pl_size < 0) frame_data->pl_size = 0; /* FIXME: OK? */
625     }
626     /* offset += 1; skip the space */
627
628     if (is_ANS) { /* We need to put in the ansno */
629         offset += 1; /* skip the space */
630         /* Dissect the message number */
631         offset += dissect_beep_int(tvb, offset, hdr, hf_beep_ansno, &ansno, req_ansno_hfa);
632     }
633
634     if ((cc = check_term(tvb, offset, hdr)) <= 0) {
635
636       /* We dissect the rest as data and bail ... */
637
638       if (tree) {
639         proto_tree_add_text(hdr, tvb, offset,
640                             tvb_length_remaining(tvb, offset),
641                             "Undissected Payload: %s",
642                             tvb_format_text(tvb, offset,
643                                             tvb_length_remaining(tvb, offset)
644                                             )
645                             );
646       }
647
648       return -1;
649
650     }
651
652     offset += cc;
653
654     /* Insert MIME header ... */
655
656     if (frame_data && frame_data->mime_hdr)
657       offset += dissect_beep_mime_header(tvb, offset, frame_data, hdr);
658
659     /* Now for the payload, if any */
660
661     if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */
662
663       int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
664
665       /* Except, check the payload length, and only dissect that much */
666
667       /* We need to keep track, in the conversation, of how much is left
668        * so in the next packet, we can figure out what is part of the payload
669        * and what is the next message
670        */
671
672       if (tree) {
673         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));
674
675       }
676
677       offset += pl_size;
678
679       if (request_val) {
680         request_val->size -= pl_size;
681         if (request_val->size < 0) request_val->size = 0;
682       }
683       else {
684         frame_data->pl_size -= pl_size;
685         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
686       }
687     }
688
689     /* If anything else left, dissect it ... */
690
691     if (tvb_length_remaining(tvb, offset) > 0)
692       offset += dissect_beep_tree(tvb, offset, pinfo, tree, request_val, frame_data);
693
694   } else if (tvb_strneql(tvb, offset, "SEQ ", 4) == 0) {
695
696     if (tree) {
697       proto_tree_add_boolean_hidden(tree, hf_beep_seq, tvb, offset, 3, TRUE);
698       proto_tree_add_text(tree, tvb, offset, 3, "Command: SEQ");
699     }
700
701     offset += 3;
702
703     /* Now check the space: FIXME */
704
705     offset += 1;
706
707     offset += dissect_beep_int(tvb, offset, tree, hf_beep_channel, &channel, seq_chan_hfa);
708
709     /* Check the space: FIXME */
710
711     offset += 1;
712
713     offset += dissect_beep_int(tvb, offset, tree, hf_beep_ackno, &ackno, seq_ackno_hfa);
714
715     /* Check the space: FIXME */
716
717     offset += 1;
718
719     offset += dissect_beep_int(tvb, offset, tree, hf_beep_window, &window, seq_window_hfa);
720
721     if ((cc = check_term(tvb, offset, tree)) <= 0) {
722
723       /* We dissect the rest as data and bail ... */
724
725       if (tree) {
726         proto_tree_add_text(tree, tvb, offset,
727                             tvb_length_remaining(tvb, offset),
728                             "Undissected Payload: %s",
729                             tvb_format_text(tvb, offset,
730                                             tvb_length_remaining(tvb, offset)
731                                             )
732                             );
733       }
734
735       return -1;
736
737     }
738
739     offset += cc;
740
741   } else if (tvb_strneql(tvb, offset, "END", 3) == 0) {
742
743     proto_tree *tr = NULL;
744
745     if (tree) {
746       ti = proto_tree_add_text(tree, tvb, offset, MIN(5, tvb_length_remaining(tvb, offset)), "Trailer");
747
748       tr = proto_item_add_subtree(ti, ett_trailer);
749
750       proto_tree_add_boolean_hidden(tr, hf_beep_end, tvb, offset, 3, TRUE);
751       proto_tree_add_text(tr, tvb, offset, 3, "Command: END");
752
753     }
754
755     offset += 3;
756
757     if ((cc = check_term(tvb, offset, tr)) <= 0) {
758
759       /* We dissect the rest as data and bail ... */
760
761       if (tree) {
762         proto_tree_add_text(tr, tvb, offset, tvb_length_remaining(tvb, offset),
763                             "Undissected Payload: %s",
764                             tvb_format_text(tvb, offset,
765                                             tvb_length_remaining(tvb, offset)
766                                             )
767                             );
768       }
769
770       return -1;
771
772     }
773
774     offset += cc;
775
776   }
777
778   if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect anything left over */
779
780     int pl_size = 0;
781
782     if (request_val) {
783
784       pl_size = MIN(request_val->size, tvb_length_remaining(tvb, offset));
785
786       if (pl_size == 0) { /* The whole of the rest must be payload */
787
788         pl_size = tvb_length_remaining(tvb, offset); /* Right place ? */
789
790       }
791
792     } else if (frame_data) {
793       pl_size = MIN(frame_data->pl_size, tvb_length_remaining(tvb, offset));
794     } else { /* Just in case */
795       pl_size = tvb_length_remaining(tvb, offset);
796     }
797
798     /* Take care here to handle the payload correctly, and if there is
799      * another message here, then handle it correctly as well.
800      */
801
802     /* If the pl_size == 0 and the offset == 0?, then we have not processed
803      * anything in this frame above, so we better treat all this data as
804      * payload to avoid recursion loops
805      */
806
807     if (pl_size == 0 && offset == st_offset)
808       pl_size = tvb_length_remaining(tvb, offset);
809
810     if (pl_size > 0) {
811
812       if (tree) {
813         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s",
814                             tvb_format_text(tvb, offset, pl_size));
815       }
816
817       offset += pl_size;            /* Advance past the payload */
818
819       if (request_val){
820         request_val->size -= pl_size; /* Reduce payload by what we added */
821         if (request_val->size < 0) request_val->size = 0;
822       }
823       else {
824         frame_data->pl_size -= pl_size;
825         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
826       }
827     }
828
829     if (tvb_length_remaining(tvb, offset) > 0) {
830       offset += dissect_beep_tree(tvb, offset, pinfo, tree, request_val, frame_data);
831     }
832   }
833
834   return offset - st_offset;
835
836 }
837
838 static void
839 dissect_beep(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
840 {
841   int offset;
842   struct beep_proto_data  *frame_data = NULL;
843   proto_tree              *beep_tree = NULL, *ti = NULL;
844   conversation_t          *conversation = NULL;
845   struct beep_request_key request_key, *new_request_key;
846   struct beep_request_val *request_val = NULL;
847
848   offset = 0;
849
850   /* If we have per frame data, use that, else, we must have lost the per-
851    * frame data, and we have to do a full dissect pass again.
852    *
853    * The per-frame data tells us how much of this frame is left over from a
854    * previous frame, so we dissect it as payload and then try to dissect the
855    * rest.
856    *
857    * We use the conversation to build up info on the first pass over the
858    * packets of type BEEP, and record anything that is needed if the user
859    * does random dissects of packets in per packet data.
860    *
861    * Once we have per-packet data, we don't need the conversation stuff
862    * anymore, but if per-packet data and conversation stuff gets deleted, as
863    * it does under some circumstances when a rescan is done, it all gets
864    * rebuilt.
865    */
866
867   /* Find out what conversation this packet is part of ... but only
868    * if we have no information on this packet, so find the per-frame
869    * info first.
870    */
871
872   frame_data = p_get_proto_data(pinfo->fd, proto_beep);
873
874   if (!frame_data) {
875
876     conversation = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
877                                        pinfo->srcport, pinfo->destport, 0);
878     if (conversation == NULL) { /* No conversation, create one */
879         conversation = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
880                                         pinfo->srcport, pinfo->destport, 0);
881
882       }
883
884       /*
885        * Check for and insert an entry in the request table if does not exist
886        */
887       request_key.conversation = conversation->index;
888
889       request_val = (struct beep_request_val *)g_hash_table_lookup(beep_request_hash, &request_key);
890
891       if (!request_val) { /* Create one */
892
893         new_request_key = g_mem_chunk_alloc(beep_request_keys);
894         new_request_key->conversation = conversation->index;
895
896         request_val = g_mem_chunk_alloc(beep_request_vals);
897         request_val->processed = 0;
898         request_val->size = 0;
899
900         g_hash_table_insert(beep_request_hash, new_request_key, request_val);
901
902       }
903     }
904
905   if (check_col(pinfo->cinfo, COL_PROTOCOL))
906     col_set_str(pinfo->cinfo, COL_PROTOCOL, "BEEP");
907
908   if (check_col(pinfo->cinfo, COL_INFO)) {  /* Check the type ... */
909
910     /* "tvb_format_text()" is passed a value that won't go past the end
911      * of the packet, so it won't throw an exception. */
912     col_add_str(pinfo->cinfo, COL_INFO, tvb_format_text(tvb, offset, tvb_length_remaining(tvb, offset)));
913
914   }
915
916   /* Here, we parse the message so we can retrieve the info we need, which
917    * is that there is some payload left from a previous segment on the
918    * front of this segment ... This all depends on TCP segments not getting
919    * out of order ...
920    *
921    * As a huge kludge, we push the checking for the tree down into the code
922    * and process as if we were given a tree but not call the routines that
923    * adorn the protocol tree if they were NULL.
924    */
925
926   if (tree) {  /* Build the tree info ... */
927
928     ti = proto_tree_add_item(tree, proto_beep, tvb, offset, -1, FALSE);
929
930     beep_tree = proto_item_add_subtree(ti, ett_beep);
931
932   }
933
934   /* Check the per-frame data and the conversation for any left-over
935    * payload from the previous frame
936    *
937    * We check that per-frame data exists first, and if so, use it,
938    * else we use the conversation data.
939    *
940    * We create per-frame data here as well, but we must ensure we create it
941    * after we have done the check for per-frame or conversation data.
942    *
943    * We also depend on the first frame in a group having a pl_size of 0.
944    */
945
946   if (frame_data && frame_data->pl_left > 0) {
947
948     int pl_left = frame_data->pl_left;
949
950     pl_left = MIN(pl_left, tvb_length_remaining(tvb, offset));
951
952     /* Add the payload bit, only if we have a tree */
953     if (tree) {
954       proto_tree_add_text(beep_tree, tvb, offset, pl_left, "Payload: %s",
955                           tvb_format_text(tvb, offset, pl_left));
956     }
957     offset += pl_left;
958   }
959   else if (request_val && request_val->size > 0) {
960
961     int pl_left = request_val->size;
962
963     request_val->size = 0;
964
965     /* We create the frame data here for this case, and
966      * elsewhere for other frames
967      */
968
969     frame_data = g_mem_chunk_alloc(beep_packet_infos);
970
971     frame_data->pl_left = pl_left;
972     frame_data->pl_size = 0;
973     frame_data->mime_hdr = 0;
974
975     p_add_proto_data(pinfo->fd, proto_beep, frame_data);
976
977   }
978
979   /* Set up the per-frame data here if not already done so
980    * This _must_ come after the checks above ...
981    */
982
983   if (frame_data == NULL) {
984
985     frame_data = g_mem_chunk_alloc(beep_packet_infos);
986
987     frame_data->pl_left = 0;
988     frame_data->pl_size = 0;
989     frame_data->mime_hdr = 0;
990
991     p_add_proto_data(pinfo->fd, proto_beep, frame_data);
992
993   }
994
995   if (tvb_length_remaining(tvb, offset) > 0) {
996
997     offset += dissect_beep_tree(tvb, offset, pinfo, beep_tree, request_val, frame_data);
998
999   }
1000
1001 }
1002
1003 /* Register all the bits needed with the filtering engine */
1004
1005 void
1006 proto_register_beep(void)
1007 {
1008   static hf_register_info hf[] = {
1009     { &hf_beep_proto_viol,
1010       { "Protocol Violation", "beep.violation", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1011
1012     { &hf_beep_req,
1013       { "Request", "beep.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1014
1015     { &hf_beep_req_chan,
1016       { "Request Channel Number", "beep.req.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1017
1018     { &hf_beep_rsp,
1019       { "Response", "beep.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1020
1021     { &hf_beep_rsp_chan,
1022       { "Response Channel Number", "beep.rsp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1023
1024     { &hf_beep_seq,
1025       { "Sequence", "beep.seq", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1026
1027     { &hf_beep_seq_chan,
1028       { "Sequence Channel Number", "beep.seq.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1029
1030     { &hf_beep_end,
1031       { "End", "beep.end", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1032
1033     { &hf_beep_complete,
1034       { "Complete", "beep.more.complete", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1035
1036     { &hf_beep_intermediate,
1037       { "Intermediate", "beep.more.intermediate", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1038
1039     { &hf_beep_msgno,
1040       { "Msgno", "beep.msgno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1041
1042     { &hf_beep_ansno,
1043       { "Ansno", "beep.ansno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1044
1045     { &hf_beep_seqno,
1046       { "Seqno", "beep.seqno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1047
1048     { &hf_beep_size,
1049       { "Size", "beep.size", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1050
1051     { &hf_beep_channel,
1052       { "Channel", "beep.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1053
1054     { &hf_beep_negative,
1055       { "Negative", "beep.status.negative", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1056
1057     { &hf_beep_positive,
1058       { "Positive", "beep.status.positive", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1059
1060     { &hf_beep_ackno,
1061       { "Ackno", "beep.seq.ackno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1062
1063     { &hf_beep_window,
1064       { "Window", "beep.seq.window", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1065
1066   };
1067   static gint *ett[] = {
1068     &ett_beep,
1069     &ett_mime_header,
1070     &ett_header,
1071     &ett_trailer,
1072   };
1073   module_t *beep_module;
1074
1075   proto_beep = proto_register_protocol("Blocks Extensible Exchange Protocol",
1076                                        "BEEP", "beep");
1077
1078   proto_register_field_array(proto_beep, hf, array_length(hf));
1079   proto_register_subtree_array(ett, array_length(ett));
1080   register_init_routine(&beep_init_protocol);
1081
1082   /* Register our configuration options for BEEP, particularly our port */
1083
1084   beep_module = prefs_register_protocol(proto_beep, proto_reg_handoff_beep);
1085
1086   prefs_register_uint_preference(beep_module, "tcp.port", "BEEP TCP Port",
1087                                  "Set the port for BEEP messages (if other"
1088                                  " than the default of 10288)",
1089                                  10, &global_beep_tcp_port);
1090
1091   prefs_register_bool_preference(beep_module, "strict_header_terminator",
1092                                  "BEEP Header Requires CRLF",
1093                                  "Specifies that BEEP requires CRLF as a "
1094                                  "terminator, and not just CR or LF",
1095                                  &global_beep_strict_term);
1096 }
1097
1098 /* The registration hand-off routine */
1099 void
1100 proto_reg_handoff_beep(void)
1101 {
1102   static int beep_prefs_initialized = FALSE;
1103   static dissector_handle_t beep_handle;
1104
1105   if (!beep_prefs_initialized) {
1106
1107     beep_handle = create_dissector_handle(dissect_beep, proto_beep);
1108
1109     beep_prefs_initialized = TRUE;
1110
1111   }
1112   else {
1113
1114     dissector_delete("tcp.port", tcp_port, beep_handle);
1115
1116   }
1117
1118   /* Set our port number for future use */
1119
1120   tcp_port = global_beep_tcp_port;
1121
1122   dissector_add("tcp.port", global_beep_tcp_port, beep_handle);
1123
1124 }