You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by "uhliarik (via GitHub)" <gi...@apache.org> on 2023/06/27 10:11:48 UTC

[GitHub] [apr] uhliarik commented on a diff in pull request #44: Fix socket creation when APR_UNSPEC is used

uhliarik commented on code in PR #44:
URL: https://github.com/apache/apr/pull/44#discussion_r1243494966


##########
memcache/apr_memcache.c:
##########
@@ -346,18 +346,37 @@ APR_DECLARE(apr_status_t) apr_memcache_disable_server(apr_memcache_t *mc, apr_me
     return make_server_dead(mc, ms);
 }
 
-static apr_status_t conn_connect(apr_memcache_conn_t *conn, apr_sockaddr_t *sa)
+static apr_status_t conn_connect(apr_memcache_conn_t *conn)
 {
     apr_status_t rv = APR_SUCCESS;
+    apr_sockaddr_t *sa;
+#if APR_HAVE_SOCKADDR_UN
+    apr_int32_t family = conn->ms->host[0] != '/' ? APR_UNSPEC : APR_UNIX;
+#else
+    apr_int32_t family = APR_UNSPEC;
+#endif
 
-    rv = apr_socket_timeout_set(conn->sock, 1 * APR_USEC_PER_SEC);
+    rv = apr_sockaddr_info_get(&sa, conn->ms->host, family, conn->ms->port, 0, conn->p);
     if (rv != APR_SUCCESS) {
         return rv;
     }
 
-    rv = apr_socket_connect(conn->sock, sa);
-    if (rv != APR_SUCCESS) {
-        return rv;
+    /* Cycle through address until a connect() succeeds. */
+    for (; sa; sa = sa->next) {
+        rv = apr_socket_create(&conn->sock, sa->family, SOCK_STREAM, 0, conn->p);
+        if (rv == APR_SUCCESS) {
+            rv = apr_socket_timeout_set(conn->sock, 1 * APR_USEC_PER_SEC);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+
+            rv = apr_socket_connect(conn->sock, sa);
+            if (rv == APR_SUCCESS) {
+                break;
+            }
+
+            apr_socket_close(conn->sock);
+        }
     }

Review Comment:
   This would also work IMHO. I think, since we set apr_socket_timeout_set on closed socket, it would fail anyway... But it is better way how to handle it to explicitly say what we were not able to connect to any of given addresses.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@apr.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org