lineedit: do not hang on error, but return error indicator.
[oweals/busybox.git] / libbb / xconnect.c
index 975844500df80e6c90fa12aa6dbf8dbd309069a2..3a6585caa8763aeb473ca6c6e4a7d15eff51b1a2 100644 (file)
@@ -4,11 +4,14 @@
  *
  * Connect to host at port using address resolution from getaddrinfo
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
+#include <sys/types.h>
+#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)
@@ -19,6 +22,8 @@ int FAST_FUNC setsockopt_broadcast(int fd)
 {
        return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
 }
+
+#ifdef SO_BINDTODEVICE
 int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
 {
        int r;
@@ -34,19 +39,42 @@ int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
                bb_perror_msg("can't bind to interface %s", iface);
        return r;
 }
+#else
+int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
+               const char *iface UNUSED_PARAM)
+{
+       bb_error_msg("SO_BINDTODEVICE is not supported on this system");
+       return -1;
+}
+#endif
 
-len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
+static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
 {
-       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 (get_name(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;
+               get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
+       } else {
+               memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
+       }
+       return lsa_ptr;
+}
+
+len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
+{
+       return get_lsa(fd, getsockname);
+}
+
+len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
+{
+       return get_lsa(fd, getpeername);
 }
 
 void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
@@ -56,9 +84,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");
        }
 }
 
@@ -89,28 +117,6 @@ unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsign
 }
 
 
-/* "Old" networking API - only IPv4 */
-
-/*
-void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
-{
-       struct hostent *he;
-
-       memset(s_in, 0, sizeof(struct sockaddr_in));
-       s_in->sin_family = AF_INET;
-       he = xgethostbyname(host);
-       memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
-}
-
-
-int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
-{
-       int s = xsocket(AF_INET, SOCK_STREAM, 0);
-       xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
-       return s;
-}
-*/
-
 /* "New" networking API */
 
 
@@ -151,23 +157,38 @@ 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)
 {
+IF_NOT_FEATURE_IPV6(sa_family_t af = AF_INET;)
        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,14 +218,43 @@ USE_FEATURE_IPV6(sa_family_t af,)
                                xfunc_die();
                        return NULL;
                }
+ skip: ;
        }
 
+       /* Next two if blocks allow to skip getaddrinfo()
+        * in case host name is a numeric IP(v6) address.
+        * getaddrinfo() initializes DNS resolution machinery,
+        * scans network config and such - tens of syscalls.
+        */
+       /* If we were not asked specifically for IPv6,
+        * check whether this is a numeric IPv4 */
+       IF_FEATURE_IPV6(if(af != AF_INET6)) {
+               struct in_addr in4;
+               if (inet_aton(host, &in4) != 0) {
+                       r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in));
+                       r->len = sizeof(struct sockaddr_in);
+                       r->u.sa.sa_family = AF_INET;
+                       r->u.sin.sin_addr = in4;
+                       goto set_port;
+               }
+       }
+#if ENABLE_FEATURE_IPV6
+       /* If we were not asked specifically for IPv4,
+        * check whether this is a numeric IPv6 */
+       if (af != AF_INET) {
+               struct in6_addr in6;
+               if (inet_pton(AF_INET6, host, &in6) > 0) {
+                       r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_in6));
+                       r->len = sizeof(struct sockaddr_in6);
+                       r->u.sa.sa_family = AF_INET6;
+                       r->u.sin6.sin6_addr = in6;
+                       goto set_port;
+               }
+       }
+#endif
+
        memset(&hint, 0 , sizeof(hint));
-#if !ENABLE_FEATURE_IPV6
-       hint.ai_family = AF_INET; /* do not try to find IPv6 */
-#else
        hint.ai_family = af;
-#endif
        /* Needed. Or else we will get each address thrice (or more)
         * for each possible socket type (tcp,udp,raw...): */
        hint.ai_socktype = SOCK_STREAM;
@@ -224,9 +278,11 @@ USE_FEATURE_IPV6(sa_family_t af,)
                }
        }
 #endif
-       r = xmalloc(offsetof(len_and_sockaddr, u.sa) + used_res->ai_addrlen);
+       r = xmalloc(LSA_LEN_SIZE + used_res->ai_addrlen);
        r->len = used_res->ai_addrlen;
        memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
+
+ set_port:
        set_nport(r, htons(port));
  ret:
        freeaddrinfo(result);
@@ -264,9 +320,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;
@@ -289,7 +345,7 @@ int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,
                len = sizeof(struct sockaddr_in6);
        }
 #endif
-       lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
+       lsa = xzalloc(LSA_LEN_SIZE + len);
        lsa->len = len;
        lsa->u.sa.sa_family = family;
        *lsap = lsa;
@@ -298,7 +354,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 +367,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 +417,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)