msvcrt: _Gettnames() should respect user overrides.
[wine.git] / dlls / jsproxy / main.c
bloba52fbb4df7d5b3c5f038b5996afe89d37ed9114f
1 /*
2 * Copyright 2014 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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SOCKET_H
25 # include <sys/socket.h>
26 #endif
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 # include <netdb.h>
32 #endif
33 #if defined(__MINGW32__) || defined (_MSC_VER)
34 # include <ws2tcpip.h>
35 #else
36 # define closesocket close
37 # define ioctlsocket ioctl
38 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #ifndef __MINGW32__
43 #define USE_WS_PREFIX
44 #endif
45 #include "winsock2.h"
46 #include "ws2ipdef.h"
47 #include "winnls.h"
48 #include "wininet.h"
49 #define COBJMACROS
50 #include "ole2.h"
51 #include "dispex.h"
52 #include "activscp.h"
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
56 static HINSTANCE instance;
58 WINE_DEFAULT_DEBUG_CHANNEL(jsproxy);
60 static CRITICAL_SECTION cs_jsproxy;
61 static CRITICAL_SECTION_DEBUG critsect_debug =
63 0, 0, &cs_jsproxy,
64 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
65 0, 0, { (DWORD_PTR)(__FILE__ ": cs_jsproxy") }
67 static CRITICAL_SECTION cs_jsproxy = { &critsect_debug, -1, 0, 0, 0, 0 };
69 static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
70 static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
72 /******************************************************************
73 * DllMain (jsproxy.@)
75 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
77 switch (reason)
79 case DLL_PROCESS_ATTACH:
80 instance = hinst;
81 DisableThreadLibraryCalls( hinst );
82 break;
84 case DLL_PROCESS_DETACH:
85 break;
87 return TRUE;
90 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t size)
92 return HeapAlloc(GetProcessHeap(), 0, size);
95 static inline BOOL heap_free(void *mem)
97 return HeapFree(GetProcessHeap(), 0, mem);
100 static inline WCHAR *strdupAW( const char *src, int len )
102 WCHAR *dst = NULL;
103 if (src)
105 int dst_len = MultiByteToWideChar( CP_ACP, 0, src, len, NULL, 0 );
106 if ((dst = heap_alloc( (dst_len + 1) * sizeof(WCHAR) )))
108 len = MultiByteToWideChar( CP_ACP, 0, src, len, dst, dst_len );
109 dst[dst_len] = 0;
112 return dst;
115 static inline char *strdupWA( const WCHAR *src )
117 char *dst = NULL;
118 if (src)
120 int len = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
121 if ((dst = heap_alloc( len ))) WideCharToMultiByte( CP_ACP, 0, src, -1, dst, len, NULL, NULL );
123 return dst;
126 static struct pac_script
128 WCHAR *text;
129 } pac_script;
130 static struct pac_script *global_script = &pac_script;
132 /******************************************************************
133 * InternetDeInitializeAutoProxyDll (jsproxy.@)
135 BOOL WINAPI InternetDeInitializeAutoProxyDll( LPSTR mime, DWORD reserved )
137 TRACE( "%s, %u\n", debugstr_a(mime), reserved );
139 EnterCriticalSection( &cs_jsproxy );
141 heap_free( global_script->text );
142 global_script->text = NULL;
144 LeaveCriticalSection( &cs_jsproxy );
145 return TRUE;
148 static WCHAR *load_script( const char *filename )
150 HANDLE handle;
151 DWORD size, bytes_read;
152 char *buffer;
153 int len;
154 WCHAR *script = NULL;
156 handle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
157 if (handle == INVALID_HANDLE_VALUE) return NULL;
159 size = GetFileSize( handle, NULL );
160 if (!(buffer = heap_alloc( size ))) goto done;
161 if (!ReadFile( handle, buffer, size, &bytes_read, NULL ) || bytes_read != size) goto done;
163 len = MultiByteToWideChar( CP_ACP, 0, buffer, size, NULL, 0 );
164 if (!(script = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
165 MultiByteToWideChar( CP_ACP, 0, buffer, size, script, len );
166 script[len] = 0;
168 done:
169 CloseHandle( handle );
170 heap_free( buffer );
171 return script;
174 /******************************************************************
175 * InternetInitializeAutoProxyDll (jsproxy.@)
177 BOOL WINAPI JSPROXY_InternetInitializeAutoProxyDll( DWORD version, LPSTR tmpfile, LPSTR mime,
178 AutoProxyHelperFunctions *callbacks,
179 LPAUTO_PROXY_SCRIPT_BUFFER buffer )
181 BOOL ret = FALSE;
183 TRACE( "%u, %s, %s, %p, %p\n", version, debugstr_a(tmpfile), debugstr_a(mime), callbacks, buffer );
185 if (callbacks) FIXME( "callbacks not supported\n" );
187 EnterCriticalSection( &cs_jsproxy );
189 if (buffer && buffer->dwStructSize == sizeof(*buffer) && buffer->lpszScriptBuffer)
191 DWORD i, len = 0;
192 for (i = 0; i < buffer->dwScriptBufferSize; i++)
194 if (!buffer->lpszScriptBuffer[i]) break;
195 len++;
197 if (len == buffer->dwScriptBufferSize)
199 SetLastError( ERROR_INVALID_PARAMETER );
200 LeaveCriticalSection( &cs_jsproxy );
201 return FALSE;
203 heap_free( global_script->text );
204 if ((global_script->text = strdupAW( buffer->lpszScriptBuffer, len ))) ret = TRUE;
206 else
208 heap_free( global_script->text );
209 if ((global_script->text = load_script( tmpfile ))) ret = TRUE;
212 LeaveCriticalSection( &cs_jsproxy );
213 return ret;
216 static HRESULT WINAPI dispex_QueryInterface(
217 IDispatchEx *iface, REFIID riid, void **ppv )
219 *ppv = NULL;
221 if (IsEqualGUID( riid, &IID_IUnknown ) ||
222 IsEqualGUID( riid, &IID_IDispatch ) ||
223 IsEqualGUID( riid, &IID_IDispatchEx ))
224 *ppv = iface;
225 else
226 return E_NOINTERFACE;
228 return S_OK;
231 static ULONG WINAPI dispex_AddRef(
232 IDispatchEx *iface )
234 return 2;
237 static ULONG WINAPI dispex_Release(
238 IDispatchEx *iface )
240 return 1;
243 static HRESULT WINAPI dispex_GetTypeInfoCount(
244 IDispatchEx *iface, UINT *info )
246 return E_NOTIMPL;
249 static HRESULT WINAPI dispex_GetTypeInfo(
250 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
252 return E_NOTIMPL;
255 static HRESULT WINAPI dispex_GetIDsOfNames(
256 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
258 return E_NOTIMPL;
261 static HRESULT WINAPI dispex_Invoke(
262 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
263 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
265 return E_NOTIMPL;
268 static HRESULT WINAPI dispex_DeleteMemberByName(
269 IDispatchEx *iface, BSTR name, DWORD flags )
271 return E_NOTIMPL;
274 static HRESULT WINAPI dispex_DeleteMemberByDispID(
275 IDispatchEx *iface, DISPID id )
277 return E_NOTIMPL;
280 static HRESULT WINAPI dispex_GetMemberProperties(
281 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
283 return E_NOTIMPL;
286 static HRESULT WINAPI dispex_GetMemberName(
287 IDispatchEx *iface, DISPID id, BSTR *name )
289 return E_NOTIMPL;
292 static HRESULT WINAPI dispex_GetNextDispID(
293 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
295 return E_NOTIMPL;
298 static HRESULT WINAPI dispex_GetNameSpaceParent(
299 IDispatchEx *iface, IUnknown **unk )
301 return E_NOTIMPL;
304 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
306 static HRESULT WINAPI dispex_GetDispID(
307 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
309 if (!strcmpW( name, dns_resolveW ))
311 *id = DISPID_GLOBAL_DNSRESOLVE;
312 return S_OK;
314 return DISP_E_UNKNOWNNAME;
317 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
319 char *ret;
320 DWORD size = 0;
322 GetComputerNameExA( format, NULL, &size );
323 if (GetLastError() != ERROR_MORE_DATA) return NULL;
324 if (!(ret = heap_alloc( size ))) return NULL;
325 if (!GetComputerNameExA( format, ret, &size ))
327 heap_free( ret );
328 return NULL;
330 return ret;
333 static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
335 sprintfW( buf, fmt,
336 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
337 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
338 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
339 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
342 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
344 #ifdef HAVE_GETADDRINFO
345 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
346 WCHAR addr[16];
347 struct addrinfo *ai, *elem;
348 char *hostnameA;
349 int res;
351 if (hostname[0])
352 hostnameA = strdupWA( hostname );
353 else
354 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
356 if (!hostnameA) return E_OUTOFMEMORY;
357 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
358 heap_free( hostnameA );
359 if (res) return S_FALSE;
361 elem = ai;
362 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
363 if (!elem)
365 freeaddrinfo( ai );
366 return S_FALSE;
368 printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
369 freeaddrinfo( ai );
370 V_VT( result ) = VT_BSTR;
371 V_BSTR( result ) = SysAllocString( addr );
372 return S_OK;
373 #else
374 FIXME("getaddrinfo not found at build time\n");
375 return S_FALSE;
376 #endif
379 static HRESULT WINAPI dispex_InvokeEx(
380 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
381 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
383 if (id == DISPID_GLOBAL_DNSRESOLVE)
385 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
386 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
387 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
389 return DISP_E_MEMBERNOTFOUND;
392 static const IDispatchExVtbl dispex_vtbl =
394 dispex_QueryInterface,
395 dispex_AddRef,
396 dispex_Release,
397 dispex_GetTypeInfoCount,
398 dispex_GetTypeInfo,
399 dispex_GetIDsOfNames,
400 dispex_Invoke,
401 dispex_GetDispID,
402 dispex_InvokeEx,
403 dispex_DeleteMemberByName,
404 dispex_DeleteMemberByDispID,
405 dispex_GetMemberProperties,
406 dispex_GetMemberName,
407 dispex_GetNextDispID,
408 dispex_GetNameSpaceParent
411 static IDispatchEx global_dispex = { &dispex_vtbl };
413 static HRESULT WINAPI site_QueryInterface(
414 IActiveScriptSite *iface, REFIID riid, void **ppv )
416 *ppv = NULL;
418 if (IsEqualGUID( &IID_IUnknown, riid ))
419 *ppv = iface;
420 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
421 *ppv = iface;
422 else
423 return E_NOINTERFACE;
425 IUnknown_AddRef( (IUnknown *)*ppv );
426 return S_OK;
429 static ULONG WINAPI site_AddRef(
430 IActiveScriptSite *iface )
432 return 2;
435 static ULONG WINAPI site_Release(
436 IActiveScriptSite *iface )
438 return 1;
441 static HRESULT WINAPI site_GetLCID(
442 IActiveScriptSite *iface, LCID *lcid )
444 return E_NOTIMPL;
447 static HRESULT WINAPI site_GetItemInfo(
448 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
449 IUnknown **item, ITypeInfo **type_info )
451 if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
453 *item = (IUnknown *)&global_dispex;
454 return S_OK;
456 return E_NOTIMPL;
459 static HRESULT WINAPI site_GetDocVersionString(
460 IActiveScriptSite *iface, BSTR *version )
462 return E_NOTIMPL;
465 static HRESULT WINAPI site_OnScriptTerminate(
466 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
468 return E_NOTIMPL;
471 static HRESULT WINAPI site_OnStateChange(
472 IActiveScriptSite *iface, SCRIPTSTATE state )
474 return E_NOTIMPL;
477 static HRESULT WINAPI site_OnScriptError(
478 IActiveScriptSite *iface, IActiveScriptError *error )
480 return E_NOTIMPL;
483 static HRESULT WINAPI site_OnEnterScript(
484 IActiveScriptSite *iface )
486 return E_NOTIMPL;
489 static HRESULT WINAPI site_OnLeaveScript(
490 IActiveScriptSite *iface )
492 return E_NOTIMPL;
495 static const IActiveScriptSiteVtbl site_vtbl =
497 site_QueryInterface,
498 site_AddRef,
499 site_Release,
500 site_GetLCID,
501 site_GetItemInfo,
502 site_GetDocVersionString,
503 site_OnScriptTerminate,
504 site_OnStateChange,
505 site_OnScriptError,
506 site_OnEnterScript,
507 site_OnLeaveScript
510 static IActiveScriptSite script_site = { &site_vtbl };
512 static BSTR include_pac_utils( const WCHAR *script )
514 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
515 HMODULE hmod = GetModuleHandleA( "jsproxy.dll" );
516 HRSRC rsrc;
517 DWORD size;
518 const char *data;
519 BSTR ret;
520 int len;
522 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
523 size = SizeofResource( hmod, rsrc );
524 data = LoadResource( hmod, rsrc );
526 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
527 if (!(ret = SysAllocStringLen( NULL, len + strlenW( script ) + 1 ))) return NULL;
528 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
529 strcpyW( ret + len, script );
530 return ret;
533 #ifdef _WIN64
534 #define IActiveScriptParse_Release IActiveScriptParse64_Release
535 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
536 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
537 #else
538 #define IActiveScriptParse_Release IActiveScriptParse32_Release
539 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
540 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
541 #endif
543 static BOOL run_script( const WCHAR *script, const WCHAR *url, const WCHAR *hostname, char **result_str, DWORD *result_len )
545 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
546 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
547 IActiveScriptParse *parser = NULL;
548 IActiveScript *engine = NULL;
549 IDispatch *dispatch = NULL;
550 BOOL ret = FALSE;
551 CLSID clsid;
552 DISPID dispid;
553 BSTR func = NULL, full_script = NULL;
554 VARIANT args[2], retval;
555 DISPPARAMS params;
556 HRESULT hr, init;
558 init = CoInitialize( NULL );
559 hr = CLSIDFromProgID( jscriptW, &clsid );
560 if (hr != S_OK) goto done;
562 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
563 &IID_IActiveScript, (void **)&engine );
564 if (hr != S_OK) goto done;
566 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
567 if (hr != S_OK) goto done;
569 hr = IActiveScriptParse_InitNew( parser );
570 if (hr != S_OK) goto done;
572 hr = IActiveScript_SetScriptSite( engine, &script_site );
573 if (hr != S_OK) goto done;
575 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
576 if (hr != S_OK) goto done;
578 if (!(full_script = include_pac_utils( script ))) goto done;
580 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
581 if (hr != S_OK) goto done;
583 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
584 if (hr != S_OK) goto done;
586 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
587 if (hr != S_OK) goto done;
589 if (!(func = SysAllocString( findproxyW ))) goto done;
590 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
591 if (hr != S_OK) goto done;
593 V_VT( &args[0] ) = VT_BSTR;
594 V_BSTR( &args[0] ) = SysAllocString( hostname );
595 V_VT( &args[1] ) = VT_BSTR;
596 V_BSTR( &args[1] ) = SysAllocString( url );
598 params.rgvarg = args;
599 params.rgdispidNamedArgs = NULL;
600 params.cArgs = 2;
601 params.cNamedArgs = 0;
602 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
603 &params, &retval, NULL, NULL );
604 VariantClear( &args[0] );
605 VariantClear( &args[1] );
606 if (hr != S_OK)
608 WARN("script failed 0x%08x\n", hr);
609 goto done;
611 if ((*result_str = strdupWA( V_BSTR( &retval ) )))
613 TRACE( "result: %s\n", debugstr_a(*result_str) );
614 *result_len = strlen( *result_str ) + 1;
615 ret = TRUE;
617 VariantClear( &retval );
619 done:
620 SysFreeString( full_script );
621 SysFreeString( func );
622 if (dispatch) IDispatch_Release( dispatch );
623 if (parser) IActiveScriptParse_Release( parser );
624 if (engine) IActiveScript_Release( engine );
625 if (SUCCEEDED( init )) CoUninitialize();
626 return ret;
629 /******************************************************************
630 * InternetGetProxyInfo (jsproxy.@)
632 BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
633 LPDWORD len_proxy )
635 WCHAR *urlW = NULL, *hostnameW = NULL;
636 BOOL ret = FALSE;
638 TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url), len_url, hostname, len_hostname, proxy, len_proxy );
640 EnterCriticalSection( &cs_jsproxy );
642 if (!global_script->text)
644 SetLastError( ERROR_CAN_NOT_COMPLETE );
645 goto done;
647 if (hostname && len_hostname < strlen( hostname ))
649 SetLastError( ERROR_INSUFFICIENT_BUFFER );
650 goto done;
652 if (!(urlW = strdupAW( url, -1 ))) goto done;
653 if (hostname && !(hostnameW = strdupAW( hostname, -1 ))) goto done;
655 TRACE( "%s\n", debugstr_w(global_script->text) );
656 ret = run_script( global_script->text, urlW, hostnameW, proxy, len_proxy );
658 done:
659 heap_free( hostnameW );
660 heap_free( urlW );
661 LeaveCriticalSection( &cs_jsproxy );
662 return ret;