kernelbase: Let GetModuleBaseName succeed on 64bit modules in wow64.
[wine.git] / dlls / webservices / listener.c
blob6b9dca0548669c4b6ee836acd7d672454a3e1c5d
1 /*
2 * Copyright 2017 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 <stdarg.h>
20 #include <locale.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "webservices.h"
26 #include "wine/debug.h"
27 #include "wine/list.h"
28 #include "webservices_private.h"
29 #include "sock.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
33 HINSTANCE webservices_instance;
35 static BOOL winsock_loaded;
37 static BOOL WINAPI winsock_startup( INIT_ONCE *once, void *param, void **ctx )
39 int ret;
40 WSADATA data;
41 if (!(ret = WSAStartup( MAKEWORD(1,1), &data ))) winsock_loaded = TRUE;
42 else ERR( "WSAStartup failed: %d\n", ret );
43 return TRUE;
46 void winsock_init(void)
48 static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
49 InitOnceExecuteOnce( &once, winsock_startup, NULL, NULL );
52 _locale_t c_locale;
54 /******************************************************************
55 * DllMain (webservices.@)
57 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, void *reserved )
59 switch (reason)
61 case DLL_PROCESS_ATTACH:
62 webservices_instance = hinst;
63 DisableThreadLibraryCalls( hinst );
64 c_locale = _create_locale( LC_ALL, "C" );
65 break;
67 case DLL_PROCESS_DETACH:
68 if (reserved) break;
69 if (winsock_loaded) WSACleanup();
70 _free_locale( c_locale );
71 break;
73 return TRUE;
76 static const struct prop_desc listener_props[] =
78 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_LISTEN_BACKLOG */
79 { sizeof(WS_IP_VERSION), FALSE }, /* WS_LISTENER_PROPERTY_IP_VERSION */
80 { sizeof(WS_LISTENER_STATE), TRUE }, /* WS_LISTENER_PROPERTY_STATE */
81 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_LISTENER_PROPERTY_ASYNC_CALLBACK_MODEL */
82 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_TYPE */
83 { sizeof(WS_CHANNEL_BINDING), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_BINDING */
84 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CONNECT_TIMEOUT */
85 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_IS_MULTICAST */
86 { 0, FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_INTERFACES */
87 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_LOOPBACK */
88 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CLOSE_TIMEOUT */
89 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TO_HEADER_MATCHING_OPTIONS */
90 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TRANSPORT_URL_MATCHING_OPTIONS */
91 { sizeof(WS_CUSTOM_LISTENER_CALLBACKS), FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_CALLBACKS */
92 { 0, FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_PARAMETERS */
93 { sizeof(void *), TRUE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_INSTANCE */
94 { sizeof(WS_DISALLOWED_USER_AGENT_SUBSTRINGS), FALSE } /* WS_LISTENER_PROPERTY_DISALLOWED_USER_AGENT */
97 struct listener
99 ULONG magic;
100 CRITICAL_SECTION cs;
101 WS_CHANNEL_TYPE type;
102 WS_CHANNEL_BINDING binding;
103 WS_LISTENER_STATE state;
104 HANDLE wait;
105 HANDLE cancel;
106 WS_CHANNEL *channel;
107 union
109 struct
111 SOCKET socket;
112 } tcp;
113 struct
115 SOCKET socket;
116 } udp;
117 } u;
118 ULONG prop_count;
119 struct prop prop[ARRAY_SIZE( listener_props )];
122 #define LISTENER_MAGIC (('L' << 24) | ('I' << 16) | ('S' << 8) | 'T')
124 static struct listener *alloc_listener(void)
126 static const ULONG count = ARRAY_SIZE( listener_props );
127 struct listener *ret;
128 ULONG size = sizeof(*ret) + prop_size( listener_props, count );
130 if (!(ret = calloc( 1, size ))) return NULL;
132 ret->magic = LISTENER_MAGIC;
133 if (!(ret->wait = CreateEventW( NULL, FALSE, FALSE, NULL )))
135 free( ret );
136 return NULL;
138 if (!(ret->cancel = CreateEventW( NULL, FALSE, FALSE, NULL )))
140 CloseHandle( ret->wait );
141 free( ret );
142 return NULL;
144 InitializeCriticalSection( &ret->cs );
145 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": listener.cs");
147 prop_init( listener_props, count, ret->prop, &ret[1] );
148 ret->prop_count = count;
149 return ret;
152 static void reset_listener( struct listener *listener )
154 listener->state = WS_LISTENER_STATE_CREATED;
155 SetEvent( listener->cancel );
156 listener->channel = NULL;
158 switch (listener->binding)
160 case WS_TCP_CHANNEL_BINDING:
161 closesocket( listener->u.tcp.socket );
162 listener->u.tcp.socket = -1;
163 break;
165 case WS_UDP_CHANNEL_BINDING:
166 closesocket( listener->u.udp.socket );
167 listener->u.udp.socket = -1;
168 break;
170 default: break;
174 static void free_listener( struct listener *listener )
176 reset_listener( listener );
178 CloseHandle( listener->wait );
179 CloseHandle( listener->cancel );
181 listener->cs.DebugInfo->Spare[0] = 0;
182 DeleteCriticalSection( &listener->cs );
183 free( listener );
186 static HRESULT create_listener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
187 const WS_LISTENER_PROPERTY *properties, ULONG count, struct listener **ret )
189 struct listener *listener;
190 HRESULT hr;
191 ULONG i;
193 if (!(listener = alloc_listener())) return E_OUTOFMEMORY;
195 for (i = 0; i < count; i++)
197 hr = prop_set( listener->prop, listener->prop_count, properties[i].id, properties[i].value,
198 properties[i].valueSize );
199 if (hr != S_OK)
201 free_listener( listener );
202 return hr;
206 listener->type = type;
207 listener->binding = binding;
209 switch (listener->binding)
211 case WS_TCP_CHANNEL_BINDING:
212 listener->u.tcp.socket = -1;
213 break;
215 case WS_UDP_CHANNEL_BINDING:
216 listener->u.udp.socket = -1;
217 break;
219 default: break;
222 *ret = listener;
223 return S_OK;
226 /**************************************************************************
227 * WsCreateListener [webservices.@]
229 HRESULT WINAPI WsCreateListener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
230 const WS_LISTENER_PROPERTY *properties, ULONG count,
231 const WS_SECURITY_DESCRIPTION *desc, WS_LISTENER **handle,
232 WS_ERROR *error )
234 struct listener *listener;
235 HRESULT hr;
237 TRACE( "%u %u %p %lu %p %p %p\n", type, binding, properties, count, desc, handle, error );
238 if (error) FIXME( "ignoring error parameter\n" );
239 if (desc) FIXME( "ignoring security description\n" );
241 if (!handle) return E_INVALIDARG;
243 if (type != WS_CHANNEL_TYPE_DUPLEX_SESSION && type != WS_CHANNEL_TYPE_DUPLEX)
245 FIXME( "channel type %u not implemented\n", type );
246 return E_NOTIMPL;
248 if (binding != WS_TCP_CHANNEL_BINDING && binding != WS_UDP_CHANNEL_BINDING)
250 FIXME( "channel binding %u not implemented\n", binding );
251 return E_NOTIMPL;
254 if ((hr = create_listener( type, binding, properties, count, &listener )) != S_OK) return hr;
256 TRACE( "created %p\n", listener );
257 *handle = (WS_LISTENER *)listener;
258 return S_OK;
261 /**************************************************************************
262 * WsFreeListener [webservices.@]
264 void WINAPI WsFreeListener( WS_LISTENER *handle )
266 struct listener *listener = (struct listener *)handle;
268 TRACE( "%p\n", handle );
270 if (!listener) return;
272 EnterCriticalSection( &listener->cs );
274 if (listener->magic != LISTENER_MAGIC)
276 LeaveCriticalSection( &listener->cs );
277 return;
280 listener->magic = 0;
282 LeaveCriticalSection( &listener->cs );
283 free_listener( listener );
286 HRESULT resolve_hostname( const WCHAR *host, USHORT port, struct sockaddr *addr, int *addr_len, int flags )
288 WCHAR service[6];
289 ADDRINFOW hints, *res, *info;
290 HRESULT hr = WS_E_ADDRESS_NOT_AVAILABLE;
292 memset( &hints, 0, sizeof(hints) );
293 hints.ai_flags = flags;
294 hints.ai_family = AF_INET;
296 *addr_len = 0;
297 swprintf( service, ARRAY_SIZE(service), L"%u", port );
298 if (GetAddrInfoW( host, service, &hints, &res )) return HRESULT_FROM_WIN32( WSAGetLastError() );
300 info = res;
301 while (info && info->ai_family != AF_INET) info = info->ai_next;
302 if (info)
304 memcpy( addr, info->ai_addr, info->ai_addrlen );
305 *addr_len = info->ai_addrlen;
306 hr = S_OK;
309 FreeAddrInfoW( res );
310 return hr;
313 HRESULT parse_url( const WS_STRING *str, WS_URL_SCHEME_TYPE *scheme, WCHAR **host, USHORT *port )
315 WS_HEAP *heap;
316 WS_NETTCP_URL *url;
317 HRESULT hr;
319 if ((hr = WsCreateHeap( 1 << 8, 0, NULL, 0, &heap, NULL )) != S_OK) return hr;
320 if ((hr = WsDecodeUrl( str, 0, heap, (WS_URL **)&url, NULL )) != S_OK)
322 WsFreeHeap( heap );
323 return hr;
326 if (url->host.length == 1 && (url->host.chars[0] == '+' || url->host.chars[0] == '*')) *host = NULL;
327 else
329 if (!(*host = malloc( (url->host.length + 1) * sizeof(WCHAR) )))
331 WsFreeHeap( heap );
332 return E_OUTOFMEMORY;
334 memcpy( *host, url->host.chars, url->host.length * sizeof(WCHAR) );
335 (*host)[url->host.length] = 0;
337 *scheme = url->url.scheme;
338 *port = url->port;
340 WsFreeHeap( heap );
341 return hr;
344 static HRESULT open_listener_tcp( struct listener *listener, const WS_STRING *url )
346 struct sockaddr_storage storage;
347 struct sockaddr *addr = (struct sockaddr *)&storage;
348 int addr_len, on = 1;
349 WS_URL_SCHEME_TYPE scheme;
350 WCHAR *host;
351 USHORT port;
352 HRESULT hr;
354 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
355 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
357 free( host );
358 return WS_E_INVALID_ENDPOINT_URL;
361 winsock_init();
363 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
364 free( host );
365 if (hr != S_OK) return hr;
367 if ((listener->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
368 return HRESULT_FROM_WIN32( WSAGetLastError() );
370 if (setsockopt( listener->u.tcp.socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) ) < 0)
372 closesocket( listener->u.tcp.socket );
373 listener->u.tcp.socket = -1;
374 return HRESULT_FROM_WIN32( WSAGetLastError() );
377 if (bind( listener->u.tcp.socket, addr, addr_len ) < 0)
379 closesocket( listener->u.tcp.socket );
380 listener->u.tcp.socket = -1;
381 return HRESULT_FROM_WIN32( WSAGetLastError() );
384 if (listen( listener->u.tcp.socket, 0 ) < 0)
386 closesocket( listener->u.tcp.socket );
387 listener->u.tcp.socket = -1;
388 return HRESULT_FROM_WIN32( WSAGetLastError() );
391 listener->state = WS_LISTENER_STATE_OPEN;
392 return S_OK;
395 static HRESULT open_listener_udp( struct listener *listener, const WS_STRING *url )
397 struct sockaddr_storage storage;
398 struct sockaddr *addr = (struct sockaddr *)&storage;
399 int addr_len;
400 WS_URL_SCHEME_TYPE scheme;
401 WCHAR *host;
402 USHORT port;
403 HRESULT hr;
405 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
406 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
408 free( host );
409 return WS_E_INVALID_ENDPOINT_URL;
412 winsock_init();
414 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
415 free( host );
416 if (hr != S_OK) return hr;
418 if ((listener->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
419 return HRESULT_FROM_WIN32( WSAGetLastError() );
421 if (bind( listener->u.udp.socket, addr, addr_len ) < 0)
423 closesocket( listener->u.udp.socket );
424 listener->u.udp.socket = -1;
425 return HRESULT_FROM_WIN32( WSAGetLastError() );
428 listener->state = WS_LISTENER_STATE_OPEN;
429 return S_OK;
432 static HRESULT open_listener( struct listener *listener, const WS_STRING *url )
434 switch (listener->binding)
436 case WS_TCP_CHANNEL_BINDING:
437 return open_listener_tcp( listener, url );
439 case WS_UDP_CHANNEL_BINDING:
440 return open_listener_udp( listener, url );
442 default:
443 ERR( "unhandled binding %u\n", listener->binding );
444 return E_NOTIMPL;
448 /**************************************************************************
449 * WsOpenListener [webservices.@]
451 HRESULT WINAPI WsOpenListener( WS_LISTENER *handle, WS_STRING *url, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
453 struct listener *listener = (struct listener *)handle;
454 HRESULT hr;
456 TRACE( "%p %s %p %p\n", handle, url ? debugstr_wn(url->chars, url->length) : "null", ctx, error );
457 if (error) FIXME( "ignoring error parameter\n" );
458 if (ctx) FIXME( "ignoring ctx parameter\n" );
460 if (!listener || !url) return E_INVALIDARG;
462 EnterCriticalSection( &listener->cs );
464 if (listener->magic != LISTENER_MAGIC)
466 LeaveCriticalSection( &listener->cs );
467 return E_INVALIDARG;
470 if (listener->state != WS_LISTENER_STATE_CREATED) hr = WS_E_INVALID_OPERATION;
471 else hr = open_listener( listener, url );
473 LeaveCriticalSection( &listener->cs );
474 TRACE( "returning %#lx\n", hr );
475 return hr;
478 static void close_listener( struct listener *listener )
480 reset_listener( listener );
481 listener->state = WS_LISTENER_STATE_CLOSED;
484 /**************************************************************************
485 * WsCloseListener [webservices.@]
487 HRESULT WINAPI WsCloseListener( WS_LISTENER *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
489 struct listener *listener = (struct listener *)handle;
490 HRESULT hr = S_OK;
492 TRACE( "%p %p %p\n", handle, ctx, error );
493 if (error) FIXME( "ignoring error parameter\n" );
494 if (ctx) FIXME( "ignoring ctx parameter\n" );
496 if (!listener) return E_INVALIDARG;
498 EnterCriticalSection( &listener->cs );
500 if (listener->magic != LISTENER_MAGIC)
502 LeaveCriticalSection( &listener->cs );
503 return E_INVALIDARG;
506 close_listener( listener );
508 LeaveCriticalSection( &listener->cs );
509 TRACE( "returning %#lx\n", hr );
510 return hr;
513 /**************************************************************************
514 * WsResetListener [webservices.@]
516 HRESULT WINAPI WsResetListener( WS_LISTENER *handle, WS_ERROR *error )
518 struct listener *listener = (struct listener *)handle;
519 HRESULT hr = S_OK;
521 TRACE( "%p %p\n", handle, error );
522 if (error) FIXME( "ignoring error parameter\n" );
524 if (!listener) return E_INVALIDARG;
526 EnterCriticalSection( &listener->cs );
528 if (listener->magic != LISTENER_MAGIC)
530 LeaveCriticalSection( &listener->cs );
531 return E_INVALIDARG;
534 if (listener->state != WS_LISTENER_STATE_CREATED && listener->state != WS_LISTENER_STATE_CLOSED)
535 hr = WS_E_INVALID_OPERATION;
536 else
537 reset_listener( listener );
539 LeaveCriticalSection( &listener->cs );
540 TRACE( "returning %#lx\n", hr );
541 return hr;
544 /**************************************************************************
545 * WsGetListenerProperty [webservices.@]
547 HRESULT WINAPI WsGetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, void *buf,
548 ULONG size, WS_ERROR *error )
550 struct listener *listener = (struct listener *)handle;
551 HRESULT hr = S_OK;
553 TRACE( "%p %u %p %lu %p\n", handle, id, buf, size, error );
554 if (error) FIXME( "ignoring error parameter\n" );
556 if (!listener) return E_INVALIDARG;
558 EnterCriticalSection( &listener->cs );
560 if (listener->magic != LISTENER_MAGIC)
562 LeaveCriticalSection( &listener->cs );
563 return E_INVALIDARG;
566 switch (id)
568 case WS_LISTENER_PROPERTY_STATE:
569 if (!buf || size != sizeof(listener->state)) hr = E_INVALIDARG;
570 else *(WS_LISTENER_STATE *)buf = listener->state;
571 break;
573 case WS_LISTENER_PROPERTY_CHANNEL_TYPE:
574 if (!buf || size != sizeof(listener->type)) hr = E_INVALIDARG;
575 else *(WS_CHANNEL_TYPE *)buf = listener->type;
576 break;
578 case WS_LISTENER_PROPERTY_CHANNEL_BINDING:
579 if (!buf || size != sizeof(listener->binding)) hr = E_INVALIDARG;
580 else *(WS_CHANNEL_BINDING *)buf = listener->binding;
581 break;
583 default:
584 hr = prop_get( listener->prop, listener->prop_count, id, buf, size );
587 LeaveCriticalSection( &listener->cs );
588 TRACE( "returning %#lx\n", hr );
589 return hr;
592 /**************************************************************************
593 * WsSetListenerProperty [webservices.@]
595 HRESULT WINAPI WsSetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, const void *value,
596 ULONG size, WS_ERROR *error )
598 struct listener *listener = (struct listener *)handle;
599 HRESULT hr;
601 TRACE( "%p %u %p %lu %p\n", handle, id, value, size, error );
602 if (error) FIXME( "ignoring error parameter\n" );
604 if (!listener) return E_INVALIDARG;
606 EnterCriticalSection( &listener->cs );
608 if (listener->magic != LISTENER_MAGIC)
610 LeaveCriticalSection( &listener->cs );
611 return E_INVALIDARG;
614 hr = prop_set( listener->prop, listener->prop_count, id, value, size );
616 LeaveCriticalSection( &listener->cs );
617 TRACE( "returning %#lx\n", hr );
618 return hr;
621 /**************************************************************************
622 * WsAcceptChannel [webservices.@]
624 HRESULT WINAPI WsAcceptChannel( WS_LISTENER *handle, WS_CHANNEL *channel_handle, const WS_ASYNC_CONTEXT *ctx,
625 WS_ERROR *error )
627 struct listener *listener = (struct listener *)handle;
628 HRESULT hr = E_NOTIMPL;
629 HANDLE wait, cancel;
631 TRACE( "%p %p %p %p\n", handle, channel_handle, ctx, error );
632 if (error) FIXME( "ignoring error parameter\n" );
633 if (ctx) FIXME( "ignoring ctx parameter\n" );
635 if (!listener || !channel_handle) return E_INVALIDARG;
637 EnterCriticalSection( &listener->cs );
639 if (listener->magic != LISTENER_MAGIC)
641 LeaveCriticalSection( &listener->cs );
642 return E_INVALIDARG;
645 if (listener->state != WS_LISTENER_STATE_OPEN || (listener->channel && listener->channel != channel_handle))
647 hr = WS_E_INVALID_OPERATION;
649 else
651 wait = listener->wait;
652 cancel = listener->cancel;
653 listener->channel = channel_handle;
655 switch (listener->binding)
657 case WS_TCP_CHANNEL_BINDING:
659 SOCKET socket = listener->u.tcp.socket;
661 LeaveCriticalSection( &listener->cs );
662 hr = channel_accept_tcp( socket, wait, cancel, channel_handle );
663 TRACE( "returning %#lx\n", hr );
664 return hr;
666 case WS_UDP_CHANNEL_BINDING:
668 SOCKET socket = listener->u.udp.socket;
670 LeaveCriticalSection( &listener->cs );
671 hr = channel_accept_udp( socket, wait, cancel, channel_handle );
672 TRACE( "returning %#lx\n", hr );
673 return hr;
675 default:
676 FIXME( "listener binding %u not supported\n", listener->binding );
677 break;
681 LeaveCriticalSection( &listener->cs );
682 TRACE( "returning %#lx\n", hr );
683 return hr;