Save the function code and FID for pipe requests, and use it for the
[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.21 2001/09/03 10:33:05 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 unsigned 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 static 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 static 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 static 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 static 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 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 bxxp_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_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 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_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 static 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 static 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;
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), i));
468
469   sscanf(int_buff, "%d", &ival);  /* FIXME: Dangerous */
470
471   if (tree) {
472     proto_tree_add_uint(tree, hf, tvb, offset, i, ival);
473   }
474
475   while (hfa[ind]) {
476
477     proto_tree_add_uint_hidden(tree, *hfa[ind], tvb, offset, i, ival);
478     ind++;
479
480   }
481
482   *val = ival;  /* Return the value */
483
484   return i;
485
486 }
487
488 static void
489 set_mime_hdr_flags(int more, struct bxxp_request_val *request_val, 
490                    struct bxxp_proto_data *frame_data, packet_info *pinfo)
491 {
492
493   if (!request_val) return; /* Nothing to do ??? */
494
495   if (pinfo->destport == tcp_port) { /* Going to the server ... client */
496
497     if (request_val->c_mime_hdr) {
498
499       frame_data->mime_hdr = 0;
500
501       if (!more) request_val->c_mime_hdr = 0; 
502
503     }
504     else {
505
506       frame_data->mime_hdr = 1;
507
508       if (more) request_val->c_mime_hdr = 1;
509
510     }
511
512   }
513   else {
514
515     if (request_val->s_mime_hdr) {
516
517       frame_data->mime_hdr = 0;
518
519       if (!more) request_val->s_mime_hdr = 0; 
520
521     }
522     else {
523
524       frame_data->mime_hdr = 1;
525
526       if (more) request_val->s_mime_hdr = 1;
527
528     }
529
530   }
531
532 }
533
534 /* Build the tree
535  *
536  * A return value of <= 0 says we bailed out, skip the rest of this message,
537  * if any.
538  *
539  * A return value > 0 is the count of bytes we consumed ...
540  */
541
542 static int
543 dissect_bxxp_tree(tvbuff_t *tvb, int offset, packet_info *pinfo, 
544                   proto_tree *tree, struct bxxp_request_val *request_val, 
545                   struct bxxp_proto_data *frame_data)
546 {
547   proto_tree     *ti = NULL, *hdr = NULL;
548   int            st_offset, serial, seqno, size, channel, ackno, window, cc,
549                  more;
550
551   st_offset = offset;
552
553   if (tvb_strneql(tvb, offset, "REQ ", 4) == 0) {
554
555     if (tree) {
556       ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");
557
558       hdr = proto_item_add_subtree(ti, ett_header);
559
560       proto_tree_add_boolean_hidden(hdr, hf_bxxp_req, tvb, offset, 3, TRUE);
561       proto_tree_add_text(hdr, tvb, offset, 3, "Command: REQ");
562     }
563
564     offset += 3;
565
566 #if 0
567     if (tvb_get_guint8(tvb, offset) != ' ') { /* Protocol violation */
568
569       /* Hmm, FIXME ... Add some code here ... */
570
571     }
572 #endif
573
574     offset += 1;  /* Skip the space */
575
576     /* Insert the more elements ... */
577
578     if ((more = dissect_bxxp_more(tvb, offset, pinfo->fd, hdr)) >= 0) {
579
580       /* Figure out which direction this is in and what mime_hdr flag to 
581        * add to the frame_data. If there are missing segments, this code
582        * will get it wrong!
583        */
584
585       set_mime_hdr_flags(more, request_val, frame_data, pinfo);
586
587     }
588     else {  /* Protocol violation, so dissect rest as undisectable */
589
590       if (tree) {
591
592         proto_tree_add_text(hdr, tvb, offset, 
593                             tvb_length_remaining(tvb, offset),
594                             "Undissected Payload: %s",
595                             tvb_format_text(tvb, offset,
596                                             tvb_length_remaining(tvb, offset)
597                                             )
598                             );
599
600       }
601
602       return -1;
603
604     }
605
606     offset += 1;
607       
608     /* Check the space ... */
609
610     offset += 1;
611
612     /* Dissect the serial */
613
614     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial, req_serial_hfa);
615     /* skip the space */
616
617     offset += 1;
618
619     /* now for the seqno */
620
621     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno, req_seqno_hfa);
622
623     /* skip the space */
624
625     offset += 1;
626
627     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size, req_size_hfa);
628     if (request_val)   /* FIXME, is this the right order ... */
629       request_val -> size = size;  /* Stash this away */
630     else {
631       frame_data->pl_size = size;
632       if (frame_data->pl_size < 0) frame_data->pl_size = 0; /* FIXME: OK? */
633     }
634
635     /* Check the space */
636
637     offset += 1;
638
639     /* Get the channel */
640
641     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_channel, &channel, req_chan_hfa);
642       
643     if ((cc = check_term(tvb, offset, hdr)) <= 0) {
644
645       /* We dissect the rest as data and bail ... */
646
647       if (tree) {
648         proto_tree_add_text(hdr, tvb, offset, 
649                             tvb_length_remaining(tvb, offset),
650                             "Undissected Payload: %s", 
651                             tvb_format_text(tvb, offset, 
652                                             tvb_length_remaining(tvb, offset)
653                                             )
654                             );
655       }
656
657       return -1;
658
659     }
660
661     offset += cc;
662     
663     /* Insert MIME header ... */
664
665     if (frame_data && frame_data->mime_hdr)
666       offset += dissect_bxxp_mime_header(tvb, offset, frame_data, hdr);
667
668     /* Now for the payload, if any */
669
670     if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */
671
672       int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
673
674       /* Except, check the payload length, and only dissect that much */
675
676       /* We need to keep track, in the conversation, of how much is left 
677        * so in the next packet, we can figure out what is part of the payload
678        * and what is the next message
679        */
680
681       if (tree) {
682         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));
683
684       }
685
686       offset += pl_size;
687
688       if (request_val) {
689         request_val->size -= pl_size;
690         if (request_val->size < 0) request_val->size = 0;
691       }
692       else {
693         frame_data->pl_size -= pl_size;
694         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
695       }
696     }
697       
698     /* If anything else left, dissect it ... */
699
700     if (tvb_length_remaining(tvb, offset) > 0)
701       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
702
703   } else if (tvb_strneql(tvb, offset, "RSP ", 4) == 0) {
704
705     if (tree) {
706
707       ti = proto_tree_add_text(tree, tvb, offset, header_len(tvb, offset) + 2, "Header");
708
709       hdr = proto_item_add_subtree(ti, ett_header);
710
711       proto_tree_add_boolean_hidden(hdr, hf_bxxp_rsp, tvb, offset, 3, TRUE);
712       proto_tree_add_text(hdr, tvb, offset, 3, "Command: RSP");
713
714     }
715
716     offset += 3;
717
718     /* Now check the space: FIXME */
719
720     offset += 1;
721
722     /* Next, the 'more' flag ... */
723
724     if ((more = dissect_bxxp_more(tvb, offset, pinfo->fd, hdr)) >= 0) {
725
726       set_mime_hdr_flags(more, request_val, frame_data, pinfo);
727
728     }
729     else { 
730
731       if (tree) {
732
733         proto_tree_add_text(hdr, tvb, offset, 
734                             tvb_length_remaining(tvb, offset),
735                             "Undissected Payload: %s",
736                             tvb_format_text(tvb, offset,
737                                             tvb_length_remaining(tvb, offset)
738                                             )
739                             );
740
741       }
742
743       return -1;
744
745     }
746
747     offset += 1;
748
749     /* Check the space */
750
751     offset += 1;
752
753     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_serial, &serial, rsp_serial_hfa);
754     /* skip the space */
755
756     offset += 1;
757
758     /* now for the seqno */
759
760     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_seqno, &seqno, rsp_seqno_hfa);
761
762     /* skip the space */
763
764     offset += 1;
765
766     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, hdr, hf_bxxp_size, &size, rsp_size_hfa);
767     if (request_val)
768       request_val->size = size;
769     else
770       frame_data->pl_size = size;
771
772     /* Check the space ... */
773
774     offset += 1;
775
776     dissect_bxxp_status(tvb, offset, pinfo->fd, hdr);
777
778     offset += 1;
779
780     if ((cc = check_term(tvb, offset, hdr)) <= 0) {
781
782       /* We dissect the rest as data and bail ... */
783
784       if (tree) {
785         proto_tree_add_text(hdr, tvb, offset, 
786                             tvb_length_remaining(tvb, offset),
787                             "Undissected Payload: %s", 
788                             tvb_format_text(tvb, offset, 
789                                             tvb_length_remaining(tvb, offset)
790                                             )
791                             );
792       }
793
794       return -1;
795
796     }
797
798     offset += cc;
799     
800     /* Insert MIME header ... */
801
802     if (frame_data && frame_data->mime_hdr)
803       offset += dissect_bxxp_mime_header(tvb, offset, frame_data, hdr);
804
805     /* Now for the payload, if any */
806
807     if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect what is left as payload */
808
809       int pl_size = MIN(size, tvb_length_remaining(tvb, offset));
810       
811       /* Except, check the payload length, and only dissect that much */
812
813       if (tree) {
814         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s", tvb_format_text(tvb, offset, pl_size));
815       }
816
817       offset += pl_size;
818
819       if (request_val) {
820         request_val->size -= pl_size;
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 anything else left, dissect it ... As what? */
830
831     if (tvb_length_remaining(tvb, offset) > 0)
832       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
833
834   } else if (tvb_strneql(tvb, offset, "SEQ ", 4) == 0) {
835
836     if (tree) {
837       proto_tree_add_boolean_hidden(tree, hf_bxxp_seq, tvb, offset, 3, TRUE);
838       proto_tree_add_text(tree, tvb, offset, 3, "Command: SEQ");
839     }
840
841     offset += 3;
842
843     /* Now check the space: FIXME */
844
845     offset += 1;
846
847     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_channel, &channel, seq_chan_hfa);
848
849     /* Check the space: FIXME */
850
851     offset += 1;
852
853     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_ackno, &ackno, seq_ackno_hfa);
854
855     /* Check the space: FIXME */
856
857     offset += 1;
858
859     offset += dissect_bxxp_int(tvb, offset, pinfo->fd, tree, hf_bxxp_window, &window, seq_window_hfa);
860
861     if ((cc = check_term(tvb, offset, tree)) <= 0) {
862
863       /* We dissect the rest as data and bail ... */
864
865       if (tree) {
866         proto_tree_add_text(tree, tvb, offset, 
867                             tvb_length_remaining(tvb, offset),
868                             "Undissected Payload: %s", 
869                             tvb_format_text(tvb, offset, 
870                                             tvb_length_remaining(tvb, offset)
871                                             )
872                             );
873       }
874
875       return -1;
876
877     }
878
879     offset += cc;
880
881   } else if (tvb_strneql(tvb, offset, "END", 3) == 0) {
882
883     proto_tree *tr = NULL;
884
885     if (tree) {
886       ti = proto_tree_add_text(tree, tvb, offset, MIN(5, tvb_length_remaining(tvb, offset)), "Trailer");
887
888       tr = proto_item_add_subtree(ti, ett_trailer);
889
890       proto_tree_add_boolean_hidden(tr, hf_bxxp_end, tvb, offset, 3, TRUE);
891       proto_tree_add_text(tr, tvb, offset, 3, "Command: END");
892
893     }
894
895     offset += 3;
896
897     if ((cc = check_term(tvb, offset, tr)) <= 0) {
898
899       /* We dissect the rest as data and bail ... */
900
901       if (tree) { 
902         proto_tree_add_text(tr, tvb, offset, tvb_length_remaining(tvb, offset),
903                             "Undissected Payload: %s", 
904                             tvb_format_text(tvb, offset, 
905                                             tvb_length_remaining(tvb, offset)
906                                             )
907                             );
908       }
909
910       return -1;
911
912     }
913
914     offset += cc;
915
916   }
917
918   if (tvb_length_remaining(tvb, offset) > 0) { /* Dissect anything left over */
919
920     int pl_size = 0;
921
922     if (request_val) {
923
924       pl_size = MIN(request_val->size, tvb_length_remaining(tvb, offset));
925
926       /* FIXME: May be redundent ... */
927
928       if (pl_size == 0 && offset == st_offset) { /* The whole of the rest must be payload */
929       
930         pl_size = tvb_length_remaining(tvb, offset); /* Right place ? */
931       
932       }
933
934     } else if (frame_data) {
935       pl_size = MIN(frame_data->pl_size, tvb_length_remaining(tvb, offset));
936     } else { /* Just in case */
937       pl_size = tvb_length_remaining(tvb, offset);
938     }
939
940     /* Take care here to handle the payload correctly, and if there is 
941      * another message here, then handle it correctly as well.
942      */
943
944     /* If the pl_size == 0 and the offset == st_offset, then we have not 
945      * processed anything in this frame above, so we better treat all this
946      * data as payload to avoid recursion loops
947      */
948
949     if (pl_size == 0 && offset == st_offset) 
950       pl_size = tvb_length_remaining(tvb, offset);
951
952     if (pl_size > 0) {
953
954       if (tree) {
955         proto_tree_add_text(tree, tvb, offset, pl_size, "Payload: %s",
956                             tvb_format_text(tvb, offset, pl_size));
957       }
958
959       offset += pl_size;            /* Advance past the payload */
960
961       if (request_val){
962         request_val->size -= pl_size; /* Reduce payload by what we added */
963         if (request_val->size < 0) request_val->size = 0;
964       }
965       else {
966         frame_data->pl_size -= pl_size;
967         if (frame_data->pl_size < 0) frame_data->pl_size = 0;
968       }
969     }
970
971     if (tvb_length_remaining(tvb, offset) > 0) {
972       offset += dissect_bxxp_tree(tvb, offset, pinfo, tree, request_val, frame_data);
973     }
974   }
975
976   return offset - st_offset;
977
978 }
979
980 static void
981 dissect_bxxp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
982 {
983   int offset;
984   struct bxxp_proto_data  *frame_data = NULL;
985   proto_tree              *bxxp_tree = NULL, *ti = NULL;
986   conversation_t          *conversation = NULL;
987   struct bxxp_request_key request_key, *new_request_key;
988   struct bxxp_request_val *request_val = NULL;
989
990   offset = 0;
991
992   /* If we have per frame data, use that, else, we must have lost the per-
993    * frame data, and we have to do a full dissect pass again.
994    *
995    * The per-frame data tells us how much of this frame is left over from a
996    * previous frame, so we dissect it as payload and then try to dissect the
997    * rest.
998    * 
999    * We use the conversation to build up info on the first pass over the
1000    * packets of type BXXP, and record anything that is needed if the user
1001    * does random dissects of packets in per packet data.
1002    *
1003    * Once we have per-packet data, we don't need the conversation stuff 
1004    * anymore, but if per-packet data and conversation stuff gets deleted, as 
1005    * it does under some circumstances when a rescan is done, it all gets 
1006    * rebuilt.
1007    */
1008
1009   /* Find out what conversation this packet is part of ... but only
1010    * if we have no information on this packet, so find the per-frame 
1011    * info first.
1012    */
1013
1014   frame_data = p_get_proto_data(pinfo->fd, proto_bxxp);
1015
1016   if (!frame_data) {
1017
1018     conversation = find_conversation(&pinfo->src, &pinfo->dst, pinfo->ptype,
1019                                        pinfo->srcport, pinfo->destport, 0);
1020     if (conversation == NULL) { /* No conversation, create one */
1021         conversation = conversation_new(&pinfo->src, &pinfo->dst, pinfo->ptype,
1022                                         pinfo->srcport, pinfo->destport, 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, "", HFILL }},
1153
1154     { &hf_bxxp_req,
1155       { "Request", "bxxp.req", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1156
1157     { &hf_bxxp_req_chan,
1158       { "Request Channel Number", "bxxp.req.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1159
1160     { &hf_bxxp_rsp,
1161       { "Response", "bxxp.rsp", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1162
1163     { &hf_bxxp_rsp_chan,
1164       { "Response Channel Number", "bxxp.rsp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1165
1166     { &hf_bxxp_seq,
1167       { "Sequence", "bxxp.seq", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1168
1169     { &hf_bxxp_seq_chan,
1170       { "Sequence Channel Number", "bxxp.seq.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1171
1172     { &hf_bxxp_end,
1173       { "End", "bxxp.end", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1174
1175     { &hf_bxxp_complete,
1176       { "Complete", "bxxp.more.complete", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1177
1178     { &hf_bxxp_intermediate,
1179       { "Intermediate", "bxxp.more.intermediate", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1180
1181     { &hf_bxxp_serial,
1182       { "Serial", "bxxp.serial", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1183
1184     { &hf_bxxp_seqno,
1185       { "Seqno", "bxxp.seqno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1186
1187     { &hf_bxxp_size,
1188       { "Size", "bxxp.size", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1189
1190     { &hf_bxxp_channel,
1191       { "Channel", "bxxp.channel", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1192
1193     { &hf_bxxp_negative,
1194       { "Negative", "bxxp.status.negative", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1195
1196     { &hf_bxxp_positive,
1197       { "Positive", "bxxp.status.positive", FT_BOOLEAN, BASE_NONE, NULL, 0x0, "", HFILL }},
1198
1199     { &hf_bxxp_ackno,
1200       { "Ackno", "bxxp.seq.ackno", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
1201
1202     { &hf_bxxp_window,
1203       { "Window", "bxxp.seq.window", FT_UINT32, BASE_DEC, NULL, 0x0, "", HFILL }},
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 }