d3d9: Fix a few ok conditions in the visual tests.
[wine.git] / dlls / wininet / netconnection.c
blob63f3f8e5bef4b5f31706e0aa5e79b492c117093a
1 /*
2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
6 * David Hammerton
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_POLL_H
27 #include <poll.h>
28 #endif
29 #ifdef HAVE_SYS_POLL_H
30 # include <sys/poll.h>
31 #endif
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 #endif
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <stdio.h>
49 #include <errno.h>
51 #include "wine/library.h"
52 #include "windef.h"
53 #include "winbase.h"
54 #include "wininet.h"
55 #include "winerror.h"
56 #include "wincrypt.h"
58 #include "wine/debug.h"
59 #include "internet.h"
61 /* To avoid conflicts with the Unix socket headers. we only need it for
62 * the error codes anyway. */
63 #define USE_WS_PREFIX
64 #include "winsock2.h"
66 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
71 /* FIXME!!!!!!
72 * This should use winsock - To use winsock the functions will have to change a bit
73 * as they are designed for unix sockets.
74 * SSL stuff should use crypt32.dll
77 #ifdef SONAME_LIBSSL
79 #include <openssl/err.h>
81 static void *OpenSSL_ssl_handle;
82 static void *OpenSSL_crypto_handle;
84 static SSL_METHOD *meth;
85 static SSL_CTX *ctx;
87 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
89 /* OpenSSL functions that we use */
90 MAKE_FUNCPTR(SSL_library_init);
91 MAKE_FUNCPTR(SSL_load_error_strings);
92 MAKE_FUNCPTR(SSLv23_method);
93 MAKE_FUNCPTR(SSL_CTX_new);
94 MAKE_FUNCPTR(SSL_new);
95 MAKE_FUNCPTR(SSL_free);
96 MAKE_FUNCPTR(SSL_set_fd);
97 MAKE_FUNCPTR(SSL_connect);
98 MAKE_FUNCPTR(SSL_shutdown);
99 MAKE_FUNCPTR(SSL_write);
100 MAKE_FUNCPTR(SSL_read);
101 MAKE_FUNCPTR(SSL_get_verify_result);
102 MAKE_FUNCPTR(SSL_get_peer_certificate);
103 MAKE_FUNCPTR(SSL_CTX_get_timeout);
104 MAKE_FUNCPTR(SSL_CTX_set_timeout);
105 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
106 MAKE_FUNCPTR(i2d_X509);
108 /* OpenSSL's libcrypto functions that we use */
109 MAKE_FUNCPTR(BIO_new_fp);
110 MAKE_FUNCPTR(ERR_get_error);
111 MAKE_FUNCPTR(ERR_error_string);
112 #undef MAKE_FUNCPTR
114 #endif
116 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
118 connection->useSSL = FALSE;
119 connection->socketFD = -1;
120 if (useSSL)
122 #ifdef SONAME_LIBSSL
123 TRACE("using SSL connection\n");
124 if (OpenSSL_ssl_handle) /* already initialized everything */
125 return TRUE;
126 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
127 if (!OpenSSL_ssl_handle)
129 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
130 SONAME_LIBSSL);
131 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
132 return FALSE;
134 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
135 if (!OpenSSL_crypto_handle)
137 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
138 SONAME_LIBCRYPTO);
139 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
140 return FALSE;
143 /* mmm nice ugly macroness */
144 #define DYNSSL(x) \
145 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
146 if (!p##x) \
148 ERR("failed to load symbol %s\n", #x); \
149 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
150 return FALSE; \
153 DYNSSL(SSL_library_init);
154 DYNSSL(SSL_load_error_strings);
155 DYNSSL(SSLv23_method);
156 DYNSSL(SSL_CTX_new);
157 DYNSSL(SSL_new);
158 DYNSSL(SSL_free);
159 DYNSSL(SSL_set_fd);
160 DYNSSL(SSL_connect);
161 DYNSSL(SSL_shutdown);
162 DYNSSL(SSL_write);
163 DYNSSL(SSL_read);
164 DYNSSL(SSL_get_verify_result);
165 DYNSSL(SSL_get_peer_certificate);
166 DYNSSL(SSL_CTX_get_timeout);
167 DYNSSL(SSL_CTX_set_timeout);
168 DYNSSL(SSL_CTX_set_default_verify_paths);
169 DYNSSL(i2d_X509);
170 #undef DYNSSL
172 #define DYNCRYPTO(x) \
173 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
174 if (!p##x) \
176 ERR("failed to load symbol %s\n", #x); \
177 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
178 return FALSE; \
180 DYNCRYPTO(BIO_new_fp);
181 DYNCRYPTO(ERR_get_error);
182 DYNCRYPTO(ERR_error_string);
183 #undef DYNCRYPTO
185 pSSL_library_init();
186 pSSL_load_error_strings();
187 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
189 meth = pSSLv23_method();
190 connection->peek_msg = NULL;
191 connection->peek_msg_mem = NULL;
192 #else
193 FIXME("can't use SSL, not compiled in.\n");
194 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
195 return FALSE;
196 #endif
198 return TRUE;
201 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
203 if (connection->socketFD == -1)
204 return FALSE;
205 else
206 return TRUE;
209 /* translate a unix error code into a winsock one */
210 static int sock_get_error( int err )
212 #if !defined(__MINGW32__) && !defined (_MSC_VER)
213 switch (err)
215 case EINTR: return WSAEINTR;
216 case EBADF: return WSAEBADF;
217 case EPERM:
218 case EACCES: return WSAEACCES;
219 case EFAULT: return WSAEFAULT;
220 case EINVAL: return WSAEINVAL;
221 case EMFILE: return WSAEMFILE;
222 case EWOULDBLOCK: return WSAEWOULDBLOCK;
223 case EINPROGRESS: return WSAEINPROGRESS;
224 case EALREADY: return WSAEALREADY;
225 case ENOTSOCK: return WSAENOTSOCK;
226 case EDESTADDRREQ: return WSAEDESTADDRREQ;
227 case EMSGSIZE: return WSAEMSGSIZE;
228 case EPROTOTYPE: return WSAEPROTOTYPE;
229 case ENOPROTOOPT: return WSAENOPROTOOPT;
230 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
231 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
232 case EOPNOTSUPP: return WSAEOPNOTSUPP;
233 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
234 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
235 case EADDRINUSE: return WSAEADDRINUSE;
236 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
237 case ENETDOWN: return WSAENETDOWN;
238 case ENETUNREACH: return WSAENETUNREACH;
239 case ENETRESET: return WSAENETRESET;
240 case ECONNABORTED: return WSAECONNABORTED;
241 case EPIPE:
242 case ECONNRESET: return WSAECONNRESET;
243 case ENOBUFS: return WSAENOBUFS;
244 case EISCONN: return WSAEISCONN;
245 case ENOTCONN: return WSAENOTCONN;
246 case ESHUTDOWN: return WSAESHUTDOWN;
247 case ETOOMANYREFS: return WSAETOOMANYREFS;
248 case ETIMEDOUT: return WSAETIMEDOUT;
249 case ECONNREFUSED: return WSAECONNREFUSED;
250 case ELOOP: return WSAELOOP;
251 case ENAMETOOLONG: return WSAENAMETOOLONG;
252 case EHOSTDOWN: return WSAEHOSTDOWN;
253 case EHOSTUNREACH: return WSAEHOSTUNREACH;
254 case ENOTEMPTY: return WSAENOTEMPTY;
255 #ifdef EPROCLIM
256 case EPROCLIM: return WSAEPROCLIM;
257 #endif
258 #ifdef EUSERS
259 case EUSERS: return WSAEUSERS;
260 #endif
261 #ifdef EDQUOT
262 case EDQUOT: return WSAEDQUOT;
263 #endif
264 #ifdef ESTALE
265 case ESTALE: return WSAESTALE;
266 #endif
267 #ifdef EREMOTE
268 case EREMOTE: return WSAEREMOTE;
269 #endif
270 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
272 #endif
273 return err;
276 /******************************************************************************
277 * NETCON_create
278 * Basically calls 'socket()'
280 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
281 int type, int protocol)
283 #ifdef SONAME_LIBSSL
284 if (connection->useSSL)
285 return FALSE;
286 #endif
288 connection->socketFD = socket(domain, type, protocol);
289 if (connection->socketFD == -1)
291 INTERNET_SetLastError(sock_get_error(errno));
292 return FALSE;
294 return TRUE;
297 /******************************************************************************
298 * NETCON_close
299 * Basically calls 'close()' unless we should use SSL
301 BOOL NETCON_close(WININET_NETCONNECTION *connection)
303 int result;
305 if (!NETCON_connected(connection)) return FALSE;
307 #ifdef SONAME_LIBSSL
308 if (connection->useSSL)
310 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
311 connection->peek_msg = NULL;
312 connection->peek_msg_mem = NULL;
313 connection->peek_len = 0;
315 pSSL_shutdown(connection->ssl_s);
316 pSSL_free(connection->ssl_s);
317 connection->ssl_s = NULL;
319 connection->useSSL = FALSE;
321 #endif
323 result = closesocket(connection->socketFD);
324 connection->socketFD = -1;
326 if (result == -1)
328 INTERNET_SetLastError(sock_get_error(errno));
329 return FALSE;
331 return TRUE;
333 #ifdef SONAME_LIBSSL
334 static BOOL check_hostname(X509 *cert, char *hostname)
336 /* FIXME: implement */
337 return TRUE;
339 #endif
340 /******************************************************************************
341 * NETCON_secure_connect
342 * Initiates a secure connection over an existing plaintext connection.
344 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
346 #ifdef SONAME_LIBSSL
347 long verify_res;
348 X509 *cert;
349 int len;
350 char *hostname_unix;
352 /* can't connect if we are already connected */
353 if (connection->useSSL)
355 ERR("already connected\n");
356 return FALSE;
359 ctx = pSSL_CTX_new(meth);
360 if (!pSSL_CTX_set_default_verify_paths(ctx))
362 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
363 pERR_error_string(pERR_get_error(), 0));
364 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
365 return FALSE;
367 connection->ssl_s = pSSL_new(ctx);
368 if (!connection->ssl_s)
370 ERR("SSL_new failed: %s\n",
371 pERR_error_string(pERR_get_error(), 0));
372 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
373 goto fail;
376 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
378 ERR("SSL_set_fd failed: %s\n",
379 pERR_error_string(pERR_get_error(), 0));
380 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
381 goto fail;
384 if (pSSL_connect(connection->ssl_s) <= 0)
386 ERR("SSL_connect failed: %s\n",
387 pERR_error_string(pERR_get_error(), 0));
388 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
389 goto fail;
391 cert = pSSL_get_peer_certificate(connection->ssl_s);
392 if (!cert)
394 ERR("no certificate for server %s\n", debugstr_w(hostname));
395 /* FIXME: is this the best error? */
396 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
397 goto fail;
399 verify_res = pSSL_get_verify_result(connection->ssl_s);
400 if (verify_res != X509_V_OK)
402 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
403 /* FIXME: we should set an error and return, but we only warn at
404 * the moment */
407 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
408 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
409 if (!hostname_unix)
411 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
412 goto fail;
414 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
416 if (!check_hostname(cert, hostname_unix))
418 HeapFree(GetProcessHeap(), 0, hostname_unix);
419 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
420 goto fail;
423 HeapFree(GetProcessHeap(), 0, hostname_unix);
424 connection->useSSL = TRUE;
425 return TRUE;
427 fail:
428 if (connection->ssl_s)
430 pSSL_shutdown(connection->ssl_s);
431 pSSL_free(connection->ssl_s);
432 connection->ssl_s = NULL;
434 #endif
435 return FALSE;
438 /******************************************************************************
439 * NETCON_connect
440 * Connects to the specified address.
442 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
443 unsigned int addrlen)
445 int result;
447 if (!NETCON_connected(connection)) return FALSE;
449 result = connect(connection->socketFD, serv_addr, addrlen);
450 if (result == -1)
452 WARN("Unable to connect to host (%s)\n", strerror(errno));
453 INTERNET_SetLastError(sock_get_error(errno));
455 closesocket(connection->socketFD);
456 connection->socketFD = -1;
457 return FALSE;
460 return TRUE;
463 /******************************************************************************
464 * NETCON_send
465 * Basically calls 'send()' unless we should use SSL
466 * number of chars send is put in *sent
468 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
469 int *sent /* out */)
471 if (!NETCON_connected(connection)) return FALSE;
472 if (!connection->useSSL)
474 *sent = send(connection->socketFD, msg, len, flags);
475 if (*sent == -1)
477 INTERNET_SetLastError(sock_get_error(errno));
478 return FALSE;
480 return TRUE;
482 else
484 #ifdef SONAME_LIBSSL
485 if (flags)
486 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
487 *sent = pSSL_write(connection->ssl_s, msg, len);
488 if (*sent < 1 && len)
489 return FALSE;
490 return TRUE;
491 #else
492 return FALSE;
493 #endif
497 /******************************************************************************
498 * NETCON_recv
499 * Basically calls 'recv()' unless we should use SSL
500 * number of chars received is put in *recvd
502 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
503 int *recvd /* out */)
505 *recvd = 0;
506 if (!NETCON_connected(connection)) return FALSE;
507 if (!len)
508 return TRUE;
509 if (!connection->useSSL)
511 *recvd = recv(connection->socketFD, buf, len, flags);
512 if (*recvd == -1)
514 INTERNET_SetLastError(sock_get_error(errno));
515 return FALSE;
517 return TRUE;
519 else
521 #ifdef SONAME_LIBSSL
522 if (flags & ~(MSG_PEEK|MSG_WAITALL))
523 FIXME("SSL_read does not support the following flag: %08x\n", flags);
525 /* this ugly hack is all for MSG_PEEK. eww gross */
526 if (flags & MSG_PEEK && !connection->peek_msg)
528 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
530 else if (flags & MSG_PEEK && connection->peek_msg)
532 if (len < connection->peek_len)
533 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
534 *recvd = min(len, connection->peek_len);
535 memcpy(buf, connection->peek_msg, *recvd);
536 return TRUE;
538 else if (connection->peek_msg)
540 *recvd = min(len, connection->peek_len);
541 memcpy(buf, connection->peek_msg, *recvd);
542 connection->peek_len -= *recvd;
543 connection->peek_msg += *recvd;
544 if (connection->peek_len == 0)
546 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
547 connection->peek_msg_mem = NULL;
548 connection->peek_msg = NULL;
550 /* check if we got enough data from the peek buffer */
551 if (!(flags & MSG_WAITALL) || (*recvd == len))
552 return TRUE;
553 /* otherwise, fall through */
555 *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
556 if (flags & MSG_PEEK) /* must copy stuff into buffer */
558 connection->peek_len = *recvd;
559 if (!*recvd)
561 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
562 connection->peek_msg_mem = NULL;
563 connection->peek_msg = NULL;
565 else
566 memcpy(connection->peek_msg, buf, *recvd);
568 if (*recvd < 1 && len)
569 return FALSE;
570 return TRUE;
571 #else
572 return FALSE;
573 #endif
577 /******************************************************************************
578 * NETCON_query_data_available
579 * Returns the number of bytes of peeked data plus the number of bytes of
580 * queued, but unread data.
582 BOOL NETCON_query_data_available(WININET_NETCONNECTION *connection, DWORD *available)
584 *available = 0;
585 if (!NETCON_connected(connection))
586 return FALSE;
588 #ifdef SONAME_LIBSSL
589 if (connection->peek_msg) *available = connection->peek_len;
590 #endif
592 #ifdef FIONREAD
593 if (!connection->useSSL)
595 int unread;
596 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
597 if (!retval)
599 TRACE("%d bytes of queued, but unread data\n", unread);
600 *available += unread;
603 #endif
604 return TRUE;
607 /******************************************************************************
608 * NETCON_getNextLine
610 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
613 TRACE("\n");
615 if (!NETCON_connected(connection)) return FALSE;
617 if (!connection->useSSL)
619 struct pollfd pfd;
620 BOOL bSuccess = FALSE;
621 DWORD nRecv = 0;
623 pfd.fd = connection->socketFD;
624 pfd.events = POLLIN;
626 while (nRecv < *dwBuffer)
628 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
630 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
632 INTERNET_SetLastError(sock_get_error(errno));
633 goto lend;
636 if (lpszBuffer[nRecv] == '\n')
638 bSuccess = TRUE;
639 break;
641 if (lpszBuffer[nRecv] != '\r')
642 nRecv++;
644 else
646 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
647 goto lend;
651 lend: /* FIXME: don't use labels */
652 if (bSuccess)
654 lpszBuffer[nRecv++] = '\0';
655 *dwBuffer = nRecv;
656 TRACE(":%u %s\n", nRecv, lpszBuffer);
657 return TRUE;
659 else
661 return FALSE;
664 else
666 #ifdef SONAME_LIBSSL
667 long prev_timeout;
668 DWORD nRecv = 0;
669 BOOL success = TRUE;
671 prev_timeout = pSSL_CTX_get_timeout(ctx);
672 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
674 while (nRecv < *dwBuffer)
676 int recv = 1;
677 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
679 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
680 success = FALSE;
683 if (lpszBuffer[nRecv] == '\n')
685 success = TRUE;
686 break;
688 if (lpszBuffer[nRecv] != '\r')
689 nRecv++;
692 pSSL_CTX_set_timeout(ctx, prev_timeout);
693 if (success)
695 lpszBuffer[nRecv++] = '\0';
696 *dwBuffer = nRecv;
697 TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
698 return TRUE;
700 return FALSE;
701 #else
702 return FALSE;
703 #endif
708 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
710 #ifdef SONAME_LIBSSL
711 X509* cert;
712 unsigned char* buffer,*p;
713 INT len;
714 BOOL malloced = FALSE;
715 LPCVOID r = NULL;
717 if (!connection->useSSL)
718 return NULL;
720 cert = pSSL_get_peer_certificate(connection->ssl_s);
721 p = NULL;
722 len = pi2d_X509(cert,&p);
724 * SSL 0.9.7 and above malloc the buffer if it is null.
725 * however earlier version do not and so we would need to alloc the buffer.
727 * see the i2d_X509 man page for more details.
729 if (!p)
731 buffer = HeapAlloc(GetProcessHeap(),0,len);
732 p = buffer;
733 len = pi2d_X509(cert,&p);
735 else
737 buffer = p;
738 malloced = TRUE;
741 r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
743 if (malloced)
744 free(buffer);
745 else
746 HeapFree(GetProcessHeap(),0,buffer);
748 return r;
749 #else
750 return NULL;
751 #endif
754 DWORD NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
756 int result;
757 struct timeval tv;
759 /* FIXME: we should probably store the timeout in the connection to set
760 * when we do connect */
761 if (!NETCON_connected(connection))
762 return ERROR_SUCCESS;
764 /* value is in milliseconds, convert to struct timeval */
765 tv.tv_sec = value / 1000;
766 tv.tv_usec = (value % 1000) * 1000;
768 result = setsockopt(connection->socketFD, SOL_SOCKET,
769 send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
770 sizeof(tv));
772 if (result == -1)
774 WARN("setsockopt failed (%s)\n", strerror(errno));
775 return sock_get_error(errno);
778 return ERROR_SUCCESS;