traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / libbb / xconnect.c
index 975844500df80e6c90fa12aa6dbf8dbd309069a2..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");
        }
 }
 
@@ -151,23 +158,37 @@ void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
  * port: if neither of above specifies port # */
 static len_and_sockaddr* str2sockaddr(
                const char *host, int port,
-USE_FEATURE_IPV6(sa_family_t af,)
+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 */
                host++;
                cp = strchr(host, ']');
-               if (!cp || cp[1] != ':') { /* Malformed: must have [xx]:nn */
+               if (!cp || (cp[1] != ':' && cp[1] != '\0')) {
+                       /* Malformed: must be [xx]:nn or [xx] */
                        bb_error_msg("bad address '%s'", org_host);
                        if (ai_flags & DIE_ON_ERROR)
                                xfunc_die();
@@ -182,9 +203,13 @@ USE_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 != ':')
+               if (ENABLE_FEATURE_IPV6 && *cp != ':') {
                        cp++; /* skip ']' */
+                       if (*cp == '\0') /* [xx] without port */
+                               goto skip;
+               }
                cp++; /* skip ':' */
                port = bb_strtou(cp, NULL, 10);
                if (errno || (unsigned)port > 0xffff) {
@@ -193,6 +218,7 @@ USE_FEATURE_IPV6(sa_family_t af,)
                                xfunc_die();
                        return NULL;
                }
+ skip: ;
        }
 
        memset(&hint, 0 , sizeof(hint));
@@ -264,9 +290,9 @@ len_and_sockaddr* FAST_FUNC xdotted2sockaddr(const char *host, int port)
 }
 
 #undef xsocket_type
-int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type)
+int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
 {
-       SKIP_FEATURE_IPV6(enum { family = AF_INET };)
+       IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
        len_and_sockaddr *lsa;
        int fd;
        int len;
@@ -298,7 +324,7 @@ int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,
 
 int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
 {
-       return xsocket_type(lsap, USE_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
+       return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
 }
 
 static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
@@ -311,7 +337,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, USE_FEATURE_IPV6(AF_UNSPEC,) sock_type);
+               fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
                set_nport(lsa, htons(port));
        }
        setsockopt_reuseaddr(fd);
@@ -361,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)