Give the code that computes protocol statistics a progress dialog box,
[obnox/wireshark/wip.git] / packet-bxxp.c
1 /* packet-bxxp.c
2  * Routines for BXXP packet disassembly
3  *
4  * $Id: packet-bxxp.c,v 1.16 2001/01/22 08:03:44 guy Exp $
5  *
6  * Copyright (c) 2000 by Richard Sharpe <rsharpe@ns.aus.com>
7  *
8  * Developed with funding from InvisibleWorlds (www.invisibleworlds.com) via
9  * Collab.Net.
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs
13  * Copyright 1999 Gerald Combs
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <time.h>
45 #include <glib.h>
46 #include <string.h>
47 #include "packet.h"
48 #include "resolv.h"
49 #include "prefs.h"
50 #include "conversation.h"
51
52 #define TCP_PORT_BXXP 10288
53 void proto_reg_handoff_bxxp(void);
54
55
56 static int global_bxxp_tcp_port = TCP_PORT_BXXP;
57 static int global_bxxp_strict_term = TRUE;
58
59 static int proto_bxxp = -1;
60
61 static int hf_bxxp_req = -1;
62 static int hf_bxxp_req_chan = -1;
63 static int hf_bxxp_rsp_chan = -1;
64 static int hf_bxxp_seq_chan = -1;
65 static int hf_bxxp_rsp = -1;
66 static int hf_bxxp_seq = -1;
67 static int hf_bxxp_end = -1;
68 static int hf_bxxp_proto_viol = -1;
69 static int hf_bxxp_complete = -1;   /* No More data follows */
70 static int hf_bxxp_intermediate = -1; /* More data follows */
71 static int hf_bxxp_serial = -1;
72 static int hf_bxxp_seqno = -1;
73 static int hf_bxxp_size = -1;
74 static int hf_bxxp_channel = -1;
75 static int hf_bxxp_positive = -1;
76 static int hf_bxxp_negative = -1;
77 static int hf_bxxp_ackno = -1;
78 static int hf_bxxp_window = -1;
79
80 /* Arrays of hf entry pointers for some routines to use. If you want more
81  * hidden items added for a field, add them to the list before the NULL, 
82  * and the various routines that these are passed to will add them.
83  */
84
85 static int *req_serial_hfa[] = { &hf_bxxp_serial, NULL };
86 static int *req_seqno_hfa[]  = { &hf_bxxp_seqno, NULL };
87 static int *req_size_hfa[]   = { &hf_bxxp_size, NULL };
88 static int *req_chan_hfa[]   = { &hf_bxxp_channel, &hf_bxxp_req_chan, NULL };
89 static int *rsp_serial_hfa[] = { &hf_bxxp_serial, NULL };
90 static int *rsp_seqno_hfa[]  = { &hf_bxxp_seqno, NULL };
91 static int *rsp_size_hfa[]   = { &hf_bxxp_size, NULL };
92 static int *seq_chan_hfa[]   = { &hf_bxxp_channel, &hf_bxxp_seq_chan, NULL };
93 static int *seq_ackno_hfa[]  = { &hf_bxxp_ackno, NULL };
94 static int *seq_window_hfa[] = { &hf_bxxp_window, NULL };
95
96 static int ett_bxxp = -1;
97 static int ett_mime_header = -1;
98 static int ett_header = -1;
99 static int ett_trailer = -1;
100
101 static int tcp_port = 0;
102
103 /* Get the state of the more flag ... */
104
105 #define BXXP_VIOL         0
106 #define BXXP_INTERMEDIATE 1
107 #define BXXP_COMPLETE     2
108
109 /*
110  * Per-frame data
111  *
112  * pl_left is the amount of data in this packet that belongs to another
113  * frame ...
114  * 
115  * It relies on TCP segments not being re-ordered too much ...
116  */
117 struct bxxp_proto_data {
118   int pl_left;   /* Payload at beginning of frame */
119   int pl_size;   /* Payload in current message ...*/
120   int mime_hdr;  /* Whether we expect a mime header. 1 on first, 0 on rest 
121                   * in a message
122                   */
123 };
124
125 /*
126  * Conversation stuff
127  */
128 static int bxxp_packet_init_count = 100;
129
130 struct bxxp_request_key {
131   guint32 conversation;
132 };
133
134 struct bxxp_request_val {
135   guint16 processed;     /* Have we processed this conversation? */
136   int size;              /* Size of the message                  */
137                          /* We need an indication in each dirn of
138                           * whether on not a mime header is expected 
139                           */
140   int c_mime_hdr, s_mime_hdr;
141 };
142
143 GHashTable *bxxp_request_hash = NULL;
144 GMemChunk  *bxxp_request_keys = NULL;
145 GMemChunk  *bxxp_request_vals = NULL;
146 GMemChunk  *bxxp_packet_infos = NULL;
147
148 /* Hash Functions */
149 gint
150 bxxp_equal(gconstpointer v, gconstpointer w)
151 {
152   struct bxxp_request_key *v1 = (struct bxxp_request_key *)v;
153   struct bxxp_request_key *v2 = (struct bxxp_request_key *)w;
154
155 #if defined(DEBUG_BXXP_HASH)
156   printf("Comparing %08X\n      and %08X\n",
157          v1->conversation, v2->conversation);
158 #endif
159
160   if (v1->conversation == v2->conversation)
161     return 1;
162
163   return 0;
164
165 }
166
167 static guint
168 bxxp_hash(gconstpointer v)
169 {
170   struct bxxp_request_key *key = (struct bxxp_request_key *)v;
171   guint val;
172
173   val = key->conversation;
174
175 #if defined(DEBUG_BXXP_HASH)
176   printf("BXXP Hash calculated as %u\n", val);
177 #endif
178
179   return val;
180
181 }
182
183 static void
184 bxxp_init_protocol(void)
185 {
186 #if defined(DEBUG_BXXP_HASH)
187   fprintf(stderr, "Initializing BXXP hashtable area\n");
188 #endif
189
190   if (bxxp_request_hash)
191     g_hash_table_destroy(bxxp_request_hash);
192   if (bxxp_request_keys)
193     g_mem_chunk_destroy(bxxp_request_keys);
194   if (bxxp_request_vals)
195     g_mem_chunk_destroy(bxxp_request_vals);
196   if (bxxp_packet_infos)
197     g_mem_chunk_destroy(bxxp_packet_infos);
198
199   bxxp_request_hash = g_hash_table_new(bxxp_hash, bxxp_equal);
200   bxxp_request_keys = g_mem_chunk_new("bxxp_request_keys",
201                                        sizeof(struct bxxp_request_key),
202                                        bxxp_packet_init_count * sizeof(struct bxxp_request_key), G_ALLOC_AND_FREE);
203   bxxp_request_vals = g_mem_chunk_new("bxxp_request_vals", 
204                                       sizeof(struct bxxp_request_val),
205                                       bxxp_packet_init_count * sizeof(struct bxxp_request_val), G_ALLOC_AND_FREE);
206   bxxp_packet_infos = g_mem_chunk_new("bxxp_packet_infos",
207                                       sizeof(struct bxxp_proto_data),
208                                       bxxp_packet_init_count * sizeof(struct bxxp_proto_data), G_ALLOC_AND_FREE);
209 }
210
211 /*
212  * BXXP routines
213  */
214
215 int bxxp_get_more(char more)
216 {
217
218   if (more == '.')
219     return BXXP_COMPLETE;
220   else if (more == '*')
221     return BXXP_INTERMEDIATE;
222
223   return BXXP_VIOL;
224 }
225
226 /* dissect the more flag, and return a value of:
227  *  1 -> more
228  *  0 -> no more
229  *  -1 -> Proto violation
230  */
231
232 int
233 dissect_bxxp_more(tvbuff_t *tvb, int offset, frame_data *fd, 
234                   proto_tree *tree)
235 {
236
237
238   switch (bxxp_get_more(tvb_get_guint8(tvb, offset))) {
239
240   case BXXP_COMPLETE:
241
242     if (tree) {
243       proto_tree_add_boolean_hidden(tree, hf_bxxp_complete, tvb, offset, 1, TRUE);
244       proto_tree_add_text(tree, tvb, offset, 1, "More: Complete");
245     }
246
247     return 0;
248
249     break;
250
251   case BXXP_INTERMEDIATE:
252
253     if (tree) {
254       proto_tree_add_boolean_hidden(tree, hf_bxxp_intermediate, tvb, offset, 1, TRUE);
255       proto_tree_add_text(tree, tvb, offset, 1, "More: Intermediate");
256     }
257
258     return 1;
259
260     break;
261
262   default:  
263
264     if (tree) {
265       proto_tree_add_boolean_hidden(tree, hf_bxxp_proto_viol, tvb, offset, 1, TRUE);
266       proto_tree_add_text(tree, tvb, offset, 1, "PROTOCOL VIOLATION: Expected More Flag (* or .)");
267     }
268
269     return -1;
270
271     break;
272   }
273
274 }
275
276 void dissect_bxxp_status(tvbuff_t *tvb, int offset, frame_data *fd,
277                          proto_tree *tree)
278 {
279
280   /* FIXME: We should return a value to indicate all OK. */
281   
282   switch(tvb_get_guint8(tvb, offset)) {
283
284   case '+':
285
286     if (tree) {
287       proto_tree_add_boolean_hidden(tree, hf_bxxp_positive, tvb, offset, 1, TRUE);
288       proto_tree_add_text(tree, tvb, offset, 1, "Status: Positive");
289     }
290
291     break;
292
293   case '-':
294
295     if (tree) {
296       proto_tree_add_boolean_hidden(tree, hf_bxxp_negative, tvb, offset, 1, TRUE);
297       proto_tree_add_text(tree, tvb, offset, 1, "Status: Negative");
298     }
299
300     break;
301
302   default:  /* Proto violation: FIXME */
303
304     break;
305
306   }
307
308 }
309
310 int num_len(tvbuff_t *tvb, int offset)
311 {
312   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 bxxp_strict_term
325  */ 
326
327 int 
328 check_term(tvbuff_t *tvb, int offset, proto_tree *tree)
329 {
330
331   /* First, check for CRLF, or, if global_bxxp_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_bxxp_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_bxxp_proto_viol, tvb, offset, 1, TRUE);
353     }
354     return 1;
355
356   }
357   else if ((tvb_get_guint8(tvb, offset) == 0x0a) && !global_bxxp_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_bxxp_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_bxxp_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 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_bxxp_strict_term is 
387    * not set.
388    */
389
390   while (1) {
391
392     if (tvb_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_bxxp_strict_term && (sc == 0x0d || sc == 0x0a))
400       return i;   /* Done here also ... */
401           
402     i++;
403
404   }
405 }
406
407 int
408 dissect_bxxp_mime_header(tvbuff_t *tvb, int offset, 
409                          struct bxxp_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 int
458 dissect_bxxp_int(tvbuff_t *tvb, int offset, frame_data *fd,
459                     proto_tree *tree, int hf, int *val, int *hfa[])
460 {
461   int ival, ind = 0, i = num_len(tvb, offset);
462   guint8 int_buff[100];
463
464   memset(int_buff, '\0', sizeof(int_buff));
465
466   tvb_memcpy(tvb, int_buff, offset, MIN(sizeof(int_buff), i));
467
468   sscanf(int_buff, "%d", &ival);  /* FIXME: Dangerous */
469
470   if (tree) {
471     proto_tree_add_uint(tree, hf, tvb, offset, i, ival);
472   }
473
474   while (hfa[ind]) {
475
476     proto_tree_add_uint_hidden(tree, *hfa[ind], tvb, offset, i, ival);
477     ind++;
478
479   }
480
481   *val = ival;  /* Return the value */
482
483   return i;
484
485 }
486
487 void
488 set_mime_hdr_flags(int more, struct bxxp_request_val *request_val, 
489                    struct bxxp_proto_data *frame_data)
490 {
491
492   if (!request_val) return; /* Nothing to do ??? */
493
494   if (pi.destport == tcp_port) { /* Going to the server ... client */
495
496     if (request_val->c_mime_hdr) {
497
498       frame_data->mime_hdr = 0;
499
500       if (!more) request_val->c_mime_hdr = 0; 
501
502     }
503     else {
504
505       frame_data->mime_hdr = 1;
506
507       if (more) request_val->c_mime_hdr = 1;
508
509     }
510
511   }
512   else {
513
514     if (request_val->s_mime_hdr) {
515
516       frame_data->mime_hdr = 0;
517
518       if (!more) request_val->s_mime_hdr = 0; 
519
520     }
521     else {
522
523       frame_data->mime_hdr = 1;
524
525       if (more) request_val->s_mime_hdr = 1;
526
527     }
528
529   }
530
531 }
532
533 /* Build the tree
534  *
535  * A return value of <= 0 says we bailed out, skip the rest of this message,
536  * if any.
537  *
538  * A return value > 0 is the count of bytes we consumed ...
539  */
540
541 int
542 dissect_bxxp_tree(tvbuff_t *tvb, int offset, packet_info *pinfo, 
543                   proto_tree *tree, struct bxxp_request_val *request_val, 
544                   struct bxxp_proto_data *frame_data)
545 {
546   proto_tree     *ti = NULL, *hdr = NULL;
547   int            st_offset, serial, seqno, size, channel, ackno, window, cc,
548                  more;
549
550   st_offset = offset;
551
552   if (tvb_strneql(tvb, offset, "REQ ", 4) == 0) {
553
554     if (tree) {
555       ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");
556
557       hdr = proto_item_add_subtree(ti, ett_header);
558
559       proto_tree_add_boolean_hidden(hdr, hf_bxxp_req, tvb, offset, 3, TRUE);
560       proto_tree_add_text(hdr, NullTVB, offset, 3, "Command: REQ");
561     }
562
563     offset += 3;
564
565 #if 0
566     if (tvb_get_guint8(tvb, offset) != ' ') { /* Protocol violation */
567
568       /* Hmm, FIXME ... Add some code here ... */
569
570     }
571 #endif
572
573     offset += 1;  /* Skip the space */
574
575     /* Insert the more elements ... */
576
577     if ((more = dissect_bxxp_more(tvb, offset, pinfo->fd, hdr)) >= 0) {
578
579       /* Figure out which direction this is in and what mime_hdr flag to 
580        * add to the frame_data. If there are missing segments, this code
581        * will get it wrong!
582        */
583
584       set_mime_hdr_flags(more, request_val, frame_data);
585
586     }
587     else {  /* Protocol violation, so dissect rest as undisectable */
588
589       if (tree) {
590
591         proto_tree_add_text(hdr, tvb, offset, 
592                             tvb_length_remaining(tvb, offset),
593                             "Undissected Payload: %s",
594                             tvb_format_text(tvb, offset,
595                                             tvb_length_remaining(tvb, offset)
596                                             )
597                             );
598
599       }
600
601       return -1;
602
603     }
604
605     offset += 1;
606       
607     /* Check the space ... */
608
609     offset += 1;
610
611     /* Dissect the serial */
612
613     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial, req_serial_hfa);
614     /* skip the space */
615
616     offset += 1;
617
618     /* now for the seqno */
619
620     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno, req_seqno_hfa);
621
622     /* skip the space */
623
624     offset += 1;
625
626     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size, req_size_hfa);
627     if (request_val)   /* FIXME, is this the right order ... */
628       request_val -> size = size;  /* Stash this away */
629     else {
630       frame_data->pl_size = size;
631       if (frame_data->pl_size < 0) frame_data->pl_size = 0; /* FIXME: OK? */
632     }
633
634     /* Check the space */
635
636     offset += 1;
637
638     /* Get the channel */
639
640     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_channel, &channel, req_chan_hfa);
641       
642     if ((cc = check_term(tvb, offset, hdr)) <= 0) {
643
644       /* We dissect the rest as data and bail ... */
645
646       if (tree) {
647         proto_tree_add_text(hdr, tvb, offset, 
648                             tvb_length_remaining(tvb, offset),
649                             "Undissected Payload: %s", 
650                             tvb_format_text(tvb, offset, 
651                                             tvb_length_remaining(tvb, offset)
652                                             )
653                             );
654       }
655
656       return -1;
657
658     }
659
660     offset += cc;
661     
662     /* Insert MIME header ... */
663
664     if (frame_data && frame_data->mime_hdr)
665       offset += dissect_bxxp_mime_header(tvb, offset, frame_data, hdr);
666
667     /* Now for the payload, if any */
668
669     if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */
670
671       int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
672
673       /* Except, check the payload length, and only dissect that much */
674
675       /* We need to keep track, in the conversation, of how much is left 
676        * so in the next packet, we can figure out what is part of the payload
677        * and what is the next message
678        */
679
680       if (tree) {
681         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));
682
683       }
684
685       offset += pl_size;
686
687       if (request_val) {
688         request_val->size -= pl_size;
689         if (request_val->size < 0) request_val->size = 0;
690       }
691       else {
692         frame_data->pl_size -= pl_size;
693         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
694       }
695     }
696       
697     /* If anything else left, dissect it ... */
698
699     if (tvb_length_remaining(tvb, offset) > 0)
700       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
701
702   } else if (tvb_strneql(tvb, offset, "RSP ", 4) == 0) {
703
704     if (tree) {
705
706       ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");
707
708       hdr = proto_item_add_subtree(ti, ett_header);
709
710       proto_tree_add_boolean_hidden(hdr, hf_bxxp_rsp, NullTVB, offset, 3, TRUE);
711       proto_tree_add_text(hdr, tvb, offset, 3, "Command: RSP");
712
713     }
714
715     offset += 3;
716
717     /* Now check the space: FIXME */
718
719     offset += 1;
720
721     /* Next, the 'more' flag ... */
722
723     if ((more = dissect_bxxp_more(tvb, offset, pinfo->fd, hdr)) >= 0) {
724
725       set_mime_hdr_flags(more, request_val, frame_data);
726
727     }
728     else { 
729
730       if (tree) {
731
732         proto_tree_add_text(hdr, tvb, offset, 
733                             tvb_length_remaining(tvb, offset),
734                             "Undissected Payload: %s",
735                             tvb_format_text(tvb, offset,
736                                             tvb_length_remaining(tvb, offset)
737                                             )
738                             );
739
740       }
741
742       return -1;
743
744     }
745
746     offset += 1;
747
748     /* Check the space */
749
750     offset += 1;
751
752     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial, rsp_serial_hfa);
753     /* skip the space */
754
755     offset += 1;
756
757     /* now for the seqno */
758
759     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno, rsp_seqno_hfa);
760
761     /* skip the space */
762
763     offset += 1;
764
765     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size, rsp_size_hfa);
766     if (request_val)
767       request_val->size = size;
768     else
769       frame_data->pl_size = size;
770
771     /* Check the space ... */
772
773     offset += 1;
774
775     dissect_bxxp_status(tvb, offset, pinfo->fd, hdr);
776
777     offset += 1;
778
779     if ((cc = check_term(tvb, offset, hdr)) <= 0) {
780
781       /* We dissect the rest as data and bail ... */
782
783       if (tree) {
784         proto_tree_add_text(hdr, tvb, offset, 
785                             tvb_length_remaining(tvb, offset),
786                             "Undissected Payload: %s", 
787                             tvb_format_text(tvb, offset, 
788                                             tvb_length_remaining(tvb, offset)
789                                             )
790                             );
791       }
792
793       return -1;
794
795     }
796
797     offset += cc;
798     
799     /* Insert MIME header ... */
800
801     if (frame_data && frame_data->mime_hdr)
802       offset += dissect_bxxp_mime_header(tvb, offset, frame_data, hdr);
803
804     /* Now for the payload, if any */
805
806     if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */
807
808       int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
809       
810       /* Except, check the payload length, and only dissect that much */
811
812       if (tree) {
813         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));
814       }
815
816       offset += pl_size;
817
818       if (request_val) {
819         request_val->size -= pl_size;
820         if (request_val->size < 0) request_val->size = 0;
821       }
822       else {
823         frame_data->pl_size -= pl_size;
824         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
825       }
826     }
827
828     /* If anything else left, dissect it ... As what? */
829
830     if (tvb_length_remaining(tvb, offset) > 0)
831       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
832
833   } else if (tvb_strneql(tvb, offset, "SEQ ", 4) == 0) {
834
835     if (tree) {
836       proto_tree_add_boolean_hidden(tree, hf_bxxp_seq, NullTVB, offset, 3, TRUE);
837       proto_tree_add_text(tree, tvb, offset, 3, "Command: SEQ");
838     }
839
840     offset += 3;
841
842     /* Now check the space: FIXME */
843
844     offset += 1;
845
846     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_channel, &channel, seq_chan_hfa);
847
848     /* Check the space: FIXME */
849
850     offset += 1;
851
852     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_ackno, &ackno, seq_ackno_hfa);
853
854     /* Check the space: FIXME */
855
856     offset += 1;
857
858     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_window, &window, seq_window_hfa);
859
860     if ((cc = check_term(tvb, offset, tree)) <= 0) {
861
862       /* We dissect the rest as data and bail ... */
863
864       if (tree) {
865         proto_tree_add_text(tree, tvb, offset, 
866                             tvb_length_remaining(tvb, offset),
867                             "Undissected Payload: %s", 
868                             tvb_format_text(tvb, offset, 
869                                             tvb_length_remaining(tvb, offset)
870                                             )
871                             );
872       }
873
874       return -1;
875
876     }
877
878     offset += cc;
879
880   } else if (tvb_strneql(tvb, offset, "END", 3) == 0) {
881
882     proto_tree *tr = NULL;
883
884     if (tree) {
885       ti = proto_tree_add_text(tree, tvb, offset, MIN(5, tvb_length_remaining(tvb, offset)), "Trailer");
886
887       tr = proto_item_add_subtree(ti, ett_trailer);
888
889       proto_tree_add_boolean_hidden(tr, hf_bxxp_end, NullTVB, offset, 3, TRUE);
890       proto_tree_add_text(tr, tvb, offset, 3, "Command: END");
891
892     }
893
894     offset += 3;
895
896     if ((cc = check_term(tvb, offset, tr)) <= 0) {
897
898       /* We dissect the rest as data and bail ... */
899
900       if (tree) { 
901         proto_tree_add_text(tr, tvb, offset, tvb_length_remaining(tvb, offset),
902                             "Undissected Payload: %s", 
903                             tvb_format_text(tvb, offset, 
904                                             tvb_length_remaining(tvb, offset)
905                                             )
906                             );
907       }
908
909       return -1;
910
911     }
912
913     offset += cc;
914
915   }
916
917   if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect anything left over */
918
919     int pl_size = 0;
920
921     if (request_val) {
922
923       pl_size = MIN(request_val->size, tvb_length_remaining(tvb, offset));
924
925       /* FIXME: May be redundent ... */
926
927       if (pl_size == 0 && offset == st_offset) { /* The whole of the rest must be payload */
928       
929         pl_size = tvb_length_remaining(tvb, offset); /* Right place ? */
930       
931       }
932
933     } else if (frame_data) {
934       pl_size = MIN(frame_data->pl_size, tvb_length_remaining(tvb, offset));
935     } else { /* Just in case */
936       pl_size = tvb_length_remaining(tvb, offset);
937     }
938
939     /* Take care here to handle the payload correctly, and if there is 
940      * another message here, then handle it correctly as well.
941      */
942
943     /* If the pl_size == 0 and the offset == st_offset, then we have not 
944      * processed anything in this frame above, so we better treat all this
945      * data as payload to avoid recursion loops
946      */
947
948     if (pl_size == 0 && offset == st_offset) 
949       pl_size = tvb_length_remaining(tvb, offset);
950
951     if (pl_size > 0) {
952
953       if (tree) {
954         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s",
955                             tvb_format_text(tvb, offset, pl_size));
956       }
957
958       offset += pl_size;            /* Advance past the payload */
959
960       if (request_val){
961         request_val->size -= pl_size; /* Reduce payload by what we added */
962         if (request_val->size < 0) request_val->size = 0;
963       }
964       else {
965         frame_data->pl_size -= pl_size;
966         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
967       }
968     }
969
970     if (tvb_length_remaining(tvb, offset) > 0) {
971       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
972     }
973   }
974
975   return offset - st_offset;
976
977 }
978
979 static void
980 dissect_bxxp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
981 {
982   int offset;
983   struct bxxp_proto_data  *frame_data = NULL;
984   proto_tree              *bxxp_tree = NULL, *ti = NULL;
985   conversation_t          *conversation = NULL;
986   struct bxxp_request_key request_key, *new_request_key;
987   struct bxxp_request_val *request_val = NULL;
988
989   offset = 0;
990
991   /* If we have per frame data, use that, else, we must have lost the per-
992    * frame data, and we have to do a full dissect pass again.
993    *
994    * The per-frame data tells us how much of this frame is left over from a
995    * previous frame, so we dissect it as payload and then try to dissect the
996    * rest.
997    * 
998    * We use the conversation to build up info on the first pass over the
999    * packets of type BXXP, and record anything that is needed if the user
1000    * does random dissects of packets in per packet data.
1001    *
1002    * Once we have per-packet data, we don't need the conversation stuff 
1003    * anymore, but if per-packet data and conversation stuff gets deleted, as 
1004    * it does under some circumstances when a rescan is done, it all gets 
1005    * rebuilt.
1006    */
1007
1008   /* Find out what conversation this packet is part of ... but only
1009    * if we have no information on this packet, so find the per-frame 
1010    * info first.
1011    */
1012
1013   frame_data = p_get_proto_data(pinfo->fd, proto_bxxp);
1014
1015   if (!frame_data) {
1016
1017     conversation = find_conversation(&pinfo->src, &pinfo->dst, pi.ptype,
1018                                        pinfo->srcport, pinfo->destport, 0);
1019     if (conversation == NULL) { /* No conversation, create one */
1020         conversation = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
1021                                         pinfo->srcport, pinfo->destport, NULL,
1022                                         0);
1023
1024       }
1025
1026       /* 
1027        * Check for and insert an entry in the request table if does not exist
1028        */
1029       request_key.conversation = conversation->index;
1030
1031       request_val = (struct bxxp_request_val *)g_hash_table_lookup(bxxp_request_hash, &request_key);
1032       
1033       if (!request_val) { /* Create one */
1034
1035         new_request_key = g_mem_chunk_alloc(bxxp_request_keys);
1036         new_request_key->conversation = conversation->index;
1037
1038         request_val = g_mem_chunk_alloc(bxxp_request_vals);
1039         request_val->processed = 0;
1040         request_val->size = 0;
1041
1042         g_hash_table_insert(bxxp_request_hash, new_request_key, request_val);
1043
1044       }
1045     }
1046
1047   if (check_col(pinfo->fd, COL_PROTOCOL))
1048     col_set_str(pinfo->fd, COL_PROTOCOL, "BXXP");
1049
1050   if (check_col(pinfo->fd, COL_INFO)) {  /* Check the type ... */
1051
1052     /* "tvb_format_text()" is passed a value that won't go past the end
1053      * of the packet, so it won't throw an exception. */
1054     col_add_str(pinfo->fd, COL_INFO, tvb_format_text(tvb, offset, tvb_length_remaining(tvb, offset)));
1055
1056   }
1057
1058   /* Here, we parse the message so we can retrieve the info we need, which 
1059    * is that there is some payload left from a previous segment on the 
1060    * front of this segment ... This all depends on TCP segments not getting
1061    * out of order ... 
1062    *
1063    * As a huge kludge, we push the checking for the tree down into the code
1064    * and process as if we were given a tree but not call the routines that
1065    * adorn the protocol tree if they were NULL.
1066    */
1067
1068   if (tree) {  /* Build the tree info ... */
1069
1070     ti = proto_tree_add_item(tree, proto_bxxp, tvb, offset, tvb_length(tvb), FALSE);
1071
1072     bxxp_tree = proto_item_add_subtree(ti, ett_bxxp);
1073
1074   }
1075   
1076   /* Check the per-frame data and the conversation for any left-over 
1077    * payload from the previous frame 
1078    *
1079    * We check that per-frame data exists first, and if so, use it,
1080    * else we use the conversation data.
1081    *
1082    * We create per-frame data here as well, but we must ensure we create it
1083    * after we have done the check for per-frame or conversation data.
1084    *
1085    * We also depend on the first frame in a group having a pl_size of 0.
1086    */
1087
1088   if (frame_data && frame_data->pl_left > 0) {
1089
1090     int pl_left = frame_data->pl_left;
1091
1092     pl_left = MIN(pl_left, tvb_length_remaining(tvb, offset));
1093
1094     /* Add the payload bit, only if we have a tree */
1095     if (tree) {
1096       proto_tree_add_text(bxxp_tree, tvb, offset, pl_left, "Payload: %s",
1097                           tvb_format_text(tvb, offset, pl_left));
1098     }
1099     offset += pl_left;
1100   }
1101   else if (request_val && request_val->size > 0) {
1102
1103     int pl_left = request_val->size;
1104
1105     request_val->size = 0;
1106
1107     /* We create the frame data here for this case, and 
1108      * elsewhere for other frames
1109      */
1110
1111     frame_data = g_mem_chunk_alloc(bxxp_packet_infos);
1112
1113     frame_data->pl_left = pl_left;
1114     frame_data->pl_size = 0;
1115     frame_data->mime_hdr = 0;
1116       
1117     p_add_proto_data(pinfo->fd, proto_bxxp, frame_data);
1118
1119   }
1120
1121   /* Set up the per-frame data here if not already done so
1122    * This _must_ come after the checks above ...
1123    */
1124
1125   if (frame_data == NULL) { 
1126
1127     frame_data = g_mem_chunk_alloc(bxxp_packet_infos);
1128
1129     frame_data->pl_left = 0;
1130     frame_data->pl_size = 0;
1131     frame_data->mime_hdr = 0;
1132
1133     p_add_proto_data(pinfo->fd, proto_bxxp, frame_data);
1134         
1135   }
1136
1137   if (tvb_length_remaining(tvb, offset) > 0) {
1138
1139     offset += dissect_bxxp_tree(tvb, offset, pinfo, bxxp_tree, request_val, frame_data);
1140
1141   }
1142
1143 }
1144
1145 /* Register all the bits needed with the filtering engine */
1146
1147 void 
1148 proto_register_bxxp(void)
1149 {
1150   static hf_register_info hf[] = {
1151     { &hf_bxxp_proto_viol,
1152       { "Protocol Violation", "bxxp.violation", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1153
1154     { &hf_bxxp_req,
1155       { "Request", "bxxp.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1156
1157     { &hf_bxxp_req_chan,
1158       { "Request Channel Number", "bxxp.req.channel", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},
1159
1160     { &hf_bxxp_rsp,
1161       { "Response", "bxxp.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1162
1163     { &hf_bxxp_rsp_chan,
1164       { "Response Channel Number", "bxxp.rsp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},
1165
1166     { &hf_bxxp_seq,
1167       { "Sequence", "bxxp.seq", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1168
1169     { &hf_bxxp_seq_chan,
1170       { "Sequence Channel Number", "bxxp.seq.channel", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},
1171
1172     { &hf_bxxp_end,
1173       { "End", "bxxp.end", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1174
1175     { &hf_bxxp_complete,
1176       { "Complete", "bxxp.more.complete", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1177
1178     { &hf_bxxp_intermediate,
1179       { "Intermediate", "bxxp.more.intermediate", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "" }},
1180
1181     { &hf_bxxp_serial,
1182       { "Serial", "bxxp.serial", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},
1183
1184     { &hf_bxxp_seqno,
1185       { "Seqno", "bxxp.seqno", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},
1186
1187     { &hf_bxxp_size,
1188       { "Size", "bxxp.size", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},
1189
1190     { &hf_bxxp_channel,
1191       { "Channel", "bxxp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "" }},
1192
1193     { &hf_bxxp_negative,
1194       { "Negative", "bxxp.status.negative", FT_BOOLEAN, BASE_NONE, NULL, 0x0, ""}},
1195
1196     { &hf_bxxp_positive,
1197       { "Positive", "bxxp.status.positive", FT_BOOLEAN, BASE_NONE, NULL, 0x0, ""}},
1198
1199     { &hf_bxxp_ackno,
1200       { "Ackno", "bxxp.seq.ackno", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},
1201
1202     { &hf_bxxp_window,
1203       { "Window", "bxxp.seq.window", FT_UINT32, BASE_DEC, NULL, 0x0, ""}},
1204
1205   };
1206   static gint *ett[] = {
1207     &ett_bxxp,
1208     &ett_mime_header,
1209     &ett_header,
1210     &ett_trailer,
1211   };
1212   module_t *bxxp_module; 
1213
1214   proto_bxxp = proto_register_protocol("Blocks eXtensible eXchange Protocol",
1215                                        "BXXP", "bxxp");
1216
1217   proto_register_field_array(proto_bxxp, hf, array_length(hf));
1218   proto_register_subtree_array(ett, array_length(ett));
1219   register_init_routine(&bxxp_init_protocol);
1220
1221   /* Register our configuration options for BXXP, particularly our port */
1222
1223   bxxp_module = prefs_register_protocol(proto_bxxp, proto_reg_handoff_bxxp);
1224
1225   prefs_register_uint_preference(bxxp_module, "tcp.port", "BXXP TCP Port",
1226                                  "Set the port for BXXP messages (if other"
1227                                  " than the default of 10288)",
1228                                  10, &global_bxxp_tcp_port);
1229
1230   prefs_register_bool_preference(bxxp_module, "strict_header_terminator", 
1231                                  "BXXP Header Requires CRLF", 
1232                                  "Specifies that BXXP requires CRLF as a "
1233                                  "terminator, and not just CR or LF",
1234                                  &global_bxxp_strict_term);
1235 }
1236
1237 /* The registration hand-off routine */
1238 void
1239 proto_reg_handoff_bxxp(void)
1240 {
1241   static int bxxp_prefs_initialized = FALSE;
1242
1243   if (bxxp_prefs_initialized) {
1244
1245     dissector_delete("tcp.port", tcp_port, dissect_bxxp);
1246
1247   }
1248   else {
1249
1250     bxxp_prefs_initialized = TRUE;
1251
1252   }
1253
1254   /* Set our port number for future use */
1255
1256   tcp_port = global_bxxp_tcp_port;
1257
1258   dissector_add("tcp.port", global_bxxp_tcp_port, dissect_bxxp, proto_bxxp);
1259
1260 }