You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by ro...@apache.org on 2026/01/14 11:46:43 UTC
(axis-axis2-c-core) 02/03: AXIS2C-1484: Allow HTTP server to bind to specific address
This is an automated email from the ASF dual-hosted git repository.
robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git
commit e58f62fe550f9ae3807c6b031db7bb64798ab4f7
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Tue Jan 13 17:34:56 2026 -1000
AXIS2C-1484: Allow HTTP server to bind to specific address
Add support for binding the HTTP server to a specific IP address instead
of INADDR_ANY (all interfaces). This enables running the server on a
specific network interface for security or multi-homed configurations.
Changes:
- Add addr parameter to axutil_network_handler_create_server_socket()
- Add addr parameter to axis2_http_svr_thread_create()
- Add addr field to axis2_http_server_impl_t structure
- Read bind address from "addr" parameter in transport configuration
- Support both IP addresses and hostnames for bind address
- Update all callers to pass NULL for backward compatibility
Configuration example in axis2.xml:
<transportReceiver name="http">
<parameter name="port">8080</parameter>
<parameter name="addr">192.168.1.100</parameter>
</transportReceiver>
Also fixes AXIS2C-1383 (duplicate issue).
Co-Authored-By: Claude Opus 4.5 <no...@anthropic.com>
---
include/axis2_http_svr_thread.h | 2 ++
include/axis2_http_transport.h | 2 ++
src/core/transport/http/receiver/http_receiver.c | 31 +++++++++++++++++++++-
src/core/transport/http/receiver/http_svr_thread.c | 3 ++-
src/core/transport/tcp/receiver/tcp_svr_thread.c | 2 +-
tools/tcpmon/src/session.c | 2 +-
util/include/axutil_network_handler.h | 2 ++
util/src/network_handler.c | 24 +++++++++++++++++
8 files changed, 64 insertions(+), 4 deletions(-)
diff --git a/include/axis2_http_svr_thread.h b/include/axis2_http_svr_thread.h
index 12fe115f8..df90a1e0b 100644
--- a/include/axis2_http_svr_thread.h
+++ b/include/axis2_http_svr_thread.h
@@ -101,11 +101,13 @@ extern "C"
/**
* @param env pointer to environment struct
+ * @param addr server address to bind to, or NULL for INADDR_ANY (all interfaces)
* @param port
*/
AXIS2_EXTERN axis2_http_svr_thread_t *AXIS2_CALL
axis2_http_svr_thread_create(
const axutil_env_t * env,
+ const axis2_char_t * addr,
int port);
AXIS2_EXTERN int AXIS2_CALL
diff --git a/include/axis2_http_transport.h b/include/axis2_http_transport.h
index 7cbbd6e7d..5c5df49e6 100644
--- a/include/axis2_http_transport.h
+++ b/include/axis2_http_transport.h
@@ -1116,6 +1116,8 @@ extern "C"
#define AXIS2_PORT_STRING "port"
+ #define AXIS2_ADDR_STRING "addr"
+
#define AXIS2_DEFAULT_HOST_ADDRESS "127.0.0.1"
#define AXIS2_DEFAULT_SVC_PATH "/axis2/services/"
diff --git a/src/core/transport/http/receiver/http_receiver.c b/src/core/transport/http/receiver/http_receiver.c
index e2cbec24a..e5e7b975a 100644
--- a/src/core/transport/http/receiver/http_receiver.c
+++ b/src/core/transport/http/receiver/http_receiver.c
@@ -37,6 +37,7 @@ typedef struct axis2_http_server_impl
axis2_http_svr_thread_t *svr_thread;
int port;
axis2_char_t *svr_ip;
+ axis2_char_t *addr; /* AXIS2C-1484: Server bind address, NULL for INADDR_ANY */
axis2_conf_ctx_t *conf_ctx;
axis2_conf_ctx_t *conf_ctx_private;
axis2_bool_t is_application_client_side;
@@ -143,6 +144,7 @@ axis2_http_server_create(
server_impl->conf_ctx_private = NULL;
server_impl->port = port;
server_impl->svr_ip = NULL;
+ server_impl->addr = NULL;
server_impl->is_application_client_side = AXIS2_FALSE;
server_impl->http_server.ops = &http_transport_receiver_ops_var;
@@ -200,6 +202,8 @@ axis2_http_server_create_with_file(
server_impl->conf_ctx_private = NULL;
server_impl->port = port;
server_impl->svr_ip = NULL;
+ server_impl->addr = NULL;
+ server_impl->is_application_client_side = AXIS2_FALSE;
server_impl->http_server.ops = &http_transport_receiver_ops_var;
if(file)
@@ -261,6 +265,13 @@ axis2_http_server_free(
server_impl->conf_ctx_private = NULL;
}
+ /* AXIS2C-1484: Free bind address if set */
+ if(server_impl->addr)
+ {
+ AXIS2_FREE(env->allocator, server_impl->addr);
+ server_impl->addr = NULL;
+ }
+
/**
* Do not free this. It may own to some other object
*/
@@ -298,6 +309,23 @@ axis2_http_server_init(
{
server_impl->port = atoi(port_str);
}
+
+ /* AXIS2C-1484: Read server bind address from config */
+ param = (axutil_param_t *)axutil_param_container_get_param(
+ axis2_transport_in_desc_param_container(in_desc, env), env, AXIS2_ADDR_STRING);
+ if(param)
+ {
+ axis2_char_t *addr_str = axutil_param_get_value(param, env);
+ if(addr_str)
+ {
+ if(server_impl->addr)
+ {
+ AXIS2_FREE(env->allocator, server_impl->addr);
+ }
+ server_impl->addr = axutil_strdup(env, addr_str);
+ }
+ }
+
return AXIS2_SUCCESS;
}
@@ -310,7 +338,8 @@ axis2_http_server_start(
axis2_http_worker_t *worker = NULL;
server_impl = AXIS2_INTF_TO_IMPL(server);
- server_impl->svr_thread = axis2_http_svr_thread_create(env, server_impl->port);
+ server_impl->svr_thread = axis2_http_svr_thread_create(env, server_impl->addr,
+ server_impl->port);
if(!server_impl->svr_thread)
{
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "unable to create server thread for port %d",
diff --git a/src/core/transport/http/receiver/http_svr_thread.c b/src/core/transport/http/receiver/http_svr_thread.c
index 022cef3ef..d1e501581 100644
--- a/src/core/transport/http/receiver/http_svr_thread.c
+++ b/src/core/transport/http/receiver/http_svr_thread.c
@@ -53,6 +53,7 @@ axis2_svr_thread_worker_func(
axis2_http_svr_thread_t *AXIS2_CALL
axis2_http_svr_thread_create(
const axutil_env_t * env,
+ const axis2_char_t * addr,
int port)
{
axis2_http_svr_thread_t *svr_thread = NULL;
@@ -70,7 +71,7 @@ axis2_http_svr_thread_create(
svr_thread->port = port;
svr_thread->listen_socket = (int)axutil_network_handler_create_server_socket(env,
- svr_thread->port);
+ addr, svr_thread->port);
if(-1 == svr_thread->listen_socket)
{
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Http server thread socket creation failed.");
diff --git a/src/core/transport/tcp/receiver/tcp_svr_thread.c b/src/core/transport/tcp/receiver/tcp_svr_thread.c
index b5c5fd0cd..bddf05a18 100644
--- a/src/core/transport/tcp/receiver/tcp_svr_thread.c
+++ b/src/core/transport/tcp/receiver/tcp_svr_thread.c
@@ -91,7 +91,7 @@ axis2_tcp_svr_thread_create(
svr_thread->stopped = AXIS2_FALSE;
svr_thread->port = port;
svr_thread->listen_socket = (int)axutil_network_handler_create_server_socket(env,
- svr_thread->port);
+ NULL, svr_thread->port);
if(-1 == svr_thread->listen_socket)
{
axis2_tcp_svr_thread_free((axis2_tcp_svr_thread_t *)svr_thread, env);
diff --git a/tools/tcpmon/src/session.c b/tools/tcpmon/src/session.c
index 29d4c782d..4b62072f0 100644
--- a/tools/tcpmon/src/session.c
+++ b/tools/tcpmon/src/session.c
@@ -505,7 +505,7 @@ server_funct(
env = thread_data->env;
listen_socket = (int)axutil_network_handler_create_server_socket
- (env, session_impl->listen_port);
+ (env, NULL, session_impl->listen_port);
if (-1 == listen_socket)
{
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
diff --git a/util/include/axutil_network_handler.h b/util/include/axutil_network_handler.h
index bc361e2e6..06126c8a9 100644
--- a/util/include/axutil_network_handler.h
+++ b/util/include/axutil_network_handler.h
@@ -50,6 +50,7 @@ extern "C"
/**
* creates a server socket for a given port
+ * @param addr IP address to bind to, or NULL for INADDR_ANY (all interfaces)
* @param port port of the socket to be bound
* @return creates server socket
*/
@@ -57,6 +58,7 @@ extern "C"
axutil_network_handler_create_server_socket(
const axutil_env_t * env,
+ const axis2_char_t * addr,
int port);
/**
diff --git a/util/src/network_handler.c b/util/src/network_handler.c
index f31fe6cb0..14930d21f 100644
--- a/util/src/network_handler.c
+++ b/util/src/network_handler.c
@@ -120,6 +120,7 @@ axutil_network_handler_open_socket(
AXIS2_EXTERN axis2_socket_t AXIS2_CALL
axutil_network_handler_create_server_socket(
const axutil_env_t *env,
+ const axis2_char_t *addr,
int port)
{
axis2_socket_t sock = AXIS2_INVALID_SOCKET;
@@ -168,6 +169,29 @@ axutil_network_handler_create_server_socket(
sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
sock_addr.sin_port = htons((axis2_unsigned_short_t)port);
+ /* AXIS2C-1484: If addr is specified, bind to that specific address only */
+ if(addr != NULL)
+ {
+ sock_addr.sin_addr.s_addr = inet_addr(addr);
+ if(sock_addr.sin_addr.s_addr == AXIS2_INADDR_NONE)
+ {
+ /* addr may be a hostname, try to resolve it */
+ struct hostent *lphost = NULL;
+ lphost = gethostbyname(addr);
+ if(lphost)
+ {
+ sock_addr.sin_addr.s_addr = ((struct in_addr *)lphost->h_addr)->s_addr;
+ }
+ else
+ {
+ AXIS2_ERROR_SET(env->error, AXIS2_ERROR_INVALID_ADDRESS, AXIS2_FAILURE);
+ AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
+ "Failed to resolve server bind address: %s", addr);
+ return AXIS2_INVALID_SOCKET;
+ }
+ }
+ }
+
/* Bind the socket to our port number */
if(bind(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) < 0)
{