hgomez 2004/07/28 00:44:44 Modified: ajp/ajplib/test ajp.h ajp_msg.c ajp_link.c Log: ajp_link/ajp_msg are more APR compliants and use the ap_ log func.
ajp_link is now minimal and use only apr_socket_t. Revision Changes Path 1.5 +13 -28 jakarta-tomcat-connectors/ajp/ajplib/test/ajp.h Index: ajp.h =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/ajp/ajplib/test/ajp.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- ajp.h 27 Jul 2004 15:07:31 -0000 1.4 +++ ajp.h 28 Jul 2004 07:44:43 -0000 1.5 @@ -53,34 +53,9 @@ #endif -struct ajp_idef -{ - char * host; - apr_port_t port; - apr_sockaddr_t * addr; - int keepalive; - int timeout; - int ndelay; -}; - -typedef struct ajp_idef ajp_idef_t; - -struct ajp_ilink -{ - apr_socket_t * sock; - int requests; -}; - -typedef struct ajp_ilink ajp_ilink_t; - -struct ajp_env -{ - apr_pool_t * log; - apr_pool_t * pool; - int loglevel; -}; - -typedef struct ajp_env ajp_env_t; +#ifdef AJP_USE_HTTPD_WRAP +#include "httpd_wrap.h" +#endif struct ajp_msg { @@ -97,6 +72,16 @@ #define AJP_HEADER_SZ_LEN 2 #define AJP_MSG_BUFFER_SZ (8*1024) #define AJP13_MAX_SEND_BODY_SZ (AJP_DEF_BUFFER_SZ - 6) + +/* Webserver ask container to take control (logon phase) */ +#define CMD_AJP13_PING (unsigned char)8 + +/* Webserver check if container is alive, since container should respond by cpong */ +#define CMD_AJP13_CPING (unsigned char)10 + +/* Container response to cping request */ +#define CMD_AJP13_CPONG (unsigned char)9 + #endif /* AJP_H */ 1.4 +416 -136 jakarta-tomcat-connectors/ajp/ajplib/test/ajp_msg.c Index: ajp_msg.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/ajp/ajplib/test/ajp_msg.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ajp_msg.c 27 Jul 2004 14:38:35 -0000 1.3 +++ ajp_msg.c 28 Jul 2004 07:44:43 -0000 1.4 @@ -15,17 +15,86 @@ #include "ajp.h" -apr_status_t ajp_msg_check_header(ajp_env_t *env, ajp_msg_t * msg) + +static char *hex_table = "0123456789ABCDEF"; + +/** + * Dump up to the first 1024 bytes on an AJP Message + * + * @param msg AJP Message to dump + * @param err error string to display + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err) +{ + int i; + int j; + char line[80]; + char *current; + apr_byte_t x; + apr_size_t len = msg->len; + + /* Display only first 1024 bytes */ + if (len > 1024) + len = 1024; + + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, NULL, + "ajp_msg_dump(): %s pos=%d len=%d max=%d", + err, msg->pos, msg->len, AJP_MSG_BUFFER_SZ); + + for (i = 0; i < len; i += 16) { + current = line; + + for (j = 0; j < 16; j++) { + x = msg->buf[i + j]; + + *current++ = hex_table[x >> 4]; + *current++ = hex_table[x & 0x0f]; + *current++ = ' '; + } + *current++ = ' '; + *current++ = '-'; + *current++ = ' '; + for (j = 0; j < 16; j++) { + x = msg->buf[i + j]; + + if (x > 0x20 && x < 0x7F) { + *current++ = x; + } + else { + *current++ = '.'; + } + } + + *current++ = '\0'; + + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, NULL, + "ajp_msg_dump(): %.4x %s", + i, line); + } + + return APR_SUCCESS; +} + + +/** + * Check a new AJP Message by looking at signature and return its size + * + * @param msg AJP Message to check + * @param len Pointer to returned len + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len) { apr_byte_t *head = msg->buf; apr_size_t msglen; if (!((head[0] == 0x41 && head[1] == 0x42) || (head[0] == 0x12 && head[1] == 0x34))) { - - fprintf(stderr, - "ajp_check_msg_header() got bad signature %x%x\n", - head[0], head[1]); + + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_check_msg_header() got bad signature %x%x", + head[0], head[1]); return -1; } @@ -34,25 +103,40 @@ msglen += (head[3] & 0xFF); if (msglen > AJP_MSG_BUFFER_SZ) { - fprintf(stderr, - "ajp_check_msg_header() incoming message is too big %d, max is %d\n", - msglen, AJP_MSG_BUFFER_SZ); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_check_msg_header() incoming message is too big %d, max is %d", + msglen, AJP_MSG_BUFFER_SZ); return -2; } msg->len = msglen + AJP_HEADER_LEN; msg->pos = AJP_HEADER_LEN; - - return 0; + *len = msglen; + + return APR_SUCCESS; } -void ajp_msg_reset(ajp_env_t *env, ajp_msg_t * msg) +/** + * Reset an AJP Message + * + * @param msg AJP Message to reset + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_reset(ajp_msg_t *msg) { msg->len = AJP_HEADER_LEN; msg->pos = AJP_HEADER_LEN; + + return APR_SUCCESS; } -void ajp_msg_end(ajp_env_t *env, ajp_msg_t * msg) +/** + * Mark the end of an AJP Message + * + * @param msg AJP Message to end + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_end(ajp_msg_t *msg) { apr_size_t len = msg->len - AJP_HEADER_LEN; @@ -67,257 +151,453 @@ msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF); msg->buf[3] = (apr_byte_t)(len & 0xFF); + + return APR_SUCCESS; } -apr_status_t ajp_msg_append_uint32(ajp_env_t *env, ajp_msg_t *msg, - const apr_uint32_t val) +/** + * Add an unsigned 32bits value to AJP Message + * + * @param msg AJP Message to get value from + * @param value value to add to AJP Message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, const apr_uint32_t value) { apr_size_t len = msg->len; - if (len + AJP_HEADER_LEN > AJP_MSG_BUFFER_SZ) { + if ((len + 4) > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_append_uint32(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - msg->buf[len] = (apr_byte_t)((val >> 24) & 0xFF); - msg->buf[len + 1] = (apr_byte_t)((val >> 16) & 0xFF); - msg->buf[len + 2] = (apr_byte_t)((val >> 8) & 0xFF); - msg->buf[len + 3] = (apr_byte_t)(val & 0xFF); + msg->buf[len] = (apr_byte_t)((value >> 24) & 0xFF); + msg->buf[len + 1] = (apr_byte_t)((value >> 16) & 0xFF); + msg->buf[len + 2] = (apr_byte_t)((value >> 8) & 0xFF); + msg->buf[len + 3] = (apr_byte_t)(value & 0xFF); msg->len += 4; - return 0; + return APR_SUCCESS; } -apr_status_t ajp_msg_append_uint16(ajp_env_t *env, ajp_msg_t *msg, - const apr_uint16_t val) +/** + * Add an unsigned 16bits value to AJP Message + * + * @param msg AJP Message to get value from + * @param value value to add to AJP Message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, const apr_uint16_t value) { apr_size_t len = msg->len; - if (len + 2 > AJP_MSG_BUFFER_SZ) { + if ((len + 2) > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_append_uint16(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - msg->buf[len] = (apr_byte_t)((val >> 8) & 0xFF); - msg->buf[len + 1] = (apr_byte_t)(val & 0xFF); + msg->buf[len] = (apr_byte_t)((value >> 8) & 0xFF); + msg->buf[len + 1] = (apr_byte_t)(value & 0xFF); msg->len += 2; - return 0; + return APR_SUCCESS; } -apr_status_t ajp_msg_append_byte(ajp_env_t *env, ajp_msg_t *msg, - const apr_byte_t val) +/** + * Add an unsigned 8bits value to AJP Message + * + * @param msg AJP Message to get value from + * @param value value to add to AJP Message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, const apr_byte_t value) { apr_size_t len = msg->len; if ((len + 1) > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_append_uint8(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - msg->buf[len] = val; + msg->buf[len] = value; msg->len += 1; - return 0; + return APR_SUCCESS; } -apr_status_t ajp_msg_append_cvt_string(ajp_env_t *env, ajp_msg_t *msg, - const char *param, int convert) -{ - apr_size_t len; - - if (param == NULL) { - return(ajp_msg_append_int(env, msg, 0xFFFF)); - } - - len = strlen(param); - if (msg->len + len + 2 > AJP_MSG_BUFFER_SZ) { +/** + * Add a String in AJP message, and transform the String in ASCII + * if convert is set and we're on an EBCDIC machine + * + * @param msg AJP Message to get value from + * @param value Pointer to String + * @param convert When set told to convert String to ASCII + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_cvt_string(ajp_msg_t *msg, const char *value, int convert) +{ + int len; + + if (value == NULL) { + return(ajp_msg_append_uint16(msg, 0xFFFF)); + } + + len = strlen(value); + if ((msg->len + len + 2) > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_append_cvt_string(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } /* ignore error - we checked once */ - ajp_msg_append_int(env, msg, (unsigned short)len); + ajp_msg_append_uint16(msg, (apr_uint16_t)len); /* We checked for space !! */ - strncpy((char *)msg->buf + msg->len, param, len + 1); /* including \0 */ + strncpy((char *)msg->buf + msg->len, value, len + 1); /* including \0 */ if (convert) ajp_xlate_to_ascii((char *)msg->buf + msg->len, len + 1); /* convert from EBCDIC if needed */ msg->len += len + 1; - return 0; + return APR_SUCCESS; } -apr_status_t ajp_msg_append_string(ajp_env_t *env, ajp_msg_t *msg, - const char *param) +/** + * Add a String in AJP message, and transform + * the String in ASCII if we're on an EBCDIC machine + * + * @param msg AJP Message to get value from + * @param value Pointer to String + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_string(ajp_msg_t *msg, const char *value) { - return ajp_msg_append_cvt_string(env, msg, param, 1); + return ajp_msg_append_cvt_string(msg, value, 1); } -apr_status_t ajp_msg_append_bytes(ajp_env_t *env, ajp_msg_t *msg, - const apr_byte_t *param, const apr_size_t len) +/** + * Add a String in AJP message. + * + * @param msg AJP Message to get value from + * @param value Pointer to String + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_ascii_string(ajp_msg_t *msg, const char *value) { - if (!len) { - return 0; + return ajp_msg_append_cvt_string(msg, value, 0); +} + + +/** + * Add a Byte array to AJP Message + * + * @param msg AJP Message to get value from + * @param value Pointer to Byte array + * @param valuelen Byte array len + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value, const apr_size_t valuelen) +{ + if (! valuelen) { + return APR_SUCCESS; /* Shouldn't we indicate an error ? */ } - if ((msg->len + len) > AJP_MSG_BUFFER_SZ) { + if ((msg->len + valuelen) > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_append_bytes(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } /* We checked for space !! */ - memcpy((char *)msg->buf + msg->len, param, len); - msg->len += len; + memcpy((char *)msg->buf + msg->len, value, valuelen); + msg->len += valuelen; - return 0; + return APR_SUCCESS; } -apr_uint32_t ajp_msg_get_uint32(ajp_env_t *env, ajp_msg_t *msg) +/** + * Get a 32bits unsigned value from AJP Message + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue) { - apr_uint32_t i; + apr_uint32_t value; if ((msg->pos + 3) > msg->len) { - fprintf(stderr, - "ajp_msg_get_long(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_get_long(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - i = ((msg->buf[(msg->pos++)] & 0xFF) << 24); - i |= ((msg->buf[(msg->pos++)] & 0xFF) << 16); - i |= ((msg->buf[(msg->pos++)] & 0xFF) << 8); - i |= ((msg->buf[(msg->pos++)] & 0xFF)); - return i; + value = ((msg->buf[(msg->pos++)] & 0xFF) << 24); + value |= ((msg->buf[(msg->pos++)] & 0xFF) << 16); + value |= ((msg->buf[(msg->pos++)] & 0xFF) << 8); + value |= ((msg->buf[(msg->pos++)] & 0xFF)); + + *rvalue = value; + return APR_SUCCESS; } -apr_uint16_t ajp_msg_get_uint16(ajp_env_t *env, ajp_msg_t *msg) + +/** + * Get a 16bits unsigned value from AJP Message + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue) { - apr_uint16_t i; + apr_uint16_t value; - if (msg->pos + 1 > msg->len) { - fprintf(stderr, - "ajp_msg_get_int(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + if ((msg->pos + 1) > msg->len) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_get_int(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - i = ((msg->buf[(msg->pos++)] & 0xFF) << 8); - i += ((msg->buf[(msg->pos++)] & 0xFF)); + value = ((msg->buf[(msg->pos++)] & 0xFF) << 8); + value += ((msg->buf[(msg->pos++)] & 0xFF)); - return i; + *rvalue = value; + return APR_SUCCESS; } -apr_uint16_t ajp_msg_peek_int(ajp_env_t *env, ajp_msg_t *msg) +/** + * Peek a 16bits unsigned value from AJP Message, position in message + * is not updated + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_peek_int(ajp_msg_t *msg, apr_uint16_t *rvalue) { - apr_uint16_t i; + apr_uint16_t value; - if (msg->pos + 1 > msg->len) { - fprintf(stderr, - "ajp_msg_peek_int(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + if ((msg->pos + 1) > msg->len) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_peek_int(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - i = ((msg->buf[(msg->pos)] & 0xFF) << 8); - i += ((msg->buf[(msg->pos + 1)] & 0xFF)); - return i; + + value = ((msg->buf[(msg->pos)] & 0xFF) << 8); + value += ((msg->buf[(msg->pos + 1)] & 0xFF)); + + *rvalue = value; + return APR_SUCCESS; } -apr_byte_t ajp_msg_get_byte(ajp_env_t *env, ajp_msg_t *msg) +/** + * Get a 8bits unsigned value from AJP Message + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_get_byte(ajp_msg_t *msg, apr_byte_t *rvalue) { - apr_byte_t rc; + apr_byte_t value; if (msg->pos > msg->len) { - fprintf(stderr, - "ajp_msg_get_byte(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_get_byte(): BufferOverflowException %d %d", + msg->pos, msg->len); return -1; } - rc = msg->buf[msg->pos++]; - - return rc; + + *rvalue = msg->buf[msg->pos++]; + return APR_SUCCESS; } -char * ajp_msg_get_string(ajp_env_t *env, ajp_msg_t *msg) + +/** + * Get a String value from AJP Message + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue) { - apr_size_t size = (apr_size_t)ajp_msg_get_uint16(env, msg); - apr_size_t start = msg->pos; + apr_uint16_t size; + apr_size_t start; + apr_status_t status; + + status = ajp_msg_get_uint16(msg, &size); + start = msg->pos; - if ((size < 0) || (size + start > AJP_MSG_BUFFER_SZ)) { - fprintf(stderr, - "ajp_msg_get_string(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_get_string(): BufferOverflowException %d %d", + msg->pos, msg->len); - return (unsigned char *)"ERROR"; /* XXX */ + return -1; } - msg->pos += size; - msg->pos++; /* terminating NULL */ + msg->pos += (apr_size_t)size; + msg->pos++; /* a String in AJP is NULL terminated */ - return (unsigned char *)(msg->buf + start); + *rvalue = (char *)(msg->buf + start); + return APR_SUCCESS; } -apr_byte_t *ajp_msg_get_bytes(ajp_env_t *env, ajp_msg_t *msg, apr_size_t *lenP) -{ - apr_size_t size = (apr_size_t)ajp_msg_get_uint16(env, msg); - apr_size_t start = msg->pos; - *lenP = size; +/** + * Get a Byte array from AJP Message + * + * @param msg AJP Message to get value from + * @param rvalue Pointer where value will be returned + * @param rvalueLen Pointer where Byte array len will be returned + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue, apr_size_t *rvalueLen) +{ + apr_uint16_t size; + apr_size_t start; + apr_status_t status; - if ((size < 0) || (size + start > AJP_MSG_BUFFER_SZ)) { - fprintf(stderr, - "ajp_msg_get_bytes(): BufferOverflowException %d %d\n", - msg->pos, msg->len); + status = ajp_msg_get_uint16(msg, &size); + start = msg->pos; - return (unsigned char *)"ERROR"; /* XXX */ + if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_get_bytes(): BufferOverflowException %d %d", + msg->pos, msg->len); + return -1; } - msg->pos += size; - msg->pos++; /* terminating NULL */ + msg->pos += (apr_size_t)size; /* only bytes, no trailer */ + + *rvalue = (apr_byte_t *)(msg->buf + start); + *rvalueLen = size; - return (apr_byte_t *)(msg->buf + start); + return APR_SUCCESS; } -ajp_msg_t *ajp_msg_create(ajp_env_t *env) -{ - ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(env->pool, sizeof(ajp_msg_t)); - if (!msg) - return NULL; +/** + * Create an AJP Message from pool + * + * @param pool memory pool to allocate AJP message from + * @param rmsg Pointer to newly created AJP message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t *rmsg) +{ + ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t)); + if (! msg) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_create(): can't allocate AJP message memory"); + return -1; + } + msg->serverSide = 0; - msg->buf = (apr_byte_t *)apr_palloc(env->pool, AJP_MSG_BUFFER_SZ); + msg->buf = (apr_byte_t *)apr_palloc(pool, AJP_MSG_BUFFER_SZ); if (msg->buf == NULL) { - return NULL; + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_create(): can't allocate AJP message memory"); + return -1; } msg->len = 0; msg->headerLen = AJP_HEADER_LEN; - - return msg; + rmsg = msg; + + return APR_SUCCESS; } -apr_status_t ajp_msg_copy(ajp_env_t *env, ajp_msg_t *msg, ajp_msg_t *dmsg) -{ - if (dmsg == NULL) +/** + * Recopy an AJP Message to another + * + * @param smsg source AJP message + * @param dmsg destination AJP message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg) +{ + if (dmsg == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_copy(): destination msg is null"); return -1; - - if (msg->len > AJP_MSG_BUFFER_SZ) { - fprintf(stderr, - "ajp_msg_copy(): destination buffer too small %d/%d\n", - msg->len, AJP_MSG_BUFFER_SZ); + } + + if (smsg->len > AJP_MSG_BUFFER_SZ) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_msg_copy(): destination buffer too small %d, max size is %d", + smsg->len, AJP_MSG_BUFFER_SZ); return -2; } - memcpy(dmsg->buf, msg->buf, msg->len); - dmsg->len = msg->len; - dmsg->pos = msg->pos; + memcpy(dmsg->buf, smsg->buf, smsg->len); + dmsg->len = smsg->len; + dmsg->pos = smsg->pos; + + return APR_SUCCESS; +} + + +/** + * Serialize in an AJP Message a PING command + * + * +-----------------------+ + * | PING CMD (1 byte) | + * +-----------------------+ + * + * @param smsg AJP message to put serialized message + * @return APR_SUCCESS or error + */ +apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg) +{ + ajp_msg_reset(msg); + + if (msg_append_uint8(msg, CMD_AJP13_PING) != APR_SUCCESS) + return -1; + + return APR_SUCCESS; +} - return dmsg->len; +/** + * Serialize in an AJP Message a CPING command + * + * +-----------------------+ + * | CPING CMD (1 byte) | + * +-----------------------+ + * + * @param smsg AJP message to put serialized message + * @return APR_SUCCESS or error + */ +int ajp_msg_serialize_cping(ajp_msg_t *msg) +{ + ajp_msg_reset(msg); + + if (msg_append_uint8(msg, CMD_AJP13_CPING) != APR_SUCCESS) + return -1; + + return APR_SUCCESS; } 1.3 +72 -208 jakarta-tomcat-connectors/ajp/ajplib/test/ajp_link.c Index: ajp_link.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-connectors/ajp/ajplib/test/ajp_link.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ajp_link.c 27 Jul 2004 13:46:32 -0000 1.2 +++ ajp_link.c 28 Jul 2004 07:44:43 -0000 1.3 @@ -15,253 +15,117 @@ #include "ajp.h" -/** - Resolve host -**/ -int ajp_idef_resolve(ajp_env_t *env, char *host, apr_port_t port, ajp_idef_t * idef) -{ - int err; - - err = apr_sockaddr_info_get(&idef->addr, host, APR_UNSPEC, port, 0, env->pool); - - if (err != APR_SUCCESS) { - return err; - } - return 0; -} - - -/** - Create (connect) instance link -**/ -int ajp_ilink_connect(ajp_env_t *env, ajp_idef_t * idef, ajp_ilink_t *link) +apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg) { - apr_sockaddr_t *remote_sa = idef->addr; - int ndelay = idef->ndelay; - int keepalive = idef->keepalive; - - apr_socket_t *sock; - apr_status_t ret; - apr_int32_t timeout = (apr_int32_t) (idef->timeout * APR_USEC_PER_SEC); - char msg[128]; - int connected = 0; - - while (remote_sa && !connected) { - if ((ret = apr_socket_create(&sock, remote_sa->family, SOCK_STREAM, -#if (APR_MAJOR_VERSION > 0) - APR_PROTO_TCP, -#endif - env->pool)) != APR_SUCCESS) { - if (remote_sa->next) { - fprintf(stderr, - "ajp_ilink_connect() error %d creating socket to %s\n", - ret, idef->host); - } - else { - fprintf(stderr, - "ajp_ilink_connect() error %d creating socket to %s, no more addr\n", - ret, idef->host); - } - remote_sa = remote_sa->next; - continue; - } - /* store the socket information */ - link->sock = sock; - /* no requests yet */ - link->requests = 0; - - fprintf(stdout, "ajp_ilink_connect() create tcp socket %d\n", sock); - - /* the default timeout (0) will set the socket to blocking with - infinite timeouts. - */ - - if (timeout <= 0) - apr_socket_timeout_set(sock, -1); - else - apr_socket_timeout_set(sock, timeout); - - /* make the connection out of the socket */ - do { - ret = apr_socket_connect(sock, remote_sa); - } while (APR_STATUS_IS_EINTR(ret)); - - /* if an error occurred, loop round and try again */ - if (ret != APR_SUCCESS) { - apr_socket_close(sock); - if (remote_sa->next) { - fprintf(stderr, - "ajp_ilink_connect() attempt to connect to %pI (%s) failed %d\n", - remote_sa, idef->host, ret); - } - else { - fprintf(stderr, - "ajp_ilink_connect() attempt to connect to %pI (%s) failed %d, no more addr\n", - remote_sa, idef->host, ret); - } - remote_sa = remote_sa->next; - continue; - } - connected = 1; - } - - if (!connected) { - apr_socket_close(sock); + char *buf; + apr_status_t status; + apr_size_t length; + + if (sock == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_ilink_send(): NULL socket provided"); return -1; } - /* enable the use of keep-alive packets on TCP connection */ - if (keepalive) { - int set = 1; - if ((ret = - apr_socket_opt_set(sock, APR_SO_KEEPALIVE, set)) != APR_SUCCESS) { - apr_socket_close(sock); - fprintf(stderr, "ajp_ilink_connect() keepalive failed %d %s\n", - ret, apr_strerror(ret, msg, sizeof(msg))); - return -1; - } - } - - /* Disable the Nagle algorithm if ndelay is set */ - if (ndelay) { - int set = 1; - if ((ret = - apr_socket_opt_set(sock, APR_TCP_NODELAY, set)) != APR_SUCCESS) { - apr_socket_close(sock); - fprintf(stderr, "ajp_ilink_connect() nodelay failed %d %s\n", - ret, apr_strerror(ret, msg, sizeof(msg))); - return -1; - } - } - - fprintf(stdout, "ajp_ilink_connect(), sock = %d\n", sock); - - return 0; -} - - -/** - Close instance link -**/ - -int ajp_ilink_close(ajp_env_t *env, ajp_ilink_t *link) -{ - apr_socket_t *sd; - apr_status_t rc; - - sd = link->sock; - if (sd == NULL) - return -1; - - fprintf(stdout, - "ajp_ilink_close() closing sock = %d after %d requests\n", - sd, link->requests); - - rc = apr_socket_close(sd); - return rc; -} - - -int ajp_ilink_send(ajp_env_t *env, ajp_ilink_t *link, ajp_msg_t *msg) -{ - char *b; - int len; - apr_socket_t *sock; - apr_status_t stat; - apr_size_t length; - char data[128]; - - sock = link->sock; - - if (sock == NULL) - return -1; - - ajp_msg_end(env, msg); + ajp_msg_end(msg); - len = msg->len; - b = msg->buf; + length = msg->len; + buf = (char *)msg->buf; - length = (apr_size_t) len; do { apr_size_t written = length; - stat = apr_socket_send(sock, b, &written); - if (stat != APR_SUCCESS) { - fprintf(stderr, - "ajp_ilink_send() send failed %d %s\n", - stat, apr_strerror(stat, data, sizeof(data))); + status = apr_socket_send(sock, buf, &written); + if (status != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, status, NULL, + "ajp_ilink_send(): send failed"); return -3; /* -2 is not possible... */ } length -= written; - b += written; + buf += written; } while (length); - return 0; + return APR_SUCCESS; } -static int ajp_ilink_readN(ajp_env_t *env, ajp_ilink_t *link, char * buf, apr_size_t len) +static apr_status_t ajp_ilink_readN(apr_socket_t *sock, char * buf, const apr_size_t len) { - apr_socket_t *sock; - apr_size_t length; - apr_status_t stat; - int rdlen; - - sock = link->sock; - - if (sock == NULL) + apr_size_t length; + apr_status_t status; + apr_size_t rdlen; + + if (sock == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_ilink_readN(): NULL socket provided"); return -1; + } - rdlen = 0; + rdlen = 0; length = len; + while (rdlen < len) { - stat = apr_socket_recv(sock, buf + rdlen, &length); + status = apr_socket_recv(sock, buf + rdlen, &length); - if (stat == APR_EOF) - return -1; /* socket closed. */ - else if (APR_STATUS_IS_EAGAIN(stat)) + if (status == APR_EOF) + return status; /* socket closed. */ + else if (APR_STATUS_IS_EAGAIN(status)) continue; - else if (stat != APR_SUCCESS) - return -1; /* any error. */ + else if (status != APR_SUCCESS) + return status; /* any error. */ + rdlen += length; - length = (apr_size_t) (len - rdlen); + length = len - rdlen; } - return rdlen; + return APR_SUCCESS; } -int ajp_ilink_receive(ajp_env_t *env, ajp_ilink_t *link, ajp_msg_t *msg) +apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg) { - int hlen = msg->headerLen; - int blen; - int rc; - - - ajp_ilink_channel_apr_readN(env, link, msg->buf, hlen); - - blen = ajp_msg_check_header(env, msg); - - if (blen < 0) { - fprintf(stderr, - "ajp_ilink_receive() received bad header\n"); + apr_status_t status; + apr_size_t hlen;; + apr_size_t blen; + + if (sock == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_ilink_receive(): NULL socket provided"); return -1; } - rc = jk2_channel_apr_readN(env, link, msg->buf + hlen, blen); + hlen = msg->headerLen; + + status = ajp_ilink_readN(sock, msg->buf, hlen); + + if (status != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, status, NULL, + "ajp_ilink_receive() can't receive header\n"); + return -1; + } + + status = ajp_msg_check_header(msg, &blen); - if (rc < 0) { - fprintf(stderr, - "ajp_ilink_receive() error while receiving message body %d %d\n", - rc, errno); + if (status != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, NULL, + "ajp_ilink_receive() received bad header\n"); return -2; } - fprintf(stdout, - "ajp_ilink_receive() received packet len=%d type=%d\n", - blen, (int)msg->buf[hlen]); - return 0; + status = ajp_ilink_readN(sock, msg->buf + hlen, blen); + + if (status != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, status, NULL, + "ajp_ilink_receive() error while receiving message body %of length %d\n", + hlen); + return -3; + } + + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, NULL, + "ajp_ilink_receive() received packet len=%d type=%d\n", + blen, (int)msg->buf[hlen]); + return APR_SUCCESS; } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]