nwrap: Fix resolving hostnames with a trailing dot.
[sfrench/samba-autobuild/.git] / lib / util / util_net.c
index 139c9d42bcaa51c0a4938bc07b58f71bca7d8c1d..d58855da5b2b99afc6082ef5ce8960e43b486dc6 100644 (file)
@@ -93,10 +93,10 @@ 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;
-       int int_flags;
 
        if (strchr_m(str, ':')) {
                char *p = strchr_m(str, '%');
@@ -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;
                }
        }
@@ -321,26 +330,70 @@ bool is_ipaddress_v6(const char *str)
        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 false;
@@ -723,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;
@@ -803,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}};