mshtml: Use event target vtbl to construct target chain in fire_event_obj.
[wine.git] / dlls / webservices / listener.c
blob41afb274438c2bbdac291f99f88106bdb9b1311b
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>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "webservices.h"
25 #include "wine/debug.h"
26 #include "wine/list.h"
27 #include "wine/unicode.h"
28 #include "webservices_private.h"
29 #include "sock.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
33 static BOOL winsock_loaded;
35 static BOOL WINAPI winsock_startup( INIT_ONCE *once, void *param, void **ctx )
37 int ret;
38 WSADATA data;
39 if (!(ret = WSAStartup( MAKEWORD(1,1), &data ))) winsock_loaded = TRUE;
40 else ERR( "WSAStartup failed: %d\n", ret );
41 return TRUE;
44 void winsock_init(void)
46 static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
47 InitOnceExecuteOnce( &once, winsock_startup, NULL, NULL );
50 static const struct prop_desc listener_props[] =
52 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_LISTEN_BACKLOG */
53 { sizeof(WS_IP_VERSION), FALSE }, /* WS_LISTENER_PROPERTY_IP_VERSION */
54 { sizeof(WS_LISTENER_STATE), TRUE }, /* WS_LISTENER_PROPERTY_STATE */
55 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_LISTENER_PROPERTY_ASYNC_CALLBACK_MODEL */
56 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_TYPE */
57 { sizeof(WS_CHANNEL_BINDING), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_BINDING */
58 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CONNECT_TIMEOUT */
59 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_IS_MULTICAST */
60 { 0, FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_INTERFACES */
61 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_LOOPBACK */
62 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CLOSE_TIMEOUT */
63 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TO_HEADER_MATCHING_OPTIONS */
64 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TRANSPORT_URL_MATCHING_OPTIONS */
65 { sizeof(WS_CUSTOM_LISTENER_CALLBACKS), FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_CALLBACKS */
66 { 0, FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_PARAMETERS */
67 { sizeof(void *), TRUE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_INSTANCE */
68 { sizeof(WS_DISALLOWED_USER_AGENT_SUBSTRINGS), FALSE } /* WS_LISTENER_PROPERTY_DISALLOWED_USER_AGENT */
71 struct listener
73 ULONG magic;
74 CRITICAL_SECTION cs;
75 WS_CHANNEL_TYPE type;
76 WS_CHANNEL_BINDING binding;
77 WS_LISTENER_STATE state;
78 HANDLE wait;
79 HANDLE cancel;
80 WS_CHANNEL *channel;
81 union
83 struct
85 SOCKET socket;
86 } tcp;
87 struct
89 SOCKET socket;
90 } udp;
91 } u;
92 ULONG prop_count;
93 struct prop prop[sizeof(listener_props)/sizeof(listener_props[0])];
96 #define LISTENER_MAGIC (('L' << 24) | ('I' << 16) | ('S' << 8) | 'T')
98 static struct listener *alloc_listener(void)
100 static const ULONG count = sizeof(listener_props)/sizeof(listener_props[0]);
101 struct listener *ret;
102 ULONG size = sizeof(*ret) + prop_size( listener_props, count );
104 if (!(ret = heap_alloc_zero( size ))) return NULL;
106 ret->magic = LISTENER_MAGIC;
107 if (!(ret->wait = CreateEventW( NULL, FALSE, FALSE, NULL )))
109 heap_free( ret );
110 return NULL;
112 if (!(ret->cancel = CreateEventW( NULL, FALSE, FALSE, NULL )))
114 CloseHandle( ret->wait );
115 heap_free( ret );
116 return NULL;
118 InitializeCriticalSection( &ret->cs );
119 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": listener.cs");
121 prop_init( listener_props, count, ret->prop, &ret[1] );
122 ret->prop_count = count;
123 return ret;
126 static void reset_listener( struct listener *listener )
128 listener->state = WS_LISTENER_STATE_CREATED;
129 SetEvent( listener->cancel );
130 listener->channel = NULL;
132 switch (listener->binding)
134 case WS_TCP_CHANNEL_BINDING:
135 closesocket( listener->u.tcp.socket );
136 listener->u.tcp.socket = -1;
137 break;
139 case WS_UDP_CHANNEL_BINDING:
140 closesocket( listener->u.udp.socket );
141 listener->u.udp.socket = -1;
142 break;
144 default: break;
148 static void free_listener( struct listener *listener )
150 reset_listener( listener );
152 CloseHandle( listener->wait );
153 CloseHandle( listener->cancel );
155 listener->cs.DebugInfo->Spare[0] = 0;
156 DeleteCriticalSection( &listener->cs );
157 heap_free( listener );
160 static HRESULT create_listener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
161 const WS_LISTENER_PROPERTY *properties, ULONG count, struct listener **ret )
163 struct listener *listener;
164 HRESULT hr;
165 ULONG i;
167 if (!(listener = alloc_listener())) return E_OUTOFMEMORY;
169 for (i = 0; i < count; i++)
171 hr = prop_set( listener->prop, listener->prop_count, properties[i].id, properties[i].value,
172 properties[i].valueSize );
173 if (hr != S_OK)
175 free_listener( listener );
176 return hr;
180 listener->type = type;
181 listener->binding = binding;
183 switch (listener->binding)
185 case WS_TCP_CHANNEL_BINDING:
186 listener->u.tcp.socket = -1;
187 break;
189 case WS_UDP_CHANNEL_BINDING:
190 listener->u.udp.socket = -1;
191 break;
193 default: break;
196 *ret = listener;
197 return S_OK;
200 /**************************************************************************
201 * WsCreateListener [webservices.@]
203 HRESULT WINAPI WsCreateListener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
204 const WS_LISTENER_PROPERTY *properties, ULONG count,
205 const WS_SECURITY_DESCRIPTION *desc, WS_LISTENER **handle,
206 WS_ERROR *error )
208 struct listener *listener;
209 HRESULT hr;
211 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
212 if (error) FIXME( "ignoring error parameter\n" );
213 if (desc) FIXME( "ignoring security description\n" );
215 if (!handle) return E_INVALIDARG;
217 if (type != WS_CHANNEL_TYPE_DUPLEX_SESSION && type != WS_CHANNEL_TYPE_DUPLEX)
219 FIXME( "channel type %u not implemented\n", type );
220 return E_NOTIMPL;
222 if (binding != WS_TCP_CHANNEL_BINDING && binding != WS_UDP_CHANNEL_BINDING)
224 FIXME( "channel binding %u not implemented\n", binding );
225 return E_NOTIMPL;
228 if ((hr = create_listener( type, binding, properties, count, &listener )) != S_OK) return hr;
230 TRACE( "created %p\n", listener );
231 *handle = (WS_LISTENER *)listener;
232 return S_OK;
235 /**************************************************************************
236 * WsFreeListener [webservices.@]
238 void WINAPI WsFreeListener( WS_LISTENER *handle )
240 struct listener *listener = (struct listener *)handle;
242 TRACE( "%p\n", handle );
244 if (!listener) return;
246 EnterCriticalSection( &listener->cs );
248 if (listener->magic != LISTENER_MAGIC)
250 LeaveCriticalSection( &listener->cs );
251 return;
254 listener->magic = 0;
256 LeaveCriticalSection( &listener->cs );
257 free_listener( listener );
260 HRESULT resolve_hostname( const WCHAR *host, USHORT port, struct sockaddr *addr, int *addr_len, int flags )
262 static const WCHAR fmtW[] = {'%','u',0};
263 WCHAR service[6];
264 ADDRINFOW hints, *res, *info;
265 HRESULT hr = WS_E_ADDRESS_NOT_AVAILABLE;
267 memset( &hints, 0, sizeof(hints) );
268 hints.ai_flags = flags;
269 hints.ai_family = AF_INET;
271 *addr_len = 0;
272 sprintfW( service, fmtW, port );
273 if (GetAddrInfoW( host, service, &hints, &res )) return HRESULT_FROM_WIN32( WSAGetLastError() );
275 info = res;
276 while (info && info->ai_family != AF_INET) info = info->ai_next;
277 if (info)
279 memcpy( addr, info->ai_addr, info->ai_addrlen );
280 *addr_len = info->ai_addrlen;
281 hr = S_OK;
284 FreeAddrInfoW( res );
285 return hr;
288 HRESULT parse_url( const WS_STRING *str, WS_URL_SCHEME_TYPE *scheme, WCHAR **host, USHORT *port )
290 WS_HEAP *heap;
291 WS_NETTCP_URL *url;
292 HRESULT hr;
294 if ((hr = WsCreateHeap( 1 << 8, 0, NULL, 0, &heap, NULL )) != S_OK) return hr;
295 if ((hr = WsDecodeUrl( str, 0, heap, (WS_URL **)&url, NULL )) != S_OK)
297 WsFreeHeap( heap );
298 return hr;
301 if (url->host.length == 1 && (url->host.chars[0] == '+' || url->host.chars[0] == '*')) *host = NULL;
302 else
304 if (!(*host = heap_alloc( (url->host.length + 1) * sizeof(WCHAR) )))
306 WsFreeHeap( heap );
307 return E_OUTOFMEMORY;
309 memcpy( *host, url->host.chars, url->host.length * sizeof(WCHAR) );
310 (*host)[url->host.length] = 0;
312 *scheme = url->url.scheme;
313 *port = url->port;
315 WsFreeHeap( heap );
316 return hr;
319 static HRESULT open_listener_tcp( struct listener *listener, const WS_STRING *url )
321 struct sockaddr_storage storage;
322 struct sockaddr *addr = (struct sockaddr *)&storage;
323 int addr_len, on = 1;
324 WS_URL_SCHEME_TYPE scheme;
325 WCHAR *host;
326 USHORT port;
327 HRESULT hr;
329 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
330 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
332 heap_free( host );
333 return WS_E_INVALID_ENDPOINT_URL;
336 winsock_init();
338 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
339 heap_free( host );
340 if (hr != S_OK) return hr;
342 if ((listener->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
343 return HRESULT_FROM_WIN32( WSAGetLastError() );
345 if (setsockopt( listener->u.tcp.socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) ) < 0)
347 closesocket( listener->u.tcp.socket );
348 listener->u.tcp.socket = -1;
349 return HRESULT_FROM_WIN32( WSAGetLastError() );
352 if (bind( listener->u.tcp.socket, addr, addr_len ) < 0)
354 closesocket( listener->u.tcp.socket );
355 listener->u.tcp.socket = -1;
356 return HRESULT_FROM_WIN32( WSAGetLastError() );
359 if (listen( listener->u.tcp.socket, 0 ) < 0)
361 closesocket( listener->u.tcp.socket );
362 listener->u.tcp.socket = -1;
363 return HRESULT_FROM_WIN32( WSAGetLastError() );
366 listener->state = WS_LISTENER_STATE_OPEN;
367 return S_OK;
370 static HRESULT open_listener_udp( struct listener *listener, const WS_STRING *url )
372 struct sockaddr_storage storage;
373 struct sockaddr *addr = (struct sockaddr *)&storage;
374 int addr_len;
375 WS_URL_SCHEME_TYPE scheme;
376 WCHAR *host;
377 USHORT port;
378 HRESULT hr;
380 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
381 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
383 heap_free( host );
384 return WS_E_INVALID_ENDPOINT_URL;
387 winsock_init();
389 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
390 heap_free( host );
391 if (hr != S_OK) return hr;
393 if ((listener->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
394 return HRESULT_FROM_WIN32( WSAGetLastError() );
396 if (bind( listener->u.udp.socket, addr, addr_len ) < 0)
398 closesocket( listener->u.udp.socket );
399 listener->u.udp.socket = -1;
400 return HRESULT_FROM_WIN32( WSAGetLastError() );
403 listener->state = WS_LISTENER_STATE_OPEN;
404 return S_OK;
407 static HRESULT open_listener( struct listener *listener, const WS_STRING *url )
409 switch (listener->binding)
411 case WS_TCP_CHANNEL_BINDING:
412 return open_listener_tcp( listener, url );
414 case WS_UDP_CHANNEL_BINDING:
415 return open_listener_udp( listener, url );
417 default:
418 ERR( "unhandled binding %u\n", listener->binding );
419 return E_NOTIMPL;
423 /**************************************************************************
424 * WsOpenListener [webservices.@]
426 HRESULT WINAPI WsOpenListener( WS_LISTENER *handle, WS_STRING *url, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
428 struct listener *listener = (struct listener *)handle;
429 HRESULT hr;
431 TRACE( "%p %s %p %p\n", handle, url ? debugstr_wn(url->chars, url->length) : "null", ctx, error );
432 if (error) FIXME( "ignoring error parameter\n" );
433 if (ctx) FIXME( "ignoring ctx parameter\n" );
435 if (!listener || !url) return E_INVALIDARG;
437 EnterCriticalSection( &listener->cs );
439 if (listener->magic != LISTENER_MAGIC)
441 LeaveCriticalSection( &listener->cs );
442 return E_INVALIDARG;
445 if (listener->state != WS_LISTENER_STATE_CREATED)
447 LeaveCriticalSection( &listener->cs );
448 return WS_E_INVALID_OPERATION;
451 hr = open_listener( listener, url );
453 LeaveCriticalSection( &listener->cs );
454 return hr;
457 static void close_listener( struct listener *listener )
459 reset_listener( listener );
460 listener->state = WS_LISTENER_STATE_CLOSED;
463 /**************************************************************************
464 * WsCloseListener [webservices.@]
466 HRESULT WINAPI WsCloseListener( WS_LISTENER *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
468 struct listener *listener = (struct listener *)handle;
470 TRACE( "%p %p %p\n", handle, ctx, error );
471 if (error) FIXME( "ignoring error parameter\n" );
472 if (ctx) FIXME( "ignoring ctx parameter\n" );
474 if (!listener) return E_INVALIDARG;
476 EnterCriticalSection( &listener->cs );
478 if (listener->magic != LISTENER_MAGIC)
480 LeaveCriticalSection( &listener->cs );
481 return E_INVALIDARG;
484 close_listener( listener );
486 LeaveCriticalSection( &listener->cs );
487 return S_OK;
490 /**************************************************************************
491 * WsResetListener [webservices.@]
493 HRESULT WINAPI WsResetListener( WS_LISTENER *handle, WS_ERROR *error )
495 struct listener *listener = (struct listener *)handle;
497 TRACE( "%p %p\n", handle, error );
498 if (error) FIXME( "ignoring error parameter\n" );
500 if (!listener) return E_INVALIDARG;
502 EnterCriticalSection( &listener->cs );
504 if (listener->magic != LISTENER_MAGIC)
506 LeaveCriticalSection( &listener->cs );
507 return E_INVALIDARG;
510 if (listener->state != WS_LISTENER_STATE_CREATED && listener->state != WS_LISTENER_STATE_CLOSED)
512 LeaveCriticalSection( &listener->cs );
513 return WS_E_INVALID_OPERATION;
516 reset_listener( listener );
518 LeaveCriticalSection( &listener->cs );
519 return S_OK;
522 /**************************************************************************
523 * WsGetListenerProperty [webservices.@]
525 HRESULT WINAPI WsGetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, void *buf,
526 ULONG size, WS_ERROR *error )
528 struct listener *listener = (struct listener *)handle;
529 HRESULT hr = S_OK;
531 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
532 if (error) FIXME( "ignoring error parameter\n" );
534 if (!listener) return E_INVALIDARG;
536 EnterCriticalSection( &listener->cs );
538 if (listener->magic != LISTENER_MAGIC)
540 LeaveCriticalSection( &listener->cs );
541 return E_INVALIDARG;
544 switch (id)
546 case WS_LISTENER_PROPERTY_STATE:
547 if (!buf || size != sizeof(listener->state)) hr = E_INVALIDARG;
548 else *(WS_LISTENER_STATE *)buf = listener->state;
549 break;
551 case WS_LISTENER_PROPERTY_CHANNEL_TYPE:
552 if (!buf || size != sizeof(listener->type)) hr = E_INVALIDARG;
553 else *(WS_CHANNEL_TYPE *)buf = listener->type;
554 break;
556 case WS_LISTENER_PROPERTY_CHANNEL_BINDING:
557 if (!buf || size != sizeof(listener->binding)) hr = E_INVALIDARG;
558 else *(WS_CHANNEL_BINDING *)buf = listener->binding;
559 break;
561 default:
562 hr = prop_get( listener->prop, listener->prop_count, id, buf, size );
565 LeaveCriticalSection( &listener->cs );
566 return hr;
569 /**************************************************************************
570 * WsSetListenerProperty [webservices.@]
572 HRESULT WINAPI WsSetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, const void *value,
573 ULONG size, WS_ERROR *error )
575 struct listener *listener = (struct listener *)handle;
576 HRESULT hr;
578 TRACE( "%p %u %p %u\n", handle, id, value, size );
579 if (error) FIXME( "ignoring error parameter\n" );
581 if (!listener) return E_INVALIDARG;
583 EnterCriticalSection( &listener->cs );
585 if (listener->magic != LISTENER_MAGIC)
587 LeaveCriticalSection( &listener->cs );
588 return E_INVALIDARG;
591 hr = prop_set( listener->prop, listener->prop_count, id, value, size );
593 LeaveCriticalSection( &listener->cs );
594 return hr;
597 /**************************************************************************
598 * WsAcceptChannel [webservices.@]
600 HRESULT WINAPI WsAcceptChannel( WS_LISTENER *handle, WS_CHANNEL *channel_handle, const WS_ASYNC_CONTEXT *ctx,
601 WS_ERROR *error )
603 struct listener *listener = (struct listener *)handle;
604 HRESULT hr = E_NOTIMPL;
605 HANDLE wait, cancel;
607 TRACE( "%p %p %p %p\n", handle, channel_handle, ctx, error );
608 if (error) FIXME( "ignoring error parameter\n" );
609 if (ctx) FIXME( "ignoring ctx parameter\n" );
611 if (!listener || !channel_handle) return E_INVALIDARG;
613 EnterCriticalSection( &listener->cs );
615 if (listener->magic != LISTENER_MAGIC)
617 LeaveCriticalSection( &listener->cs );
618 return E_INVALIDARG;
621 if (listener->state != WS_LISTENER_STATE_OPEN || listener->channel)
623 LeaveCriticalSection( &listener->cs );
624 return WS_E_INVALID_OPERATION;
627 wait = listener->wait;
628 cancel = listener->cancel;
629 listener->channel = channel_handle;
631 switch (listener->binding)
633 case WS_TCP_CHANNEL_BINDING:
635 SOCKET socket = listener->u.tcp.socket;
637 LeaveCriticalSection( &listener->cs );
638 return channel_accept_tcp( socket, wait, cancel, channel_handle );
640 case WS_UDP_CHANNEL_BINDING:
642 SOCKET socket = listener->u.udp.socket;
644 LeaveCriticalSection( &listener->cs );
645 return channel_accept_udp( socket, wait, cancel, channel_handle );
647 default:
648 FIXME( "listener binding %u not supported\n", listener->binding );
649 break;
652 LeaveCriticalSection( &listener->cs );
653 return hr;