rasapi32: Update spec file.
[wine/zf.git] / dlls / webservices / listener.c
blobd41b64b97491740e61cddf63c8242c8cb5f573cf
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/heap.h"
28 #include "wine/list.h"
29 #include "webservices_private.h"
30 #include "sock.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
34 HINSTANCE webservices_instance;
36 static BOOL winsock_loaded;
38 static BOOL WINAPI winsock_startup( INIT_ONCE *once, void *param, void **ctx )
40 int ret;
41 WSADATA data;
42 if (!(ret = WSAStartup( MAKEWORD(1,1), &data ))) winsock_loaded = TRUE;
43 else ERR( "WSAStartup failed: %d\n", ret );
44 return TRUE;
47 void winsock_init(void)
49 static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
50 InitOnceExecuteOnce( &once, winsock_startup, NULL, NULL );
53 _locale_t c_locale;
55 /******************************************************************
56 * DllMain (webservices.@)
58 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, void *reserved )
60 switch (reason)
62 case DLL_PROCESS_ATTACH:
63 webservices_instance = hinst;
64 DisableThreadLibraryCalls( hinst );
65 c_locale = _create_locale( LC_ALL, "C" );
66 break;
68 case DLL_PROCESS_DETACH:
69 if (reserved) break;
70 if (winsock_loaded) WSACleanup();
71 _free_locale( c_locale );
72 break;
74 return TRUE;
77 static const struct prop_desc listener_props[] =
79 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_LISTEN_BACKLOG */
80 { sizeof(WS_IP_VERSION), FALSE }, /* WS_LISTENER_PROPERTY_IP_VERSION */
81 { sizeof(WS_LISTENER_STATE), TRUE }, /* WS_LISTENER_PROPERTY_STATE */
82 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_LISTENER_PROPERTY_ASYNC_CALLBACK_MODEL */
83 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_TYPE */
84 { sizeof(WS_CHANNEL_BINDING), TRUE }, /* WS_LISTENER_PROPERTY_CHANNEL_BINDING */
85 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CONNECT_TIMEOUT */
86 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_IS_MULTICAST */
87 { 0, FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_INTERFACES */
88 { sizeof(BOOL), FALSE }, /* WS_LISTENER_PROPERTY_MULTICAST_LOOPBACK */
89 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_CLOSE_TIMEOUT */
90 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TO_HEADER_MATCHING_OPTIONS */
91 { sizeof(ULONG), FALSE }, /* WS_LISTENER_PROPERTY_TRANSPORT_URL_MATCHING_OPTIONS */
92 { sizeof(WS_CUSTOM_LISTENER_CALLBACKS), FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_CALLBACKS */
93 { 0, FALSE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_PARAMETERS */
94 { sizeof(void *), TRUE }, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_INSTANCE */
95 { sizeof(WS_DISALLOWED_USER_AGENT_SUBSTRINGS), FALSE } /* WS_LISTENER_PROPERTY_DISALLOWED_USER_AGENT */
98 struct listener
100 ULONG magic;
101 CRITICAL_SECTION cs;
102 WS_CHANNEL_TYPE type;
103 WS_CHANNEL_BINDING binding;
104 WS_LISTENER_STATE state;
105 HANDLE wait;
106 HANDLE cancel;
107 WS_CHANNEL *channel;
108 union
110 struct
112 SOCKET socket;
113 } tcp;
114 struct
116 SOCKET socket;
117 } udp;
118 } u;
119 ULONG prop_count;
120 struct prop prop[ARRAY_SIZE( listener_props )];
123 #define LISTENER_MAGIC (('L' << 24) | ('I' << 16) | ('S' << 8) | 'T')
125 static struct listener *alloc_listener(void)
127 static const ULONG count = ARRAY_SIZE( listener_props );
128 struct listener *ret;
129 ULONG size = sizeof(*ret) + prop_size( listener_props, count );
131 if (!(ret = heap_alloc_zero( size ))) return NULL;
133 ret->magic = LISTENER_MAGIC;
134 if (!(ret->wait = CreateEventW( NULL, FALSE, FALSE, NULL )))
136 heap_free( ret );
137 return NULL;
139 if (!(ret->cancel = CreateEventW( NULL, FALSE, FALSE, NULL )))
141 CloseHandle( ret->wait );
142 heap_free( ret );
143 return NULL;
145 InitializeCriticalSection( &ret->cs );
146 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": listener.cs");
148 prop_init( listener_props, count, ret->prop, &ret[1] );
149 ret->prop_count = count;
150 return ret;
153 static void reset_listener( struct listener *listener )
155 listener->state = WS_LISTENER_STATE_CREATED;
156 SetEvent( listener->cancel );
157 listener->channel = NULL;
159 switch (listener->binding)
161 case WS_TCP_CHANNEL_BINDING:
162 closesocket( listener->u.tcp.socket );
163 listener->u.tcp.socket = -1;
164 break;
166 case WS_UDP_CHANNEL_BINDING:
167 closesocket( listener->u.udp.socket );
168 listener->u.udp.socket = -1;
169 break;
171 default: break;
175 static void free_listener( struct listener *listener )
177 reset_listener( listener );
179 CloseHandle( listener->wait );
180 CloseHandle( listener->cancel );
182 listener->cs.DebugInfo->Spare[0] = 0;
183 DeleteCriticalSection( &listener->cs );
184 heap_free( listener );
187 static HRESULT create_listener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
188 const WS_LISTENER_PROPERTY *properties, ULONG count, struct listener **ret )
190 struct listener *listener;
191 HRESULT hr;
192 ULONG i;
194 if (!(listener = alloc_listener())) return E_OUTOFMEMORY;
196 for (i = 0; i < count; i++)
198 hr = prop_set( listener->prop, listener->prop_count, properties[i].id, properties[i].value,
199 properties[i].valueSize );
200 if (hr != S_OK)
202 free_listener( listener );
203 return hr;
207 listener->type = type;
208 listener->binding = binding;
210 switch (listener->binding)
212 case WS_TCP_CHANNEL_BINDING:
213 listener->u.tcp.socket = -1;
214 break;
216 case WS_UDP_CHANNEL_BINDING:
217 listener->u.udp.socket = -1;
218 break;
220 default: break;
223 *ret = listener;
224 return S_OK;
227 /**************************************************************************
228 * WsCreateListener [webservices.@]
230 HRESULT WINAPI WsCreateListener( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
231 const WS_LISTENER_PROPERTY *properties, ULONG count,
232 const WS_SECURITY_DESCRIPTION *desc, WS_LISTENER **handle,
233 WS_ERROR *error )
235 struct listener *listener;
236 HRESULT hr;
238 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
239 if (error) FIXME( "ignoring error parameter\n" );
240 if (desc) FIXME( "ignoring security description\n" );
242 if (!handle) return E_INVALIDARG;
244 if (type != WS_CHANNEL_TYPE_DUPLEX_SESSION && type != WS_CHANNEL_TYPE_DUPLEX)
246 FIXME( "channel type %u not implemented\n", type );
247 return E_NOTIMPL;
249 if (binding != WS_TCP_CHANNEL_BINDING && binding != WS_UDP_CHANNEL_BINDING)
251 FIXME( "channel binding %u not implemented\n", binding );
252 return E_NOTIMPL;
255 if ((hr = create_listener( type, binding, properties, count, &listener )) != S_OK) return hr;
257 TRACE( "created %p\n", listener );
258 *handle = (WS_LISTENER *)listener;
259 return S_OK;
262 /**************************************************************************
263 * WsFreeListener [webservices.@]
265 void WINAPI WsFreeListener( WS_LISTENER *handle )
267 struct listener *listener = (struct listener *)handle;
269 TRACE( "%p\n", handle );
271 if (!listener) return;
273 EnterCriticalSection( &listener->cs );
275 if (listener->magic != LISTENER_MAGIC)
277 LeaveCriticalSection( &listener->cs );
278 return;
281 listener->magic = 0;
283 LeaveCriticalSection( &listener->cs );
284 free_listener( listener );
287 HRESULT resolve_hostname( const WCHAR *host, USHORT port, struct sockaddr *addr, int *addr_len, int flags )
289 WCHAR service[6];
290 ADDRINFOW hints, *res, *info;
291 HRESULT hr = WS_E_ADDRESS_NOT_AVAILABLE;
293 memset( &hints, 0, sizeof(hints) );
294 hints.ai_flags = flags;
295 hints.ai_family = AF_INET;
297 *addr_len = 0;
298 swprintf( service, ARRAY_SIZE(service), L"%u", port );
299 if (GetAddrInfoW( host, service, &hints, &res )) return HRESULT_FROM_WIN32( WSAGetLastError() );
301 info = res;
302 while (info && info->ai_family != AF_INET) info = info->ai_next;
303 if (info)
305 memcpy( addr, info->ai_addr, info->ai_addrlen );
306 *addr_len = info->ai_addrlen;
307 hr = S_OK;
310 FreeAddrInfoW( res );
311 return hr;
314 HRESULT parse_url( const WS_STRING *str, WS_URL_SCHEME_TYPE *scheme, WCHAR **host, USHORT *port )
316 WS_HEAP *heap;
317 WS_NETTCP_URL *url;
318 HRESULT hr;
320 if ((hr = WsCreateHeap( 1 << 8, 0, NULL, 0, &heap, NULL )) != S_OK) return hr;
321 if ((hr = WsDecodeUrl( str, 0, heap, (WS_URL **)&url, NULL )) != S_OK)
323 WsFreeHeap( heap );
324 return hr;
327 if (url->host.length == 1 && (url->host.chars[0] == '+' || url->host.chars[0] == '*')) *host = NULL;
328 else
330 if (!(*host = heap_alloc( (url->host.length + 1) * sizeof(WCHAR) )))
332 WsFreeHeap( heap );
333 return E_OUTOFMEMORY;
335 memcpy( *host, url->host.chars, url->host.length * sizeof(WCHAR) );
336 (*host)[url->host.length] = 0;
338 *scheme = url->url.scheme;
339 *port = url->port;
341 WsFreeHeap( heap );
342 return hr;
345 static HRESULT open_listener_tcp( struct listener *listener, const WS_STRING *url )
347 struct sockaddr_storage storage;
348 struct sockaddr *addr = (struct sockaddr *)&storage;
349 int addr_len, on = 1;
350 WS_URL_SCHEME_TYPE scheme;
351 WCHAR *host;
352 USHORT port;
353 HRESULT hr;
355 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
356 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
358 heap_free( host );
359 return WS_E_INVALID_ENDPOINT_URL;
362 winsock_init();
364 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
365 heap_free( host );
366 if (hr != S_OK) return hr;
368 if ((listener->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
369 return HRESULT_FROM_WIN32( WSAGetLastError() );
371 if (setsockopt( listener->u.tcp.socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) ) < 0)
373 closesocket( listener->u.tcp.socket );
374 listener->u.tcp.socket = -1;
375 return HRESULT_FROM_WIN32( WSAGetLastError() );
378 if (bind( listener->u.tcp.socket, addr, addr_len ) < 0)
380 closesocket( listener->u.tcp.socket );
381 listener->u.tcp.socket = -1;
382 return HRESULT_FROM_WIN32( WSAGetLastError() );
385 if (listen( listener->u.tcp.socket, 0 ) < 0)
387 closesocket( listener->u.tcp.socket );
388 listener->u.tcp.socket = -1;
389 return HRESULT_FROM_WIN32( WSAGetLastError() );
392 listener->state = WS_LISTENER_STATE_OPEN;
393 return S_OK;
396 static HRESULT open_listener_udp( struct listener *listener, const WS_STRING *url )
398 struct sockaddr_storage storage;
399 struct sockaddr *addr = (struct sockaddr *)&storage;
400 int addr_len;
401 WS_URL_SCHEME_TYPE scheme;
402 WCHAR *host;
403 USHORT port;
404 HRESULT hr;
406 if ((hr = parse_url( url, &scheme, &host, &port )) != S_OK) return hr;
407 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
409 heap_free( host );
410 return WS_E_INVALID_ENDPOINT_URL;
413 winsock_init();
415 hr = resolve_hostname( host, port, addr, &addr_len, AI_PASSIVE );
416 heap_free( host );
417 if (hr != S_OK) return hr;
419 if ((listener->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
420 return HRESULT_FROM_WIN32( WSAGetLastError() );
422 if (bind( listener->u.udp.socket, addr, addr_len ) < 0)
424 closesocket( listener->u.udp.socket );
425 listener->u.udp.socket = -1;
426 return HRESULT_FROM_WIN32( WSAGetLastError() );
429 listener->state = WS_LISTENER_STATE_OPEN;
430 return S_OK;
433 static HRESULT open_listener( struct listener *listener, const WS_STRING *url )
435 switch (listener->binding)
437 case WS_TCP_CHANNEL_BINDING:
438 return open_listener_tcp( listener, url );
440 case WS_UDP_CHANNEL_BINDING:
441 return open_listener_udp( listener, url );
443 default:
444 ERR( "unhandled binding %u\n", listener->binding );
445 return E_NOTIMPL;
449 /**************************************************************************
450 * WsOpenListener [webservices.@]
452 HRESULT WINAPI WsOpenListener( WS_LISTENER *handle, WS_STRING *url, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
454 struct listener *listener = (struct listener *)handle;
455 HRESULT hr;
457 TRACE( "%p %s %p %p\n", handle, url ? debugstr_wn(url->chars, url->length) : "null", ctx, error );
458 if (error) FIXME( "ignoring error parameter\n" );
459 if (ctx) FIXME( "ignoring ctx parameter\n" );
461 if (!listener || !url) return E_INVALIDARG;
463 EnterCriticalSection( &listener->cs );
465 if (listener->magic != LISTENER_MAGIC)
467 LeaveCriticalSection( &listener->cs );
468 return E_INVALIDARG;
471 if (listener->state != WS_LISTENER_STATE_CREATED) hr = WS_E_INVALID_OPERATION;
472 else hr = open_listener( listener, url );
474 LeaveCriticalSection( &listener->cs );
475 TRACE( "returning %08x\n", hr );
476 return hr;
479 static void close_listener( struct listener *listener )
481 reset_listener( listener );
482 listener->state = WS_LISTENER_STATE_CLOSED;
485 /**************************************************************************
486 * WsCloseListener [webservices.@]
488 HRESULT WINAPI WsCloseListener( WS_LISTENER *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
490 struct listener *listener = (struct listener *)handle;
491 HRESULT hr = S_OK;
493 TRACE( "%p %p %p\n", handle, ctx, error );
494 if (error) FIXME( "ignoring error parameter\n" );
495 if (ctx) FIXME( "ignoring ctx parameter\n" );
497 if (!listener) return E_INVALIDARG;
499 EnterCriticalSection( &listener->cs );
501 if (listener->magic != LISTENER_MAGIC)
503 LeaveCriticalSection( &listener->cs );
504 return E_INVALIDARG;
507 close_listener( listener );
509 LeaveCriticalSection( &listener->cs );
510 TRACE( "returning %08x\n", hr );
511 return hr;
514 /**************************************************************************
515 * WsResetListener [webservices.@]
517 HRESULT WINAPI WsResetListener( WS_LISTENER *handle, WS_ERROR *error )
519 struct listener *listener = (struct listener *)handle;
520 HRESULT hr = S_OK;
522 TRACE( "%p %p\n", handle, error );
523 if (error) FIXME( "ignoring error parameter\n" );
525 if (!listener) return E_INVALIDARG;
527 EnterCriticalSection( &listener->cs );
529 if (listener->magic != LISTENER_MAGIC)
531 LeaveCriticalSection( &listener->cs );
532 return E_INVALIDARG;
535 if (listener->state != WS_LISTENER_STATE_CREATED && listener->state != WS_LISTENER_STATE_CLOSED)
536 hr = WS_E_INVALID_OPERATION;
537 else
538 reset_listener( listener );
540 LeaveCriticalSection( &listener->cs );
541 TRACE( "returning %08x\n", hr );
542 return hr;
545 /**************************************************************************
546 * WsGetListenerProperty [webservices.@]
548 HRESULT WINAPI WsGetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, void *buf,
549 ULONG size, WS_ERROR *error )
551 struct listener *listener = (struct listener *)handle;
552 HRESULT hr = S_OK;
554 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
555 if (error) FIXME( "ignoring error parameter\n" );
557 if (!listener) return E_INVALIDARG;
559 EnterCriticalSection( &listener->cs );
561 if (listener->magic != LISTENER_MAGIC)
563 LeaveCriticalSection( &listener->cs );
564 return E_INVALIDARG;
567 switch (id)
569 case WS_LISTENER_PROPERTY_STATE:
570 if (!buf || size != sizeof(listener->state)) hr = E_INVALIDARG;
571 else *(WS_LISTENER_STATE *)buf = listener->state;
572 break;
574 case WS_LISTENER_PROPERTY_CHANNEL_TYPE:
575 if (!buf || size != sizeof(listener->type)) hr = E_INVALIDARG;
576 else *(WS_CHANNEL_TYPE *)buf = listener->type;
577 break;
579 case WS_LISTENER_PROPERTY_CHANNEL_BINDING:
580 if (!buf || size != sizeof(listener->binding)) hr = E_INVALIDARG;
581 else *(WS_CHANNEL_BINDING *)buf = listener->binding;
582 break;
584 default:
585 hr = prop_get( listener->prop, listener->prop_count, id, buf, size );
588 LeaveCriticalSection( &listener->cs );
589 TRACE( "returning %08x\n", hr );
590 return hr;
593 /**************************************************************************
594 * WsSetListenerProperty [webservices.@]
596 HRESULT WINAPI WsSetListenerProperty( WS_LISTENER *handle, WS_LISTENER_PROPERTY_ID id, const void *value,
597 ULONG size, WS_ERROR *error )
599 struct listener *listener = (struct listener *)handle;
600 HRESULT hr;
602 TRACE( "%p %u %p %u\n", handle, id, value, size );
603 if (error) FIXME( "ignoring error parameter\n" );
605 if (!listener) return E_INVALIDARG;
607 EnterCriticalSection( &listener->cs );
609 if (listener->magic != LISTENER_MAGIC)
611 LeaveCriticalSection( &listener->cs );
612 return E_INVALIDARG;
615 hr = prop_set( listener->prop, listener->prop_count, id, value, size );
617 LeaveCriticalSection( &listener->cs );
618 TRACE( "returning %08x\n", hr );
619 return hr;
622 /**************************************************************************
623 * WsAcceptChannel [webservices.@]
625 HRESULT WINAPI WsAcceptChannel( WS_LISTENER *handle, WS_CHANNEL *channel_handle, const WS_ASYNC_CONTEXT *ctx,
626 WS_ERROR *error )
628 struct listener *listener = (struct listener *)handle;
629 HRESULT hr = E_NOTIMPL;
630 HANDLE wait, cancel;
632 TRACE( "%p %p %p %p\n", handle, channel_handle, ctx, error );
633 if (error) FIXME( "ignoring error parameter\n" );
634 if (ctx) FIXME( "ignoring ctx parameter\n" );
636 if (!listener || !channel_handle) return E_INVALIDARG;
638 EnterCriticalSection( &listener->cs );
640 if (listener->magic != LISTENER_MAGIC)
642 LeaveCriticalSection( &listener->cs );
643 return E_INVALIDARG;
646 if (listener->state != WS_LISTENER_STATE_OPEN || (listener->channel && listener->channel != channel_handle))
648 hr = WS_E_INVALID_OPERATION;
650 else
652 wait = listener->wait;
653 cancel = listener->cancel;
654 listener->channel = channel_handle;
656 switch (listener->binding)
658 case WS_TCP_CHANNEL_BINDING:
660 SOCKET socket = listener->u.tcp.socket;
662 LeaveCriticalSection( &listener->cs );
663 hr = channel_accept_tcp( socket, wait, cancel, channel_handle );
664 TRACE( "returning %08x\n", hr );
665 return hr;
667 case WS_UDP_CHANNEL_BINDING:
669 SOCKET socket = listener->u.udp.socket;
671 LeaveCriticalSection( &listener->cs );
672 hr = channel_accept_udp( socket, wait, cancel, channel_handle );
673 TRACE( "returning %08x\n", hr );
674 return hr;
676 default:
677 FIXME( "listener binding %u not supported\n", listener->binding );
678 break;
682 LeaveCriticalSection( &listener->cs );
683 TRACE( "returning %08x\n", hr );
684 return hr;