]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
acl.c: Improve ast_ouraddrfor() diagnostic messages. 55/4655/1
authorRichard Mudgett <rmudgett@digium.com>
Wed, 21 Dec 2016 22:25:00 +0000 (16:25 -0600)
committerRichard Mudgett <rmudgett@digium.com>
Thu, 22 Dec 2016 18:16:20 +0000 (12:16 -0600)
* Made not generate strings unless they will actually be used.

ASTERISK-26672

Change-Id: I155fbe7fdff5ce47dfe5326f3baf5446849702c3

main/acl.c

index 87776b33b1afce4ff48fe73137fcfc0173863a55..9820e8bef2c71a6a858f0a4d8e8304a3c4b375ee 100644 (file)
@@ -914,40 +914,48 @@ int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
 
 int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
 {
+       /*
+        * We must create the errno string before creating the address
+        * string because it could wipe out errno on the error return
+        * paths.
+        */
+       const char *sock_err;
        int port;
        int s;
 
+       /* Preserve our original address port */
        port = ast_sockaddr_port(us);
 
-       if ((s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET,
-                       SOCK_DGRAM, 0)) < 0) {
-               ast_log(LOG_ERROR, "Cannot create socket\n");
+       s = socket(ast_sockaddr_is_ipv6(them) ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
+       if (s < 0) {
+               sock_err = ast_strdupa(strerror(errno));
+               ast_log(LOG_ERROR, "Cannot create socket to %s: %s\n",
+                       ast_sockaddr_stringify_addr(them), sock_err);
                return -1;
        }
 
        if (ast_connect(s, them)) {
-               ast_log(LOG_WARNING, "Cannot connect\n");
+               sock_err = ast_strdupa(strerror(errno));
+               ast_log(LOG_WARNING, "Cannot connect to %s: %s\n",
+                       ast_sockaddr_stringify_addr(them), sock_err);
                close(s);
                return -1;
        }
        if (ast_getsockname(s, us)) {
-
-               ast_log(LOG_WARNING, "Cannot get socket name\n");
+               sock_err = ast_strdupa(strerror(errno));
+               ast_log(LOG_WARNING, "Cannot get socket name for connection to %s: %s\n",
+                       ast_sockaddr_stringify_addr(them), sock_err);
                close(s);
                return -1;
        }
        close(s);
 
-       {
-               const char *them_addr = ast_strdupa(ast_sockaddr_stringify_addr(them));
-               const char *us_addr = ast_strdupa(ast_sockaddr_stringify_addr(us));
-
-               ast_debug(3, "For destination '%s', our source address is '%s'.\n",
-                               them_addr, us_addr);
-       }
-
        ast_sockaddr_set_port(us, port);
 
+       ast_debug(3, "For destination '%s', our source address is '%s'.\n",
+               ast_strdupa(ast_sockaddr_stringify_addr(them)),
+               ast_strdupa(ast_sockaddr_stringify_addr(us)));
+
        return 0;
 }