nslookup: full circle. Here we started IPv6 work. Use "new API"
[oweals/busybox.git] / libbb / xconnect.c
index cf1f9d59acf27fd7aa81bce93c29e9b3100c9463..188837e36e320650131f54946200fb13784c3c32 100644 (file)
@@ -6,39 +6,60 @@
  *
  */
 
+#include <netinet/in.h>
 #include "libbb.h"
 
-/* Return network byte ordered port number for a service.
+int setsockopt_reuseaddr(int fd)
+{
+       return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1));
+}
+int setsockopt_broadcast(int fd)
+{
+       return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
+}
+
+void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
+{
+       if (connect(s, s_addr, addrlen) < 0) {
+               if (ENABLE_FEATURE_CLEAN_UP)
+                       close(s);
+               if (s_addr->sa_family == AF_INET)
+                       bb_perror_msg_and_die("%s (%s)",
+                               "cannot connect to remote host",
+                               inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
+               bb_perror_msg_and_die("cannot connect to remote host");
+       }
+}
+
+/* Return port number for a service.
  * If "port" is a number use it as the port.
  * If "port" is a name it is looked up in /etc/services, if it isnt found return
- * default_port
- */
-unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
+ * default_port */
+unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
 {
-       unsigned short port_nr = htons(default_port);
+       unsigned port_nr = default_port;
        if (port) {
-               char *endptr;
                int old_errno;
-               long port_long;
 
                /* Since this is a lib function, we're not allowed to reset errno to 0.
                 * Doing so could break an app that is deferring checking of errno. */
                old_errno = errno;
-               errno = 0;
-               port_long = strtol(port, &endptr, 10);
-               if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
+               port_nr = bb_strtou(port, NULL, 10);
+               if (errno || port_nr > 65535) {
                        struct servent *tserv = getservbyname(port, protocol);
-                       if (tserv) {
-                               port_nr = tserv->s_port;
-                       }
-               } else {
-                       port_nr = htons(port_long);
+                       port_nr = default_port;
+                       if (tserv)
+                               port_nr = ntohs(tserv->s_port);
                }
                errno = old_errno;
        }
-       return port_nr;
+       return (uint16_t)port_nr;
 }
 
+
+/* "Old" networking API - only IPv4 */
+
+/*
 void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
 {
        struct hostent *he;
@@ -49,16 +70,6 @@ void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
        memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
 }
 
-void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
-{
-       if (connect(s, s_addr, addrlen) < 0) {
-               if (ENABLE_FEATURE_CLEAN_UP) close(s);
-               if (s_addr->sa_family == AF_INET)
-                       bb_perror_msg_and_die("cannot connect to remote host (%s)",
-                               inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
-               bb_perror_msg_and_die("cannot connect to remote host");
-       }
-}
 
 int xconnect_tcp_v4(struct sockaddr_in *s_addr)
 {
@@ -66,155 +77,221 @@ int xconnect_tcp_v4(struct sockaddr_in *s_addr)
        xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
        return s;
 }
+*/
 
-int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
-{
-       union {
-               struct in_addr a4;
-#if ENABLE_FEATURE_IPV6
-               struct in6_addr a6;
-#endif
-       } a;
+/* "New" networking API */
 
-       /* TODO maybe: port spec? like n.n.n.n:nn */
 
+int get_nport(len_and_sockaddr *lsa)
+{
 #if ENABLE_FEATURE_IPV6
-       if (socklen >= sizeof(struct sockaddr_in6)
-        && inet_pton(AF_INET6, dotted, &a.a6) > 0
-       ) {
-               ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
-               ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
-               /* ((struct sockaddr_in6*)sp)->sin6_port = */
-               return 0; /* success */
+       if (lsa->sa.sa_family == AF_INET6) {
+               return lsa->sin6.sin6_port;
        }
 #endif
-       if (socklen >= sizeof(struct sockaddr_in)
-        && inet_pton(AF_INET, dotted, &a.a4) > 0
-       ) {
-               ((struct sockaddr_in*)sp)->sin_family = AF_INET;
-               ((struct sockaddr_in*)sp)->sin_addr = a.a4;
-               /* ((struct sockaddr_in*)sp)->sin_port = */
-               return 0; /* success */
+       if (lsa->sa.sa_family == AF_INET) {
+               return lsa->sin.sin_port;
        }
-       return 1;
+       /* What? UNIX socket? IPX?? :) */
+       return -1;
 }
 
-int xsocket_stream_ip4or6(sa_family_t *fp)
+void set_nport(len_and_sockaddr *lsa, unsigned port)
 {
-       int fd;
 #if ENABLE_FEATURE_IPV6
-       fd = socket(AF_INET6, SOCK_STREAM, 0);
-       if (fp) *fp = AF_INET6;
-       if (fd < 0)
+       if (lsa->sa.sa_family == AF_INET6) {
+               lsa->sin6.sin6_port = port;
+               return;
+       }
 #endif
-       {
-               fd = xsocket(AF_INET, SOCK_STREAM, 0);
-               if (fp) *fp = AF_INET;
+       if (lsa->sa.sa_family == AF_INET) {
+               lsa->sin.sin_port = port;
+               return;
        }
-       return fd;
+       /* What? UNIX socket? IPX?? :) */
 }
 
-int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
+/* host: "1.2.3.4[:port]", "www.google.com[:port]"
+ * port: if neither of above specifies port #
+ */
+static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
 {
-       static const int on = 1;
-       int fd;
-       sockaddr_inet sa;
-
-       memset(&sa, 0, sizeof(sa));
-       if (hostaddr && dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
-               bb_show_usage();
+       int rc;
+       len_and_sockaddr *r; // = NULL;
+       struct addrinfo *result = NULL;
+       const char *org_host = host; /* only for error msg */
+       const char *cp;
+       struct addrinfo hint;
 
-       if (!sa.sa.sa_family)
-               fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
-       else /* user specified bind addr dictates family */
-               fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
-       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+       /* Ugly parsing of host:addr */
+       if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
+               host++;
+               cp = strchr(host, ']');
+               if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
+                       bb_error_msg_and_die("bad address '%s'", org_host);
+                       //return r; /* return NULL */
+       } else {
+               cp = strrchr(host, ':');
+               if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
+                       /* There is more than one ':' (e.g. "::1") */
+                       cp = NULL; /* it's not a port spec */
+               }
+       }
+       if (cp) {
+               int sz = cp - host + 1;
+               host = safe_strncpy(alloca(sz), host, sz);
+               if (ENABLE_FEATURE_IPV6 && *cp != ':')
+                       cp++; /* skip ']' */
+               cp++; /* skip ':' */
+               port = xatou16(cp);
+       }
 
-       /* if (port >= 0) { */
-#if ENABLE_FEATURE_IPV6
-               if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
-                       sa.sin6.sin6_port = htons(port);
+       memset(&hint, 0 , sizeof(hint));
+       /* hint.ai_family = AF_UNSPEC; - zero anyway */
+#if !ENABLE_FEATURE_IPV6
+       hint.ai_family = AF_INET; /* do not try to find IPv6 */
 #endif
-               if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
-                       sa.sin.sin_port = htons(port);
-       /* } */
-
-       xbind(fd, &sa.sa, sizeof(sa));
-       return fd;
+       /* Needed. Or else we will get each address thrice (or more)
+        * for each possible socket type (tcp,udp,raw...): */
+       hint.ai_socktype = SOCK_STREAM;
+       hint.ai_flags = ai_flags;
+       rc = getaddrinfo(host, NULL, &hint, &result);
+       if (rc || !result)
+               bb_error_msg_and_die("bad address '%s'", org_host);
+       r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
+       r->len = result->ai_addrlen;
+       memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
+       set_nport(r, htons(port));
+       freeaddrinfo(result);
+       return r;
 }
 
-int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
+len_and_sockaddr* host2sockaddr(const char *host, int port)
 {
-       union {
-               struct in_addr a4;
-#if ENABLE_FEATURE_IPV6
-               struct in6_addr a6;
-#endif
-       } a;
-
-       /* TODO maybe: port spec? like n.n.n.n:nn */
+       return str2sockaddr(host, port, 0);
+}
 
-#if ENABLE_FEATURE_IPV6
-       if (socklen >= sizeof(struct sockaddr_in6)
-        && inet_pton(AF_INET6, dotted, &a.a6) > 0
-       ) {
-               ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
-               ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
-               /* ((struct sockaddr_in6*)sp)->sin6_port = */
-               return 0; /* success */
-       }
-#endif
-       if (socklen >= sizeof(struct sockaddr_in)
-        && inet_pton(AF_INET, dotted, &a.a4) > 0
-       ) {
-               ((struct sockaddr_in*)sp)->sin_family = AF_INET;
-               ((struct sockaddr_in*)sp)->sin_addr = a.a4;
-               /* ((struct sockaddr_in*)sp)->sin_port = */
-               return 0; /* success */
-       }
-       return 1;
+static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
+{
+       return str2sockaddr(host, port, NI_NUMERICHOST);
 }
 
-int xsocket_stream_ip4or6(sa_family_t *fp)
+int xsocket_stream(len_and_sockaddr **lsap)
 {
+       len_and_sockaddr *lsa;
        int fd;
+       int len = sizeof(struct sockaddr_in);
+       int family = AF_INET;
+
 #if ENABLE_FEATURE_IPV6
        fd = socket(AF_INET6, SOCK_STREAM, 0);
-       if (fp) *fp = AF_INET6;
-       if (fd < 0)
+       if (fd >= 0) {
+               len = sizeof(struct sockaddr_in6);
+               family = AF_INET6;
+       } else
 #endif
        {
                fd = xsocket(AF_INET, SOCK_STREAM, 0);
-               if (fp) *fp = AF_INET;
        }
+       lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
+       lsa->len = len;
+       lsa->sa.sa_family = family;
+       *lsap = lsa;
        return fd;
 }
 
-int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
+int create_and_bind_stream_or_die(const char *bindaddr, int port)
 {
-       static const int on = 1;
        int fd;
-       sockaddr_inet sa;
+       len_and_sockaddr *lsa;
 
-       memset(&sa, 0, sizeof(sa));
-       if (hostaddr) {
-               if (dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
-                       bb_error_msg_and_die("bad address '%s'", hostaddr);
+       if (bindaddr && bindaddr[0]) {
+               lsa = dotted2sockaddr(bindaddr, port);
+               /* currently NULL check is in str2sockaddr */
+               //if (!lsa)
+               //      bb_error_msg_and_die("bad address '%s'", bindaddr);
                /* user specified bind addr dictates family */
-               fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
-       } else 
-               fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
-       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+               fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
+       } else {
+               fd = xsocket_stream(&lsa);
+               set_nport(lsa, htons(port));
+       }
+       setsockopt_reuseaddr(fd);
+       xbind(fd, &lsa->sa, lsa->len);
+       free(lsa);
+       return fd;
+}
+
+int create_and_connect_stream_or_die(const char *peer, int port)
+{
+       int fd;
+       len_and_sockaddr *lsa;
 
-       /* if (port >= 0) { */
+       lsa = host2sockaddr(peer, port);
+       /* currently NULL check is in str2sockaddr */
+       //if (!lsa)
+       //      bb_error_msg_and_die("bad address '%s'", peer);
+       fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
+       setsockopt_reuseaddr(fd);
+       xconnect(fd, &lsa->sa, lsa->len);
+       free(lsa);
+       return fd;
+}
+
+int xconnect_stream(const len_and_sockaddr *lsa)
+{
+       int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
+       xconnect(fd, &lsa->sa, lsa->len);
+       return fd;
+}
+
+/* We hijack this constant to mean something else */
+/* It doesn't hurt because we will add this bit anyway */
+#define IGNORE_PORT NI_NUMERICSERV
+static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
+{
+       char host[128];
+       char serv[16];
+       int rc = getnameinfo(sa, salen,
+                       host, sizeof(host),
+       /* can do ((flags & IGNORE_PORT) ? NULL : serv) but why bother? */
+                       serv, sizeof(serv),
+                       /* do not resolve port# into service _name_ */
+                       flags | NI_NUMERICSERV
+       );
+       if (rc)
+               return NULL;
+       if (flags & IGNORE_PORT)
+               return xstrdup(host);
 #if ENABLE_FEATURE_IPV6
-               if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
-                       sa.sin6.sin6_port = htons(port);
+       if (sa->sa_family == AF_INET6) {
+               if (strchr(host, ':')) /* heh, it's not a resolved hostname */
+                       return xasprintf("[%s]:%s", host, serv);
+               /*return xasprintf("%s:%s", host, serv);*/
+               /* - fall through instead */
+       }
 #endif
-               if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
-                       sa.sin.sin_port = htons(port);
-       /* } */
+       /* For now we don't support anything else, so it has to be INET */
+       /*if (sa->sa_family == AF_INET)*/
+               return xasprintf("%s:%s", host, serv);
+       /*return xstrdup(host);*/
+}
 
-       xbind(fd, &sa.sa, sizeof(sa));
-       return fd;
+char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
+{
+       return sockaddr2str(sa, salen, 0);
+}
+
+char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa, socklen_t salen)
+{
+       return sockaddr2str(sa, salen, NI_NAMEREQD | IGNORE_PORT);
+}
+char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
+{
+       return sockaddr2str(sa, salen, NI_NUMERICHOST);
+}
+
+char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa, socklen_t salen)
+{
+       return sockaddr2str(sa, salen, NI_NUMERICHOST | IGNORE_PORT);
 }