From 8a21d466ead08df84b777cfccde25a399dcd37bf Mon Sep 17 00:00:00 2001 From: Sebastien Duthil <sduthil@wazo.community> Date: Wed, 30 Jun 2021 18:15:10 -0400 Subject: [PATCH] stun: Emit warning message when STUN request times out Without this message, it is not obvious that the reason is STUN timeout. ASTERISK-29507 #close Change-Id: I26e4853c23a1aed324552e1b9683ea3c05cb1f74 --- main/stun.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main/stun.c b/main/stun.c index e917944a60..4e9dac18af 100644 --- a/main/stun.c +++ b/main/stun.c @@ -120,6 +120,8 @@ struct stun_addr { #define STUN_UNKNOWN_ATTRIBUTES 0x000a #define STUN_REFLECTED_FROM 0x000b +#define STUN_MAX_RETRIES 3 + /*! \brief helper function to print message names */ static const char *stun_msg2str(int msg) { @@ -235,6 +237,22 @@ static void append_attr_address(struct stun_attr **attr, int attrval, struct soc } } +static void handle_stun_timeout(int retry, struct sockaddr_in *dst) +{ + if (retry < STUN_MAX_RETRIES) { + ast_log(LOG_NOTICE, + "Attempt %d to send STUN request to '%s' timed out.", + retry, + ast_inet_ntoa(dst->sin_addr)); + } else { + ast_log(LOG_WARNING, + "Attempt %d to send STUN request to '%s' timed out." + "Check that the server address is correct and reachable.\n", + retry, + ast_inet_ntoa(dst->sin_addr)); + } +} + /*! \brief wrapper to send an STUN message */ static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp) { @@ -410,7 +428,7 @@ int ast_stun_request(int s, struct sockaddr_in *dst, req->msglen = htons(reqlen); req->msgtype = htons(STUN_BINDREQ); - for (retry = 0; retry++ < 3;) { /* XXX make retries configurable */ + for (retry = 0; retry++ < STUN_MAX_RETRIES;) { /* XXX make retries configurable */ /* send request, possibly wait for reply */ struct sockaddr_in src; socklen_t srclen; @@ -438,6 +456,7 @@ try_again: ms = ast_remaining_ms(start, 3000); if (ms <= 0) { /* No response, timeout */ + handle_stun_timeout(retry, dst); res = 1; continue; } @@ -448,6 +467,7 @@ try_again: } if (!res) { /* No response, timeout */ + handle_stun_timeout(retry, dst); res = 1; continue; } -- GitLab