]> granicus.if.org Git - apache/blob - modules/proxy/ajp_msg.c
jakarta-tomcat-connectors AJP files.
[apache] / modules / proxy / ajp_msg.c
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "ajp.h"
17
18
19 static char *hex_table = "0123456789ABCDEF";
20
21 /**
22  * Dump up to the first 1024 bytes on an AJP Message
23  *
24  * @param msg       AJP Message to dump
25  * @param err       error string to display
26  * @return          APR_SUCCESS or error
27  */
28 apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err)
29 {
30     apr_size_t  i, j;
31     char        line[80];
32     char        *current;
33     apr_byte_t  x;
34     apr_size_t  len = msg->len;
35
36     /* Display only first 1024 bytes */
37     if (len > 1024)
38         len = 1024;
39
40     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
41                   "ajp_msg_dump(): %s pos=%d len=%d max=%d",
42                   err, msg->pos, msg->len, AJP_MSG_BUFFER_SZ);
43
44     for (i = 0; i < len; i += 16) {
45         current = line;
46
47         for (j = 0; j < 16; j++) {
48              x = msg->buf[i + j];
49
50             *current++ = hex_table[x >> 4];
51             *current++ = hex_table[x & 0x0f];
52             *current++ = ' ';
53         }
54         *current++ = ' ';
55         *current++ = '-';
56         *current++ = ' ';
57         for (j = 0; j < 16; j++) {
58             x = msg->buf[i + j];
59
60             if (x > 0x20 && x < 0x7F) {
61                 *current++ = x;
62             }
63             else {
64                 *current++ = '.';
65             }
66         }
67
68         *current++ = '\0';
69
70         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
71                       "ajp_msg_dump(): %.4x    %s",
72                       i, line);
73     }
74     
75     return APR_SUCCESS;
76 }
77
78
79 /**
80  * Check a new AJP Message by looking at signature and return its size
81  *
82  * @param msg       AJP Message to check
83  * @param len       Pointer to returned len
84  * @return          APR_SUCCESS or error
85  */
86 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
87 {
88     apr_byte_t *head = msg->buf;
89     apr_size_t msglen;
90
91     if (!((head[0] == 0x41 && head[1] == 0x42) ||
92           (head[0] == 0x12 && head[1] == 0x34))) {
93
94         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
95                       "ajp_check_msg_header() got bad signature %x%x",
96                       head[0], head[1]);
97
98         return AJP_EBAD_SIGNATURE;
99     }
100
101     msglen  = ((head[2] & 0xff) << 8);
102     msglen += (head[3] & 0xFF);
103
104     if (msglen > AJP_MSG_BUFFER_SZ) {
105         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
106                       "ajp_check_msg_header() incoming message is too big %d, max is %d",
107                       msglen, AJP_MSG_BUFFER_SZ);
108         return AJP_ETOBIG;
109     }
110
111     msg->len = msglen + AJP_HEADER_LEN;
112     msg->pos = AJP_HEADER_LEN;
113     *len     = msglen;
114     
115     return APR_SUCCESS;
116 }
117
118 /**
119  * Reset an AJP Message
120  *
121  * @param msg       AJP Message to reset
122  * @return          APR_SUCCESS or error
123  */
124 apr_status_t ajp_msg_reset(ajp_msg_t *msg)
125 {
126     msg->len = AJP_HEADER_LEN;
127     msg->pos = AJP_HEADER_LEN;
128     
129     return APR_SUCCESS;
130 }
131
132 /**
133  * Mark the end of an AJP Message
134  *
135  * @param msg       AJP Message to end
136  * @return          APR_SUCCESS or error
137  */
138 apr_status_t ajp_msg_end(ajp_msg_t *msg)
139 {
140     apr_size_t len = msg->len - AJP_HEADER_LEN;
141
142     if (msg->server_side) {
143         msg->buf[0] = 0x41;
144         msg->buf[1] = 0x42;
145     }
146     else {
147         msg->buf[0] = 0x12;
148         msg->buf[1] = 0x34;
149     }
150
151     msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF);
152     msg->buf[3] = (apr_byte_t)(len & 0xFF);
153     
154     return APR_SUCCESS;
155 }
156
157 /**
158  * Add an unsigned 32bits value to AJP Message
159  *
160  * @param msg       AJP Message to get value from
161  * @param value     value to add to AJP Message
162  * @return          APR_SUCCESS or error
163  */
164 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value)
165 {
166     apr_size_t len = msg->len;
167
168     if ((len + 4) > AJP_MSG_BUFFER_SZ) {
169         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
170                       "ajp_msg_append_uint32(): BufferOverflowException %d %d",
171                       msg->pos, msg->len);
172         return AJP_EOVERFLOW;
173     }
174
175     msg->buf[len]     = (apr_byte_t)((value >> 24) & 0xFF);
176     msg->buf[len + 1] = (apr_byte_t)((value >> 16) & 0xFF);
177     msg->buf[len + 2] = (apr_byte_t)((value >> 8) & 0xFF);
178     msg->buf[len + 3] = (apr_byte_t)(value & 0xFF);
179
180     msg->len += 4;
181
182     return APR_SUCCESS;
183 }
184
185 /**
186  * Add an unsigned 16bits value to AJP Message
187  *
188  * @param msg       AJP Message to get value from
189  * @param value     value to add to AJP Message
190  * @return          APR_SUCCESS or error
191  */
192 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value)
193 {
194     apr_size_t len = msg->len;
195
196     if ((len + 2) > AJP_MSG_BUFFER_SZ) {
197         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
198                       "ajp_msg_append_uint16(): BufferOverflowException %d %d",
199                       msg->pos, msg->len);
200         return AJP_EOVERFLOW;
201     }
202
203     msg->buf[len]     = (apr_byte_t)((value >> 8) & 0xFF);
204     msg->buf[len + 1] = (apr_byte_t)(value & 0xFF);
205
206     msg->len += 2;
207
208     return APR_SUCCESS;
209 }
210
211 /**
212  * Add an unsigned 8bits value to AJP Message
213  *
214  * @param msg       AJP Message to get value from
215  * @param value     value to add to AJP Message
216  * @return          APR_SUCCESS or error
217  */
218 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
219 {
220     apr_size_t len = msg->len;
221
222     if ((len + 1) > AJP_MSG_BUFFER_SZ) {
223         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
224                       "ajp_msg_append_uint8(): BufferOverflowException %d %d",
225                       msg->pos, msg->len);
226         return AJP_EOVERFLOW;
227     }
228
229     msg->buf[len] = value;
230     msg->len += 1;
231
232     return APR_SUCCESS;
233 }
234
235 /**
236  *  Add a String in AJP message, and transform the String in ASCII 
237  *  if convert is set and we're on an EBCDIC machine    
238  *
239  * @param msg       AJP Message to get value from
240  * @param value     Pointer to String
241  * @param convert   When set told to convert String to ASCII
242  * @return          APR_SUCCESS or error
243  */
244 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
245                                       int convert)
246 {
247     size_t len;
248
249     if (value == NULL) {
250         return(ajp_msg_append_uint16(msg, 0xFFFF));
251     }
252
253     len = strlen(value);
254     if ((msg->len + len + 2) > AJP_MSG_BUFFER_SZ) {
255         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
256             "ajp_msg_append_cvt_string(): BufferOverflowException %d %d",
257             msg->pos, msg->len);
258         return AJP_EOVERFLOW;
259     }
260
261     /* ignore error - we checked once */
262     ajp_msg_append_uint16(msg, (apr_uint16_t)len);
263
264     /* We checked for space !!  */
265     memcpy(msg->buf + msg->len, value, len + 1); /* including \0 */
266
267     if (convert)   /* convert from EBCDIC if needed */
268         ajp_xlate_to_ascii((char *)msg->buf + msg->len, len + 1);
269
270     msg->len += len + 1;
271
272     return APR_SUCCESS;
273 }
274
275 /**
276  * Add a Byte array to AJP Message
277  *
278  * @param msg       AJP Message to get value from
279  * @param value     Pointer to Byte array
280  * @param valuelen  Byte array len
281  * @return          APR_SUCCESS or error
282  */
283 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
284                                   apr_size_t valuelen)
285 {
286     if (! valuelen) {
287         return APR_SUCCESS; /* Shouldn't we indicate an error ? */
288     }
289
290     if ((msg->len + valuelen) > AJP_MSG_BUFFER_SZ) {
291         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
292                       "ajp_msg_append_bytes(): BufferOverflowException %d %d",
293                       msg->pos, msg->len);
294         return AJP_EOVERFLOW;
295     }
296
297     /* We checked for space !!  */
298     memcpy(msg->buf + msg->len, value, valuelen);
299     msg->len += valuelen;
300
301     return APR_SUCCESS;
302 }
303
304 /**
305  * Get a 32bits unsigned value from AJP Message
306  *
307  * @param msg       AJP Message to get value from
308  * @param rvalue    Pointer where value will be returned
309  * @return          APR_SUCCESS or error
310  */
311 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
312 {
313     apr_uint32_t value;
314
315     if ((msg->pos + 3) > msg->len) {
316         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
317                       "ajp_msg_get_long(): BufferOverflowException %d %d",
318                       msg->pos, msg->len);
319
320         return AJP_EOVERFLOW;
321     }
322
323     value  = ((msg->buf[(msg->pos++)] & 0xFF) << 24);
324     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 16);
325     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 8);
326     value |= ((msg->buf[(msg->pos++)] & 0xFF));
327     
328     *rvalue = value;
329     return APR_SUCCESS;
330 }
331
332
333 /**
334  * Get a 16bits unsigned value from AJP Message
335  *
336  * @param msg       AJP Message to get value from
337  * @param rvalue    Pointer where value will be returned
338  * @return          APR_SUCCESS or error
339  */
340 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
341 {
342     apr_uint16_t value;
343     
344     if ((msg->pos + 1) > msg->len) {
345         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
346                       "ajp_msg_get_int(): BufferOverflowException %d %d",
347                       msg->pos, msg->len);
348
349         return AJP_EOVERFLOW;
350     }
351
352     value  = ((msg->buf[(msg->pos++)] & 0xFF) << 8);
353     value += ((msg->buf[(msg->pos++)] & 0xFF));
354
355     *rvalue = value;
356     return APR_SUCCESS;
357 }
358
359 /**
360  * Peek a 16bits unsigned value from AJP Message, position in message
361  * is not updated
362  *
363  * @param msg       AJP Message to get value from
364  * @param rvalue    Pointer where value will be returned
365  * @return          APR_SUCCESS or error
366  */
367 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
368 {
369     apr_uint16_t value;
370
371     if ((msg->pos + 1) > msg->len) {
372         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
373                       "ajp_msg_peek_int(): BufferOverflowException %d %d",
374                       msg->pos, msg->len);
375
376         return AJP_EOVERFLOW;
377     }
378     
379     value = ((msg->buf[(msg->pos)] & 0xFF) << 8);
380     value += ((msg->buf[(msg->pos + 1)] & 0xFF));
381     
382     *rvalue = value;
383     return APR_SUCCESS;
384 }
385
386 /**
387  * Peek a 8bits unsigned value from AJP Message, position in message
388  * is not updated
389  *
390  * @param msg       AJP Message to get value from
391  * @param rvalue    Pointer where value will be returned
392  * @return          APR_SUCCESS or error
393  */
394 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
395 {
396     if (msg->pos > msg->len) {
397         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
398                       "ajp_msg_peek_uint8(): BufferOverflowException %d %d",
399                       msg->pos, msg->len);
400
401         return AJP_EOVERFLOW;
402     }
403     
404     *rvalue = msg->buf[msg->pos];
405     return APR_SUCCESS;
406 }
407
408 /**
409  * Get a 8bits unsigned value from AJP Message
410  *
411  * @param msg       AJP Message to get value from
412  * @param rvalue    Pointer where value will be returned
413  * @return          APR_SUCCESS or error
414  */
415 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
416 {
417
418     if (msg->pos > msg->len) {
419         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
420                       "ajp_msg_get_uint8(): BufferOverflowException %d %d",
421                       msg->pos, msg->len);
422
423         return AJP_EOVERFLOW;
424     }
425     
426     *rvalue = msg->buf[msg->pos++];
427     return APR_SUCCESS;
428 }
429
430
431 /**
432  * Get a String value from AJP Message
433  *
434  * @param msg       AJP Message to get value from
435  * @param rvalue    Pointer where value will be returned
436  * @return          APR_SUCCESS or error
437  */
438 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue)
439 {
440     apr_uint16_t size;
441     apr_size_t   start;
442     apr_status_t status;
443        
444     status = ajp_msg_get_uint16(msg, &size);
445     start = msg->pos;
446
447     if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
448         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
449                       "ajp_msg_get_string(): BufferOverflowException %d %d",
450                       msg->pos, msg->len);
451
452         return AJP_EOVERFLOW;
453     }
454
455     msg->pos += (apr_size_t)size;
456     msg->pos++;                   /* a String in AJP is NULL terminated */
457
458     *rvalue = (char *)(msg->buf + start);
459     return APR_SUCCESS;
460 }
461
462
463 /**
464  * Get a Byte array from AJP Message
465  *
466  * @param msg       AJP Message to get value from
467  * @param rvalue    Pointer where value will be returned
468  * @param rvalueLen Pointer where Byte array len will be returned
469  * @return          APR_SUCCESS or error
470  */
471 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
472                                apr_size_t *rvalue_len)
473 {
474     apr_uint16_t size;
475     apr_size_t   start;
476     apr_status_t status;
477
478     status = ajp_msg_get_uint16(msg, &size);
479     /* save the current position */
480     start = msg->pos;
481
482     if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
483         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
484                       "ajp_msg_get_bytes(): BufferOverflowException %d %d",
485                       msg->pos, msg->len);
486         return AJP_EOVERFLOW;
487     }
488     msg->pos += (apr_size_t)size;   /* only bytes, no trailer */
489
490     *rvalue     = msg->buf + start;
491     *rvalue_len = size;
492
493     return APR_SUCCESS;
494 }
495
496
497 /**
498  * Create an AJP Message from pool
499  *
500  * @param pool      memory pool to allocate AJP message from
501  * @param rmsg      Pointer to newly created AJP message
502  * @return          APR_SUCCESS or error
503  */
504 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg)
505 {
506     ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t));
507
508     if (!msg) {
509         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
510                       "ajp_msg_create(): can't allocate AJP message memory");
511         return APR_ENOPOOL;
512     }
513     
514     msg->server_side = 0;
515
516     msg->buf = (apr_byte_t *)apr_palloc(pool, AJP_MSG_BUFFER_SZ);
517     
518     /* XXX: This should never happen
519      * In case if the OS cannont allocate 8K of data
520      * we are in serious trouble
521      * No need to check the alloc return value, cause the
522      * core dump is probably the best solution anyhow.
523      */
524     if (msg->buf == NULL) {
525         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
526                       "ajp_msg_create(): can't allocate AJP message memory");
527         return APR_ENOPOOL;
528     }
529
530     msg->len = 0;
531     msg->header_len = AJP_HEADER_LEN;
532     *rmsg = msg;
533     
534     return APR_SUCCESS;
535 }
536
537 /**
538  * Recopy an AJP Message to another
539  *
540  * @param smsg      source AJP message
541  * @param dmsg      destination AJP message
542  * @return          APR_SUCCESS or error
543  */
544 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg)
545 {
546     if (dmsg == NULL) {
547         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
548                       "ajp_msg_copy(): destination msg is null");
549         return AJP_EINVAL;
550     }
551     
552     if (smsg->len > AJP_MSG_BUFFER_SZ) {
553         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
554             "ajp_msg_copy(): destination buffer too small %d, max size is %d",
555             smsg->len, AJP_MSG_BUFFER_SZ);
556         return  AJP_ETOSMALL;
557     }
558
559     memcpy(dmsg->buf, smsg->buf, smsg->len);
560     dmsg->len = smsg->len;
561     dmsg->pos = smsg->pos;
562
563     return APR_SUCCESS;
564 }
565
566
567 /**
568  * Serialize in an AJP Message a PING command
569  *
570  * +-----------------------+
571  * | PING CMD (1 byte)     |
572  * +-----------------------+
573  *
574  * @param smsg      AJP message to put serialized message
575  * @return          APR_SUCCESS or error
576  */
577 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg)
578 {
579     apr_status_t rc;
580     ajp_msg_reset(msg);
581
582     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_PING)) != APR_SUCCESS)
583         return rc;
584         
585     return APR_SUCCESS;
586 }
587
588 /** 
589  * Serialize in an AJP Message a CPING command
590  *
591  * +-----------------------+
592  * | CPING CMD (1 byte)    |
593  * +-----------------------+
594  *
595  * @param smsg      AJP message to put serialized message
596  * @return          APR_SUCCESS or error
597  */
598 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg)
599 {
600     apr_status_t rc;
601     ajp_msg_reset(msg);
602
603     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_CPING)) != APR_SUCCESS)
604         return rc;
605         
606     return APR_SUCCESS;
607 }