From Graeme Lunt
[obnox/wireshark/wip.git] / epan / dissectors / packet-ajp13.c
1 /* packet-ajp13.c
2  * Routines for AJP13 dissection
3  * Copyright 2002, Christopher K. St. John <cks@distributopia.com>
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include <epan/emem.h>
38 #include <epan/conversation.h>
39 #include "packet-tcp.h"
40
41
42
43 /* IMPORTANT IMPLEMENTATION NOTES
44  *
45  * You need to be looking at: jk/doc/AJP13.html in the
46  * jakarta-tomcat-connectors repository.
47  *
48  * If you're an ethereal dissector guru, then you can skip the rest of
49  * this. I'm writing it all down because I've written 3 dissectors so
50  * far and every time I've forgotten it all and had to re-learn it
51  * from scratch. Not this time, damnit.
52  *
53  * Dissector routines get called in two phases:
54  *
55  * The first phase is an in-order traversal of every incoming
56  * frame. Since we know it's in-order, we can set up a "conversational
57  * state" that records context-sensitive stuff like "was there a
58  * content-length in the previous request". During this first pass
59  * through the data, the "tree" parameter might be null, or not. For
60  * the regular gui-based ethereal, it's null, which means we don't
61  * actually display the dissected data in the gui quite yet. For the
62  * text based interface, we might do the parsing and display both in
63  * this first pass.
64  *
65  * The second phase happens when the data is actually displayed. In
66  * this pase the "tree" param is non-null, so you've got a hook to
67  * hang the parsed-out display data on. Since there might be gigabytes
68  * worth of capture data, the display code only calls the dissector
69  * for the stuff the user actually clicks on. So you have to assume
70  * the dissector is getting called on random frames, you can't depend
71  * on ordering anymore.
72  *
73  * But some parts of the AJP13 capture stream are context sensitive.
74  * That's no big deal during the first in-order pass, but the second
75  * phase requires us to display any random frame correctly. So during
76  * the first in-order phase we create a per-frame user data structure
77  * and attach it to the frame using p_add_proto_data.
78  *
79  * Since AJP13 is a TCP/IP based protocol, writing a dissector for it
80  * requires addressing several other issues:
81  *
82  * 1) TCP/IP segments can get retransmitted or be sent out of
83  * order. Users don't normally care, because the low-level kernel
84  * networking code takes care of reassembling them properly. But we're
85  * looking at raw network packets, aren't we? The stuff on the
86  * wire. Ethereal has been getting better and better at helping
87  * dissectors with this. I'm a little fuzzy on the details, but my
88  * uderstanding is that ethereal now contains a fairly substantial
89  * user-space TCP/IP stack so it can re-assemble the data. But I might
90  * be wrong. Since AJP13 is going to be used either on the loopback
91  * interface or on a LAN, it isn't likely to be a big issues anyway.
92  *
93  * 2) AJP13 packets (PDU's or protocol data unit's in
94  * networking-speak) don't necessarily line up with TCP segments. That
95  * is, one TCP segment can have more than one AJP13 PDU, or one AJP13
96  * PDU can stretch across multiple TCP segments. Assembling them is
97  * obviously possible, but a royal pain. During the "phase one"
98  * in-order pass you have to keep track of a bunch of offsets and
99  * store which PDU goes with which TCP segment. Luckly, recent
100  * (0.9.4+) versions of ethereal provide the "tcp_dissect_pdus()"
101  * function that takes care of much of the work. See the comments in
102  * packet-tcp.c, the example code in packet-dns.c, or check the
103  * ethereal-dev archives for details.
104  *
105  * 3) Ethereal isn't guaranteed to see all the data. I'm a little
106  * unclear on all the possible failure modes, but it comes down to: a)
107  * Not your fault: it's an imperfect world, we're eavesdroppers, and
108  * stuff happens. We might totally miss packets or get garbled
109  * data. Or b) Totally your fault: you turn on the capture during the
110  * middle of an AJP13 conversation and the capture starts out with
111  * half an AJP13 PDU. This code doesn't currently handle either case
112  * very well, but you can get arbitrarily clever. Like: put in tests
113  * to see if this packet has reasonable field values, and if it
114  * doesn't, walk the offset ahead until we see a matching magic number
115  * field, then re-test. But we don't do that now, and since we're
116  * using tcp_dissect_pdu's, I'm not sure how to do it.
117  *
118  */
119
120
121 /*
122  * Request/response header codes. Common headers are stored as ints in
123  * an effort to improve performance. Why can't we just have one big
124  * list?
125  */
126
127 static const value_string req_header_codes[] = {
128   { 0x01, "accept" },
129   { 0x02, "accept-charset" },
130   { 0x03, "accept-encoding" },
131   { 0x04, "accept-language" },
132   { 0x05, "authorization" },
133   { 0x06, "connection" },
134   { 0x07, "content-type" },
135   { 0x08, "content-length" },
136   { 0x09, "cookie" },
137   { 0x0A, "cookie2" },
138   { 0x0B, "host" },
139   { 0x0C, "pragma" },
140   { 0x0D, "referer" },
141   { 0x0E, "user-agent" },
142   { 0, NULL}
143 };
144
145
146 static const value_string rsp_header_codes[] = {
147   { 0x01, "Content-Type" },
148   { 0x02, "Content-Language" },
149   { 0x03, "Content-Length" },
150   { 0x04, "Date" },
151   { 0x05, "Last-Modified" },
152   { 0x06, "Location" },
153   { 0x07, "Set-Cookie" },
154   { 0x08, "Set-Cookie2" },
155   { 0x09, "Servlet-Engine" },
156   { 0x0A, "Status" },
157   { 0x0B, "WWW-Authenticate" },
158   { 0, NULL}
159 };
160
161
162 static const value_string mtype_codes[] = {
163   { 0, "BAD" },
164   { 1, "BAD" },
165   { 2, "FORWARD REQUEST" },
166   { 3, "SEND BODY CHUNK" },
167   { 4, "SEND HEADERS" },
168   { 5, "END RESPONSE" },
169   { 6, "GET BODY CHUNK" },
170   { 7, "SHUTDOWN" },
171   { 0, NULL }
172 };
173
174
175 static const value_string http_method_codes[] = {
176   { 1, "OPTIONS" },
177   { 2, "GET" },
178   { 3, "HEAD" },
179   { 4, "POST" },
180   { 5, "PUT" },
181   { 6, "DELETE" },
182   { 7, "TRACE" },
183   { 8, "PROPFIND" },
184   { 9, "PROPPATCH" },
185   { 10, "MKCOL" },
186   { 11, "COPY" },
187   { 12, "MOVE" },
188   { 13, "LOCK" },
189   { 14, "UNLOCK" },
190   { 15, "ACL" },
191   { 16, "REPORT" },
192   { 17, "VERSION-CONTROL" },
193   { 18, "CHECKIN" },
194   { 19, "CHECKOUT" },
195   { 20, "UNCHECKOUT" },
196   { 21, "SEARCH" },
197   { 0, NULL }
198 };
199
200
201
202 static int proto_ajp13     = -1;
203 static int hf_ajp13_magic  = -1;
204 static int hf_ajp13_len    = -1;
205 static int hf_ajp13_code   = -1;
206 static int hf_ajp13_method = -1;
207 static int hf_ajp13_ver    = -1;
208 static int hf_ajp13_uri    = -1;
209 static int hf_ajp13_raddr  = -1;
210 static int hf_ajp13_rhost  = -1;
211 static int hf_ajp13_srv    = -1;
212 static int hf_ajp13_port   = -1;
213 static int hf_ajp13_sslp   = -1;
214 static int hf_ajp13_nhdr   = -1;
215 static int hf_ajp13_hname  = -1;
216 static int hf_ajp13_hval   = -1;
217 static int hf_ajp13_rlen   = -1;
218 static int hf_ajp13_reusep = -1;
219 static int hf_ajp13_rstatus= -1;
220 static int hf_ajp13_rsmsg  = -1;
221 static int hf_ajp13_data   = -1;
222 static gint ett_ajp13 = -1;
223
224
225 typedef struct ajp13_conv_data {
226   int content_length;
227   gboolean was_get_body_chunk;  /* XXX - not used */
228 } ajp13_conv_data;
229
230 typedef struct ajp13_frame_data {
231   gboolean is_request_body;
232 } ajp13_frame_data;
233
234 /* ajp13, in sort of a belt-and-suspenders move, encodes strings with
235  * both a leading length field, and a trailing null. Mostly, see
236  * AJPv13.html. The returned length _includes_ the trailing null, if
237  * there is one.
238  *
239  * XXX - is there a tvbuff routine to handle this?
240  */
241 static guint16
242 get_nstring(tvbuff_t *tvb, gint offset, guint8* cbuf, size_t cbuflen)
243 {
244   guint16 len;
245   guint16 copylen;
246
247   len = tvb_get_ntohs(tvb, offset);
248
249   if (len == 0xffff) {
250     cbuf[0] = '\0';
251     len = 0;
252   } else {
253     copylen = len;
254     if (copylen > cbuflen - 1)
255       copylen = cbuflen - 1;
256     tvb_memcpy(tvb, cbuf, offset+2, copylen);
257     cbuf[copylen] = '\0';
258     len++;
259   }
260   return len;
261 }
262
263
264
265 /* dissect a response. more work to do here.
266  */
267 static void
268 display_rsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ajp13_tree)
269 {
270   const gchar* msg_code = NULL;
271   int pos = 0;
272   guint8 mcode = 0;
273   char *mcode_buf;
274   int i;
275
276   /* MAGIC
277    */
278   if (ajp13_tree)
279     proto_tree_add_item(ajp13_tree, hf_ajp13_magic, tvb, pos, 2, 0);
280   pos+=2;
281
282   /* PDU LENGTH
283    */
284   if (ajp13_tree)
285     proto_tree_add_item(ajp13_tree, hf_ajp13_len,   tvb, pos, 2, 0);
286   pos+=2;
287
288   /* MESSAGE TYPE CODE
289    */
290   mcode = tvb_get_guint8(tvb, pos);
291   msg_code = val_to_str(mcode, mtype_codes, "UNKNOWN");
292   mcode_buf=ep_alloc(32);
293   g_snprintf(mcode_buf, 32, "(%d) %s", mcode, msg_code);
294   if (ajp13_tree)
295     proto_tree_add_string(ajp13_tree, hf_ajp13_code, tvb, pos, 1, mcode_buf);
296   pos+=1;
297
298   if(check_col(pinfo->cinfo, COL_INFO))
299     col_append_str(pinfo->cinfo, COL_INFO, msg_code);
300
301   if (mcode == 5) {
302     if (ajp13_tree)
303       proto_tree_add_item(ajp13_tree, hf_ajp13_reusep, tvb, pos, 1, 0);
304     pos+=1;
305
306   } else if (mcode == 4) {
307
308     guint8 rsmsg_bytes[8*1024]; /* DANGER WILL ROBINSON */
309     guint16 rsmsg_len;
310     guint16 nhdr;
311     guint16 rcode_num;
312
313     /* HTTP RESPONSE STATUS CODE
314      */
315     rcode_num = tvb_get_ntohs(tvb, pos);
316     if (ajp13_tree)
317       proto_tree_add_item(ajp13_tree, hf_ajp13_rstatus, tvb, pos, 2, 0);
318     pos+=2;
319     if(check_col(pinfo->cinfo, COL_INFO))
320       col_append_fstr(pinfo->cinfo, COL_INFO, ":%d", rcode_num);
321
322     /* HTTP RESPONSE STATUS MESSAGE
323      */
324     rsmsg_len = get_nstring(tvb, pos, rsmsg_bytes, sizeof rsmsg_bytes);
325     pos+=2;
326     if (ajp13_tree)
327       proto_tree_add_item(ajp13_tree, hf_ajp13_rsmsg, tvb, pos, rsmsg_len, 0);
328     pos+=rsmsg_len;
329     /* dangerous assumption that we can just %s out raw bytes */
330     if(check_col(pinfo->cinfo, COL_INFO))
331       col_append_fstr(pinfo->cinfo, COL_INFO, " %s", rsmsg_bytes);
332
333     /* NUMBER OF HEADERS
334      */
335     nhdr = tvb_get_ntohs(tvb, pos);
336     if (ajp13_tree)
337       proto_tree_add_item(ajp13_tree, hf_ajp13_nhdr, tvb, pos, 2, 0);
338     pos+=2;
339
340     /* HEADERS
341      */
342     for(i=0; i<nhdr; i++) {
343
344       guint8 hcd;
345       guint8 hid;
346       char hval[8192];
347       guint16 hval_len;
348       int orig_pos = pos;
349       const gchar* hname = NULL;
350       int dp = 0;
351       int cl = 0;
352       guint8 hname_bytes[1024];
353
354       /* HEADER CODE/NAME
355        */
356       hcd = tvb_get_guint8(tvb, pos);
357
358       if (hcd == 0xA0) {
359         pos+=1;
360         hid = tvb_get_guint8(tvb, pos);
361         pos+=1;
362
363         hname = val_to_str(hid, rsp_header_codes, "UNKNOWN");
364         /* Content-Length header (encoded by 0x08) is special */
365         if (hid == 0x08)
366           cl = 1;
367       } else {
368         int hname_len = get_nstring(tvb, pos, hname_bytes, sizeof hname_bytes);
369
370         pos+=hname_len+2;
371         hname = (gchar*)hname_bytes; /* VERY EVIL */
372       }
373
374       dp = pos-orig_pos;
375
376       /* HEADER VALUE
377        */
378       orig_pos = pos;
379       hval_len = get_nstring(tvb, pos, hval, sizeof hval);
380
381       pos+=hval_len+2;
382       dp = pos - orig_pos;
383       if (ajp13_tree) {
384         gchar *hname_value;
385         hname_value=ep_alloc(512);
386         g_snprintf(hname_value, 512, "%s : %s", hname, hval);
387         proto_tree_add_string(ajp13_tree, hf_ajp13_hval, tvb, orig_pos, dp, hname_value);
388       }
389     }
390
391   } else if (mcode == 6) {
392     if (ajp13_tree)
393       proto_tree_add_item(ajp13_tree, hf_ajp13_rlen, tvb, pos, 2, 0);
394     pos+=2;
395
396   } else {
397     /* MESSAGE DATA (COPOUT)
398      */
399     if (ajp13_tree)
400       proto_tree_add_item(ajp13_tree, hf_ajp13_data,  tvb, pos+2, -1, 0);
401   }
402 }
403
404
405
406 /* dissect a request body. see AJPv13.html, but the idea is that these
407  * packets, unlike all other packets, have no type field. you just
408  * sort of have to know that they're coming based on the previous
409  * packets.
410  */
411 static void
412 display_req_body(tvbuff_t *tvb, proto_tree *ajp13_tree)
413 {
414   /*printf("ajp13:display_req_body()\n");*/
415
416   if (ajp13_tree) {
417
418     guint8 body_bytes[128*1024]; /* DANGER WILL ROBINSON */
419     int pos = 0;
420     guint16 body_len;
421
422     /* MAGIC
423      */
424     proto_tree_add_item(ajp13_tree, hf_ajp13_magic, tvb, pos, 2, 0);
425     pos+=2;
426
427     /* PACKET LENGTH
428      */
429     proto_tree_add_item(ajp13_tree, hf_ajp13_len, tvb, pos, 2, 0);
430     pos+=2;
431
432     /* BODY (AS STRING)
433      */
434     body_len = get_nstring(tvb, pos, body_bytes, sizeof body_bytes);
435     proto_tree_add_item(ajp13_tree, hf_ajp13_data, tvb, pos+2, body_len-1, 0);
436   }
437 }
438
439
440
441 /* note that even if ajp13_tree is null on the first pass, we still
442  * need to dissect the packet in order to determine if there is a
443  * content-length, and thus if there is a subsequent automatic
444  * request-body transmitted in the next request packet. if there is a
445  * content-length, we record the fact in the conversation context.
446  * ref the top of this file for comments explaining the multi-pass
447  * thing.
448 */
449 static void
450 display_req_forward(tvbuff_t *tvb, packet_info *pinfo,
451                     proto_tree *ajp13_tree,
452                     ajp13_conv_data* cd)
453 {
454   int pos = 0;
455   guint8 meth;
456   guint8 cod;
457   guint8 ver[1024];
458   guint16 ver_len;
459   guint8 uri[4096];
460   guint16 uri_len;
461   guint8 raddr[4096];
462   guint16 raddr_len;
463   guint8 rhost[4096];
464   guint16 rhost_len;
465   guint8 srv[4096];
466   guint16 srv_len;
467   guint nhdr;
468   guint i;
469
470   if (ajp13_tree)
471     proto_tree_add_item(ajp13_tree, hf_ajp13_magic, tvb, pos, 2, 0);
472   pos+=2;
473
474   if (ajp13_tree)
475     proto_tree_add_item(ajp13_tree, hf_ajp13_len, tvb, pos, 2, 0);
476   pos+=2;
477
478   /* PACKET CODE
479    */
480   cod = tvb_get_guint8(tvb, 4);
481   if (ajp13_tree) {
482     const gchar* msg_code = NULL;
483     char *mcode_buf;
484     msg_code = val_to_str(cod, mtype_codes, "UNKNOWN");
485     mcode_buf=ep_alloc(32);
486     g_snprintf(mcode_buf, 32, "(%d) %s", cod, msg_code);
487     proto_tree_add_string(ajp13_tree, hf_ajp13_code, tvb, pos, 1, mcode_buf);
488   }
489   pos+=1;
490
491   /* HTTP METHOD (ENCODED AS INTEGER)
492    */
493   {
494     const gchar* meth_code = NULL;
495     meth = tvb_get_guint8(tvb, pos);
496     meth_code = val_to_str(meth, http_method_codes, "UNKNOWN");
497     if (ajp13_tree) {
498       char *mcode_buf;
499       mcode_buf=ep_alloc(32);
500       g_snprintf(mcode_buf, 32, "(%d) %s", meth, meth_code);
501       proto_tree_add_string(ajp13_tree, hf_ajp13_method, tvb, pos, 1, mcode_buf);
502     }
503     if(check_col(pinfo->cinfo, COL_INFO))
504       col_append_str(pinfo->cinfo, COL_INFO, meth_code);
505     pos+=1;
506   }
507
508   /* HTTP VERSION STRING
509    */
510   ver_len = get_nstring(tvb, pos, ver, sizeof ver);
511   pos+=2; /* skip over size */
512   if (ajp13_tree)
513     proto_tree_add_item(ajp13_tree, hf_ajp13_ver, tvb, pos, ver_len, 0);
514   pos=pos+ver_len;  /* skip over chars + trailing null */
515
516   /* URI
517    */
518   uri_len = get_nstring(tvb, pos, uri, sizeof uri);
519   pos+=2; /* skip over size */
520   if (ajp13_tree)
521     proto_tree_add_item(ajp13_tree, hf_ajp13_uri, tvb, pos, uri_len, 0);
522   pos=pos+uri_len;  /* skip over chars + trailing null */
523
524
525   if(check_col(pinfo->cinfo, COL_INFO))
526     col_append_fstr(pinfo->cinfo, COL_INFO, " %s %s", uri, ver);
527
528
529   /* REMOTE ADDRESS
530    */
531   raddr_len = get_nstring(tvb, pos, raddr, sizeof raddr);
532   pos+=2; /* skip over size */
533   if (ajp13_tree)
534     proto_tree_add_item(ajp13_tree, hf_ajp13_raddr, tvb, pos, raddr_len, 0);
535   pos=pos+raddr_len;  /* skip over chars + trailing null */
536
537   /* REMOTE HOST
538    */
539   rhost_len = get_nstring(tvb, pos, rhost, sizeof rhost);
540   pos+=2; /* skip over size */
541   if (ajp13_tree)
542     proto_tree_add_item(ajp13_tree, hf_ajp13_rhost, tvb, pos, rhost_len, 0);
543   pos=pos+rhost_len;  /* skip over chars + trailing null */
544
545   /* SERVER NAME
546    */
547   srv_len = get_nstring(tvb, pos, srv, sizeof srv);
548   pos+=2; /* skip over size */
549   if (ajp13_tree)
550     proto_tree_add_item(ajp13_tree, hf_ajp13_srv, tvb, pos, srv_len, 0);
551   pos=pos+srv_len;  /* skip over chars + trailing null */
552
553   /* SERVER PORT
554    */
555   if (ajp13_tree)
556     proto_tree_add_item(ajp13_tree, hf_ajp13_port, tvb, pos, 2, 0);
557   pos+=2;
558
559   /* IS SSL?
560    */
561   if (ajp13_tree)
562     proto_tree_add_item(ajp13_tree, hf_ajp13_sslp, tvb, pos, 1, 0);
563   pos+=1;
564
565   /* NUM HEADERS
566    */
567   nhdr = tvb_get_ntohs(tvb, pos);
568
569   if (ajp13_tree)
570     proto_tree_add_item(ajp13_tree, hf_ajp13_nhdr, tvb, pos, 2, 0);
571   pos+=2;
572   cd->content_length = 0;
573
574   /* HEADERS
575    */
576   for(i=0; i<nhdr; i++) {
577
578     guint8 hcd;
579     guint8 hid;
580     int orig_pos = pos;
581     const gchar* hname = NULL;
582     int dp = 0;
583     int cl = 0;
584     char *hval;
585     guint16 hval_len;
586
587     /* HEADER CODE/NAME
588      */
589     hcd = tvb_get_guint8(tvb, pos);
590
591     if (hcd == 0xA0) {
592       pos+=1;
593       hid = tvb_get_guint8(tvb, pos);
594       pos+=1;
595
596       hname = val_to_str(hid, req_header_codes, "UNKNOWN");
597       if (hid == 0x08)
598         cl = 1;
599     } else {
600       guint8 *hname_bytes;
601       int hname_len;
602
603       hname_bytes=ep_alloc(1024);
604       hname_len = get_nstring(tvb, pos, hname_bytes, 1024);
605       pos+=hname_len+2;
606       hname = (gchar*)hname_bytes;
607     }
608
609     dp = pos-orig_pos;
610
611     /* HEADER VALUE
612      */
613     orig_pos = pos;
614     hval=ep_alloc(8192);
615     hval_len = get_nstring(tvb, pos, hval, 8192);
616
617     pos+=hval_len+2;
618     dp = pos - orig_pos;
619     if (ajp13_tree) {
620       proto_tree_add_string_format(ajp13_tree, hf_ajp13_hval,
621                                    tvb, orig_pos, dp, hname,
622                                    "%s: %s", hname, hval);
623     }
624     if (cl) {
625       cl = atoi(hval);
626       cd->content_length = cl;
627     }
628   }
629 }
630
631
632
633 /* main dissector function. ethereal calls it for segments in both
634  * directions.
635  */
636 static void
637 dissect_ajp13_tcp_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
638 {
639   guint16 mag;
640   guint16 len;
641   conversation_t *conv = NULL;
642   ajp13_conv_data *cd = NULL;
643   proto_tree *ajp13_tree = NULL;
644   ajp13_frame_data* fd = NULL;
645
646   /* conversational state really only does us good during the first
647    * in-order traversal
648    */
649   conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
650                            pinfo->srcport, pinfo->destport, 0);
651   if (!conv) {
652     conv = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype,
653                             pinfo->srcport, pinfo->destport, 0);
654   }
655   cd = (ajp13_conv_data*)conversation_get_proto_data(conv, proto_ajp13);
656   if (!cd) {
657     cd = se_alloc(sizeof(ajp13_conv_data));
658     cd->content_length = 0;
659     cd->was_get_body_chunk = FALSE;
660     conversation_add_proto_data(conv, proto_ajp13, cd);
661   }
662
663   /* we use the per segment user data to record the conversational
664    * state for use later on when we're called out of order (see
665    * comments at top of this file)
666    */
667   fd = (ajp13_frame_data*)p_get_proto_data(pinfo->fd, proto_ajp13);
668   if (!fd) {
669     /*printf("ajp13:dissect_ajp13_common():no frame data, adding");*/
670     /* since there's no per-packet user data, this must be the first
671      * time we've see the packet, and it must be the first "in order"
672      * pass through the data.
673      */
674     fd = se_alloc(sizeof(ajp13_frame_data));
675     p_add_proto_data(pinfo->fd, proto_ajp13, fd);
676     fd->is_request_body = FALSE;
677     if (cd->content_length) {
678       /* this is screwy, see AJPv13.html. the idea is that if the
679        * request has a body (as determined by the content-length
680        * header), then there's always an immediate follow-up PDU with
681        * no GET_BODY_CHUNK from the container.
682        */
683       fd->is_request_body = TRUE;
684     }
685   }
686
687   if (check_col(pinfo->cinfo, COL_INFO))
688     col_clear(pinfo->cinfo, COL_INFO);
689
690   mag = tvb_get_ntohs(tvb, 0);
691   len = tvb_get_ntohs(tvb, 2);
692
693   if (check_col(pinfo->cinfo, COL_PROTOCOL))
694     col_set_str(pinfo->cinfo, COL_PROTOCOL, "AJP13");
695   if (check_col(pinfo->cinfo, COL_INFO)) {
696     if (mag == 0x1234 && !fd->is_request_body)
697       col_append_fstr(pinfo->cinfo, COL_INFO, "%d:REQ:", conv->index);
698     else if (mag == 0x1234 && fd->is_request_body)
699       col_append_fstr(pinfo->cinfo, COL_INFO, "%d:REQ:Body", conv->index);
700     else if (mag == 0x4142)
701       col_append_fstr(pinfo->cinfo, COL_INFO, "%d:RSP:", conv->index);
702     else
703       col_set_str(pinfo->cinfo, COL_INFO, "AJP13 Error?");
704   }
705
706   if (tree) {
707     proto_item *ti;
708     ti = proto_tree_add_item(tree, proto_ajp13, tvb, 0, tvb_length(tvb), FALSE);
709     ajp13_tree = proto_item_add_subtree(ti, ett_ajp13);
710   }
711
712   if (mag == 0x1234) {
713
714     if (fd->is_request_body)
715       display_req_body(tvb, ajp13_tree);
716     else
717       display_req_forward(tvb, pinfo, ajp13_tree, cd);
718
719   } else if (mag == 0x4142) {
720
721     display_rsp(tvb, pinfo, ajp13_tree);
722
723   }
724 }
725
726
727
728 /* given the first chunk of the AJP13 pdu, extract out and return the
729  * packet length. see comments in packet-tcp.c:tcp_dissect_pdus().
730  */
731 static guint
732 get_ajp13_pdu_len(tvbuff_t *tvb, int offset)
733 {
734   guint16 magic;
735   guint16 plen;
736   magic = tvb_get_ntohs(tvb, offset);
737   plen = tvb_get_ntohs(tvb, offset+2);
738   plen += 4;
739   return plen;
740 }
741
742
743
744 /* Code to actually dissect the packets.
745  */
746 static void
747 dissect_ajp13(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
748 {
749   /* Set up structures needed to add the protocol subtree and manage it
750    */
751   tcp_dissect_pdus(tvb, pinfo, tree,
752                    TRUE,                   /* desegment or not   */
753                    4,                      /* magic + length */
754                    get_ajp13_pdu_len,      /* use first 4, calc data len */
755                    dissect_ajp13_tcp_pdu); /* the naive dissector */
756 }
757
758
759
760 void
761 proto_register_ajp13(void)
762 {
763   static hf_register_info hf[] = {
764     { &hf_ajp13_magic,
765       { "Magic",  "ajp13.magic", FT_BYTES, BASE_HEX, NULL, 0x0, "Magic Number",
766         HFILL }
767     },
768     { &hf_ajp13_len,
769       { "Length",  "ajp13.len", FT_UINT16, BASE_DEC, NULL, 0x0, "Data Length",
770         HFILL }
771     },
772     { &hf_ajp13_code,
773       { "Code",  "ajp13.code", FT_STRING, BASE_DEC, NULL, 0x0, "Type Code",
774          HFILL }
775     },
776     { &hf_ajp13_method,
777       { "Method",  "ajp13.method", FT_STRING, BASE_DEC, NULL, 0x0, "HTTP Method",
778         HFILL }
779     },
780     { &hf_ajp13_ver,
781       { "Version",  "ajp13.ver", FT_STRING, BASE_DEC, NULL, 0x0, "HTTP Version",
782         HFILL }
783     },
784     { &hf_ajp13_uri,
785       { "URI",  "ajp13.uri", FT_STRING, BASE_DEC, NULL, 0x0, "HTTP URI",
786         HFILL }
787     },
788     { &hf_ajp13_raddr,
789       { "RADDR",  "ajp13.raddr", FT_STRING, BASE_DEC, NULL, 0x0, "Remote Address",
790         HFILL }
791     },
792     { &hf_ajp13_rhost,
793       { "RHOST",  "ajp13.rhost", FT_STRING, BASE_DEC, NULL, 0x0, "Remote Host",
794         HFILL }
795     },
796     { &hf_ajp13_srv,
797       { "SRV",  "ajp13.srv", FT_STRING, BASE_DEC, NULL, 0x0, "Server",
798         HFILL }
799     },
800     { &hf_ajp13_port,
801       { "PORT",  "ajp13.port", FT_UINT16, BASE_DEC, NULL, 0x0, "Port",
802         HFILL }
803     },
804     { &hf_ajp13_sslp,
805       { "SSLP",  "ajp13.sslp", FT_UINT8, BASE_DEC, NULL, 0x0, "Is SSL?",
806         HFILL }
807     },
808     { &hf_ajp13_nhdr,
809       { "NHDR",  "ajp13.nhdr", FT_UINT16, BASE_DEC, NULL, 0x0, "Num Headers",
810         HFILL }
811     },
812     { &hf_ajp13_hname,
813       { "HNAME",  "ajp13.hname", FT_STRING, BASE_DEC, NULL, 0x0, "Header Name",
814         HFILL }
815     },
816     { &hf_ajp13_hval,
817       { "HVAL",  "ajp13.hval", FT_STRING, BASE_DEC, NULL, 0x0, "Header Value",
818         HFILL }
819     },
820     { &hf_ajp13_rlen,
821       { "RLEN",  "ajp13.rlen", FT_UINT16, BASE_DEC, NULL, 0x0, "Requested Length",
822         HFILL }
823     },
824     { &hf_ajp13_reusep,
825       { "REUSEP",  "ajp13.reusep", FT_UINT8, BASE_DEC, NULL, 0x0, "Reuse Connection?",
826         HFILL }
827     },
828     { &hf_ajp13_rstatus,
829       { "RSTATUS",  "ajp13.rstatus", FT_UINT16, BASE_DEC, NULL, 0x0, "HTTP Status Code",
830         HFILL }
831     },
832     { &hf_ajp13_rsmsg,
833       { "RSMSG",  "ajp13.rmsg", FT_STRING, BASE_DEC, NULL, 0x0, "HTTP Status Message",
834         HFILL }
835     },
836     { &hf_ajp13_data,
837       { "Data",  "ajp13.data", FT_STRING, BASE_DEC, NULL, 0x0, "Data",
838         HFILL }
839     },
840   };
841
842   static gint *ett[] = {
843     &ett_ajp13,
844   };
845
846   /* Register the protocol name and description
847    */
848   proto_ajp13 = proto_register_protocol("Apache JServ Protocol v1.3", "AJP13", "ajp13");
849
850   proto_register_field_array(proto_ajp13, hf, array_length(hf));
851   proto_register_subtree_array(ett, array_length(ett));
852
853 }
854
855
856
857 void
858 proto_reg_handoff_ajp13(void)
859 {
860   dissector_handle_t ajp13_handle;
861   ajp13_handle = create_dissector_handle(dissect_ajp13, proto_ajp13);
862   dissector_add("tcp.port", 8009, ajp13_handle);
863 }