]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
stun.c: Fix ast_stun_request() erratic timeout.
authorRichard Mudgett <rmudgett@digium.com>
Thu, 6 Apr 2017 22:31:14 +0000 (17:31 -0500)
committerRichard Mudgett <rmudgett@digium.com>
Tue, 11 Apr 2017 18:00:06 +0000 (13:00 -0500)
If ast_stun_request() receives packets other than a STUN response then we
could conceivably never exit if we continue to receive packets with less
than three seconds between them.

* Fix poll timeout to keep track of the time when we sent the STUN
request.  We will now send a STUN request every three seconds regardless
of how many other packets we receive while waiting for a response until we
have completed three STUN request transmission cycles.

Change-Id: Ib606cb08585e06eb50877f67b8d3bd385a85c266

main/stun.c

index ecabdadef113da79c2988e481b342188c49da9f7..d9f8c8751ab76f92e01ce70fb25b2b93e10185f6 100644 (file)
@@ -413,6 +413,7 @@ int ast_stun_request(int s, struct sockaddr_in *dst,
                /* send request, possibly wait for reply */
                struct sockaddr_in src;
                socklen_t srclen;
+               struct timeval start;
 
                /* Send STUN message. */
                res = stun_send(s, dst, req);
@@ -426,12 +427,20 @@ int ast_stun_request(int s, struct sockaddr_in *dst,
                        break;
                }
 
+               start = ast_tvnow();
 try_again:
                /* Wait for response. */
                {
                        struct pollfd pfds = { .fd = s, .events = POLLIN };
+                       int ms;
 
-                       res = ast_poll(&pfds, 1, 3000);
+                       ms = ast_remaining_ms(start, 3000);
+                       if (ms <= 0) {
+                               /* No response, timeout */
+                               res = 1;
+                               continue;
+                       }
+                       res = ast_poll(&pfds, 1, ms);
                        if (res < 0) {
                                /* Error */
                                continue;