2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
42 #include "wine/library.h"
48 #include "wine/debug.h"
51 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
54 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
57 * This should use winsock - To use winsock the funtions will have to change a bit
58 * as they are designed for unix sockets.
59 * SSL stuff should use crypt32.dll
62 #ifdef HAVE_OPENSSL_SSL_H
65 #define SONAME_LIBSSL "libssl.so"
67 #ifndef SONAME_LIBCRYPTO
68 #define SONAME_LIBCRYPTO "libcrypto.so"
71 static void *OpenSSL_ssl_handle
;
72 static void *OpenSSL_crypto_handle
;
74 static SSL_METHOD
*meth
;
77 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
79 /* OpenSSL funtions that we use */
80 MAKE_FUNCPTR(SSL_library_init
);
81 MAKE_FUNCPTR(SSL_load_error_strings
);
82 MAKE_FUNCPTR(SSLv23_method
);
83 MAKE_FUNCPTR(SSL_CTX_new
);
84 MAKE_FUNCPTR(SSL_new
);
85 MAKE_FUNCPTR(SSL_set_bio
);
86 MAKE_FUNCPTR(SSL_connect
);
87 MAKE_FUNCPTR(SSL_write
);
88 MAKE_FUNCPTR(SSL_read
);
89 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
90 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
92 /* OpenSSL's libcrypto functions that we use */
93 MAKE_FUNCPTR(BIO_new_socket
);
94 MAKE_FUNCPTR(BIO_new_fp
);
99 void NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
101 connection
->useSSL
= useSSL
;
102 connection
->socketFD
= -1;
103 if (connection
->useSSL
)
105 #ifdef HAVE_OPENSSL_SSL_H
106 TRACE("using SSL connection\n");
107 connection
->ssl_sock
= -1;
108 if (OpenSSL_ssl_handle
) /* already initilzed everything */
110 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
111 if (!OpenSSL_ssl_handle
)
113 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
115 connection
->useSSL
= FALSE
;
118 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
119 if (!OpenSSL_crypto_handle
)
121 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
123 connection
->useSSL
= FALSE
;
127 /* mmm nice ugly macroness */
129 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
132 ERR("failed to load symbol %s\n", #x); \
133 connection->useSSL = FALSE; \
137 DYNSSL(SSL_library_init
);
138 DYNSSL(SSL_load_error_strings
);
139 DYNSSL(SSLv23_method
);
146 DYNSSL(SSL_CTX_get_timeout
);
147 DYNSSL(SSL_CTX_set_timeout
);
150 #define DYNCRYPTO(x) \
151 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
154 ERR("failed to load symbol %s\n", #x); \
155 connection->useSSL = FALSE; \
158 DYNCRYPTO(BIO_new_fp
);
159 DYNCRYPTO(BIO_new_socket
);
163 pSSL_load_error_strings();
164 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
166 meth
= pSSLv23_method();
167 /* FIXME: SECURITY PROBLEM! WE ARN'T VERIFYING THE HOSTS CERTIFICATES OR ANYTHING */
169 FIXME("can't use SSL, not compiled in.\n");
170 connection
->useSSL
= FALSE
;
175 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
177 if (!connection
->useSSL
)
179 if (connection
->socketFD
== -1)
185 #ifdef HAVE_OPENSSL_SSL_H
186 if (connection
->ssl_sock
== -1)
195 /******************************************************************************
197 * Basically calls 'socket()' unless useSSL is supplised,
198 * in which case we do other things.
200 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
201 int type
, int protocol
)
203 if (!connection
->useSSL
)
205 connection
->socketFD
= socket(domain
, type
, protocol
);
206 if (connection
->socketFD
== -1)
212 #ifdef HAVE_OPENSSL_SSL_H
213 connection
->ssl_sock
= socket(domain
, type
, protocol
);
221 /******************************************************************************
223 * Basically calls 'close()' unless we should use SSL
225 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
227 if (!NETCON_connected
) return FALSE
;
228 if (!connection
->useSSL
)
231 result
= close(connection
->socketFD
);
232 connection
->socketFD
= -1;
239 #ifdef HAVE_OPENSSL_SSL_H
240 close(connection
->ssl_sock
);
241 connection
->ssl_sock
= -1;
242 /* FIXME should we call SSL_shutdown here?? Probably on whatever is the
243 * opposite of NETCON_init.... */
251 /******************************************************************************
253 * Basically calls 'connect()' unless we should use SSL
255 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
258 if (!NETCON_connected
) return FALSE
;
259 if (!connection
->useSSL
)
262 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
269 #ifdef HAVE_OPENSSL_SSL_H
272 ctx
= pSSL_CTX_new(meth
);
273 connection
->ssl_s
= pSSL_new(ctx
);
275 if (connect(connection
->ssl_sock
, serv_addr
, addrlen
) == -1)
278 sbio
= pBIO_new_socket(connection
->ssl_sock
, BIO_NOCLOSE
);
279 pSSL_set_bio(connection
->ssl_s
, sbio
, sbio
);
280 if (pSSL_connect(connection
->ssl_s
) <= 0)
282 ERR("ssl couldn't connect\n");
292 /******************************************************************************
294 * Basically calls 'send()' unless we should use SSL
295 * number of chars send is put in *sent
297 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
300 if (!NETCON_connected
) return FALSE
;
301 if (!connection
->useSSL
)
303 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
310 #ifdef HAVE_OPENSSL_SSL_H
312 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
313 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
314 if (*sent
< 1 && len
)
323 /******************************************************************************
325 * Basically calls 'recv()' unless we should use SSL
326 * number of chars receieved is put in *recvd
328 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
329 int *recvd
/* out */)
331 if (!NETCON_connected
) return FALSE
;
332 if (!connection
->useSSL
)
334 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
341 #ifdef HAVE_OPENSSL_SSL_H
342 static char *peek_msg
= NULL
;
343 static char *peek_msg_mem
= NULL
;
345 if (flags
& (~MSG_PEEK
))
346 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
348 /* this ugly hack is all for MSG_PEEK. eww gross */
349 if (flags
& MSG_PEEK
&& !peek_msg
)
351 peek_msg
= peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
353 else if (flags
& MSG_PEEK
&& peek_msg
)
355 if (len
< strlen(peek_msg
))
356 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
357 strncpy(buf
, peek_msg
, len
);
358 *recvd
= (strlen(peek_msg
) <= len
? strlen(peek_msg
) : len
);
363 strncpy(buf
, peek_msg
, len
);
364 peek_msg
+= *recvd
= min(len
, strlen(peek_msg
));
365 if (*peek_msg
== '\0' || *(peek_msg
- 1) == '\0')
367 HeapFree(GetProcessHeap(), 0, peek_msg_mem
);
373 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
374 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
378 HeapFree(GetProcessHeap(), 0, peek_msg_mem
);
384 strncpy(peek_msg
, buf
, *recvd
);
385 peek_msg
[*recvd
] = '\0';
388 if (*recvd
< 1 && len
)
397 /******************************************************************************
400 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
405 if (!NETCON_connected(connection
)) return FALSE
;
407 if (!connection
->useSSL
)
411 BOOL bSuccess
= FALSE
;
415 FD_SET(connection
->socketFD
, &infd
);
416 tv
.tv_sec
=RESPONSE_TIMEOUT
;
419 while (nRecv
< *dwBuffer
)
421 if (select(connection
->socketFD
+1,&infd
,NULL
,NULL
,&tv
) > 0)
423 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
425 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
); /* fixme: right error? */
429 if (lpszBuffer
[nRecv
] == '\n')
434 if (lpszBuffer
[nRecv
] != '\r')
439 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
444 lend
: /* FIXME: don't use labels */
447 lpszBuffer
[nRecv
] = '\0';
448 *dwBuffer
= nRecv
- 1;
449 TRACE(":%d %s\n", nRecv
, lpszBuffer
);
459 #ifdef HAVE_OPENSSL_SSL_H
464 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
465 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
467 while (nRecv
< *dwBuffer
)
470 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
472 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
476 if (lpszBuffer
[nRecv
] == '\n')
481 if (lpszBuffer
[nRecv
] != '\r')
485 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
488 lpszBuffer
[nRecv
] = '\0';
489 *dwBuffer
= nRecv
- 1;
490 TRACE("_SSL:%d %s\n", nRecv
, lpszBuffer
);