From 81fd77e9e4a47a1bddde1509bfe0d1926a077942 Mon Sep 17 00:00:00 2001
From: Jonh Wendell <[email protected]>
Date: Mon, 6 Feb 2012 09:43:18 -0200
Subject: [PATCH] Make unix (local) sockets work without IPv6 enabled.

The xsocket_type() function had an optional "family" argument
that was enabled only if IPv6 is enabled. In the case of the
function was called with a valid AF_UNIX argument, and IPv6 is
disabled, this argument was silently ignored.

This patch makes the "family" argument mandatory, while keeping
the old behavior i.e., if AF_UNSPEC is passed, we try first IPv6
(if it's enabled) and fallback to IPv4.

Also I changed all callers of xsocket_type() to reflect its new
interface.
---
 include/libbb.h  |    5 -----
 libbb/xconnect.c |   19 +++++++++++--------
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 4975b97..f743bdf 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -568,12 +568,7 @@ enum {
  * and if kernel doesn't support it, fall back to IPv4.
  * This is useful if you plan to bind to resulting local lsa.
  */
-#if ENABLE_FEATURE_IPV6
 int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type) FAST_FUNC;
-#else
-int xsocket_type(len_and_sockaddr **lsap, int sock_type) FAST_FUNC;
-#define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type))
-#endif
 int xsocket_stream(len_and_sockaddr **lsap) FAST_FUNC;
 /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  * numeric IP ("N.N.N.N") or numeric IPv6 address,
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index 4b7c110..de17a6d 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -323,27 +323,30 @@ len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
 }
 
 #undef xsocket_type
-int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
+int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
 {
-	IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
 	len_and_sockaddr *lsa;
 	int fd;
 	int len;
 
-#if ENABLE_FEATURE_IPV6
 	if (family == AF_UNSPEC) {
+#if ENABLE_FEATURE_IPV6
 		fd = socket(AF_INET6, sock_type, 0);
 		if (fd >= 0) {
 			family = AF_INET6;
 			goto done;
 		}
+#endif
 		family = AF_INET;
 	}
-#endif
+
 	fd = xsocket(family, sock_type, 0);
-	len = sizeof(struct sockaddr_in);
+	if (family == AF_INET)
+	  len = sizeof(struct sockaddr_in);
+	else if (family == AF_UNIX)
+	  len = sizeof(struct sockaddr_un);
 #if ENABLE_FEATURE_IPV6
-	if (family == AF_INET6) {
+	else if (family == AF_INET6) {
  done:
 		len = sizeof(struct sockaddr_in6);
 	}
@@ -357,7 +360,7 @@ int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,)
 
 int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
 {
-	return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
+	return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
 }
 
 static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
@@ -370,7 +373,7 @@ static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
 		/* user specified bind addr dictates family */
 		fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
 	} else {
-		fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
+		fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
 		set_nport(&lsa->u.sa, htons(port));
 	}
 	setsockopt_reuseaddr(fd);
-- 
1.7.5.4

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to