2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
22 #include "wine/library.h"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
32 #ifdef HAVE_SYS_IOCTL_H
33 # include <sys/ioctl.h>
35 #ifdef HAVE_SYS_FILIO_H
36 # include <sys/filio.h>
41 #ifdef HAVE_OPENSSL_SSL_H
42 # include <openssl/ssl.h>
51 /* to avoid conflicts with the Unix socket headers */
55 #include "winhttp_private.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
59 #define DEFAULT_SEND_TIMEOUT 30
60 #define DEFAULT_RECEIVE_TIMEOUT 30
62 #ifndef HAVE_GETADDRINFO
64 /* critical section to protect non-reentrant gethostbyname() */
65 static CRITICAL_SECTION cs_gethostbyname
;
66 static CRITICAL_SECTION_DEBUG critsect_debug
=
68 0, 0, &cs_gethostbyname
,
69 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
70 0, 0, { (DWORD_PTR
)(__FILE__
": cs_gethostbyname") }
72 static CRITICAL_SECTION cs_gethostbyname
= { &critsect_debug
, -1, 0, 0, 0, 0 };
78 #include <openssl/err.h>
80 static void *libssl_handle
;
81 static void *libcrypto_handle
;
83 static SSL_METHOD
*method
;
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
87 MAKE_FUNCPTR( SSL_library_init
);
88 MAKE_FUNCPTR( SSL_load_error_strings
);
89 MAKE_FUNCPTR( SSLv23_method
);
90 MAKE_FUNCPTR( SSL_CTX_new
);
91 MAKE_FUNCPTR( SSL_CTX_free
);
92 MAKE_FUNCPTR( SSL_new
);
93 MAKE_FUNCPTR( SSL_free
);
94 MAKE_FUNCPTR( SSL_set_fd
);
95 MAKE_FUNCPTR( SSL_connect
);
96 MAKE_FUNCPTR( SSL_shutdown
);
97 MAKE_FUNCPTR( SSL_write
);
98 MAKE_FUNCPTR( SSL_read
);
99 MAKE_FUNCPTR( SSL_get_verify_result
);
100 MAKE_FUNCPTR( SSL_get_peer_certificate
);
101 MAKE_FUNCPTR( SSL_CTX_get_timeout
);
102 MAKE_FUNCPTR( SSL_CTX_set_timeout
);
103 MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths
);
105 MAKE_FUNCPTR( BIO_new_fp
);
106 MAKE_FUNCPTR( ERR_get_error
);
107 MAKE_FUNCPTR( ERR_error_string
);
112 /* translate a unix error code into a winsock error code */
113 static int sock_get_error( int err
)
117 case EINTR
: return WSAEINTR
;
118 case EBADF
: return WSAEBADF
;
120 case EACCES
: return WSAEACCES
;
121 case EFAULT
: return WSAEFAULT
;
122 case EINVAL
: return WSAEINVAL
;
123 case EMFILE
: return WSAEMFILE
;
124 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
125 case EINPROGRESS
: return WSAEINPROGRESS
;
126 case EALREADY
: return WSAEALREADY
;
127 case ENOTSOCK
: return WSAENOTSOCK
;
128 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
129 case EMSGSIZE
: return WSAEMSGSIZE
;
130 case EPROTOTYPE
: return WSAEPROTOTYPE
;
131 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
132 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
133 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
134 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
135 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
136 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
137 case EADDRINUSE
: return WSAEADDRINUSE
;
138 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
139 case ENETDOWN
: return WSAENETDOWN
;
140 case ENETUNREACH
: return WSAENETUNREACH
;
141 case ENETRESET
: return WSAENETRESET
;
142 case ECONNABORTED
: return WSAECONNABORTED
;
144 case ECONNRESET
: return WSAECONNRESET
;
145 case ENOBUFS
: return WSAENOBUFS
;
146 case EISCONN
: return WSAEISCONN
;
147 case ENOTCONN
: return WSAENOTCONN
;
148 case ESHUTDOWN
: return WSAESHUTDOWN
;
149 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
150 case ETIMEDOUT
: return WSAETIMEDOUT
;
151 case ECONNREFUSED
: return WSAECONNREFUSED
;
152 case ELOOP
: return WSAELOOP
;
153 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
154 case EHOSTDOWN
: return WSAEHOSTDOWN
;
155 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
156 case ENOTEMPTY
: return WSAENOTEMPTY
;
158 case EPROCLIM
: return WSAEPROCLIM
;
161 case EUSERS
: return WSAEUSERS
;
164 case EDQUOT
: return WSAEDQUOT
;
167 case ESTALE
: return WSAESTALE
;
170 case EREMOTE
: return WSAEREMOTE
;
172 default: errno
= err
; perror( "sock_set_error" ); return WSAEFAULT
;
177 BOOL
netconn_init( netconn_t
*conn
, BOOL secure
)
180 if (!secure
) return TRUE
;
182 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
183 if (libssl_handle
) return TRUE
;
184 if (!(libssl_handle
= wine_dlopen( SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0 )))
186 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL
);
187 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
190 if (!(libcrypto_handle
= wine_dlopen( SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0 )))
192 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO
);
193 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
196 #define LOAD_FUNCPTR(x) \
197 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
199 ERR("Failed to load symbol %s\n", #x); \
200 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
203 LOAD_FUNCPTR( SSL_library_init
);
204 LOAD_FUNCPTR( SSL_load_error_strings
);
205 LOAD_FUNCPTR( SSLv23_method
);
206 LOAD_FUNCPTR( SSL_CTX_new
);
207 LOAD_FUNCPTR( SSL_CTX_free
);
208 LOAD_FUNCPTR( SSL_new
);
209 LOAD_FUNCPTR( SSL_free
);
210 LOAD_FUNCPTR( SSL_set_fd
);
211 LOAD_FUNCPTR( SSL_connect
);
212 LOAD_FUNCPTR( SSL_shutdown
);
213 LOAD_FUNCPTR( SSL_write
);
214 LOAD_FUNCPTR( SSL_read
);
215 LOAD_FUNCPTR( SSL_get_verify_result
);
216 LOAD_FUNCPTR( SSL_get_peer_certificate
);
217 LOAD_FUNCPTR( SSL_CTX_get_timeout
);
218 LOAD_FUNCPTR( SSL_CTX_set_timeout
);
219 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths
);
222 #define LOAD_FUNCPTR(x) \
223 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
225 ERR("Failed to load symbol %s\n", #x); \
226 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
229 LOAD_FUNCPTR( BIO_new_fp
);
230 LOAD_FUNCPTR( ERR_get_error
);
231 LOAD_FUNCPTR( ERR_error_string
);
235 pSSL_load_error_strings();
236 pBIO_new_fp( stderr
, BIO_NOCLOSE
);
238 method
= pSSLv23_method();
239 conn
->ssl_ctx
= pSSL_CTX_new( method
);
242 WARN("SSL support not compiled in.\n");
243 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
249 BOOL
netconn_connected( netconn_t
*conn
)
251 return (conn
->socket
!= -1);
254 BOOL
netconn_create( netconn_t
*conn
, int domain
, int type
, int protocol
)
256 if ((conn
->socket
= socket( domain
, type
, protocol
)) == -1)
258 WARN("unable to create socket (%s)\n", strerror(errno
));
259 set_last_error( sock_get_error( errno
) );
265 BOOL
netconn_close( netconn_t
*conn
)
272 heap_free( conn
->peek_msg_mem
);
273 conn
->peek_msg_mem
= NULL
;
274 conn
->peek_msg
= NULL
;
277 pSSL_shutdown( conn
->ssl_conn
);
278 pSSL_free( conn
->ssl_conn
);
279 pSSL_CTX_free( conn
->ssl_ctx
);
281 conn
->ssl_conn
= NULL
;
282 conn
->ssl_ctx
= NULL
;
283 conn
->secure
= FALSE
;
286 res
= close( conn
->socket
);
290 set_last_error( sock_get_error( errno
) );
296 BOOL
netconn_connect( netconn_t
*conn
, const struct sockaddr
*sockaddr
, unsigned int addr_len
)
298 if (connect( conn
->socket
, sockaddr
, addr_len
) == -1)
300 WARN("unable to connect to host (%s)\n", strerror(errno
));
301 set_last_error( sock_get_error( errno
) );
307 BOOL
netconn_secure_connect( netconn_t
*conn
)
313 if (!pSSL_CTX_set_default_verify_paths( conn
->ssl_ctx
))
315 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
316 set_last_error( ERROR_OUTOFMEMORY
);
319 if (!(conn
->ssl_conn
= pSSL_new( conn
->ssl_ctx
)))
321 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
322 set_last_error( ERROR_OUTOFMEMORY
);
325 if (!pSSL_set_fd( conn
->ssl_conn
, conn
->socket
))
327 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
328 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
331 if (pSSL_connect( conn
->ssl_conn
) <= 0)
333 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
334 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
337 if (!(cert
= pSSL_get_peer_certificate( conn
->ssl_conn
)))
339 ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
340 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR
);
343 if ((res
= pSSL_get_verify_result( conn
->ssl_conn
)) != X509_V_OK
)
345 /* FIXME: we should set an error and return, but we only print an error at the moment */
346 ERR("couldn't verify server certificate (%ld)\n", res
);
348 TRACE("established SSL connection\n");
355 pSSL_shutdown( conn
->ssl_conn
);
356 pSSL_free( conn
->ssl_conn
);
357 conn
->ssl_conn
= NULL
;
363 BOOL
netconn_send( netconn_t
*conn
, const void *msg
, size_t len
, int flags
, int *sent
)
365 if (!netconn_connected( conn
)) return FALSE
;
369 if (flags
) FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
370 *sent
= pSSL_write( conn
->ssl_conn
, msg
, len
);
371 if (*sent
< 1 && len
) return FALSE
;
377 if ((*sent
= send( conn
->socket
, msg
, len
, flags
)) == -1)
379 set_last_error( sock_get_error( errno
) );
385 BOOL
netconn_recv( netconn_t
*conn
, void *buf
, size_t len
, int flags
, int *recvd
)
388 if (!netconn_connected( conn
)) return FALSE
;
389 if (!len
) return TRUE
;
394 if (flags
& ~(MSG_PEEK
| MSG_WAITALL
))
395 FIXME("SSL_read does not support the following flags: %08x\n", flags
);
397 /* this ugly hack is all for MSG_PEEK */
398 if (flags
& MSG_PEEK
&& !conn
->peek_msg
)
400 if (!(conn
->peek_msg
= conn
->peek_msg_mem
= heap_alloc( len
+ 1 ))) return FALSE
;
402 else if (flags
& MSG_PEEK
&& conn
->peek_msg
)
404 if (len
< conn
->peek_len
) FIXME("buffer isn't big enough, should we wrap?\n");
405 *recvd
= min( len
, conn
->peek_len
);
406 memcpy( buf
, conn
->peek_msg
, *recvd
);
409 else if (conn
->peek_msg
)
411 *recvd
= min( len
, conn
->peek_len
);
412 memcpy( buf
, conn
->peek_msg
, *recvd
);
413 conn
->peek_len
-= *recvd
;
414 conn
->peek_msg
+= *recvd
;
416 if (conn
->peek_len
== 0)
418 heap_free( conn
->peek_msg_mem
);
419 conn
->peek_msg_mem
= NULL
;
420 conn
->peek_msg
= NULL
;
422 /* check if we have enough data from the peek buffer */
423 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
)) return TRUE
;
425 *recvd
+= pSSL_read( conn
->ssl_conn
, (char *)buf
+ *recvd
, len
- *recvd
);
426 if (flags
& MSG_PEEK
) /* must copy into buffer */
428 conn
->peek_len
= *recvd
;
431 heap_free( conn
->peek_msg_mem
);
432 conn
->peek_msg_mem
= NULL
;
433 conn
->peek_msg
= NULL
;
435 else memcpy( conn
->peek_msg
, buf
, *recvd
);
437 if (*recvd
< 1 && len
) return FALSE
;
443 if ((*recvd
= recv( conn
->socket
, buf
, len
, flags
)) == -1)
445 set_last_error( sock_get_error( errno
) );
451 BOOL
netconn_query_data_available( netconn_t
*conn
, DWORD
*available
)
457 if (!netconn_connected( conn
)) return FALSE
;
462 if (conn
->peek_msg
) *available
= conn
->peek_len
;
467 if (!(ret
= ioctl( conn
->socket
, FIONREAD
, &unread
)))
469 TRACE("%d bytes of queued, but unread data\n", unread
);
470 *available
+= unread
;
476 BOOL
netconn_get_next_line( netconn_t
*conn
, char *buffer
, DWORD
*buflen
)
482 if (!netconn_connected( conn
)) return FALSE
;
489 timeout
= pSSL_CTX_get_timeout( conn
->ssl_ctx
);
490 pSSL_CTX_set_timeout( conn
->ssl_ctx
, DEFAULT_RECEIVE_TIMEOUT
);
492 while (recvd
< *buflen
)
495 if (!netconn_recv( conn
, &buffer
[recvd
], 1, 0, &dummy
))
497 set_last_error( ERROR_CONNECTION_ABORTED
);
500 if (buffer
[recvd
] == '\n')
505 if (buffer
[recvd
] != '\r') recvd
++;
507 pSSL_CTX_set_timeout( conn
->ssl_ctx
, timeout
);
512 TRACE("received line %s\n", debugstr_a(buffer
));
520 pfd
.fd
= conn
->socket
;
522 while (recvd
< *buflen
)
524 if (poll( &pfd
, 1, DEFAULT_RECEIVE_TIMEOUT
* 1000 ) > 0)
526 if (recv( conn
->socket
, &buffer
[recvd
], 1, 0 ) <= 0)
528 set_last_error( sock_get_error( errno
) );
531 if (buffer
[recvd
] == '\n')
536 if (buffer
[recvd
] != '\r') recvd
++;
540 set_last_error( ERROR_WINHTTP_TIMEOUT
);
548 TRACE("received line %s\n", debugstr_a(buffer
));
553 DWORD
netconn_set_timeout( netconn_t
*netconn
, BOOL send
, int value
)
558 /* value is in milliseconds, convert to struct timeval */
559 tv
.tv_sec
= value
/ 1000;
560 tv
.tv_usec
= (value
% 1000) * 1000;
562 if ((res
= setsockopt( netconn
->socket
, SOL_SOCKET
, send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
, sizeof(tv
) ) == -1))
564 WARN("setsockopt failed (%s)\n", strerror( errno
));
565 return sock_get_error( errno
);
567 return ERROR_SUCCESS
;
570 BOOL
netconn_resolve( WCHAR
*hostnameW
, INTERNET_PORT port
, struct sockaddr_in
*sa
)
573 #ifdef HAVE_GETADDRINFO
574 struct addrinfo
*res
, hints
;
580 if (!(hostname
= strdupWA( hostnameW
))) return FALSE
;
582 #ifdef HAVE_GETADDRINFO
583 memset( &hints
, 0, sizeof(struct addrinfo
) );
584 hints
.ai_family
= AF_INET
;
586 ret
= getaddrinfo( hostname
, NULL
, &hints
, &res
);
587 heap_free( hostname
);
590 TRACE("failed to get address of %s (%s)\n", debugstr_a(hostname
), gai_strerror(ret
));
593 memset( sa
, 0, sizeof(struct sockaddr_in
) );
594 memcpy( &sa
->sin_addr
, &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
, sizeof(struct in_addr
) );
595 sa
->sin_family
= res
->ai_family
;
596 sa
->sin_port
= htons( port
);
600 EnterCriticalSection( &cs_gethostbyname
);
602 he
= gethostbyname( hostname
);
603 heap_free( hostname
);
606 TRACE("failed to get address of %s (%d)\n", debugstr_a(hostname
), h_errno
);
607 LeaveCriticalSection( &cs_gethostbyname
);
610 memset( sa
, 0, sizeof(struct sockaddr_in
) );
611 memcpy( (char *)&sa
->sin_addr
, he
->h_addr
, he
->h_length
);
612 sa
->sin_family
= he
->h_addrtype
;
613 sa
->sin_port
= htons( port
);
615 LeaveCriticalSection( &cs_gethostbyname
);