nwrap: Fix resolving hostnames with a trailing dot.
[sfrench/samba-autobuild/.git] / lib / util / util_net.c
index 57a0b1e40c32b5ab64f331dacca789558ced01fe..d58855da5b2b99afc6082ef5ce8960e43b486dc6 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/>.
 */
@@ -73,10 +73,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;
@@ -94,6 +93,7 @@ static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
                bool prefer_ipv4)
 {
        struct addrinfo *res = NULL;
+       int int_flags;
 #if defined(HAVE_IPV6)
        char addr[INET6_ADDRSTRLEN];
        unsigned int scope_id = 0;
@@ -107,9 +107,18 @@ static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
                 */
 
                if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
-                       strlcpy(addr, str,
-                               MIN(PTR_DIFF(p,str)+1,
-                                       sizeof(addr)));
+                       /* Length of string we want to copy.
+                          This is IP:v6:addr (removing the %ifname).
+                        */
+                       size_t len = PTR_DIFF(p,str);
+
+                       if (len+1 > sizeof(addr)) {
+                               /* string+nul too long for array. */
+                               return false;
+                       }
+                       memcpy(addr, str, len);
+                       addr[len] = '\0';
+
                        str = addr;
                }
        }
@@ -117,7 +126,13 @@ static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
 
        zero_sockaddr(pss);
 
-       if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
+       if (flags & AI_NUMERICHOST) {
+               int_flags = flags;
+       } else {
+               int_flags = flags|AI_ADDRCONFIG;
+       }
+
+       if (!interpret_string_addr_internal(&res, str, int_flags)) {
                return false;
        }
        if (!res) {
@@ -306,38 +321,91 @@ bool is_ipaddress_v4(const char *str)
 }
 
 /**
- * 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;
 
        if (strchr_m(str, ':')) {
-               char addr[INET6_ADDRSTRLEN];
-               struct in6_addr dest6;
-               const char *sp = str;
+               char buf[INET6_ADDRSTRLEN] = { 0, };
+               size_t len;
+               const char *addr = str;
+               const char *idxs = NULL;
+               unsigned int idx = 0;
+               struct in6_addr ip6;
                char *p = strchr_m(str, '%');
 
+               if (p && (p > str)) {
+                       len = PTR_DIFF(p, str);
+                       idxs = p + 1;
+               } else {
+                       len = strlen(str);
+               }
+
+               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;
+
+                       ret = sscanf(idxs, "%5u%c", &idx, &c);
+                       if (ret != 1) {
+                               idx = 0;
+                       }
+
+                       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 (p && (p > str) && (if_nametoindex(p+1) != 0)) {
-                       strlcpy(addr, str,
-                               MIN(PTR_DIFF(p,str)+1,
-                                       sizeof(addr)));
-                       sp = addr;
+                       if (idx > 0) {
+                               /* a valid index */
+                               idxs = NULL;
+                       }
                }
-               ret = inet_pton(AF_INET6, sp, &dest6);
-               if (ret > 0) {
-                       return true;
+
+               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);
 }
 
 /**
@@ -417,6 +485,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.
  */
@@ -685,7 +776,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;
@@ -765,11 +859,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}};