ws2_32: Create a wrapper for connect.

This commit is contained in:
Mike Kaplinskiy 2010-07-31 23:21:31 -04:00 committed by Alexandre Julliard
parent fae0b6fa63
commit 15cc301728
1 changed files with 32 additions and 27 deletions

View File

@ -1803,6 +1803,33 @@ int WINAPI WS_closesocket(SOCKET s)
return SOCKET_ERROR; return SOCKET_ERROR;
} }
static int do_connect(int fd, const struct WS_sockaddr* name, int namelen)
{
union generic_unix_sockaddr uaddr;
unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr);
if (!uaddrlen)
return WSAEFAULT;
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 connect to magic replace-loopback address,
* assuming we really want to connect to localhost */
TRACE("Trying to connect to magic IP address, using "
"INADDR_LOOPBACK instead.\n");
in4->sin_addr.s_addr = htonl(WS_INADDR_LOOPBACK);
}
}
if (connect(fd, &uaddr.addr, uaddrlen) == 0)
return 0;
return wsaErrno();
}
/*********************************************************************** /***********************************************************************
* connect (WS2_32.4) * connect (WS2_32.4)
*/ */
@ -1814,33 +1841,11 @@ int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
if (fd != -1) if (fd != -1)
{ {
union generic_unix_sockaddr uaddr; int ret = do_connect(fd, name, namelen);
unsigned int uaddrlen = ws_sockaddr_ws2u(name, namelen, &uaddr); if (ret == 0)
goto connect_success;
if (!uaddrlen) if (ret == WSAEINPROGRESS)
{
SetLastError(WSAEFAULT);
}
else
{
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 connect to magic replace-loopback address,
* assuming we really want to connect to localhost */
TRACE("Trying to connect to magic IP address, using "
"INADDR_LOOPBACK instead.\n");
in4->sin_addr.s_addr = htonl(WS_INADDR_LOOPBACK);
}
}
if (connect(fd, &uaddr.addr, uaddrlen) == 0)
goto connect_success;
}
if (errno == EINPROGRESS)
{ {
/* tell wineserver that a connection is in progress */ /* tell wineserver that a connection is in progress */
_enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE, _enable_event(SOCKET2HANDLE(s), FD_CONNECT|FD_READ|FD_WRITE,
@ -1868,7 +1873,7 @@ int WINAPI WS_connect(SOCKET s, const struct WS_sockaddr* name, int namelen)
} }
else else
{ {
SetLastError(wsaErrno()); SetLastError(ret);
} }
release_sock_fd( s, fd ); release_sock_fd( s, fd );
} }