1 /* Copyright 1999-2004 The Apache Software Foundation
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 static char *hex_table = "0123456789ABCDEF";
22 * Dump up to the first 1024 bytes on an AJP Message
24 * @param msg AJP Message to dump
25 * @param err error string to display
26 * @return APR_SUCCESS or error
28 apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err)
34 apr_size_t len = msg->len;
36 /* Display only first 1024 bytes */
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);
44 for (i = 0; i < len; i += 16) {
47 for (j = 0; j < 16; j++) {
50 *current++ = hex_table[x >> 4];
51 *current++ = hex_table[x & 0x0f];
57 for (j = 0; j < 16; j++) {
60 if (x > 0x20 && x < 0x7F) {
70 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
71 "ajp_msg_dump(): %.4x %s",
80 * Check a new AJP Message by looking at signature and return its size
82 * @param msg AJP Message to check
83 * @param len Pointer to returned len
84 * @return APR_SUCCESS or error
86 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
88 apr_byte_t *head = msg->buf;
91 if (!((head[0] == 0x41 && head[1] == 0x42) ||
92 (head[0] == 0x12 && head[1] == 0x34))) {
94 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
95 "ajp_check_msg_header() got bad signature %x%x",
98 return AJP_EBAD_SIGNATURE;
101 msglen = ((head[2] & 0xff) << 8);
102 msglen += (head[3] & 0xFF);
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);
111 msg->len = msglen + AJP_HEADER_LEN;
112 msg->pos = AJP_HEADER_LEN;
119 * Reset an AJP Message
121 * @param msg AJP Message to reset
122 * @return APR_SUCCESS or error
124 apr_status_t ajp_msg_reset(ajp_msg_t *msg)
126 msg->len = AJP_HEADER_LEN;
127 msg->pos = AJP_HEADER_LEN;
133 * Mark the end of an AJP Message
135 * @param msg AJP Message to end
136 * @return APR_SUCCESS or error
138 apr_status_t ajp_msg_end(ajp_msg_t *msg)
140 apr_size_t len = msg->len - AJP_HEADER_LEN;
142 if (msg->server_side) {
151 msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF);
152 msg->buf[3] = (apr_byte_t)(len & 0xFF);
158 * Add an unsigned 32bits value to AJP Message
160 * @param msg AJP Message to get value from
161 * @param value value to add to AJP Message
162 * @return APR_SUCCESS or error
164 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value)
166 apr_size_t len = msg->len;
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",
172 return AJP_EOVERFLOW;
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);
186 * Add an unsigned 16bits value to AJP Message
188 * @param msg AJP Message to get value from
189 * @param value value to add to AJP Message
190 * @return APR_SUCCESS or error
192 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value)
194 apr_size_t len = msg->len;
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",
200 return AJP_EOVERFLOW;
203 msg->buf[len] = (apr_byte_t)((value >> 8) & 0xFF);
204 msg->buf[len + 1] = (apr_byte_t)(value & 0xFF);
212 * Add an unsigned 8bits value to AJP Message
214 * @param msg AJP Message to get value from
215 * @param value value to add to AJP Message
216 * @return APR_SUCCESS or error
218 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
220 apr_size_t len = msg->len;
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",
226 return AJP_EOVERFLOW;
229 msg->buf[len] = value;
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
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
244 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
250 return(ajp_msg_append_uint16(msg, 0xFFFF));
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",
258 return AJP_EOVERFLOW;
261 /* ignore error - we checked once */
262 ajp_msg_append_uint16(msg, (apr_uint16_t)len);
264 /* We checked for space !! */
265 memcpy(msg->buf + msg->len, value, len + 1); /* including \0 */
267 if (convert) /* convert from EBCDIC if needed */
268 ajp_xlate_to_ascii((char *)msg->buf + msg->len, len + 1);
276 * Add a Byte array to AJP Message
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
283 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
287 return APR_SUCCESS; /* Shouldn't we indicate an error ? */
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",
294 return AJP_EOVERFLOW;
297 /* We checked for space !! */
298 memcpy(msg->buf + msg->len, value, valuelen);
299 msg->len += valuelen;
305 * Get a 32bits unsigned value from AJP Message
307 * @param msg AJP Message to get value from
308 * @param rvalue Pointer where value will be returned
309 * @return APR_SUCCESS or error
311 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
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",
320 return AJP_EOVERFLOW;
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));
334 * Get a 16bits unsigned value from AJP Message
336 * @param msg AJP Message to get value from
337 * @param rvalue Pointer where value will be returned
338 * @return APR_SUCCESS or error
340 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
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",
349 return AJP_EOVERFLOW;
352 value = ((msg->buf[(msg->pos++)] & 0xFF) << 8);
353 value += ((msg->buf[(msg->pos++)] & 0xFF));
360 * Peek a 16bits unsigned value from AJP Message, position in message
363 * @param msg AJP Message to get value from
364 * @param rvalue Pointer where value will be returned
365 * @return APR_SUCCESS or error
367 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
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",
376 return AJP_EOVERFLOW;
379 value = ((msg->buf[(msg->pos)] & 0xFF) << 8);
380 value += ((msg->buf[(msg->pos + 1)] & 0xFF));
387 * Peek a 8bits unsigned value from AJP Message, position in message
390 * @param msg AJP Message to get value from
391 * @param rvalue Pointer where value will be returned
392 * @return APR_SUCCESS or error
394 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
396 if (msg->pos > msg->len) {
397 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
398 "ajp_msg_peek_uint8(): BufferOverflowException %d %d",
401 return AJP_EOVERFLOW;
404 *rvalue = msg->buf[msg->pos];
409 * Get a 8bits unsigned value from AJP Message
411 * @param msg AJP Message to get value from
412 * @param rvalue Pointer where value will be returned
413 * @return APR_SUCCESS or error
415 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
418 if (msg->pos > msg->len) {
419 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
420 "ajp_msg_get_uint8(): BufferOverflowException %d %d",
423 return AJP_EOVERFLOW;
426 *rvalue = msg->buf[msg->pos++];
432 * Get a String value from AJP Message
434 * @param msg AJP Message to get value from
435 * @param rvalue Pointer where value will be returned
436 * @return APR_SUCCESS or error
438 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue)
444 status = ajp_msg_get_uint16(msg, &size);
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",
452 return AJP_EOVERFLOW;
455 msg->pos += (apr_size_t)size;
456 msg->pos++; /* a String in AJP is NULL terminated */
458 *rvalue = (char *)(msg->buf + start);
464 * Get a Byte array from AJP Message
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
471 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
472 apr_size_t *rvalue_len)
478 status = ajp_msg_get_uint16(msg, &size);
479 /* save the current position */
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",
486 return AJP_EOVERFLOW;
488 msg->pos += (apr_size_t)size; /* only bytes, no trailer */
490 *rvalue = msg->buf + start;
498 * Create an AJP Message from pool
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
504 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg)
506 ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t));
509 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
510 "ajp_msg_create(): can't allocate AJP message memory");
514 msg->server_side = 0;
516 msg->buf = (apr_byte_t *)apr_palloc(pool, AJP_MSG_BUFFER_SZ);
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.
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");
531 msg->header_len = AJP_HEADER_LEN;
538 * Recopy an AJP Message to another
540 * @param smsg source AJP message
541 * @param dmsg destination AJP message
542 * @return APR_SUCCESS or error
544 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg)
547 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
548 "ajp_msg_copy(): destination msg is null");
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);
559 memcpy(dmsg->buf, smsg->buf, smsg->len);
560 dmsg->len = smsg->len;
561 dmsg->pos = smsg->pos;
568 * Serialize in an AJP Message a PING command
570 * +-----------------------+
571 * | PING CMD (1 byte) |
572 * +-----------------------+
574 * @param smsg AJP message to put serialized message
575 * @return APR_SUCCESS or error
577 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg)
582 if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_PING)) != APR_SUCCESS)
589 * Serialize in an AJP Message a CPING command
591 * +-----------------------+
592 * | CPING CMD (1 byte) |
593 * +-----------------------+
595 * @param smsg AJP message to put serialized message
596 * @return APR_SUCCESS or error
598 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg)
603 if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_CPING)) != APR_SUCCESS)