diff --git a/src/core/config.c b/src/core/config.c index c5d67dc..2f30bf8 100644 --- a/src/core/config.c +++ b/src/core/config.c @@ -111,6 +111,7 @@ #define DEF_SERVER_BIND_ADDR "any" #define DEF_SERVER_PORT 1511 #define DEF_SERVER_BACKLOG 50 +#define DEF_SERVER_ALT_PORTS "" #define DEF_HUB_NAME "uhub" #define DEF_HUB_DESCRIPTION "" #define DEF_HUB_ENABLED 1 @@ -184,6 +185,7 @@ void config_defaults(struct hub_config* config) DEFAULT_STRING (file_rules, DEF_FILE_RULES); DEFAULT_INTEGER(server_port, DEF_SERVER_PORT); DEFAULT_INTEGER(server_listen_backlog, DEF_SERVER_BACKLOG); + DEFAULT_STRING (server_alt_ports, DEF_SERVER_ALT_PORTS); DEFAULT_INTEGER(max_users, DEF_MAX_USERS); DEFAULT_INTEGER(max_chat_history, DEF_MAX_CHAT_HISTORY); DEFAULT_INTEGER(max_logout_log, DEF_MAX_LOGOUT_LOG); @@ -255,6 +257,7 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li GET_STR (server_bind_addr); GET_INT (server_port); GET_INT (server_listen_backlog); + GET_STR (server_alt_ports); GET_STR (hub_name); GET_STR (hub_description); GET_BOOL(hub_enabled); @@ -328,6 +331,7 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li void free_config(struct hub_config* config) { hub_free(config->server_bind_addr); + hub_free(config->server_alt_ports); hub_free(config->file_motd); hub_free(config->file_acl); hub_free(config->file_rules); @@ -406,6 +410,7 @@ void dump_config(struct hub_config* config, int ignore_defaults) DUMP_STR (server_bind_addr, DEF_SERVER_BIND_ADDR); DUMP_INT (server_port, DEF_SERVER_PORT); DUMP_INT (server_listen_backlog, DEF_SERVER_BACKLOG); + DUMP_STR (server_alt_ports, DEF_SERVER_ALT_PORTS); DUMP_STR (hub_name, DEF_HUB_NAME); DUMP_STR (hub_description, DEF_HUB_DESCRIPTION); DUMP_BOOL(hub_enabled, DEF_HUB_ENABLED); diff --git a/src/core/config.h b/src/core/config.h index 0adacd6..58f37c0 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -25,6 +25,7 @@ struct hub_config int server_port; /**<<< "Server port to bind to (default: 1511)" */ char* server_bind_addr; /**<<< "Server bind address (default: '0.0.0.0' or '::')" */ int server_listen_backlog; /**<<< "Server listen backlog (default: 50)" */ + char* server_alt_ports; /**<<< "Comma separated list of alternative ports to listen to (default: '')" */ int hub_enabled; /**<<< "Is server enabled (default: 1)" */ int show_banner; /**<<< "Show banner on connect (default: 1)" */ int max_users; /**<<< "Maximum number of users allowed on the hub (default: 500)" */ diff --git a/src/core/hub.c b/src/core/hub.c index ad72826..aa46483 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -496,6 +496,62 @@ static struct net_connection* start_listening_socket(const char* bind_addr, uint return server; } +struct server_alt_port_data +{ + struct hub_info* hub; + struct hub_config* config; +}; + +static int server_alt_port_start_one(char* line, int count, void* ptr) +{ + struct server_alt_port_data* data = (struct server_alt_port_data*) ptr; + + int port = uhub_atoi(line); + struct net_connection* con = start_listening_socket(data->config->server_bind_addr, port, data->config->server_listen_backlog, data->hub); + if (con) + { + list_append(data->hub->server_alt_ports, con); + LOG_INFO("Listening on alternate port %d...", port); + return 0; + } + return -1; +} + +static void server_alt_port_start(struct hub_info* hub, struct hub_config* config) +{ + if (!config->server_alt_ports || !*config->server_alt_ports) + return; + + hub->server_alt_ports = (struct linked_list*) list_create(); + + struct server_alt_port_data data; + data.hub = hub; + data.config = config; + + string_split(config->server_alt_ports, ",", &data, server_alt_port_start_one); +} + +static void server_alt_port_clear(void* ptr) +{ + struct net_connection* con = (struct net_connection*) ptr; + if (con) + { + net_con_close(con); + hub_free(con); + } +} + +static void server_alt_port_stop(struct hub_info* hub) +{ + if (hub->server_alt_ports) + { + list_clear(hub->server_alt_ports, &server_alt_port_clear); + list_destroy(hub->server_alt_ports); + } +} + + + struct hub_info* hub_start_service(struct hub_config* config) { struct hub_info* hub = 0; @@ -596,6 +652,8 @@ struct hub_info* hub_start_service(struct hub_config* config) return 0; } + server_alt_port_start(hub, config); + hub->status = hub_status_running; g_hub = hub; @@ -610,6 +668,7 @@ void hub_shutdown_service(struct hub_info* hub) event_queue_shutdown(hub->queue); net_con_close(hub->server); hub_free(hub->server); + server_alt_port_stop(hub); uman_shutdown(hub); hub->status = hub_status_stopped; hub_free(hub->sendbuf); diff --git a/src/core/hub.h b/src/core/hub.h index 8eab77b..af6db67 100644 --- a/src/core/hub.h +++ b/src/core/hub.h @@ -91,6 +91,7 @@ struct hub_logout_info struct hub_info { struct net_connection* server; + struct linked_list* server_alt_ports; struct hub_stats stats; struct event_queue* queue; struct hub_config* config;