]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
remove select call from switch_nat.c
authorAnthony Minessale <anthm@freeswitch.org>
Tue, 15 Jul 2014 22:01:30 +0000 (03:01 +0500)
committerAnthony Minessale <anthm@freeswitch.org>
Tue, 15 Jul 2014 22:01:34 +0000 (03:01 +0500)
src/include/switch_apr.h
src/switch_apr.c
src/switch_nat.c

index 387cc7a94f5db7cd6e340da4565d32f878d60600..c723a533e391fc0ea8ea274b1f544d8b0771b650 100644 (file)
@@ -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.
index 7b41c60af41251fbea6814ad80199601e834699d..a12ae2ddeb6d7e228df350650578f488f1921e58 100644 (file)
@@ -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);
index 3e404ae9857066215844c59673137314c6a9bc94..503fa891c3c6651ec4e0cad0981a5b2ca990e938 100644 (file)
@@ -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);
        }