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
23 #include "webservices.h"
25 #include "wine/debug.h"
26 #include "wine/list.h"
27 #include "wine/unicode.h"
28 #include "webservices_private.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
)
39 if (!(ret
= WSAStartup( MAKEWORD(1,1), &data
))) winsock_loaded
= TRUE
;
40 else ERR( "WSAStartup failed: %d\n", ret
);
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 */
76 WS_CHANNEL_BINDING binding
;
77 WS_LISTENER_STATE state
;
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
)))
112 if (!(ret
->cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
114 CloseHandle( ret
->wait
);
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
;
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;
139 case WS_UDP_CHANNEL_BINDING
:
140 closesocket( listener
->u
.udp
.socket
);
141 listener
->u
.udp
.socket
= -1;
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
;
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
);
175 free_listener( listener
);
180 listener
->type
= type
;
181 listener
->binding
= binding
;
183 switch (listener
->binding
)
185 case WS_TCP_CHANNEL_BINDING
:
186 listener
->u
.tcp
.socket
= -1;
189 case WS_UDP_CHANNEL_BINDING
:
190 listener
->u
.udp
.socket
= -1;
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
,
208 struct listener
*listener
;
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
);
222 if (binding
!= WS_TCP_CHANNEL_BINDING
&& binding
!= WS_UDP_CHANNEL_BINDING
)
224 FIXME( "channel binding %u not implemented\n", binding
);
228 if ((hr
= create_listener( type
, binding
, properties
, count
, &listener
)) != S_OK
) return hr
;
230 *handle
= (WS_LISTENER
*)listener
;
234 /**************************************************************************
235 * WsFreeListener [webservices.@]
237 void WINAPI
WsFreeListener( WS_LISTENER
*handle
)
239 struct listener
*listener
= (struct listener
*)handle
;
241 TRACE( "%p\n", handle
);
243 if (!listener
) return;
245 EnterCriticalSection( &listener
->cs
);
247 if (listener
->magic
!= LISTENER_MAGIC
)
249 LeaveCriticalSection( &listener
->cs
);
255 LeaveCriticalSection( &listener
->cs
);
256 free_listener( listener
);
259 HRESULT
resolve_hostname( const WCHAR
*host
, USHORT port
, struct sockaddr
*addr
, int *addr_len
, int flags
)
261 static const WCHAR fmtW
[] = {'%','u',0};
263 ADDRINFOW hints
, *res
, *info
;
264 HRESULT hr
= WS_E_ADDRESS_NOT_AVAILABLE
;
266 memset( &hints
, 0, sizeof(hints
) );
267 hints
.ai_flags
= flags
;
268 hints
.ai_family
= AF_INET
;
271 sprintfW( service
, fmtW
, port
);
272 if (GetAddrInfoW( host
, service
, &hints
, &res
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
275 while (info
&& info
->ai_family
!= AF_INET
) info
= info
->ai_next
;
278 memcpy( addr
, info
->ai_addr
, info
->ai_addrlen
);
279 *addr_len
= info
->ai_addrlen
;
283 FreeAddrInfoW( res
);
287 HRESULT
parse_url( const WS_STRING
*str
, WS_URL_SCHEME_TYPE
*scheme
, WCHAR
**host
, USHORT
*port
)
293 if ((hr
= WsCreateHeap( 1 << 8, 0, NULL
, 0, &heap
, NULL
)) != S_OK
) return hr
;
294 if ((hr
= WsDecodeUrl( str
, 0, heap
, (WS_URL
**)&url
, NULL
)) != S_OK
)
300 if (url
->host
.length
== 1 && (url
->host
.chars
[0] == '+' || url
->host
.chars
[0] == '*')) *host
= NULL
;
303 if (!(*host
= heap_alloc( (url
->host
.length
+ 1) * sizeof(WCHAR
) )))
306 return E_OUTOFMEMORY
;
308 memcpy( *host
, url
->host
.chars
, url
->host
.length
* sizeof(WCHAR
) );
309 (*host
)[url
->host
.length
] = 0;
311 *scheme
= url
->url
.scheme
;
318 static HRESULT
open_listener_tcp( struct listener
*listener
, const WS_STRING
*url
)
320 struct sockaddr_storage storage
;
321 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
322 int addr_len
, on
= 1;
323 WS_URL_SCHEME_TYPE scheme
;
328 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
329 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
332 return WS_E_INVALID_ENDPOINT_URL
;
337 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
339 if (hr
!= S_OK
) return hr
;
341 if ((listener
->u
.tcp
.socket
= socket( addr
->sa_family
, SOCK_STREAM
, 0 )) == -1)
342 return HRESULT_FROM_WIN32( WSAGetLastError() );
344 if (setsockopt( listener
->u
.tcp
.socket
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof(on
) ) < 0)
346 closesocket( listener
->u
.tcp
.socket
);
347 listener
->u
.tcp
.socket
= -1;
348 return HRESULT_FROM_WIN32( WSAGetLastError() );
351 if (bind( listener
->u
.tcp
.socket
, addr
, addr_len
) < 0)
353 closesocket( listener
->u
.tcp
.socket
);
354 listener
->u
.tcp
.socket
= -1;
355 return HRESULT_FROM_WIN32( WSAGetLastError() );
358 if (listen( listener
->u
.tcp
.socket
, 0 ) < 0)
360 closesocket( listener
->u
.tcp
.socket
);
361 listener
->u
.tcp
.socket
= -1;
362 return HRESULT_FROM_WIN32( WSAGetLastError() );
365 listener
->state
= WS_LISTENER_STATE_OPEN
;
369 static HRESULT
open_listener_udp( struct listener
*listener
, const WS_STRING
*url
)
371 struct sockaddr_storage storage
;
372 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
374 WS_URL_SCHEME_TYPE scheme
;
379 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
380 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
383 return WS_E_INVALID_ENDPOINT_URL
;
388 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
390 if (hr
!= S_OK
) return hr
;
392 if ((listener
->u
.udp
.socket
= socket( addr
->sa_family
, SOCK_DGRAM
, 0 )) == -1)
393 return HRESULT_FROM_WIN32( WSAGetLastError() );
395 if (bind( listener
->u
.udp
.socket
, addr
, addr_len
) < 0)
397 closesocket( listener
->u
.udp
.socket
);
398 listener
->u
.udp
.socket
= -1;
399 return HRESULT_FROM_WIN32( WSAGetLastError() );
402 listener
->state
= WS_LISTENER_STATE_OPEN
;
406 static HRESULT
open_listener( struct listener
*listener
, const WS_STRING
*url
)
408 switch (listener
->binding
)
410 case WS_TCP_CHANNEL_BINDING
:
411 return open_listener_tcp( listener
, url
);
413 case WS_UDP_CHANNEL_BINDING
:
414 return open_listener_udp( listener
, url
);
417 ERR( "unhandled binding %u\n", listener
->binding
);
422 /**************************************************************************
423 * WsOpenListener [webservices.@]
425 HRESULT WINAPI
WsOpenListener( WS_LISTENER
*handle
, WS_STRING
*url
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
427 struct listener
*listener
= (struct listener
*)handle
;
430 TRACE( "%p %s %p %p\n", handle
, url
? debugstr_wn(url
->chars
, url
->length
) : "null", ctx
, error
);
431 if (error
) FIXME( "ignoring error parameter\n" );
432 if (ctx
) FIXME( "ignoring ctx parameter\n" );
434 if (!listener
|| !url
) return E_INVALIDARG
;
436 EnterCriticalSection( &listener
->cs
);
438 if (listener
->magic
!= LISTENER_MAGIC
)
440 LeaveCriticalSection( &listener
->cs
);
444 if (listener
->state
!= WS_LISTENER_STATE_CREATED
)
446 LeaveCriticalSection( &listener
->cs
);
447 return WS_E_INVALID_OPERATION
;
450 hr
= open_listener( listener
, url
);
452 LeaveCriticalSection( &listener
->cs
);
456 static void close_listener( struct listener
*listener
)
458 reset_listener( listener
);
459 listener
->state
= WS_LISTENER_STATE_CLOSED
;
462 /**************************************************************************
463 * WsCloseListener [webservices.@]
465 HRESULT WINAPI
WsCloseListener( WS_LISTENER
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
467 struct listener
*listener
= (struct listener
*)handle
;
469 TRACE( "%p %p %p\n", handle
, ctx
, error
);
470 if (error
) FIXME( "ignoring error parameter\n" );
471 if (ctx
) FIXME( "ignoring ctx parameter\n" );
473 if (!listener
) return E_INVALIDARG
;
475 EnterCriticalSection( &listener
->cs
);
477 if (listener
->magic
!= LISTENER_MAGIC
)
479 LeaveCriticalSection( &listener
->cs
);
483 close_listener( listener
);
485 LeaveCriticalSection( &listener
->cs
);
489 /**************************************************************************
490 * WsResetListener [webservices.@]
492 HRESULT WINAPI
WsResetListener( WS_LISTENER
*handle
, WS_ERROR
*error
)
494 struct listener
*listener
= (struct listener
*)handle
;
496 TRACE( "%p %p\n", handle
, error
);
497 if (error
) FIXME( "ignoring error parameter\n" );
499 if (!listener
) return E_INVALIDARG
;
501 EnterCriticalSection( &listener
->cs
);
503 if (listener
->magic
!= LISTENER_MAGIC
)
505 LeaveCriticalSection( &listener
->cs
);
509 if (listener
->state
!= WS_LISTENER_STATE_CREATED
&& listener
->state
!= WS_LISTENER_STATE_CLOSED
)
511 LeaveCriticalSection( &listener
->cs
);
512 return WS_E_INVALID_OPERATION
;
515 reset_listener( listener
);
517 LeaveCriticalSection( &listener
->cs
);
521 /**************************************************************************
522 * WsGetListenerProperty [webservices.@]
524 HRESULT WINAPI
WsGetListenerProperty( WS_LISTENER
*handle
, WS_LISTENER_PROPERTY_ID id
, void *buf
,
525 ULONG size
, WS_ERROR
*error
)
527 struct listener
*listener
= (struct listener
*)handle
;
530 TRACE( "%p %u %p %u %p\n", handle
, id
, buf
, size
, error
);
531 if (error
) FIXME( "ignoring error parameter\n" );
533 if (!listener
) return E_INVALIDARG
;
535 EnterCriticalSection( &listener
->cs
);
537 if (listener
->magic
!= LISTENER_MAGIC
)
539 LeaveCriticalSection( &listener
->cs
);
545 case WS_LISTENER_PROPERTY_STATE
:
546 if (!buf
|| size
!= sizeof(listener
->state
)) hr
= E_INVALIDARG
;
547 else *(WS_LISTENER_STATE
*)buf
= listener
->state
;
550 case WS_LISTENER_PROPERTY_CHANNEL_TYPE
:
551 if (!buf
|| size
!= sizeof(listener
->type
)) hr
= E_INVALIDARG
;
552 else *(WS_CHANNEL_TYPE
*)buf
= listener
->type
;
555 case WS_LISTENER_PROPERTY_CHANNEL_BINDING
:
556 if (!buf
|| size
!= sizeof(listener
->binding
)) hr
= E_INVALIDARG
;
557 else *(WS_CHANNEL_BINDING
*)buf
= listener
->binding
;
561 hr
= prop_get( listener
->prop
, listener
->prop_count
, id
, buf
, size
);
564 LeaveCriticalSection( &listener
->cs
);
568 /**************************************************************************
569 * WsSetListenerProperty [webservices.@]
571 HRESULT WINAPI
WsSetListenerProperty( WS_LISTENER
*handle
, WS_LISTENER_PROPERTY_ID id
, const void *value
,
572 ULONG size
, WS_ERROR
*error
)
574 struct listener
*listener
= (struct listener
*)handle
;
577 TRACE( "%p %u %p %u\n", handle
, id
, value
, size
);
578 if (error
) FIXME( "ignoring error parameter\n" );
580 if (!listener
) return E_INVALIDARG
;
582 EnterCriticalSection( &listener
->cs
);
584 if (listener
->magic
!= LISTENER_MAGIC
)
586 LeaveCriticalSection( &listener
->cs
);
590 hr
= prop_set( listener
->prop
, listener
->prop_count
, id
, value
, size
);
592 LeaveCriticalSection( &listener
->cs
);
596 /**************************************************************************
597 * WsAcceptChannel [webservices.@]
599 HRESULT WINAPI
WsAcceptChannel( WS_LISTENER
*handle
, WS_CHANNEL
*channel_handle
, const WS_ASYNC_CONTEXT
*ctx
,
602 struct listener
*listener
= (struct listener
*)handle
;
603 HRESULT hr
= E_NOTIMPL
;
606 TRACE( "%p %p %p %p\n", handle
, channel_handle
, ctx
, error
);
607 if (error
) FIXME( "ignoring error parameter\n" );
608 if (ctx
) FIXME( "ignoring ctx parameter\n" );
610 if (!listener
|| !channel_handle
) return E_INVALIDARG
;
612 EnterCriticalSection( &listener
->cs
);
614 if (listener
->magic
!= LISTENER_MAGIC
)
616 LeaveCriticalSection( &listener
->cs
);
620 if (listener
->state
!= WS_LISTENER_STATE_OPEN
|| listener
->channel
)
622 LeaveCriticalSection( &listener
->cs
);
623 return WS_E_INVALID_OPERATION
;
626 wait
= listener
->wait
;
627 cancel
= listener
->cancel
;
628 listener
->channel
= channel_handle
;
630 switch (listener
->binding
)
632 case WS_TCP_CHANNEL_BINDING
:
634 SOCKET socket
= listener
->u
.tcp
.socket
;
636 LeaveCriticalSection( &listener
->cs
);
637 return channel_accept_tcp( socket
, wait
, cancel
, channel_handle
);
639 case WS_UDP_CHANNEL_BINDING
:
641 SOCKET socket
= listener
->u
.udp
.socket
;
643 LeaveCriticalSection( &listener
->cs
);
644 return channel_accept_udp( socket
, wait
, cancel
, channel_handle
);
647 FIXME( "listener binding %u not supported\n", listener
->binding
);
651 LeaveCriticalSection( &listener
->cs
);