traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / libbb / xconnect.c
index f853e95934375b9b232bb909874c1f2d489cfd81..f018ca9d9e66fcf9ededfe6961e388e1aafd779d 100644 (file)
@@ -7,8 +7,10 @@
  * Licensed under GPLv2, see file LICENSE in this tarball for details.
  */
 
+#include <sys/socket.h> /* netinet/in.h needs it */
 #include <netinet/in.h>
 #include <net/if.h>
+#include <sys/un.h>
 #include "libbb.h"
 
 void FAST_FUNC setsockopt_reuseaddr(int fd)
@@ -61,9 +63,9 @@ void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
                        close(s);
                if (s_addr->sa_family == AF_INET)
                        bb_perror_msg_and_die("%s (%s)",
-                               "cannot connect to remote host",
+                               "can't connect to remote host",
                                inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
-               bb_perror_msg_and_die("cannot connect to remote host");
+               bb_perror_msg_and_die("can't connect to remote host");
        }
 }
 
@@ -160,13 +162,26 @@ IF_FEATURE_IPV6(sa_family_t af,)
                int ai_flags)
 {
        int rc;
-       len_and_sockaddr *r = NULL;
+       len_and_sockaddr *r;
        struct addrinfo *result = NULL;
        struct addrinfo *used_res;
        const char *org_host = host; /* only for error msg */
        const char *cp;
        struct addrinfo hint;
 
+       if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
+               struct sockaddr_un *sun;
+
+               r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
+               r->len = sizeof(struct sockaddr_un);
+               r->u.sa.sa_family = AF_UNIX;
+               sun = (struct sockaddr_un *)&r->u.sa;
+               safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
+               return r;
+       }
+
+       r = NULL;
+
        /* Ugly parsing of host:addr */
        if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
                /* Even uglier parsing of [xx]:nn */
@@ -188,6 +203,7 @@ IF_FEATURE_IPV6(sa_family_t af,)
        }
        if (cp) { /* points to ":" or "]:" */
                int sz = cp - host + 1;
+
                host = safe_strncpy(alloca(sz), host, sz);
                if (ENABLE_FEATURE_IPV6 && *cp != ':') {
                        cp++; /* skip ']' */
@@ -371,6 +387,13 @@ static char* FAST_FUNC sockaddr2str(const struct sockaddr *sa, int flags)
        int rc;
        socklen_t salen;
 
+       if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
+               struct sockaddr_un *sun = (struct sockaddr_un *)sa;
+               return xasprintf("local:%.*s",
+                               (int) sizeof(sun->sun_path),
+                               sun->sun_path);
+       }
+
        salen = LSA_SIZEOF_SA;
 #if ENABLE_FEATURE_IPV6
        if (sa->sa_family == AF_INET)