From a6c395c156a5d399edea10bb8731ea49226e7932 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Thu, 23 Jan 2003 05:38:20 +0000
Subject: [PATCH] Duncan Wilcox reported a crash with --interface on FreeBSD
 when ipv6-enabled and this has been verified to correct the problem.

---
 lib/connect.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lib/connect.c b/lib/connect.c
index 05201bab45..8844253265 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -251,14 +251,23 @@ static CURLcode bindlocal(struct connectdata *conn,
         Curl_resolv_unlock(h);
         /* we don't need it anymore after this function has returned */
 
-        memset((char *)&sa, 0, sizeof(sa));
 #ifdef ENABLE_IPV6
-        memcpy((char *)&sa.sin_addr, addr->ai_addr, addr->ai_addrlen);        
-        sa.sin_family = addr->ai_family;
+        (void)sa; /* prevent compiler warning */
+        if( bind(sockfd, addr->ai_addr, addr->ai_addrlen) >= 0) {
+          /* we succeeded to bind */
+          struct sockaddr_in6 add;
+	
+          size = sizeof(add);
+          if(getsockname(sockfd, (struct sockaddr *) &add,
+                         (socklen_t *)&size)<0) {
+            failf(data, "getsockname() failed");
+            return CURLE_HTTP_PORT_FAILED;
+          }
+        }
 #else
+        memset((char *)&sa, 0, sizeof(sa));
         memcpy((char *)&sa.sin_addr, addr->h_addr, addr->h_length);
         sa.sin_family = AF_INET;
-#endif
         sa.sin_addr.s_addr = in;
         sa.sin_port = 0; /* get any port */
 	
@@ -273,6 +282,7 @@ static CURLcode bindlocal(struct connectdata *conn,
             return CURLE_HTTP_PORT_FAILED;
           }
         }
+#endif
         else {
           switch(errno) {
           case EBADF:
-- 
GitLab