From 1bcbe8651acbdf21ceb1cf42bd53e57fa39bdef7 Mon Sep 17 00:00:00 2001 From: Kai Blin Date: Thu, 24 Apr 2008 13:45:10 +0200 Subject: [PATCH] ws2_32: Work around the host name resolving to 127.x.x.x when using that for binding. --- dlls/ws2_32/socket.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/dlls/ws2_32/socket.c b/dlls/ws2_32/socket.c index a06aabfb3eb..1fe0034aaf5 100644 --- a/dlls/ws2_32/socket.c +++ b/dlls/ws2_32/socket.c @@ -3,7 +3,7 @@ * * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka. * Copyright (C) 2005 Marcus Meissner - * Copyright (C) 2006 Kai Blin + * Copyright (C) 2006-2008 Kai Blin * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -359,6 +359,8 @@ static const int ws_eai_map[][2] = { 0, 0 } }; +static const char magic_loopback_addr[] = {127, 12, 34, 56}; + static inline DWORD NtStatusToWSAError( const DWORD status ) { /* We only need to cover the status codes set by server async request handling */ @@ -1427,6 +1429,18 @@ int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen) } } #endif + if (name->sa_family == WS_AF_INET) + { + struct sockaddr_in *in4 = (struct sockaddr_in*) &uaddr; + if (memcmp(&in4->sin_addr, &magic_loopback_addr, 4) == 0) + { + /* Trying to bind to the default host interface, using + * INADDR_ANY instead*/ + WARN("Trying to bind to magic IP address, using " + "INADDR_ANY instead.\n"); + in4->sin_addr.s_addr = htonl(WS_INADDR_ANY); + } + } if (bind(fd, &uaddr.addr, uaddrlen) < 0) { int loc_errno = errno; @@ -3230,6 +3244,13 @@ struct WS_hostent* WINAPI WS_gethostbyname(const char* name) #else LeaveCriticalSection( &csWSgetXXXbyYYY ); #endif + if (retval->h_addr_list[0][0] == 127 && + strcmp(name, "localhost") != 0) + { + /* hostname != "localhost" but has loopback address. replace by our + * special address.*/ + memcpy(retval->h_addr_list[0], magic_loopback_addr, 4); + } TRACE( "%s ret %p\n", debugstr_a(name), retval ); return retval; }