4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 Mike McCormack
8 * Copyright 2006 Damjan Jovanovic
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define WIN32_NO_STATUS
42 #include "wine/unicode.h"
47 #include "wine/debug.h"
49 #include "rpc_binding.h"
50 #include "rpc_assoc.h"
51 #include "rpc_message.h"
52 #include "rpc_server.h"
53 #include "epm_towers.h"
55 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
57 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
59 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
61 static RpcConnection
*rpcrt4_spawn_connection(RpcConnection
*old_connection
);
63 /**** ncacn_np support ****/
65 typedef struct _RpcConnection_np
71 IO_STATUS_BLOCK io_status
;
76 static RpcConnection
*rpcrt4_conn_np_alloc(void)
78 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
82 static HANDLE
get_np_event(RpcConnection_np
*connection
)
84 HANDLE event
= InterlockedExchangePointer(&connection
->event_cache
, NULL
);
85 return event
? event
: CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
88 static void release_np_event(RpcConnection_np
*connection
, HANDLE event
)
90 event
= InterlockedExchangePointer(&connection
->event_cache
, event
);
95 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*conn
)
97 RpcConnection_np
*connection
= (RpcConnection_np
*) conn
;
99 TRACE("listening on %s\n", connection
->listen_pipe
);
101 connection
->pipe
= CreateNamedPipeA(connection
->listen_pipe
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
102 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
103 PIPE_UNLIMITED_INSTANCES
,
104 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
105 if (connection
->pipe
== INVALID_HANDLE_VALUE
)
107 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
108 if (GetLastError() == ERROR_FILE_EXISTS
)
109 return RPC_S_DUPLICATE_ENDPOINT
;
111 return RPC_S_CANT_CREATE_ENDPOINT
;
117 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
119 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
123 TRACE("connecting to %s\n", pname
);
129 dwFlags
= SECURITY_SQOS_PRESENT
;
130 switch (Connection
->QOS
->qos
->ImpersonationType
)
132 case RPC_C_IMP_LEVEL_DEFAULT
:
133 /* FIXME: what to do here? */
135 case RPC_C_IMP_LEVEL_ANONYMOUS
:
136 dwFlags
|= SECURITY_ANONYMOUS
;
138 case RPC_C_IMP_LEVEL_IDENTIFY
:
139 dwFlags
|= SECURITY_IDENTIFICATION
;
141 case RPC_C_IMP_LEVEL_IMPERSONATE
:
142 dwFlags
|= SECURITY_IMPERSONATION
;
144 case RPC_C_IMP_LEVEL_DELEGATE
:
145 dwFlags
|= SECURITY_DELEGATION
;
148 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
149 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
151 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
152 OPEN_EXISTING
, dwFlags
| FILE_FLAG_OVERLAPPED
, 0);
153 if (pipe
!= INVALID_HANDLE_VALUE
) break;
154 err
= GetLastError();
155 if (err
== ERROR_PIPE_BUSY
) {
156 if (WaitNamedPipeA(pname
, NMPWAIT_USE_DEFAULT_WAIT
)) {
157 TRACE("retrying busy server\n");
160 TRACE("connection failed, error=%x\n", err
);
161 return RPC_S_SERVER_TOO_BUSY
;
163 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
164 err
= GetLastError();
165 WARN("connection failed, error=%x\n", err
);
166 return RPC_S_SERVER_UNAVAILABLE
;
171 /* pipe is connected; change to message-read mode. */
172 dwMode
= PIPE_READMODE_MESSAGE
;
173 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
179 static char *ncalrpc_pipe_name(const char *endpoint
)
181 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
184 /* protseq=ncalrpc: supposed to use NT LPC ports,
185 * but we'll implement it with named pipes for now */
186 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
187 strcat(strcpy(pipe_name
, prefix
), endpoint
);
191 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
193 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
197 /* already connected? */
201 pname
= ncalrpc_pipe_name(Connection
->Endpoint
);
202 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
208 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
211 RpcConnection
*Connection
;
212 char generated_endpoint
[22];
216 static LONG lrpc_nameless_id
;
217 DWORD process_id
= GetCurrentProcessId();
218 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
219 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
220 "LRPC%08x.%08x", process_id
, id
);
221 endpoint
= generated_endpoint
;
224 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
225 endpoint
, NULL
, NULL
, NULL
, NULL
);
229 ((RpcConnection_np
*)Connection
)->listen_pipe
= ncalrpc_pipe_name(Connection
->Endpoint
);
230 r
= rpcrt4_conn_create_pipe(Connection
);
232 EnterCriticalSection(&protseq
->cs
);
233 list_add_head(&protseq
->listeners
, &Connection
->protseq_entry
);
234 Connection
->protseq
= protseq
;
235 LeaveCriticalSection(&protseq
->cs
);
240 static char *ncacn_pipe_name(const char *endpoint
)
242 static const char prefix
[] = "\\\\.";
245 /* protseq=ncacn_np: named pipes */
246 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
247 strcat(strcpy(pipe_name
, prefix
), endpoint
);
251 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
253 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
257 /* already connected? */
261 pname
= ncacn_pipe_name(Connection
->Endpoint
);
262 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
268 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
271 RpcConnection
*Connection
;
272 char generated_endpoint
[26];
276 static LONG np_nameless_id
;
277 DWORD process_id
= GetCurrentProcessId();
278 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
279 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
280 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
281 endpoint
= generated_endpoint
;
284 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
285 endpoint
, NULL
, NULL
, NULL
, NULL
);
289 ((RpcConnection_np
*)Connection
)->listen_pipe
= ncacn_pipe_name(Connection
->Endpoint
);
290 r
= rpcrt4_conn_create_pipe(Connection
);
292 EnterCriticalSection(&protseq
->cs
);
293 list_add_head(&protseq
->listeners
, &Connection
->protseq_entry
);
294 Connection
->protseq
= protseq
;
295 LeaveCriticalSection(&protseq
->cs
);
300 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
302 /* because of the way named pipes work, we'll transfer the connected pipe
303 * to the child, then reopen the server binding to continue listening */
305 new_npc
->pipe
= old_npc
->pipe
;
307 assert(!old_npc
->listen_event
);
310 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
312 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
315 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
316 status
= rpcrt4_conn_create_pipe(old_conn
);
318 /* Store the local computer name as the NetworkAddr for ncacn_np as long as
319 * we don't support named pipes over the network. */
320 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
321 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
323 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
324 return RPC_S_OUT_OF_RESOURCES
;
330 static RPC_STATUS
is_pipe_listening(const char *pipe_name
)
332 return WaitNamedPipeA(pipe_name
, 1) ? RPC_S_OK
: RPC_S_NOT_LISTENING
;
335 static RPC_STATUS
rpcrt4_ncacn_np_is_server_listening(const char *endpoint
)
340 pipe_name
= ncacn_pipe_name(endpoint
);
341 status
= is_pipe_listening(pipe_name
);
342 I_RpcFree(pipe_name
);
346 static RPC_STATUS
rpcrt4_ncalrpc_np_is_server_listening(const char *endpoint
)
351 pipe_name
= ncalrpc_pipe_name(endpoint
);
352 status
= is_pipe_listening(pipe_name
);
353 I_RpcFree(pipe_name
);
357 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
359 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
362 TRACE("%s\n", old_conn
->Endpoint
);
364 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
365 status
= rpcrt4_conn_create_pipe(old_conn
);
367 /* Store the local computer name as the NetworkAddr for ncalrpc. */
368 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
369 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
371 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
372 return RPC_S_OUT_OF_RESOURCES
;
378 static int rpcrt4_conn_np_read(RpcConnection
*conn
, void *buffer
, unsigned int count
)
380 RpcConnection_np
*connection
= (RpcConnection_np
*) conn
;
384 event
= get_np_event(connection
);
388 if (connection
->read_closed
)
389 status
= STATUS_CANCELLED
;
391 status
= NtReadFile(connection
->pipe
, event
, NULL
, NULL
, &connection
->io_status
, buffer
, count
, NULL
, NULL
);
392 if (status
== STATUS_PENDING
)
394 /* check read_closed again before waiting to avoid a race */
395 if (connection
->read_closed
)
397 IO_STATUS_BLOCK io_status
;
398 NtCancelIoFileEx(connection
->pipe
, &connection
->io_status
, &io_status
);
400 WaitForSingleObject(event
, INFINITE
);
401 status
= connection
->io_status
.Status
;
403 release_np_event(connection
, event
);
404 return status
&& status
!= STATUS_BUFFER_OVERFLOW
? -1 : connection
->io_status
.Information
;
407 static int rpcrt4_conn_np_write(RpcConnection
*conn
, const void *buffer
, unsigned int count
)
409 RpcConnection_np
*connection
= (RpcConnection_np
*) conn
;
410 IO_STATUS_BLOCK io_status
;
414 event
= get_np_event(connection
);
418 status
= NtWriteFile(connection
->pipe
, event
, NULL
, NULL
, &io_status
, buffer
, count
, NULL
, NULL
);
419 if (status
== STATUS_PENDING
)
421 WaitForSingleObject(event
, INFINITE
);
422 status
= io_status
.Status
;
424 release_np_event(connection
, event
);
428 assert(io_status
.Information
== count
);
432 static int rpcrt4_conn_np_close(RpcConnection
*conn
)
434 RpcConnection_np
*connection
= (RpcConnection_np
*) conn
;
435 if (connection
->pipe
)
437 FlushFileBuffers(connection
->pipe
);
438 CloseHandle(connection
->pipe
);
439 connection
->pipe
= 0;
441 if (connection
->listen_event
)
443 CloseHandle(connection
->listen_event
);
444 connection
->listen_event
= 0;
446 if (connection
->event_cache
)
448 CloseHandle(connection
->event_cache
);
449 connection
->event_cache
= 0;
454 static void rpcrt4_conn_np_close_read(RpcConnection
*conn
)
456 RpcConnection_np
*connection
= (RpcConnection_np
*)conn
;
457 IO_STATUS_BLOCK io_status
;
459 connection
->read_closed
= TRUE
;
460 NtCancelIoFileEx(connection
->pipe
, &connection
->io_status
, &io_status
);
463 static void rpcrt4_conn_np_cancel_call(RpcConnection
*conn
)
465 RpcConnection_np
*connection
= (RpcConnection_np
*)conn
;
466 CancelIoEx(connection
->pipe
, NULL
);
469 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
471 /* FIXME: implement when named pipe writes use overlapped I/O */
475 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
476 const char *networkaddr
,
477 const char *endpoint
)
479 twr_empty_floor_t
*smb_floor
;
480 twr_empty_floor_t
*nb_floor
;
482 size_t networkaddr_size
;
483 size_t endpoint_size
;
485 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
487 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
488 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
489 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
494 smb_floor
= (twr_empty_floor_t
*)tower_data
;
496 tower_data
+= sizeof(*smb_floor
);
498 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
499 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
500 smb_floor
->count_rhs
= endpoint_size
;
503 memcpy(tower_data
, endpoint
, endpoint_size
);
506 tower_data
+= endpoint_size
;
508 nb_floor
= (twr_empty_floor_t
*)tower_data
;
510 tower_data
+= sizeof(*nb_floor
);
512 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
513 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
514 nb_floor
->count_rhs
= networkaddr_size
;
517 memcpy(tower_data
, networkaddr
, networkaddr_size
);
524 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
529 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
530 const twr_empty_floor_t
*nb_floor
;
532 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
534 if (tower_size
< sizeof(*smb_floor
))
535 return EPT_S_NOT_REGISTERED
;
537 tower_data
+= sizeof(*smb_floor
);
538 tower_size
-= sizeof(*smb_floor
);
540 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
541 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
542 (smb_floor
->count_rhs
> tower_size
) ||
543 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
544 return EPT_S_NOT_REGISTERED
;
548 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
550 return RPC_S_OUT_OF_RESOURCES
;
551 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
553 tower_data
+= smb_floor
->count_rhs
;
554 tower_size
-= smb_floor
->count_rhs
;
556 if (tower_size
< sizeof(*nb_floor
))
557 return EPT_S_NOT_REGISTERED
;
559 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
561 tower_data
+= sizeof(*nb_floor
);
562 tower_size
-= sizeof(*nb_floor
);
564 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
565 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
566 (nb_floor
->count_rhs
> tower_size
) ||
567 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
568 return EPT_S_NOT_REGISTERED
;
572 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
577 I_RpcFree(*endpoint
);
580 return RPC_S_OUT_OF_RESOURCES
;
582 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
588 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
590 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
593 TRACE("(%p)\n", conn
);
595 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
596 return RPCRT4_default_impersonate_client(conn
);
598 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
601 DWORD error
= GetLastError();
602 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
605 case ERROR_CANNOT_IMPERSONATE
:
606 return RPC_S_NO_CONTEXT_AVAILABLE
;
612 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
616 TRACE("(%p)\n", conn
);
618 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
619 return RPCRT4_default_revert_to_self(conn
);
621 ret
= RevertToSelf();
624 WARN("RevertToSelf failed with error %u\n", GetLastError());
625 return RPC_S_NO_CONTEXT_AVAILABLE
;
630 typedef struct _RpcServerProtseq_np
632 RpcServerProtseq common
;
634 } RpcServerProtseq_np
;
636 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
638 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ps
));
640 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
644 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
646 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
647 SetEvent(npps
->mgr_event
);
650 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
652 HANDLE
*objs
= prev_array
;
653 RpcConnection_np
*conn
;
654 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
656 EnterCriticalSection(&protseq
->cs
);
658 /* open and count connections */
660 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_np
, common
.protseq_entry
)
662 if (!conn
->pipe
&& rpcrt4_conn_create_pipe(&conn
->common
) != RPC_S_OK
)
664 if (!conn
->listen_event
)
669 event
= get_np_event(conn
);
673 status
= NtFsControlFile(conn
->pipe
, event
, NULL
, NULL
, &conn
->io_status
, FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
677 case STATUS_PIPE_CONNECTED
:
678 conn
->io_status
.Status
= status
;
684 ERR("pipe listen error %x\n", status
);
688 conn
->listen_event
= event
;
693 /* make array of connections */
695 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
697 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
700 ERR("couldn't allocate objs\n");
701 LeaveCriticalSection(&protseq
->cs
);
705 objs
[0] = npps
->mgr_event
;
707 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_np
, common
.protseq_entry
)
709 if (conn
->listen_event
)
710 objs
[(*count
)++] = conn
->listen_event
;
712 LeaveCriticalSection(&protseq
->cs
);
716 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
718 HeapFree(GetProcessHeap(), 0, array
);
721 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
724 HANDLE
*objs
= wait_array
;
726 RpcConnection
*cconn
= NULL
;
727 RpcConnection_np
*conn
;
734 /* an alertable wait isn't strictly necessary, but due to our
735 * overlapped I/O implementation in Wine we need to free some memory
736 * by the file user APC being called, even if no completion routine was
737 * specified at the time of starting the async operation */
738 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
739 } while (res
== WAIT_IO_COMPLETION
);
741 if (res
== WAIT_OBJECT_0
)
743 else if (res
== WAIT_FAILED
)
745 ERR("wait failed with error %d\n", GetLastError());
750 b_handle
= objs
[res
- WAIT_OBJECT_0
];
751 /* find which connection got a RPC */
752 EnterCriticalSection(&protseq
->cs
);
753 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_np
, common
.protseq_entry
)
755 if (b_handle
== conn
->listen_event
)
757 release_np_event(conn
, conn
->listen_event
);
758 conn
->listen_event
= NULL
;
759 if (conn
->io_status
.Status
== STATUS_SUCCESS
|| conn
->io_status
.Status
== STATUS_PIPE_CONNECTED
)
760 cconn
= rpcrt4_spawn_connection(&conn
->common
);
762 ERR("listen failed %x\n", conn
->io_status
.Status
);
766 LeaveCriticalSection(&protseq
->cs
);
769 ERR("failed to locate connection for handle %p\n", b_handle
);
772 RPCRT4_new_client(cconn
);
777 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
778 const char *networkaddr
,
779 const char *endpoint
)
781 twr_empty_floor_t
*pipe_floor
;
783 size_t endpoint_size
;
785 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
787 endpoint_size
= strlen(endpoint
) + 1;
788 size
= sizeof(*pipe_floor
) + endpoint_size
;
793 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
795 tower_data
+= sizeof(*pipe_floor
);
797 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
798 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
799 pipe_floor
->count_rhs
= endpoint_size
;
801 memcpy(tower_data
, endpoint
, endpoint_size
);
806 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
811 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
813 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
815 if (tower_size
< sizeof(*pipe_floor
))
816 return EPT_S_NOT_REGISTERED
;
818 tower_data
+= sizeof(*pipe_floor
);
819 tower_size
-= sizeof(*pipe_floor
);
821 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
822 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
823 (pipe_floor
->count_rhs
> tower_size
) ||
824 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
825 return EPT_S_NOT_REGISTERED
;
832 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
834 return RPC_S_OUT_OF_RESOURCES
;
835 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
841 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
846 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
847 unsigned char *in_buffer
,
848 unsigned int in_size
,
849 unsigned char *out_buffer
,
850 unsigned int *out_size
)
852 /* since this protocol is local to the machine there is no need to
853 * authenticate the caller */
858 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
859 enum secure_packet_direction dir
,
860 RpcPktHdr
*hdr
, unsigned int hdr_size
,
861 unsigned char *stub_data
, unsigned int stub_data_size
,
862 RpcAuthVerifier
*auth_hdr
,
863 unsigned char *auth_value
, unsigned int auth_value_size
)
865 /* since this protocol is local to the machine there is no need to secure
870 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
871 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
872 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
874 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
875 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
879 FIXME("privs not implemented\n");
882 if (server_princ_name
)
884 FIXME("server_princ_name not implemented\n");
885 *server_princ_name
= NULL
;
887 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
888 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
891 FIXME("authorization service not implemented\n");
892 *authz_svc
= RPC_C_AUTHZ_NONE
;
895 FIXME("flags 0x%x not implemented\n", flags
);
900 /**** ncacn_ip_tcp support ****/
902 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
903 const char *networkaddr
,
904 unsigned char tcp_protid
,
905 const char *endpoint
)
907 twr_tcp_floor_t
*tcp_floor
;
908 twr_ipv4_floor_t
*ipv4_floor
;
910 struct addrinfo hints
;
912 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
914 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
919 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
920 tower_data
+= sizeof(*tcp_floor
);
922 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
924 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
925 tcp_floor
->protid
= tcp_protid
;
926 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
928 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
929 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
930 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
932 hints
.ai_flags
= AI_NUMERICHOST
;
933 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
934 hints
.ai_family
= PF_INET
;
935 hints
.ai_socktype
= SOCK_STREAM
;
936 hints
.ai_protocol
= IPPROTO_TCP
;
937 hints
.ai_addrlen
= 0;
938 hints
.ai_addr
= NULL
;
939 hints
.ai_canonname
= NULL
;
940 hints
.ai_next
= NULL
;
942 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
945 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
948 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
953 if (ai
->ai_family
== PF_INET
)
955 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
956 tcp_floor
->port
= sin
->sin_port
;
957 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
961 ERR("unexpected protocol family %d\n", ai
->ai_family
);
971 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
974 unsigned char tcp_protid
,
977 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
978 const twr_ipv4_floor_t
*ipv4_floor
;
979 struct in_addr in_addr
;
981 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
983 if (tower_size
< sizeof(*tcp_floor
))
984 return EPT_S_NOT_REGISTERED
;
986 tower_data
+= sizeof(*tcp_floor
);
987 tower_size
-= sizeof(*tcp_floor
);
989 if (tower_size
< sizeof(*ipv4_floor
))
990 return EPT_S_NOT_REGISTERED
;
992 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
994 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
995 (tcp_floor
->protid
!= tcp_protid
) ||
996 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
997 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
998 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
999 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1000 return EPT_S_NOT_REGISTERED
;
1004 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1006 return RPC_S_OUT_OF_RESOURCES
;
1007 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1012 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1017 I_RpcFree(*endpoint
);
1020 return RPC_S_OUT_OF_RESOURCES
;
1022 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1023 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1025 ERR("inet_ntop: %u\n", WSAGetLastError());
1026 I_RpcFree(*networkaddr
);
1027 *networkaddr
= NULL
;
1030 I_RpcFree(*endpoint
);
1033 return EPT_S_NOT_REGISTERED
;
1040 typedef struct _RpcConnection_tcp
1042 RpcConnection common
;
1045 HANDLE cancel_event
;
1046 } RpcConnection_tcp
;
1048 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1050 static BOOL wsa_inited
;
1054 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1055 /* Note: WSAStartup can be called more than once so we don't bother with
1056 * making accesses to wsa_inited thread-safe */
1059 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1060 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1061 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1063 ERR("event creation failed\n");
1064 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1070 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1072 HANDLE wait_handles
[2];
1074 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1076 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1079 wait_handles
[0] = tcpc
->sock_event
;
1080 wait_handles
[1] = tcpc
->cancel_event
;
1081 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1086 case WAIT_OBJECT_0
+ 1:
1089 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1094 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1097 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1099 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1102 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1108 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1113 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1115 RpcConnection_tcp
*tcpc
;
1116 tcpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_tcp
));
1120 if (!rpcrt4_sock_wait_init(tcpc
))
1122 HeapFree(GetProcessHeap(), 0, tcpc
);
1125 return &tcpc
->common
;
1128 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1130 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1133 struct addrinfo
*ai
;
1134 struct addrinfo
*ai_cur
;
1135 struct addrinfo hints
;
1137 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1139 if (tcpc
->sock
!= -1)
1143 hints
.ai_family
= PF_UNSPEC
;
1144 hints
.ai_socktype
= SOCK_STREAM
;
1145 hints
.ai_protocol
= IPPROTO_TCP
;
1146 hints
.ai_addrlen
= 0;
1147 hints
.ai_addr
= NULL
;
1148 hints
.ai_canonname
= NULL
;
1149 hints
.ai_next
= NULL
;
1151 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1154 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1155 Connection
->Endpoint
, gai_strerror(ret
));
1156 return RPC_S_SERVER_UNAVAILABLE
;
1159 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1164 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1166 TRACE("skipping non-IP/IPv6 address family\n");
1174 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1175 host
, sizeof(host
), service
, sizeof(service
),
1176 NI_NUMERICHOST
| NI_NUMERICSERV
);
1177 TRACE("trying %s:%s\n", host
, service
);
1180 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1183 WARN("socket() failed: %u\n", WSAGetLastError());
1187 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1189 WARN("connect() failed: %u\n", WSAGetLastError());
1194 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1196 setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1198 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1203 TRACE("connected\n");
1208 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1209 return RPC_S_SERVER_UNAVAILABLE
;
1212 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1214 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1217 struct addrinfo
*ai
;
1218 struct addrinfo
*ai_cur
;
1219 struct addrinfo hints
;
1221 TRACE("(%p, %s)\n", protseq
, endpoint
);
1223 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1224 hints
.ai_family
= PF_UNSPEC
;
1225 hints
.ai_socktype
= SOCK_STREAM
;
1226 hints
.ai_protocol
= IPPROTO_TCP
;
1227 hints
.ai_addrlen
= 0;
1228 hints
.ai_addr
= NULL
;
1229 hints
.ai_canonname
= NULL
;
1230 hints
.ai_next
= NULL
;
1232 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1235 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1237 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1238 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1239 return RPC_S_CANT_CREATE_ENDPOINT
;
1242 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1244 RpcConnection_tcp
*tcpc
;
1245 RPC_STATUS create_status
;
1246 struct sockaddr_storage sa
;
1248 char service
[NI_MAXSERV
];
1251 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1253 TRACE("skipping non-IP/IPv6 address family\n");
1260 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1261 host
, sizeof(host
), service
, sizeof(service
),
1262 NI_NUMERICHOST
| NI_NUMERICSERV
);
1263 TRACE("trying %s:%s\n", host
, service
);
1266 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1269 WARN("socket() failed: %u\n", WSAGetLastError());
1270 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1274 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1277 WARN("bind failed: %u\n", WSAGetLastError());
1279 if (WSAGetLastError() == WSAEADDRINUSE
)
1280 status
= RPC_S_DUPLICATE_ENDPOINT
;
1282 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1286 sa_len
= sizeof(sa
);
1287 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1289 WARN("getsockname() failed: %u\n", WSAGetLastError());
1291 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1295 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1296 NULL
, 0, service
, sizeof(service
),
1300 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1302 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1306 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1307 protseq
->Protseq
, NULL
,
1308 service
, NULL
, NULL
, NULL
, NULL
);
1309 if (create_status
!= RPC_S_OK
)
1312 status
= create_status
;
1317 ret
= listen(sock
, protseq
->MaxCalls
);
1320 WARN("listen failed: %u\n", WSAGetLastError());
1321 RPCRT4_ReleaseConnection(&tcpc
->common
);
1322 status
= RPC_S_OUT_OF_RESOURCES
;
1325 /* need a non-blocking socket, otherwise accept() has a potential
1326 * race-condition (poll() says it is readable, connection drops,
1327 * and accept() blocks until the next connection comes...)
1330 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1333 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1334 RPCRT4_ReleaseConnection(&tcpc
->common
);
1335 status
= RPC_S_OUT_OF_RESOURCES
;
1339 EnterCriticalSection(&protseq
->cs
);
1340 list_add_tail(&protseq
->listeners
, &tcpc
->common
.protseq_entry
);
1341 tcpc
->common
.protseq
= protseq
;
1342 LeaveCriticalSection(&protseq
->cs
);
1346 /* since IPv4 and IPv6 share the same port space, we only need one
1347 * successful bind to listen for both */
1348 TRACE("listening on %s\n", endpoint
);
1353 ERR("couldn't listen on port %s\n", endpoint
);
1357 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1360 struct sockaddr_in address
;
1362 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1363 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1366 addrsize
= sizeof(address
);
1367 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1370 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1371 return RPC_S_OUT_OF_RESOURCES
;
1375 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1378 client
->common
.NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, INET6_ADDRSTRLEN
);
1379 ret
= getnameinfo((struct sockaddr
*)&address
, addrsize
, client
->common
.NetworkAddr
, INET6_ADDRSTRLEN
, NULL
, 0, NI_NUMERICHOST
);
1382 ERR("Failed to retrieve the IP address, error %d\n", ret
);
1383 return RPC_S_OUT_OF_RESOURCES
;
1386 TRACE("Accepted a new TCP connection from %s\n", client
->common
.NetworkAddr
);
1390 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1391 void *buffer
, unsigned int count
)
1393 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1395 while (bytes_read
!= count
)
1397 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1402 else if (WSAGetLastError() == WSAEINTR
)
1404 else if (WSAGetLastError() != WSAEWOULDBLOCK
)
1406 WARN("recv() failed: %u\n", WSAGetLastError());
1411 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1415 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1419 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1420 const void *buffer
, unsigned int count
)
1422 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1423 int bytes_written
= 0;
1424 while (bytes_written
!= count
)
1426 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1429 else if (WSAGetLastError() == WSAEINTR
)
1431 else if (WSAGetLastError() != WSAEWOULDBLOCK
)
1435 if (!rpcrt4_sock_wait_for_send(tcpc
))
1439 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1440 return bytes_written
;
1443 static int rpcrt4_conn_tcp_close(RpcConnection
*conn
)
1445 RpcConnection_tcp
*connection
= (RpcConnection_tcp
*) conn
;
1447 TRACE("%d\n", connection
->sock
);
1449 if (connection
->sock
!= -1)
1450 closesocket(connection
->sock
);
1451 connection
->sock
= -1;
1452 CloseHandle(connection
->sock_event
);
1453 CloseHandle(connection
->cancel_event
);
1457 static void rpcrt4_conn_tcp_close_read(RpcConnection
*conn
)
1459 RpcConnection_tcp
*connection
= (RpcConnection_tcp
*) conn
;
1460 shutdown(connection
->sock
, SD_RECEIVE
);
1463 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*conn
)
1465 RpcConnection_tcp
*connection
= (RpcConnection_tcp
*) conn
;
1467 TRACE("%p\n", connection
);
1469 SetEvent(connection
->cancel_event
);
1472 static RPC_STATUS
rpcrt4_conn_tcp_is_server_listening(const char *endpoint
)
1475 return RPC_S_ACCESS_DENIED
;
1478 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1480 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1482 TRACE("%p\n", Connection
);
1484 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1489 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1490 const char *networkaddr
,
1491 const char *endpoint
)
1493 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1494 EPM_PROTOCOL_TCP
, endpoint
);
1497 typedef struct _RpcServerProtseq_sock
1499 RpcServerProtseq common
;
1501 } RpcServerProtseq_sock
;
1503 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1505 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ps
));
1508 static BOOL wsa_inited
;
1512 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1513 /* Note: WSAStartup can be called more than once so we don't bother with
1514 * making accesses to wsa_inited thread-safe */
1517 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1522 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1524 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1525 SetEvent(sockps
->mgr_event
);
1528 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1530 HANDLE
*objs
= prev_array
;
1531 RpcConnection_tcp
*conn
;
1532 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1534 EnterCriticalSection(&protseq
->cs
);
1536 /* open and count connections */
1538 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_tcp
, common
.protseq_entry
)
1540 if (conn
->sock
!= -1)
1544 /* make array of connections */
1546 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1548 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1551 ERR("couldn't allocate objs\n");
1552 LeaveCriticalSection(&protseq
->cs
);
1556 objs
[0] = sockps
->mgr_event
;
1558 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_tcp
, common
.protseq_entry
)
1560 if (conn
->sock
!= -1)
1562 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1563 if (res
== SOCKET_ERROR
)
1564 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1567 objs
[*count
] = conn
->sock_event
;
1572 LeaveCriticalSection(&protseq
->cs
);
1576 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1578 HeapFree(GetProcessHeap(), 0, array
);
1581 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1584 HANDLE
*objs
= wait_array
;
1586 RpcConnection
*cconn
= NULL
;
1587 RpcConnection_tcp
*conn
;
1594 /* an alertable wait isn't strictly necessary, but due to our
1595 * overlapped I/O implementation in Wine we need to free some memory
1596 * by the file user APC being called, even if no completion routine was
1597 * specified at the time of starting the async operation */
1598 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1599 } while (res
== WAIT_IO_COMPLETION
);
1601 if (res
== WAIT_OBJECT_0
)
1603 if (res
== WAIT_FAILED
)
1605 ERR("wait failed with error %d\n", GetLastError());
1609 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1611 /* find which connection got a RPC */
1612 EnterCriticalSection(&protseq
->cs
);
1613 LIST_FOR_EACH_ENTRY(conn
, &protseq
->listeners
, RpcConnection_tcp
, common
.protseq_entry
)
1615 if (b_handle
== conn
->sock_event
)
1617 cconn
= rpcrt4_spawn_connection(&conn
->common
);
1621 LeaveCriticalSection(&protseq
->cs
);
1624 ERR("failed to locate connection for handle %p\n", b_handle
);
1628 RPCRT4_new_client(cconn
);
1632 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1637 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1638 networkaddr
, EPM_PROTOCOL_TCP
,
1642 /**** ncacn_http support ****/
1644 /* 60 seconds is the period native uses */
1645 #define HTTP_IDLE_TIME 60000
1647 /* reference counted to avoid a race between a cancelled call's connection
1648 * being destroyed and the asynchronous InternetReadFileEx call being
1650 typedef struct _RpcHttpAsyncData
1653 HANDLE completion_event
;
1655 INTERNET_BUFFERSW inet_buffers
;
1656 CRITICAL_SECTION cs
;
1659 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1661 return InterlockedIncrement(&data
->refs
);
1664 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1666 ULONG refs
= InterlockedDecrement(&data
->refs
);
1669 TRACE("destroying async data %p\n", data
);
1670 CloseHandle(data
->completion_event
);
1671 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1672 data
->cs
.DebugInfo
->Spare
[0] = 0;
1673 DeleteCriticalSection(&data
->cs
);
1674 HeapFree(GetProcessHeap(), 0, data
);
1679 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
1681 ResetEvent(async_data
->completion_event
);
1682 RpcHttpAsyncData_AddRef(async_data
);
1685 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
1687 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
1691 RpcHttpAsyncData_Release(async_data
);
1695 if(GetLastError() != ERROR_IO_PENDING
) {
1696 RpcHttpAsyncData_Release(async_data
);
1697 ERR("Request failed with error %d\n", GetLastError());
1698 return RPC_S_SERVER_UNAVAILABLE
;
1701 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
1702 if(res
!= WAIT_OBJECT_0
) {
1703 TRACE("Cancelled\n");
1704 return RPC_S_CALL_CANCELLED
;
1707 if(async_data
->async_result
) {
1708 ERR("Async request failed with error %d\n", async_data
->async_result
);
1709 return RPC_S_SERVER_UNAVAILABLE
;
1724 unsigned int data_len
;
1725 BOOL finished
; /* finished authenticating */
1728 typedef struct _RpcConnection_http
1730 RpcConnection common
;
1733 HINTERNET in_request
;
1734 HINTERNET out_request
;
1736 HANDLE timer_cancelled
;
1737 HANDLE cancel_event
;
1738 DWORD last_sent_time
;
1739 ULONG bytes_received
;
1740 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1741 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1742 UUID connection_uuid
;
1745 RpcHttpAsyncData
*async_data
;
1746 } RpcConnection_http
;
1748 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1750 RpcConnection_http
*httpc
;
1751 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1752 if (!httpc
) return NULL
;
1753 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1754 if (!httpc
->async_data
)
1756 HeapFree(GetProcessHeap(), 0, httpc
);
1759 TRACE("async data = %p\n", httpc
->async_data
);
1760 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1761 httpc
->async_data
->refs
= 1;
1762 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSW
);
1763 InitializeCriticalSection(&httpc
->async_data
->cs
);
1764 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
1765 return &httpc
->common
;
1768 typedef struct _HttpTimerThreadData
1771 DWORD
*last_sent_time
;
1772 HANDLE timer_cancelled
;
1773 } HttpTimerThreadData
;
1775 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1777 HINTERNET in_request
= param
;
1778 RpcPktHdr
*idle_pkt
;
1780 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1784 DWORD bytes_written
;
1785 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1786 RPCRT4_FreeHeader(idle_pkt
);
1790 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1792 DWORD cur_time
= GetTickCount();
1793 DWORD cached_last_sent_time
= *last_sent_time
;
1794 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1797 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1799 HttpTimerThreadData
*data_in
= param
;
1800 HttpTimerThreadData data
;
1804 HeapFree(GetProcessHeap(), 0, data_in
);
1806 for (timeout
= HTTP_IDLE_TIME
;
1807 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1808 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1810 /* are we too soon after last send? */
1811 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
1813 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
1816 CloseHandle(data
.timer_cancelled
);
1820 static VOID WINAPI
rpcrt4_http_internet_callback(
1821 HINTERNET hInternet
,
1822 DWORD_PTR dwContext
,
1823 DWORD dwInternetStatus
,
1824 LPVOID lpvStatusInformation
,
1825 DWORD dwStatusInformationLength
)
1827 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
1829 switch (dwInternetStatus
)
1831 case INTERNET_STATUS_REQUEST_COMPLETE
:
1832 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1835 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
1837 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
1838 SetEvent(async_data
->completion_event
);
1839 RpcHttpAsyncData_Release(async_data
);
1845 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
1852 WCHAR
*status_text
= buf
;
1856 size
= sizeof(status_code
);
1857 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
1859 return GetLastError();
1860 if (status_code
== HTTP_STATUS_OK
)
1864 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1865 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1867 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
1868 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1871 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
1872 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
1874 if (status_code
== HTTP_STATUS_DENIED
)
1875 return ERROR_ACCESS_DENIED
;
1876 return RPC_S_SERVER_UNAVAILABLE
;
1879 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
1881 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
1882 LPWSTR proxy
= NULL
;
1884 LPWSTR password
= NULL
;
1885 LPWSTR servername
= NULL
;
1886 const WCHAR
*option
;
1889 if (httpc
->common
.QOS
&&
1890 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
1892 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
1893 if (http_cred
->TransportCredentials
)
1896 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
1897 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
1898 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1900 return RPC_S_OUT_OF_RESOURCES
;
1902 if (cred
->DomainLength
)
1904 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
1905 p
+= cred
->DomainLength
;
1909 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
1910 p
[cred
->UserLength
] = 0;
1912 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
1916 for (option
= httpc
->common
.NetworkOptions
; option
;
1917 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
1919 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
1920 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
1922 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
1924 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
1925 const WCHAR
*value_end
;
1928 value_end
= strchrW(option
, ',');
1930 value_end
= value_start
+ strlenW(value_start
);
1931 for (p
= value_start
; p
< value_end
; p
++)
1938 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
1939 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
1941 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
1943 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
1944 const WCHAR
*value_end
;
1946 value_end
= strchrW(option
, ',');
1948 value_end
= value_start
+ strlenW(value_start
);
1949 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
1950 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
1953 FIXME("unhandled option %s\n", debugstr_w(option
));
1956 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
1957 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
1958 if (!httpc
->app_info
)
1960 HeapFree(GetProcessHeap(), 0, password
);
1961 HeapFree(GetProcessHeap(), 0, user
);
1962 HeapFree(GetProcessHeap(), 0, proxy
);
1963 HeapFree(GetProcessHeap(), 0, servername
);
1964 ERR("InternetOpenW failed with error %d\n", GetLastError());
1965 return RPC_S_SERVER_UNAVAILABLE
;
1967 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
1969 /* if no RpcProxy option specified, set the HTTP server address to the
1970 * RPC server address */
1973 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
1976 HeapFree(GetProcessHeap(), 0, password
);
1977 HeapFree(GetProcessHeap(), 0, user
);
1978 HeapFree(GetProcessHeap(), 0, proxy
);
1979 return RPC_S_OUT_OF_RESOURCES
;
1981 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
1984 port
= (httpc
->common
.QOS
&&
1985 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
1986 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
1987 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
1989 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
1990 INTERNET_SERVICE_HTTP
, 0, 0);
1992 HeapFree(GetProcessHeap(), 0, password
);
1993 HeapFree(GetProcessHeap(), 0, user
);
1994 HeapFree(GetProcessHeap(), 0, proxy
);
1996 if (!httpc
->session
)
1998 ERR("InternetConnectW failed with error %d\n", GetLastError());
1999 HeapFree(GetProcessHeap(), 0, servername
);
2000 return RPC_S_SERVER_UNAVAILABLE
;
2002 httpc
->servername
= servername
;
2006 static int rpcrt4_http_async_read(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2007 void *buffer
, unsigned int count
)
2011 unsigned int bytes_left
= count
;
2012 RPC_STATUS status
= RPC_S_OK
;
2014 async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
2018 async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2019 prepare_async_request(async_data
);
2020 ret
= InternetReadFileExW(req
, &async_data
->inet_buffers
, IRF_ASYNC
, 0);
2021 status
= wait_async_request(async_data
, ret
, cancel_event
);
2022 if (status
!= RPC_S_OK
)
2024 if (status
== RPC_S_CALL_CANCELLED
)
2025 TRACE("call cancelled\n");
2029 if (!async_data
->inet_buffers
.dwBufferLength
)
2031 memcpy(buf
, async_data
->inet_buffers
.lpvBuffer
,
2032 async_data
->inet_buffers
.dwBufferLength
);
2034 bytes_left
-= async_data
->inet_buffers
.dwBufferLength
;
2035 buf
+= async_data
->inet_buffers
.dwBufferLength
;
2038 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2039 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2041 TRACE("%p %p %u -> %u\n", req
, buffer
, count
, status
);
2042 return status
== RPC_S_OK
? count
: -1;
2045 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2051 TRACE("sending echo request to server\n");
2053 prepare_async_request(async_data
);
2054 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2055 status
= wait_async_request(async_data
, ret
, cancel_event
);
2056 if (status
!= RPC_S_OK
) return status
;
2058 status
= rpcrt4_http_check_response(req
);
2059 if (status
!= RPC_S_OK
) return status
;
2061 rpcrt4_http_async_read(req
, async_data
, cancel_event
, buf
, sizeof(buf
));
2062 /* FIXME: do something with retrieved data */
2067 static RPC_STATUS
insert_content_length_header(HINTERNET request
, DWORD len
)
2069 static const WCHAR fmtW
[] =
2070 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
2071 WCHAR header
[sizeof(fmtW
) / sizeof(fmtW
[0]) + 10];
2073 sprintfW(header
, fmtW
, len
);
2074 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
))) return RPC_S_OK
;
2075 return RPC_S_SERVER_UNAVAILABLE
;
2078 /* prepare the in pipe for use by RPC packets */
2079 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2080 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2081 const UUID
*association_uuid
, BOOL authorized
)
2086 INTERNET_BUFFERSW buffers_in
;
2087 DWORD bytes_written
;
2091 /* ask wininet to authorize, if necessary */
2092 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2093 if (status
!= RPC_S_OK
) return status
;
2095 memset(&buffers_in
, 0, sizeof(buffers_in
));
2096 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2097 /* FIXME: get this from the registry */
2098 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2099 status
= insert_content_length_header(in_request
, buffers_in
.dwBufferTotal
);
2100 if (status
!= RPC_S_OK
) return status
;
2102 prepare_async_request(async_data
);
2103 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2104 status
= wait_async_request(async_data
, ret
, cancel_event
);
2105 if (status
!= RPC_S_OK
) return status
;
2107 TRACE("sending HTTP connect header to server\n");
2108 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2109 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2110 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2111 RPCRT4_FreeHeader(hdr
);
2114 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2115 return RPC_S_SERVER_UNAVAILABLE
;
2121 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcHttpAsyncData
*async_data
,
2122 HANDLE cancel_event
, RpcPktHdr
*hdr
, BYTE
**data
)
2124 unsigned short data_len
;
2127 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, hdr
, sizeof(hdr
->common
)) < 0)
2128 return RPC_S_SERVER_UNAVAILABLE
;
2129 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2131 ERR("wrong packet type received %d or wrong frag_len %d\n",
2132 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2133 return RPC_S_PROTOCOL_ERROR
;
2136 size
= sizeof(hdr
->http
) - sizeof(hdr
->common
);
2137 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, &hdr
->common
+ 1, size
) < 0)
2138 return RPC_S_SERVER_UNAVAILABLE
;
2140 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2143 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2145 return RPC_S_OUT_OF_RESOURCES
;
2146 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, *data
, data_len
) < 0)
2148 HeapFree(GetProcessHeap(), 0, *data
);
2149 return RPC_S_SERVER_UNAVAILABLE
;
2155 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2157 ERR("invalid http packet\n");
2158 HeapFree(GetProcessHeap(), 0, *data
);
2159 return RPC_S_PROTOCOL_ERROR
;
2165 /* prepare the out pipe for use by RPC packets */
2166 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2167 HANDLE cancel_event
, const UUID
*connection_uuid
,
2168 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2174 BYTE
*data_from_server
;
2175 RpcPktHdr pkt_from_server
;
2176 ULONG field1
, field3
;
2181 /* ask wininet to authorize, if necessary */
2182 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2183 if (status
!= RPC_S_OK
) return status
;
2186 rpcrt4_http_async_read(out_request
, async_data
, cancel_event
, buf
, sizeof(buf
));
2188 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2189 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2191 status
= insert_content_length_header(out_request
, hdr
->common
.frag_len
);
2192 if (status
!= RPC_S_OK
)
2194 RPCRT4_FreeHeader(hdr
);
2198 TRACE("sending HTTP connect header to server\n");
2199 prepare_async_request(async_data
);
2200 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2201 status
= wait_async_request(async_data
, ret
, cancel_event
);
2202 RPCRT4_FreeHeader(hdr
);
2203 if (status
!= RPC_S_OK
) return status
;
2205 status
= rpcrt4_http_check_response(out_request
);
2206 if (status
!= RPC_S_OK
) return status
;
2208 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2209 &pkt_from_server
, &data_from_server
);
2210 if (status
!= RPC_S_OK
) return status
;
2211 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2213 HeapFree(GetProcessHeap(), 0, data_from_server
);
2214 if (status
!= RPC_S_OK
) return status
;
2215 TRACE("received (%d) from first prepare header\n", field1
);
2219 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2220 &pkt_from_server
, &data_from_server
);
2221 if (status
!= RPC_S_OK
) return status
;
2222 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2224 TRACE("http idle packet, waiting for real packet\n");
2225 HeapFree(GetProcessHeap(), 0, data_from_server
);
2226 if (pkt_from_server
.http
.num_data_items
!= 0)
2228 ERR("HTTP idle packet should have no data items instead of %d\n",
2229 pkt_from_server
.http
.num_data_items
);
2230 return RPC_S_PROTOCOL_ERROR
;
2233 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2234 &field1
, flow_control_increment
,
2236 HeapFree(GetProcessHeap(), 0, data_from_server
);
2237 if (status
!= RPC_S_OK
) return status
;
2238 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2243 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2245 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2250 /* first 6 bits, all from bin[0] */
2251 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2252 x
= (bin
[0] & 3) << 4;
2254 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2257 base64
[i
++] = enc
[x
];
2262 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2263 x
= (bin
[1] & 0x0f) << 2;
2265 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2268 base64
[i
++] = enc
[x
];
2272 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2274 /* last 6 bits, all from bin [2] */
2275 base64
[i
++] = enc
[bin
[2] & 0x3f];
2283 static inline char decode_char( WCHAR c
)
2285 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2286 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2287 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2288 if (c
== '+') return 62;
2289 if (c
== '/') return 63;
2293 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2296 char c0
, c1
, c2
, c3
;
2297 const WCHAR
*p
= base64
;
2301 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2302 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2303 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2304 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2308 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2309 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2310 buf
[i
+ 2] = (c2
<< 6) | c3
;
2318 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2319 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2321 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2324 else if (p
[3] == '=')
2326 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2327 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2328 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2332 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2333 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2339 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2340 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2341 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2342 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2346 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2347 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2348 buf
[i
+ 2] = (c2
<< 6) | c3
;
2355 static struct authinfo
*alloc_authinfo(void)
2357 struct authinfo
*ret
;
2359 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2361 SecInvalidateHandle(&ret
->cred
);
2362 SecInvalidateHandle(&ret
->ctx
);
2363 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2369 ret
->finished
= FALSE
;
2373 static void destroy_authinfo(struct authinfo
*info
)
2377 if (SecIsValidHandle(&info
->ctx
))
2378 DeleteSecurityContext(&info
->ctx
);
2379 if (SecIsValidHandle(&info
->cred
))
2380 FreeCredentialsHandle(&info
->cred
);
2382 HeapFree(GetProcessHeap(), 0, info
->data
);
2383 HeapFree(GetProcessHeap(), 0, info
);
2386 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2387 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2388 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2389 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2390 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2400 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2401 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2402 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2403 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2404 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2406 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2408 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2411 for (i
= 0; i
< num_auth_schemes
; i
++)
2413 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2414 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2419 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2421 DWORD len
, index
= 0;
2425 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2426 if (auth_scheme_from_header(buffer
) == scheme
) break;
2431 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2432 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2434 struct authinfo
*info
= *auth_ptr
;
2435 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2436 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2438 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2440 switch (creds
->AuthnSchemes
[0])
2442 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2444 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2445 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2447 info
->data_len
= userlen
+ passlen
+ 1;
2448 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2450 status
= RPC_S_OUT_OF_MEMORY
;
2453 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2454 info
->data
[userlen
] = ':';
2455 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2457 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2458 info
->finished
= TRUE
;
2462 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2463 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2466 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2467 SECURITY_STATUS ret
;
2468 SecBufferDesc out_desc
, in_desc
;
2470 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2474 WCHAR auth_value
[2048];
2475 DWORD size
= sizeof(auth_value
);
2478 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2479 else scheme
= negotiateW
;
2480 scheme_len
= strlenW( scheme
);
2485 SecPkgInfoW
*pkg_info
;
2487 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2488 if (ret
!= SEC_E_OK
) break;
2490 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2491 if (ret
!= SEC_E_OK
) break;
2493 info
->max_token
= pkg_info
->cbMaxToken
;
2494 FreeContextBuffer(pkg_info
);
2499 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2500 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2502 ERR("authentication scheme changed\n");
2506 in
.BufferType
= SECBUFFER_TOKEN
;
2510 in_desc
.ulVersion
= 0;
2511 in_desc
.cBuffers
= 1;
2512 in_desc
.pBuffers
= &in
;
2514 p
= auth_value
+ scheme_len
;
2515 if (!first
&& *p
== ' ')
2517 int len
= strlenW(++p
);
2518 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2519 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2520 decode_base64(p
, len
, in
.pvBuffer
);
2522 out
.BufferType
= SECBUFFER_TOKEN
;
2523 out
.cbBuffer
= info
->max_token
;
2524 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2526 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2529 out_desc
.ulVersion
= 0;
2530 out_desc
.cBuffers
= 1;
2531 out_desc
.pBuffers
= &out
;
2533 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2534 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2535 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2536 &info
->attr
, &info
->exp
);
2537 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2538 if (ret
== SEC_E_OK
)
2540 HeapFree(GetProcessHeap(), 0, info
->data
);
2541 info
->data
= out
.pvBuffer
;
2542 info
->data_len
= out
.cbBuffer
;
2543 info
->finished
= TRUE
;
2544 TRACE("sending last auth packet\n");
2547 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2549 HeapFree(GetProcessHeap(), 0, info
->data
);
2550 info
->data
= out
.pvBuffer
;
2551 info
->data_len
= out
.cbBuffer
;
2552 TRACE("sending next auth packet\n");
2557 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2558 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2561 info
->scheme
= creds
->AuthnSchemes
[0];
2565 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2569 if (status
!= RPC_S_OK
)
2571 destroy_authinfo(info
);
2579 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2581 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2582 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2583 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2584 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2585 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2586 const WCHAR
*scheme_str
;
2587 WCHAR
*header
, *ptr
;
2588 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2592 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2593 scheme_str
= basicW
;
2594 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2596 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2597 scheme_str
= negotiateW
;
2598 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2600 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2602 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2605 ERR("unknown scheme %u\n", scheme
);
2606 return RPC_S_SERVER_UNAVAILABLE
;
2608 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2610 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2611 ptr
= header
+ auth_len
;
2612 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2614 len
= encode_base64(data
, data_len
, ptr
);
2618 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2620 HeapFree(GetProcessHeap(), 0, header
);
2625 static void drain_content(HINTERNET request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2627 DWORD count
, len
= 0, size
= sizeof(len
);
2630 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2634 count
= min(sizeof(buf
), len
);
2635 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, buf
, count
) <= 0) return;
2640 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2642 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2643 struct authinfo
*info
= NULL
;
2649 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2650 if (status
!= RPC_S_OK
) break;
2652 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2653 if (status
!= RPC_S_OK
) break;
2655 prepare_async_request(httpc
->async_data
);
2656 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2657 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2658 if (status
!= RPC_S_OK
|| info
->finished
) break;
2660 status
= rpcrt4_http_check_response(request
);
2661 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
2662 drain_content(request
, httpc
->async_data
, httpc
->cancel_event
);
2665 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2666 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
2668 destroy_authinfo(info
);
2672 static BOOL
has_credentials(RpcConnection_http
*httpc
)
2674 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
2675 SEC_WINNT_AUTH_IDENTITY_W
*id
;
2677 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
2680 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2681 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
2684 id
= creds
->TransportCredentials
;
2685 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
2690 static BOOL
is_secure(RpcConnection_http
*httpc
)
2692 return httpc
->common
.QOS
&&
2693 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2694 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2697 static RPC_STATUS
set_auth_cookie(RpcConnection_http
*httpc
, const WCHAR
*value
)
2699 static WCHAR httpW
[] = {'h','t','t','p',0};
2700 static WCHAR httpsW
[] = {'h','t','t','p','s',0};
2706 if (!value
) return RPC_S_OK
;
2708 uc
.dwStructSize
= sizeof(uc
);
2709 uc
.lpszScheme
= is_secure(httpc
) ? httpsW
: httpW
;
2710 uc
.dwSchemeLength
= 0;
2711 uc
.lpszHostName
= httpc
->servername
;
2712 uc
.dwHostNameLength
= 0;
2714 uc
.lpszUserName
= NULL
;
2715 uc
.dwUserNameLength
= 0;
2716 uc
.lpszPassword
= NULL
;
2717 uc
.dwPasswordLength
= 0;
2718 uc
.lpszUrlPath
= NULL
;
2719 uc
.dwUrlPathLength
= 0;
2720 uc
.lpszExtraInfo
= NULL
;
2721 uc
.dwExtraInfoLength
= 0;
2723 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
2724 return RPC_S_SERVER_UNAVAILABLE
;
2726 if (!(url
= HeapAlloc(GetProcessHeap(), 0, len
))) return RPC_S_OUT_OF_MEMORY
;
2728 len
= len
/ sizeof(WCHAR
) - 1;
2729 if (!InternetCreateUrlW(&uc
, 0, url
, &len
))
2731 HeapFree(GetProcessHeap(), 0, url
);
2732 return RPC_S_SERVER_UNAVAILABLE
;
2735 ret
= InternetSetCookieW(url
, NULL
, value
);
2736 HeapFree(GetProcessHeap(), 0, url
);
2737 if (!ret
) return RPC_S_SERVER_UNAVAILABLE
;
2742 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2744 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2745 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2746 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2747 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2748 static const WCHAR wszColon
[] = {':',0};
2749 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2750 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2754 BOOL secure
, credentials
;
2755 HttpTimerThreadData
*timer_data
;
2758 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2760 if (Connection
->server
)
2762 ERR("ncacn_http servers not supported yet\n");
2763 return RPC_S_SERVER_UNAVAILABLE
;
2766 if (httpc
->in_request
)
2769 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2771 UuidCreate(&httpc
->connection_uuid
);
2772 UuidCreate(&httpc
->in_pipe_uuid
);
2773 UuidCreate(&httpc
->out_pipe_uuid
);
2775 status
= rpcrt4_http_internet_connect(httpc
);
2776 if (status
!= RPC_S_OK
)
2779 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2781 return RPC_S_OUT_OF_MEMORY
;
2782 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2783 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2784 strcatW(url
, wszColon
);
2785 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2787 secure
= is_secure(httpc
);
2788 credentials
= has_credentials(httpc
);
2790 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
2791 INTERNET_FLAG_NO_AUTO_REDIRECT
;
2792 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
2793 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
2795 status
= set_auth_cookie(httpc
, Connection
->CookieAuth
);
2796 if (status
!= RPC_S_OK
)
2798 HeapFree(GetProcessHeap(), 0, url
);
2801 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
2802 flags
, (DWORD_PTR
)httpc
->async_data
);
2803 if (!httpc
->in_request
)
2805 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2806 HeapFree(GetProcessHeap(), 0, url
);
2807 return RPC_S_SERVER_UNAVAILABLE
;
2812 status
= authorize_request(httpc
, httpc
->in_request
);
2813 if (status
!= RPC_S_OK
)
2815 HeapFree(GetProcessHeap(), 0, url
);
2818 status
= rpcrt4_http_check_response(httpc
->in_request
);
2819 if (status
!= RPC_S_OK
)
2821 HeapFree(GetProcessHeap(), 0, url
);
2824 drain_content(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
);
2827 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
2828 flags
, (DWORD_PTR
)httpc
->async_data
);
2829 HeapFree(GetProcessHeap(), 0, url
);
2830 if (!httpc
->out_request
)
2832 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2833 return RPC_S_SERVER_UNAVAILABLE
;
2838 status
= authorize_request(httpc
, httpc
->out_request
);
2839 if (status
!= RPC_S_OK
)
2843 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
,
2844 &httpc
->connection_uuid
, &httpc
->in_pipe_uuid
,
2845 &Connection
->assoc
->http_uuid
, credentials
);
2846 if (status
!= RPC_S_OK
)
2849 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
,
2850 &httpc
->connection_uuid
, &httpc
->out_pipe_uuid
,
2851 &httpc
->flow_control_increment
, credentials
);
2852 if (status
!= RPC_S_OK
)
2855 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2856 httpc
->last_sent_time
= GetTickCount();
2857 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2859 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2861 return ERROR_OUTOFMEMORY
;
2862 timer_data
->timer_param
= httpc
->in_request
;
2863 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2864 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2865 /* FIXME: should use CreateTimerQueueTimer when implemented */
2866 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2869 HeapFree(GetProcessHeap(), 0, timer_data
);
2870 return GetLastError();
2872 CloseHandle(thread
);
2877 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2880 return RPC_S_SERVER_UNAVAILABLE
;
2883 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2884 void *buffer
, unsigned int count
)
2886 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2887 return rpcrt4_http_async_read(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
, buffer
, count
);
2890 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2892 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2896 RpcPktCommonHdr common_hdr
;
2900 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2903 /* read packet common header */
2904 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2905 if (dwRead
!= sizeof(common_hdr
)) {
2906 WARN("Short read of header, %d bytes\n", dwRead
);
2907 status
= RPC_S_PROTOCOL_ERROR
;
2910 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2911 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2913 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2914 status
= RPC_S_PROTOCOL_ERROR
;
2918 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2919 if (status
!= RPC_S_OK
) goto fail
;
2921 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2922 if (hdr_length
== 0) {
2923 WARN("header length == 0\n");
2924 status
= RPC_S_PROTOCOL_ERROR
;
2928 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2931 status
= RPC_S_OUT_OF_RESOURCES
;
2934 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2936 /* read the rest of packet header */
2937 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2938 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2939 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2940 status
= RPC_S_PROTOCOL_ERROR
;
2944 if (common_hdr
.frag_len
- hdr_length
)
2946 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2949 status
= RPC_S_OUT_OF_RESOURCES
;
2953 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2954 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2956 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2957 status
= RPC_S_PROTOCOL_ERROR
;
2964 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2966 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2968 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2969 status
= RPC_S_PROTOCOL_ERROR
;
2972 if ((*Header
)->http
.flags
== 0x0001)
2974 TRACE("http idle packet, waiting for real packet\n");
2975 if ((*Header
)->http
.num_data_items
!= 0)
2977 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2978 status
= RPC_S_PROTOCOL_ERROR
;
2982 else if ((*Header
)->http
.flags
== 0x0002)
2984 ULONG bytes_transmitted
;
2985 ULONG flow_control_increment
;
2987 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2990 &flow_control_increment
,
2992 if (status
!= RPC_S_OK
)
2994 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2995 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2996 /* FIXME: do something with parsed data */
3000 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
3001 status
= RPC_S_PROTOCOL_ERROR
;
3004 RPCRT4_FreeHeader(*Header
);
3006 HeapFree(GetProcessHeap(), 0, *Payload
);
3014 httpc
->bytes_received
+= common_hdr
.frag_len
;
3016 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
3018 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
3020 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
3021 httpc
->bytes_received
,
3022 httpc
->flow_control_increment
,
3023 &httpc
->out_pipe_uuid
);
3026 DWORD bytes_written
;
3028 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
3029 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
3030 RPCRT4_FreeHeader(hdr
);
3032 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
3037 if (status
!= RPC_S_OK
) {
3038 RPCRT4_FreeHeader(*Header
);
3040 HeapFree(GetProcessHeap(), 0, *Payload
);
3046 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
3047 const void *buffer
, unsigned int count
)
3049 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3050 DWORD bytes_written
;
3053 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
3054 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
3055 httpc
->last_sent_time
= GetTickCount();
3056 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
3057 return ret
? bytes_written
: -1;
3060 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
3062 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3066 SetEvent(httpc
->timer_cancelled
);
3067 if (httpc
->in_request
)
3068 InternetCloseHandle(httpc
->in_request
);
3069 httpc
->in_request
= NULL
;
3070 if (httpc
->out_request
)
3071 InternetCloseHandle(httpc
->out_request
);
3072 httpc
->out_request
= NULL
;
3073 if (httpc
->app_info
)
3074 InternetCloseHandle(httpc
->app_info
);
3075 httpc
->app_info
= NULL
;
3077 InternetCloseHandle(httpc
->session
);
3078 httpc
->session
= NULL
;
3079 RpcHttpAsyncData_Release(httpc
->async_data
);
3080 if (httpc
->cancel_event
)
3081 CloseHandle(httpc
->cancel_event
);
3082 HeapFree(GetProcessHeap(), 0, httpc
->servername
);
3083 httpc
->servername
= NULL
;
3088 static void rpcrt4_ncacn_http_close_read(RpcConnection
*conn
)
3090 rpcrt4_ncacn_http_close(conn
); /* FIXME */
3093 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
3095 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3097 SetEvent(httpc
->cancel_event
);
3100 static RPC_STATUS
rpcrt4_ncacn_http_is_server_listening(const char *endpoint
)
3103 return RPC_S_ACCESS_DENIED
;
3106 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
3108 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3112 prepare_async_request(httpc
->async_data
);
3113 ret
= InternetQueryDataAvailable(httpc
->out_request
,
3114 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
3115 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3116 return status
== RPC_S_OK
? 0 : -1;
3119 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
3120 const char *networkaddr
,
3121 const char *endpoint
)
3123 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
3124 EPM_PROTOCOL_HTTP
, endpoint
);
3127 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
3132 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
3133 networkaddr
, EPM_PROTOCOL_HTTP
,
3137 static const struct connection_ops conn_protseq_list
[] = {
3139 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
3140 rpcrt4_conn_np_alloc
,
3141 rpcrt4_ncacn_np_open
,
3142 rpcrt4_ncacn_np_handoff
,
3143 rpcrt4_conn_np_read
,
3144 rpcrt4_conn_np_write
,
3145 rpcrt4_conn_np_close
,
3146 rpcrt4_conn_np_close_read
,
3147 rpcrt4_conn_np_cancel_call
,
3148 rpcrt4_ncacn_np_is_server_listening
,
3149 rpcrt4_conn_np_wait_for_incoming_data
,
3150 rpcrt4_ncacn_np_get_top_of_tower
,
3151 rpcrt4_ncacn_np_parse_top_of_tower
,
3153 RPCRT4_default_is_authorized
,
3154 RPCRT4_default_authorize
,
3155 RPCRT4_default_secure_packet
,
3156 rpcrt4_conn_np_impersonate_client
,
3157 rpcrt4_conn_np_revert_to_self
,
3158 RPCRT4_default_inquire_auth_client
,
3161 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
3162 rpcrt4_conn_np_alloc
,
3163 rpcrt4_ncalrpc_open
,
3164 rpcrt4_ncalrpc_handoff
,
3165 rpcrt4_conn_np_read
,
3166 rpcrt4_conn_np_write
,
3167 rpcrt4_conn_np_close
,
3168 rpcrt4_conn_np_close_read
,
3169 rpcrt4_conn_np_cancel_call
,
3170 rpcrt4_ncalrpc_np_is_server_listening
,
3171 rpcrt4_conn_np_wait_for_incoming_data
,
3172 rpcrt4_ncalrpc_get_top_of_tower
,
3173 rpcrt4_ncalrpc_parse_top_of_tower
,
3175 rpcrt4_ncalrpc_is_authorized
,
3176 rpcrt4_ncalrpc_authorize
,
3177 rpcrt4_ncalrpc_secure_packet
,
3178 rpcrt4_conn_np_impersonate_client
,
3179 rpcrt4_conn_np_revert_to_self
,
3180 rpcrt4_ncalrpc_inquire_auth_client
,
3183 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
3184 rpcrt4_conn_tcp_alloc
,
3185 rpcrt4_ncacn_ip_tcp_open
,
3186 rpcrt4_conn_tcp_handoff
,
3187 rpcrt4_conn_tcp_read
,
3188 rpcrt4_conn_tcp_write
,
3189 rpcrt4_conn_tcp_close
,
3190 rpcrt4_conn_tcp_close_read
,
3191 rpcrt4_conn_tcp_cancel_call
,
3192 rpcrt4_conn_tcp_is_server_listening
,
3193 rpcrt4_conn_tcp_wait_for_incoming_data
,
3194 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
3195 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
3197 RPCRT4_default_is_authorized
,
3198 RPCRT4_default_authorize
,
3199 RPCRT4_default_secure_packet
,
3200 RPCRT4_default_impersonate_client
,
3201 RPCRT4_default_revert_to_self
,
3202 RPCRT4_default_inquire_auth_client
,
3205 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
3206 rpcrt4_ncacn_http_alloc
,
3207 rpcrt4_ncacn_http_open
,
3208 rpcrt4_ncacn_http_handoff
,
3209 rpcrt4_ncacn_http_read
,
3210 rpcrt4_ncacn_http_write
,
3211 rpcrt4_ncacn_http_close
,
3212 rpcrt4_ncacn_http_close_read
,
3213 rpcrt4_ncacn_http_cancel_call
,
3214 rpcrt4_ncacn_http_is_server_listening
,
3215 rpcrt4_ncacn_http_wait_for_incoming_data
,
3216 rpcrt4_ncacn_http_get_top_of_tower
,
3217 rpcrt4_ncacn_http_parse_top_of_tower
,
3218 rpcrt4_ncacn_http_receive_fragment
,
3219 RPCRT4_default_is_authorized
,
3220 RPCRT4_default_authorize
,
3221 RPCRT4_default_secure_packet
,
3222 RPCRT4_default_impersonate_client
,
3223 RPCRT4_default_revert_to_self
,
3224 RPCRT4_default_inquire_auth_client
,
3229 static const struct protseq_ops protseq_list
[] =
3233 rpcrt4_protseq_np_alloc
,
3234 rpcrt4_protseq_np_signal_state_changed
,
3235 rpcrt4_protseq_np_get_wait_array
,
3236 rpcrt4_protseq_np_free_wait_array
,
3237 rpcrt4_protseq_np_wait_for_new_connection
,
3238 rpcrt4_protseq_ncacn_np_open_endpoint
,
3242 rpcrt4_protseq_np_alloc
,
3243 rpcrt4_protseq_np_signal_state_changed
,
3244 rpcrt4_protseq_np_get_wait_array
,
3245 rpcrt4_protseq_np_free_wait_array
,
3246 rpcrt4_protseq_np_wait_for_new_connection
,
3247 rpcrt4_protseq_ncalrpc_open_endpoint
,
3251 rpcrt4_protseq_sock_alloc
,
3252 rpcrt4_protseq_sock_signal_state_changed
,
3253 rpcrt4_protseq_sock_get_wait_array
,
3254 rpcrt4_protseq_sock_free_wait_array
,
3255 rpcrt4_protseq_sock_wait_for_new_connection
,
3256 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
3260 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3263 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3264 if (!strcmp(protseq_list
[i
].name
, protseq
))
3265 return &protseq_list
[i
];
3269 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3272 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3273 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3274 return &conn_protseq_list
[i
];
3278 /**** interface to rest of code ****/
3280 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3282 TRACE("(Connection == ^%p)\n", Connection
);
3284 assert(!Connection
->server
);
3285 return Connection
->ops
->open_connection_client(Connection
);
3288 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3290 TRACE("(Connection == ^%p)\n", Connection
);
3291 if (SecIsValidHandle(&Connection
->ctx
))
3293 DeleteSecurityContext(&Connection
->ctx
);
3294 SecInvalidateHandle(&Connection
->ctx
);
3296 rpcrt4_conn_close(Connection
);
3300 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3301 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3302 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3304 static LONG next_id
;
3305 const struct connection_ops
*ops
;
3306 RpcConnection
* NewConnection
;
3308 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3311 FIXME("not supported for protseq %s\n", Protseq
);
3312 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3315 NewConnection
= ops
->alloc();
3316 NewConnection
->ref
= 1;
3317 NewConnection
->server
= server
;
3318 NewConnection
->ops
= ops
;
3319 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3320 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3321 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3322 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3323 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3324 NewConnection
->NextCallId
= 1;
3326 SecInvalidateHandle(&NewConnection
->ctx
);
3327 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3328 NewConnection
->AuthInfo
= AuthInfo
;
3329 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3330 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3331 NewConnection
->QOS
= QOS
;
3333 list_init(&NewConnection
->conn_pool_entry
);
3334 list_init(&NewConnection
->protseq_entry
);
3336 TRACE("connection: %p\n", NewConnection
);
3337 *Connection
= NewConnection
;
3342 static RpcConnection
*rpcrt4_spawn_connection(RpcConnection
*old_connection
)
3344 RpcConnection
*connection
;
3347 err
= RPCRT4_CreateConnection(&connection
, old_connection
->server
, rpcrt4_conn_get_name(old_connection
),
3348 old_connection
->NetworkAddr
, old_connection
->Endpoint
, NULL
,
3349 old_connection
->AuthInfo
, old_connection
->QOS
, old_connection
->CookieAuth
);
3350 if (err
!= RPC_S_OK
)
3353 rpcrt4_conn_handoff(old_connection
, connection
);
3354 if (old_connection
->protseq
)
3356 EnterCriticalSection(&old_connection
->protseq
->cs
);
3357 connection
->protseq
= old_connection
->protseq
;
3358 list_add_tail(&old_connection
->protseq
->connections
, &connection
->protseq_entry
);
3359 LeaveCriticalSection(&old_connection
->protseq
->cs
);
3364 void rpcrt4_conn_release_and_wait(RpcConnection
*connection
)
3366 HANDLE event
= NULL
;
3368 if (connection
->ref
> 1)
3369 event
= connection
->wait_release
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
3371 RPCRT4_ReleaseConnection(connection
);
3375 WaitForSingleObject(event
, INFINITE
);
3380 RpcConnection
*RPCRT4_GrabConnection(RpcConnection
*connection
)
3382 LONG ref
= InterlockedIncrement(&connection
->ref
);
3383 TRACE("%p ref=%u\n", connection
, ref
);
3387 void RPCRT4_ReleaseConnection(RpcConnection
*connection
)
3391 /* protseq stores a list of active connections, but does not own references to them.
3392 * It may need to grab a connection from the list, which could lead to a race if
3393 * connection is being released, but not yet removed from the list. We handle that
3394 * by synchronizing on CS here. */
3395 if (connection
->protseq
)
3397 EnterCriticalSection(&connection
->protseq
->cs
);
3398 ref
= InterlockedDecrement(&connection
->ref
);
3400 list_remove(&connection
->protseq_entry
);
3401 LeaveCriticalSection(&connection
->protseq
->cs
);
3405 ref
= InterlockedDecrement(&connection
->ref
);
3408 TRACE("%p ref=%u\n", connection
, ref
);
3412 RPCRT4_CloseConnection(connection
);
3413 RPCRT4_strfree(connection
->Endpoint
);
3414 RPCRT4_strfree(connection
->NetworkAddr
);
3415 HeapFree(GetProcessHeap(), 0, connection
->NetworkOptions
);
3416 HeapFree(GetProcessHeap(), 0, connection
->CookieAuth
);
3417 if (connection
->AuthInfo
) RpcAuthInfo_Release(connection
->AuthInfo
);
3418 if (connection
->QOS
) RpcQualityOfService_Release(connection
->QOS
);
3421 if (connection
->server_binding
) RPCRT4_ReleaseBinding(connection
->server_binding
);
3423 if (connection
->wait_release
) SetEvent(connection
->wait_release
);
3425 HeapFree(GetProcessHeap(), 0, connection
);
3429 RPC_STATUS
RPCRT4_IsServerListening(const char *protseq
, const char *endpoint
)
3431 const struct connection_ops
*ops
;
3433 ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3436 FIXME("not supported for protseq %s\n", protseq
);
3437 return RPC_S_INVALID_BINDING
;
3440 return ops
->is_server_listening(endpoint
);
3443 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3445 const char *protseq
,
3446 const char *networkaddr
,
3447 const char *endpoint
)
3449 twr_empty_floor_t
*protocol_floor
;
3450 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3455 return RPC_S_INVALID_RPC_PROTSEQ
;
3459 *tower_size
= sizeof(*protocol_floor
);
3460 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3464 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3465 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3466 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3467 protocol_floor
->count_rhs
= 0;
3469 tower_data
+= sizeof(*protocol_floor
);
3471 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3473 return EPT_S_NOT_REGISTERED
;
3475 *tower_size
+= sizeof(*protocol_floor
);
3480 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3486 const twr_empty_floor_t
*protocol_floor
;
3487 const twr_empty_floor_t
*floor4
;
3488 const struct connection_ops
*protseq_ops
= NULL
;
3492 if (tower_size
< sizeof(*protocol_floor
))
3493 return EPT_S_NOT_REGISTERED
;
3495 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3496 tower_data
+= sizeof(*protocol_floor
);
3497 tower_size
-= sizeof(*protocol_floor
);
3498 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3499 (protocol_floor
->count_rhs
> tower_size
))
3500 return EPT_S_NOT_REGISTERED
;
3501 tower_data
+= protocol_floor
->count_rhs
;
3502 tower_size
-= protocol_floor
->count_rhs
;
3504 floor4
= (const twr_empty_floor_t
*)tower_data
;
3505 if ((tower_size
< sizeof(*floor4
)) ||
3506 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3507 return EPT_S_NOT_REGISTERED
;
3509 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3510 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3511 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3513 protseq_ops
= &conn_protseq_list
[i
];
3518 return EPT_S_NOT_REGISTERED
;
3520 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3522 if ((status
== RPC_S_OK
) && protseq
)
3524 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3525 strcpy(*protseq
, protseq_ops
->name
);
3531 /***********************************************************************
3532 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3534 * Checks if the given protocol sequence is known by the RPC system.
3535 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3538 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3542 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3543 ps
, sizeof ps
, NULL
, NULL
);
3544 if (rpcrt4_get_conn_protseq_ops(ps
))
3547 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3549 return RPC_S_INVALID_RPC_PROTSEQ
;
3552 /***********************************************************************
3553 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3555 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3557 UNICODE_STRING protseqW
;
3559 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3561 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3562 RtlFreeUnicodeString(&protseqW
);
3565 return RPC_S_OUT_OF_MEMORY
;
3568 /***********************************************************************
3569 * RpcProtseqVectorFreeA (RPCRT4.@)
3571 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3573 TRACE("(%p)\n", protseqs
);
3578 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3579 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3580 HeapFree(GetProcessHeap(), 0, *protseqs
);
3586 /***********************************************************************
3587 * RpcProtseqVectorFreeW (RPCRT4.@)
3589 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3591 TRACE("(%p)\n", protseqs
);
3596 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3597 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3598 HeapFree(GetProcessHeap(), 0, *protseqs
);
3604 /***********************************************************************
3605 * RpcNetworkInqProtseqsW (RPCRT4.@)
3607 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3609 RPC_PROTSEQ_VECTORW
*pvector
;
3611 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3613 TRACE("(%p)\n", protseqs
);
3615 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3618 pvector
= *protseqs
;
3620 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3622 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3623 if (pvector
->Protseq
[i
] == NULL
)
3625 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3626 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3632 if (status
!= RPC_S_OK
)
3633 RpcProtseqVectorFreeW(protseqs
);
3637 /***********************************************************************
3638 * RpcNetworkInqProtseqsA (RPCRT4.@)
3640 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3642 RPC_PROTSEQ_VECTORA
*pvector
;
3644 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3646 TRACE("(%p)\n", protseqs
);
3648 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3651 pvector
= *protseqs
;
3653 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3655 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3656 if (pvector
->Protseq
[i
] == NULL
)
3658 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3664 if (status
!= RPC_S_OK
)
3665 RpcProtseqVectorFreeA(protseqs
);