diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index d35488d..4fb4013 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -104,6 +104,7 @@ static struct { switch_mutex_t *listener_mutex; switch_event_node_t *node; int debug; + switch_memory_pool_t *pool; } globals; static struct { @@ -111,6 +112,7 @@ static struct { switch_mutex_t *sock_mutex; listener_t *listeners; uint8_t ready; + switch_sockaddr_t *sa; } listen_list; #define MAX_ACL 100 @@ -146,6 +148,7 @@ static const char *format2str(event_format_t format) static void remove_listener(listener_t *listener); static void kill_listener(listener_t *l, const char *message); static void kill_all_listeners(void); +static int config(void); static uint32_t next_id(void) { @@ -162,6 +165,37 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_pass, prefs.password); static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj); static void launch_listener_thread(listener_t *listener); +switch_status_t listen_socket_init() { + switch_status_t rv; + + rv = switch_sockaddr_info_get(&listen_list.sa, prefs.ip, SWITCH_INET, prefs.port, 0, globals.pool); + if (rv) + goto sock_fail; + + rv = switch_socket_create(&listen_list.sock, switch_sockaddr_get_family(listen_list.sa), SOCK_STREAM, SWITCH_PROTO_TCP, globals.pool); + if (rv) + goto sock_fail; + + rv = switch_socket_opt_set(listen_list.sock, SWITCH_SO_REUSEADDR, 1); + if (rv) + goto sock_fail; + + rv = switch_socket_bind(listen_list.sock, listen_list.sa); + if (rv) + goto sock_fail; + + rv = switch_socket_listen(listen_list.sock, 5); + if (rv) + goto sock_fail; + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", prefs.ip, prefs.port); + + return SWITCH_STATUS_SUCCESS; + +sock_fail: + return SWITCH_STATUS_FALSE; +} + static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_level_t level) { listener_t *l; @@ -537,6 +571,10 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_event_socket_shutdown) prefs.done = 1; + if (prefs.nat_map && switch_nat_get_type()) { + switch_nat_del_mapping(prefs.port, SWITCH_NAT_TCP); + } + kill_all_listeners(); switch_log_unbind_logger(socket_logger); @@ -1071,6 +1109,18 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_event_socket_load) switch_api_interface_t *api_interface; memset(&globals, 0, sizeof(globals)); + globals.pool = pool; + + config(); + + if (listen_socket_init() != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind listener socket!\n"); + return SWITCH_STATUS_GENERR; + } + + if (prefs.nat_map) { + switch_nat_add_mapping(prefs.port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE); + } switch_mutex_init(&globals.listener_mutex, SWITCH_MUTEX_NESTED, pool); @@ -2748,7 +2798,6 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime) { switch_memory_pool_t *pool = NULL, *listener_pool = NULL; switch_status_t rv; - switch_sockaddr_t *sa; switch_socket_t *inbound_socket = NULL; listener_t *listener; uint32_t x = 0; @@ -2759,46 +2808,14 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime) return SWITCH_STATUS_TERM; } - config(); - - while (!prefs.done) { - rv = switch_sockaddr_info_get(&sa, prefs.ip, SWITCH_INET, prefs.port, 0, pool); - if (rv) - goto fail; - rv = switch_socket_create(&listen_list.sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, pool); - if (rv) - goto sock_fail; - rv = switch_socket_opt_set(listen_list.sock, SWITCH_SO_REUSEADDR, 1); - if (rv) - goto sock_fail; - rv = switch_socket_bind(listen_list.sock, sa); - if (rv) - goto sock_fail; - rv = switch_socket_listen(listen_list.sock, 5); - if (rv) - goto sock_fail; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", prefs.ip, prefs.port); - - if (prefs.nat_map) { - switch_nat_add_mapping(prefs.port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE); - } - - break; - sock_fail: - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error! Could not listen on %s:%u\n", prefs.ip, prefs.port); - switch_yield(100000); - } - listen_list.ready = 1; - while (!prefs.done) { if (switch_core_new_memory_pool(&listener_pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n"); goto fail; } - if ((rv = switch_socket_accept(&inbound_socket, listen_list.sock, listener_pool))) { if (prefs.done) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting Down\n"); @@ -2852,12 +2869,6 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime) end: - close_socket(&listen_list.sock); - - if (prefs.nat_map && switch_nat_get_type()) { - switch_nat_del_mapping(prefs.port, SWITCH_NAT_TCP); - } - if (pool) { switch_core_destroy_memory_pool(&pool); }