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 pool pool to allocate from
25 * @param msg AJP Message to dump
26 * @param err error string to display
27 * @return dump message
29 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err)
37 apr_size_t len = msg->len;
39 /* Display only first 1024 bytes */
42 rv = apr_palloc(pool, bl);
44 "ajp_msg_dump(): %s pos=%" APR_SIZE_T_FMT
45 " len=%" APR_SIZE_T_FMT " max=%d\n",
46 err, msg->pos, msg->len, AJP_MSG_BUFFER_SZ);
49 for (i = 0; i < len; i += 16) {
52 for (j = 0; j < 16; j++) {
55 *current++ = hex_table[x >> 4];
56 *current++ = hex_table[x & 0x0f];
62 for (j = 0; j < 16; j++) {
65 if (x > 0x20 && x < 0x7F) {
75 "ajp_msg_dump(): %.4lx %s\n",
76 (unsigned long)i, line);
87 * Check a new AJP Message by looking at signature and return its size
89 * @param msg AJP Message to check
90 * @param len Pointer to returned len
91 * @return APR_SUCCESS or error
93 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
95 apr_byte_t *head = msg->buf;
98 if (!((head[0] == 0x41 && head[1] == 0x42) ||
99 (head[0] == 0x12 && head[1] == 0x34))) {
101 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
102 "ajp_check_msg_header() got bad signature %x%x",
105 return AJP_EBAD_SIGNATURE;
108 msglen = ((head[2] & 0xff) << 8);
109 msglen += (head[3] & 0xFF);
111 if (msglen > AJP_MSG_BUFFER_SZ) {
112 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
113 "ajp_check_msg_header() incoming message is "
114 "too big %" APR_SIZE_T_FMT ", max is %d",
115 msglen, AJP_MSG_BUFFER_SZ);
119 msg->len = msglen + AJP_HEADER_LEN;
120 msg->pos = AJP_HEADER_LEN;
127 * Reset an AJP Message
129 * @param msg AJP Message to reset
130 * @return APR_SUCCESS or error
132 apr_status_t ajp_msg_reset(ajp_msg_t *msg)
134 msg->len = AJP_HEADER_LEN;
135 msg->pos = AJP_HEADER_LEN;
141 * Mark the end of an AJP Message
143 * @param msg AJP Message to end
144 * @return APR_SUCCESS or error
146 apr_status_t ajp_msg_end(ajp_msg_t *msg)
148 apr_size_t len = msg->len - AJP_HEADER_LEN;
150 if (msg->server_side) {
159 msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF);
160 msg->buf[3] = (apr_byte_t)(len & 0xFF);
165 static APR_INLINE int ajp_log_overflow(ajp_msg_t *msg, const char *context)
167 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
168 "%s(): BufferOverflowException %" APR_SIZE_T_FMT
170 context, msg->pos, msg->len);
171 return AJP_EOVERFLOW;
175 * Add an unsigned 32bits value to AJP Message
177 * @param msg AJP Message to get value from
178 * @param value value to add to AJP Message
179 * @return APR_SUCCESS or error
181 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value)
183 apr_size_t len = msg->len;
185 if ((len + 4) > AJP_MSG_BUFFER_SZ) {
186 return ajp_log_overflow(msg, "ajp_msg_append_uint32");
189 msg->buf[len] = (apr_byte_t)((value >> 24) & 0xFF);
190 msg->buf[len + 1] = (apr_byte_t)((value >> 16) & 0xFF);
191 msg->buf[len + 2] = (apr_byte_t)((value >> 8) & 0xFF);
192 msg->buf[len + 3] = (apr_byte_t)(value & 0xFF);
200 * Add an unsigned 16bits value to AJP Message
202 * @param msg AJP Message to get value from
203 * @param value value to add to AJP Message
204 * @return APR_SUCCESS or error
206 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value)
208 apr_size_t len = msg->len;
210 if ((len + 2) > AJP_MSG_BUFFER_SZ) {
211 return ajp_log_overflow(msg, "ajp_msg_append_uint16");
214 msg->buf[len] = (apr_byte_t)((value >> 8) & 0xFF);
215 msg->buf[len + 1] = (apr_byte_t)(value & 0xFF);
223 * Add an unsigned 8bits value to AJP Message
225 * @param msg AJP Message to get value from
226 * @param value value to add to AJP Message
227 * @return APR_SUCCESS or error
229 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
231 apr_size_t len = msg->len;
233 if ((len + 1) > AJP_MSG_BUFFER_SZ) {
234 return ajp_log_overflow(msg, "ajp_msg_append_uint8");
237 msg->buf[len] = value;
244 * Add a String in AJP message, and transform the String in ASCII
245 * if convert is set and we're on an EBCDIC machine
247 * @param msg AJP Message to get value from
248 * @param value Pointer to String
249 * @param convert When set told to convert String to ASCII
250 * @return APR_SUCCESS or error
252 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
258 return(ajp_msg_append_uint16(msg, 0xFFFF));
262 if ((msg->len + len + 2) > AJP_MSG_BUFFER_SZ) {
263 return ajp_log_overflow(msg, "ajp_msg_append_cvt_string");
266 /* ignore error - we checked once */
267 ajp_msg_append_uint16(msg, (apr_uint16_t)len);
269 /* We checked for space !! */
270 memcpy(msg->buf + msg->len, value, len + 1); /* including \0 */
272 if (convert) /* convert from EBCDIC if needed */
273 ajp_xlate_to_ascii((char *)msg->buf + msg->len, len + 1);
281 * Add a Byte array to AJP Message
283 * @param msg AJP Message to get value from
284 * @param value Pointer to Byte array
285 * @param valuelen Byte array len
286 * @return APR_SUCCESS or error
288 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
292 return APR_SUCCESS; /* Shouldn't we indicate an error ? */
295 if ((msg->len + valuelen) > AJP_MSG_BUFFER_SZ) {
296 return ajp_log_overflow(msg, "ajp_msg_append_bytes");
299 /* We checked for space !! */
300 memcpy(msg->buf + msg->len, value, valuelen);
301 msg->len += valuelen;
307 * Get a 32bits unsigned value from AJP Message
309 * @param msg AJP Message to get value from
310 * @param rvalue Pointer where value will be returned
311 * @return APR_SUCCESS or error
313 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
317 if ((msg->pos + 3) > msg->len) {
318 return ajp_log_overflow(msg, "ajp_msg_get_uint32");
321 value = ((msg->buf[(msg->pos++)] & 0xFF) << 24);
322 value |= ((msg->buf[(msg->pos++)] & 0xFF) << 16);
323 value |= ((msg->buf[(msg->pos++)] & 0xFF) << 8);
324 value |= ((msg->buf[(msg->pos++)] & 0xFF));
332 * Get a 16bits unsigned value from AJP Message
334 * @param msg AJP Message to get value from
335 * @param rvalue Pointer where value will be returned
336 * @return APR_SUCCESS or error
338 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
342 if ((msg->pos + 1) > msg->len) {
343 return ajp_log_overflow(msg, "ajp_msg_get_uint16");
346 value = ((msg->buf[(msg->pos++)] & 0xFF) << 8);
347 value += ((msg->buf[(msg->pos++)] & 0xFF));
354 * Peek a 16bits unsigned value from AJP Message, position in message
357 * @param msg AJP Message to get value from
358 * @param rvalue Pointer where value will be returned
359 * @return APR_SUCCESS or error
361 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
365 if ((msg->pos + 1) > msg->len) {
366 return ajp_log_overflow(msg, "ajp_msg_peek_uint16");
369 value = ((msg->buf[(msg->pos)] & 0xFF) << 8);
370 value += ((msg->buf[(msg->pos + 1)] & 0xFF));
377 * Peek a 8bits unsigned value from AJP Message, position in message
380 * @param msg AJP Message to get value from
381 * @param rvalue Pointer where value will be returned
382 * @return APR_SUCCESS or error
384 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
386 if (msg->pos > msg->len) {
387 return ajp_log_overflow(msg, "ajp_msg_peek_uint8");
390 *rvalue = msg->buf[msg->pos];
395 * Get a 8bits unsigned value from AJP Message
397 * @param msg AJP Message to get value from
398 * @param rvalue Pointer where value will be returned
399 * @return APR_SUCCESS or error
401 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
404 if (msg->pos > msg->len) {
405 return ajp_log_overflow(msg, "ajp_msg_get_uint8");
408 *rvalue = msg->buf[msg->pos++];
414 * Get a String value from AJP Message
416 * @param msg AJP Message to get value from
417 * @param rvalue Pointer where value will be returned
418 * @return APR_SUCCESS or error
420 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue)
426 status = ajp_msg_get_uint16(msg, &size);
429 if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
430 return ajp_log_overflow(msg, "ajp_msg_get_string");
433 msg->pos += (apr_size_t)size;
434 msg->pos++; /* a String in AJP is NULL terminated */
436 *rvalue = (char *)(msg->buf + start);
442 * Get a Byte array from AJP Message
444 * @param msg AJP Message to get value from
445 * @param rvalue Pointer where value will be returned
446 * @param rvalueLen Pointer where Byte array len will be returned
447 * @return APR_SUCCESS or error
449 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
450 apr_size_t *rvalue_len)
456 status = ajp_msg_get_uint16(msg, &size);
457 /* save the current position */
460 if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
461 return ajp_log_overflow(msg, "ajp_msg_get_bytes");
463 msg->pos += (apr_size_t)size; /* only bytes, no trailer */
465 *rvalue = msg->buf + start;
473 * Create an AJP Message from pool
475 * @param pool memory pool to allocate AJP message from
476 * @param rmsg Pointer to newly created AJP message
477 * @return APR_SUCCESS or error
479 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg)
481 ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t));
484 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
485 "ajp_msg_create(): can't allocate AJP message memory");
489 msg->server_side = 0;
491 msg->buf = (apr_byte_t *)apr_palloc(pool, AJP_MSG_BUFFER_SZ);
493 /* XXX: This should never happen
494 * In case if the OS cannont allocate 8K of data
495 * we are in serious trouble
496 * No need to check the alloc return value, cause the
497 * core dump is probably the best solution anyhow.
499 if (msg->buf == NULL) {
500 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
501 "ajp_msg_create(): can't allocate AJP message memory");
506 msg->header_len = AJP_HEADER_LEN;
513 * Recopy an AJP Message to another
515 * @param smsg source AJP message
516 * @param dmsg destination AJP message
517 * @return APR_SUCCESS or error
519 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg)
522 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
523 "ajp_msg_copy(): destination msg is null");
527 if (smsg->len > AJP_MSG_BUFFER_SZ) {
528 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
529 "ajp_msg_copy(): destination buffer too "
530 "small %" APR_SIZE_T_FMT ", max size is %d",
531 smsg->len, AJP_MSG_BUFFER_SZ);
535 memcpy(dmsg->buf, smsg->buf, smsg->len);
536 dmsg->len = smsg->len;
537 dmsg->pos = smsg->pos;
544 * Serialize in an AJP Message a PING command
546 * +-----------------------+
547 * | PING CMD (1 byte) |
548 * +-----------------------+
550 * @param smsg AJP message to put serialized message
551 * @return APR_SUCCESS or error
553 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg)
558 if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_PING)) != APR_SUCCESS)
565 * Serialize in an AJP Message a CPING command
567 * +-----------------------+
568 * | CPING CMD (1 byte) |
569 * +-----------------------+
571 * @param smsg AJP message to put serialized message
572 * @return APR_SUCCESS or error
574 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg)
579 if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_CPING)) != APR_SUCCESS)