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 TRACE( "created %p\n", listener
);
231 *handle
= (WS_LISTENER
*)listener
;
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
);
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};
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
;
272 sprintfW( service
, fmtW
, port
);
273 if (GetAddrInfoW( host
, service
, &hints
, &res
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
276 while (info
&& info
->ai_family
!= AF_INET
) info
= info
->ai_next
;
279 memcpy( addr
, info
->ai_addr
, info
->ai_addrlen
);
280 *addr_len
= info
->ai_addrlen
;
284 FreeAddrInfoW( res
);
288 HRESULT
parse_url( const WS_STRING
*str
, WS_URL_SCHEME_TYPE
*scheme
, WCHAR
**host
, USHORT
*port
)
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
)
301 if (url
->host
.length
== 1 && (url
->host
.chars
[0] == '+' || url
->host
.chars
[0] == '*')) *host
= NULL
;
304 if (!(*host
= heap_alloc( (url
->host
.length
+ 1) * sizeof(WCHAR
) )))
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
;
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
;
329 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
330 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
333 return WS_E_INVALID_ENDPOINT_URL
;
338 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
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
;
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
;
375 WS_URL_SCHEME_TYPE scheme
;
380 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
381 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
384 return WS_E_INVALID_ENDPOINT_URL
;
389 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
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
;
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
);
418 ERR( "unhandled binding %u\n", listener
->binding
);
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
;
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
);
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
);
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
);
484 close_listener( listener
);
486 LeaveCriticalSection( &listener
->cs
);
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
);
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
);
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
;
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
);
546 case WS_LISTENER_PROPERTY_STATE
:
547 if (!buf
|| size
!= sizeof(listener
->state
)) hr
= E_INVALIDARG
;
548 else *(WS_LISTENER_STATE
*)buf
= listener
->state
;
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
;
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
;
562 hr
= prop_get( listener
->prop
, listener
->prop_count
, id
, buf
, size
);
565 LeaveCriticalSection( &listener
->cs
);
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
;
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
);
591 hr
= prop_set( listener
->prop
, listener
->prop_count
, id
, value
, size
);
593 LeaveCriticalSection( &listener
->cs
);
597 /**************************************************************************
598 * WsAcceptChannel [webservices.@]
600 HRESULT WINAPI
WsAcceptChannel( WS_LISTENER
*handle
, WS_CHANNEL
*channel_handle
, const WS_ASYNC_CONTEXT
*ctx
,
603 struct listener
*listener
= (struct listener
*)handle
;
604 HRESULT hr
= E_NOTIMPL
;
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
);
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
);
648 FIXME( "listener binding %u not supported\n", listener
->binding
);
652 LeaveCriticalSection( &listener
->cs
);