*/
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
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.
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);
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";
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) {
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);
}