diff options
author | gmcdonald | 2010-02-13 01:32:03 +0000 |
---|---|---|
committer | gmcdonald | 2010-02-13 01:32:03 +0000 |
commit | 0425aadc78680e53000fd0108b540d6eca048516 (patch) | |
tree | 8ec7ab8e015d454c5ec586dfc91e05a2dce1cfc0 /tools/tcpmon/src | |
download | axis2c-0425aadc78680e53000fd0108b540d6eca048516.tar.gz axis2c-0425aadc78680e53000fd0108b540d6eca048516.tar.bz2 |
Moving axis svn, part of TLP move INFRA-2441
git-svn-id: http://svn.apache.org/repos/asf/axis/axis2/c/core/trunk@909681 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tools/tcpmon/src')
-rw-r--r-- | tools/tcpmon/src/Makefile.am | 21 | ||||
-rw-r--r-- | tools/tcpmon/src/entry.c | 656 | ||||
-rw-r--r-- | tools/tcpmon/src/session.c | 632 | ||||
-rw-r--r-- | tools/tcpmon/src/tcpmon.c | 1037 | ||||
-rw-r--r-- | tools/tcpmon/src/tcpmon_entry_local.h | 16 | ||||
-rw-r--r-- | tools/tcpmon/src/tcpmon_session_local.h | 15 | ||||
-rw-r--r-- | tools/tcpmon/src/util.c | 806 |
7 files changed, 3183 insertions, 0 deletions
diff --git a/tools/tcpmon/src/Makefile.am b/tools/tcpmon/src/Makefile.am new file mode 100644 index 0000000..674fbca --- /dev/null +++ b/tools/tcpmon/src/Makefile.am @@ -0,0 +1,21 @@ +prgbindir=$(prefix)/bin/tools/tcpmon + +prgbin_PROGRAMS = tcpmon + +tcpmon_SOURCES = tcpmon.c \ + entry.c \ + session.c \ + util.c + +tcpmon_LDADD = \ + ../../../util/src/libaxutil.la \ + ../../../axiom/src/om/libaxis2_axiom.la \ + ../../../axiom/src/parser/$(WRAPPER_DIR)/libaxis2_parser.la + +INCLUDES = -I$(top_builddir)/include \ + -I ../../../util/include \ + -I ../../../axiom/include \ + -I ../../../include \ + -I ../include + +EXTRA_DIST=tcpmon_entry_local.h tcpmon_session_local.h diff --git a/tools/tcpmon/src/entry.c b/tools/tcpmon/src/entry.c new file mode 100644 index 0000000..37d2af1 --- /dev/null +++ b/tools/tcpmon/src/entry.c @@ -0,0 +1,656 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <axutil_utils.h> +#include <axutil_error.h> +#include <axutil_string.h> +#include <axutil_network_handler.h> +#include <axutil_stream.h> +#include <time.h> + +#include "tcpmon_entry_local.h" +#include "tcpmon_session_local.h" +#include "tcpmon_util.h" + +#define AXIS2_TCPMON + +/** + * @brief + */ +typedef struct tcpmon_entry_impl +{ + tcpmon_entry_t entry; + + axis2_char_t *arrived_time; + axis2_char_t *sent_time; + axis2_char_t *sent_data; + axis2_char_t *arrived_data; + axis2_char_t *sent_headers; + axis2_char_t *arrived_headers; + axis2_bool_t is_success; + axis2_char_t *time_diff; + axis2_char_t *test_file_name; + int format_bit; + int sent_data_length; + int arrived_data_length; +} +tcpmon_entry_impl_t; + +#define AXIS2_INTF_TO_IMPL(entry) \ + ((tcpmon_entry_impl_t *) entry) + +/************************* Function prototypes ********************************/ + + +axis2_status_t AXIS2_CALL tcpmon_entry_free( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_arrived_time( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_sent_time( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_time_diff( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_sent_data( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_sent_headers( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_arrived_data( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *AXIS2_CALL tcpmon_entry_arrived_headers( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_bool_t AXIS2_CALL tcpmon_entry_is_success( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_char_t *get_current_stream_to_buffer( + const axutil_env_t * env, + axutil_stream_t * stream, + int *stream_size); + + +int AXIS2_CALL tcpmon_entry_get_format_bit( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +int AXIS2_CALL tcpmon_entry_get_sent_data_length( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +int AXIS2_CALL tcpmon_entry_get_arrived_data_length( + tcpmon_entry_t * entry, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_entry_set_format_bit( + tcpmon_entry_t * entry, + const axutil_env_t * env, + int format_bit); + +/************************** End of function prototypes ************************/ + +tcpmon_entry_t *AXIS2_CALL +tcpmon_entry_create( + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = (tcpmon_entry_impl_t *) AXIS2_MALLOC(env-> + allocator, + sizeof + (tcpmon_entry_impl_t)); + + if (!entry_impl) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + entry_impl->arrived_time = AXIS2_MALLOC(env->allocator, 32); + entry_impl->sent_time = AXIS2_MALLOC(env->allocator, 32); + entry_impl->time_diff = AXIS2_MALLOC(env->allocator, 32); + entry_impl->arrived_data = NULL; + entry_impl->sent_data = NULL; + entry_impl->arrived_headers = NULL; + entry_impl->sent_headers = NULL; + entry_impl->is_success = AXIS2_FALSE; + entry_impl->format_bit = 0; + entry_impl->sent_data_length = 0; + entry_impl->arrived_data_length = 0; + + entry_impl->entry.ops = + AXIS2_MALLOC(env->allocator, sizeof(tcpmon_entry_ops_t)); + if (!entry_impl->entry.ops) + { + tcpmon_entry_free(&(entry_impl->entry), env); + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + entry_impl->entry.ops->free = tcpmon_entry_free; + entry_impl->entry.ops->arrived_time = tcpmon_entry_arrived_time; + entry_impl->entry.ops->sent_time = tcpmon_entry_sent_time; + entry_impl->entry.ops->time_diff = tcpmon_entry_time_diff; + entry_impl->entry.ops->sent_data = tcpmon_entry_sent_data; + entry_impl->entry.ops->sent_headers = tcpmon_entry_sent_headers; + entry_impl->entry.ops->arrived_data = tcpmon_entry_arrived_data; + entry_impl->entry.ops->arrived_headers = tcpmon_entry_arrived_headers; + entry_impl->entry.ops->is_success = tcpmon_entry_is_success; + entry_impl->entry.ops->set_format_bit = tcpmon_entry_set_format_bit; + entry_impl->entry.ops->get_format_bit = tcpmon_entry_get_format_bit; + entry_impl->entry.ops->get_sent_data_length = tcpmon_entry_get_sent_data_length; + entry_impl->entry.ops->get_arrived_data_length = tcpmon_entry_get_arrived_data_length; + + return &(entry_impl->entry); +} + +/***************************Function implementation****************************/ + +axis2_status_t AXIS2_CALL +tcpmon_entry_free( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + if (entry->ops) + { + AXIS2_FREE(env->allocator, entry->ops); + entry->ops = NULL; + } + + if (entry_impl->arrived_time) + { + AXIS2_FREE(env->allocator, entry_impl->arrived_time); + entry_impl->arrived_time = NULL; + } + if (entry_impl->sent_time) + { + AXIS2_FREE(env->allocator, entry_impl->sent_time); + entry_impl->sent_time = NULL; + } + if (entry_impl->time_diff) + { + AXIS2_FREE(env->allocator, entry_impl->time_diff); + entry_impl->time_diff = NULL; + } + if (entry_impl->arrived_data) + { + AXIS2_FREE(env->allocator, entry_impl->arrived_data); + entry_impl->arrived_data = NULL; + } + if (entry_impl->sent_data) + { + AXIS2_FREE(env->allocator, entry_impl->sent_data); + entry_impl->sent_data = NULL; + } + if (entry_impl->arrived_headers) + { + AXIS2_FREE(env->allocator, entry_impl->arrived_headers); + entry_impl->arrived_headers = NULL; + } + if (entry_impl->sent_headers) + { + AXIS2_FREE(env->allocator, entry_impl->sent_headers); + entry_impl->sent_headers = NULL; + } + + if (entry_impl) + { + AXIS2_FREE(env->allocator, entry_impl); + entry_impl = NULL; + } + + return AXIS2_SUCCESS; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_arrived_time( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->arrived_time; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_sent_time( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->sent_time; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_time_diff( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->time_diff; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_sent_data( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->sent_data; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_sent_headers( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->sent_headers; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_arrived_data( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->arrived_data; +} + +axis2_char_t *AXIS2_CALL +tcpmon_entry_arrived_headers( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->arrived_headers; +} + +axis2_bool_t AXIS2_CALL +tcpmon_entry_is_success( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FALSE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->is_success; +} + +int AXIS2_CALL +tcpmon_entry_get_format_bit( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->format_bit; +} + +int AXIS2_CALL +tcpmon_entry_get_sent_data_length( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->sent_data_length; +} + +int AXIS2_CALL +tcpmon_entry_get_arrived_data_length( + tcpmon_entry_t * entry, + const axutil_env_t * env) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + return entry_impl->arrived_data_length; +} + +axis2_status_t AXIS2_CALL +tcpmon_entry_set_format_bit( + tcpmon_entry_t * entry, + const axutil_env_t * env, + int format_bit) +{ + tcpmon_entry_impl_t *entry_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + entry_impl->format_bit = format_bit; + + return AXIS2_SUCCESS; +} + +/** implimentations for protected methods */ + +/** executes as new entry arises */ +void *AXIS2_CALL +tcpmon_entry_new_entry_funct( + axutil_thread_t * thd, + void *data) +{ + tcpmon_entry_request_data_t *req_data = (tcpmon_entry_request_data_t *) data; + const axutil_env_t *env = NULL; + int client_socket = -1; + int host_socket = -1; + tcpmon_session_t *session; + TCPMON_SESSION_TRANS_ERROR_FUNCT on_trans_fault_funct; + TCPMON_SESSION_NEW_ENTRY_FUNCT on_new_entry; + + axutil_stream_t *client_stream = NULL; + axutil_stream_t *host_stream = NULL; + int buffer_size = 0; + axis2_char_t *headers = NULL; + axis2_char_t *content = NULL; + axis2_char_t *buffer = NULL; + time_t now; + struct tm *localTime = NULL; + int arrived_secs = 0; + int sent_secs = 0; + int time_diff_i = 0; + int target_port = 0; + axis2_char_t *target_host = NULL; + int test_bit = 0; + int format_bit = 0; + tcpmon_entry_t *entry = NULL; + tcpmon_entry_impl_t *entry_impl = NULL; + + env = req_data->env; + client_socket = req_data->socket; + session = req_data->session; + on_trans_fault_funct = tcpmon_session_get_on_trans_fault(session, env); + on_new_entry = tcpmon_session_get_on_new_entry(session, env); + + entry = tcpmon_entry_create(env); + entry_impl = AXIS2_INTF_TO_IMPL(entry); + + target_port = TCPMON_SESSION_GET_TARGET_PORT(session, env); + target_host = TCPMON_SESSION_GET_TARGET_HOST(session, env); + if (target_port == -1 || target_host == NULL) + { + axutil_network_handler_close_socket(env, client_socket); + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Missing target port and host" + "input missing"); + if (on_trans_fault_funct) + { + (on_trans_fault_funct) (env, "Missing target port and host"); + } + if (thd) + { + AXIS2_FREE(env->allocator, thd); + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); + } + return NULL; + } + client_stream = axutil_stream_create_socket(env, client_socket); + if (!client_stream) + { + axutil_network_handler_close_socket(env, client_socket); + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, + "Error in creating client stream" "handling response"); + + /** call the callback */ + if (on_trans_fault_funct) + { + (on_trans_fault_funct) (env, "error in creating the client stream"); + } + if (thd) + { + AXIS2_FREE(env->allocator, thd); + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); + } + return NULL; + } + + buffer = tcpmon_util_read_current_stream(env, client_stream, &buffer_size, + &headers, &content); + + headers =(char *) tcpmon_util_str_replace(env, headers,"localhost", target_host); + test_bit = TCPMON_SESSION_GET_TEST_BIT(session, env); + + if (test_bit) + { + tcpmon_util_write_to_file("reqest", buffer); + } + + format_bit = TCPMON_SESSION_GET_FORMAT_BIT(session, env); + TCPMON_ENTRY_SET_FORMAT_BIT(entry, env, format_bit); + + now = time(NULL); + localTime = localtime(&now); + + sprintf(entry_impl->sent_time, "%d:%d:%d", localTime->tm_hour, + localTime->tm_min, localTime->tm_sec); + sent_secs = + localTime->tm_hour * 60 * 60 + localTime->tm_min * 60 + + localTime->tm_sec; + + /*free ( localTime); */ + + entry_impl->sent_headers = headers; + entry_impl->sent_data = content; + entry_impl->sent_data_length = buffer_size; + + if (on_new_entry) + { + (on_new_entry) (env, entry, 0); + } + + host_socket = + (int)axutil_network_handler_open_socket(env, target_host, target_port); + if (-1 == host_socket) + { + axutil_stream_write(client_stream, env, NULL, 0); + axutil_stream_free(client_stream, env); + axutil_network_handler_close_socket(env, client_socket); + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in creating host_socket" + "creating socket"); + + /** call the callback */ + if (on_trans_fault_funct) + { + (on_trans_fault_funct) (env, "error in creating the host socket"); + } + if (thd) + { + AXIS2_FREE(env->allocator, thd); + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); + } + return NULL; + } + + host_stream = axutil_stream_create_socket(env, host_socket); + if (!host_stream) + { + axutil_stream_write(client_stream, env, NULL, 0); + axutil_stream_free(client_stream, env); + axutil_network_handler_close_socket(env, client_socket); + axutil_network_handler_close_socket(env, host_socket); + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in creating host stream" + "handling response"); + + /** call the callback */ + if (on_trans_fault_funct) + { + (on_trans_fault_funct) (env, "error in creating the host stream"); + } + if (thd) + { + AXIS2_FREE(env->allocator, thd); + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); + } + return NULL; + } + + axutil_stream_write(host_stream, env, buffer, buffer_size); + AXIS2_FREE(env->allocator, buffer); + + buffer = tcpmon_util_read_current_stream(env, host_stream, &buffer_size, + &headers, &content); + + test_bit = TCPMON_SESSION_GET_TEST_BIT(session, env); + if (test_bit) + { + tcpmon_util_write_to_file("response", buffer); + } + + now = time(NULL); + localTime = localtime(&now); + + sprintf(entry_impl->arrived_time, "%d:%d:%d", localTime->tm_hour, + localTime->tm_min, localTime->tm_sec); + arrived_secs = + localTime->tm_hour * 60 * 60 + localTime->tm_min * 60 + + localTime->tm_sec; + /*free ( localTime); */ + + time_diff_i = arrived_secs - sent_secs; + if (time_diff_i < 0) + { + time_diff_i += 24 * 60 * 60; + } + sprintf(entry_impl->time_diff, "%d sec(s)", time_diff_i); + + entry_impl->arrived_headers = headers; + entry_impl->arrived_data = content; + entry_impl->arrived_data_length = buffer_size; + if (buffer == NULL || buffer_size == 0) + { + entry_impl->is_success = 0; + } + else + { + entry_impl->is_success = 1; + } + + if (on_new_entry) + { + (on_new_entry) (env, entry, 1); + } + + axutil_stream_write(client_stream, env, buffer, buffer_size); + AXIS2_FREE(env->allocator, buffer); + + axutil_stream_free(client_stream, env); + axutil_stream_free(host_stream, env); + axutil_network_handler_close_socket(env, client_socket); + axutil_network_handler_close_socket(env, host_socket); + + if (entry_impl) + { + tcpmon_entry_free(&(entry_impl->entry), env); + } + if (thd) + { + AXIS2_FREE(env->allocator, thd); + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); + } + return NULL; +} + + diff --git a/tools/tcpmon/src/session.c b/tools/tcpmon/src/session.c new file mode 100644 index 0000000..29d4c78 --- /dev/null +++ b/tools/tcpmon/src/session.c @@ -0,0 +1,632 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <axutil_string.h> +#include <axutil_utils.h> +#include <axutil_error.h> +#include <tcpmon_util.h> +#include <axutil_thread.h> +#include <axutil_network_handler.h> +#include <axutil_array_list.h> + +#include "tcpmon_session_local.h" +#include "tcpmon_entry_local.h" +#include "tcpmon_util.h" + +/** + * @brief + */ +typedef struct tcpmon_session_impl +{ + tcpmon_session_t session; + int listen_port; + int target_port; + int test_bit; + int format_bit; + axis2_char_t *target_host; + TCPMON_SESSION_NEW_ENTRY_FUNCT on_new_entry_funct; + TCPMON_SESSION_TRANS_ERROR_FUNCT on_trans_fault_funct; + axutil_array_list_t *entries; + + axis2_bool_t is_running; +} +tcpmon_session_impl_t; + +typedef struct tcpmon_session_server_thread_data +{ + tcpmon_session_impl_t *session_impl; + const axutil_env_t *env; +} +tcpmon_session_server_thread_data_t; + +#define AXIS2_INTF_TO_IMPL(session) \ + ((tcpmon_session_impl_t *) session) +axutil_thread_t *server_thread = NULL; +tcpmon_session_server_thread_data_t *thread_data = NULL; + + +/************************* Function prototypes ********************************/ + +axis2_status_t AXIS2_CALL tcpmon_session_free( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_set_listen_port( + tcpmon_session_t * session, + const axutil_env_t * env, + int listen_port); + +int AXIS2_CALL tcpmon_session_get_listen_port( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_set_target_port( + tcpmon_session_t * session, + const axutil_env_t * env, + int target_port); + +int AXIS2_CALL tcpmon_session_get_target_port( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_set_target_host( + tcpmon_session_t * session, + const axutil_env_t * env, + axis2_char_t * target_host); + +axis2_char_t *AXIS2_CALL tcpmon_session_get_target_host( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_start( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_stop( + tcpmon_session_t * session, + const axutil_env_t * env); + +axis2_status_t AXIS2_CALL tcpmon_session_on_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env, + TCPMON_SESSION_NEW_ENTRY_FUNCT on_new_entry_funct); + +axis2_status_t AXIS2_CALL tcpmon_session_on_trans_fault( + tcpmon_session_t * session, + const axutil_env_t * env, + TCPMON_SESSION_TRANS_ERROR_FUNCT on_trans_fault_funct); + +int AXIS2_CALL tcpmon_session_get_test_bit( + tcpmon_session_t * session, + const axutil_env_t * env); + +int AXIS2_CALL tcpmon_session_set_test_bit( + tcpmon_session_t * session, + const axutil_env_t * env, + int test_bit); + +int AXIS2_CALL tcpmon_session_get_format_bit( + tcpmon_session_t * session, + const axutil_env_t * env); + +int AXIS2_CALL tcpmon_session_set_format_bit( + tcpmon_session_t * session, + const axutil_env_t * env, + int format_bit); + +/** internal implementations */ + +static void *AXIS2_THREAD_FUNC server_funct( + axutil_thread_t * thd, + void *data); + +/************************** End of function prototypes ************************/ + +tcpmon_session_t *AXIS2_CALL +tcpmon_session_create( + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + session_impl = (tcpmon_session_impl_t *) AXIS2_MALLOC(env-> + allocator, + sizeof + (tcpmon_session_impl_t)); + + if (!session_impl) + { + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + session_impl->listen_port = -1; + session_impl->target_port = -1; + session_impl->test_bit = -1; + session_impl->format_bit = 0; + session_impl->target_host = NULL; + + session_impl->on_new_entry_funct = NULL; + session_impl->on_trans_fault_funct = NULL; + session_impl->entries = + axutil_array_list_create(env, AXIS2_ARRAY_LIST_DEFAULT_CAPACITY); + + session_impl->session.ops = + AXIS2_MALLOC(env->allocator, sizeof(tcpmon_session_ops_t)); + if (!session_impl->session.ops) + { + tcpmon_session_free(&(session_impl->session), env); + AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); + return NULL; + } + + session_impl->is_running = AXIS2_FALSE; + session_impl->session.ops->free = tcpmon_session_free; + session_impl->session.ops->set_test_bit = tcpmon_session_set_test_bit; + session_impl->session.ops->get_test_bit = tcpmon_session_get_test_bit; + session_impl->session.ops->get_format_bit = tcpmon_session_get_format_bit; + session_impl->session.ops->set_format_bit = tcpmon_session_set_format_bit; + session_impl->session.ops->set_listen_port = tcpmon_session_set_listen_port; + session_impl->session.ops->get_listen_port = tcpmon_session_get_listen_port; + session_impl->session.ops->set_target_port = tcpmon_session_set_target_port; + session_impl->session.ops->get_target_port = tcpmon_session_get_target_port; + session_impl->session.ops->set_target_host = tcpmon_session_set_target_host; + session_impl->session.ops->get_target_host = tcpmon_session_get_target_host; + session_impl->session.ops->start = tcpmon_session_start; + session_impl->session.ops->stop = tcpmon_session_stop; + session_impl->session.ops->on_new_entry = tcpmon_session_on_new_entry; + session_impl->session.ops->on_trans_fault = tcpmon_session_on_trans_fault; + + return &(session_impl->session); +} + +/***************************Function implementation****************************/ + +axis2_status_t AXIS2_CALL +tcpmon_session_free( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + int entries_size = 0; + tcpmon_entry_t *entry = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + for (entries_size = axutil_array_list_size(session_impl->entries, env) - 1; + entries_size >= 0; entries_size--) + { + TCPMON_ENTRY_FREE(entry, env); + } + axutil_array_list_free(session_impl->entries, env); + + if (session->ops) + { + AXIS2_FREE(env->allocator, session->ops); + session->ops = NULL; + } + + if (session_impl->target_host) + { + AXIS2_FREE(env->allocator, session_impl->target_host); + session_impl->target_host = NULL; + } + + if (session_impl) + { + AXIS2_FREE(env->allocator, session_impl); + session_impl = NULL; + } + + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_set_test_bit( + tcpmon_session_t * session, + const axutil_env_t * env, + int test_bit) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + session_impl->test_bit = test_bit; + return AXIS2_SUCCESS; +} + +int AXIS2_CALL +tcpmon_session_get_test_bit( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->test_bit; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_set_format_bit( + tcpmon_session_t * session, + const axutil_env_t * env, + int format_bit) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->format_bit = format_bit; + + return AXIS2_SUCCESS; +} + +int AXIS2_CALL +tcpmon_session_get_format_bit( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->format_bit; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_set_listen_port( + tcpmon_session_t * session, + const axutil_env_t * env, + int listen_port) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->listen_port = listen_port; + return AXIS2_SUCCESS; +} + +int AXIS2_CALL +tcpmon_session_get_listen_port( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, -1); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->listen_port; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_set_target_port( + tcpmon_session_t * session, + const axutil_env_t * env, + int target_port) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->target_port = target_port; + return AXIS2_SUCCESS; +} + +int AXIS2_CALL +tcpmon_session_get_target_port( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, -1); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->target_port; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_set_target_host( + tcpmon_session_t * session, + const axutil_env_t * env, + axis2_char_t * target_host) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->target_host = + (axis2_char_t *) axutil_strdup(env, target_host); + return AXIS2_SUCCESS; +} + +axis2_char_t *AXIS2_CALL +tcpmon_session_get_target_host( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->target_host; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_start( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + thread_data = + (tcpmon_session_server_thread_data_t *) AXIS2_MALLOC(env->allocator, + sizeof + (tcpmon_session_server_thread_data_t)); + thread_data->session_impl = session_impl; + thread_data->env = env; + + session_impl->is_running = AXIS2_TRUE; + server_thread = axutil_thread_pool_get_thread(env->thread_pool, + server_funct, + (void *) thread_data); + if (!server_thread) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Thread creation failed" + "server thread"); + if (session_impl->on_trans_fault_funct) + { + (session_impl->on_trans_fault_funct) (env, + "error in creating the server thread"); + } + } + + axutil_thread_pool_thread_detach(env->thread_pool, server_thread); + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_stop( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + session_impl->is_running = AXIS2_FALSE; + + if (server_thread) + { + AXIS2_FREE(env->allocator, server_thread); + server_thread = NULL; + } + if (thread_data) + { + AXIS2_FREE(env->allocator, (tcpmon_session_server_thread_data_t *)thread_data); + thread_data = NULL; + } + + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_on_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env, + TCPMON_SESSION_NEW_ENTRY_FUNCT on_new_entry_funct) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->on_new_entry_funct = on_new_entry_funct; + + return AXIS2_SUCCESS; +} + +axis2_status_t AXIS2_CALL +tcpmon_session_on_trans_fault( + tcpmon_session_t * session, + const axutil_env_t * env, + TCPMON_SESSION_TRANS_ERROR_FUNCT on_trans_fault_funct) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + session_impl->on_trans_fault_funct = on_trans_fault_funct; + return AXIS2_SUCCESS; +} + +/** internal implementations */ +static void *AXIS2_THREAD_FUNC +server_funct( + axutil_thread_t * thd, + void *data) +{ + tcpmon_session_server_thread_data_t *thread_data = (tcpmon_session_server_thread_data_t *)data; + tcpmon_session_impl_t *session_impl = NULL; + const axutil_env_t *env = NULL; + int listen_socket = -1; + int socket = -1; + axutil_thread_t *request_thread = NULL; + tcpmon_entry_request_data_t *request_thread_data = NULL; + + session_impl = thread_data->session_impl; + env = thread_data->env; + + listen_socket = (int)axutil_network_handler_create_server_socket + (env, session_impl->listen_port); + if (-1 == listen_socket) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, + "error in creating the server socket, " + "port may be already occupied", "create socket"); + if (session_impl->on_trans_fault_funct) + { + (session_impl->on_trans_fault_funct) (env, + "error in creating the server socket, " + "port may be already occupied"); + } + if (thd) + { + AXIS2_FREE(env->allocator, server_thread); + server_thread = NULL; + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_session_server_thread_data_t *)data); + thread_data = NULL; + } + return NULL; + } + while (session_impl->is_running) + { + socket = (int)axutil_network_handler_svr_socket_accept(env, listen_socket); + if (socket == -1) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, + "error in creating the socket" "create socket"); + if (session_impl->on_trans_fault_funct) + { + (session_impl->on_trans_fault_funct) (env, + "error in creating the socket"); + } + break; + } + + request_thread_data = + (tcpmon_entry_request_data_t *) AXIS2_MALLOC(env->allocator, + sizeof + (tcpmon_entry_request_data_t)); + request_thread_data->env = env; + request_thread_data->socket = socket; + request_thread_data->session = (tcpmon_session_t *) session_impl; + + request_thread = axutil_thread_pool_get_thread(env->thread_pool, + tcpmon_entry_new_entry_funct, + (void *) + request_thread_data); + if (!request_thread) + { + AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Thread creation failed" + "request thread"); + if (session_impl->on_trans_fault_funct) + { + (session_impl->on_trans_fault_funct) (env, + "fail in creating the thread"); + } + break; + } + + axutil_thread_pool_thread_detach(env->thread_pool, request_thread); + } + axutil_network_handler_close_socket(env, listen_socket); + if (thd) + { + AXIS2_FREE(env->allocator, server_thread); + server_thread = NULL; + } + if (data) + { + AXIS2_FREE(env->allocator, (tcpmon_session_server_thread_data_t *)data); + thread_data = NULL; + } + return NULL; +} + +/* implementations for protected functions */ + +axis2_status_t +tcpmon_session_add_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env, + tcpmon_entry_t * entry) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, AXIS2_FAILURE); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + axutil_array_list_add(session_impl->entries, env, entry); + return AXIS2_SUCCESS; + +} + +TCPMON_SESSION_TRANS_ERROR_FUNCT +tcpmon_session_get_on_trans_fault( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->on_trans_fault_funct; +} + +TCPMON_SESSION_NEW_ENTRY_FUNCT +tcpmon_session_get_on_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env) +{ + tcpmon_session_impl_t *session_impl = NULL; + + AXIS2_ENV_CHECK(env, NULL); + + session_impl = AXIS2_INTF_TO_IMPL(session); + + return session_impl->on_new_entry_funct; +} diff --git a/tools/tcpmon/src/tcpmon.c b/tools/tcpmon/src/tcpmon.c new file mode 100644 index 0000000..567bfc3 --- /dev/null +++ b/tools/tcpmon/src/tcpmon.c @@ -0,0 +1,1037 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axutil_utils.h> +#include <axutil_uuid_gen.h> +#include <axutil_error_default.h> +#include <axutil_log_default.h> +#include <axutil_thread_pool.h> +#include <tcpmon_session.h> +#include <tcpmon_entry.h> +#include <tcpmon_util.h> +#include <signal.h> +#include <stdio.h> +#include <axutil_stream.h> +#include <axutil_network_handler.h> +#include <axis2_http_transport.h> +#include <axutil_version.h> + +#include <tcpmon_util.h> + +#define SIZE 1024 +axis2_char_t *tcpmon_traffic_log = "tcpmon_traffic.log"; +axutil_env_t *system_env = NULL; +tcpmon_session_t *session = NULL; +char *target_host = NULL; + +int on_new_entry( + const axutil_env_t * env, + tcpmon_entry_t * entry, + int status); + +int on_new_entry_to_file( + const axutil_env_t * env, + tcpmon_entry_t * entry, + int status); + +int on_error_func( + const axutil_env_t * env, + char *error_message); + + +void sig_handler( + int signal); + +int resend_request( + const axutil_env_t * env, + int status); + +int +main( + int argc, + char **argv) +{ + axutil_env_t *env = NULL; + int c; + int listen_port = 9099, + target_port = 9090; /* the default port simple axis2 server is run on 9090 */ + int test_bit = 0; + int format_bit = 0; /* pretty print the request/response SOAP messages */ + int ii = 1; + + if (!axutil_strcmp(argv[1], "-h")) + { + printf + ("Usage : %s [-lp LISTEN_PORT] [-tp TARGET_PORT] [-th TARGET_HOST] [-f LOG_FILE] [options]\n", + argv[0]); + fprintf(stdout, " Options :\n"); + fprintf(stdout, + "\t-lp LISTEN_PORT \tport number to listen on, default is 9099\n"); + fprintf(stdout, + "\t-tp TARGET_PORT \tport number to connect and re-direct messages, default is 9090\n"); + fprintf(stdout, + "\t-th TARGET_HOST \ttarget host to connect, default is localhost\n"); + fprintf(stdout, + "\t-f LOG_FILE \tfile to write the messages to, default is %s\n", + tcpmon_traffic_log); + fprintf(stdout, + "\t--format \tenable xml formatting\n"); + fprintf(stdout, + "\t--test \tenable testing last request/response by logging it separately\n"); + fprintf(stdout, " Help :\n\t-h \tdisplay this help screen.\n\n"); + return 0; + } + + env = axutil_env_create_all("axis2_tcpmon.log", AXIS2_LOG_LEVEL_DEBUG); + + signal(SIGINT, sig_handler); + system_env = env; +#ifndef WIN32 + signal(SIGPIPE, sig_handler); +#endif + + target_host = axutil_strdup(env, "localhost"); + + while (ii < argc) + { + if (!strcmp("-lp", argv[ii])) + { + ii++; + if (argv[ii]) + { + listen_port = atoi(argv[ii++]); + if (listen_port == 0) + { + printf("INVALID value for listen port\n"); + printf("Use -h for help\n"); + AXIS2_FREE(env->allocator, target_host); + if (env) + { + axutil_env_free((axutil_env_t *) env); + env = NULL; + } + return 0; + } + } + } + else if (!strcmp("-tp", argv[ii])) + { + ii++; + if (argv[ii]) + { + target_port = atoi(argv[ii++]); + if (target_port == 0) + { + printf("INVALID value for target port\n"); + printf("Use -h for help\n"); + AXIS2_FREE(env->allocator, target_host); + if (env) + { + axutil_env_free((axutil_env_t *) env); + env = NULL; + } + return 0; + } + } + } + else if (!strcmp("-th", argv[ii])) + { + ii++; + if (argv[ii]) + { + AXIS2_FREE(env->allocator, target_host); + target_host = (char *) axutil_strdup(env, argv[ii++]); + } + } + else if (!strcmp("--test", argv[ii])) + { + ii++; + test_bit = 1; + } + else if (!strcmp("--format", argv[ii])) + { + ii++; + format_bit = 1; + } + else if (!strcmp("-f", argv[ii])) + { + ii++; + if (argv[ii]) + { + tcpmon_traffic_log = argv[ii++]; + } + } + else + { + printf("INVALID value for tcpmon \n"); + printf("Use -h for help\n"); + AXIS2_FREE(env->allocator, target_host); + if (env) + { + axutil_env_free((axutil_env_t *) env); + env = NULL; + } + return 0; + } + } + + if (!(listen_port && target_port && target_host)) + { + printf("ERROR: essential argument missing \n"); + printf + ("Please recheck values of listen_port (-lp), target_port(-tp) and target_host (-th)\n"); + AXIS2_FREE(env->allocator, target_host); + if (env) + { + axutil_env_free((axutil_env_t *) env); + env = NULL; + } + return 0; + } + + printf("Listen port : %d Target port : %d Target host: %s\n", + listen_port, target_port, target_host); + session = tcpmon_session_create(env); + + TCPMON_SESSION_SET_LISTEN_PORT(session, env, listen_port); + TCPMON_SESSION_SET_TARGET_PORT(session, env, target_port); + TCPMON_SESSION_SET_TARGET_HOST(session, env, target_host); + TCPMON_SESSION_ON_TRANS_FAULT(session, env, on_error_func); + + TCPMON_SESSION_ON_NEW_ENTRY(session, env, on_new_entry_to_file); + + TCPMON_SESSION_SET_TEST_BIT(session, env, test_bit); + TCPMON_SESSION_SET_FORMAT_BIT(session, env, format_bit); + TCPMON_SESSION_START(session, env); + + do + { + c = getchar(); + if (c == 'f') + { + format_bit = format_bit ? 0 : 1; + TCPMON_SESSION_SET_FORMAT_BIT(session, env, format_bit); + } + else if (c == 'r') + { + resend_request(env, 0); + } + } + while (c != 'q'); + printf("\n\n"); + + TCPMON_SESSION_STOP(session, env); + TCPMON_SESSION_FREE(session, env); + AXIS2_FREE(env->allocator, target_host); + if (env) + { + axutil_env_free((axutil_env_t *) env); + env = NULL; + } + return 0; +} + +int +on_new_entry_to_file( + const axutil_env_t * env, + tcpmon_entry_t * entry, + int status) +{ + char *plain_buffer = NULL; + char *formated_buffer = NULL; + int format = 0; + FILE *file; + char *convert = NULL; + char *uuid = NULL; + int resend = 0; + + file = fopen(tcpmon_traffic_log, "ab"); + + if (NULL == file) + { + printf("\ncould not create or open log-file\n"); + return -1; + } + + fprintf(file, + "\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n"); + + format = TCPMON_ENTRY_GET_FORMAT_BIT(entry, env); + + if (status == 0) + { + if (strstr(TCPMON_ENTRY_SENT_HEADERS(entry, env), AXIS2_HTTP_HEADER_USER_AGENT + ": " AXIS2_HTTP_HEADER_SERVER_AXIS2C "TCPMon\r\n")) + { + resend = 1; + } + plain_buffer = TCPMON_ENTRY_SENT_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + if (TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) != + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)) + + (int)strlen(plain_buffer) + 4) + { + format = 0; /* mtom scenario */ + } + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + /* 2 screen */ + printf("\n\n%s\n", resend ? "RESENDING DATA..": "SENDING DATA.."); + printf("/* sending time = %s*/\n", TCPMON_ENTRY_SENT_TIME(entry, env)); + uuid = axutil_uuid_gen(env); + printf("/* message uuid = %s*/\n", uuid); + printf("---------------------\n"); + + if (format || TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + printf("%s\n\n%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env), + formated_buffer); + } + else + { + int count = 0; + int printed = 0; + axis2_char_t *formated_buffer_temp = formated_buffer; + printf("%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env)); + count = TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + printf("%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + printf("%c", '\0'); + formated_buffer += plen; + } + } + formated_buffer = formated_buffer_temp; + } + + /* 2 file */ + fprintf(file, "%s\n", resend ? "RESENDING DATA..": "SENDING DATA.."); + fprintf(file, "/* sending time = %s*/\n", + TCPMON_ENTRY_SENT_TIME(entry, env)); + fprintf(file, "/* message uuid = %s*/\n", uuid); + AXIS2_FREE(env->allocator, uuid); + fprintf(file, "---------------------\n"); + + convert = axutil_strdup(env, TCPMON_ENTRY_SENT_HEADERS(entry, env)); + convert = tcpmon_util_str_replace(env, convert, "; ", ";\n\t"); + fprintf(file, "%s\r\n\r\n", convert); + if (convert) + { + AXIS2_FREE(env->allocator, convert); + convert = NULL; + } + if (strcmp(formated_buffer, "") != 0) + { + if (format || TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + convert = axutil_strdup(env, formated_buffer); + convert = tcpmon_util_str_replace(env, convert, "><", ">\n<"); + fprintf(file, "%s", convert); + if (convert) + { + AXIS2_FREE(env->allocator, convert); + convert = NULL; + } + } + else + { + int count = 0; + int printed = 0; + count = TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + fprintf(file, "%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + fprintf(file, "%c", '\0'); + formated_buffer += plen; + } + } + } + } + } + if (status == 1) + { + plain_buffer = TCPMON_ENTRY_ARRIVED_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + if (TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) != + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)) + + (int)strlen(plain_buffer) + 4) + { + format = 0; /* mtom scenario */ + } + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + /* 2 screen */ + printf("\n\n%s\n", "RETRIEVING DATA.."); + printf("/* retrieving time = %s*/\n", + TCPMON_ENTRY_ARRIVED_TIME(entry, env)); + printf("/* time throughput = %s*/\n", + TCPMON_ENTRY_TIME_DIFF(entry, env)); + printf("---------------------\n"); + + if (format || TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + printf("%s\n\n%s\n\n", TCPMON_ENTRY_ARRIVED_HEADERS(entry, env), + formated_buffer); + } + else + { + int count = 0; + int printed = 0; + axis2_char_t *formated_buffer_temp = formated_buffer; + printf("%s\n\n", TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + count = TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + printf("%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + printf("%c", '\0'); + formated_buffer += plen; + } + } + formated_buffer = formated_buffer_temp; + } + + /* 2 file */ + fprintf(file, "%s\n", "RETRIEVING DATA.."); + fprintf(file, "/* retrieving time = %s*/\n", + TCPMON_ENTRY_ARRIVED_TIME(entry, env)); + fprintf(file, "/* time throughput = %s*/\n", + TCPMON_ENTRY_TIME_DIFF(entry, env)); + fprintf(file, "---------------------\n"); + + convert = axutil_strdup(env, TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + convert = tcpmon_util_str_replace(env, convert, "; ", ";\n\t"); + fprintf(file, "%s\r\n\r\n", convert); + if (convert) + { + AXIS2_FREE(env->allocator, convert); + convert = NULL; + } + if (strcmp(formated_buffer, "") != 0) + { + if (format || TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + convert = axutil_strdup(env, formated_buffer); + convert = tcpmon_util_str_replace(env, convert, "><", ">\n<"); + fprintf(file, "%s", convert); + if (convert) + { + AXIS2_FREE(env->allocator, convert); + convert = NULL; + } + } + else + { + int count = 0; + int printed = 0; + count = TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + fprintf(file, "%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + fprintf(file, "%c", '\0'); + formated_buffer += plen; + } + } + } + } + } + fclose(file); + return 0; +} + +int +resend_request( + const axutil_env_t * env, + int status) +{ + FILE *file; + axis2_char_t *uuid = NULL; + axis2_char_t *buffer = NULL; + int read_len = 0; + + if (status == 0) + { + int c; + int i = 0; + do + { + c = getchar(); + } + while (c == ' '); + uuid = AXIS2_MALLOC(env->allocator, sizeof(axis2_char_t) * 37); + for (i = 0; i < 36; i++) + { + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || c == '-') + { + uuid[i] = (axis2_char_t)c; + } + else if (c >= 'A' && c <= 'F') + { + uuid[i] = (axis2_char_t)(c + 32); + } + else + { + return 0; + } + c = getchar(); + } + uuid[i] = '\0'; + } + + file = fopen(tcpmon_traffic_log, "rb"); + + if (!file) + { + printf("\ncould not create or open log-file\n"); + return -1; + } + + buffer = AXIS2_MALLOC(env->allocator, sizeof(axis2_char_t) * SIZE); + if (!buffer) + { + printf("\ngimme more memory\n"); + return -1; + } + buffer[SIZE - 1] = '\0'; + + read_len = (int)fread(buffer, sizeof(char), SIZE - 1, file); + + while(read_len) + { + axis2_char_t *search = "/* message uuid = "; + axis2_char_t *tmp1 = NULL; + axis2_char_t *tmp2 = NULL; + axis2_char_t *tmp3 = NULL; + axis2_char_t *uuid_match = NULL; + int offset = 0; + int loop_state = 1; + int end_reached = 0; + int rounds = 0; + + offset = (int)strlen(search); + tmp3 = buffer; + if (read_len >= SIZE) + { + AXIS2_FREE(env->allocator, buffer); + AXIS2_FREE(env->allocator, uuid); + printf("\nbuffer overflow\n"); + return -1; + } + if (read_len < SIZE - 1) + { + end_reached = 1; + } + while (loop_state) + { + int temp_len = 0; + tmp1 = strstr(tmp3, search); + temp_len = (int)strlen(tmp3) + 1; + /* loop below is for mtom cases */ + while (!tmp1 && (read_len - rounds > temp_len)) + { + tmp3 += (int)strlen(tmp3) + 1; + tmp1 = strstr(tmp3, search); + temp_len += (int)strlen(tmp3) + 1; + } + if (!tmp1) + { + if (end_reached) + { + break; + } + memmove(buffer, buffer + (SIZE - 1 - offset), offset); + read_len = (int)fread(buffer + offset, sizeof(char), + SIZE - 1 - offset, file) + offset; + break; + } + else + { + rounds = (int)(tmp1 - tmp3) + offset + 36; + tmp3 = tmp1 + offset + 36; + } + if (read_len - offset - 36 < (int)(tmp1 - buffer)) + { + if (end_reached) + { + break; + } + offset += 36; + memmove(buffer, buffer + (SIZE - 1 - offset), offset); + read_len = (int)fread(buffer + offset, sizeof(char), + SIZE - 1 - offset, file) + offset; + break; + } + tmp2 = tmp1 + offset; + uuid_match = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * 37); + if (!uuid_match) + { + printf("\ngimme more memory\n"); + return -1; + } + memcpy(uuid_match, tmp2, 36); + uuid_match[36] = '\0'; + if (!axutil_strcasecmp(uuid_match, uuid)) + { + axis2_char_t *header_str = "*/\n---------------------\n"; + axis2_char_t *footer_str = + "\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ="; + int seek_len = 0; + int has_raw_binary = 0; + axis2_char_t *request_buffer = NULL; + AXIS2_FREE(env->allocator, uuid_match); + AXIS2_FREE(env->allocator, uuid); + end_reached = 1; + tmp2 += 36; + offset = (int)strlen(header_str); + if (read_len - offset < (int)(tmp2 - buffer)) + { + seek_len = (int)(tmp2 - buffer) + offset - read_len; + if (seek_len > 0) + { + read_len = fread(buffer, sizeof(char), seek_len, file); + } + seek_len = 0; + } + else + { + seek_len = read_len - (int)(tmp2 - buffer) - offset; + } + request_buffer = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * (48 * 1024 + 1)); + if (!request_buffer) + { + printf("\ngimme more memory\n"); + return -1; + } + if (seek_len > 0) + { + memcpy(request_buffer, buffer + (read_len - seek_len), seek_len); + } + read_len = (int)fread(request_buffer + seek_len, + sizeof(char), 48 * 1024 - seek_len, file) + seek_len; + tmp1 = NULL; + tmp3 = request_buffer; + tmp1 = strstr(tmp3, footer_str); + temp_len = (int)strlen(tmp3) + 1; + /* loop below is for mtom cases */ + while (!tmp1 && (read_len > temp_len)) + { + if (!has_raw_binary) + { + has_raw_binary = 1; + } + tmp3 += (int)strlen(tmp3) + 1; + tmp1 = strstr(tmp3, footer_str); + temp_len += (int)strlen(tmp3) + 1; + } + if (tmp1) + { + axis2_char_t *req_header = NULL; + axis2_char_t *req_payload = NULL; + int req_content_len = 0; + req_content_len = (int)(tmp1 - request_buffer) - 4; + *tmp1 = '\0'; + tmp1 = NULL; + tmp1 = strstr(request_buffer, "\r\n\r\n"); + if (tmp1) + { + axis2_char_t *listen_host = "localhost"; + int write_socket = -1; + axutil_stream_t *write_stream = NULL; + tmp1 += 2; + *tmp1 = '\0'; + req_payload = tmp1 + 2; + tmp1 = axutil_strdup(env, request_buffer); + req_content_len -= (int)strlen(tmp1); + tmp1 = tcpmon_util_str_replace(env, tmp1, ";\n\t", "; "); + req_header = tmp1; + tmp2 = strstr(req_header, AXIS2_HTTP_HEADER_USER_AGENT ":"); + if (tmp2) + { + tmp3 = strstr(tmp2, "\r\n"); + if (tmp3) + { + int header_len = 0; + axis2_char_t *user_agent = AXIS2_HTTP_HEADER_USER_AGENT + ": " AXIS2_HTTP_HEADER_SERVER_AXIS2C " TCPMon"; + header_len = (int)(tmp3 - tmp2) + 2; + tmp1 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * header_len + 1); + memcpy(tmp1, tmp2, header_len); + tmp1[header_len] = '\0'; + header_len = 2 + (int)strlen(user_agent); + tmp2 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * (header_len + 1)); + sprintf(tmp2, "%s\r\n", user_agent); + req_header = tcpmon_util_str_replace(env, req_header, tmp1, tmp2); + AXIS2_FREE(env->allocator, tmp1); + AXIS2_FREE(env->allocator, tmp2); + tmp1 = NULL; + tmp2 = NULL; + } + } + if (!has_raw_binary) + { + tmp2 = strstr(req_header, AXIS2_HTTP_HEADER_CONTENT_LENGTH ":"); + if (tmp2) + { + tmp3 = strstr(tmp2, "\r\n"); + if (tmp3) + { + int header_len = 0; + header_len = (int)(tmp3 - tmp2) + 2; + tmp1 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * header_len + 1); + memcpy(tmp1, tmp2, header_len); + tmp1[header_len] = '\0'; + tmp2 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * (header_len + 2)); + req_content_len = (int)strlen(req_payload); + sprintf(tmp2, "%s%d\r\n", AXIS2_HTTP_HEADER_CONTENT_LENGTH + ": ", req_content_len); + req_header = tcpmon_util_str_replace(env, req_header, tmp1, tmp2); + AXIS2_FREE(env->allocator, tmp1); + AXIS2_FREE(env->allocator, tmp2); + tmp1 = NULL; + tmp2 = NULL; + } + } + } + tmp2 = strstr(req_header, AXIS2_HTTP_HEADER_HOST ":"); + if (tmp2) + { + tmp3 = strstr(tmp2, "\r\n"); + if (tmp3) + { + int header_len = 0; + header_len = (int)(tmp3 - tmp2) + 2; + tmp1 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * header_len + 1); + memcpy(tmp1, tmp2, header_len); + tmp1[header_len] = '\0'; + header_len = 16 + (int)strlen(target_host); + tmp2 = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * (header_len + 1)); + sprintf(tmp2, "%s%s:%d\r\n", AXIS2_HTTP_HEADER_HOST ": ", target_host, + TCPMON_SESSION_GET_LISTEN_PORT(session, env)); + req_header = tcpmon_util_str_replace(env, req_header, tmp1, tmp2); + AXIS2_FREE(env->allocator, tmp1); + AXIS2_FREE(env->allocator, tmp2); + tmp1 = NULL; + tmp2 = NULL; + } + } + write_socket = + (int)axutil_network_handler_open_socket(env, listen_host, + TCPMON_SESSION_GET_LISTEN_PORT(session, env)); + if (write_socket == -1) + { + printf("\nerror in creating socket\n"); + } + else + { + write_stream = axutil_stream_create_socket(env, write_socket); + } + if (!write_stream) + { + printf("\nerror in creating stream\n"); + } + else + { + axutil_stream_write(write_stream, env, req_header, strlen(req_header)); + axutil_stream_write(write_stream, env, "\r\n", 2); + axutil_stream_write(write_stream, env, req_payload, req_content_len); + axutil_stream_free(write_stream, env); + axutil_network_handler_close_socket(env, write_socket); + } + AXIS2_FREE(env->allocator, req_header); + } + } + else if (read_len == 48 * 1024) + { + printf("\nrequest size greater than buffer\n"); + } + AXIS2_FREE(env->allocator, request_buffer); + break; + } + AXIS2_FREE(env->allocator, uuid_match); + } + if (end_reached) + { + break; + } + } + AXIS2_FREE(env->allocator, buffer); + + fclose(file); + return 0; +} + +int +on_new_entry( + const axutil_env_t * env, + tcpmon_entry_t * entry, + int status) +{ + char *plain_buffer = NULL; + char *formated_buffer = NULL; + int format = 0; + char *uuid = NULL; + int resend = 0; + + format = TCPMON_ENTRY_GET_FORMAT_BIT(entry, env); + + if (status == 0) + { + if (strstr(TCPMON_ENTRY_SENT_HEADERS(entry, env), + AXIS2_HTTP_HEADER_USER_AGENT ": "\ + AXIS2_HTTP_HEADER_SERVER_AXIS2C " TCPMon\r\n")) + { + resend = 1; + } + plain_buffer = TCPMON_ENTRY_SENT_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + if (TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)) + + (int)strlen(plain_buffer) + 4) + { + format = 0; /* mtom scenario */ + } + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + printf("\n\n%s\n", resend ? "RESENDING DATA..": "SENDING DATA.."); + printf("/* sending time = %s*/\n", TCPMON_ENTRY_SENT_TIME(entry, env)); + uuid = axutil_uuid_gen(env); + printf("/* message uuid = %s*/\n", uuid); + AXIS2_FREE(env->allocator, uuid); + printf("---------------------\n"); + + if (format || TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + printf("%s\n\n%s\n\n", TCPMON_ENTRY_SENT_HEADERS(entry, env), + formated_buffer); + } + else + { + int count = 0; + int printed = 0; + axis2_char_t *formated_buffer_temp = formated_buffer; + printf("%s\n", TCPMON_ENTRY_SENT_HEADERS(entry, env)); + count = TCPMON_ENTRY_GET_SENT_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_SENT_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + printf("%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + printf("%c", '\0'); + formated_buffer += plen; + } + } + formated_buffer = formated_buffer_temp; + } + } + if (status == 1) + { + plain_buffer = TCPMON_ENTRY_ARRIVED_DATA(entry, env); + if (plain_buffer) /* this can be possible as no xml present */ + { + if (TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)) + + (int)strlen(plain_buffer) + 4) + { + format = 0; /* mtom scenario */ + } + formated_buffer = tcpmon_util_format_as_xml + (env, plain_buffer, format); + } + else + { + formated_buffer = ""; + } + printf("\n\n%s\n", "RETRIEVING DATA.."); + printf("/* retrieving time = %s*/\n", + TCPMON_ENTRY_ARRIVED_TIME(entry, env)); + printf("/* time throughput = %s*/\n", + TCPMON_ENTRY_TIME_DIFF(entry, env)); + printf("---------------------\n"); + + if (format || TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) == + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)) + + (int)strlen(formated_buffer) + 4) + { + printf("%s\n\n%s\n\n", TCPMON_ENTRY_ARRIVED_HEADERS(entry, env), + formated_buffer); + } + else + { + int count = 0; + int printed = 0; + axis2_char_t *formated_buffer_temp = formated_buffer; + printf("%s\n", TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + count = TCPMON_ENTRY_GET_ARRIVED_DATA_LENGTH(entry, env) - 4 - + (int)strlen(TCPMON_ENTRY_ARRIVED_HEADERS(entry, env)); + while (count > printed) + { + int plen = 0; + plen = ((int)strlen(formated_buffer) + 1); + if (plen != 1) + { + printf("%s", formated_buffer); + } + printed += plen; + if (count > printed) + { + printf("%c", '\0'); + formated_buffer += plen; + } + } + formated_buffer = formated_buffer_temp; + } + } + return 0; +} + +int +on_error_func( + const axutil_env_t * env, + char *error_message) +{ + fprintf(stderr, "ERROR: %s\n", error_message); + return 0; +} + + +/** + * Signal handler + */ +void +sig_handler( + int signal) +{ + + if (!system_env) + { + AXIS2_LOG_ERROR(system_env->log, AXIS2_LOG_SI, + "Received signal %d, unable to proceed system_env is NULL,\ + system exit with -1", signal); + _exit (-1); + } + + switch (signal) + { + case SIGINT: + { + AXIS2_LOG_INFO(system_env->log, "Received signal SIGINT. Utility " + "shutting down"); + printf("\n\n"); + TCPMON_SESSION_STOP(session, system_env); + TCPMON_SESSION_FREE(session, system_env); + AXIS2_FREE(system_env->allocator, target_host); + if (system_env) + { + axutil_env_free(system_env); + } + exit(0); + } +#ifndef WIN32 + case SIGPIPE: + { + AXIS2_LOG_INFO(system_env->log, "Received signal SIGPIPE. Operation " + "aborted"); + return; + } +#endif + case SIGSEGV: + { + fprintf(stderr, "Received deadly signal SIGSEGV. Terminating\n"); + _exit(-1); + } + } +} + diff --git a/tools/tcpmon/src/tcpmon_entry_local.h b/tools/tcpmon/src/tcpmon_entry_local.h new file mode 100644 index 0000000..8ba9c6a --- /dev/null +++ b/tools/tcpmon/src/tcpmon_entry_local.h @@ -0,0 +1,16 @@ +#include <axutil_env.h> +#include "tcpmon_entry.h" +#include "tcpmon_session_local.h" + +typedef struct tcpmon_entry_request_data +{ + const axutil_env_t *env; + int socket; + tcpmon_session_t *session; +} +tcpmon_entry_request_data_t; + +void *AXIS2_CALL +tcpmon_entry_new_entry_funct( + axutil_thread_t * thd, + void *data); diff --git a/tools/tcpmon/src/tcpmon_session_local.h b/tools/tcpmon/src/tcpmon_session_local.h new file mode 100644 index 0000000..eee9cc6 --- /dev/null +++ b/tools/tcpmon/src/tcpmon_session_local.h @@ -0,0 +1,15 @@ +#include <tcpmon_session.h> +#include <tcpmon_entry.h> + +axis2_status_t tcpmon_session_add_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env, + tcpmon_entry_t * entry); + +TCPMON_SESSION_TRANS_ERROR_FUNCT tcpmon_session_get_on_trans_fault( + tcpmon_session_t * session, + const axutil_env_t * env); + +TCPMON_SESSION_NEW_ENTRY_FUNCT tcpmon_session_get_on_new_entry( + tcpmon_session_t * session, + const axutil_env_t * env); diff --git a/tools/tcpmon/src/util.c b/tools/tcpmon/src/util.c new file mode 100644 index 0000000..d4a73ee --- /dev/null +++ b/tools/tcpmon/src/util.c @@ -0,0 +1,806 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <axiom.h> +#include <stdlib.h> + +#include <tcpmon_util.h> + +#define START_ELEMENT 1 +#define CHAR_VALUE 2 +#define END_ELEMENT 3 +#define EMPTY_ELEMENT 4 + +typedef struct tcpmon_util_allocator +{ + int allocated; + int index; + axis2_char_t *buffer; +} +tcpmon_util_allocator_t; + +/*static void add_string(const axutil_env_t* env, + tcpmon_util_allocator_t* allocator, + axis2_char_t* string); +*/ + +/*static void add_axis2_char_t(const axutil_env_t* env, + tcpmon_util_allocator_t* allocator, + axis2_char_t c, + int turns); +*/ +axis2_char_t * +tcpmon_util_format_as_xml( + const axutil_env_t * env, + axis2_char_t * data, + int format) +{ + if (format) + { + int c; + int tab_pos = 0; + int has_value = 0; + int has_space = 0; + int start_ele = 0; + int prev_case = 0; + int buffer_size = 0; + + axis2_char_t *out; + + axiom_xml_reader_t *xml_reader = NULL; + + buffer_size = 2 * ((int)strlen(data)); + /* We are sure that the difference lies within the int range */ + out = AXIS2_MALLOC(env->allocator, buffer_size * sizeof(axis2_char_t)); + + if (data) + { + int size = 0; + size = (int)strlen(data); + /* We are sure that the difference lies within the int range */ + xml_reader = + axiom_xml_reader_create_for_memory(env, data, size, "utf-8", + AXIS2_XML_PARSER_TYPE_BUFFER); + if (!xml_reader) + return NULL; + } + + axiom_xml_reader_init(); + + while ((c = axiom_xml_reader_next(xml_reader, env)) != -1) + { + switch (c) + { + case AXIOM_XML_READER_START_DOCUMENT: + { + int ix; + + tcpmon_util_strcat(out, "<?xml ", &buffer_size, env); + + ix = axiom_xml_reader_get_attribute_count(xml_reader, env); + for (; ix > 0; ix--) + { + axis2_char_t *attr_prefix; + axis2_char_t *attr_name; + axis2_char_t *attr_value; + + attr_prefix = + (axis2_char_t *) + axiom_xml_reader_get_attribute_prefix_by_number + (xml_reader, env, ix); + if (attr_prefix) + { + tcpmon_util_strcat(out, attr_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + attr_name = + (axis2_char_t *) + axiom_xml_reader_get_attribute_name_by_number + (xml_reader, env, ix); + if (attr_name) + { + tcpmon_util_strcat(out, attr_name, &buffer_size, env); + tcpmon_util_strcat(out, "=\"", &buffer_size, env); + } + + attr_value = + (axis2_char_t *) + axiom_xml_reader_get_attribute_value_by_number + (xml_reader, env, ix); + if (attr_value) + { + tcpmon_util_strcat(out, attr_value, &buffer_size, env); + tcpmon_util_strcat(out, "\"", &buffer_size, env); + } + } + + printf("?>"); + } + break; + case AXIOM_XML_READER_START_ELEMENT: + { + int i, + ix, + has_prefix = 0; + + axis2_char_t *ele_name; + axis2_char_t *ele_prefix; + + prev_case = START_ELEMENT; + + has_value = 0; + has_space = 0; + + if (start_ele != 0) + tcpmon_util_strcat(out, "\n", &buffer_size, env); + + for (i = 0; i < tab_pos; i++) + tcpmon_util_strcat(out, "\t", &buffer_size, env); + + tcpmon_util_strcat(out, "<", &buffer_size, env); + + ele_prefix = + (axis2_char_t *) axiom_xml_reader_get_prefix(xml_reader, + env); + if (ele_prefix) + { + tcpmon_util_strcat(out, ele_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + ele_name = + (axis2_char_t *) axiom_xml_reader_get_name(xml_reader, + env); + if (ele_name) + { + tcpmon_util_strcat(out, ele_name, &buffer_size, env); + } + + ix = axiom_xml_reader_get_attribute_count(xml_reader, env); + for (; ix > 0; ix--) + { + axis2_char_t *attr_prefix; + axis2_char_t *attr_name; + axis2_char_t *attr_value; + + attr_prefix = + (axis2_char_t *) + axiom_xml_reader_get_attribute_prefix_by_number + (xml_reader, env, ix); + if (attr_prefix) + { + has_prefix = 1; + tcpmon_util_strcat(out, " ", &buffer_size, env); + tcpmon_util_strcat(out, attr_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + attr_name = + (axis2_char_t *) + axiom_xml_reader_get_attribute_name_by_number + (xml_reader, env, ix); + if (attr_name) + { + if (has_prefix) + { + tcpmon_util_strcat(out, attr_name, &buffer_size, + env); + tcpmon_util_strcat(out, "=\"", &buffer_size, env); + } + else + { + tcpmon_util_strcat(out, " ", &buffer_size, env); + tcpmon_util_strcat(out, attr_name, &buffer_size, + env); + tcpmon_util_strcat(out, "=\"", &buffer_size, env); + } + + has_prefix = 0; + } + + attr_value = + (axis2_char_t *) + axiom_xml_reader_get_attribute_value_by_number + (xml_reader, env, ix); + if (attr_value) + { + tcpmon_util_strcat(out, attr_value, &buffer_size, env); + tcpmon_util_strcat(out, "\"", &buffer_size, env); + } + } + + tcpmon_util_strcat(out, ">", &buffer_size, env); + + tab_pos++; + start_ele = 1; + } + break; + case AXIOM_XML_READER_CHARACTER: + { + axis2_char_t *ele_value; + + prev_case = CHAR_VALUE; + + ele_value = axiom_xml_reader_get_value(xml_reader, env); + if (ele_value) + tcpmon_util_strcat(out, ele_value, &buffer_size, env); + + has_value = 1; + + } + break; + case AXIOM_XML_READER_EMPTY_ELEMENT: + { + int i, + ix, + has_prefix = 0; + + axis2_char_t *ele_name; + axis2_char_t *ele_prefix; + + prev_case = EMPTY_ELEMENT; + + has_value = 0; + has_space = 0; + + if (start_ele != 0) + tcpmon_util_strcat(out, "\n", &buffer_size, env); + + for (i = 0; i < tab_pos; i++) + tcpmon_util_strcat(out, "\t", &buffer_size, env); + + tcpmon_util_strcat(out, "<", &buffer_size, env); + + ele_prefix = + (axis2_char_t *) axiom_xml_reader_get_prefix(xml_reader, + env); + if (ele_prefix) + { + tcpmon_util_strcat(out, ele_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + ele_name = + (axis2_char_t *) axiom_xml_reader_get_name(xml_reader, + env); + if (ele_name) + tcpmon_util_strcat(out, ele_name, &buffer_size, env); + + ix = axiom_xml_reader_get_attribute_count(xml_reader, env); + for (; ix > 0; ix--) + { + axis2_char_t *attr_prefix; + axis2_char_t *attr_name; + axis2_char_t *attr_value; + + attr_prefix = + (axis2_char_t *) + axiom_xml_reader_get_attribute_prefix_by_number + (xml_reader, env, ix); + if (attr_prefix) + { + has_prefix = 1; + tcpmon_util_strcat(out, " ", &buffer_size, env); + tcpmon_util_strcat(out, attr_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + attr_name = + (axis2_char_t *) + axiom_xml_reader_get_attribute_name_by_number + (xml_reader, env, ix); + if (attr_name) + { + if (has_prefix) + { + tcpmon_util_strcat(out, attr_name, &buffer_size, + env); + tcpmon_util_strcat(out, "=\"", &buffer_size, env); + } + else + { + tcpmon_util_strcat(out, " ", &buffer_size, env); + tcpmon_util_strcat(out, attr_name, &buffer_size, + env); + tcpmon_util_strcat(out, "=\"", &buffer_size, env); + } + has_prefix = 0; + } + + attr_value = + (axis2_char_t *) + axiom_xml_reader_get_attribute_value_by_number + (xml_reader, env, ix); + if (attr_value) + { + tcpmon_util_strcat(out, attr_value, &buffer_size, env); + tcpmon_util_strcat(out, "\"", &buffer_size, env); + } + } + + tcpmon_util_strcat(out, "/>", &buffer_size, env); + start_ele = 1; + } + break; + case AXIOM_XML_READER_END_ELEMENT: + { + int i; + + axis2_char_t *ele_prefix; + axis2_char_t *ele_name; + + tab_pos--; + + if (has_value == 0 && prev_case != START_ELEMENT) + { + tcpmon_util_strcat(out, "\n", &buffer_size, env); + for (i = 0; i < tab_pos; i++) + tcpmon_util_strcat(out, "\t", &buffer_size, env); + } + + has_value = 0; + + tcpmon_util_strcat(out, "</", &buffer_size, env); + + ele_prefix = + (axis2_char_t *) axiom_xml_reader_get_prefix(xml_reader, + env); + if (ele_prefix) + { + tcpmon_util_strcat(out, ele_prefix, &buffer_size, env); + tcpmon_util_strcat(out, ":", &buffer_size, env); + } + + ele_name = + (axis2_char_t *) axiom_xml_reader_get_name(xml_reader, + env); + if (ele_name) + { + tcpmon_util_strcat(out, ele_name, &buffer_size, env); + tcpmon_util_strcat(out, ">", &buffer_size, env); + } + prev_case = END_ELEMENT; + + } + break; + } + } + return out; + } + return data; +} + +/*void add_string(const axutil_env_t* env, + tcpmon_util_allocator_t* allocator, + axis2_char_t* string) +{ + int additional_len = 0; + void* dest = NULL; + void* src = NULL; + int count = 0; + + additional_len = axutil_strlen(string) + 1; + if (allocator-> index + additional_len >= allocator-> allocated) + { + if (allocator-> allocated == 0) + { + allocator-> buffer = + AXIS2_MALLOC(env-> allocator, additional_len); + } + else + { + allocator-> buffer = + AXIS2_REALLOC(env-> allocator, allocator-> buffer, + allocator-> allocated + additional_len); + } + allocator-> allocated += additional_len; + } + + dest = allocator-> buffer + allocator-> index; + src = string; + count = additional_len; + memcpy(dest, src, count); + + allocator-> index += count - 1; +} +*/ + +/*void add_axis2_char_t(const axutil_env_t* env, + tcpmon_util_allocator_t* allocator, + axis2_char_t c, + int turns) +{ + int additional_len = 0; + + additional_len = turns + 1; + if (allocator-> index + additional_len >= allocator-> allocated) + { + if (allocator-> allocated == 0) + { + allocator-> buffer = + AXIS2_MALLOC(env-> allocator, additional_len); + } + else + { + allocator-> buffer = + AXIS2_REALLOC(env-> allocator, allocator-> buffer, + allocator-> allocated + additional_len); + } + allocator-> allocated += additional_len; + } + + memset(allocator-> buffer + allocator-> index, c, turns); + + allocator-> index += turns; + +} */ + +axis2_char_t * +tcpmon_util_strcat( + axis2_char_t * dest, + axis2_char_t * source, + int *buff_size, + const axutil_env_t * env) +{ + int cur_len = 0; + int source_len = 0; + + axis2_char_t *tmp; + cur_len = (int)strlen(dest); + /* We are sure that the difference lies within the int range */ + source_len = (int)strlen(source); + /* We are sure that the difference lies within the int range */ + + if ((*buff_size - cur_len) < source_len) + { + *buff_size = *buff_size + (*buff_size * 2); + tmp = + (axis2_char_t *) AXIS2_REALLOC(env->allocator, dest, + *buff_size * sizeof(axis2_char_t)); + dest = tmp; + strcat((char *) dest, (char *) source); + } + else + { + strcat((char *) dest, (char *) source); + } + + return dest; +} + +char * +tcpmon_util_str_replace( + const axutil_env_t *env, + char *str, + const char *search, + const char *replace) +{ + char *str_return = NULL; + char *str_tmp = NULL; + char *str_relic = NULL; + int size = ((int)strlen(str)) * 2; + /* We are sure that the difference lies within the int range */ + int addmem = size; + int diff = (int)(strlen(replace) - strlen(search)); + /* We are sure that the difference lies within the int range */ + + str_return = (char *) AXIS2_MALLOC(env->allocator, ((size + 1) * sizeof(char))); + str_tmp = (char *) AXIS2_MALLOC(env->allocator, (size * sizeof(char))); + + + if (str_return == NULL || str_tmp == NULL) + { + AXIS2_FREE(env->allocator, str_return); + str_return = NULL; + AXIS2_FREE(env->allocator, str_tmp); + str_tmp = NULL; + return "function tcpmon_util_str_replace : give me more memory"; + } + if (!strcmp(search, replace)) + { + AXIS2_FREE(env->allocator, str_return); + str_return = NULL; + AXIS2_FREE(env->allocator, str_tmp); + str_tmp = NULL; + return str; + } + + strcpy(str_return, str); + + while ((str_relic = strstr(str_return, search)) != NULL) + { + if ((int)strlen(str_return) + diff >= addmem) + /* We are sure that the difference lies within the int range */ + { + str_return = (char *) realloc(str_return, addmem += size); + str_tmp = (char *) realloc(str_tmp, addmem); + + if (str_return == NULL || str_tmp == NULL) + { + AXIS2_FREE(env->allocator, str_return); + str_return = NULL; + AXIS2_FREE(env->allocator, str_tmp); + str_tmp = NULL; + return "function tcpmon_str_replace : gimme more memory"; + } + } + + strcpy(str_tmp, replace); + strcat(str_tmp, (str_relic + strlen(search))); + *str_relic = '\0'; + + strcat(str_return, str_tmp); + } + + AXIS2_FREE(env->allocator, str_tmp); + str_tmp = NULL; + /* free(str); */ /* we are not allocating memory using str */ + str_return[addmem] = '\0'; + return (str_return); +} + + +axis2_char_t * +tcpmon_util_read_current_stream( + const axutil_env_t * env, + axutil_stream_t * stream, + int *stream_size, + axis2_char_t ** header, + axis2_char_t ** data) +{ + int read_size = 0; + axis2_char_t *buffer = NULL; + axis2_char_t *header_ptr = NULL; + axis2_char_t *body_ptr = NULL; + int header_found = 0; + int header_just_finished = 0; + int read = 0; + int header_width = 0; + int current_line_offset = 0; + int mtom_optimized = 0; + axis2_char_t *current_line = NULL; + int line_just_ended = 1; + axis2_char_t *length_char = 0; + int length = -1; + int chunked_encoded = 0; + int is_get = 0; + int zero_content_length = 0; + + buffer = AXIS2_MALLOC(env->allocator, sizeof(axis2_char_t)); + *data = NULL; + *header = NULL; + do + { + buffer = AXIS2_REALLOC(env->allocator, buffer, + sizeof(axis2_char_t) * (read_size + 1)); + *(buffer + read_size) = '\0'; + read = axutil_stream_read(stream, env, buffer + read_size, 1); + + if (header_just_finished) + { + header_just_finished = 0; + header_width = read_size; + } + + /** identify the content lenth*/ + if (!header_found && *(buffer + read_size) == '\r') + { + *(buffer + read_size) = '\0'; + current_line = buffer + current_line_offset; + if (!mtom_optimized && strstr(current_line, "multipart/related")) + mtom_optimized = 1; + if (strstr(current_line, "Content-Length")) + { + length_char = strstr(current_line, ":"); + if (length_char) + { + length_char++; + length = atoi(length_char); + if (length == 0) + { + zero_content_length = 1; + } + } + } + if (strstr(current_line, "GET") || strstr(current_line, "HEAD") + || strstr(current_line, "DELETE")) + { + is_get = 1; + /** Captures GET style requests */ + } + *(buffer + read_size) = '\r'; + } + if (!header_found && line_just_ended) + { + line_just_ended = 0; + current_line_offset = read_size; + } + if (!header_found && *(buffer + read_size) == '\n') + { + line_just_ended = 1; /* set for the next loop to read */ + } + if (header_found) + { + length--; + } + if (header_found && + read_size >= 4 && + chunked_encoded == 1 && + *(buffer + read_size) == '\n' && + *(buffer + read_size - 1) == '\r' && + *(buffer + read_size - 2) == '\n' && + *(buffer + read_size - 3) == '\r' && + *(buffer + read_size - 4) == '0') + { + + length = 0; /** this occurs in chunked transfer encoding */ + } + + /** identify the end of the header */ + if (!header_found && + read_size >= 3 && + *(buffer + read_size) == '\n' && + *(buffer + read_size - 1) == '\r' && + *(buffer + read_size - 2) == '\n' && + *(buffer + read_size - 3) == '\r') + { + header_found = 1; + *(buffer + read_size - 3) = '\0'; + if (header_ptr) + { + AXIS2_FREE(env->allocator, header_ptr); + } + header_ptr = (axis2_char_t *) axutil_strdup(env, buffer); + header_just_finished = 1; + *(buffer + read_size - 3) = '\r'; + if (is_get && length == -1) + { + break; + } + } + read_size++; + if (!chunked_encoded && length < -1) + { + header_width = 0; + /* break; */ + + /** this is considered as transfer-encoding = chunked */ + chunked_encoded = 1; + header_found = 1; + *(buffer + read_size - 3) = '\0'; + if (header_ptr) + { + AXIS2_FREE(env->allocator, header_ptr); + } + header_ptr = (axis2_char_t *) axutil_strdup(env, buffer); + header_just_finished = 1; + *(buffer + read_size - 3) = '\r'; + } + if (!(*(buffer + read_size - 1))) + { + if (!mtom_optimized) + { + read_size--; + length = 0; + } + else + { + /**(buffer + read_size - 1) = ' ';*/ + } + } + } + while (length != 0); + + if (is_get) + { + read_size++; + } + else if (zero_content_length) + { + read_size += 3; + } + + buffer = AXIS2_REALLOC(env->allocator, buffer, + sizeof(axis2_char_t) * (read_size + 1)); + *(buffer + read_size) = '\0'; + + if (header_width != 0) + { + body_ptr = buffer + header_width; + if (body_ptr && *body_ptr) + { + if (mtom_optimized) + { + int count = read_size - (int)strlen(header_ptr) - 4; + int copied = 0; + int plen = 0; + axis2_char_t *temp = NULL; + temp = AXIS2_MALLOC(env->allocator, + sizeof(axis2_char_t) * count + 1); + while(count > copied) + { + plen = 0; + plen = ((int)strlen(body_ptr) + 1); + if (plen != 1) + { + sprintf(temp, "%s", body_ptr); + } + copied += plen; + if (count > copied) + { + temp += plen; + body_ptr += plen; + } + } + copied -= plen; + temp -= copied; + temp[count] = '\0'; + *data = temp; + } + else + { + *data = (axis2_char_t *) axutil_strdup(env, body_ptr); + } + } + body_ptr = NULL; + } + else + { + /** soap body part is unavailable */ + if (is_get) + { + *data = (axis2_char_t *) axutil_strdup(env, "\n"); + *(buffer + read_size - 1) = '\n'; + } + else if (zero_content_length) + { + *data = (axis2_char_t *) axutil_strdup(env, "\n"); + *(buffer + read_size - 3) = '\n'; + *(buffer + read_size - 2) = '\r'; + *(buffer + read_size - 1) = '\n'; + } + if (header_ptr) + { + AXIS2_FREE(env->allocator, header_ptr); + } + header_ptr = (axis2_char_t *) axutil_strdup(env, buffer); + } + + *header = header_ptr; + *stream_size = read_size; + return buffer; +} + + +int +tcpmon_util_write_to_file( + char *filename, + char *buffer) +{ + int size = 0; + if (filename) + { + FILE *fp = fopen(filename, "ab"); + size = (int)fwrite(buffer, 1, strlen(buffer), fp); + /* We are sure that the difference lies within the int range */ + fclose(fp); + } + return size; +} + |