Commit de5c1fb8 authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

wininet: Initialize winsock before creating socket in Windows builds.

parent 4fe1be14
...@@ -2573,6 +2573,7 @@ HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName, ...@@ -2573,6 +2573,7 @@ HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED, SendAsyncCallback(&hIC->hdr, dwContext, INTERNET_STATUS_NAME_RESOLVED,
szaddr, strlen(szaddr)+1); szaddr, strlen(szaddr)+1);
init_winsock();
nsocket = socket(AF_INET,SOCK_STREAM,0); nsocket = socket(AF_INET,SOCK_STREAM,0);
if (nsocket == -1) if (nsocket == -1)
{ {
...@@ -2932,6 +2933,7 @@ static BOOL FTP_InitListenSocket(ftp_session_t *lpwfs) ...@@ -2932,6 +2933,7 @@ static BOOL FTP_InitListenSocket(ftp_session_t *lpwfs)
TRACE("\n"); TRACE("\n");
init_winsock();
lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0); lpwfs->lstnSocket = socket(PF_INET, SOCK_STREAM, 0);
if (lpwfs->lstnSocket == -1) if (lpwfs->lstnSocket == -1)
{ {
......
...@@ -3477,6 +3477,7 @@ BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwRe ...@@ -3477,6 +3477,7 @@ BOOL WINAPI InternetCheckConnectionW( LPCWSTR lpszUrl, DWORD dwFlags, DWORD dwRe
if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len)) if (!GetAddress(hostW, port, (struct sockaddr *)&saddr, &sa_len))
goto End; goto End;
init_winsock();
fd = socket(saddr.ss_family, SOCK_STREAM, 0); fd = socket(saddr.ss_family, SOCK_STREAM, 0);
if (fd != -1) if (fd != -1)
{ {
......
...@@ -478,6 +478,8 @@ BOOL init_urlcache(void) DECLSPEC_HIDDEN; ...@@ -478,6 +478,8 @@ BOOL init_urlcache(void) DECLSPEC_HIDDEN;
void free_urlcache(void) DECLSPEC_HIDDEN; void free_urlcache(void) DECLSPEC_HIDDEN;
void free_cookie(void) DECLSPEC_HIDDEN; void free_cookie(void) DECLSPEC_HIDDEN;
void init_winsock(void) DECLSPEC_HIDDEN;
#define MAX_REPLY_LEN 0x5B4 #define MAX_REPLY_LEN 0x5B4
/* Used for debugging - maybe need to be shared in the Wine debugging code ? */ /* Used for debugging - maybe need to be shared in the Wine debugging code ? */
......
...@@ -27,6 +27,10 @@ ...@@ -27,6 +27,10 @@
#define NONAMELESSUNION #define NONAMELESSUNION
#if defined(__MINGW32__) || defined (_MSC_VER) #if defined(__MINGW32__) || defined (_MSC_VER)
#define USE_WINSOCK
#endif
#ifdef USE_WINSOCK
#include <ws2tcpip.h> #include <ws2tcpip.h>
#endif #endif
...@@ -62,7 +66,7 @@ ...@@ -62,7 +66,7 @@
#ifdef HAVE_NETINET_TCP_H #ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h> # include <netinet/tcp.h>
#endif #endif
#if !defined(__MINGW32__) && !defined(_MSC_VER) #ifndef USE_WINSOCK
#include <errno.h> #include <errno.h>
#endif #endif
...@@ -317,10 +321,38 @@ static BOOL ensure_cred_handle(void) ...@@ -317,10 +321,38 @@ static BOOL ensure_cred_handle(void)
return TRUE; return TRUE;
} }
#ifdef USE_WINSOCK
static BOOL winsock_loaded = FALSE;
static BOOL WINAPI winsock_startup(INIT_ONCE *once, void *param, void **context)
{
WSADATA wsa_data;
DWORD res;
res = WSAStartup(MAKEWORD(1,1), &wsa_data);
if(res == ERROR_SUCCESS)
winsock_loaded = TRUE;
else
ERR("WSAStartup failed: %u\n", res);
return TRUE;
}
#endif
void init_winsock(void)
{
#ifdef USE_WINSOCK
static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
InitOnceExecuteOnce(&init_once, winsock_startup, NULL, NULL);
#endif
}
static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD timeout) static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD timeout)
{ {
int result; int result;
ULONG flag; ULONG flag;
DWORD res;
init_winsock();
assert(server->addr_len); assert(server->addr_len);
result = netconn->socket = socket(server->addr.ss_family, SOCK_STREAM, 0); result = netconn->socket = socket(server->addr.ss_family, SOCK_STREAM, 0);
...@@ -330,7 +362,8 @@ static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD t ...@@ -330,7 +362,8 @@ static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD t
result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len); result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len);
if(result == -1) if(result == -1)
{ {
if (sock_get_error() == WSAEINPROGRESS || sock_get_error() == WSAEWOULDBLOCK) { res = sock_get_error();
if (res == WSAEINPROGRESS || res == WSAEWOULDBLOCK) {
struct pollfd pfd; struct pollfd pfd;
int res; int res;
...@@ -441,12 +474,16 @@ void NETCON_unload(void) ...@@ -441,12 +474,16 @@ void NETCON_unload(void)
if(have_compat_cred_handle) if(have_compat_cred_handle)
FreeCredentialsHandle(&compat_cred_handle); FreeCredentialsHandle(&compat_cred_handle);
DeleteCriticalSection(&init_sechandle_cs); DeleteCriticalSection(&init_sechandle_cs);
#ifdef USE_WINSOCK
WSACleanup();
#endif
} }
/* translate a unix error code into a winsock one */ /* translate a unix error code into a winsock one */
int sock_get_error(void) int sock_get_error(void)
{ {
#if defined(__MINGW32__) || defined(_MSC_VER) #ifdef USE_WINSOCK
return WSAGetLastError(); return WSAGetLastError();
#else #else
switch (errno) switch (errno)
...@@ -538,7 +575,7 @@ int sock_recv(int fd, void *msg, size_t len, int flags) ...@@ -538,7 +575,7 @@ int sock_recv(int fd, void *msg, size_t len, int flags)
static void set_socket_blocking(int socket, blocking_mode_t mode) static void set_socket_blocking(int socket, blocking_mode_t mode)
{ {
#if defined(__MINGW32__) || defined (_MSC_VER) #ifdef USE_WINSOCK
ULONG arg = mode == BLOCKING_DISALLOW; ULONG arg = mode == BLOCKING_DISALLOW;
ioctlsocket(socket, FIONBIO, &arg); ioctlsocket(socket, FIONBIO, &arg);
#endif #endif
...@@ -1041,7 +1078,7 @@ BOOL NETCON_is_alive(netconn_t *netconn) ...@@ -1041,7 +1078,7 @@ BOOL NETCON_is_alive(netconn_t *netconn)
len = sock_recv(netconn->socket, &b, 1, MSG_PEEK|MSG_DONTWAIT); len = sock_recv(netconn->socket, &b, 1, MSG_PEEK|MSG_DONTWAIT);
return len == 1 || (len == -1 && sock_get_error() == WSAEWOULDBLOCK); return len == 1 || (len == -1 && sock_get_error() == WSAEWOULDBLOCK);
#elif defined(__MINGW32__) || defined(_MSC_VER) #elif defined(USE_WINSOCK)
ULONG mode; ULONG mode;
int len; int len;
char b; char b;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment