Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

4.4 KB
Raw Blame
diff --git a/dlls/ws2_32/protocol.c b/dlls/ws2_32/protocol.c
index cdc01a8..e2168ae 100644
--- a/dlls/ws2_32/protocol.c
+++ b/dlls/ws2_32/protocol.c
@@ -67,6 +67,103 @@ static int do_getaddrinfo( const char *node, const char *service,
     }
 }
 
+/* strip a trailing ".local" (the mDNS domain macOS appends to the host name) */
+static size_t hostname_len_without_local( const char *name )
+{
+    size_t len = strlen( name );
+
+    if (len > 6 && !strcasecmp( name + len - 6, ".local" )) len -= 6;
+    return len;
+}
+
+/* whether name is this machine's own host name: the same as host (case-insensitive, with
+ * or without ".local"), or its first label */
+static BOOL hostname_matches( const char *name, const char *host )
+{
+    size_t name_len = hostname_len_without_local( name ), host_len = hostname_len_without_local( host );
+
+    if (!name_len || !host_len || name_len > host_len) return FALSE;
+    if (strncasecmp( name, host, name_len )) return FALSE;
+    return name_len == host_len || host[name_len] == '.';
+}
+
+static BOOL is_own_hostname( const char *node, const char *fqdn )
+{
+    char hostname[256];
+    struct gethostname_params params = { hostname, sizeof(hostname) };
+
+    if (!node || !node[0]) return FALSE;
+    if (fqdn && hostname_matches( node, fqdn )) return TRUE;
+    if (WS_CALL( gethostname, &params )) return FALSE;
+    return hostname_matches( node, hostname );
+}
+
+static BOOL is_loopback_addr( const struct sockaddr *addr )
+{
+    if (addr->sa_family == AF_INET)
+        return ((const struct sockaddr_in *)addr)->sin_addr.S_un.S_un_b.s_b1 == 127;
+    if (addr->sa_family == AF_INET6)
+    {
+        const IN6_ADDR *a = &((const struct sockaddr_in6 *)addr)->sin6_addr;
+        static const BYTE v4mapped[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
+        static const BYTE loopback6[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+
+        if (!memcmp( a->u.Byte, loopback6, 16 )) return TRUE;
+        return !memcmp( a->u.Byte, v4mapped, 12 ) && a->u.Byte[12] == 127;
+    }
+    return FALSE;
+}
+
+static BOOL is_linklocal6_addr( const struct sockaddr *addr )
+{
+    const IN6_ADDR *a = &((const struct sockaddr_in6 *)addr)->sin6_addr;
+
+    return addr->sa_family == AF_INET6 && a->u.Byte[0] == 0xfe && (a->u.Byte[1] & 0xc0) == 0x80;
+}
+
+/* NotProton: on macOS the host's resolver answers this machine's own name (<host>.local)
+ * with lo0's addresses first (::1, fe80::1, 127.0.0.1, ...), then the real ones. Windows
+ * never returns loopback for its own name, and programs take the first result as their
+ * local address (TEKKEN 8's matchmaking SDK then hangs). Drop loopback and IPv6 link-local
+ * results while a routable one remains, else only loopback while any other remains,
+ * keeping the order. The list is one block freed through its head, so the kept entries
+ * are copied into the leading nodes and the list is cut after them. */
+static void filter_own_host_addrinfo( struct addrinfo *info )
+{
+    struct addrinfo **nodes, *kept, *ai;
+    char *canonname = info->ai_canonname;
+    unsigned int count = 0, keep = 0, i, pass;
+
+    for (ai = info; ai; ai = ai->ai_next) count++;
+    if (!(nodes = malloc( count * (sizeof(*nodes) + sizeof(*kept)) ))) return;
+    kept = (struct addrinfo *)(nodes + count);
+
+    for (i = 0, ai = info; ai; ai = ai->ai_next) nodes[i++] = ai;
+    for (pass = 0; pass < 2 && !keep; pass++)
+    {
+        for (i = 0; i < count; i++)
+        {
+            const struct sockaddr *addr = nodes[i]->ai_addr;
+
+            if (!addr || is_loopback_addr( addr )) continue;
+            if (!pass && is_linklocal6_addr( addr )) continue;
+            kept[keep++] = *nodes[i];
+        }
+    }
+
+    if (keep && keep < count)
+    {
+        TRACE( "dropping %u loopback/link-local results of %u for the local host name\n", count - keep, count );
+        for (i = 0; i < keep; i++)
+        {
+            *nodes[i] = kept[i];
+            nodes[i]->ai_next = i + 1 < keep ? nodes[i + 1] : NULL;
+        }
+        if (!info->ai_canonname) info->ai_canonname = canonname;
+    }
+    free( nodes );
+}
+
 static int dns_only_query( const char *node, const struct addrinfo *hints, struct addrinfo **result )
 {
     DNS_STATUS status;
@@ -219,6 +316,9 @@ int WINAPI getaddrinfo( const char *node, const char *service,
         }
     }
 
+    if (!ret && *info && is_own_hostname( node, fqdn ))
+        filter_own_host_addrinfo( *info );
+
     free( fqdn );
     free( nodev6 );