1 /* 2 * Wininet 3 * 4 * Copyright 1999 Corel Corporation 5 * 6 * Ulrich Czekalla 7 * 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. 12 * 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. 17 * 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 21 */ 22 23 #ifndef _WINE_INTERNET_H_ 24 #define _WINE_INTERNET_H_ 25 26 #include "wine/heap.h" 27 #include "wine/list.h" 28 29 #include <time.h> 30 31 #include "winineti.h" 32 33 extern HMODULE WININET_hModule DECLSPEC_HIDDEN; 34 35 typedef struct { 36 WCHAR *name; 37 INTERNET_PORT port; 38 BOOL is_https; 39 struct sockaddr_storage addr; 40 int addr_len; 41 char addr_str[INET6_ADDRSTRLEN]; 42 43 WCHAR *scheme_host_port; 44 const WCHAR *host_port; 45 const WCHAR *canon_host_port; 46 47 LONG ref; 48 49 DWORD security_flags; 50 const CERT_CHAIN_CONTEXT *cert_chain; 51 52 struct list entry; 53 struct list conn_pool; 54 } server_t; 55 56 void server_addref(server_t*) DECLSPEC_HIDDEN; 57 void server_release(server_t*) DECLSPEC_HIDDEN; 58 59 typedef enum { 60 COLLECT_TIMEOUT, 61 COLLECT_CONNECTIONS, 62 COLLECT_CLEANUP 63 } collect_type_t; 64 BOOL collect_connections(collect_type_t) DECLSPEC_HIDDEN; 65 66 /* used for netconnection.c stuff */ 67 typedef struct 68 { 69 int socket; 70 BOOL secure; 71 BOOL is_blocking; 72 CtxtHandle ssl_ctx; 73 SecPkgContext_StreamSizes ssl_sizes; 74 server_t *server; 75 char *ssl_buf; 76 char *extra_buf; 77 size_t extra_len; 78 char *peek_msg; 79 char *peek_msg_mem; 80 size_t peek_len; 81 DWORD security_flags; 82 BOOL mask_errors; 83 84 BOOL keep_alive; 85 DWORD64 keep_until; 86 struct list pool_entry; 87 } netconn_t; 88 89 BOOL is_valid_netconn(netconn_t *) DECLSPEC_HIDDEN; 90 void close_netconn(netconn_t *) DECLSPEC_HIDDEN; 91 92 static inline void * __WINE_ALLOC_SIZE(2) heap_realloc_zero(void *mem, size_t len) 93 { 94 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len); 95 } 96 97 static inline LPWSTR heap_strdupW(LPCWSTR str) 98 { 99 LPWSTR ret = NULL; 100 101 if(str) { 102 DWORD size; 103 104 size = (lstrlenW(str)+1)*sizeof(WCHAR); 105 ret = heap_alloc(size); 106 if(ret) 107 memcpy(ret, str, size); 108 } 109 110 return ret; 111 } 112 113 static inline char *heap_strdupA(const char *str) 114 { 115 char *ret = NULL; 116 117 if(str) { 118 DWORD size = strlen(str)+1; 119 120 ret = heap_alloc(size); 121 if(ret) 122 memcpy(ret, str, size); 123 } 124 125 return ret; 126 } 127 128 static inline LPWSTR heap_strndupW(LPCWSTR str, UINT max_len) 129 { 130 LPWSTR ret; 131 UINT len; 132 133 if(!str) 134 return NULL; 135 136 for(len=0; len<max_len; len++) 137 if(str[len] == '\0') 138 break; 139 140 ret = heap_alloc(sizeof(WCHAR)*(len+1)); 141 if(ret) { 142 memcpy(ret, str, sizeof(WCHAR)*len); 143 ret[len] = '\0'; 144 } 145 146 return ret; 147 } 148 149 static inline WCHAR *heap_strndupAtoW(const char *str, int len_a, DWORD *len_w) 150 { 151 WCHAR *ret = NULL; 152 153 if(str) { 154 size_t len; 155 if(len_a < 0) len_a = strlen(str); 156 len = MultiByteToWideChar(CP_ACP, 0, str, len_a, NULL, 0); 157 ret = heap_alloc((len+1)*sizeof(WCHAR)); 158 if(ret) { 159 MultiByteToWideChar(CP_ACP, 0, str, len_a, ret, len); 160 ret[len] = 0; 161 *len_w = len; 162 } 163 } 164 165 return ret; 166 } 167 168 static inline WCHAR *heap_strdupAtoW(const char *str) 169 { 170 LPWSTR ret = NULL; 171 172 if(str) { 173 DWORD len; 174 175 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); 176 ret = heap_alloc(len*sizeof(WCHAR)); 177 if(ret) 178 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); 179 } 180 181 return ret; 182 } 183 184 static inline char *heap_strdupWtoA(LPCWSTR str) 185 { 186 char *ret = NULL; 187 188 if(str) { 189 DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL); 190 ret = heap_alloc(size); 191 if(ret) 192 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL); 193 } 194 195 return ret; 196 } 197 198 typedef struct { 199 const WCHAR *str; 200 size_t len; 201 } substr_t; 202 203 static inline substr_t substr(const WCHAR *str, size_t len) 204 { 205 substr_t r = {str, len}; 206 return r; 207 } 208 209 static inline substr_t substrz(const WCHAR *str) 210 { 211 return substr(str, lstrlenW(str)); 212 } 213 214 static inline void WININET_find_data_WtoA(LPWIN32_FIND_DATAW dataW, LPWIN32_FIND_DATAA dataA) 215 { 216 dataA->dwFileAttributes = dataW->dwFileAttributes; 217 dataA->ftCreationTime = dataW->ftCreationTime; 218 dataA->ftLastAccessTime = dataW->ftLastAccessTime; 219 dataA->ftLastWriteTime = dataW->ftLastWriteTime; 220 dataA->nFileSizeHigh = dataW->nFileSizeHigh; 221 dataA->nFileSizeLow = dataW->nFileSizeLow; 222 dataA->dwReserved0 = dataW->dwReserved0; 223 dataA->dwReserved1 = dataW->dwReserved1; 224 WideCharToMultiByte(CP_ACP, 0, dataW->cFileName, -1, 225 dataA->cFileName, sizeof(dataA->cFileName), 226 NULL, NULL); 227 WideCharToMultiByte(CP_ACP, 0, dataW->cAlternateFileName, -1, 228 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName), 229 NULL, NULL); 230 } 231 232 typedef enum 233 { 234 WH_HINIT = INTERNET_HANDLE_TYPE_INTERNET, 235 WH_HFTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_FTP, 236 WH_HGOPHERSESSION = INTERNET_HANDLE_TYPE_CONNECT_GOPHER, 237 WH_HHTTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_HTTP, 238 WH_HFILE = INTERNET_HANDLE_TYPE_FTP_FILE, 239 WH_HFTPFINDNEXT = INTERNET_HANDLE_TYPE_FTP_FIND, 240 WH_HHTTPREQ = INTERNET_HANDLE_TYPE_HTTP_REQUEST, 241 } WH_TYPE; 242 243 #define INET_OPENURL 0x0001 244 #define INET_CALLBACKW 0x0002 245 246 typedef struct 247 { 248 LONG ref; 249 HANDLE file_handle; 250 WCHAR *file_name; 251 WCHAR *url; 252 BOOL is_committed; 253 } req_file_t; 254 255 typedef struct _object_header_t object_header_t; 256 257 typedef struct { 258 void (*Destroy)(object_header_t*); 259 void (*CloseConnection)(object_header_t*); 260 DWORD (*QueryOption)(object_header_t*,DWORD,void*,DWORD*,BOOL); 261 DWORD (*SetOption)(object_header_t*,DWORD,void*,DWORD); 262 DWORD (*ReadFile)(object_header_t*,void*,DWORD,DWORD*,DWORD,DWORD_PTR); 263 DWORD (*WriteFile)(object_header_t*,const void*,DWORD,DWORD*); 264 DWORD (*QueryDataAvailable)(object_header_t*,DWORD*,DWORD,DWORD_PTR); 265 DWORD (*FindNextFileW)(object_header_t*,void*); 266 DWORD (*LockRequestFile)(object_header_t*,req_file_t**); 267 } object_vtbl_t; 268 269 #define INTERNET_HANDLE_IN_USE 1 270 271 struct _object_header_t 272 { 273 WH_TYPE htype; 274 const object_vtbl_t *vtbl; 275 HINTERNET hInternet; 276 BOOL valid_handle; 277 DWORD dwFlags; 278 DWORD_PTR dwContext; 279 DWORD dwError; 280 ULONG ErrorMask; 281 DWORD dwInternalFlags; 282 LONG refs; 283 BOOL decoding; 284 INTERNET_STATUS_CALLBACK lpfnStatusCB; 285 struct list entry; 286 struct list children; 287 }; 288 289 typedef struct 290 { 291 object_header_t hdr; 292 LPWSTR agent; 293 LPWSTR proxy; 294 LPWSTR proxyBypass; 295 LPWSTR proxyUsername; 296 LPWSTR proxyPassword; 297 DWORD accessType; 298 DWORD connect_timeout; 299 } appinfo_t; 300 301 typedef struct 302 { 303 object_header_t hdr; 304 appinfo_t *appInfo; 305 LPWSTR hostName; /* the final destination of the request */ 306 LPWSTR userName; 307 LPWSTR password; 308 INTERNET_PORT hostPort; /* the final destination port of the request */ 309 DWORD connect_timeout; 310 DWORD send_timeout; 311 DWORD receive_timeout; 312 } http_session_t; 313 314 #define HDR_ISREQUEST 0x0001 315 #define HDR_COMMADELIMITED 0x0002 316 #define HDR_SEMIDELIMITED 0x0004 317 318 typedef struct 319 { 320 LPWSTR lpszField; 321 LPWSTR lpszValue; 322 WORD wFlags; 323 WORD wCount; 324 } HTTPHEADERW, *LPHTTPHEADERW; 325 326 327 struct HttpAuthInfo; 328 329 typedef struct data_stream_vtbl_t data_stream_vtbl_t; 330 331 typedef struct { 332 const data_stream_vtbl_t *vtbl; 333 } data_stream_t; 334 335 typedef struct { 336 data_stream_t data_stream; 337 ULONGLONG content_length; 338 ULONGLONG content_read; 339 } netconn_stream_t; 340 341 #define READ_BUFFER_SIZE 8192 342 343 typedef struct 344 { 345 object_header_t hdr; 346 http_session_t *session; 347 server_t *server; 348 server_t *proxy; 349 LPWSTR path; 350 LPWSTR verb; 351 netconn_t *netconn; 352 DWORD security_flags; 353 DWORD connect_timeout; 354 DWORD send_timeout; 355 DWORD receive_timeout; 356 LPWSTR version; 357 DWORD status_code; 358 LPWSTR statusText; 359 DWORD bytesToWrite; 360 DWORD bytesWritten; 361 362 CRITICAL_SECTION headers_section; /* section to protect the headers array */ 363 HTTPHEADERW *custHeaders; 364 DWORD nCustHeaders; 365 366 FILETIME last_modified; 367 HANDLE hCacheFile; 368 req_file_t *req_file; 369 FILETIME expires; 370 struct HttpAuthInfo *authInfo; 371 struct HttpAuthInfo *proxyAuthInfo; 372 373 CRITICAL_SECTION read_section; /* section to protect the following fields */ 374 ULONGLONG contentLength; /* total number of bytes to be read */ 375 BOOL read_gzip; /* are we reading in gzip mode? */ 376 DWORD read_pos; /* current read position in read_buf */ 377 DWORD read_size; /* valid data size in read_buf */ 378 BYTE read_buf[READ_BUFFER_SIZE]; /* buffer for already read but not returned data */ 379 380 data_stream_t *data_stream; 381 netconn_stream_t netconn_stream; 382 } http_request_t; 383 384 typedef struct task_header_t task_header_t; 385 typedef void (*async_task_proc_t)(task_header_t*); 386 387 struct task_header_t 388 { 389 async_task_proc_t proc; 390 object_header_t *hdr; 391 }; 392 393 void *alloc_async_task(object_header_t*,async_task_proc_t,size_t) DECLSPEC_HIDDEN; 394 395 void *alloc_object(object_header_t*,const object_vtbl_t*,size_t) DECLSPEC_HIDDEN; 396 object_header_t *get_handle_object( HINTERNET hinternet ) DECLSPEC_HIDDEN; 397 object_header_t *WININET_AddRef( object_header_t *info ) DECLSPEC_HIDDEN; 398 BOOL WININET_Release( object_header_t *info ) DECLSPEC_HIDDEN; 399 400 DWORD INET_QueryOption(object_header_t*,DWORD,void*,DWORD*,BOOL) DECLSPEC_HIDDEN; 401 DWORD INET_SetOption(object_header_t*,DWORD,void*,DWORD) DECLSPEC_HIDDEN; 402 403 time_t ConvertTimeString(LPCWSTR asctime) DECLSPEC_HIDDEN; 404 405 HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName, 406 INTERNET_PORT nServerPort, LPCWSTR lpszUserName, 407 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext, 408 DWORD dwInternalFlags) DECLSPEC_HIDDEN; 409 410 DWORD HTTP_Connect(appinfo_t*,LPCWSTR, 411 INTERNET_PORT nServerPort, LPCWSTR lpszUserName, 412 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext, 413 DWORD dwInternalFlags, HINTERNET*) DECLSPEC_HIDDEN; 414 415 BOOL GetAddress(const WCHAR*,INTERNET_PORT,SOCKADDR*,int*,char*) DECLSPEC_HIDDEN; 416 417 DWORD get_cookie_header(const WCHAR*,const WCHAR*,WCHAR**) DECLSPEC_HIDDEN; 418 DWORD set_cookie(substr_t,substr_t,substr_t,substr_t,DWORD) DECLSPEC_HIDDEN; 419 420 void INTERNET_SetLastError(DWORD dwError) DECLSPEC_HIDDEN; 421 DWORD INTERNET_GetLastError(void) DECLSPEC_HIDDEN; 422 DWORD INTERNET_AsyncCall(task_header_t*) DECLSPEC_HIDDEN; 423 LPSTR INTERNET_GetResponseBuffer(void) DECLSPEC_HIDDEN; 424 425 VOID INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR dwContext, 426 DWORD dwInternetStatus, LPVOID lpvStatusInfo, 427 DWORD dwStatusInfoLength) DECLSPEC_HIDDEN; 428 WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto) DECLSPEC_HIDDEN; 429 430 DWORD create_netconn(server_t*,DWORD,BOOL,DWORD,netconn_t**) DECLSPEC_HIDDEN; 431 void free_netconn(netconn_t*) DECLSPEC_HIDDEN; 432 void NETCON_unload(void) DECLSPEC_HIDDEN; 433 DWORD NETCON_secure_connect(netconn_t*,server_t*) DECLSPEC_HIDDEN; 434 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags, 435 int *sent /* out */) DECLSPEC_HIDDEN; 436 DWORD NETCON_recv(netconn_t*,void*,size_t,BOOL,int*) DECLSPEC_HIDDEN; 437 BOOL NETCON_is_alive(netconn_t*) DECLSPEC_HIDDEN; 438 LPCVOID NETCON_GetCert(netconn_t *connection) DECLSPEC_HIDDEN; 439 int NETCON_GetCipherStrength(netconn_t*) DECLSPEC_HIDDEN; 440 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value) DECLSPEC_HIDDEN; 441 int sock_send(int fd, const void *msg, size_t len, int flags) DECLSPEC_HIDDEN; 442 int sock_recv(int fd, void *msg, size_t len, int flags) DECLSPEC_HIDDEN; 443 444 server_t *get_server(substr_t,INTERNET_PORT,BOOL,BOOL) DECLSPEC_HIDDEN; 445 446 DWORD create_req_file(const WCHAR*,req_file_t**) DECLSPEC_HIDDEN; 447 void req_file_release(req_file_t*) DECLSPEC_HIDDEN; 448 449 static inline req_file_t *req_file_addref(req_file_t *req_file) 450 { 451 InterlockedIncrement(&req_file->ref); 452 return req_file; 453 } 454 455 BOOL init_urlcache(void) DECLSPEC_HIDDEN; 456 void free_urlcache(void) DECLSPEC_HIDDEN; 457 void free_cookie(void) DECLSPEC_HIDDEN; 458 void free_authorization_cache(void) DECLSPEC_HIDDEN; 459 460 void init_winsock(void) DECLSPEC_HIDDEN; 461 462 #define MAX_REPLY_LEN 0x5B4 463 464 /* Used for debugging - maybe need to be shared in the Wine debugging code ? */ 465 typedef struct 466 { 467 DWORD val; 468 const char* name; 469 } wininet_flag_info; 470 471 /* Undocumented security flags */ 472 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000 473 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000 474 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000 475 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000 476 477 #define _SECURITY_ERROR_FLAGS_MASK \ 478 (_SECURITY_FLAG_CERT_REV_FAILED \ 479 |_SECURITY_FLAG_CERT_INVALID_CA \ 480 |_SECURITY_FLAG_CERT_INVALID_CN \ 481 |_SECURITY_FLAG_CERT_INVALID_DATE) 482 483 #endif /* _WINE_INTERNET_H_ */ 484