diff --git a/src/include/switch_apr.h b/src/include/switch_apr.h index 387cc7a94f..c723a533e3 100644 --- a/src/include/switch_apr.h +++ b/src/include/switch_apr.h @@ -1389,6 +1389,9 @@ SWITCH_DECLARE(switch_status_t) switch_pollset_poll(switch_pollset_t *pollset, s */ SWITCH_DECLARE(switch_status_t) switch_socket_create_pollset(switch_pollfd_t ** poll, switch_socket_t *sock, int16_t flags, switch_memory_pool_t *pool); +SWITCH_DECLARE(switch_interval_time_t) switch_interval_time_from_timeval(struct timeval *tvp); + + /*! \brief Create a pollfd out of a socket \param pollfd the pollfd to create @@ -1402,7 +1405,7 @@ SWITCH_DECLARE(switch_status_t) switch_socket_create_pollfd(switch_pollfd_t **po SWITCH_DECLARE(switch_status_t) switch_match_glob(const char *pattern, switch_array_header_t ** result, switch_memory_pool_t *pool); SWITCH_DECLARE(switch_status_t) switch_os_sock_get(switch_os_socket_t *thesock, switch_socket_t *sock); SWITCH_DECLARE(switch_status_t) switch_socket_addr_get(switch_sockaddr_t ** sa, switch_bool_t remote, switch_socket_t *sock); - +SWITCH_DECLARE(switch_status_t) switch_os_sock_put(switch_socket_t **sock, switch_os_socket_t *thesock, switch_memory_pool_t *pool); /** * Create an anonymous pipe. * @param in The file descriptor to use as input to the pipe. diff --git a/src/switch_apr.c b/src/switch_apr.c index 7b41c60af4..a12ae2ddeb 100644 --- a/src/switch_apr.c +++ b/src/switch_apr.c @@ -646,8 +646,18 @@ SWITCH_DECLARE(switch_status_t) switch_thread_create(switch_thread_t ** new_thre return apr_thread_create(new_thread, attr, func, data, cont); } +SWITCH_DECLARE(switch_interval_time_t) switch_interval_time_from_timeval(struct timeval *tvp) +{ + return ((switch_interval_time_t)tvp->tv_sec * 1000000) + tvp->tv_usec / 1000; +} + /* socket stubs */ +SWITCH_DECLARE(switch_status_t) switch_os_sock_put(switch_socket_t **sock, switch_os_socket_t *thesock, switch_memory_pool_t *pool) +{ + return apr_os_sock_put(sock, thesock, pool); +} + SWITCH_DECLARE(switch_status_t) switch_os_sock_get(switch_os_socket_t *thesock, switch_socket_t *sock) { return apr_os_sock_get(thesock, sock); diff --git a/src/switch_nat.c b/src/switch_nat.c index 3e404ae985..503fa891c3 100644 --- a/src/switch_nat.c +++ b/src/switch_nat.c @@ -149,9 +149,13 @@ static int get_pmp_pubaddr(char *pub_addr) int r = 0, i = 0, max = 5; natpmpresp_t response; char *pubaddr = NULL; - fd_set fds; natpmp_t natpmp; const char *err = NULL; + switch_pollfd_t *pollfd = NULL; + switch_socket_t *sock = NULL; + switch_memory_pool_t *pool; + int32_t num = 0; + switch_status_t status = SWITCH_STATUS_SUCCESS; if ((r = initnatpmp(&natpmp)) < 0) { err = "init failed"; @@ -163,24 +167,34 @@ static int get_pmp_pubaddr(char *pub_addr) goto end; } + switch_core_new_memory_pool(&pool); + + switch_os_sock_put(&sock, &natpmp.s, pool); + + if (switch_socket_create_pollset(&pollfd, sock, SWITCH_POLLIN | SWITCH_POLLERR, pool) != SWITCH_STATUS_SUCCESS) { + err = "cannot create pollset"; + goto end; + } + do { struct timeval timeout = { 1, 0 }; i++; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking for PMP %d/%d\n", i, max); - FD_ZERO(&fds); - FD_SET(natpmp.s, &fds); - if ((r = getnatpmprequesttimeout(&natpmp, &timeout)) < 0) { err = "get timeout failed"; goto end; } + + status = switch_poll(pollfd, 1, &num, switch_interval_time_from_timeval(&timeout)); - if ((r = select(FD_SETSIZE, &fds, NULL, NULL, &timeout)) < 0) { - err = "select failed"; + if (!(status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_TIMEOUT)) { + err = "poll failed"; goto end; } + r = readnatpmpresponseorretry(&natpmp, &response); + } while (r == NATPMP_TRYAGAIN && i < max); if (r < 0) { @@ -196,6 +210,8 @@ static int get_pmp_pubaddr(char *pub_addr) end: + switch_core_destroy_memory_pool(&pool); + if (err) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error checking for PMP [%s]\n", err); }