msvcrt: Set the correct error number in pow(f).
[wine.git] / dlls / webservices / channel.c
blob20d4f4c7afbf94fe0cb6a556dca0283338ddc31a
1 /*
2 * Copyright 2016 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 "winuser.h"
24 #include "rpc.h"
25 #include "webservices.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
29 #include "wine/unicode.h"
30 #include "webservices_private.h"
31 #include "sock.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
35 static const struct prop_desc channel_props[] =
37 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE */
38 { sizeof(UINT64), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_MESSAGE_SIZE */
39 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_START_SIZE */
40 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_STREAMED_FLUSH_SIZE */
41 { sizeof(WS_ENCODING), TRUE }, /* WS_CHANNEL_PROPERTY_ENCODING */
42 { sizeof(WS_ENVELOPE_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ENVELOPE_VERSION */
43 { sizeof(WS_ADDRESSING_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_ADDRESSING_VERSION */
44 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_SESSION_DICTIONARY_SIZE */
45 { sizeof(WS_CHANNEL_STATE), TRUE }, /* WS_CHANNEL_PROPERTY_STATE */
46 { sizeof(WS_CALLBACK_MODEL), FALSE }, /* WS_CHANNEL_PROPERTY_ASYNC_CALLBACK_MODEL */
47 { sizeof(WS_IP_VERSION), FALSE }, /* WS_CHANNEL_PROPERTY_IP_VERSION */
48 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RESOLVE_TIMEOUT */
49 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CONNECT_TIMEOUT */
50 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_TIMEOUT */
51 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_RESPONSE_TIMEOUT */
52 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_RECEIVE_TIMEOUT */
53 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_CLOSE_TIMEOUT */
54 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_TIMEOUTS */
55 { sizeof(WS_TRANSFER_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_TRANSFER_MODE */
56 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_INTERFACE */
57 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MULTICAST_HOPS */
58 { sizeof(WS_ENDPOINT_ADDRESS), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_ADDRESS */
59 { sizeof(SOCKADDR_STORAGE), TRUE }, /* WS_CHANNEL_PROPERTY_REMOTE_IP_ADDRESS */
60 { sizeof(ULONGLONG), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_CONNECTION_ID */
61 { sizeof(WS_CUSTOM_CHANNEL_CALLBACKS), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_CALLBACKS */
62 { 0, FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_PARAMETERS */
63 { sizeof(void *), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_CHANNEL_INSTANCE */
64 { sizeof(WS_STRING), TRUE }, /* WS_CHANNEL_PROPERTY_TRANSPORT_URL */
65 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_NO_DELAY */
66 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_SEND_KEEP_ALIVES */
67 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_TIME */
68 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_KEEP_ALIVE_INTERVAL */
69 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_MAX_HTTP_SERVER_CONNECTIONS */
70 { sizeof(BOOL), TRUE }, /* WS_CHANNEL_PROPERTY_IS_SESSION_SHUT_DOWN */
71 { sizeof(WS_CHANNEL_TYPE), TRUE }, /* WS_CHANNEL_PROPERTY_CHANNEL_TYPE */
72 { sizeof(ULONG), FALSE }, /* WS_CHANNEL_PROPERTY_TRIM_BUFFERED_MESSAGE_SIZE */
73 { sizeof(WS_CHANNEL_ENCODER), FALSE }, /* WS_CHANNEL_PROPERTY_ENCODER */
74 { sizeof(WS_CHANNEL_DECODER), FALSE }, /* WS_CHANNEL_PROPERTY_DECODER */
75 { sizeof(WS_PROTECTION_LEVEL), TRUE }, /* WS_CHANNEL_PROPERTY_PROTECTION_LEVEL */
76 { sizeof(WS_COOKIE_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_COOKIE_MODE */
77 { sizeof(WS_HTTP_PROXY_SETTING_MODE), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SETTING_MODE */
78 { sizeof(WS_CUSTOM_HTTP_PROXY), FALSE }, /* WS_CHANNEL_PROPERTY_CUSTOM_HTTP_PROXY */
79 { sizeof(WS_HTTP_MESSAGE_MAPPING), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_MESSAGE_MAPPING */
80 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ENABLE_HTTP_REDIRECT */
81 { sizeof(WS_HTTP_REDIRECT_CALLBACK_CONTEXT), FALSE }, /* WS_CHANNEL_PROPERTY_HTTP_REDIRECT_CALLBACK_CONTEXT */
82 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_FAULTS_AS_ERRORS */
83 { sizeof(BOOL), FALSE }, /* WS_CHANNEL_PROPERTY_ALLOW_UNSECURED_FAULTS */
84 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_SERVER_SPN */
85 { sizeof(WCHAR *), TRUE }, /* WS_CHANNEL_PROPERTY_HTTP_PROXY_SPN */
86 { sizeof(ULONG), FALSE } /* WS_CHANNEL_PROPERTY_MAX_HTTP_REQUEST_HEADERS_BUFFER_SIZE */
89 enum session_state
91 SESSION_STATE_UNINITIALIZED,
92 SESSION_STATE_SETUP_COMPLETE,
95 struct channel
97 ULONG magic;
98 CRITICAL_SECTION cs;
99 WS_CHANNEL_TYPE type;
100 WS_CHANNEL_BINDING binding;
101 WS_CHANNEL_STATE state;
102 WS_ENDPOINT_ADDRESS addr;
103 WS_XML_WRITER *writer;
104 WS_XML_READER *reader;
105 WS_MESSAGE *msg;
106 WS_ENCODING encoding;
107 enum session_state session_state;
108 struct dictionary dict;
109 union
111 struct
113 HINTERNET session;
114 HINTERNET connect;
115 HINTERNET request;
116 WCHAR *path;
117 DWORD flags;
118 } http;
119 struct
121 SOCKET socket;
122 } tcp;
123 struct
125 SOCKET socket;
126 } udp;
127 } u;
128 char *read_buf;
129 ULONG read_buflen;
130 ULONG read_size;
131 ULONG prop_count;
132 struct prop prop[sizeof(channel_props)/sizeof(channel_props[0])];
135 #define CHANNEL_MAGIC (('C' << 24) | ('H' << 16) | ('A' << 8) | 'N')
137 static struct channel *alloc_channel(void)
139 static const ULONG count = sizeof(channel_props)/sizeof(channel_props[0]);
140 struct channel *ret;
141 ULONG size = sizeof(*ret) + prop_size( channel_props, count );
143 if (!(ret = heap_alloc_zero( size ))) return NULL;
145 ret->magic = CHANNEL_MAGIC;
146 InitializeCriticalSection( &ret->cs );
147 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": channel.cs");
149 prop_init( channel_props, count, ret->prop, &ret[1] );
150 ret->prop_count = count;
151 return ret;
154 static void reset_channel( struct channel *channel )
156 channel->state = WS_CHANNEL_STATE_CREATED;
157 heap_free( channel->addr.url.chars );
158 channel->addr.url.chars = NULL;
159 channel->addr.url.length = 0;
160 channel->msg = NULL;
161 channel->read_size = 0;
162 channel->session_state = SESSION_STATE_UNINITIALIZED;
163 clear_dict( &channel->dict );
165 switch (channel->binding)
167 case WS_HTTP_CHANNEL_BINDING:
168 WinHttpCloseHandle( channel->u.http.request );
169 channel->u.http.request = NULL;
170 WinHttpCloseHandle( channel->u.http.connect );
171 channel->u.http.connect = NULL;
172 WinHttpCloseHandle( channel->u.http.session );
173 channel->u.http.session = NULL;
174 heap_free( channel->u.http.path );
175 channel->u.http.path = NULL;
176 channel->u.http.flags = 0;
177 break;
179 case WS_TCP_CHANNEL_BINDING:
180 closesocket( channel->u.tcp.socket );
181 channel->u.tcp.socket = -1;
182 break;
184 case WS_UDP_CHANNEL_BINDING:
185 closesocket( channel->u.udp.socket );
186 channel->u.udp.socket = -1;
187 break;
189 default: break;
193 static void free_channel( struct channel *channel )
195 reset_channel( channel );
197 WsFreeWriter( channel->writer );
198 WsFreeReader( channel->reader );
200 heap_free( channel->read_buf );
202 channel->cs.DebugInfo->Spare[0] = 0;
203 DeleteCriticalSection( &channel->cs );
204 heap_free( channel );
207 static HRESULT create_channel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
208 const WS_CHANNEL_PROPERTY *properties, ULONG count, struct channel **ret )
210 struct channel *channel;
211 ULONG i, msg_size = 65536;
212 WS_ENVELOPE_VERSION env_version = WS_ENVELOPE_VERSION_SOAP_1_2;
213 WS_ADDRESSING_VERSION addr_version = WS_ADDRESSING_VERSION_1_0;
214 HRESULT hr;
216 if (!(channel = alloc_channel())) return E_OUTOFMEMORY;
218 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
219 &msg_size, sizeof(msg_size) );
220 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION,
221 &env_version, sizeof(env_version) );
222 prop_set( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ADDRESSING_VERSION,
223 &addr_version, sizeof(addr_version) );
225 channel->type = type;
226 channel->binding = binding;
228 switch (channel->binding)
230 case WS_HTTP_CHANNEL_BINDING:
231 channel->encoding = WS_ENCODING_XML_UTF8;
232 break;
234 case WS_TCP_CHANNEL_BINDING:
235 channel->u.tcp.socket = -1;
236 channel->encoding = WS_ENCODING_XML_BINARY_SESSION_1;
237 break;
239 case WS_UDP_CHANNEL_BINDING:
240 channel->u.udp.socket = -1;
241 channel->encoding = WS_ENCODING_XML_UTF8;
242 break;
244 default: break;
247 for (i = 0; i < count; i++)
249 switch (properties[i].id)
251 case WS_CHANNEL_PROPERTY_ENCODING:
252 if (!properties[i].value || properties[i].valueSize != sizeof(channel->encoding))
254 free_channel( channel );
255 return E_INVALIDARG;
257 channel->encoding = *(WS_ENCODING *)properties[i].value;
258 break;
260 default:
261 if ((hr = prop_set( channel->prop, channel->prop_count, properties[i].id, properties[i].value,
262 properties[i].valueSize )) != S_OK)
264 free_channel( channel );
265 return hr;
267 break;
271 *ret = channel;
272 return S_OK;
275 /**************************************************************************
276 * WsCreateChannel [webservices.@]
278 HRESULT WINAPI WsCreateChannel( WS_CHANNEL_TYPE type, WS_CHANNEL_BINDING binding,
279 const WS_CHANNEL_PROPERTY *properties, ULONG count,
280 const WS_SECURITY_DESCRIPTION *desc, WS_CHANNEL **handle,
281 WS_ERROR *error )
283 struct channel *channel;
284 HRESULT hr;
286 TRACE( "%u %u %p %u %p %p %p\n", type, binding, properties, count, desc, handle, error );
287 if (error) FIXME( "ignoring error parameter\n" );
288 if (desc) FIXME( "ignoring security description\n" );
290 if (!handle) return E_INVALIDARG;
292 if (type != WS_CHANNEL_TYPE_REQUEST && type != WS_CHANNEL_TYPE_DUPLEX &&
293 type != WS_CHANNEL_TYPE_DUPLEX_SESSION)
295 FIXME( "channel type %u not implemented\n", type );
296 return E_NOTIMPL;
298 if (binding != WS_HTTP_CHANNEL_BINDING && binding != WS_TCP_CHANNEL_BINDING &&
299 binding != WS_UDP_CHANNEL_BINDING)
301 FIXME( "channel binding %u not implemented\n", binding );
302 return E_NOTIMPL;
305 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
307 *handle = (WS_CHANNEL *)channel;
308 return S_OK;
311 /**************************************************************************
312 * WsCreateChannelForListener [webservices.@]
314 HRESULT WINAPI WsCreateChannelForListener( WS_LISTENER *listener_handle, const WS_CHANNEL_PROPERTY *properties,
315 ULONG count, WS_CHANNEL **handle, WS_ERROR *error )
317 struct channel *channel;
318 WS_CHANNEL_TYPE type;
319 WS_CHANNEL_BINDING binding;
320 HRESULT hr;
322 TRACE( "%p %p %u %p %p\n", listener_handle, properties, count, handle, error );
323 if (error) FIXME( "ignoring error parameter\n" );
325 if (!listener_handle || !handle) return E_INVALIDARG;
327 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_TYPE, &type,
328 sizeof(type), NULL )) != S_OK) return hr;
330 if ((hr = WsGetListenerProperty( listener_handle, WS_LISTENER_PROPERTY_CHANNEL_BINDING, &binding,
331 sizeof(binding), NULL )) != S_OK) return hr;
333 if ((hr = create_channel( type, binding, properties, count, &channel )) != S_OK) return hr;
335 *handle = (WS_CHANNEL *)channel;
336 return S_OK;
339 /**************************************************************************
340 * WsFreeChannel [webservices.@]
342 void WINAPI WsFreeChannel( WS_CHANNEL *handle )
344 struct channel *channel = (struct channel *)handle;
346 TRACE( "%p\n", handle );
348 if (!channel) return;
350 EnterCriticalSection( &channel->cs );
352 if (channel->magic != CHANNEL_MAGIC)
354 LeaveCriticalSection( &channel->cs );
355 return;
358 channel->magic = 0;
360 LeaveCriticalSection( &channel->cs );
361 free_channel( channel );
364 /**************************************************************************
365 * WsResetChannel [webservices.@]
367 HRESULT WINAPI WsResetChannel( WS_CHANNEL *handle, WS_ERROR *error )
369 struct channel *channel = (struct channel *)handle;
371 TRACE( "%p %p\n", handle, error );
372 if (error) FIXME( "ignoring error parameter\n" );
374 if (!channel) return E_INVALIDARG;
376 EnterCriticalSection( &channel->cs );
378 if (channel->magic != CHANNEL_MAGIC)
380 LeaveCriticalSection( &channel->cs );
381 return E_INVALIDARG;
384 if (channel->state != WS_CHANNEL_STATE_CREATED && channel->state != WS_CHANNEL_STATE_CLOSED)
386 LeaveCriticalSection( &channel->cs );
387 return WS_E_INVALID_OPERATION;
390 reset_channel( channel );
392 LeaveCriticalSection( &channel->cs );
393 return S_OK;
396 /**************************************************************************
397 * WsGetChannelProperty [webservices.@]
399 HRESULT WINAPI WsGetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, void *buf,
400 ULONG size, WS_ERROR *error )
402 struct channel *channel = (struct channel *)handle;
403 HRESULT hr = S_OK;
405 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
406 if (error) FIXME( "ignoring error parameter\n" );
408 if (!channel) return E_INVALIDARG;
410 EnterCriticalSection( &channel->cs );
412 if (channel->magic != CHANNEL_MAGIC)
414 LeaveCriticalSection( &channel->cs );
415 return E_INVALIDARG;
418 switch (id)
420 case WS_CHANNEL_PROPERTY_CHANNEL_TYPE:
421 if (!buf || size != sizeof(channel->type)) hr = E_INVALIDARG;
422 else *(WS_CHANNEL_TYPE *)buf = channel->type;
423 break;
425 case WS_CHANNEL_PROPERTY_ENCODING:
426 if (!buf || size != sizeof(channel->encoding)) hr = E_INVALIDARG;
427 else *(WS_ENCODING *)buf = channel->encoding;
428 break;
430 default:
431 hr = prop_get( channel->prop, channel->prop_count, id, buf, size );
434 LeaveCriticalSection( &channel->cs );
435 return hr;
438 /**************************************************************************
439 * WsSetChannelProperty [webservices.@]
441 HRESULT WINAPI WsSetChannelProperty( WS_CHANNEL *handle, WS_CHANNEL_PROPERTY_ID id, const void *value,
442 ULONG size, WS_ERROR *error )
444 struct channel *channel = (struct channel *)handle;
445 HRESULT hr;
447 TRACE( "%p %u %p %u\n", handle, id, value, size );
448 if (error) FIXME( "ignoring error parameter\n" );
450 if (!channel) return E_INVALIDARG;
452 EnterCriticalSection( &channel->cs );
454 if (channel->magic != CHANNEL_MAGIC)
456 LeaveCriticalSection( &channel->cs );
457 return E_INVALIDARG;
460 hr = prop_set( channel->prop, channel->prop_count, id, value, size );
462 LeaveCriticalSection( &channel->cs );
463 return hr;
466 static HRESULT open_channel( struct channel *channel, const WS_ENDPOINT_ADDRESS *endpoint )
468 if (endpoint->headers || endpoint->extensions || endpoint->identity)
470 FIXME( "headers, extensions or identity not supported\n" );
471 return E_NOTIMPL;
474 if (!(channel->addr.url.chars = heap_alloc( endpoint->url.length * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
475 memcpy( channel->addr.url.chars, endpoint->url.chars, endpoint->url.length * sizeof(WCHAR) );
476 channel->addr.url.length = endpoint->url.length;
478 channel->state = WS_CHANNEL_STATE_OPEN;
479 return S_OK;
482 /**************************************************************************
483 * WsOpenChannel [webservices.@]
485 HRESULT WINAPI WsOpenChannel( WS_CHANNEL *handle, const WS_ENDPOINT_ADDRESS *endpoint,
486 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
488 struct channel *channel = (struct channel *)handle;
489 HRESULT hr;
491 TRACE( "%p %p %p %p\n", handle, endpoint, ctx, error );
492 if (error) FIXME( "ignoring error parameter\n" );
493 if (ctx) FIXME( "ignoring ctx parameter\n" );
495 if (!channel || !endpoint) return E_INVALIDARG;
497 EnterCriticalSection( &channel->cs );
499 if (channel->magic != CHANNEL_MAGIC)
501 LeaveCriticalSection( &channel->cs );
502 return E_INVALIDARG;
505 if (channel->state != WS_CHANNEL_STATE_CREATED)
507 LeaveCriticalSection( &channel->cs );
508 return WS_E_INVALID_OPERATION;
511 hr = open_channel( channel, endpoint );
513 LeaveCriticalSection( &channel->cs );
514 return hr;
517 static void close_channel( struct channel *channel )
519 reset_channel( channel );
520 channel->state = WS_CHANNEL_STATE_CLOSED;
523 /**************************************************************************
524 * WsCloseChannel [webservices.@]
526 HRESULT WINAPI WsCloseChannel( WS_CHANNEL *handle, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
528 struct channel *channel = (struct channel *)handle;
530 TRACE( "%p %p %p\n", handle, ctx, error );
531 if (error) FIXME( "ignoring error parameter\n" );
532 if (ctx) FIXME( "ignoring ctx parameter\n" );
534 if (!channel) return E_INVALIDARG;
536 EnterCriticalSection( &channel->cs );
538 if (channel->magic != CHANNEL_MAGIC)
540 LeaveCriticalSection( &channel->cs );
541 return E_INVALIDARG;
544 close_channel( channel );
546 LeaveCriticalSection( &channel->cs );
547 return S_OK;
550 static HRESULT parse_http_url( const WCHAR *url, ULONG len, URL_COMPONENTS *uc )
552 HRESULT hr = E_OUTOFMEMORY;
553 WCHAR *tmp;
554 DWORD err;
556 memset( uc, 0, sizeof(*uc) );
557 uc->dwStructSize = sizeof(*uc);
558 uc->dwHostNameLength = 128;
559 uc->lpszHostName = heap_alloc( uc->dwHostNameLength * sizeof(WCHAR) );
560 uc->dwUrlPathLength = 128;
561 uc->lpszUrlPath = heap_alloc( uc->dwUrlPathLength * sizeof(WCHAR) );
562 uc->dwExtraInfoLength = 128;
563 uc->lpszExtraInfo = heap_alloc( uc->dwExtraInfoLength * sizeof(WCHAR) );
564 if (!uc->lpszHostName || !uc->lpszUrlPath || !uc->lpszExtraInfo) goto error;
566 if (!WinHttpCrackUrl( url, len, ICU_DECODE, uc ))
568 if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
570 hr = HRESULT_FROM_WIN32( err );
571 goto error;
573 if (!(tmp = heap_realloc( uc->lpszHostName, uc->dwHostNameLength * sizeof(WCHAR) ))) goto error;
574 uc->lpszHostName = tmp;
575 if (!(tmp = heap_realloc( uc->lpszUrlPath, uc->dwUrlPathLength * sizeof(WCHAR) ))) goto error;
576 uc->lpszUrlPath = tmp;
577 if (!(tmp = heap_realloc( uc->lpszExtraInfo, uc->dwExtraInfoLength * sizeof(WCHAR) ))) goto error;
578 uc->lpszExtraInfo = tmp;
579 WinHttpCrackUrl( url, len, ICU_DECODE, uc );
582 return S_OK;
584 error:
585 heap_free( uc->lpszHostName );
586 heap_free( uc->lpszUrlPath );
587 heap_free( uc->lpszExtraInfo );
588 return hr;
591 static HRESULT connect_channel_http( struct channel *channel )
593 static const WCHAR agentW[] =
594 {'M','S','-','W','e','b','S','e','r','v','i','c','e','s','/','1','.','0',0};
595 HINTERNET ses = NULL, con = NULL;
596 URL_COMPONENTS uc;
597 HRESULT hr;
599 if (channel->u.http.connect) return S_OK;
601 if ((hr = parse_http_url( channel->addr.url.chars, channel->addr.url.length, &uc )) != S_OK) return hr;
602 if (!(channel->u.http.path = heap_alloc( (uc.dwUrlPathLength + uc.dwExtraInfoLength + 1) * sizeof(WCHAR) )))
604 hr = E_OUTOFMEMORY;
605 goto done;
607 else
609 strcpyW( channel->u.http.path, uc.lpszUrlPath );
610 if (uc.dwExtraInfoLength) strcatW( channel->u.http.path, uc.lpszExtraInfo );
613 channel->u.http.flags = WINHTTP_FLAG_REFRESH;
614 switch (uc.nScheme)
616 case INTERNET_SCHEME_HTTP: break;
617 case INTERNET_SCHEME_HTTPS:
618 channel->u.http.flags |= WINHTTP_FLAG_SECURE;
619 break;
621 default:
622 hr = WS_E_INVALID_ENDPOINT_URL;
623 goto done;
626 if (!(ses = WinHttpOpen( agentW, 0, NULL, NULL, 0 )))
628 hr = HRESULT_FROM_WIN32( GetLastError() );
629 goto done;
631 if (!(con = WinHttpConnect( ses, uc.lpszHostName, uc.nPort, 0 )))
633 hr = HRESULT_FROM_WIN32( GetLastError() );
634 goto done;
637 channel->u.http.session = ses;
638 channel->u.http.connect = con;
640 done:
641 if (hr != S_OK)
643 WinHttpCloseHandle( con );
644 WinHttpCloseHandle( ses );
646 heap_free( uc.lpszHostName );
647 heap_free( uc.lpszUrlPath );
648 heap_free( uc.lpszExtraInfo );
649 return hr;
652 static HRESULT connect_channel_tcp( struct channel *channel )
654 struct sockaddr_storage storage;
655 struct sockaddr *addr = (struct sockaddr *)&storage;
656 int addr_len;
657 WS_URL_SCHEME_TYPE scheme;
658 WCHAR *host;
659 USHORT port;
660 HRESULT hr;
662 if (channel->u.tcp.socket != -1) return S_OK;
664 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
665 if (scheme != WS_URL_NETTCP_SCHEME_TYPE)
667 heap_free( host );
668 return WS_E_INVALID_ENDPOINT_URL;
671 winsock_init();
673 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
674 heap_free( host );
675 if (hr != S_OK) return hr;
677 if ((channel->u.tcp.socket = socket( addr->sa_family, SOCK_STREAM, 0 )) == -1)
678 return HRESULT_FROM_WIN32( WSAGetLastError() );
680 if (connect( channel->u.tcp.socket, addr, addr_len ) < 0)
682 closesocket( channel->u.tcp.socket );
683 channel->u.tcp.socket = -1;
684 return HRESULT_FROM_WIN32( WSAGetLastError() );
687 return S_OK;
690 static HRESULT connect_channel_udp( struct channel *channel )
692 struct sockaddr_storage storage;
693 struct sockaddr *addr = (struct sockaddr *)&storage;
694 int addr_len;
695 WS_URL_SCHEME_TYPE scheme;
696 WCHAR *host;
697 USHORT port;
698 HRESULT hr;
700 if (channel->u.udp.socket != -1) return S_OK;
702 if ((hr = parse_url( &channel->addr.url, &scheme, &host, &port )) != S_OK) return hr;
703 if (scheme != WS_URL_SOAPUDP_SCHEME_TYPE)
705 heap_free( host );
706 return WS_E_INVALID_ENDPOINT_URL;
709 winsock_init();
711 hr = resolve_hostname( host, port, addr, &addr_len, 0 );
712 heap_free( host );
713 if (hr != S_OK) return hr;
715 if ((channel->u.udp.socket = socket( addr->sa_family, SOCK_DGRAM, 0 )) == -1)
716 return HRESULT_FROM_WIN32( WSAGetLastError() );
718 if (connect( channel->u.udp.socket, addr, addr_len ) < 0)
720 closesocket( channel->u.udp.socket );
721 channel->u.udp.socket = -1;
722 return HRESULT_FROM_WIN32( WSAGetLastError() );
725 return S_OK;
728 static HRESULT connect_channel( struct channel *channel )
730 switch (channel->binding)
732 case WS_HTTP_CHANNEL_BINDING:
733 return connect_channel_http( channel );
735 case WS_TCP_CHANNEL_BINDING:
736 return connect_channel_tcp( channel );
738 case WS_UDP_CHANNEL_BINDING:
739 return connect_channel_udp( channel );
741 default:
742 ERR( "unhandled binding %u\n", channel->binding );
743 return E_NOTIMPL;
747 static HRESULT write_message( WS_MESSAGE *handle, WS_XML_WRITER *writer, const WS_ELEMENT_DESCRIPTION *desc,
748 WS_WRITE_OPTION option, const void *body, ULONG size )
750 HRESULT hr;
751 if ((hr = WsWriteEnvelopeStart( handle, writer, NULL, NULL, NULL )) != S_OK) return hr;
752 if ((hr = WsWriteBody( handle, desc, option, body, size, NULL )) != S_OK) return hr;
753 return WsWriteEnvelopeEnd( handle, NULL );
756 static HRESULT send_message_http( HINTERNET request, BYTE *data, ULONG len )
758 if (!WinHttpSendRequest( request, NULL, 0, data, len, len, 0 ))
759 return HRESULT_FROM_WIN32( GetLastError() );
761 if (!WinHttpReceiveResponse( request, NULL ))
762 return HRESULT_FROM_WIN32( GetLastError() );
763 return S_OK;
766 static HRESULT send_byte( SOCKET socket, BYTE byte )
768 int count = send( socket, (char *)&byte, 1, 0 );
769 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
770 if (count != 1) return WS_E_OTHER;
771 return S_OK;
774 static HRESULT send_bytes( SOCKET socket, BYTE *bytes, int len )
776 int count = send( socket, (char *)bytes, len, 0 );
777 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
778 if (count != len) return WS_E_OTHER;
779 return S_OK;
782 static HRESULT send_size( SOCKET socket, ULONG size )
784 HRESULT hr;
785 if (size < 0x80) return send_byte( socket, size );
786 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
787 if ((size >>= 7) < 0x80) return send_byte( socket, size );
788 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
789 if ((size >>= 7) < 0x80) return send_byte( socket, size );
790 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
791 if ((size >>= 7) < 0x80) return send_byte( socket, size );
792 if ((hr = send_byte( socket, (size & 0x7f) | 0x80 )) != S_OK) return hr;
793 if ((size >>= 7) < 0x08) return send_byte( socket, size );
794 return E_INVALIDARG;
797 enum frame_record_type
799 FRAME_RECORD_TYPE_VERSION,
800 FRAME_RECORD_TYPE_MODE,
801 FRAME_RECORD_TYPE_VIA,
802 FRAME_RECORD_TYPE_KNOWN_ENCODING,
803 FRAME_RECORD_TYPE_EXTENSIBLE_ENCODING,
804 FRAME_RECORD_TYPE_UNSIZED_ENVELOPE,
805 FRAME_RECORD_TYPE_SIZED_ENVELOPE,
806 FRAME_RECORD_TYPE_END,
807 FRAME_RECORD_TYPE_FAULT,
808 FRAME_RECORD_TYPE_UPGRADE_REQUEST,
809 FRAME_RECORD_TYPE_UPGRADE_RESPONSE,
810 FRAME_RECORD_TYPE_PREAMBLE_ACK,
811 FRAME_RECORD_TYPE_PREAMBLE_END,
814 static inline ULONG size_length( ULONG size )
816 if (size < 0x80) return 1;
817 if (size < 0x4000) return 2;
818 if (size < 0x200000) return 3;
819 if (size < 0x10000000) return 4;
820 return 5;
823 static ULONG string_table_size( const WS_XML_DICTIONARY *dict )
825 ULONG i, size = 0;
826 for (i = 0; i < dict->stringCount; i++)
827 size += size_length( dict->strings[i].length ) + dict->strings[i].length;
828 return size;
831 static HRESULT send_string_table( SOCKET socket, const WS_XML_DICTIONARY *dict )
833 ULONG i;
834 HRESULT hr;
835 for (i = 0; i < dict->stringCount; i++)
837 if ((hr = send_size( socket, dict->strings[i].length )) != S_OK) return hr;
838 if ((hr = send_bytes( socket, dict->strings[i].bytes, dict->strings[i].length )) != S_OK) return hr;
840 return S_OK;
843 static HRESULT string_to_utf8( const WS_STRING *str, unsigned char **ret, int *len )
845 *len = WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, NULL, 0, NULL, NULL );
846 if (!(*ret = heap_alloc( *len ))) return E_OUTOFMEMORY;
847 WideCharToMultiByte( CP_UTF8, 0, str->chars, str->length, (char *)*ret, *len, NULL, NULL );
848 return S_OK;
851 enum session_mode
853 SESSION_MODE_INVALID = 0,
854 SESSION_MODE_SINGLETON = 1,
855 SESSION_MODE_DUPLEX = 2,
856 SESSION_MODE_SIMPLEX = 3,
859 static enum session_mode map_channel_type( struct channel *channel )
861 switch (channel->type)
863 case WS_CHANNEL_TYPE_DUPLEX_SESSION: return SESSION_MODE_DUPLEX;
864 default:
865 FIXME( "unhandled channel type %08x\n", channel->type );
866 return SESSION_MODE_INVALID;
870 enum known_encoding
872 KNOWN_ENCODING_SOAP11_UTF8 = 0x00,
873 KNOWN_ENCODING_SOAP11_UTF16 = 0x01,
874 KNOWN_ENCODING_SOAP11_UTF16LE = 0x02,
875 KNOWN_ENCODING_SOAP12_UTF8 = 0x03,
876 KNOWN_ENCODING_SOAP12_UTF16 = 0x04,
877 KNOWN_ENCODING_SOAP12_UTF16LE = 0x05,
878 KNOWN_ENCODING_SOAP12_MTOM = 0x06,
879 KNOWN_ENCODING_SOAP12_BINARY = 0x07,
880 KNOWN_ENCODING_SOAP12_BINARY_SESSION = 0x08,
883 static enum known_encoding map_channel_encoding( struct channel *channel )
885 WS_ENVELOPE_VERSION version;
887 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_ENVELOPE_VERSION, &version, sizeof(version) );
889 switch (version)
891 case WS_ENVELOPE_VERSION_SOAP_1_1:
892 switch (channel->encoding)
894 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP11_UTF8;
895 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP11_UTF16LE;
896 default:
897 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
898 return 0;
900 case WS_ENVELOPE_VERSION_SOAP_1_2:
901 switch (channel->encoding)
903 case WS_ENCODING_XML_UTF8: return KNOWN_ENCODING_SOAP12_UTF8;
904 case WS_ENCODING_XML_UTF16LE: return KNOWN_ENCODING_SOAP12_UTF16LE;
905 case WS_ENCODING_XML_BINARY_1: return KNOWN_ENCODING_SOAP12_BINARY;
906 case WS_ENCODING_XML_BINARY_SESSION_1: return KNOWN_ENCODING_SOAP12_BINARY_SESSION;
907 default:
908 FIXME( "unhandled version/encoding %u/%u\n", version, channel->encoding );
909 return 0;
911 default:
912 ERR( "unhandled version %u\n", version );
913 return 0;
917 #define FRAME_VERSION_MAJOR 1
918 #define FRAME_VERSION_MINOR 1
920 static HRESULT send_preamble( struct channel *channel )
922 unsigned char *url;
923 HRESULT hr;
924 int len;
926 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VERSION )) != S_OK) return hr;
927 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MAJOR )) != S_OK) return hr;
928 if ((hr = send_byte( channel->u.tcp.socket, FRAME_VERSION_MINOR )) != S_OK) return hr;
930 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_MODE )) != S_OK) return hr;
931 if ((hr = send_byte( channel->u.tcp.socket, map_channel_type(channel) )) != S_OK) return hr;
933 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_VIA )) != S_OK) return hr;
934 if ((hr = string_to_utf8( &channel->addr.url, &url, &len )) != S_OK) return hr;
935 if ((hr = send_size( channel->u.tcp.socket, len )) != S_OK) goto done;
936 if ((hr = send_bytes( channel->u.tcp.socket, url, len )) != S_OK) goto done;
938 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_KNOWN_ENCODING )) != S_OK) goto done;
939 if ((hr = send_byte( channel->u.tcp.socket, map_channel_encoding(channel) )) != S_OK) goto done;
940 hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_END );
942 done:
943 heap_free( url );
944 return hr;
947 static HRESULT receive_bytes( struct channel *channel, unsigned char *bytes, int len )
949 int count = recv( channel->u.tcp.socket, (char *)bytes, len, 0 );
950 if (count < 0) return HRESULT_FROM_WIN32( WSAGetLastError() );
951 if (count != len) return WS_E_INVALID_FORMAT;
952 return S_OK;
955 static HRESULT receive_preamble_ack( struct channel *channel )
957 unsigned char byte;
958 HRESULT hr;
960 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
961 if (byte != FRAME_RECORD_TYPE_PREAMBLE_ACK) return WS_E_INVALID_FORMAT;
962 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
963 return S_OK;
966 static HRESULT send_sized_envelope( struct channel *channel, BYTE *data, ULONG len )
968 ULONG table_size = string_table_size( &channel->dict.dict );
969 HRESULT hr;
971 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_SIZED_ENVELOPE )) != S_OK) return hr;
972 if ((hr = send_size( channel->u.tcp.socket, size_length(table_size) + table_size + len )) != S_OK) return hr;
973 if ((hr = send_size( channel->u.tcp.socket, table_size )) != S_OK) return hr;
974 if ((hr = send_string_table( channel->u.tcp.socket, &channel->dict.dict )) != S_OK) return hr;
975 return send_bytes( channel->u.tcp.socket, data, len );
978 static HRESULT open_http_request( struct channel *channel, HINTERNET *req )
980 static const WCHAR postW[] = {'P','O','S','T',0};
981 if ((*req = WinHttpOpenRequest( channel->u.http.connect, postW, channel->u.http.path,
982 NULL, NULL, NULL, channel->u.http.flags ))) return S_OK;
983 return HRESULT_FROM_WIN32( GetLastError() );
986 static HRESULT send_message( struct channel *channel, WS_MESSAGE *msg )
988 WS_XML_WRITER *writer;
989 WS_BYTES buf;
990 HRESULT hr;
992 channel->msg = msg;
993 if ((hr = connect_channel( channel )) != S_OK) return hr;
995 WsGetMessageProperty( channel->msg, WS_MESSAGE_PROPERTY_BODY_WRITER, &writer, sizeof(writer), NULL );
996 WsGetWriterProperty( writer, WS_XML_WRITER_PROPERTY_BYTES, &buf, sizeof(buf), NULL );
998 switch (channel->binding)
1000 case WS_HTTP_CHANNEL_BINDING:
1001 if (channel->u.http.request)
1003 WinHttpCloseHandle( channel->u.http.request );
1004 channel->u.http.request = NULL;
1006 if ((hr = open_http_request( channel, &channel->u.http.request )) != S_OK) return hr;
1007 if ((hr = message_insert_http_headers( msg, channel->u.http.request )) != S_OK) return hr;
1008 return send_message_http( channel->u.http.request, buf.bytes, buf.length );
1010 case WS_TCP_CHANNEL_BINDING:
1011 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1013 switch (channel->session_state)
1015 case SESSION_STATE_UNINITIALIZED:
1016 if ((hr = send_preamble( channel )) != S_OK) return hr;
1017 if ((hr = receive_preamble_ack( channel )) != S_OK) return hr;
1018 /* fall through */
1020 case SESSION_STATE_SETUP_COMPLETE:
1021 return send_sized_envelope( channel, buf.bytes, buf.length );
1023 default:
1024 ERR( "unhandled session state %u\n", channel->session_state );
1025 return WS_E_OTHER;
1028 return send_bytes( channel->u.tcp.socket, buf.bytes, buf.length );
1030 case WS_UDP_CHANNEL_BINDING:
1031 return send_bytes( channel->u.udp.socket, buf.bytes, buf.length );
1033 default:
1034 ERR( "unhandled binding %u\n", channel->binding );
1035 return E_NOTIMPL;
1039 HRESULT channel_send_message( WS_CHANNEL *handle, WS_MESSAGE *msg )
1041 struct channel *channel = (struct channel *)handle;
1042 HRESULT hr;
1044 EnterCriticalSection( &channel->cs );
1046 if (channel->magic != CHANNEL_MAGIC)
1048 LeaveCriticalSection( &channel->cs );
1049 return E_INVALIDARG;
1052 hr = send_message( channel, msg );
1054 LeaveCriticalSection( &channel->cs );
1055 return hr;
1058 static HRESULT CALLBACK dict_cb( void *state, const WS_XML_STRING *str, BOOL *found, ULONG *id, WS_ERROR *error )
1060 struct dictionary *dict = state;
1061 HRESULT hr = S_OK;
1062 int index;
1064 if ((index = find_string( dict, str->bytes, str->length, id )) == -1 ||
1065 (hr = insert_string( dict, str->bytes, str->length, index, id )) == S_OK)
1067 *found = TRUE;
1068 return S_OK;
1070 *found = FALSE;
1071 return hr;
1074 static HRESULT init_writer( struct channel *channel )
1076 WS_XML_WRITER_BUFFER_OUTPUT buf = {{WS_XML_WRITER_OUTPUT_TYPE_BUFFER}};
1077 WS_XML_WRITER_TEXT_ENCODING text = {{WS_XML_WRITER_ENCODING_TYPE_TEXT}};
1078 WS_XML_WRITER_BINARY_ENCODING bin = {{WS_XML_WRITER_ENCODING_TYPE_BINARY}};
1079 WS_XML_WRITER_ENCODING *encoding;
1080 HRESULT hr;
1082 if (!channel->writer && (hr = WsCreateWriter( NULL, 0, &channel->writer, NULL )) != S_OK) return hr;
1084 switch (channel->encoding)
1086 case WS_ENCODING_XML_UTF8:
1087 text.charSet = WS_CHARSET_UTF8;
1088 encoding = &text.encoding;
1089 break;
1091 case WS_ENCODING_XML_BINARY_SESSION_1:
1092 if ((hr = writer_enable_lookup( channel->writer )) != S_OK) return hr;
1093 clear_dict( &channel->dict );
1094 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1095 bin.dynamicStringCallback = dict_cb;
1096 bin.dynamicStringCallbackState = &channel->dict;
1097 encoding = &bin.encoding;
1098 break;
1100 case WS_ENCODING_XML_BINARY_1:
1101 encoding = &bin.encoding;
1102 break;
1104 default:
1105 FIXME( "unhandled encoding %u\n", channel->encoding );
1106 return WS_E_NOT_SUPPORTED;
1109 return WsSetOutput( channel->writer, encoding, &buf.output, NULL, 0, NULL );
1112 /**************************************************************************
1113 * WsSendMessage [webservices.@]
1115 HRESULT WINAPI WsSendMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1116 WS_WRITE_OPTION option, const void *body, ULONG size, const WS_ASYNC_CONTEXT *ctx,
1117 WS_ERROR *error )
1119 struct channel *channel = (struct channel *)handle;
1120 HRESULT hr;
1122 TRACE( "%p %p %p %08x %p %u %p %p\n", handle, msg, desc, option, body, size, ctx, error );
1123 if (error) FIXME( "ignoring error parameter\n" );
1124 if (ctx) FIXME( "ignoring ctx parameter\n" );
1126 if (!channel || !msg || !desc) return E_INVALIDARG;
1128 EnterCriticalSection( &channel->cs );
1130 if (channel->magic != CHANNEL_MAGIC)
1132 LeaveCriticalSection( &channel->cs );
1133 return E_INVALIDARG;
1136 if ((hr = WsInitializeMessage( msg, WS_REQUEST_MESSAGE, NULL, NULL )) != S_OK) goto done;
1137 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1138 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1140 if ((hr = init_writer( channel )) != S_OK) goto done;
1141 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1142 goto done;
1143 hr = send_message( channel, msg );
1145 done:
1146 LeaveCriticalSection( &channel->cs );
1147 return hr;
1150 /**************************************************************************
1151 * WsSendReplyMessage [webservices.@]
1153 HRESULT WINAPI WsSendReplyMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION *desc,
1154 WS_WRITE_OPTION option, const void *body, ULONG size, WS_MESSAGE *request,
1155 const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1157 struct channel *channel = (struct channel *)handle;
1158 GUID req_id;
1159 HRESULT hr;
1161 TRACE( "%p %p %p %08x %p %u %p %p %p\n", handle, msg, desc, option, body, size, request, ctx, error );
1162 if (error) FIXME( "ignoring error parameter\n" );
1163 if (ctx) FIXME( "ignoring ctx parameter\n" );
1165 if (!channel || !msg || !desc || !request) return E_INVALIDARG;
1167 EnterCriticalSection( &channel->cs );
1169 if (channel->magic != CHANNEL_MAGIC)
1171 LeaveCriticalSection( &channel->cs );
1172 return E_INVALIDARG;
1175 if ((hr = WsInitializeMessage( msg, WS_REPLY_MESSAGE, NULL, NULL )) != S_OK) goto done;
1176 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1177 if ((hr = message_set_action( msg, desc->action )) != S_OK) goto done;
1178 if ((hr = message_get_id( request, &req_id )) != S_OK) goto done;
1179 if ((hr = message_set_request_id( msg, &req_id )) != S_OK) goto done;
1181 if ((hr = init_writer( channel )) != S_OK) goto done;
1182 if ((hr = write_message( msg, channel->writer, desc->bodyElementDescription, option, body, size )) != S_OK)
1183 goto done;
1184 hr = send_message( channel, msg );
1186 done:
1187 LeaveCriticalSection( &channel->cs );
1188 FIXME( "returning %08x\n", hr );
1189 return hr;
1192 static HRESULT resize_read_buffer( struct channel *channel, ULONG size )
1194 if (!channel->read_buf)
1196 if (!(channel->read_buf = heap_alloc( size ))) return E_OUTOFMEMORY;
1197 channel->read_buflen = size;
1198 return S_OK;
1200 if (channel->read_buflen < size)
1202 char *tmp;
1203 ULONG new_size = max( size, channel->read_buflen * 2 );
1204 if (!(tmp = heap_realloc( channel->read_buf, new_size ))) return E_OUTOFMEMORY;
1205 channel->read_buf = tmp;
1206 channel->read_buflen = new_size;
1208 return S_OK;
1211 static HRESULT init_reader( struct channel *channel )
1213 WS_XML_READER_BUFFER_INPUT buf = {{WS_XML_READER_INPUT_TYPE_BUFFER}};
1214 WS_XML_READER_TEXT_ENCODING text = {{WS_XML_READER_ENCODING_TYPE_TEXT}};
1215 WS_XML_READER_BINARY_ENCODING bin = {{WS_XML_READER_ENCODING_TYPE_BINARY}};
1216 WS_XML_READER_ENCODING *encoding;
1217 HRESULT hr;
1219 if (!channel->reader && (hr = WsCreateReader( NULL, 0, &channel->reader, NULL )) != S_OK) return hr;
1221 switch (channel->encoding)
1223 case WS_ENCODING_XML_UTF8:
1224 text.charSet = WS_CHARSET_UTF8;
1225 encoding = &text.encoding;
1226 break;
1228 case WS_ENCODING_XML_BINARY_SESSION_1:
1229 bin.staticDictionary = (WS_XML_DICTIONARY *)&dict_builtin_static.dict;
1230 bin.dynamicDictionary = &channel->dict.dict;
1231 /* fall through */
1233 case WS_ENCODING_XML_BINARY_1:
1234 encoding = &bin.encoding;
1235 break;
1237 default:
1238 FIXME( "unhandled encoding %u\n", channel->encoding );
1239 return WS_E_NOT_SUPPORTED;
1242 buf.encodedData = channel->read_buf;
1243 buf.encodedDataSize = channel->read_size;
1244 return WsSetInput( channel->reader, encoding, &buf.input, NULL, 0, NULL );
1247 #define INITIAL_READ_BUFFER_SIZE 4096
1248 static HRESULT receive_message_http( struct channel *channel )
1250 DWORD len, bytes_read, offset = 0, size = INITIAL_READ_BUFFER_SIZE;
1251 ULONG max_len;
1252 HRESULT hr;
1254 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1255 &max_len, sizeof(max_len) );
1257 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1258 channel->read_size = 0;
1259 for (;;)
1261 if (!WinHttpQueryDataAvailable( channel->u.http.request, &len ))
1263 return HRESULT_FROM_WIN32( GetLastError() );
1265 if (!len) break;
1266 if (channel->read_size + len > max_len) return WS_E_QUOTA_EXCEEDED;
1267 if ((hr = resize_read_buffer( channel, channel->read_size + len )) != S_OK) return hr;
1269 if (!WinHttpReadData( channel->u.http.request, channel->read_buf + offset, len, &bytes_read ))
1271 return HRESULT_FROM_WIN32( GetLastError() );
1273 if (!bytes_read) break;
1274 channel->read_size += bytes_read;
1275 offset += bytes_read;
1278 return init_reader( channel );
1281 static HRESULT receive_message_unsized( struct channel *channel, SOCKET socket )
1283 int bytes_read;
1284 ULONG max_len;
1285 HRESULT hr;
1287 prop_get( channel->prop, channel->prop_count, WS_CHANNEL_PROPERTY_MAX_BUFFERED_MESSAGE_SIZE,
1288 &max_len, sizeof(max_len) );
1290 if ((hr = resize_read_buffer( channel, max_len )) != S_OK) return hr;
1292 channel->read_size = 0;
1293 if ((bytes_read = recv( socket, channel->read_buf, max_len, 0 )) < 0)
1295 return HRESULT_FROM_WIN32( WSAGetLastError() );
1297 channel->read_size = bytes_read;
1298 return S_OK;
1301 static HRESULT receive_message_sized( struct channel *channel, unsigned int size )
1303 unsigned int offset = 0, to_read = size;
1304 int bytes_read;
1305 HRESULT hr;
1307 if ((hr = resize_read_buffer( channel, size )) != S_OK) return hr;
1309 channel->read_size = 0;
1310 while (channel->read_size < size)
1312 if ((bytes_read = recv( channel->u.tcp.socket, channel->read_buf + offset, to_read, 0 )) < 0)
1314 return HRESULT_FROM_WIN32( WSAGetLastError() );
1316 if (!bytes_read) break;
1317 channel->read_size += bytes_read;
1318 to_read -= bytes_read;
1319 offset += bytes_read;
1321 if (channel->read_size != size) return WS_E_INVALID_FORMAT;
1322 return S_OK;
1325 static HRESULT receive_size( struct channel *channel, unsigned int *size )
1327 unsigned char byte;
1328 HRESULT hr;
1330 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1331 *size = byte & 0x7f;
1332 if (!(byte & 0x80)) return S_OK;
1334 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1335 *size += (byte & 0x7f) << 7;
1336 if (!(byte & 0x80)) return S_OK;
1338 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1339 *size += (byte & 0x7f) << 14;
1340 if (!(byte & 0x80)) return S_OK;
1342 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1343 *size += (byte & 0x7f) << 21;
1344 if (!(byte & 0x80)) return S_OK;
1346 if ((hr = receive_bytes( channel, &byte, 1 )) != S_OK) return hr;
1347 if (byte & ~0x0f) return WS_E_INVALID_FORMAT;
1348 *size += byte << 28;
1349 return S_OK;
1352 static WS_ENCODING map_known_encoding( enum known_encoding encoding )
1354 switch (encoding)
1356 case KNOWN_ENCODING_SOAP11_UTF8:
1357 case KNOWN_ENCODING_SOAP12_UTF8: return WS_ENCODING_XML_UTF8;
1358 case KNOWN_ENCODING_SOAP11_UTF16:
1359 case KNOWN_ENCODING_SOAP12_UTF16: return WS_ENCODING_XML_UTF16BE;
1360 case KNOWN_ENCODING_SOAP11_UTF16LE:
1361 case KNOWN_ENCODING_SOAP12_UTF16LE: return WS_ENCODING_XML_UTF16LE;
1362 case KNOWN_ENCODING_SOAP12_BINARY: return WS_ENCODING_XML_BINARY_1;
1363 case KNOWN_ENCODING_SOAP12_BINARY_SESSION: return WS_ENCODING_XML_BINARY_SESSION_1;
1364 default:
1365 WARN( "unhandled encoding %u, assuming UTF8\n", encoding );
1366 return WS_ENCODING_XML_UTF8;
1370 static HRESULT receive_preamble( struct channel *channel )
1372 unsigned char type;
1373 HRESULT hr;
1375 for (;;)
1377 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1378 if (type == FRAME_RECORD_TYPE_PREAMBLE_END) break;
1379 switch (type)
1381 case FRAME_RECORD_TYPE_VERSION:
1383 unsigned char major, minor;
1384 if ((hr = receive_bytes( channel, &major, 1 )) != S_OK) return hr;
1385 if ((hr = receive_bytes( channel, &minor, 1 )) != S_OK) return hr;
1386 TRACE( "major %u minor %u\n", major, major );
1387 break;
1389 case FRAME_RECORD_TYPE_MODE:
1391 unsigned char mode;
1392 if ((hr = receive_bytes( channel, &mode, 1 )) != S_OK) return hr;
1393 TRACE( "mode %u\n", mode );
1394 break;
1396 case FRAME_RECORD_TYPE_VIA:
1398 unsigned int size;
1399 unsigned char *url;
1401 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1402 if (!(url = heap_alloc( size ))) return E_OUTOFMEMORY;
1403 if ((hr = receive_bytes( channel, url, size )) != S_OK)
1405 heap_free( url );
1406 return hr;
1408 TRACE( "transport URL %s\n", debugstr_an((char *)url, size) );
1409 heap_free( url ); /* FIXME: verify */
1410 break;
1412 case FRAME_RECORD_TYPE_KNOWN_ENCODING:
1414 unsigned char encoding;
1415 if ((hr = receive_bytes( channel, &encoding, 1 )) != S_OK) return hr;
1416 TRACE( "encoding %u\n", encoding );
1417 channel->encoding = map_known_encoding( encoding );
1418 break;
1420 default:
1421 WARN( "unhandled record type %u\n", type );
1422 return WS_E_INVALID_FORMAT;
1426 return S_OK;
1429 static HRESULT receive_sized_envelope( struct channel *channel )
1431 unsigned char type;
1432 unsigned int size;
1433 HRESULT hr;
1435 if ((hr = receive_bytes( channel, &type, 1 )) != S_OK) return hr;
1436 if (type != FRAME_RECORD_TYPE_SIZED_ENVELOPE) return WS_E_INVALID_FORMAT;
1437 if ((hr = receive_size( channel, &size )) != S_OK) return hr;
1438 if ((hr = receive_message_sized( channel, size )) != S_OK) return hr;
1439 return S_OK;
1442 static HRESULT read_size( const BYTE **ptr, ULONG len, ULONG *size )
1444 const BYTE *buf = *ptr;
1446 if (len < 1) return WS_E_INVALID_FORMAT;
1447 *size = buf[0] & 0x7f;
1448 if (!(buf[0] & 0x80))
1450 *ptr += 1;
1451 return S_OK;
1453 if (len < 2) return WS_E_INVALID_FORMAT;
1454 *size += (buf[1] & 0x7f) << 7;
1455 if (!(buf[1] & 0x80))
1457 *ptr += 2;
1458 return S_OK;
1460 if (len < 3) return WS_E_INVALID_FORMAT;
1461 *size += (buf[2] & 0x7f) << 14;
1462 if (!(buf[2] & 0x80))
1464 *ptr += 3;
1465 return S_OK;
1467 if (len < 4) return WS_E_INVALID_FORMAT;
1468 *size += (buf[3] & 0x7f) << 21;
1469 if (!(buf[3] & 0x80))
1471 *ptr += 4;
1472 return S_OK;
1474 if (len < 5 || (buf[4] & ~0x07)) return WS_E_INVALID_FORMAT;
1475 *size += buf[4] << 28;
1476 *ptr += 5;
1477 return S_OK;
1480 static HRESULT build_dict( const BYTE *buf, ULONG buflen, struct dictionary *dict, ULONG *used )
1482 ULONG size, strings_size, strings_offset;
1483 const BYTE *ptr = buf;
1484 BYTE *bytes;
1485 int index;
1486 HRESULT hr;
1488 if ((hr = read_size( &ptr, buflen, &strings_size )) != S_OK) return hr;
1489 strings_offset = ptr - buf;
1490 if (buflen < strings_offset + strings_size) return WS_E_INVALID_FORMAT;
1491 *used = strings_offset + strings_size;
1492 if (!strings_size) return S_OK;
1494 UuidCreate( &dict->dict.guid );
1495 dict->dict.isConst = FALSE;
1497 buflen -= strings_offset;
1498 ptr = buf + strings_offset;
1499 while (ptr < buf + strings_size)
1501 if ((hr = read_size( &ptr, buflen, &size )) != S_OK)
1503 clear_dict( dict );
1504 return hr;
1506 if (size > buflen)
1508 clear_dict( dict );
1509 return WS_E_INVALID_FORMAT;
1511 buflen -= size;
1512 if (!(bytes = heap_alloc( size )))
1514 hr = E_OUTOFMEMORY;
1515 goto error;
1517 memcpy( bytes, ptr, size );
1518 if ((index = find_string( dict, bytes, size, NULL )) == -1) /* duplicate */
1520 heap_free( bytes );
1521 ptr += size;
1522 continue;
1524 if ((hr = insert_string( dict, bytes, size, index, NULL )) != S_OK)
1526 clear_dict( dict );
1527 return hr;
1529 ptr += size;
1531 return S_OK;
1533 error:
1534 clear_dict( dict );
1535 return hr;
1538 static HRESULT send_preamble_ack( struct channel *channel )
1540 HRESULT hr;
1541 if ((hr = send_byte( channel->u.tcp.socket, FRAME_RECORD_TYPE_PREAMBLE_ACK )) != S_OK) return hr;
1542 channel->session_state = SESSION_STATE_SETUP_COMPLETE;
1543 return S_OK;
1546 static HRESULT receive_message_session_setup( struct channel *channel )
1548 HRESULT hr;
1550 if ((hr = receive_preamble( channel )) != S_OK) return hr;
1551 if ((hr = send_preamble_ack( channel )) != S_OK) return hr;
1552 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1553 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1555 ULONG size;
1556 if ((hr = build_dict( (const BYTE *)channel->read_buf, channel->read_size, &channel->dict, &size )) != S_OK)
1557 return hr;
1558 channel->read_size -= size;
1559 memmove( channel->read_buf, channel->read_buf + size, channel->read_size );
1562 return init_reader( channel );
1565 static HRESULT receive_message_session( struct channel *channel )
1567 HRESULT hr;
1568 if ((hr = receive_sized_envelope( channel )) != S_OK) return hr;
1569 return init_reader( channel );
1572 static HRESULT receive_message_sock( struct channel *channel, SOCKET socket )
1574 HRESULT hr;
1575 if ((hr = receive_message_unsized( channel, socket )) != S_OK) return hr;
1576 return init_reader( channel );
1579 static HRESULT receive_message( struct channel *channel )
1581 HRESULT hr;
1582 if ((hr = connect_channel( channel )) != S_OK) return hr;
1584 switch (channel->binding)
1586 case WS_HTTP_CHANNEL_BINDING:
1587 return receive_message_http( channel );
1589 case WS_TCP_CHANNEL_BINDING:
1590 if (channel->encoding == WS_ENCODING_XML_BINARY_SESSION_1)
1592 switch (channel->session_state)
1594 case SESSION_STATE_UNINITIALIZED:
1595 return receive_message_session_setup( channel );
1597 case SESSION_STATE_SETUP_COMPLETE:
1598 return receive_message_session( channel );
1600 default:
1601 ERR( "unhandled session state %u\n", channel->session_state );
1602 return WS_E_OTHER;
1605 return receive_message_sock( channel, channel->u.tcp.socket );
1607 case WS_UDP_CHANNEL_BINDING:
1608 return receive_message_sock( channel, channel->u.udp.socket );
1610 default:
1611 ERR( "unhandled binding %u\n", channel->binding );
1612 return E_NOTIMPL;
1616 HRESULT channel_receive_message( WS_CHANNEL *handle )
1618 struct channel *channel = (struct channel *)handle;
1619 HRESULT hr;
1621 EnterCriticalSection( &channel->cs );
1623 if (channel->magic != CHANNEL_MAGIC)
1625 LeaveCriticalSection( &channel->cs );
1626 return E_INVALIDARG;
1629 hr = receive_message( channel );
1631 LeaveCriticalSection( &channel->cs );
1632 return hr;
1635 HRESULT channel_get_reader( WS_CHANNEL *handle, WS_XML_READER **reader )
1637 struct channel *channel = (struct channel *)handle;
1639 EnterCriticalSection( &channel->cs );
1641 if (channel->magic != CHANNEL_MAGIC)
1643 LeaveCriticalSection( &channel->cs );
1644 return E_INVALIDARG;
1647 *reader = channel->reader;
1649 LeaveCriticalSection( &channel->cs );
1650 return S_OK;
1653 static HRESULT read_message( WS_MESSAGE *handle, WS_XML_READER *reader, const WS_ELEMENT_DESCRIPTION *desc,
1654 WS_READ_OPTION option, WS_HEAP *heap, void *body, ULONG size )
1656 HRESULT hr;
1657 if ((hr = WsReadEnvelopeStart( handle, reader, NULL, NULL, NULL )) != S_OK) return hr;
1658 if ((hr = WsReadBody( handle, desc, option, heap, body, size, NULL )) != S_OK) return hr;
1659 return WsReadEnvelopeEnd( handle, NULL );
1662 /**************************************************************************
1663 * WsReceiveMessage [webservices.@]
1665 HRESULT WINAPI WsReceiveMessage( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_MESSAGE_DESCRIPTION **desc,
1666 ULONG count, WS_RECEIVE_OPTION option, WS_READ_OPTION read_option, WS_HEAP *heap,
1667 void *value, ULONG size, ULONG *index, const WS_ASYNC_CONTEXT *ctx, WS_ERROR *error )
1669 struct channel *channel = (struct channel *)handle;
1670 HRESULT hr;
1671 ULONG i;
1673 TRACE( "%p %p %p %u %08x %08x %p %p %u %p %p %p\n", handle, msg, desc, count, option, read_option, heap,
1674 value, size, index, ctx, error );
1675 if (error) FIXME( "ignoring error parameter\n" );
1676 if (ctx) FIXME( "ignoring ctx parameter\n" );
1678 if (!channel || !msg || !desc || !count) return E_INVALIDARG;
1680 EnterCriticalSection( &channel->cs );
1682 if (channel->magic != CHANNEL_MAGIC)
1684 LeaveCriticalSection( &channel->cs );
1685 return E_INVALIDARG;
1688 if (!channel->read_size) hr = receive_message( channel );
1689 else if (option == WS_RECEIVE_OPTIONAL_MESSAGE) hr = WS_S_END;
1690 else hr = WS_E_INVALID_FORMAT;
1692 if (hr != S_OK) goto done;
1694 for (i = 0; i < count; i++)
1696 if ((hr = read_message( msg, channel->reader, desc[i]->bodyElementDescription, read_option, heap,
1697 value, size )) == S_OK)
1699 if (index) *index = i;
1700 break;
1702 if ((hr = init_reader( channel )) != S_OK) break;
1705 done:
1706 LeaveCriticalSection( &channel->cs );
1707 return hr;
1710 /**************************************************************************
1711 * WsReadMessageStart [webservices.@]
1713 HRESULT WINAPI WsReadMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1714 WS_ERROR *error )
1716 struct channel *channel = (struct channel *)handle;
1717 HRESULT hr;
1719 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1720 if (error) FIXME( "ignoring error parameter\n" );
1721 if (ctx) FIXME( "ignoring ctx parameter\n" );
1723 if (!channel || !msg) return E_INVALIDARG;
1725 EnterCriticalSection( &channel->cs );
1727 if (channel->magic != CHANNEL_MAGIC)
1729 LeaveCriticalSection( &channel->cs );
1730 return E_INVALIDARG;
1733 if ((hr = receive_message( channel )) == S_OK)
1735 hr = WsReadEnvelopeStart( msg, channel->reader, NULL, NULL, NULL );
1738 LeaveCriticalSection( &channel->cs );
1739 return hr;
1742 /**************************************************************************
1743 * WsReadMessageEnd [webservices.@]
1745 HRESULT WINAPI WsReadMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1746 WS_ERROR *error )
1748 struct channel *channel = (struct channel *)handle;
1749 HRESULT hr;
1751 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1752 if (error) FIXME( "ignoring error parameter\n" );
1753 if (ctx) FIXME( "ignoring ctx parameter\n" );
1755 if (!channel || !msg) return E_INVALIDARG;
1757 EnterCriticalSection( &channel->cs );
1759 if (channel->magic != CHANNEL_MAGIC)
1761 LeaveCriticalSection( &channel->cs );
1762 return E_INVALIDARG;
1765 hr = WsReadEnvelopeEnd( msg, NULL );
1767 LeaveCriticalSection( &channel->cs );
1768 return hr;
1771 /**************************************************************************
1772 * WsWriteMessageStart [webservices.@]
1774 HRESULT WINAPI WsWriteMessageStart( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1775 WS_ERROR *error )
1777 struct channel *channel = (struct channel *)handle;
1778 HRESULT hr;
1780 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1781 if (error) FIXME( "ignoring error parameter\n" );
1782 if (ctx) FIXME( "ignoring ctx parameter\n" );
1784 if (!channel || !msg) return E_INVALIDARG;
1786 EnterCriticalSection( &channel->cs );
1788 if (channel->magic != CHANNEL_MAGIC)
1790 LeaveCriticalSection( &channel->cs );
1791 return E_INVALIDARG;
1794 if ((hr = init_writer( channel )) != S_OK) goto done;
1795 if ((hr = WsAddressMessage( msg, &channel->addr, NULL )) != S_OK) goto done;
1796 hr = WsWriteEnvelopeStart( msg, channel->writer, NULL, NULL, NULL );
1798 done:
1799 LeaveCriticalSection( &channel->cs );
1800 return hr;
1803 /**************************************************************************
1804 * WsWriteMessageEnd [webservices.@]
1806 HRESULT WINAPI WsWriteMessageEnd( WS_CHANNEL *handle, WS_MESSAGE *msg, const WS_ASYNC_CONTEXT *ctx,
1807 WS_ERROR *error )
1809 struct channel *channel = (struct channel *)handle;
1810 HRESULT hr;
1812 TRACE( "%p %p %p %p\n", handle, msg, ctx, error );
1813 if (error) FIXME( "ignoring error parameter\n" );
1814 if (ctx) FIXME( "ignoring ctx parameter\n" );
1816 if (!channel || !msg) return E_INVALIDARG;
1818 EnterCriticalSection( &channel->cs );
1820 if (channel->magic != CHANNEL_MAGIC)
1822 LeaveCriticalSection( &channel->cs );
1823 return E_INVALIDARG;
1826 if ((hr = WsWriteEnvelopeEnd( msg, NULL )) == S_OK) hr = send_message( channel, msg );
1828 LeaveCriticalSection( &channel->cs );
1829 return hr;
1832 static HRESULT sock_accept( SOCKET socket, HANDLE wait, HANDLE cancel, SOCKET *ret )
1834 HANDLE handles[] = { wait, cancel };
1835 ULONG nonblocking = 0;
1836 HRESULT hr = S_OK;
1838 if (WSAEventSelect( socket, handles[0], FD_ACCEPT )) return HRESULT_FROM_WIN32( WSAGetLastError() );
1840 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
1842 case 0:
1843 if ((*ret = accept( socket, NULL, NULL )) != -1)
1845 WSAEventSelect( *ret, NULL, 0 );
1846 ioctlsocket( *ret, FIONBIO, &nonblocking );
1847 break;
1849 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1850 break;
1852 case 1:
1853 hr = WS_E_OPERATION_ABORTED;
1854 break;
1856 default:
1857 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1858 break;
1861 return hr;
1864 HRESULT channel_accept_tcp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
1866 struct channel *channel = (struct channel *)handle;
1867 HRESULT hr;
1869 EnterCriticalSection( &channel->cs );
1871 if (channel->magic != CHANNEL_MAGIC)
1873 LeaveCriticalSection( &channel->cs );
1874 return E_INVALIDARG;
1877 hr = sock_accept( socket, wait, cancel, &channel->u.tcp.socket );
1879 LeaveCriticalSection( &channel->cs );
1880 return hr;
1883 static HRESULT sock_wait( SOCKET socket, HANDLE wait, HANDLE cancel )
1885 HANDLE handles[] = { wait, cancel };
1886 ULONG nonblocking = 0;
1887 HRESULT hr;
1889 if (WSAEventSelect( socket, handles[0], FD_READ )) return HRESULT_FROM_WIN32( WSAGetLastError() );
1891 switch (WSAWaitForMultipleEvents( 2, handles, FALSE, WSA_INFINITE, FALSE ))
1893 case 0:
1894 hr = S_OK;
1895 break;
1897 case 1:
1898 hr = WS_E_OPERATION_ABORTED;
1899 break;
1901 default:
1902 hr = HRESULT_FROM_WIN32( WSAGetLastError() );
1903 break;
1906 WSAEventSelect( socket, NULL, 0 );
1907 ioctlsocket( socket, FIONBIO, &nonblocking );
1908 return hr;
1911 HRESULT channel_accept_udp( SOCKET socket, HANDLE wait, HANDLE cancel, WS_CHANNEL *handle )
1913 struct channel *channel = (struct channel *)handle;
1914 HRESULT hr;
1916 EnterCriticalSection( &channel->cs );
1918 if (channel->magic != CHANNEL_MAGIC)
1920 LeaveCriticalSection( &channel->cs );
1921 return E_INVALIDARG;
1924 if ((hr = sock_wait( socket, wait, cancel )) == S_OK) channel->u.udp.socket = socket;
1926 LeaveCriticalSection( &channel->cs );
1927 return hr;