winex11: Ignore the initial WM_STATE transition out of withdrawn state.
[wine/multimedia.git] / dlls / wininet / netconnection.c
blob77f39bbf6258b5f9f17b1c825a808b892cc8723e
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 #include <sys/types.h>
27 #ifdef HAVE_POLL_H
28 #include <poll.h>
29 #endif
30 #ifdef HAVE_SYS_POLL_H
31 # include <sys/poll.h>
32 #endif
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
42 #endif
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_IOCTL_H
47 # include <sys/ioctl.h>
48 #endif
49 #include <time.h>
50 #ifdef HAVE_NETDB_H
51 # include <netdb.h>
52 #endif
53 #ifdef HAVE_NETINET_IN_H
54 # include <netinet/in.h>
55 #endif
56 #ifdef HAVE_OPENSSL_SSL_H
57 # include <openssl/ssl.h>
58 #undef FAR
59 #undef DSA
60 #endif
61 #ifdef HAVE_SYS_SOCKET_H
62 # include <sys/socket.h>
63 #endif
65 #include <stdarg.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <stdio.h>
69 #include <errno.h>
71 #include "wine/library.h"
72 #include "windef.h"
73 #include "winbase.h"
74 #include "wininet.h"
75 #include "winerror.h"
76 #include "wincrypt.h"
78 #include "wine/debug.h"
79 #include "internet.h"
81 /* To avoid conflicts with the Unix socket headers. we only need it for
82 * the error codes anyway. */
83 #define USE_WS_PREFIX
84 #include "winsock2.h"
86 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
89 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
91 /* FIXME!!!!!!
92 * This should use winsock - To use winsock the functions will have to change a bit
93 * as they are designed for unix sockets.
94 * SSL stuff should use crypt32.dll
97 #ifdef SONAME_LIBSSL
99 #include <openssl/err.h>
101 static void *OpenSSL_ssl_handle;
102 static void *OpenSSL_crypto_handle;
104 static SSL_METHOD *meth;
105 static SSL_CTX *ctx;
107 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
109 /* OpenSSL functions that we use */
110 MAKE_FUNCPTR(SSL_library_init);
111 MAKE_FUNCPTR(SSL_load_error_strings);
112 MAKE_FUNCPTR(SSLv23_method);
113 MAKE_FUNCPTR(SSL_CTX_new);
114 MAKE_FUNCPTR(SSL_new);
115 MAKE_FUNCPTR(SSL_free);
116 MAKE_FUNCPTR(SSL_set_fd);
117 MAKE_FUNCPTR(SSL_connect);
118 MAKE_FUNCPTR(SSL_shutdown);
119 MAKE_FUNCPTR(SSL_write);
120 MAKE_FUNCPTR(SSL_read);
121 MAKE_FUNCPTR(SSL_get_verify_result);
122 MAKE_FUNCPTR(SSL_get_peer_certificate);
123 MAKE_FUNCPTR(SSL_CTX_get_timeout);
124 MAKE_FUNCPTR(SSL_CTX_set_timeout);
125 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
126 MAKE_FUNCPTR(i2d_X509);
128 /* OpenSSL's libcrypto functions that we use */
129 MAKE_FUNCPTR(BIO_new_fp);
130 MAKE_FUNCPTR(ERR_get_error);
131 MAKE_FUNCPTR(ERR_error_string);
132 #undef MAKE_FUNCPTR
134 #endif
136 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
138 connection->useSSL = FALSE;
139 connection->socketFD = -1;
140 if (useSSL)
142 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
143 TRACE("using SSL connection\n");
144 if (OpenSSL_ssl_handle) /* already initialized everything */
145 return TRUE;
146 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
147 if (!OpenSSL_ssl_handle)
149 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
150 SONAME_LIBSSL);
151 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
152 return FALSE;
154 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
155 if (!OpenSSL_crypto_handle)
157 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
158 SONAME_LIBCRYPTO);
159 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
160 return FALSE;
163 /* mmm nice ugly macroness */
164 #define DYNSSL(x) \
165 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
166 if (!p##x) \
168 ERR("failed to load symbol %s\n", #x); \
169 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
170 return FALSE; \
173 DYNSSL(SSL_library_init);
174 DYNSSL(SSL_load_error_strings);
175 DYNSSL(SSLv23_method);
176 DYNSSL(SSL_CTX_new);
177 DYNSSL(SSL_new);
178 DYNSSL(SSL_free);
179 DYNSSL(SSL_set_fd);
180 DYNSSL(SSL_connect);
181 DYNSSL(SSL_shutdown);
182 DYNSSL(SSL_write);
183 DYNSSL(SSL_read);
184 DYNSSL(SSL_get_verify_result);
185 DYNSSL(SSL_get_peer_certificate);
186 DYNSSL(SSL_CTX_get_timeout);
187 DYNSSL(SSL_CTX_set_timeout);
188 DYNSSL(SSL_CTX_set_default_verify_paths);
189 DYNSSL(i2d_X509);
190 #undef DYNSSL
192 #define DYNCRYPTO(x) \
193 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
194 if (!p##x) \
196 ERR("failed to load symbol %s\n", #x); \
197 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
198 return FALSE; \
200 DYNCRYPTO(BIO_new_fp);
201 DYNCRYPTO(ERR_get_error);
202 DYNCRYPTO(ERR_error_string);
203 #undef DYNCRYPTO
205 pSSL_library_init();
206 pSSL_load_error_strings();
207 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
209 meth = pSSLv23_method();
210 connection->peek_msg = NULL;
211 connection->peek_msg_mem = NULL;
212 #else
213 FIXME("can't use SSL, not compiled in.\n");
214 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
215 return FALSE;
216 #endif
218 return TRUE;
221 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
223 if (connection->socketFD == -1)
224 return FALSE;
225 else
226 return TRUE;
229 /* translate a unix error code into a winsock one */
230 static int sock_get_error( int err )
232 #if !defined(__MINGW32__) && !defined (_MSC_VER)
233 switch (err)
235 case EINTR: return WSAEINTR;
236 case EBADF: return WSAEBADF;
237 case EPERM:
238 case EACCES: return WSAEACCES;
239 case EFAULT: return WSAEFAULT;
240 case EINVAL: return WSAEINVAL;
241 case EMFILE: return WSAEMFILE;
242 case EWOULDBLOCK: return WSAEWOULDBLOCK;
243 case EINPROGRESS: return WSAEINPROGRESS;
244 case EALREADY: return WSAEALREADY;
245 case ENOTSOCK: return WSAENOTSOCK;
246 case EDESTADDRREQ: return WSAEDESTADDRREQ;
247 case EMSGSIZE: return WSAEMSGSIZE;
248 case EPROTOTYPE: return WSAEPROTOTYPE;
249 case ENOPROTOOPT: return WSAENOPROTOOPT;
250 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
251 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
252 case EOPNOTSUPP: return WSAEOPNOTSUPP;
253 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
254 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
255 case EADDRINUSE: return WSAEADDRINUSE;
256 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
257 case ENETDOWN: return WSAENETDOWN;
258 case ENETUNREACH: return WSAENETUNREACH;
259 case ENETRESET: return WSAENETRESET;
260 case ECONNABORTED: return WSAECONNABORTED;
261 case EPIPE:
262 case ECONNRESET: return WSAECONNRESET;
263 case ENOBUFS: return WSAENOBUFS;
264 case EISCONN: return WSAEISCONN;
265 case ENOTCONN: return WSAENOTCONN;
266 case ESHUTDOWN: return WSAESHUTDOWN;
267 case ETOOMANYREFS: return WSAETOOMANYREFS;
268 case ETIMEDOUT: return WSAETIMEDOUT;
269 case ECONNREFUSED: return WSAECONNREFUSED;
270 case ELOOP: return WSAELOOP;
271 case ENAMETOOLONG: return WSAENAMETOOLONG;
272 case EHOSTDOWN: return WSAEHOSTDOWN;
273 case EHOSTUNREACH: return WSAEHOSTUNREACH;
274 case ENOTEMPTY: return WSAENOTEMPTY;
275 #ifdef EPROCLIM
276 case EPROCLIM: return WSAEPROCLIM;
277 #endif
278 #ifdef EUSERS
279 case EUSERS: return WSAEUSERS;
280 #endif
281 #ifdef EDQUOT
282 case EDQUOT: return WSAEDQUOT;
283 #endif
284 #ifdef ESTALE
285 case ESTALE: return WSAESTALE;
286 #endif
287 #ifdef EREMOTE
288 case EREMOTE: return WSAEREMOTE;
289 #endif
290 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
292 #endif
293 return err;
296 /******************************************************************************
297 * NETCON_create
298 * Basically calls 'socket()'
300 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
301 int type, int protocol)
303 #ifdef SONAME_LIBSSL
304 if (connection->useSSL)
305 return FALSE;
306 #endif
308 connection->socketFD = socket(domain, type, protocol);
309 if (connection->socketFD == -1)
311 INTERNET_SetLastError(sock_get_error(errno));
312 return FALSE;
314 return TRUE;
317 /******************************************************************************
318 * NETCON_close
319 * Basically calls 'close()' unless we should use SSL
321 BOOL NETCON_close(WININET_NETCONNECTION *connection)
323 int result;
325 if (!NETCON_connected(connection)) return FALSE;
327 #ifdef SONAME_LIBSSL
328 if (connection->useSSL)
330 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
331 connection->peek_msg = NULL;
332 connection->peek_msg_mem = NULL;
333 connection->peek_len = 0;
335 pSSL_shutdown(connection->ssl_s);
336 pSSL_free(connection->ssl_s);
337 connection->ssl_s = NULL;
339 connection->useSSL = FALSE;
341 #endif
343 result = closesocket(connection->socketFD);
344 connection->socketFD = -1;
346 if (result == -1)
348 INTERNET_SetLastError(sock_get_error(errno));
349 return FALSE;
351 return TRUE;
353 #ifdef SONAME_LIBSSL
354 static BOOL check_hostname(X509 *cert, char *hostname)
356 /* FIXME: implement */
357 return TRUE;
359 #endif
360 /******************************************************************************
361 * NETCON_secure_connect
362 * Initiates a secure connection over an existing plaintext connection.
364 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
366 #ifdef SONAME_LIBSSL
367 long verify_res;
368 X509 *cert;
369 int len;
370 char *hostname_unix;
372 /* can't connect if we are already connected */
373 if (connection->useSSL)
375 ERR("already connected\n");
376 return FALSE;
379 ctx = pSSL_CTX_new(meth);
380 if (!pSSL_CTX_set_default_verify_paths(ctx))
382 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
383 pERR_error_string(pERR_get_error(), 0));
384 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
385 return FALSE;
387 connection->ssl_s = pSSL_new(ctx);
388 if (!connection->ssl_s)
390 ERR("SSL_new failed: %s\n",
391 pERR_error_string(pERR_get_error(), 0));
392 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
393 goto fail;
396 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
398 ERR("SSL_set_fd failed: %s\n",
399 pERR_error_string(pERR_get_error(), 0));
400 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
401 goto fail;
404 if (pSSL_connect(connection->ssl_s) <= 0)
406 ERR("SSL_connect failed: %s\n",
407 pERR_error_string(pERR_get_error(), 0));
408 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
409 goto fail;
411 cert = pSSL_get_peer_certificate(connection->ssl_s);
412 if (!cert)
414 ERR("no certificate for server %s\n", debugstr_w(hostname));
415 /* FIXME: is this the best error? */
416 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
417 goto fail;
419 verify_res = pSSL_get_verify_result(connection->ssl_s);
420 if (verify_res != X509_V_OK)
422 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
423 /* FIXME: we should set an error and return, but we only warn at
424 * the moment */
427 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
428 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
429 if (!hostname_unix)
431 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
432 goto fail;
434 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
436 if (!check_hostname(cert, hostname_unix))
438 HeapFree(GetProcessHeap(), 0, hostname_unix);
439 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
440 goto fail;
443 HeapFree(GetProcessHeap(), 0, hostname_unix);
444 connection->useSSL = TRUE;
445 return TRUE;
447 fail:
448 if (connection->ssl_s)
450 pSSL_shutdown(connection->ssl_s);
451 pSSL_free(connection->ssl_s);
452 connection->ssl_s = NULL;
454 #endif
455 return FALSE;
458 /******************************************************************************
459 * NETCON_connect
460 * Connects to the specified address.
462 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
463 unsigned int addrlen)
465 int result;
467 if (!NETCON_connected(connection)) return FALSE;
469 result = connect(connection->socketFD, serv_addr, addrlen);
470 if (result == -1)
472 WARN("Unable to connect to host (%s)\n", strerror(errno));
473 INTERNET_SetLastError(sock_get_error(errno));
475 closesocket(connection->socketFD);
476 connection->socketFD = -1;
477 return FALSE;
480 return TRUE;
483 /******************************************************************************
484 * NETCON_send
485 * Basically calls 'send()' unless we should use SSL
486 * number of chars send is put in *sent
488 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
489 int *sent /* out */)
491 if (!NETCON_connected(connection)) return FALSE;
492 if (!connection->useSSL)
494 *sent = send(connection->socketFD, msg, len, flags);
495 if (*sent == -1)
497 INTERNET_SetLastError(sock_get_error(errno));
498 return FALSE;
500 return TRUE;
502 else
504 #ifdef SONAME_LIBSSL
505 if (flags)
506 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
507 *sent = pSSL_write(connection->ssl_s, msg, len);
508 if (*sent < 1 && len)
509 return FALSE;
510 return TRUE;
511 #else
512 return FALSE;
513 #endif
517 /******************************************************************************
518 * NETCON_recv
519 * Basically calls 'recv()' unless we should use SSL
520 * number of chars received is put in *recvd
522 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
523 int *recvd /* out */)
525 *recvd = 0;
526 if (!NETCON_connected(connection)) return FALSE;
527 if (!len)
528 return TRUE;
529 if (!connection->useSSL)
531 *recvd = recv(connection->socketFD, buf, len, flags);
532 if (*recvd == -1)
534 INTERNET_SetLastError(sock_get_error(errno));
535 return FALSE;
537 return TRUE;
539 else
541 #ifdef SONAME_LIBSSL
542 if (flags & ~(MSG_PEEK|MSG_WAITALL))
543 FIXME("SSL_read does not support the following flag: %08x\n", flags);
545 /* this ugly hack is all for MSG_PEEK. eww gross */
546 if (flags & MSG_PEEK && !connection->peek_msg)
548 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
550 else if (flags & MSG_PEEK && connection->peek_msg)
552 if (len < connection->peek_len)
553 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
554 *recvd = min(len, connection->peek_len);
555 memcpy(buf, connection->peek_msg, *recvd);
556 return TRUE;
558 else if (connection->peek_msg)
560 *recvd = min(len, connection->peek_len);
561 memcpy(buf, connection->peek_msg, *recvd);
562 connection->peek_len -= *recvd;
563 connection->peek_msg += *recvd;
564 if (connection->peek_len == 0)
566 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
567 connection->peek_msg_mem = NULL;
568 connection->peek_msg = NULL;
570 /* check if we got enough data from the peek buffer */
571 if (!(flags & MSG_WAITALL) || (*recvd == len))
572 return TRUE;
573 /* otherwise, fall through */
575 *recvd += pSSL_read(connection->ssl_s, (char*)buf + *recvd, len - *recvd);
576 if (flags & MSG_PEEK) /* must copy stuff into buffer */
578 connection->peek_len = *recvd;
579 if (!*recvd)
581 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
582 connection->peek_msg_mem = NULL;
583 connection->peek_msg = NULL;
585 else
586 memcpy(connection->peek_msg, buf, *recvd);
588 if (*recvd < 1 && len)
589 return FALSE;
590 return TRUE;
591 #else
592 return FALSE;
593 #endif
597 /******************************************************************************
598 * NETCON_query_data_available
599 * Returns the number of bytes of peeked data plus the number of bytes of
600 * queued, but unread data.
602 BOOL NETCON_query_data_available(WININET_NETCONNECTION *connection, DWORD *available)
604 *available = 0;
605 if (!NETCON_connected(connection))
606 return FALSE;
608 #ifdef SONAME_LIBSSL
609 if (connection->peek_msg) *available = connection->peek_len;
610 #endif
612 #ifdef FIONREAD
613 if (!connection->useSSL)
615 int unread;
616 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
617 if (!retval)
619 TRACE("%d bytes of queued, but unread data\n", unread);
620 *available += unread;
623 #endif
624 return TRUE;
627 /******************************************************************************
628 * NETCON_getNextLine
630 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
633 TRACE("\n");
635 if (!NETCON_connected(connection)) return FALSE;
637 if (!connection->useSSL)
639 struct pollfd pfd;
640 DWORD nRecv = 0;
641 int ret;
643 pfd.fd = connection->socketFD;
644 pfd.events = POLLIN;
646 while (nRecv < *dwBuffer)
648 if (poll(&pfd,1, RESPONSE_TIMEOUT * 1000) > 0)
650 if ((ret = recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0)) <= 0)
652 if (ret == -1) INTERNET_SetLastError(sock_get_error(errno));
653 break;
656 if (lpszBuffer[nRecv] == '\n')
658 lpszBuffer[nRecv++] = '\0';
659 *dwBuffer = nRecv;
660 TRACE(":%u %s\n", nRecv, lpszBuffer);
661 return TRUE;
663 if (lpszBuffer[nRecv] != '\r')
664 nRecv++;
666 else
668 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
669 break;
673 else
675 #ifdef SONAME_LIBSSL
676 long prev_timeout;
677 DWORD nRecv = 0;
678 BOOL success = TRUE;
680 prev_timeout = pSSL_CTX_get_timeout(ctx);
681 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
683 while (nRecv < *dwBuffer)
685 int recv = 1;
686 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
688 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
689 success = FALSE;
692 if (lpszBuffer[nRecv] == '\n')
694 success = TRUE;
695 break;
697 if (lpszBuffer[nRecv] != '\r')
698 nRecv++;
701 pSSL_CTX_set_timeout(ctx, prev_timeout);
702 if (success)
704 lpszBuffer[nRecv++] = '\0';
705 *dwBuffer = nRecv;
706 TRACE("_SSL:%u %s\n", nRecv, lpszBuffer);
707 return TRUE;
709 #endif
711 return FALSE;
715 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
717 #ifdef SONAME_LIBSSL
718 X509* cert;
719 unsigned char* buffer,*p;
720 INT len;
721 BOOL malloced = FALSE;
722 LPCVOID r = NULL;
724 if (!connection->useSSL)
725 return NULL;
727 cert = pSSL_get_peer_certificate(connection->ssl_s);
728 p = NULL;
729 len = pi2d_X509(cert,&p);
731 * SSL 0.9.7 and above malloc the buffer if it is null.
732 * however earlier version do not and so we would need to alloc the buffer.
734 * see the i2d_X509 man page for more details.
736 if (!p)
738 buffer = HeapAlloc(GetProcessHeap(),0,len);
739 p = buffer;
740 len = pi2d_X509(cert,&p);
742 else
744 buffer = p;
745 malloced = TRUE;
748 r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
750 if (malloced)
751 free(buffer);
752 else
753 HeapFree(GetProcessHeap(),0,buffer);
755 return r;
756 #else
757 return NULL;
758 #endif
761 DWORD NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
763 int result;
764 struct timeval tv;
766 /* FIXME: we should probably store the timeout in the connection to set
767 * when we do connect */
768 if (!NETCON_connected(connection))
769 return ERROR_SUCCESS;
771 /* value is in milliseconds, convert to struct timeval */
772 tv.tv_sec = value / 1000;
773 tv.tv_usec = (value % 1000) * 1000;
775 result = setsockopt(connection->socketFD, SOL_SOCKET,
776 send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
777 sizeof(tv));
779 if (result == -1)
781 WARN("setsockopt failed (%s)\n", strerror(errno));
782 return sock_get_error(errno);
785 return ERROR_SUCCESS;