lib: Fix a false/NULL hickup
[nivanova/samba-autobuild/.git] / lib / util / util_net.c
index 98eef3bd8bc3f1f1e310a25110d0826b4ef760d3..57db4fae4d81593a3ece0448a39575f4980d17f7 100644 (file)
@@ -8,17 +8,17 @@
    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
    Copyright (C) James J Myers 2003
    Copyright (C) Tim Potter      2000-2001
-    
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@@ -41,6 +41,115 @@ void zero_sockaddr(struct sockaddr_storage *pss)
        pss->ss_family = AF_INET;
 }
 
+static char *normalize_ipv6_literal(const char *str, char *buf, size_t *_len)
+{
+#define IPv6_LITERAL_NET ".ipv6-literal.net"
+       const size_t llen = sizeof(IPv6_LITERAL_NET) - 1;
+       size_t len = *_len;
+       int cmp;
+       size_t i;
+       size_t idx_chars = 0;
+       size_t cnt_delimiter = 0;
+       size_t cnt_chars = 0;
+
+       if (len <= llen) {
+               return NULL;
+       }
+
+       /* ignore a trailing '.' */
+       if (str[len - 1] == '.') {
+               len -= 1;
+       }
+
+       len -= llen;
+       if (len >= INET6_ADDRSTRLEN) {
+               return NULL;
+       }
+       if (len < 2) {
+               return NULL;
+       }
+
+       cmp = strncasecmp(&str[len], IPv6_LITERAL_NET, llen);
+       if (cmp != 0) {
+               return NULL;
+       }
+
+       for (i = 0; i < len; i++) {
+               if (idx_chars != 0) {
+                       break;
+               }
+
+               switch (str[i]) {
+               case '-':
+                       buf[i] = ':';
+                       cnt_chars = 0;
+                       cnt_delimiter += 1;
+                       break;
+               case 's':
+                       buf[i] = SCOPE_DELIMITER;
+                       idx_chars += 1;
+                       break;
+               case '0':
+               case '1':
+               case '2':
+               case '3':
+               case '4':
+               case '5':
+               case '6':
+               case '7':
+               case '8':
+               case '9':
+               case 'a':
+               case 'A':
+               case 'b':
+               case 'B':
+               case 'c':
+               case 'C':
+               case 'd':
+               case 'D':
+               case 'e':
+               case 'E':
+               case 'f':
+               case 'F':
+                       buf[i] = str[i];
+                       cnt_chars += 1;
+                       break;
+               default:
+                       return NULL;
+               }
+               if (cnt_chars > 4) {
+                       return NULL;
+               }
+               if (cnt_delimiter > 7) {
+                       return NULL;
+               }
+       }
+
+       if (cnt_delimiter < 2) {
+               return NULL;
+       }
+
+       for (; idx_chars != 0 && i < len; i++) {
+               switch (str[i]) {
+               case SCOPE_DELIMITER:
+               case ':':
+                       return NULL;
+               default:
+                       buf[i] = str[i];
+                       idx_chars += 1;
+                       break;
+               }
+       }
+
+       if (idx_chars == 1) {
+               return NULL;
+       }
+
+       buf[i] = '\0';
+       *_len = len;
+       return buf;
+}
+
 /**
  * Wrap getaddrinfo...
  */
@@ -49,11 +158,89 @@ bool interpret_string_addr_internal(struct addrinfo **ppres,
 {
        int ret;
        struct addrinfo hints;
+#if defined(HAVE_IPV6)
+       char addr[INET6_ADDRSTRLEN*2] = { 0, };
+       unsigned int scope_id = 0;
+       size_t len = strlen(str);
+#endif
 
        ZERO_STRUCT(hints);
 
        /* By default make sure it supports TCP. */
        hints.ai_socktype = SOCK_STREAM;
+
+       /* always try as a numeric host first. This prevents unnecessary name
+        * lookups, and also ensures we accept IPv6 addresses */
+       hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
+
+#if defined(HAVE_IPV6)
+       if (len < sizeof(addr)) {
+               char *p = NULL;
+
+               p = normalize_ipv6_literal(str, addr, &len);
+               if (p != NULL) {
+                       hints.ai_family = AF_INET6;
+                       str = p;
+               }
+       }
+
+       if (strchr_m(str, ':')) {
+               char *p = strchr_m(str, SCOPE_DELIMITER);
+
+               /*
+                * Cope with link-local.
+                * This is IP:v6:addr%ifname.
+                */
+
+               if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
+                       /* Length of string we want to copy.
+                          This is IP:v6:addr (removing the %ifname).
+                        */
+                       len = PTR_DIFF(p,str);
+
+                       if (len+1 > sizeof(addr)) {
+                               /* string+nul too long for array. */
+                               return false;
+                       }
+                       if (str != addr) {
+                               memcpy(addr, str, len);
+                       }
+                       addr[len] = '\0';
+
+                       str = addr;
+               }
+       }
+#endif
+
+       ret = getaddrinfo(str, NULL, &hints, ppres);
+       if (ret == 0) {
+#if defined(HAVE_IPV6)
+               struct sockaddr_in6 *ps6 = NULL;
+
+               if (scope_id == 0) {
+                       return true;
+               }
+               if (ppres == NULL) {
+                       return true;
+               }
+               if ((*ppres) == NULL) {
+                       return true;
+               }
+               if ((*ppres)->ai_addr->sa_family != AF_INET6) {
+                       return true;
+               }
+
+               ps6 = (struct sockaddr_in6 *)(*ppres)->ai_addr;
+
+               if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
+                               ps6->sin6_scope_id == 0) {
+                       ps6->sin6_scope_id = scope_id;
+               }
+#endif
+
+               return true;
+       }
+
        hints.ai_flags = flags;
 
        /* Linux man page on getaddrinfo() says port will be
@@ -64,10 +251,9 @@ bool interpret_string_addr_internal(struct addrinfo **ppres,
                        ppres);
 
        if (ret) {
-               DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
-                       "for name %s [%s]\n",
-                       str,
-                       gai_strerror(ret) ));
+               DEBUG(3, ("interpret_string_addr_internal: "
+                         "getaddrinfo failed for name %s (flags %d) [%s]\n",
+                         str, flags, gai_strerror(ret)));
                return false;
        }
        return true;
@@ -85,30 +271,17 @@ static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
                bool prefer_ipv4)
 {
        struct addrinfo *res = NULL;
-#if defined(HAVE_IPV6)
-       char addr[INET6_ADDRSTRLEN];
-       unsigned int scope_id = 0;
+       int int_flags;
 
-       if (strchr_m(str, ':')) {
-               char *p = strchr_m(str, '%');
-
-               /*
-                * Cope with link-local.
-                * This is IP:v6:addr%ifname.
-                */
+       zero_sockaddr(pss);
 
-               if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
-                       strlcpy(addr, str,
-                               MIN(PTR_DIFF(p,str)+1,
-                                       sizeof(addr)));
-                       str = addr;
-               }
+       if (flags & AI_NUMERICHOST) {
+               int_flags = flags;
+       } else {
+               int_flags = flags|AI_ADDRCONFIG;
        }
-#endif
 
-       zero_sockaddr(pss);
-
-       if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
+       if (!interpret_string_addr_internal(&res, str, int_flags)) {
                return false;
        }
        if (!res) {
@@ -133,16 +306,6 @@ static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
                memcpy(pss, res->ai_addr, res->ai_addrlen);
        }
 
-#if defined(HAVE_IPV6)
-       if (pss->ss_family == AF_INET6 && scope_id) {
-               struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
-               if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
-                               ps6->sin6_scope_id == 0) {
-                       ps6->sin6_scope_id = scope_id;
-               }
-       }
-#endif
-
        freeaddrinfo(res);
        return true;
 }
@@ -296,39 +459,117 @@ bool is_ipaddress_v4(const char *str)
        return false;
 }
 
+bool is_ipv6_literal(const char *str)
+{
+#if defined(HAVE_IPV6)
+       char buf[INET6_ADDRSTRLEN*2] = { 0, };
+       size_t len = strlen(str);
+       char *p = NULL;
+
+       if (len >= sizeof(buf)) {
+               return false;
+       }
+
+       p = normalize_ipv6_literal(str, buf, &len);
+       if (p == NULL) {
+               return false;
+       }
+
+       return true;
+#else
+       return false;
+#endif
+}
+
 /**
- * Return true if a string could be an IPv4 or IPv6 address.
+ * Return true if a string could be a IPv6 address.
  */
 
-bool is_ipaddress(const char *str)
+bool is_ipaddress_v6(const char *str)
 {
 #if defined(HAVE_IPV6)
        int ret = -1;
+       char *p = NULL;
+       char buf[INET6_ADDRSTRLEN] = { 0, };
+       size_t len;
+       const char *addr = str;
+       const char *idxs = NULL;
+       unsigned int idx = 0;
+       struct in6_addr ip6;
+
+       p = strchr_m(str, ':');
+       if (p == NULL) {
+               return is_ipv6_literal(str);
+       }
 
-       if (strchr_m(str, ':')) {
-               char addr[INET6_ADDRSTRLEN];
-               struct in6_addr dest6;
-               const char *sp = str;
-               char *p = strchr_m(str, '%');
+       p = strchr_m(str, SCOPE_DELIMITER);
+       if (p && (p > str)) {
+               len = PTR_DIFF(p, str);
+               idxs = p + 1;
+       } else {
+               len = strlen(str);
+       }
 
-               /*
-                * Cope with link-local.
-                * This is IP:v6:addr%ifname.
-                */
+       if (len >= sizeof(buf)) {
+               return false;
+       }
+       if (idxs != NULL) {
+               strncpy(buf, str, len);
+               addr = buf;
+       }
+
+       /*
+        * Cope with link-local.
+        * This is IP:v6:addr%ifidx.
+        */
+       if (idxs != NULL) {
+               char c;
 
-               if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
-                       strlcpy(addr, str,
-                               MIN(PTR_DIFF(p,str)+1,
-                                       sizeof(addr)));
-                       sp = addr;
+               ret = sscanf(idxs, "%5u%c", &idx, &c);
+               if (ret != 1) {
+                       idx = 0;
                }
-               ret = inet_pton(AF_INET6, sp, &dest6);
-               if (ret > 0) {
-                       return true;
+
+               if (idx > 0 && idx < UINT16_MAX) {
+                       /* a valid index */
+                       idxs = NULL;
                }
        }
+
+       /*
+        * Cope with link-local.
+        * This is IP:v6:addr%ifname.
+        */
+       if (idxs != NULL) {
+               idx = if_nametoindex(idxs);
+
+               if (idx > 0) {
+                       /* a valid index */
+                       idxs = NULL;
+               }
+       }
+
+       if (idxs != NULL) {
+               return false;
+       }
+
+       ret = inet_pton(AF_INET6, addr, &ip6);
+       if (ret <= 0) {
+               return false;
+       }
+
+       return true;
 #endif
-       return is_ipaddress_v4(str);
+       return false;
+}
+
+/**
+ * Return true if a string could be an IPv4 or IPv6 address.
+ */
+
+bool is_ipaddress(const char *str)
+{
+       return is_ipaddress_v4(str) || is_ipaddress_v6(str);
 }
 
 /**
@@ -408,6 +649,29 @@ void zero_ip_v4(struct in_addr *ip)
        ZERO_STRUCTP(ip);
 }
 
+bool is_linklocal_addr(const struct sockaddr_storage *pss)
+{
+#ifdef HAVE_IPV6
+       if (pss->ss_family == AF_INET6) {
+               const struct in6_addr *pin6 =
+                       &((const struct sockaddr_in6 *)pss)->sin6_addr;
+               return IN6_IS_ADDR_LINKLOCAL(pin6);
+       }
+#endif
+       if (pss->ss_family == AF_INET) {
+               const struct in_addr *pin =
+                       &((const struct sockaddr_in *)pss)->sin_addr;
+               struct in_addr ll_addr;
+               struct in_addr mask_addr;
+
+               /* 169.254.0.0/16, is link local, see RFC 3927 */
+               ll_addr.s_addr = 0xa9fe0000;
+               mask_addr.s_addr = 0xffff0000;
+               return same_net_v4(*pin, ll_addr, mask_addr);
+       }
+       return false;
+}
+
 /**
  * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
  */
@@ -653,11 +917,13 @@ int get_socket_port(int fd)
 
 #if defined(HAVE_IPV6)
        if (sa.ss_family == AF_INET6) {
-               return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
+               struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&sa;
+               return ntohs(sa_in6->sin6_port);
        }
 #endif
        if (sa.ss_family == AF_INET) {
-               return ntohs(((struct sockaddr_in *)&sa)->sin_port);
+               struct sockaddr_in *sa_in = (struct sockaddr_in *)&sa;
+               return ntohs(sa_in->sin_port);
        }
        return -1;
 }
@@ -676,7 +942,10 @@ static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
         * zero IPv6 address. No good choice here.
         */
 
-       strlcpy(addr_buf, "0.0.0.0", addr_len);
+       if (strlcpy(addr_buf, "0.0.0.0", addr_len) >= addr_len) {
+               /* Truncate ! */
+               return NULL;
+       }
 
        if (fd == -1) {
                return addr_buf;
@@ -756,11 +1025,17 @@ static const smb_socket_option socket_options[] = {
 #ifdef TCP_QUICKACK
   {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
 #endif
+#ifdef TCP_NODELAYACK
+  {"TCP_NODELAYACK", IPPROTO_TCP, TCP_NODELAYACK, 0, OPT_BOOL},
+#endif
 #ifdef TCP_KEEPALIVE_THRESHOLD
   {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
 #endif
 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
   {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
+#endif
+#ifdef TCP_DEFER_ACCEPT
+  {"TCP_DEFER_ACCEPT", IPPROTO_TCP, TCP_DEFER_ACCEPT, 0, OPT_INT},
 #endif
   {NULL,0,0,0,0}};