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/heap.h"
27 #include "wine/list.h"
28 #include "wine/unicode.h"
29 #include "webservices_private.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
)
42 if (!(ret
= WSAStartup( MAKEWORD(1,1), &data
))) winsock_loaded
= TRUE
;
43 else ERR( "WSAStartup failed: %d\n", ret
);
47 void winsock_init(void)
49 static INIT_ONCE once
= INIT_ONCE_STATIC_INIT
;
50 InitOnceExecuteOnce( &once
, winsock_startup
, NULL
, NULL
);
53 /******************************************************************
54 * DllMain (webservices.@)
56 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, void *reserved
)
60 case DLL_PROCESS_ATTACH
:
61 webservices_instance
= hinst
;
62 DisableThreadLibraryCalls( hinst
);
65 case DLL_PROCESS_DETACH
:
67 if (winsock_loaded
) WSACleanup();
73 static const struct prop_desc listener_props
[] =
75 { sizeof(ULONG
), FALSE
}, /* WS_LISTENER_PROPERTY_LISTEN_BACKLOG */
76 { sizeof(WS_IP_VERSION
), FALSE
}, /* WS_LISTENER_PROPERTY_IP_VERSION */
77 { sizeof(WS_LISTENER_STATE
), TRUE
}, /* WS_LISTENER_PROPERTY_STATE */
78 { sizeof(WS_CALLBACK_MODEL
), FALSE
}, /* WS_LISTENER_PROPERTY_ASYNC_CALLBACK_MODEL */
79 { sizeof(WS_CHANNEL_TYPE
), TRUE
}, /* WS_LISTENER_PROPERTY_CHANNEL_TYPE */
80 { sizeof(WS_CHANNEL_BINDING
), TRUE
}, /* WS_LISTENER_PROPERTY_CHANNEL_BINDING */
81 { sizeof(ULONG
), FALSE
}, /* WS_LISTENER_PROPERTY_CONNECT_TIMEOUT */
82 { sizeof(BOOL
), FALSE
}, /* WS_LISTENER_PROPERTY_IS_MULTICAST */
83 { 0, FALSE
}, /* WS_LISTENER_PROPERTY_MULTICAST_INTERFACES */
84 { sizeof(BOOL
), FALSE
}, /* WS_LISTENER_PROPERTY_MULTICAST_LOOPBACK */
85 { sizeof(ULONG
), FALSE
}, /* WS_LISTENER_PROPERTY_CLOSE_TIMEOUT */
86 { sizeof(ULONG
), FALSE
}, /* WS_LISTENER_PROPERTY_TO_HEADER_MATCHING_OPTIONS */
87 { sizeof(ULONG
), FALSE
}, /* WS_LISTENER_PROPERTY_TRANSPORT_URL_MATCHING_OPTIONS */
88 { sizeof(WS_CUSTOM_LISTENER_CALLBACKS
), FALSE
}, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_CALLBACKS */
89 { 0, FALSE
}, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_PARAMETERS */
90 { sizeof(void *), TRUE
}, /* WS_LISTENER_PROPERTY_CUSTOM_LISTENER_INSTANCE */
91 { sizeof(WS_DISALLOWED_USER_AGENT_SUBSTRINGS
), FALSE
} /* WS_LISTENER_PROPERTY_DISALLOWED_USER_AGENT */
99 WS_CHANNEL_BINDING binding
;
100 WS_LISTENER_STATE state
;
116 struct prop prop
[sizeof(listener_props
)/sizeof(listener_props
[0])];
119 #define LISTENER_MAGIC (('L' << 24) | ('I' << 16) | ('S' << 8) | 'T')
121 static struct listener
*alloc_listener(void)
123 static const ULONG count
= sizeof(listener_props
)/sizeof(listener_props
[0]);
124 struct listener
*ret
;
125 ULONG size
= sizeof(*ret
) + prop_size( listener_props
, count
);
127 if (!(ret
= heap_alloc_zero( size
))) return NULL
;
129 ret
->magic
= LISTENER_MAGIC
;
130 if (!(ret
->wait
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
135 if (!(ret
->cancel
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
137 CloseHandle( ret
->wait
);
141 InitializeCriticalSection( &ret
->cs
);
142 ret
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": listener.cs");
144 prop_init( listener_props
, count
, ret
->prop
, &ret
[1] );
145 ret
->prop_count
= count
;
149 static void reset_listener( struct listener
*listener
)
151 listener
->state
= WS_LISTENER_STATE_CREATED
;
152 SetEvent( listener
->cancel
);
153 listener
->channel
= NULL
;
155 switch (listener
->binding
)
157 case WS_TCP_CHANNEL_BINDING
:
158 closesocket( listener
->u
.tcp
.socket
);
159 listener
->u
.tcp
.socket
= -1;
162 case WS_UDP_CHANNEL_BINDING
:
163 closesocket( listener
->u
.udp
.socket
);
164 listener
->u
.udp
.socket
= -1;
171 static void free_listener( struct listener
*listener
)
173 reset_listener( listener
);
175 CloseHandle( listener
->wait
);
176 CloseHandle( listener
->cancel
);
178 listener
->cs
.DebugInfo
->Spare
[0] = 0;
179 DeleteCriticalSection( &listener
->cs
);
180 heap_free( listener
);
183 static HRESULT
create_listener( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
184 const WS_LISTENER_PROPERTY
*properties
, ULONG count
, struct listener
**ret
)
186 struct listener
*listener
;
190 if (!(listener
= alloc_listener())) return E_OUTOFMEMORY
;
192 for (i
= 0; i
< count
; i
++)
194 hr
= prop_set( listener
->prop
, listener
->prop_count
, properties
[i
].id
, properties
[i
].value
,
195 properties
[i
].valueSize
);
198 free_listener( listener
);
203 listener
->type
= type
;
204 listener
->binding
= binding
;
206 switch (listener
->binding
)
208 case WS_TCP_CHANNEL_BINDING
:
209 listener
->u
.tcp
.socket
= -1;
212 case WS_UDP_CHANNEL_BINDING
:
213 listener
->u
.udp
.socket
= -1;
223 /**************************************************************************
224 * WsCreateListener [webservices.@]
226 HRESULT WINAPI
WsCreateListener( WS_CHANNEL_TYPE type
, WS_CHANNEL_BINDING binding
,
227 const WS_LISTENER_PROPERTY
*properties
, ULONG count
,
228 const WS_SECURITY_DESCRIPTION
*desc
, WS_LISTENER
**handle
,
231 struct listener
*listener
;
234 TRACE( "%u %u %p %u %p %p %p\n", type
, binding
, properties
, count
, desc
, handle
, error
);
235 if (error
) FIXME( "ignoring error parameter\n" );
236 if (desc
) FIXME( "ignoring security description\n" );
238 if (!handle
) return E_INVALIDARG
;
240 if (type
!= WS_CHANNEL_TYPE_DUPLEX_SESSION
&& type
!= WS_CHANNEL_TYPE_DUPLEX
)
242 FIXME( "channel type %u not implemented\n", type
);
245 if (binding
!= WS_TCP_CHANNEL_BINDING
&& binding
!= WS_UDP_CHANNEL_BINDING
)
247 FIXME( "channel binding %u not implemented\n", binding
);
251 if ((hr
= create_listener( type
, binding
, properties
, count
, &listener
)) != S_OK
) return hr
;
253 TRACE( "created %p\n", listener
);
254 *handle
= (WS_LISTENER
*)listener
;
258 /**************************************************************************
259 * WsFreeListener [webservices.@]
261 void WINAPI
WsFreeListener( WS_LISTENER
*handle
)
263 struct listener
*listener
= (struct listener
*)handle
;
265 TRACE( "%p\n", handle
);
267 if (!listener
) return;
269 EnterCriticalSection( &listener
->cs
);
271 if (listener
->magic
!= LISTENER_MAGIC
)
273 LeaveCriticalSection( &listener
->cs
);
279 LeaveCriticalSection( &listener
->cs
);
280 free_listener( listener
);
283 HRESULT
resolve_hostname( const WCHAR
*host
, USHORT port
, struct sockaddr
*addr
, int *addr_len
, int flags
)
285 static const WCHAR fmtW
[] = {'%','u',0};
287 ADDRINFOW hints
, *res
, *info
;
288 HRESULT hr
= WS_E_ADDRESS_NOT_AVAILABLE
;
290 memset( &hints
, 0, sizeof(hints
) );
291 hints
.ai_flags
= flags
;
292 hints
.ai_family
= AF_INET
;
295 sprintfW( service
, fmtW
, port
);
296 if (GetAddrInfoW( host
, service
, &hints
, &res
)) return HRESULT_FROM_WIN32( WSAGetLastError() );
299 while (info
&& info
->ai_family
!= AF_INET
) info
= info
->ai_next
;
302 memcpy( addr
, info
->ai_addr
, info
->ai_addrlen
);
303 *addr_len
= info
->ai_addrlen
;
307 FreeAddrInfoW( res
);
311 HRESULT
parse_url( const WS_STRING
*str
, WS_URL_SCHEME_TYPE
*scheme
, WCHAR
**host
, USHORT
*port
)
317 if ((hr
= WsCreateHeap( 1 << 8, 0, NULL
, 0, &heap
, NULL
)) != S_OK
) return hr
;
318 if ((hr
= WsDecodeUrl( str
, 0, heap
, (WS_URL
**)&url
, NULL
)) != S_OK
)
324 if (url
->host
.length
== 1 && (url
->host
.chars
[0] == '+' || url
->host
.chars
[0] == '*')) *host
= NULL
;
327 if (!(*host
= heap_alloc( (url
->host
.length
+ 1) * sizeof(WCHAR
) )))
330 return E_OUTOFMEMORY
;
332 memcpy( *host
, url
->host
.chars
, url
->host
.length
* sizeof(WCHAR
) );
333 (*host
)[url
->host
.length
] = 0;
335 *scheme
= url
->url
.scheme
;
342 static HRESULT
open_listener_tcp( struct listener
*listener
, const WS_STRING
*url
)
344 struct sockaddr_storage storage
;
345 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
346 int addr_len
, on
= 1;
347 WS_URL_SCHEME_TYPE scheme
;
352 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
353 if (scheme
!= WS_URL_NETTCP_SCHEME_TYPE
)
356 return WS_E_INVALID_ENDPOINT_URL
;
361 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
363 if (hr
!= S_OK
) return hr
;
365 if ((listener
->u
.tcp
.socket
= socket( addr
->sa_family
, SOCK_STREAM
, 0 )) == -1)
366 return HRESULT_FROM_WIN32( WSAGetLastError() );
368 if (setsockopt( listener
->u
.tcp
.socket
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof(on
) ) < 0)
370 closesocket( listener
->u
.tcp
.socket
);
371 listener
->u
.tcp
.socket
= -1;
372 return HRESULT_FROM_WIN32( WSAGetLastError() );
375 if (bind( listener
->u
.tcp
.socket
, addr
, addr_len
) < 0)
377 closesocket( listener
->u
.tcp
.socket
);
378 listener
->u
.tcp
.socket
= -1;
379 return HRESULT_FROM_WIN32( WSAGetLastError() );
382 if (listen( listener
->u
.tcp
.socket
, 0 ) < 0)
384 closesocket( listener
->u
.tcp
.socket
);
385 listener
->u
.tcp
.socket
= -1;
386 return HRESULT_FROM_WIN32( WSAGetLastError() );
389 listener
->state
= WS_LISTENER_STATE_OPEN
;
393 static HRESULT
open_listener_udp( struct listener
*listener
, const WS_STRING
*url
)
395 struct sockaddr_storage storage
;
396 struct sockaddr
*addr
= (struct sockaddr
*)&storage
;
398 WS_URL_SCHEME_TYPE scheme
;
403 if ((hr
= parse_url( url
, &scheme
, &host
, &port
)) != S_OK
) return hr
;
404 if (scheme
!= WS_URL_SOAPUDP_SCHEME_TYPE
)
407 return WS_E_INVALID_ENDPOINT_URL
;
412 hr
= resolve_hostname( host
, port
, addr
, &addr_len
, AI_PASSIVE
);
414 if (hr
!= S_OK
) return hr
;
416 if ((listener
->u
.udp
.socket
= socket( addr
->sa_family
, SOCK_DGRAM
, 0 )) == -1)
417 return HRESULT_FROM_WIN32( WSAGetLastError() );
419 if (bind( listener
->u
.udp
.socket
, addr
, addr_len
) < 0)
421 closesocket( listener
->u
.udp
.socket
);
422 listener
->u
.udp
.socket
= -1;
423 return HRESULT_FROM_WIN32( WSAGetLastError() );
426 listener
->state
= WS_LISTENER_STATE_OPEN
;
430 static HRESULT
open_listener( struct listener
*listener
, const WS_STRING
*url
)
432 switch (listener
->binding
)
434 case WS_TCP_CHANNEL_BINDING
:
435 return open_listener_tcp( listener
, url
);
437 case WS_UDP_CHANNEL_BINDING
:
438 return open_listener_udp( listener
, url
);
441 ERR( "unhandled binding %u\n", listener
->binding
);
446 /**************************************************************************
447 * WsOpenListener [webservices.@]
449 HRESULT WINAPI
WsOpenListener( WS_LISTENER
*handle
, WS_STRING
*url
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
451 struct listener
*listener
= (struct listener
*)handle
;
454 TRACE( "%p %s %p %p\n", handle
, url
? debugstr_wn(url
->chars
, url
->length
) : "null", ctx
, error
);
455 if (error
) FIXME( "ignoring error parameter\n" );
456 if (ctx
) FIXME( "ignoring ctx parameter\n" );
458 if (!listener
|| !url
) return E_INVALIDARG
;
460 EnterCriticalSection( &listener
->cs
);
462 if (listener
->magic
!= LISTENER_MAGIC
)
464 LeaveCriticalSection( &listener
->cs
);
468 if (listener
->state
!= WS_LISTENER_STATE_CREATED
)
470 LeaveCriticalSection( &listener
->cs
);
471 return WS_E_INVALID_OPERATION
;
474 hr
= open_listener( listener
, url
);
476 LeaveCriticalSection( &listener
->cs
);
480 static void close_listener( struct listener
*listener
)
482 reset_listener( listener
);
483 listener
->state
= WS_LISTENER_STATE_CLOSED
;
486 /**************************************************************************
487 * WsCloseListener [webservices.@]
489 HRESULT WINAPI
WsCloseListener( WS_LISTENER
*handle
, const WS_ASYNC_CONTEXT
*ctx
, WS_ERROR
*error
)
491 struct listener
*listener
= (struct listener
*)handle
;
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
);
507 close_listener( listener
);
509 LeaveCriticalSection( &listener
->cs
);
513 /**************************************************************************
514 * WsResetListener [webservices.@]
516 HRESULT WINAPI
WsResetListener( WS_LISTENER
*handle
, WS_ERROR
*error
)
518 struct listener
*listener
= (struct listener
*)handle
;
520 TRACE( "%p %p\n", handle
, error
);
521 if (error
) FIXME( "ignoring error parameter\n" );
523 if (!listener
) return E_INVALIDARG
;
525 EnterCriticalSection( &listener
->cs
);
527 if (listener
->magic
!= LISTENER_MAGIC
)
529 LeaveCriticalSection( &listener
->cs
);
533 if (listener
->state
!= WS_LISTENER_STATE_CREATED
&& listener
->state
!= WS_LISTENER_STATE_CLOSED
)
535 LeaveCriticalSection( &listener
->cs
);
536 return WS_E_INVALID_OPERATION
;
539 reset_listener( listener
);
541 LeaveCriticalSection( &listener
->cs
);
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
;
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
);
569 case WS_LISTENER_PROPERTY_STATE
:
570 if (!buf
|| size
!= sizeof(listener
->state
)) hr
= E_INVALIDARG
;
571 else *(WS_LISTENER_STATE
*)buf
= listener
->state
;
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
;
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
;
585 hr
= prop_get( listener
->prop
, listener
->prop_count
, id
, buf
, size
);
588 LeaveCriticalSection( &listener
->cs
);
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
;
601 TRACE( "%p %u %p %u\n", handle
, id
, value
, size
);
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
);
614 hr
= prop_set( listener
->prop
, listener
->prop_count
, id
, value
, size
);
616 LeaveCriticalSection( &listener
->cs
);
620 /**************************************************************************
621 * WsAcceptChannel [webservices.@]
623 HRESULT WINAPI
WsAcceptChannel( WS_LISTENER
*handle
, WS_CHANNEL
*channel_handle
, const WS_ASYNC_CONTEXT
*ctx
,
626 struct listener
*listener
= (struct listener
*)handle
;
627 HRESULT hr
= E_NOTIMPL
;
630 TRACE( "%p %p %p %p\n", handle
, channel_handle
, ctx
, error
);
631 if (error
) FIXME( "ignoring error parameter\n" );
632 if (ctx
) FIXME( "ignoring ctx parameter\n" );
634 if (!listener
|| !channel_handle
) return E_INVALIDARG
;
636 EnterCriticalSection( &listener
->cs
);
638 if (listener
->magic
!= LISTENER_MAGIC
)
640 LeaveCriticalSection( &listener
->cs
);
644 if (listener
->state
!= WS_LISTENER_STATE_OPEN
|| listener
->channel
)
646 LeaveCriticalSection( &listener
->cs
);
647 return WS_E_INVALID_OPERATION
;
650 wait
= listener
->wait
;
651 cancel
= listener
->cancel
;
652 listener
->channel
= channel_handle
;
654 switch (listener
->binding
)
656 case WS_TCP_CHANNEL_BINDING
:
658 SOCKET socket
= listener
->u
.tcp
.socket
;
660 LeaveCriticalSection( &listener
->cs
);
661 return channel_accept_tcp( socket
, wait
, cancel
, channel_handle
);
663 case WS_UDP_CHANNEL_BINDING
:
665 SOCKET socket
= listener
->u
.udp
.socket
;
667 LeaveCriticalSection( &listener
->cs
);
668 return channel_accept_udp( socket
, wait
, cancel
, channel_handle
);
671 FIXME( "listener binding %u not supported\n", listener
->binding
);
675 LeaveCriticalSection( &listener
->cs
);