winhttp: Pass socket address as sockaddr_storage to netconn_connect.

Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Hans Leidekker <hans@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Jacek Caban 2017-07-18 00:24:53 +02:00 committed by Alexandre Julliard
parent d1199995bf
commit e129023eb7
3 changed files with 33 additions and 40 deletions

View File

@ -335,12 +335,25 @@ BOOL netconn_close( netconn_t *conn )
return TRUE; return TRUE;
} }
BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout ) BOOL netconn_connect( netconn_t *conn, const struct sockaddr_storage *sockaddr, int timeout )
{ {
unsigned int addr_len;
BOOL ret = FALSE; BOOL ret = FALSE;
int res; int res;
ULONG state; ULONG state;
switch (sockaddr->ss_family)
{
case AF_INET:
addr_len = sizeof(struct sockaddr_in);
break;
case AF_INET6:
addr_len = sizeof(struct sockaddr_in6);
break;
default:
assert(0);
}
if (timeout > 0) if (timeout > 0)
{ {
state = 1; state = 1;
@ -350,7 +363,7 @@ BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned
for (;;) for (;;)
{ {
res = 0; res = 0;
if (connect( conn->socket, sockaddr, addr_len ) < 0) if (connect( conn->socket, (const struct sockaddr *)sockaddr, addr_len ) < 0)
{ {
res = sock_get_error( errno ); res = sock_get_error( errno );
if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS) if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
@ -758,7 +771,7 @@ DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
static DWORD resolve_hostname( const WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len ) static DWORD resolve_hostname( const WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr_storage *sa )
{ {
char *hostname; char *hostname;
#ifdef HAVE_GETADDRINFO #ifdef HAVE_GETADDRINFO
@ -792,13 +805,6 @@ static DWORD resolve_hostname( const WCHAR *hostnameW, INTERNET_PORT port, struc
} }
} }
heap_free( hostname ); heap_free( hostname );
if (*sa_len < res->ai_addrlen)
{
WARN("address too small\n");
freeaddrinfo( res );
return ERROR_WINHTTP_NAME_NOT_RESOLVED;
}
*sa_len = res->ai_addrlen;
memcpy( sa, res->ai_addr, res->ai_addrlen ); memcpy( sa, res->ai_addr, res->ai_addrlen );
/* Copy port */ /* Copy port */
switch (res->ai_family) switch (res->ai_family)
@ -824,13 +830,6 @@ static DWORD resolve_hostname( const WCHAR *hostnameW, INTERNET_PORT port, struc
LeaveCriticalSection( &cs_gethostbyname ); LeaveCriticalSection( &cs_gethostbyname );
return ERROR_WINHTTP_NAME_NOT_RESOLVED; return ERROR_WINHTTP_NAME_NOT_RESOLVED;
} }
if (*sa_len < sizeof(struct sockaddr_in))
{
WARN("address too small\n");
LeaveCriticalSection( &cs_gethostbyname );
return ERROR_WINHTTP_NAME_NOT_RESOLVED;
}
*sa_len = sizeof(struct sockaddr_in);
memset( sa, 0, sizeof(struct sockaddr_in) ); memset( sa, 0, sizeof(struct sockaddr_in) );
memcpy( &sin->sin_addr, he->h_addr, he->h_length ); memcpy( &sin->sin_addr, he->h_addr, he->h_length );
sin->sin_family = he->h_addrtype; sin->sin_family = he->h_addrtype;
@ -845,17 +844,16 @@ struct resolve_args
{ {
const WCHAR *hostname; const WCHAR *hostname;
INTERNET_PORT port; INTERNET_PORT port;
struct sockaddr *sa; struct sockaddr_storage *sa;
socklen_t *sa_len;
}; };
static DWORD CALLBACK resolve_proc( LPVOID arg ) static DWORD CALLBACK resolve_proc( LPVOID arg )
{ {
struct resolve_args *ra = arg; struct resolve_args *ra = arg;
return resolve_hostname( ra->hostname, ra->port, ra->sa, ra->sa_len ); return resolve_hostname( ra->hostname, ra->port, ra->sa );
} }
BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len, int timeout ) BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr_storage *sa, int timeout )
{ {
DWORD ret; DWORD ret;
@ -868,7 +866,6 @@ BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa,
ra.hostname = hostname; ra.hostname = hostname;
ra.port = port; ra.port = port;
ra.sa = sa; ra.sa = sa;
ra.sa_len = sa_len;
thread = CreateThread( NULL, 0, resolve_proc, &ra, 0, NULL ); thread = CreateThread( NULL, 0, resolve_proc, &ra, 0, NULL );
if (!thread) return FALSE; if (!thread) return FALSE;
@ -878,7 +875,7 @@ BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa,
else ret = ERROR_WINHTTP_TIMEOUT; else ret = ERROR_WINHTTP_TIMEOUT;
CloseHandle( thread ); CloseHandle( thread );
} }
else ret = resolve_hostname( hostname, port, sa, sa_len ); else ret = resolve_hostname( hostname, port, sa );
if (ret) if (ret)
{ {

View File

@ -970,12 +970,12 @@ static BOOL secure_proxy_connect( request_t *request )
#define INET6_ADDRSTRLEN 46 #define INET6_ADDRSTRLEN 46
#endif #endif
static WCHAR *addr_to_str( struct sockaddr *addr ) static WCHAR *addr_to_str( struct sockaddr_storage *addr )
{ {
char buf[INET6_ADDRSTRLEN]; char buf[INET6_ADDRSTRLEN];
void *src; void *src;
switch (addr->sa_family) switch (addr->ss_family)
{ {
case AF_INET: case AF_INET:
src = &((struct sockaddr_in *)addr)->sin_addr; src = &((struct sockaddr_in *)addr)->sin_addr;
@ -984,10 +984,10 @@ static WCHAR *addr_to_str( struct sockaddr *addr )
src = &((struct sockaddr_in6 *)addr)->sin6_addr; src = &((struct sockaddr_in6 *)addr)->sin6_addr;
break; break;
default: default:
WARN("unsupported address family %d\n", addr->sa_family); WARN("unsupported address family %d\n", addr->ss_family);
return NULL; return NULL;
} }
if (!inet_ntop( addr->sa_family, src, buf, sizeof(buf) )) return NULL; if (!inet_ntop( addr->ss_family, src, buf, sizeof(buf) )) return NULL;
return strdupAW( buf ); return strdupAW( buf );
} }
@ -997,42 +997,38 @@ static BOOL open_connection( request_t *request )
connect_t *connect; connect_t *connect;
WCHAR *addressW = NULL; WCHAR *addressW = NULL;
INTERNET_PORT port; INTERNET_PORT port;
socklen_t slen;
struct sockaddr *saddr;
DWORD len; DWORD len;
if (request->netconn) goto done; if (request->netconn) goto done;
connect = request->connect; connect = request->connect;
port = connect->serverport ? connect->serverport : (request->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80); port = connect->serverport ? connect->serverport : (request->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
saddr = (struct sockaddr *)&connect->sockaddr;
slen = sizeof(struct sockaddr);
if (!connect->resolved) if (!connect->resolved)
{ {
len = strlenW( connect->servername ) + 1; len = strlenW( connect->servername ) + 1;
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, connect->servername, len ); send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_RESOLVING_NAME, connect->servername, len );
if (!netconn_resolve( connect->servername, port, saddr, &slen, request->resolve_timeout )) return FALSE; if (!netconn_resolve( connect->servername, port, &connect->sockaddr, request->resolve_timeout )) return FALSE;
connect->resolved = TRUE; connect->resolved = TRUE;
if (!(addressW = addr_to_str( saddr ))) return FALSE; if (!(addressW = addr_to_str( &connect->sockaddr ))) return FALSE;
len = strlenW( addressW ) + 1; len = strlenW( addressW ) + 1;
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, addressW, len ); send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_NAME_RESOLVED, addressW, len );
} }
if (!addressW && !(addressW = addr_to_str( saddr ))) return FALSE; if (!addressW && !(addressW = addr_to_str( &connect->sockaddr ))) return FALSE;
TRACE("connecting to %s:%u\n", debugstr_w(addressW), port); TRACE("connecting to %s:%u\n", debugstr_w(addressW), port);
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, addressW, 0 ); send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, addressW, 0 );
if (!(netconn = netconn_create( saddr->sa_family, SOCK_STREAM, 0 ))) if (!(netconn = netconn_create( connect->sockaddr.ss_family, SOCK_STREAM, 0 )))
{ {
heap_free( addressW ); heap_free( addressW );
return FALSE; return FALSE;
} }
netconn_set_timeout( netconn, TRUE, request->send_timeout ); netconn_set_timeout( netconn, TRUE, request->send_timeout );
netconn_set_timeout( netconn, FALSE, request->recv_timeout ); netconn_set_timeout( netconn, FALSE, request->recv_timeout );
if (!netconn_connect( netconn, saddr, slen, request->connect_timeout )) if (!netconn_connect( netconn, &connect->sockaddr, request->connect_timeout ))
{ {
netconn_close( netconn ); netconn_close( netconn );
heap_free( addressW ); heap_free( addressW );

View File

@ -282,12 +282,12 @@ void send_callback( object_header_t *, DWORD, LPVOID, DWORD ) DECLSPEC_HIDDEN;
void close_connection( request_t * ) DECLSPEC_HIDDEN; void close_connection( request_t * ) DECLSPEC_HIDDEN;
BOOL netconn_close( netconn_t * ) DECLSPEC_HIDDEN; BOOL netconn_close( netconn_t * ) DECLSPEC_HIDDEN;
BOOL netconn_connect( netconn_t *, const struct sockaddr *, unsigned int, int ) DECLSPEC_HIDDEN; BOOL netconn_connect( netconn_t *, const struct sockaddr_storage *, int ) DECLSPEC_HIDDEN;
netconn_t *netconn_create( int, int, int ) DECLSPEC_HIDDEN; netconn_t *netconn_create( int, int, int ) DECLSPEC_HIDDEN;
void netconn_unload( void ) DECLSPEC_HIDDEN; void netconn_unload( void ) DECLSPEC_HIDDEN;
ULONG netconn_query_data_available( netconn_t * ) DECLSPEC_HIDDEN; ULONG netconn_query_data_available( netconn_t * ) DECLSPEC_HIDDEN;
BOOL netconn_recv( netconn_t *, void *, size_t, int, int * ) DECLSPEC_HIDDEN; BOOL netconn_recv( netconn_t *, void *, size_t, int, int * ) DECLSPEC_HIDDEN;
BOOL netconn_resolve( WCHAR *, INTERNET_PORT, struct sockaddr *, socklen_t *, int ) DECLSPEC_HIDDEN; BOOL netconn_resolve( WCHAR *, INTERNET_PORT, struct sockaddr_storage *, int ) DECLSPEC_HIDDEN;
BOOL netconn_secure_connect( netconn_t *, WCHAR *, DWORD ) DECLSPEC_HIDDEN; BOOL netconn_secure_connect( netconn_t *, WCHAR *, DWORD ) DECLSPEC_HIDDEN;
BOOL netconn_send( netconn_t *, const void *, size_t, int * ) DECLSPEC_HIDDEN; BOOL netconn_send( netconn_t *, const void *, size_t, int * ) DECLSPEC_HIDDEN;
DWORD netconn_set_timeout( netconn_t *, BOOL, int ) DECLSPEC_HIDDEN; DWORD netconn_set_timeout( netconn_t *, BOOL, int ) DECLSPEC_HIDDEN;