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