traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / libbb / xconnect.c
index 1b4f4f78affa01e5b3f5bb1a0bbf857cff294914..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)
@@ -37,16 +39,21 @@ int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
 
 len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
 {
-       len_and_sockaddr *lsa;
-       socklen_t len = 0;
+       len_and_sockaddr lsa;
+       len_and_sockaddr *lsa_ptr;
 
-       /* Can be optimized to do only one getsockname() */
-       if (getsockname(fd, NULL, &len) != 0)
+       lsa.len = LSA_SIZEOF_SA;
+       if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
                return NULL;
-       lsa = xzalloc(LSA_LEN_SIZE + len);
-       lsa->len = len;
-       getsockname(fd, &lsa->u.sa, &lsa->len);
-       return lsa;
+
+       lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
+       if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
+               lsa_ptr->len = lsa.len;
+               getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
+       } else {
+               memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
+       }
+       return lsa_ptr;
 }
 
 void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
@@ -56,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");
        }
 }
 
@@ -155,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 */
@@ -183,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 ']' */
@@ -366,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)