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
28 #if defined(__MINGW32__) || defined (_MSC_VER)
29 # include <ws2tcpip.h>
31 # define EADDRINUSE WSAEADDRINUSE
34 # define EAGAIN WSAEWOULDBLOCK
37 # define errno WSAGetLastError()
44 # ifdef HAVE_SYS_SOCKET_H
45 # include <sys/socket.h>
47 # ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
50 # ifdef HAVE_NETINET_TCP_H
51 # include <netinet/tcp.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
59 # ifdef HAVE_SYS_POLL_H
60 # include <sys/poll.h>
62 # ifdef HAVE_SYS_FILIO_H
63 # include <sys/filio.h>
65 # ifdef HAVE_SYS_IOCTL_H
66 # include <sys/ioctl.h>
68 # define closesocket close
69 # define ioctlsocket ioctl
70 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
74 #include "epm_towers.h"
77 # define SOL_TCP IPPROTO_TCP
80 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
83 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
85 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
87 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
89 /**** ncacn_np support ****/
91 typedef struct _RpcConnection_np
99 static RpcConnection
*rpcrt4_conn_np_alloc(void)
101 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
105 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
106 npc
->listening
= FALSE
;
111 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
116 npc
->listening
= TRUE
;
119 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
122 switch(GetLastError())
124 case ERROR_PIPE_CONNECTED
:
125 SetEvent(npc
->ovl
.hEvent
);
127 case ERROR_IO_PENDING
:
128 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
130 case ERROR_NO_DATA_DETECTED
:
131 /* client has disconnected, retry */
132 DisconnectNamedPipe( npc
->pipe
);
135 npc
->listening
= FALSE
;
136 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
137 return RPC_S_OUT_OF_RESOURCES
;
143 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
148 npc
->listening
= TRUE
;
149 npc
->listen_thread
= CreateThread(NULL
, 0, listen_thread
, npc
, 0, NULL
);
150 if (!npc
->listen_thread
)
152 npc
->listening
= FALSE
;
153 ERR("Couldn't create listen thread (error was %d)\n", GetLastError());
154 return RPC_S_OUT_OF_RESOURCES
;
160 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
162 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
163 TRACE("listening on %s\n", pname
);
165 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
166 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
167 PIPE_UNLIMITED_INSTANCES
,
168 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
169 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
170 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
171 if (GetLastError() == ERROR_FILE_EXISTS
)
172 return RPC_S_DUPLICATE_ENDPOINT
;
174 return RPC_S_CANT_CREATE_ENDPOINT
;
177 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
178 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
180 /* Note: we don't call ConnectNamedPipe here because it must be done in the
181 * server thread as the thread must be alertable */
185 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
187 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
191 TRACE("connecting to %s\n", pname
);
197 dwFlags
= SECURITY_SQOS_PRESENT
;
198 switch (Connection
->QOS
->qos
->ImpersonationType
)
200 case RPC_C_IMP_LEVEL_DEFAULT
:
201 /* FIXME: what to do here? */
203 case RPC_C_IMP_LEVEL_ANONYMOUS
:
204 dwFlags
|= SECURITY_ANONYMOUS
;
206 case RPC_C_IMP_LEVEL_IDENTIFY
:
207 dwFlags
|= SECURITY_IDENTIFICATION
;
209 case RPC_C_IMP_LEVEL_IMPERSONATE
:
210 dwFlags
|= SECURITY_IMPERSONATION
;
212 case RPC_C_IMP_LEVEL_DELEGATE
:
213 dwFlags
|= SECURITY_DELEGATION
;
216 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
217 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
219 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
220 OPEN_EXISTING
, dwFlags
, 0);
221 if (pipe
!= INVALID_HANDLE_VALUE
) break;
222 err
= GetLastError();
223 if (err
== ERROR_PIPE_BUSY
) {
224 TRACE("connection failed, error=%x\n", err
);
225 return RPC_S_SERVER_TOO_BUSY
;
226 } else if (err
== ERROR_BAD_NETPATH
) {
227 TRACE("connection failed, error=%x\n", err
);
228 return RPC_S_SERVER_UNAVAILABLE
;
230 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
231 err
= GetLastError();
232 WARN("connection failed, error=%x\n", err
);
233 return RPC_S_SERVER_UNAVAILABLE
;
238 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
239 /* pipe is connected; change to message-read mode. */
240 dwMode
= PIPE_READMODE_MESSAGE
;
241 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
242 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
248 static char *ncalrpc_pipe_name(const char *endpoint
)
250 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
253 /* protseq=ncalrpc: supposed to use NT LPC ports,
254 * but we'll implement it with named pipes for now */
255 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
256 strcat(strcpy(pipe_name
, prefix
), endpoint
);
260 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
262 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
266 /* already connected? */
270 pname
= ncalrpc_pipe_name(Connection
->Endpoint
);
271 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
277 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
281 RpcConnection
*Connection
;
282 char generated_endpoint
[22];
286 static LONG lrpc_nameless_id
;
287 DWORD process_id
= GetCurrentProcessId();
288 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
289 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
290 "LRPC%08x.%08x", process_id
, id
);
291 endpoint
= generated_endpoint
;
294 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
295 endpoint
, NULL
, NULL
, NULL
, NULL
);
299 pname
= ncalrpc_pipe_name(Connection
->Endpoint
);
300 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
303 EnterCriticalSection(&protseq
->cs
);
304 Connection
->Next
= protseq
->conn
;
305 protseq
->conn
= Connection
;
306 LeaveCriticalSection(&protseq
->cs
);
311 static char *ncacn_pipe_name(const char *endpoint
)
313 static const char prefix
[] = "\\\\.";
316 /* protseq=ncacn_np: named pipes */
317 pipe_name
= I_RpcAllocate(sizeof(prefix
) + strlen(endpoint
));
318 strcat(strcpy(pipe_name
, prefix
), endpoint
);
322 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
324 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
325 static const char prefix
[] = "\\\\";
326 static const char local
[] = ".";
327 BOOL bUseLocalName
= TRUE
;
328 CHAR ComputerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
329 DWORD bufLen
= sizeof(ComputerName
)/sizeof(ComputerName
[0]);
335 /* already connected? */
339 /* protseq=ncacn_np: named pipes */
340 size
= strlen(prefix
);
342 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
344 bUseLocalName
= TRUE
;
345 size
+= strlen(local
);
349 NetworkAddr
= Connection
->NetworkAddr
;
350 if (NetworkAddr
[0] == '\\' && NetworkAddr
[1] == '\\')
353 if (GetComputerNameA(ComputerName
, &bufLen
))
355 if (stricmp(ComputerName
, NetworkAddr
) == 0)
357 bUseLocalName
= TRUE
;
358 size
+= strlen(local
);
362 bUseLocalName
= FALSE
;
363 size
+= strlen(NetworkAddr
);
368 bUseLocalName
= FALSE
;
369 size
+= strlen(NetworkAddr
);
373 size
+= strlen(Connection
->Endpoint
) + 1;
375 pname
= I_RpcAllocate(size
);
376 strcpy(pname
, prefix
);
378 strcat(pname
, local
);
380 strcat(pname
, NetworkAddr
);
381 strcat(pname
, Connection
->Endpoint
);
382 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
388 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
392 RpcConnection
*Connection
;
393 char generated_endpoint
[21];
397 static LONG np_nameless_id
;
398 DWORD process_id
= GetCurrentProcessId();
399 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
400 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
401 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
402 endpoint
= generated_endpoint
;
405 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
406 endpoint
, NULL
, NULL
, NULL
, NULL
);
410 pname
= ncacn_pipe_name(Connection
->Endpoint
);
411 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
414 EnterCriticalSection(&protseq
->cs
);
415 Connection
->Next
= protseq
->conn
;
416 protseq
->conn
= Connection
;
417 LeaveCriticalSection(&protseq
->cs
);
422 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
424 /* because of the way named pipes work, we'll transfer the connected pipe
425 * to the child, then reopen the server binding to continue listening */
427 new_npc
->pipe
= old_npc
->pipe
;
428 new_npc
->ovl
= old_npc
->ovl
;
430 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
431 old_npc
->listening
= FALSE
;
434 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
436 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
440 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
442 pname
= ncacn_pipe_name(old_conn
->Endpoint
);
443 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
446 /* Store the local computer name as the NetworkAddr for ncacn_np as long as
447 * we don't support named pipes over the network. */
448 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
449 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
451 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
452 return RPC_S_OUT_OF_RESOURCES
;
458 static RPC_STATUS
is_pipe_listening(const char *pipe_name
)
460 return WaitNamedPipeA(pipe_name
, 1) ? RPC_S_OK
: RPC_S_NOT_LISTENING
;
463 static RPC_STATUS
rpcrt4_ncacn_np_is_server_listening(const char *endpoint
)
468 pipe_name
= ncacn_pipe_name(endpoint
);
469 status
= is_pipe_listening(pipe_name
);
470 I_RpcFree(pipe_name
);
474 static RPC_STATUS
rpcrt4_ncalrpc_np_is_server_listening(const char *endpoint
)
479 pipe_name
= ncalrpc_pipe_name(endpoint
);
480 status
= is_pipe_listening(pipe_name
);
481 I_RpcFree(pipe_name
);
485 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
487 DWORD len
= MAX_COMPUTERNAME_LENGTH
+ 1;
491 TRACE("%s\n", old_conn
->Endpoint
);
493 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
495 pname
= ncalrpc_pipe_name(old_conn
->Endpoint
);
496 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
499 /* Store the local computer name as the NetworkAddr for ncalrpc. */
500 new_conn
->NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, len
);
501 if (!GetComputerNameA(new_conn
->NetworkAddr
, &len
))
503 ERR("Failed to retrieve the computer name, error %u\n", GetLastError());
504 return RPC_S_OUT_OF_RESOURCES
;
510 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
511 void *buffer
, unsigned int count
)
513 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
516 unsigned int bytes_left
= count
;
519 ZeroMemory(&ovl
, sizeof(ovl
));
520 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
525 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
526 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
527 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
528 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
530 if (!ret
|| !bytes_read
)
532 bytes_left
-= bytes_read
;
535 CloseHandle(ovl
.hEvent
);
536 return ret
? count
: -1;
539 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
540 const void *buffer
, unsigned int count
)
542 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
543 const char *buf
= buffer
;
545 unsigned int bytes_left
= count
;
548 ZeroMemory(&ovl
, sizeof(ovl
));
549 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
554 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
555 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
556 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
557 if (!ret
|| !bytes_written
)
559 bytes_left
-= bytes_written
;
560 buf
+= bytes_written
;
562 CloseHandle(ovl
.hEvent
);
563 return ret
? count
: -1;
566 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
568 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
570 FlushFileBuffers(npc
->pipe
);
571 CloseHandle(npc
->pipe
);
574 if (npc
->ovl
.hEvent
) {
575 CloseHandle(npc
->ovl
.hEvent
);
581 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
583 /* FIXME: implement when named pipe writes use overlapped I/O */
586 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
588 /* FIXME: implement when named pipe writes use overlapped I/O */
592 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
593 const char *networkaddr
,
594 const char *endpoint
)
596 twr_empty_floor_t
*smb_floor
;
597 twr_empty_floor_t
*nb_floor
;
599 size_t networkaddr_size
;
600 size_t endpoint_size
;
602 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
604 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
605 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
606 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
611 smb_floor
= (twr_empty_floor_t
*)tower_data
;
613 tower_data
+= sizeof(*smb_floor
);
615 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
616 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
617 smb_floor
->count_rhs
= endpoint_size
;
620 memcpy(tower_data
, endpoint
, endpoint_size
);
623 tower_data
+= endpoint_size
;
625 nb_floor
= (twr_empty_floor_t
*)tower_data
;
627 tower_data
+= sizeof(*nb_floor
);
629 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
630 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
631 nb_floor
->count_rhs
= networkaddr_size
;
634 memcpy(tower_data
, networkaddr
, networkaddr_size
);
641 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
646 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
647 const twr_empty_floor_t
*nb_floor
;
649 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
651 if (tower_size
< sizeof(*smb_floor
))
652 return EPT_S_NOT_REGISTERED
;
654 tower_data
+= sizeof(*smb_floor
);
655 tower_size
-= sizeof(*smb_floor
);
657 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
658 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
659 (smb_floor
->count_rhs
> tower_size
) ||
660 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
661 return EPT_S_NOT_REGISTERED
;
665 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
667 return RPC_S_OUT_OF_RESOURCES
;
668 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
670 tower_data
+= smb_floor
->count_rhs
;
671 tower_size
-= smb_floor
->count_rhs
;
673 if (tower_size
< sizeof(*nb_floor
))
674 return EPT_S_NOT_REGISTERED
;
676 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
678 tower_data
+= sizeof(*nb_floor
);
679 tower_size
-= sizeof(*nb_floor
);
681 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
682 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
683 (nb_floor
->count_rhs
> tower_size
) ||
684 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
685 return EPT_S_NOT_REGISTERED
;
689 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
694 I_RpcFree(*endpoint
);
697 return RPC_S_OUT_OF_RESOURCES
;
699 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
705 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
707 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
710 TRACE("(%p)\n", conn
);
712 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
713 return RPCRT4_default_impersonate_client(conn
);
715 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
718 DWORD error
= GetLastError();
719 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
722 case ERROR_CANNOT_IMPERSONATE
:
723 return RPC_S_NO_CONTEXT_AVAILABLE
;
729 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
733 TRACE("(%p)\n", conn
);
735 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
736 return RPCRT4_default_revert_to_self(conn
);
738 ret
= RevertToSelf();
741 WARN("RevertToSelf failed with error %u\n", GetLastError());
742 return RPC_S_NO_CONTEXT_AVAILABLE
;
747 typedef struct _RpcServerProtseq_np
749 RpcServerProtseq common
;
751 } RpcServerProtseq_np
;
753 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
755 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
757 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
761 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
763 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
764 SetEvent(npps
->mgr_event
);
767 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
769 HANDLE
*objs
= prev_array
;
770 RpcConnection_np
*conn
;
771 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
773 EnterCriticalSection(&protseq
->cs
);
775 /* open and count connections */
777 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
779 rpcrt4_conn_listen_pipe(conn
);
780 if (conn
->ovl
.hEvent
)
782 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
785 /* make array of connections */
787 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
789 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
792 ERR("couldn't allocate objs\n");
793 LeaveCriticalSection(&protseq
->cs
);
797 objs
[0] = npps
->mgr_event
;
799 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
801 if ((objs
[*count
] = conn
->ovl
.hEvent
))
803 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
805 LeaveCriticalSection(&protseq
->cs
);
809 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
811 HeapFree(GetProcessHeap(), 0, array
);
814 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
817 HANDLE
*objs
= wait_array
;
819 RpcConnection
*cconn
;
820 RpcConnection_np
*conn
;
827 /* an alertable wait isn't strictly necessary, but due to our
828 * overlapped I/O implementation in Wine we need to free some memory
829 * by the file user APC being called, even if no completion routine was
830 * specified at the time of starting the async operation */
831 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
832 } while (res
== WAIT_IO_COMPLETION
);
834 if (res
== WAIT_OBJECT_0
)
836 else if (res
== WAIT_FAILED
)
838 ERR("wait failed with error %d\n", GetLastError());
843 b_handle
= objs
[res
- WAIT_OBJECT_0
];
844 /* find which connection got a RPC */
845 EnterCriticalSection(&protseq
->cs
);
846 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
848 if (b_handle
== conn
->ovl
.hEvent
) break;
849 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
853 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
855 ERR("failed to locate connection for handle %p\n", b_handle
);
856 LeaveCriticalSection(&protseq
->cs
);
859 RPCRT4_new_client(cconn
);
866 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
867 const char *networkaddr
,
868 const char *endpoint
)
870 twr_empty_floor_t
*pipe_floor
;
872 size_t endpoint_size
;
874 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
876 endpoint_size
= strlen(endpoint
) + 1;
877 size
= sizeof(*pipe_floor
) + endpoint_size
;
882 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
884 tower_data
+= sizeof(*pipe_floor
);
886 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
887 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
888 pipe_floor
->count_rhs
= endpoint_size
;
890 memcpy(tower_data
, endpoint
, endpoint_size
);
895 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
900 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
902 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
904 if (tower_size
< sizeof(*pipe_floor
))
905 return EPT_S_NOT_REGISTERED
;
907 tower_data
+= sizeof(*pipe_floor
);
908 tower_size
-= sizeof(*pipe_floor
);
910 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
911 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
912 (pipe_floor
->count_rhs
> tower_size
) ||
913 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
914 return EPT_S_NOT_REGISTERED
;
921 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
923 return RPC_S_OUT_OF_RESOURCES
;
924 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
930 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
935 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
936 unsigned char *in_buffer
,
937 unsigned int in_size
,
938 unsigned char *out_buffer
,
939 unsigned int *out_size
)
941 /* since this protocol is local to the machine there is no need to
942 * authenticate the caller */
947 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
948 enum secure_packet_direction dir
,
949 RpcPktHdr
*hdr
, unsigned int hdr_size
,
950 unsigned char *stub_data
, unsigned int stub_data_size
,
951 RpcAuthVerifier
*auth_hdr
,
952 unsigned char *auth_value
, unsigned int auth_value_size
)
954 /* since this protocol is local to the machine there is no need to secure
959 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
960 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
961 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
963 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
964 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
968 FIXME("privs not implemented\n");
971 if (server_princ_name
)
973 FIXME("server_princ_name not implemented\n");
974 *server_princ_name
= NULL
;
976 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
977 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
980 FIXME("authorization service not implemented\n");
981 *authz_svc
= RPC_C_AUTHZ_NONE
;
984 FIXME("flags 0x%x not implemented\n", flags
);
989 /**** ncacn_ip_tcp support ****/
991 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
992 const char *networkaddr
,
993 unsigned char tcp_protid
,
994 const char *endpoint
)
996 twr_tcp_floor_t
*tcp_floor
;
997 twr_ipv4_floor_t
*ipv4_floor
;
999 struct addrinfo hints
;
1001 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1003 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1008 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1009 tower_data
+= sizeof(*tcp_floor
);
1011 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1013 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1014 tcp_floor
->protid
= tcp_protid
;
1015 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1017 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1018 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1019 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1021 hints
.ai_flags
= AI_NUMERICHOST
;
1022 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1023 hints
.ai_family
= PF_INET
;
1024 hints
.ai_socktype
= SOCK_STREAM
;
1025 hints
.ai_protocol
= IPPROTO_TCP
;
1026 hints
.ai_addrlen
= 0;
1027 hints
.ai_addr
= NULL
;
1028 hints
.ai_canonname
= NULL
;
1029 hints
.ai_next
= NULL
;
1031 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1034 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1037 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1042 if (ai
->ai_family
== PF_INET
)
1044 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1045 tcp_floor
->port
= sin
->sin_port
;
1046 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1050 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1060 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1063 unsigned char tcp_protid
,
1066 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1067 const twr_ipv4_floor_t
*ipv4_floor
;
1068 struct in_addr in_addr
;
1070 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1072 if (tower_size
< sizeof(*tcp_floor
))
1073 return EPT_S_NOT_REGISTERED
;
1075 tower_data
+= sizeof(*tcp_floor
);
1076 tower_size
-= sizeof(*tcp_floor
);
1078 if (tower_size
< sizeof(*ipv4_floor
))
1079 return EPT_S_NOT_REGISTERED
;
1081 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1083 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1084 (tcp_floor
->protid
!= tcp_protid
) ||
1085 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1086 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1087 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1088 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1089 return EPT_S_NOT_REGISTERED
;
1093 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1095 return RPC_S_OUT_OF_RESOURCES
;
1096 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1101 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1106 I_RpcFree(*endpoint
);
1109 return RPC_S_OUT_OF_RESOURCES
;
1111 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1112 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1114 ERR("inet_ntop: %s\n", strerror(errno
));
1115 I_RpcFree(*networkaddr
);
1116 *networkaddr
= NULL
;
1119 I_RpcFree(*endpoint
);
1122 return EPT_S_NOT_REGISTERED
;
1129 typedef struct _RpcConnection_tcp
1131 RpcConnection common
;
1133 #ifdef HAVE_SOCKETPAIR
1137 HANDLE cancel_event
;
1139 } RpcConnection_tcp
;
1141 #ifdef HAVE_SOCKETPAIR
1143 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1145 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1147 ERR("socketpair() failed: %s\n", strerror(errno
));
1153 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1155 struct pollfd pfds
[2];
1156 pfds
[0].fd
= tcpc
->sock
;
1157 pfds
[0].events
= POLLIN
;
1158 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1159 pfds
[1].events
= POLLIN
;
1160 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1162 ERR("poll() failed: %s\n", strerror(errno
));
1165 if (pfds
[1].revents
& POLLIN
) /* canceled */
1168 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1174 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1177 pfd
.fd
= tcpc
->sock
;
1178 pfd
.events
= POLLOUT
;
1179 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1181 ERR("poll() failed: %s\n", strerror(errno
));
1187 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1191 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1194 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1196 close(tcpc
->cancel_fds
[0]);
1197 close(tcpc
->cancel_fds
[1]);
1200 #else /* HAVE_SOCKETPAIR */
1202 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1204 static BOOL wsa_inited
;
1208 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1209 /* Note: WSAStartup can be called more than once so we don't bother with
1210 * making accesses to wsa_inited thread-safe */
1213 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1214 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1215 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1217 ERR("event creation failed\n");
1218 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1224 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1226 HANDLE wait_handles
[2];
1228 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1230 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1233 wait_handles
[0] = tcpc
->sock_event
;
1234 wait_handles
[1] = tcpc
->cancel_event
;
1235 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1240 case WAIT_OBJECT_0
+ 1:
1243 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1248 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1251 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1253 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1256 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1262 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1267 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1269 SetEvent(tcpc
->cancel_event
);
1272 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1274 CloseHandle(tcpc
->sock_event
);
1275 CloseHandle(tcpc
->cancel_event
);
1280 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1282 RpcConnection_tcp
*tcpc
;
1283 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1287 if (!rpcrt4_sock_wait_init(tcpc
))
1289 HeapFree(GetProcessHeap(), 0, tcpc
);
1292 return &tcpc
->common
;
1295 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1297 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1300 struct addrinfo
*ai
;
1301 struct addrinfo
*ai_cur
;
1302 struct addrinfo hints
;
1304 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1306 if (tcpc
->sock
!= -1)
1310 hints
.ai_family
= PF_UNSPEC
;
1311 hints
.ai_socktype
= SOCK_STREAM
;
1312 hints
.ai_protocol
= IPPROTO_TCP
;
1313 hints
.ai_addrlen
= 0;
1314 hints
.ai_addr
= NULL
;
1315 hints
.ai_canonname
= NULL
;
1316 hints
.ai_next
= NULL
;
1318 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1321 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1322 Connection
->Endpoint
, gai_strerror(ret
));
1323 return RPC_S_SERVER_UNAVAILABLE
;
1326 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1331 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1333 TRACE("skipping non-IP/IPv6 address family\n");
1341 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1342 host
, sizeof(host
), service
, sizeof(service
),
1343 NI_NUMERICHOST
| NI_NUMERICSERV
);
1344 TRACE("trying %s:%s\n", host
, service
);
1347 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1350 WARN("socket() failed: %s\n", strerror(errno
));
1354 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1356 WARN("connect() failed: %s\n", strerror(errno
));
1361 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1363 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1365 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1370 TRACE("connected\n");
1375 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1376 return RPC_S_SERVER_UNAVAILABLE
;
1379 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1381 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1384 struct addrinfo
*ai
;
1385 struct addrinfo
*ai_cur
;
1386 struct addrinfo hints
;
1387 RpcConnection
*first_connection
= NULL
;
1389 TRACE("(%p, %s)\n", protseq
, endpoint
);
1391 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1392 hints
.ai_family
= PF_UNSPEC
;
1393 hints
.ai_socktype
= SOCK_STREAM
;
1394 hints
.ai_protocol
= IPPROTO_TCP
;
1395 hints
.ai_addrlen
= 0;
1396 hints
.ai_addr
= NULL
;
1397 hints
.ai_canonname
= NULL
;
1398 hints
.ai_next
= NULL
;
1400 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1403 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1405 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1406 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1407 return RPC_S_CANT_CREATE_ENDPOINT
;
1410 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1412 RpcConnection_tcp
*tcpc
;
1413 RPC_STATUS create_status
;
1414 struct sockaddr_storage sa
;
1416 char service
[NI_MAXSERV
];
1419 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1421 TRACE("skipping non-IP/IPv6 address family\n");
1428 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1429 host
, sizeof(host
), service
, sizeof(service
),
1430 NI_NUMERICHOST
| NI_NUMERICSERV
);
1431 TRACE("trying %s:%s\n", host
, service
);
1434 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1437 WARN("socket() failed: %s\n", strerror(errno
));
1438 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1442 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1445 WARN("bind failed: %s\n", strerror(errno
));
1447 if (errno
== EADDRINUSE
)
1448 status
= RPC_S_DUPLICATE_ENDPOINT
;
1450 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1454 sa_len
= sizeof(sa
);
1455 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1457 WARN("getsockname() failed: %s\n", strerror(errno
));
1459 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1463 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1464 NULL
, 0, service
, sizeof(service
),
1468 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1470 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1474 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1475 protseq
->Protseq
, NULL
,
1476 service
, NULL
, NULL
, NULL
, NULL
);
1477 if (create_status
!= RPC_S_OK
)
1480 status
= create_status
;
1485 ret
= listen(sock
, protseq
->MaxCalls
);
1488 WARN("listen failed: %s\n", strerror(errno
));
1489 RPCRT4_ReleaseConnection(&tcpc
->common
);
1490 status
= RPC_S_OUT_OF_RESOURCES
;
1493 /* need a non-blocking socket, otherwise accept() has a potential
1494 * race-condition (poll() says it is readable, connection drops,
1495 * and accept() blocks until the next connection comes...)
1498 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1501 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1502 RPCRT4_ReleaseConnection(&tcpc
->common
);
1503 status
= RPC_S_OUT_OF_RESOURCES
;
1507 tcpc
->common
.Next
= first_connection
;
1508 first_connection
= &tcpc
->common
;
1510 /* since IPv4 and IPv6 share the same port space, we only need one
1511 * successful bind to listen for both */
1517 /* if at least one connection was created for an endpoint then
1519 if (first_connection
)
1521 RpcConnection
*conn
;
1523 /* find last element in list */
1524 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1527 EnterCriticalSection(&protseq
->cs
);
1528 conn
->Next
= protseq
->conn
;
1529 protseq
->conn
= first_connection
;
1530 LeaveCriticalSection(&protseq
->cs
);
1532 TRACE("listening on %s\n", endpoint
);
1536 ERR("couldn't listen on port %s\n", endpoint
);
1540 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1543 struct sockaddr_in address
;
1545 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1546 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1549 addrsize
= sizeof(address
);
1550 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1553 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1554 return RPC_S_OUT_OF_RESOURCES
;
1558 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1561 client
->common
.NetworkAddr
= HeapAlloc(GetProcessHeap(), 0, INET6_ADDRSTRLEN
);
1562 ret
= getnameinfo((struct sockaddr
*)&address
, addrsize
, client
->common
.NetworkAddr
, INET6_ADDRSTRLEN
, NULL
, 0, NI_NUMERICHOST
);
1565 ERR("Failed to retrieve the IP address, error %d\n", ret
);
1566 return RPC_S_OUT_OF_RESOURCES
;
1569 TRACE("Accepted a new TCP connection from %s\n", client
->common
.NetworkAddr
);
1573 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1574 void *buffer
, unsigned int count
)
1576 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1578 while (bytes_read
!= count
)
1580 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1585 else if (errno
== EINTR
)
1587 else if (errno
!= EAGAIN
)
1589 WARN("recv() failed: %s\n", strerror(errno
));
1594 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1598 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1602 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1603 const void *buffer
, unsigned int count
)
1605 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1606 int bytes_written
= 0;
1607 while (bytes_written
!= count
)
1609 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1612 else if (errno
== EINTR
)
1614 else if (errno
!= EAGAIN
)
1618 if (!rpcrt4_sock_wait_for_send(tcpc
))
1622 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1623 return bytes_written
;
1626 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1628 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1630 TRACE("%d\n", tcpc
->sock
);
1632 if (tcpc
->sock
!= -1)
1633 closesocket(tcpc
->sock
);
1635 rpcrt4_sock_wait_destroy(tcpc
);
1639 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1641 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1642 TRACE("%p\n", Connection
);
1643 rpcrt4_sock_wait_cancel(tcpc
);
1646 static RPC_STATUS
rpcrt4_conn_tcp_is_server_listening(const char *endpoint
)
1649 return RPC_S_ACCESS_DENIED
;
1652 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1654 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1656 TRACE("%p\n", Connection
);
1658 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1663 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1664 const char *networkaddr
,
1665 const char *endpoint
)
1667 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1668 EPM_PROTOCOL_TCP
, endpoint
);
1671 #ifdef HAVE_SOCKETPAIR
1673 typedef struct _RpcServerProtseq_sock
1675 RpcServerProtseq common
;
1678 } RpcServerProtseq_sock
;
1680 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1682 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1686 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1688 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1689 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1690 ps
->mgr_event_rcv
= fds
[0];
1691 ps
->mgr_event_snd
= fds
[1];
1695 ERR("socketpair failed with error %s\n", strerror(errno
));
1696 HeapFree(GetProcessHeap(), 0, ps
);
1703 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1705 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1707 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1710 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1712 struct pollfd
*poll_info
= prev_array
;
1713 RpcConnection_tcp
*conn
;
1714 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1716 EnterCriticalSection(&protseq
->cs
);
1718 /* open and count connections */
1720 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1722 if (conn
->sock
!= -1)
1724 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1727 /* make array of connections */
1729 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1731 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1734 ERR("couldn't allocate poll_info\n");
1735 LeaveCriticalSection(&protseq
->cs
);
1739 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1740 poll_info
[0].events
= POLLIN
;
1742 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1744 if (conn
->sock
!= -1)
1746 poll_info
[*count
].fd
= conn
->sock
;
1747 poll_info
[*count
].events
= POLLIN
;
1750 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1752 LeaveCriticalSection(&protseq
->cs
);
1756 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1758 HeapFree(GetProcessHeap(), 0, array
);
1761 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1763 struct pollfd
*poll_info
= wait_array
;
1766 RpcConnection
*cconn
;
1767 RpcConnection_tcp
*conn
;
1772 ret
= poll(poll_info
, count
, -1);
1775 ERR("poll failed with error %d\n", ret
);
1779 for (i
= 0; i
< count
; i
++)
1780 if (poll_info
[i
].revents
& POLLIN
)
1782 /* RPC server event */
1786 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1790 /* find which connection got a RPC */
1791 EnterCriticalSection(&protseq
->cs
);
1792 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1794 if (poll_info
[i
].fd
== conn
->sock
) break;
1795 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1799 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1801 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1802 LeaveCriticalSection(&protseq
->cs
);
1804 RPCRT4_new_client(cconn
);
1812 #else /* HAVE_SOCKETPAIR */
1814 typedef struct _RpcServerProtseq_sock
1816 RpcServerProtseq common
;
1818 } RpcServerProtseq_sock
;
1820 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1822 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1825 static BOOL wsa_inited
;
1829 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1830 /* Note: WSAStartup can be called more than once so we don't bother with
1831 * making accesses to wsa_inited thread-safe */
1834 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1839 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1841 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1842 SetEvent(sockps
->mgr_event
);
1845 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1847 HANDLE
*objs
= prev_array
;
1848 RpcConnection_tcp
*conn
;
1849 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1851 EnterCriticalSection(&protseq
->cs
);
1853 /* open and count connections */
1855 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1858 if (conn
->sock
!= -1)
1860 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1863 /* make array of connections */
1865 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1867 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1870 ERR("couldn't allocate objs\n");
1871 LeaveCriticalSection(&protseq
->cs
);
1875 objs
[0] = sockps
->mgr_event
;
1877 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1880 if (conn
->sock
!= -1)
1882 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1883 if (res
== SOCKET_ERROR
)
1884 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1887 objs
[*count
] = conn
->sock_event
;
1891 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1893 LeaveCriticalSection(&protseq
->cs
);
1897 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1899 HeapFree(GetProcessHeap(), 0, array
);
1902 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1905 HANDLE
*objs
= wait_array
;
1907 RpcConnection
*cconn
;
1908 RpcConnection_tcp
*conn
;
1915 /* an alertable wait isn't strictly necessary, but due to our
1916 * overlapped I/O implementation in Wine we need to free some memory
1917 * by the file user APC being called, even if no completion routine was
1918 * specified at the time of starting the async operation */
1919 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1920 } while (res
== WAIT_IO_COMPLETION
);
1922 if (res
== WAIT_OBJECT_0
)
1924 else if (res
== WAIT_FAILED
)
1926 ERR("wait failed with error %d\n", GetLastError());
1931 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1932 /* find which connection got a RPC */
1933 EnterCriticalSection(&protseq
->cs
);
1934 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1937 if (b_handle
== conn
->sock_event
) break;
1938 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1942 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1944 ERR("failed to locate connection for handle %p\n", b_handle
);
1945 LeaveCriticalSection(&protseq
->cs
);
1948 RPCRT4_new_client(cconn
);
1955 #endif /* HAVE_SOCKETPAIR */
1957 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1962 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1963 networkaddr
, EPM_PROTOCOL_TCP
,
1967 /**** ncacn_http support ****/
1969 /* 60 seconds is the period native uses */
1970 #define HTTP_IDLE_TIME 60000
1972 /* reference counted to avoid a race between a cancelled call's connection
1973 * being destroyed and the asynchronous InternetReadFileEx call being
1975 typedef struct _RpcHttpAsyncData
1978 HANDLE completion_event
;
1980 INTERNET_BUFFERSW inet_buffers
;
1981 CRITICAL_SECTION cs
;
1984 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1986 return InterlockedIncrement(&data
->refs
);
1989 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1991 ULONG refs
= InterlockedDecrement(&data
->refs
);
1994 TRACE("destroying async data %p\n", data
);
1995 CloseHandle(data
->completion_event
);
1996 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1997 data
->cs
.DebugInfo
->Spare
[0] = 0;
1998 DeleteCriticalSection(&data
->cs
);
1999 HeapFree(GetProcessHeap(), 0, data
);
2004 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
2006 ResetEvent(async_data
->completion_event
);
2007 RpcHttpAsyncData_AddRef(async_data
);
2010 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
2012 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
2016 RpcHttpAsyncData_Release(async_data
);
2020 if(GetLastError() != ERROR_IO_PENDING
) {
2021 RpcHttpAsyncData_Release(async_data
);
2022 ERR("Request failed with error %d\n", GetLastError());
2023 return RPC_S_SERVER_UNAVAILABLE
;
2026 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2027 if(res
!= WAIT_OBJECT_0
) {
2028 TRACE("Cancelled\n");
2029 return RPC_S_CALL_CANCELLED
;
2032 if(async_data
->async_result
) {
2033 ERR("Async request failed with error %d\n", async_data
->async_result
);
2034 return RPC_S_SERVER_UNAVAILABLE
;
2049 unsigned int data_len
;
2050 BOOL finished
; /* finished authenticating */
2053 typedef struct _RpcConnection_http
2055 RpcConnection common
;
2058 HINTERNET in_request
;
2059 HINTERNET out_request
;
2061 HANDLE timer_cancelled
;
2062 HANDLE cancel_event
;
2063 DWORD last_sent_time
;
2064 ULONG bytes_received
;
2065 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
2066 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
2067 UUID connection_uuid
;
2070 RpcHttpAsyncData
*async_data
;
2071 } RpcConnection_http
;
2073 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
2075 RpcConnection_http
*httpc
;
2076 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
2077 if (!httpc
) return NULL
;
2078 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
2079 if (!httpc
->async_data
)
2081 HeapFree(GetProcessHeap(), 0, httpc
);
2084 TRACE("async data = %p\n", httpc
->async_data
);
2085 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2086 httpc
->async_data
->refs
= 1;
2087 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSW
);
2088 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2089 InitializeCriticalSection(&httpc
->async_data
->cs
);
2090 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
2091 return &httpc
->common
;
2094 typedef struct _HttpTimerThreadData
2097 DWORD
*last_sent_time
;
2098 HANDLE timer_cancelled
;
2099 } HttpTimerThreadData
;
2101 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
2103 HINTERNET in_request
= param
;
2104 RpcPktHdr
*idle_pkt
;
2106 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
2110 DWORD bytes_written
;
2111 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
2112 RPCRT4_FreeHeader(idle_pkt
);
2116 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
2118 DWORD cur_time
= GetTickCount();
2119 DWORD cached_last_sent_time
= *last_sent_time
;
2120 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2123 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2125 HttpTimerThreadData
*data_in
= param
;
2126 HttpTimerThreadData data
;
2130 HeapFree(GetProcessHeap(), 0, data_in
);
2132 for (timeout
= HTTP_IDLE_TIME
;
2133 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2134 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2136 /* are we too soon after last send? */
2137 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2139 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2142 CloseHandle(data
.timer_cancelled
);
2146 static VOID WINAPI
rpcrt4_http_internet_callback(
2147 HINTERNET hInternet
,
2148 DWORD_PTR dwContext
,
2149 DWORD dwInternetStatus
,
2150 LPVOID lpvStatusInformation
,
2151 DWORD dwStatusInformationLength
)
2153 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2155 switch (dwInternetStatus
)
2157 case INTERNET_STATUS_REQUEST_COMPLETE
:
2158 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2161 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2163 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2164 SetEvent(async_data
->completion_event
);
2165 RpcHttpAsyncData_Release(async_data
);
2171 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2178 WCHAR
*status_text
= buf
;
2182 size
= sizeof(status_code
);
2183 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2185 return GetLastError();
2186 if (status_code
== HTTP_STATUS_OK
)
2190 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2191 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2193 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2194 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2197 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2198 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2200 if (status_code
== HTTP_STATUS_DENIED
)
2201 return ERROR_ACCESS_DENIED
;
2202 return RPC_S_SERVER_UNAVAILABLE
;
2205 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2207 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2208 LPWSTR proxy
= NULL
;
2210 LPWSTR password
= NULL
;
2211 LPWSTR servername
= NULL
;
2212 const WCHAR
*option
;
2215 if (httpc
->common
.QOS
&&
2216 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2218 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2219 if (http_cred
->TransportCredentials
)
2222 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2223 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2224 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2226 return RPC_S_OUT_OF_RESOURCES
;
2228 if (cred
->DomainLength
)
2230 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2231 p
+= cred
->DomainLength
;
2235 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2236 p
[cred
->UserLength
] = 0;
2238 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2242 for (option
= httpc
->common
.NetworkOptions
; option
;
2243 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2245 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2246 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2248 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2250 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2251 const WCHAR
*value_end
;
2254 value_end
= strchrW(option
, ',');
2256 value_end
= value_start
+ strlenW(value_start
);
2257 for (p
= value_start
; p
< value_end
; p
++)
2264 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2265 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2267 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2269 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2270 const WCHAR
*value_end
;
2272 value_end
= strchrW(option
, ',');
2274 value_end
= value_start
+ strlenW(value_start
);
2275 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2276 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2279 FIXME("unhandled option %s\n", debugstr_w(option
));
2282 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2283 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2284 if (!httpc
->app_info
)
2286 HeapFree(GetProcessHeap(), 0, password
);
2287 HeapFree(GetProcessHeap(), 0, user
);
2288 HeapFree(GetProcessHeap(), 0, proxy
);
2289 HeapFree(GetProcessHeap(), 0, servername
);
2290 ERR("InternetOpenW failed with error %d\n", GetLastError());
2291 return RPC_S_SERVER_UNAVAILABLE
;
2293 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2295 /* if no RpcProxy option specified, set the HTTP server address to the
2296 * RPC server address */
2299 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2302 HeapFree(GetProcessHeap(), 0, password
);
2303 HeapFree(GetProcessHeap(), 0, user
);
2304 HeapFree(GetProcessHeap(), 0, proxy
);
2305 return RPC_S_OUT_OF_RESOURCES
;
2307 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2310 port
= (httpc
->common
.QOS
&&
2311 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2312 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2313 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2315 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2316 INTERNET_SERVICE_HTTP
, 0, 0);
2318 HeapFree(GetProcessHeap(), 0, password
);
2319 HeapFree(GetProcessHeap(), 0, user
);
2320 HeapFree(GetProcessHeap(), 0, proxy
);
2322 if (!httpc
->session
)
2324 ERR("InternetConnectW failed with error %d\n", GetLastError());
2325 HeapFree(GetProcessHeap(), 0, servername
);
2326 return RPC_S_SERVER_UNAVAILABLE
;
2328 httpc
->servername
= servername
;
2332 static int rpcrt4_http_async_read(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2333 void *buffer
, unsigned int count
)
2337 unsigned int bytes_left
= count
;
2338 RPC_STATUS status
= RPC_S_OK
;
2340 async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
2344 async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2345 prepare_async_request(async_data
);
2346 ret
= InternetReadFileExW(req
, &async_data
->inet_buffers
, IRF_ASYNC
, 0);
2347 status
= wait_async_request(async_data
, ret
, cancel_event
);
2348 if (status
!= RPC_S_OK
)
2350 if (status
== RPC_S_CALL_CANCELLED
)
2351 TRACE("call cancelled\n");
2355 if (!async_data
->inet_buffers
.dwBufferLength
)
2357 memcpy(buf
, async_data
->inet_buffers
.lpvBuffer
,
2358 async_data
->inet_buffers
.dwBufferLength
);
2360 bytes_left
-= async_data
->inet_buffers
.dwBufferLength
;
2361 buf
+= async_data
->inet_buffers
.dwBufferLength
;
2364 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2365 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2367 TRACE("%p %p %u -> %u\n", req
, buffer
, count
, status
);
2368 return status
== RPC_S_OK
? count
: -1;
2371 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2377 TRACE("sending echo request to server\n");
2379 prepare_async_request(async_data
);
2380 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2381 status
= wait_async_request(async_data
, ret
, cancel_event
);
2382 if (status
!= RPC_S_OK
) return status
;
2384 status
= rpcrt4_http_check_response(req
);
2385 if (status
!= RPC_S_OK
) return status
;
2387 rpcrt4_http_async_read(req
, async_data
, cancel_event
, buf
, sizeof(buf
));
2388 /* FIXME: do something with retrieved data */
2393 static RPC_STATUS
insert_content_length_header(HINTERNET request
, DWORD len
)
2395 static const WCHAR fmtW
[] =
2396 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
2397 WCHAR header
[sizeof(fmtW
) / sizeof(fmtW
[0]) + 10];
2399 sprintfW(header
, fmtW
, len
);
2400 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
))) return RPC_S_OK
;
2401 return RPC_S_SERVER_UNAVAILABLE
;
2404 /* prepare the in pipe for use by RPC packets */
2405 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2406 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2407 const UUID
*association_uuid
, BOOL authorized
)
2412 INTERNET_BUFFERSW buffers_in
;
2413 DWORD bytes_written
;
2417 /* ask wininet to authorize, if necessary */
2418 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2419 if (status
!= RPC_S_OK
) return status
;
2421 memset(&buffers_in
, 0, sizeof(buffers_in
));
2422 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2423 /* FIXME: get this from the registry */
2424 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2425 status
= insert_content_length_header(in_request
, buffers_in
.dwBufferTotal
);
2426 if (status
!= RPC_S_OK
) return status
;
2428 prepare_async_request(async_data
);
2429 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2430 status
= wait_async_request(async_data
, ret
, cancel_event
);
2431 if (status
!= RPC_S_OK
) return status
;
2433 TRACE("sending HTTP connect header to server\n");
2434 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2435 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2436 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2437 RPCRT4_FreeHeader(hdr
);
2440 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2441 return RPC_S_SERVER_UNAVAILABLE
;
2447 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcHttpAsyncData
*async_data
,
2448 HANDLE cancel_event
, RpcPktHdr
*hdr
, BYTE
**data
)
2450 unsigned short data_len
;
2453 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, hdr
, sizeof(hdr
->common
)) < 0)
2454 return RPC_S_SERVER_UNAVAILABLE
;
2455 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2457 ERR("wrong packet type received %d or wrong frag_len %d\n",
2458 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2459 return RPC_S_PROTOCOL_ERROR
;
2462 size
= sizeof(hdr
->http
) - sizeof(hdr
->common
);
2463 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, &hdr
->common
+ 1, size
) < 0)
2464 return RPC_S_SERVER_UNAVAILABLE
;
2466 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2469 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2471 return RPC_S_OUT_OF_RESOURCES
;
2472 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, *data
, data_len
) < 0)
2474 HeapFree(GetProcessHeap(), 0, *data
);
2475 return RPC_S_SERVER_UNAVAILABLE
;
2481 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2483 ERR("invalid http packet\n");
2484 HeapFree(GetProcessHeap(), 0, *data
);
2485 return RPC_S_PROTOCOL_ERROR
;
2491 /* prepare the out pipe for use by RPC packets */
2492 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2493 HANDLE cancel_event
, const UUID
*connection_uuid
,
2494 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2500 BYTE
*data_from_server
;
2501 RpcPktHdr pkt_from_server
;
2502 ULONG field1
, field3
;
2507 /* ask wininet to authorize, if necessary */
2508 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2509 if (status
!= RPC_S_OK
) return status
;
2512 rpcrt4_http_async_read(out_request
, async_data
, cancel_event
, buf
, sizeof(buf
));
2514 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2515 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2517 status
= insert_content_length_header(out_request
, hdr
->common
.frag_len
);
2518 if (status
!= RPC_S_OK
)
2520 RPCRT4_FreeHeader(hdr
);
2524 TRACE("sending HTTP connect header to server\n");
2525 prepare_async_request(async_data
);
2526 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2527 status
= wait_async_request(async_data
, ret
, cancel_event
);
2528 RPCRT4_FreeHeader(hdr
);
2529 if (status
!= RPC_S_OK
) return status
;
2531 status
= rpcrt4_http_check_response(out_request
);
2532 if (status
!= RPC_S_OK
) return status
;
2534 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2535 &pkt_from_server
, &data_from_server
);
2536 if (status
!= RPC_S_OK
) return status
;
2537 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2539 HeapFree(GetProcessHeap(), 0, data_from_server
);
2540 if (status
!= RPC_S_OK
) return status
;
2541 TRACE("received (%d) from first prepare header\n", field1
);
2545 status
= rpcrt4_http_read_http_packet(out_request
, async_data
, cancel_event
,
2546 &pkt_from_server
, &data_from_server
);
2547 if (status
!= RPC_S_OK
) return status
;
2548 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2550 TRACE("http idle packet, waiting for real packet\n");
2551 HeapFree(GetProcessHeap(), 0, data_from_server
);
2552 if (pkt_from_server
.http
.num_data_items
!= 0)
2554 ERR("HTTP idle packet should have no data items instead of %d\n",
2555 pkt_from_server
.http
.num_data_items
);
2556 return RPC_S_PROTOCOL_ERROR
;
2559 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2560 &field1
, flow_control_increment
,
2562 HeapFree(GetProcessHeap(), 0, data_from_server
);
2563 if (status
!= RPC_S_OK
) return status
;
2564 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2569 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2571 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2576 /* first 6 bits, all from bin[0] */
2577 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2578 x
= (bin
[0] & 3) << 4;
2580 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2583 base64
[i
++] = enc
[x
];
2588 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2589 x
= (bin
[1] & 0x0f) << 2;
2591 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2594 base64
[i
++] = enc
[x
];
2598 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2600 /* last 6 bits, all from bin [2] */
2601 base64
[i
++] = enc
[bin
[2] & 0x3f];
2609 static inline char decode_char( WCHAR c
)
2611 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2612 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2613 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2614 if (c
== '+') return 62;
2615 if (c
== '/') return 63;
2619 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2622 char c0
, c1
, c2
, c3
;
2623 const WCHAR
*p
= base64
;
2627 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2628 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2629 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2630 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2634 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2635 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2636 buf
[i
+ 2] = (c2
<< 6) | c3
;
2644 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2645 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2647 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2650 else if (p
[3] == '=')
2652 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2653 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2654 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2658 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2659 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2665 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2666 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2667 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2668 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2672 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2673 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2674 buf
[i
+ 2] = (c2
<< 6) | c3
;
2681 static struct authinfo
*alloc_authinfo(void)
2683 struct authinfo
*ret
;
2685 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2687 SecInvalidateHandle(&ret
->cred
);
2688 SecInvalidateHandle(&ret
->ctx
);
2689 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2695 ret
->finished
= FALSE
;
2699 static void destroy_authinfo(struct authinfo
*info
)
2703 if (SecIsValidHandle(&info
->ctx
))
2704 DeleteSecurityContext(&info
->ctx
);
2705 if (SecIsValidHandle(&info
->cred
))
2706 FreeCredentialsHandle(&info
->cred
);
2708 HeapFree(GetProcessHeap(), 0, info
->data
);
2709 HeapFree(GetProcessHeap(), 0, info
);
2712 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2713 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2714 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2715 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2716 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2726 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2727 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2728 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2729 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2730 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2732 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2734 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2737 for (i
= 0; i
< num_auth_schemes
; i
++)
2739 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2740 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2745 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2747 DWORD len
, index
= 0;
2751 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2752 if (auth_scheme_from_header(buffer
) == scheme
) break;
2757 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2758 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2760 struct authinfo
*info
= *auth_ptr
;
2761 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2762 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2764 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2766 switch (creds
->AuthnSchemes
[0])
2768 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2770 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2771 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2773 info
->data_len
= userlen
+ passlen
+ 1;
2774 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2776 status
= RPC_S_OUT_OF_MEMORY
;
2779 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2780 info
->data
[userlen
] = ':';
2781 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2783 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2784 info
->finished
= TRUE
;
2788 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2789 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2792 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2793 SECURITY_STATUS ret
;
2794 SecBufferDesc out_desc
, in_desc
;
2796 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2800 WCHAR auth_value
[2048];
2801 DWORD size
= sizeof(auth_value
);
2804 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2805 else scheme
= negotiateW
;
2806 scheme_len
= strlenW( scheme
);
2811 SecPkgInfoW
*pkg_info
;
2813 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2814 if (ret
!= SEC_E_OK
) break;
2816 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2817 if (ret
!= SEC_E_OK
) break;
2819 info
->max_token
= pkg_info
->cbMaxToken
;
2820 FreeContextBuffer(pkg_info
);
2825 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2826 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2828 ERR("authentication scheme changed\n");
2832 in
.BufferType
= SECBUFFER_TOKEN
;
2836 in_desc
.ulVersion
= 0;
2837 in_desc
.cBuffers
= 1;
2838 in_desc
.pBuffers
= &in
;
2840 p
= auth_value
+ scheme_len
;
2841 if (!first
&& *p
== ' ')
2843 int len
= strlenW(++p
);
2844 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2845 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2846 decode_base64(p
, len
, in
.pvBuffer
);
2848 out
.BufferType
= SECBUFFER_TOKEN
;
2849 out
.cbBuffer
= info
->max_token
;
2850 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2852 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2855 out_desc
.ulVersion
= 0;
2856 out_desc
.cBuffers
= 1;
2857 out_desc
.pBuffers
= &out
;
2859 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2860 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2861 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2862 &info
->attr
, &info
->exp
);
2863 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2864 if (ret
== SEC_E_OK
)
2866 HeapFree(GetProcessHeap(), 0, info
->data
);
2867 info
->data
= out
.pvBuffer
;
2868 info
->data_len
= out
.cbBuffer
;
2869 info
->finished
= TRUE
;
2870 TRACE("sending last auth packet\n");
2873 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2875 HeapFree(GetProcessHeap(), 0, info
->data
);
2876 info
->data
= out
.pvBuffer
;
2877 info
->data_len
= out
.cbBuffer
;
2878 TRACE("sending next auth packet\n");
2883 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2884 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2887 info
->scheme
= creds
->AuthnSchemes
[0];
2891 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2895 if (status
!= RPC_S_OK
)
2897 destroy_authinfo(info
);
2905 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2907 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2908 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2909 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2910 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2911 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2912 const WCHAR
*scheme_str
;
2913 WCHAR
*header
, *ptr
;
2914 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2918 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2919 scheme_str
= basicW
;
2920 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2922 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2923 scheme_str
= negotiateW
;
2924 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2926 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2928 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2931 ERR("unknown scheme %u\n", scheme
);
2932 return RPC_S_SERVER_UNAVAILABLE
;
2934 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2936 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2937 ptr
= header
+ auth_len
;
2938 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2940 len
= encode_base64(data
, data_len
, ptr
);
2944 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2946 HeapFree(GetProcessHeap(), 0, header
);
2951 static void drain_content(HINTERNET request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2953 DWORD count
, len
= 0, size
= sizeof(len
);
2956 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2960 count
= min(sizeof(buf
), len
);
2961 if (rpcrt4_http_async_read(request
, async_data
, cancel_event
, buf
, count
) <= 0) return;
2966 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2968 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2969 struct authinfo
*info
= NULL
;
2975 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2976 if (status
!= RPC_S_OK
) break;
2978 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2979 if (status
!= RPC_S_OK
) break;
2981 prepare_async_request(httpc
->async_data
);
2982 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2983 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2984 if (status
!= RPC_S_OK
|| info
->finished
) break;
2986 status
= rpcrt4_http_check_response(request
);
2987 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
2988 drain_content(request
, httpc
->async_data
, httpc
->cancel_event
);
2991 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2992 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
2994 destroy_authinfo(info
);
2998 static BOOL
has_credentials(RpcConnection_http
*httpc
)
3000 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
3001 SEC_WINNT_AUTH_IDENTITY_W
*id
;
3003 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
3006 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
3007 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
3010 id
= creds
->TransportCredentials
;
3011 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
3016 static BOOL
is_secure(RpcConnection_http
*httpc
)
3018 return httpc
->common
.QOS
&&
3019 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
3020 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
3023 static RPC_STATUS
set_auth_cookie(RpcConnection_http
*httpc
, const WCHAR
*value
)
3025 static WCHAR httpW
[] = {'h','t','t','p',0};
3026 static WCHAR httpsW
[] = {'h','t','t','p','s',0};
3032 if (!value
) return RPC_S_OK
;
3034 uc
.dwStructSize
= sizeof(uc
);
3035 uc
.lpszScheme
= is_secure(httpc
) ? httpsW
: httpW
;
3036 uc
.dwSchemeLength
= 0;
3037 uc
.lpszHostName
= httpc
->servername
;
3038 uc
.dwHostNameLength
= 0;
3040 uc
.lpszUserName
= NULL
;
3041 uc
.dwUserNameLength
= 0;
3042 uc
.lpszPassword
= NULL
;
3043 uc
.dwPasswordLength
= 0;
3044 uc
.lpszUrlPath
= NULL
;
3045 uc
.dwUrlPathLength
= 0;
3046 uc
.lpszExtraInfo
= NULL
;
3047 uc
.dwExtraInfoLength
= 0;
3049 if (!InternetCreateUrlW(&uc
, 0, NULL
, &len
) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
3050 return RPC_S_SERVER_UNAVAILABLE
;
3052 if (!(url
= HeapAlloc(GetProcessHeap(), 0, len
))) return RPC_S_OUT_OF_MEMORY
;
3054 len
= len
/ sizeof(WCHAR
) - 1;
3055 if (!InternetCreateUrlW(&uc
, 0, url
, &len
))
3057 HeapFree(GetProcessHeap(), 0, url
);
3058 return RPC_S_SERVER_UNAVAILABLE
;
3061 ret
= InternetSetCookieW(url
, NULL
, value
);
3062 HeapFree(GetProcessHeap(), 0, url
);
3063 if (!ret
) return RPC_S_SERVER_UNAVAILABLE
;
3068 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
3070 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
3071 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
3072 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
3073 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
3074 static const WCHAR wszColon
[] = {':',0};
3075 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
3076 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
3080 BOOL secure
, credentials
;
3081 HttpTimerThreadData
*timer_data
;
3084 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
3086 if (Connection
->server
)
3088 ERR("ncacn_http servers not supported yet\n");
3089 return RPC_S_SERVER_UNAVAILABLE
;
3092 if (httpc
->in_request
)
3095 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3097 UuidCreate(&httpc
->connection_uuid
);
3098 UuidCreate(&httpc
->in_pipe_uuid
);
3099 UuidCreate(&httpc
->out_pipe_uuid
);
3101 status
= rpcrt4_http_internet_connect(httpc
);
3102 if (status
!= RPC_S_OK
)
3105 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
3107 return RPC_S_OUT_OF_MEMORY
;
3108 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
3109 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
3110 strcatW(url
, wszColon
);
3111 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
3113 secure
= is_secure(httpc
);
3114 credentials
= has_credentials(httpc
);
3116 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
3117 INTERNET_FLAG_NO_AUTO_REDIRECT
;
3118 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
3119 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
3121 status
= set_auth_cookie(httpc
, Connection
->CookieAuth
);
3122 if (status
!= RPC_S_OK
)
3124 HeapFree(GetProcessHeap(), 0, url
);
3127 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
3128 flags
, (DWORD_PTR
)httpc
->async_data
);
3129 if (!httpc
->in_request
)
3131 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
3132 HeapFree(GetProcessHeap(), 0, url
);
3133 return RPC_S_SERVER_UNAVAILABLE
;
3138 status
= authorize_request(httpc
, httpc
->in_request
);
3139 if (status
!= RPC_S_OK
)
3141 HeapFree(GetProcessHeap(), 0, url
);
3144 status
= rpcrt4_http_check_response(httpc
->in_request
);
3145 if (status
!= RPC_S_OK
)
3147 HeapFree(GetProcessHeap(), 0, url
);
3150 drain_content(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
);
3153 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
3154 flags
, (DWORD_PTR
)httpc
->async_data
);
3155 HeapFree(GetProcessHeap(), 0, url
);
3156 if (!httpc
->out_request
)
3158 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
3159 return RPC_S_SERVER_UNAVAILABLE
;
3164 status
= authorize_request(httpc
, httpc
->out_request
);
3165 if (status
!= RPC_S_OK
)
3169 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
,
3170 &httpc
->connection_uuid
, &httpc
->in_pipe_uuid
,
3171 &Connection
->assoc
->http_uuid
, credentials
);
3172 if (status
!= RPC_S_OK
)
3175 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
,
3176 &httpc
->connection_uuid
, &httpc
->out_pipe_uuid
,
3177 &httpc
->flow_control_increment
, credentials
);
3178 if (status
!= RPC_S_OK
)
3181 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
3182 httpc
->last_sent_time
= GetTickCount();
3183 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3185 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
3187 return ERROR_OUTOFMEMORY
;
3188 timer_data
->timer_param
= httpc
->in_request
;
3189 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
3190 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
3191 /* FIXME: should use CreateTimerQueueTimer when implemented */
3192 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
3195 HeapFree(GetProcessHeap(), 0, timer_data
);
3196 return GetLastError();
3198 CloseHandle(thread
);
3203 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
3206 return RPC_S_SERVER_UNAVAILABLE
;
3209 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
3210 void *buffer
, unsigned int count
)
3212 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3213 return rpcrt4_http_async_read(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
, buffer
, count
);
3216 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
3218 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3222 RpcPktCommonHdr common_hdr
;
3226 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
3229 /* read packet common header */
3230 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
3231 if (dwRead
!= sizeof(common_hdr
)) {
3232 WARN("Short read of header, %d bytes\n", dwRead
);
3233 status
= RPC_S_PROTOCOL_ERROR
;
3236 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
3237 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
3239 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
3240 status
= RPC_S_PROTOCOL_ERROR
;
3244 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
3245 if (status
!= RPC_S_OK
) goto fail
;
3247 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
3248 if (hdr_length
== 0) {
3249 WARN("header length == 0\n");
3250 status
= RPC_S_PROTOCOL_ERROR
;
3254 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
3257 status
= RPC_S_OUT_OF_RESOURCES
;
3260 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
3262 /* read the rest of packet header */
3263 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
3264 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
3265 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
3266 status
= RPC_S_PROTOCOL_ERROR
;
3270 if (common_hdr
.frag_len
- hdr_length
)
3272 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
3275 status
= RPC_S_OUT_OF_RESOURCES
;
3279 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
3280 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
3282 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
3283 status
= RPC_S_PROTOCOL_ERROR
;
3290 if ((*Header
)->common
.ptype
== PKT_HTTP
)
3292 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
3294 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
3295 status
= RPC_S_PROTOCOL_ERROR
;
3298 if ((*Header
)->http
.flags
== 0x0001)
3300 TRACE("http idle packet, waiting for real packet\n");
3301 if ((*Header
)->http
.num_data_items
!= 0)
3303 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
3304 status
= RPC_S_PROTOCOL_ERROR
;
3308 else if ((*Header
)->http
.flags
== 0x0002)
3310 ULONG bytes_transmitted
;
3311 ULONG flow_control_increment
;
3313 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
3316 &flow_control_increment
,
3318 if (status
!= RPC_S_OK
)
3320 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
3321 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
3322 /* FIXME: do something with parsed data */
3326 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
3327 status
= RPC_S_PROTOCOL_ERROR
;
3330 RPCRT4_FreeHeader(*Header
);
3332 HeapFree(GetProcessHeap(), 0, *Payload
);
3340 httpc
->bytes_received
+= common_hdr
.frag_len
;
3342 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
3344 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
3346 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
3347 httpc
->bytes_received
,
3348 httpc
->flow_control_increment
,
3349 &httpc
->out_pipe_uuid
);
3352 DWORD bytes_written
;
3354 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
3355 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
3356 RPCRT4_FreeHeader(hdr
);
3358 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
3363 if (status
!= RPC_S_OK
) {
3364 RPCRT4_FreeHeader(*Header
);
3366 HeapFree(GetProcessHeap(), 0, *Payload
);
3372 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
3373 const void *buffer
, unsigned int count
)
3375 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3376 DWORD bytes_written
;
3379 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
3380 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
3381 httpc
->last_sent_time
= GetTickCount();
3382 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
3383 return ret
? bytes_written
: -1;
3386 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
3388 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3392 SetEvent(httpc
->timer_cancelled
);
3393 if (httpc
->in_request
)
3394 InternetCloseHandle(httpc
->in_request
);
3395 httpc
->in_request
= NULL
;
3396 if (httpc
->out_request
)
3397 InternetCloseHandle(httpc
->out_request
);
3398 httpc
->out_request
= NULL
;
3399 if (httpc
->app_info
)
3400 InternetCloseHandle(httpc
->app_info
);
3401 httpc
->app_info
= NULL
;
3403 InternetCloseHandle(httpc
->session
);
3404 httpc
->session
= NULL
;
3405 RpcHttpAsyncData_Release(httpc
->async_data
);
3406 if (httpc
->cancel_event
)
3407 CloseHandle(httpc
->cancel_event
);
3408 HeapFree(GetProcessHeap(), 0, httpc
->servername
);
3409 httpc
->servername
= NULL
;
3414 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
3416 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3418 SetEvent(httpc
->cancel_event
);
3421 static RPC_STATUS
rpcrt4_ncacn_http_is_server_listening(const char *endpoint
)
3424 return RPC_S_ACCESS_DENIED
;
3427 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
3429 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3433 prepare_async_request(httpc
->async_data
);
3434 ret
= InternetQueryDataAvailable(httpc
->out_request
,
3435 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
3436 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3437 return status
== RPC_S_OK
? 0 : -1;
3440 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
3441 const char *networkaddr
,
3442 const char *endpoint
)
3444 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
3445 EPM_PROTOCOL_HTTP
, endpoint
);
3448 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
3453 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
3454 networkaddr
, EPM_PROTOCOL_HTTP
,
3458 static const struct connection_ops conn_protseq_list
[] = {
3460 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
3461 rpcrt4_conn_np_alloc
,
3462 rpcrt4_ncacn_np_open
,
3463 rpcrt4_ncacn_np_handoff
,
3464 rpcrt4_conn_np_read
,
3465 rpcrt4_conn_np_write
,
3466 rpcrt4_conn_np_close
,
3467 rpcrt4_conn_np_cancel_call
,
3468 rpcrt4_ncacn_np_is_server_listening
,
3469 rpcrt4_conn_np_wait_for_incoming_data
,
3470 rpcrt4_ncacn_np_get_top_of_tower
,
3471 rpcrt4_ncacn_np_parse_top_of_tower
,
3473 RPCRT4_default_is_authorized
,
3474 RPCRT4_default_authorize
,
3475 RPCRT4_default_secure_packet
,
3476 rpcrt4_conn_np_impersonate_client
,
3477 rpcrt4_conn_np_revert_to_self
,
3478 RPCRT4_default_inquire_auth_client
,
3481 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
3482 rpcrt4_conn_np_alloc
,
3483 rpcrt4_ncalrpc_open
,
3484 rpcrt4_ncalrpc_handoff
,
3485 rpcrt4_conn_np_read
,
3486 rpcrt4_conn_np_write
,
3487 rpcrt4_conn_np_close
,
3488 rpcrt4_conn_np_cancel_call
,
3489 rpcrt4_ncalrpc_np_is_server_listening
,
3490 rpcrt4_conn_np_wait_for_incoming_data
,
3491 rpcrt4_ncalrpc_get_top_of_tower
,
3492 rpcrt4_ncalrpc_parse_top_of_tower
,
3494 rpcrt4_ncalrpc_is_authorized
,
3495 rpcrt4_ncalrpc_authorize
,
3496 rpcrt4_ncalrpc_secure_packet
,
3497 rpcrt4_conn_np_impersonate_client
,
3498 rpcrt4_conn_np_revert_to_self
,
3499 rpcrt4_ncalrpc_inquire_auth_client
,
3502 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
3503 rpcrt4_conn_tcp_alloc
,
3504 rpcrt4_ncacn_ip_tcp_open
,
3505 rpcrt4_conn_tcp_handoff
,
3506 rpcrt4_conn_tcp_read
,
3507 rpcrt4_conn_tcp_write
,
3508 rpcrt4_conn_tcp_close
,
3509 rpcrt4_conn_tcp_cancel_call
,
3510 rpcrt4_conn_tcp_is_server_listening
,
3511 rpcrt4_conn_tcp_wait_for_incoming_data
,
3512 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
3513 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
3515 RPCRT4_default_is_authorized
,
3516 RPCRT4_default_authorize
,
3517 RPCRT4_default_secure_packet
,
3518 RPCRT4_default_impersonate_client
,
3519 RPCRT4_default_revert_to_self
,
3520 RPCRT4_default_inquire_auth_client
,
3523 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
3524 rpcrt4_ncacn_http_alloc
,
3525 rpcrt4_ncacn_http_open
,
3526 rpcrt4_ncacn_http_handoff
,
3527 rpcrt4_ncacn_http_read
,
3528 rpcrt4_ncacn_http_write
,
3529 rpcrt4_ncacn_http_close
,
3530 rpcrt4_ncacn_http_cancel_call
,
3531 rpcrt4_ncacn_http_is_server_listening
,
3532 rpcrt4_ncacn_http_wait_for_incoming_data
,
3533 rpcrt4_ncacn_http_get_top_of_tower
,
3534 rpcrt4_ncacn_http_parse_top_of_tower
,
3535 rpcrt4_ncacn_http_receive_fragment
,
3536 RPCRT4_default_is_authorized
,
3537 RPCRT4_default_authorize
,
3538 RPCRT4_default_secure_packet
,
3539 RPCRT4_default_impersonate_client
,
3540 RPCRT4_default_revert_to_self
,
3541 RPCRT4_default_inquire_auth_client
,
3546 static const struct protseq_ops protseq_list
[] =
3550 rpcrt4_protseq_np_alloc
,
3551 rpcrt4_protseq_np_signal_state_changed
,
3552 rpcrt4_protseq_np_get_wait_array
,
3553 rpcrt4_protseq_np_free_wait_array
,
3554 rpcrt4_protseq_np_wait_for_new_connection
,
3555 rpcrt4_protseq_ncacn_np_open_endpoint
,
3559 rpcrt4_protseq_np_alloc
,
3560 rpcrt4_protseq_np_signal_state_changed
,
3561 rpcrt4_protseq_np_get_wait_array
,
3562 rpcrt4_protseq_np_free_wait_array
,
3563 rpcrt4_protseq_np_wait_for_new_connection
,
3564 rpcrt4_protseq_ncalrpc_open_endpoint
,
3568 rpcrt4_protseq_sock_alloc
,
3569 rpcrt4_protseq_sock_signal_state_changed
,
3570 rpcrt4_protseq_sock_get_wait_array
,
3571 rpcrt4_protseq_sock_free_wait_array
,
3572 rpcrt4_protseq_sock_wait_for_new_connection
,
3573 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
3577 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3580 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3581 if (!strcmp(protseq_list
[i
].name
, protseq
))
3582 return &protseq_list
[i
];
3586 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3589 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3590 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3591 return &conn_protseq_list
[i
];
3595 /**** interface to rest of code ****/
3597 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3599 TRACE("(Connection == ^%p)\n", Connection
);
3601 assert(!Connection
->server
);
3602 return Connection
->ops
->open_connection_client(Connection
);
3605 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3607 TRACE("(Connection == ^%p)\n", Connection
);
3608 if (SecIsValidHandle(&Connection
->ctx
))
3610 DeleteSecurityContext(&Connection
->ctx
);
3611 SecInvalidateHandle(&Connection
->ctx
);
3613 rpcrt4_conn_close(Connection
);
3617 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3618 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3619 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3621 static LONG next_id
;
3622 const struct connection_ops
*ops
;
3623 RpcConnection
* NewConnection
;
3625 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3628 FIXME("not supported for protseq %s\n", Protseq
);
3629 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3632 NewConnection
= ops
->alloc();
3633 NewConnection
->ref
= 1;
3634 NewConnection
->Next
= NULL
;
3635 NewConnection
->server_binding
= NULL
;
3636 NewConnection
->server
= server
;
3637 NewConnection
->ops
= ops
;
3638 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3639 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3640 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3641 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3642 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3643 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
3644 NewConnection
->NextCallId
= 1;
3646 SecInvalidateHandle(&NewConnection
->ctx
);
3647 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
3648 NewConnection
->attr
= 0;
3649 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3650 NewConnection
->AuthInfo
= AuthInfo
;
3651 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3652 NewConnection
->encryption_auth_len
= 0;
3653 NewConnection
->signature_auth_len
= 0;
3654 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3655 NewConnection
->QOS
= QOS
;
3657 list_init(&NewConnection
->conn_pool_entry
);
3658 NewConnection
->async_state
= NULL
;
3660 TRACE("connection: %p\n", NewConnection
);
3661 *Connection
= NewConnection
;
3666 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3670 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
, rpcrt4_conn_get_name(OldConnection
),
3671 OldConnection
->NetworkAddr
, OldConnection
->Endpoint
, NULL
,
3672 OldConnection
->AuthInfo
, OldConnection
->QOS
, OldConnection
->CookieAuth
);
3673 if (err
== RPC_S_OK
)
3674 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3678 RpcConnection
*RPCRT4_GrabConnection( RpcConnection
*conn
)
3680 InterlockedIncrement( &conn
->ref
);
3684 RPC_STATUS
RPCRT4_ReleaseConnection(RpcConnection
* Connection
)
3686 if (InterlockedDecrement( &Connection
->ref
) > 0) return RPC_S_OK
;
3688 TRACE("destroying connection %p\n", Connection
);
3690 RPCRT4_CloseConnection(Connection
);
3691 RPCRT4_strfree(Connection
->Endpoint
);
3692 RPCRT4_strfree(Connection
->NetworkAddr
);
3693 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3694 HeapFree(GetProcessHeap(), 0, Connection
->CookieAuth
);
3695 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3696 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3699 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3701 HeapFree(GetProcessHeap(), 0, Connection
);
3705 RPC_STATUS
RPCRT4_IsServerListening(const char *protseq
, const char *endpoint
)
3707 const struct connection_ops
*ops
;
3709 ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3712 FIXME("not supported for protseq %s\n", protseq
);
3713 return RPC_S_INVALID_BINDING
;
3716 return ops
->is_server_listening(endpoint
);
3719 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3721 const char *protseq
,
3722 const char *networkaddr
,
3723 const char *endpoint
)
3725 twr_empty_floor_t
*protocol_floor
;
3726 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3731 return RPC_S_INVALID_RPC_PROTSEQ
;
3735 *tower_size
= sizeof(*protocol_floor
);
3736 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3740 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3741 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3742 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3743 protocol_floor
->count_rhs
= 0;
3745 tower_data
+= sizeof(*protocol_floor
);
3747 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3749 return EPT_S_NOT_REGISTERED
;
3751 *tower_size
+= sizeof(*protocol_floor
);
3756 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3762 const twr_empty_floor_t
*protocol_floor
;
3763 const twr_empty_floor_t
*floor4
;
3764 const struct connection_ops
*protseq_ops
= NULL
;
3768 if (tower_size
< sizeof(*protocol_floor
))
3769 return EPT_S_NOT_REGISTERED
;
3771 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3772 tower_data
+= sizeof(*protocol_floor
);
3773 tower_size
-= sizeof(*protocol_floor
);
3774 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3775 (protocol_floor
->count_rhs
> tower_size
))
3776 return EPT_S_NOT_REGISTERED
;
3777 tower_data
+= protocol_floor
->count_rhs
;
3778 tower_size
-= protocol_floor
->count_rhs
;
3780 floor4
= (const twr_empty_floor_t
*)tower_data
;
3781 if ((tower_size
< sizeof(*floor4
)) ||
3782 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3783 return EPT_S_NOT_REGISTERED
;
3785 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3786 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3787 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3789 protseq_ops
= &conn_protseq_list
[i
];
3794 return EPT_S_NOT_REGISTERED
;
3796 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3798 if ((status
== RPC_S_OK
) && protseq
)
3800 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3801 strcpy(*protseq
, protseq_ops
->name
);
3807 /***********************************************************************
3808 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3810 * Checks if the given protocol sequence is known by the RPC system.
3811 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3814 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3818 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3819 ps
, sizeof ps
, NULL
, NULL
);
3820 if (rpcrt4_get_conn_protseq_ops(ps
))
3823 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3825 return RPC_S_INVALID_RPC_PROTSEQ
;
3828 /***********************************************************************
3829 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3831 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3833 UNICODE_STRING protseqW
;
3835 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3837 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3838 RtlFreeUnicodeString(&protseqW
);
3841 return RPC_S_OUT_OF_MEMORY
;
3844 /***********************************************************************
3845 * RpcProtseqVectorFreeA (RPCRT4.@)
3847 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3849 TRACE("(%p)\n", protseqs
);
3854 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3855 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3856 HeapFree(GetProcessHeap(), 0, *protseqs
);
3862 /***********************************************************************
3863 * RpcProtseqVectorFreeW (RPCRT4.@)
3865 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3867 TRACE("(%p)\n", protseqs
);
3872 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3873 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3874 HeapFree(GetProcessHeap(), 0, *protseqs
);
3880 /***********************************************************************
3881 * RpcNetworkInqProtseqsW (RPCRT4.@)
3883 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3885 RPC_PROTSEQ_VECTORW
*pvector
;
3887 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3889 TRACE("(%p)\n", protseqs
);
3891 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3894 pvector
= *protseqs
;
3896 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3898 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3899 if (pvector
->Protseq
[i
] == NULL
)
3901 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3902 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3908 if (status
!= RPC_S_OK
)
3909 RpcProtseqVectorFreeW(protseqs
);
3913 /***********************************************************************
3914 * RpcNetworkInqProtseqsA (RPCRT4.@)
3916 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3918 RPC_PROTSEQ_VECTORA
*pvector
;
3920 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3922 TRACE("(%p)\n", protseqs
);
3924 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3927 pvector
= *protseqs
;
3929 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3931 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3932 if (pvector
->Protseq
[i
] == NULL
)
3934 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3940 if (status
!= RPC_S_OK
)
3941 RpcProtseqVectorFreeA(protseqs
);