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
34 #include <sys/types.h>
36 #if defined(__MINGW32__) || defined (_MSC_VER)
37 # include <ws2tcpip.h>
39 # define EADDRINUSE WSAEADDRINUSE
42 # define EAGAIN WSAEWOULDBLOCK
45 # define errno WSAGetLastError()
52 # ifdef HAVE_SYS_SOCKET_H
53 # include <sys/socket.h>
55 # ifdef HAVE_NETINET_IN_H
56 # include <netinet/in.h>
58 # ifdef HAVE_NETINET_TCP_H
59 # include <netinet/tcp.h>
61 # ifdef HAVE_ARPA_INET_H
62 # include <arpa/inet.h>
67 # ifdef HAVE_SYS_POLL_H
68 # include <sys/poll.h>
70 # ifdef HAVE_SYS_FILIO_H
71 # include <sys/filio.h>
73 # ifdef HAVE_SYS_IOCTL_H
74 # include <sys/ioctl.h>
76 # define closesocket close
77 # define ioctlsocket ioctl
78 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
86 #include "wine/unicode.h"
91 #include "wine/debug.h"
93 #include "rpc_binding.h"
94 #include "rpc_assoc.h"
95 #include "rpc_message.h"
96 #include "rpc_server.h"
97 #include "epm_towers.h"
100 # define SOL_TCP IPPROTO_TCP
103 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
105 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
107 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
109 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
111 /**** ncacn_np support ****/
113 typedef struct _RpcConnection_np
115 RpcConnection common
;
117 HANDLE listen_thread
;
121 static RpcConnection
*rpcrt4_conn_np_alloc(void)
123 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
127 static DWORD CALLBACK
listen_thread(void *arg
)
129 RpcConnection_np
*npc
= arg
;
132 if (ConnectNamedPipe(npc
->pipe
, NULL
))
135 switch(GetLastError())
137 case ERROR_PIPE_CONNECTED
:
139 case ERROR_HANDLES_CLOSED
:
140 /* connection closed during listen */
141 return RPC_S_NO_CONTEXT_AVAILABLE
;
142 case ERROR_NO_DATA_DETECTED
:
143 /* client has disconnected, retry */
144 DisconnectNamedPipe( npc
->pipe
);
147 npc
->listening
= FALSE
;
148 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
149 return RPC_S_OUT_OF_RESOURCES
;
154 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
159 npc
->listening
= TRUE
;
160 npc
->listen_thread
= CreateThread(NULL
, 0, listen_thread
, npc
, 0, NULL
);
161 if (!npc
->listen_thread
)
163 npc
->listening
= FALSE
;
164 ERR("Couldn't create listen thread (error was %d)\n", GetLastError());
165 return RPC_S_OUT_OF_RESOURCES
;
170 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
172 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
173 TRACE("listening on %s\n", pname
);
175 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
176 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
177 PIPE_UNLIMITED_INSTANCES
,
178 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
179 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
180 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
181 if (GetLastError() == ERROR_FILE_EXISTS
)
182 return RPC_S_DUPLICATE_ENDPOINT
;
184 return RPC_S_CANT_CREATE_ENDPOINT
;
187 /* Note: we don't call ConnectNamedPipe here because it must be done in the
188 * server thread as the thread must be alertable */
192 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
194 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
198 TRACE("connecting to %s\n", pname
);
204 dwFlags
= SECURITY_SQOS_PRESENT
;
205 switch (Connection
->QOS
->qos
->ImpersonationType
)
207 case RPC_C_IMP_LEVEL_DEFAULT
:
208 /* FIXME: what to do here? */
210 case RPC_C_IMP_LEVEL_ANONYMOUS
:
211 dwFlags
|= SECURITY_ANONYMOUS
;
213 case RPC_C_IMP_LEVEL_IDENTIFY
:
214 dwFlags
|= SECURITY_IDENTIFICATION
;
216 case RPC_C_IMP_LEVEL_IMPERSONATE
:
217 dwFlags
|= SECURITY_IMPERSONATION
;
219 case RPC_C_IMP_LEVEL_DELEGATE
:
220 dwFlags
|= SECURITY_DELEGATION
;
223 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
224 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
226 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
227 OPEN_EXISTING
, dwFlags
, 0);
228 if (pipe
!= INVALID_HANDLE_VALUE
) break;
229 err
= GetLastError();
230 if (err
== ERROR_PIPE_BUSY
) {
231 TRACE("connection failed, error=%x\n", err
);
232 return RPC_S_SERVER_TOO_BUSY
;
234 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
235 err
= GetLastError();
236 WARN("connection failed, error=%x\n", err
);
237 return RPC_S_SERVER_UNAVAILABLE
;
242 /* pipe is connected; change to message-read mode. */
243 dwMode
= PIPE_READMODE_MESSAGE
;
244 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
250 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
252 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
253 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
257 /* already connected? */
261 /* protseq=ncalrpc: supposed to use NT LPC ports,
262 * but we'll implement it with named pipes for now */
263 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
264 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
265 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
271 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
273 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
276 RpcConnection
*Connection
;
277 char generated_endpoint
[22];
281 static LONG lrpc_nameless_id
;
282 DWORD process_id
= GetCurrentProcessId();
283 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
284 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
285 "LRPC%08x.%08x", process_id
, id
);
286 endpoint
= generated_endpoint
;
289 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
290 endpoint
, NULL
, NULL
, NULL
, NULL
);
294 /* protseq=ncalrpc: supposed to use NT LPC ports,
295 * but we'll implement it with named pipes for now */
296 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
297 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
298 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
301 EnterCriticalSection(&protseq
->cs
);
302 Connection
->Next
= protseq
->conn
;
303 protseq
->conn
= Connection
;
304 LeaveCriticalSection(&protseq
->cs
);
309 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
311 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
312 static const char prefix
[] = "\\\\.";
316 /* already connected? */
320 /* protseq=ncacn_np: named pipes */
321 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
322 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
323 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
329 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
331 static const char prefix
[] = "\\\\.";
334 RpcConnection
*Connection
;
335 char generated_endpoint
[21];
339 static LONG np_nameless_id
;
340 DWORD process_id
= GetCurrentProcessId();
341 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
342 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
343 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
344 endpoint
= generated_endpoint
;
347 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
348 endpoint
, NULL
, NULL
, NULL
, NULL
);
352 /* protseq=ncacn_np: named pipes */
353 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
354 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
355 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
358 EnterCriticalSection(&protseq
->cs
);
359 Connection
->Next
= protseq
->conn
;
360 protseq
->conn
= Connection
;
361 LeaveCriticalSection(&protseq
->cs
);
366 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
368 /* because of the way named pipes work, we'll transfer the connected pipe
369 * to the child, then reopen the server binding to continue listening */
371 new_npc
->pipe
= old_npc
->pipe
;
372 new_npc
->listen_thread
= old_npc
->listen_thread
;
374 old_npc
->listen_thread
= 0;
375 old_npc
->listening
= FALSE
;
378 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
382 static const char prefix
[] = "\\\\.";
384 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
386 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
387 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
388 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
394 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
398 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
400 TRACE("%s\n", old_conn
->Endpoint
);
402 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
404 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
405 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
406 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
412 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
413 void *buffer
, unsigned int count
)
415 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
418 unsigned int bytes_left
= count
;
423 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
424 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
426 if (!ret
|| !bytes_read
)
428 bytes_left
-= bytes_read
;
431 return ret
? count
: -1;
434 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
435 const void *buffer
, unsigned int count
)
437 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
438 const char *buf
= buffer
;
440 unsigned int bytes_left
= count
;
445 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
446 if (!ret
|| !bytes_written
)
448 bytes_left
-= bytes_written
;
449 buf
+= bytes_written
;
451 return ret
? count
: -1;
454 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
456 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
458 FlushFileBuffers(npc
->pipe
);
459 CloseHandle(npc
->pipe
);
462 if (npc
->listen_thread
) {
463 CloseHandle(npc
->listen_thread
);
464 npc
->listen_thread
= 0;
469 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
471 /* FIXME: implement when named pipe writes use overlapped I/O */
474 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
476 /* FIXME: implement when named pipe writes use overlapped I/O */
480 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
481 const char *networkaddr
,
482 const char *endpoint
)
484 twr_empty_floor_t
*smb_floor
;
485 twr_empty_floor_t
*nb_floor
;
487 size_t networkaddr_size
;
488 size_t endpoint_size
;
490 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
492 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
493 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
494 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
499 smb_floor
= (twr_empty_floor_t
*)tower_data
;
501 tower_data
+= sizeof(*smb_floor
);
503 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
504 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
505 smb_floor
->count_rhs
= endpoint_size
;
508 memcpy(tower_data
, endpoint
, endpoint_size
);
511 tower_data
+= endpoint_size
;
513 nb_floor
= (twr_empty_floor_t
*)tower_data
;
515 tower_data
+= sizeof(*nb_floor
);
517 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
518 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
519 nb_floor
->count_rhs
= networkaddr_size
;
522 memcpy(tower_data
, networkaddr
, networkaddr_size
);
529 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
534 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
535 const twr_empty_floor_t
*nb_floor
;
537 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
539 if (tower_size
< sizeof(*smb_floor
))
540 return EPT_S_NOT_REGISTERED
;
542 tower_data
+= sizeof(*smb_floor
);
543 tower_size
-= sizeof(*smb_floor
);
545 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
546 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
547 (smb_floor
->count_rhs
> tower_size
) ||
548 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
549 return EPT_S_NOT_REGISTERED
;
553 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
555 return RPC_S_OUT_OF_RESOURCES
;
556 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
558 tower_data
+= smb_floor
->count_rhs
;
559 tower_size
-= smb_floor
->count_rhs
;
561 if (tower_size
< sizeof(*nb_floor
))
562 return EPT_S_NOT_REGISTERED
;
564 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
566 tower_data
+= sizeof(*nb_floor
);
567 tower_size
-= sizeof(*nb_floor
);
569 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
570 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
571 (nb_floor
->count_rhs
> tower_size
) ||
572 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
573 return EPT_S_NOT_REGISTERED
;
577 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
582 I_RpcFree(*endpoint
);
585 return RPC_S_OUT_OF_RESOURCES
;
587 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
593 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
595 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
598 TRACE("(%p)\n", conn
);
600 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
601 return RPCRT4_default_impersonate_client(conn
);
603 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
606 DWORD error
= GetLastError();
607 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
610 case ERROR_CANNOT_IMPERSONATE
:
611 return RPC_S_NO_CONTEXT_AVAILABLE
;
617 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
621 TRACE("(%p)\n", conn
);
623 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
624 return RPCRT4_default_revert_to_self(conn
);
626 ret
= RevertToSelf();
629 WARN("RevertToSelf failed with error %u\n", GetLastError());
630 return RPC_S_NO_CONTEXT_AVAILABLE
;
635 typedef struct _RpcServerProtseq_np
637 RpcServerProtseq common
;
639 } RpcServerProtseq_np
;
641 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
643 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
645 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
649 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
651 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
652 SetEvent(npps
->mgr_event
);
655 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
657 HANDLE
*objs
= prev_array
;
658 RpcConnection_np
*conn
;
659 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
661 EnterCriticalSection(&protseq
->cs
);
663 /* open and count connections */
665 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
667 rpcrt4_conn_listen_pipe(conn
);
668 if (conn
->listen_thread
)
670 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
673 /* make array of connections */
675 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
677 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
680 ERR("couldn't allocate objs\n");
681 LeaveCriticalSection(&protseq
->cs
);
685 objs
[0] = npps
->mgr_event
;
687 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
689 if ((objs
[*count
] = conn
->listen_thread
))
691 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
693 LeaveCriticalSection(&protseq
->cs
);
697 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
699 HeapFree(GetProcessHeap(), 0, array
);
702 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
705 HANDLE
*objs
= wait_array
;
707 RpcConnection
*cconn
;
708 RpcConnection_np
*conn
;
715 /* an alertable wait isn't strictly necessary, but due to our
716 * overlapped I/O implementation in Wine we need to free some memory
717 * by the file user APC being called, even if no completion routine was
718 * specified at the time of starting the async operation */
719 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
720 } while (res
== WAIT_IO_COMPLETION
);
722 if (res
== WAIT_OBJECT_0
)
724 else if (res
== WAIT_FAILED
)
726 ERR("wait failed with error %d\n", GetLastError());
731 b_handle
= objs
[res
- WAIT_OBJECT_0
];
732 /* find which connection got a RPC */
733 EnterCriticalSection(&protseq
->cs
);
734 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
736 if (b_handle
== conn
->listen_thread
) break;
737 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
743 if (GetExitCodeThread(conn
->listen_thread
, &exit_code
) && exit_code
== RPC_S_OK
)
744 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
745 CloseHandle(conn
->listen_thread
);
746 conn
->listen_thread
= 0;
749 ERR("failed to locate connection for handle %p\n", b_handle
);
750 LeaveCriticalSection(&protseq
->cs
);
753 RPCRT4_new_client(cconn
);
760 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
761 const char *networkaddr
,
762 const char *endpoint
)
764 twr_empty_floor_t
*pipe_floor
;
766 size_t endpoint_size
;
768 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
770 endpoint_size
= strlen(endpoint
) + 1;
771 size
= sizeof(*pipe_floor
) + endpoint_size
;
776 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
778 tower_data
+= sizeof(*pipe_floor
);
780 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
781 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
782 pipe_floor
->count_rhs
= endpoint_size
;
784 memcpy(tower_data
, endpoint
, endpoint_size
);
789 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
794 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
796 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
798 if (tower_size
< sizeof(*pipe_floor
))
799 return EPT_S_NOT_REGISTERED
;
801 tower_data
+= sizeof(*pipe_floor
);
802 tower_size
-= sizeof(*pipe_floor
);
804 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
805 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
806 (pipe_floor
->count_rhs
> tower_size
) ||
807 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
808 return EPT_S_NOT_REGISTERED
;
815 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
817 return RPC_S_OUT_OF_RESOURCES
;
818 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
824 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
829 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
830 unsigned char *in_buffer
,
831 unsigned int in_size
,
832 unsigned char *out_buffer
,
833 unsigned int *out_size
)
835 /* since this protocol is local to the machine there is no need to
836 * authenticate the caller */
841 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
842 enum secure_packet_direction dir
,
843 RpcPktHdr
*hdr
, unsigned int hdr_size
,
844 unsigned char *stub_data
, unsigned int stub_data_size
,
845 RpcAuthVerifier
*auth_hdr
,
846 unsigned char *auth_value
, unsigned int auth_value_size
)
848 /* since this protocol is local to the machine there is no need to secure
853 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
854 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
855 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
857 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
858 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
862 FIXME("privs not implemented\n");
865 if (server_princ_name
)
867 FIXME("server_princ_name not implemented\n");
868 *server_princ_name
= NULL
;
870 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
871 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
874 FIXME("authorization service not implemented\n");
875 *authz_svc
= RPC_C_AUTHZ_NONE
;
878 FIXME("flags 0x%x not implemented\n", flags
);
883 /**** ncacn_ip_tcp support ****/
885 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
886 const char *networkaddr
,
887 unsigned char tcp_protid
,
888 const char *endpoint
)
890 twr_tcp_floor_t
*tcp_floor
;
891 twr_ipv4_floor_t
*ipv4_floor
;
893 struct addrinfo hints
;
895 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
897 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
902 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
903 tower_data
+= sizeof(*tcp_floor
);
905 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
907 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
908 tcp_floor
->protid
= tcp_protid
;
909 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
911 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
912 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
913 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
915 hints
.ai_flags
= AI_NUMERICHOST
;
916 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
917 hints
.ai_family
= PF_INET
;
918 hints
.ai_socktype
= SOCK_STREAM
;
919 hints
.ai_protocol
= IPPROTO_TCP
;
920 hints
.ai_addrlen
= 0;
921 hints
.ai_addr
= NULL
;
922 hints
.ai_canonname
= NULL
;
923 hints
.ai_next
= NULL
;
925 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
928 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
931 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
936 if (ai
->ai_family
== PF_INET
)
938 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
939 tcp_floor
->port
= sin
->sin_port
;
940 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
944 ERR("unexpected protocol family %d\n", ai
->ai_family
);
954 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
957 unsigned char tcp_protid
,
960 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
961 const twr_ipv4_floor_t
*ipv4_floor
;
962 struct in_addr in_addr
;
964 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
966 if (tower_size
< sizeof(*tcp_floor
))
967 return EPT_S_NOT_REGISTERED
;
969 tower_data
+= sizeof(*tcp_floor
);
970 tower_size
-= sizeof(*tcp_floor
);
972 if (tower_size
< sizeof(*ipv4_floor
))
973 return EPT_S_NOT_REGISTERED
;
975 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
977 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
978 (tcp_floor
->protid
!= tcp_protid
) ||
979 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
980 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
981 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
982 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
983 return EPT_S_NOT_REGISTERED
;
987 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
989 return RPC_S_OUT_OF_RESOURCES
;
990 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
995 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1000 I_RpcFree(*endpoint
);
1003 return RPC_S_OUT_OF_RESOURCES
;
1005 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1006 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1008 ERR("inet_ntop: %s\n", strerror(errno
));
1009 I_RpcFree(*networkaddr
);
1010 *networkaddr
= NULL
;
1013 I_RpcFree(*endpoint
);
1016 return EPT_S_NOT_REGISTERED
;
1023 typedef struct _RpcConnection_tcp
1025 RpcConnection common
;
1027 #ifdef HAVE_SOCKETPAIR
1031 HANDLE cancel_event
;
1033 } RpcConnection_tcp
;
1035 #ifdef HAVE_SOCKETPAIR
1037 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1039 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1041 ERR("socketpair() failed: %s\n", strerror(errno
));
1047 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1049 struct pollfd pfds
[2];
1050 pfds
[0].fd
= tcpc
->sock
;
1051 pfds
[0].events
= POLLIN
;
1052 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1053 pfds
[1].events
= POLLIN
;
1054 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1056 ERR("poll() failed: %s\n", strerror(errno
));
1059 if (pfds
[1].revents
& POLLIN
) /* canceled */
1062 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1068 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1071 pfd
.fd
= tcpc
->sock
;
1072 pfd
.events
= POLLOUT
;
1073 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1075 ERR("poll() failed: %s\n", strerror(errno
));
1081 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1085 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1088 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1090 close(tcpc
->cancel_fds
[0]);
1091 close(tcpc
->cancel_fds
[1]);
1094 #else /* HAVE_SOCKETPAIR */
1096 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1098 static BOOL wsa_inited
;
1102 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1103 /* Note: WSAStartup can be called more than once so we don't bother with
1104 * making accesses to wsa_inited thread-safe */
1107 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1108 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1109 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1111 ERR("event creation failed\n");
1112 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1118 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1120 HANDLE wait_handles
[2];
1122 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1124 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1127 wait_handles
[0] = tcpc
->sock_event
;
1128 wait_handles
[1] = tcpc
->cancel_event
;
1129 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1134 case WAIT_OBJECT_0
+ 1:
1137 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1142 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1145 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1147 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1150 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1156 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1161 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1163 SetEvent(tcpc
->cancel_event
);
1166 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1168 CloseHandle(tcpc
->sock_event
);
1169 CloseHandle(tcpc
->cancel_event
);
1174 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1176 RpcConnection_tcp
*tcpc
;
1177 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1181 if (!rpcrt4_sock_wait_init(tcpc
))
1183 HeapFree(GetProcessHeap(), 0, tcpc
);
1186 return &tcpc
->common
;
1189 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1191 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1194 struct addrinfo
*ai
;
1195 struct addrinfo
*ai_cur
;
1196 struct addrinfo hints
;
1198 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1200 if (tcpc
->sock
!= -1)
1204 hints
.ai_family
= PF_UNSPEC
;
1205 hints
.ai_socktype
= SOCK_STREAM
;
1206 hints
.ai_protocol
= IPPROTO_TCP
;
1207 hints
.ai_addrlen
= 0;
1208 hints
.ai_addr
= NULL
;
1209 hints
.ai_canonname
= NULL
;
1210 hints
.ai_next
= NULL
;
1212 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1215 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1216 Connection
->Endpoint
, gai_strerror(ret
));
1217 return RPC_S_SERVER_UNAVAILABLE
;
1220 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1225 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1227 TRACE("skipping non-IP/IPv6 address family\n");
1235 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1236 host
, sizeof(host
), service
, sizeof(service
),
1237 NI_NUMERICHOST
| NI_NUMERICSERV
);
1238 TRACE("trying %s:%s\n", host
, service
);
1241 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1244 WARN("socket() failed: %s\n", strerror(errno
));
1248 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1250 WARN("connect() failed: %s\n", strerror(errno
));
1255 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1257 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1259 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1264 TRACE("connected\n");
1269 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1270 return RPC_S_SERVER_UNAVAILABLE
;
1273 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1275 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1278 struct addrinfo
*ai
;
1279 struct addrinfo
*ai_cur
;
1280 struct addrinfo hints
;
1281 RpcConnection
*first_connection
= NULL
;
1283 TRACE("(%p, %s)\n", protseq
, endpoint
);
1285 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1286 hints
.ai_family
= PF_UNSPEC
;
1287 hints
.ai_socktype
= SOCK_STREAM
;
1288 hints
.ai_protocol
= IPPROTO_TCP
;
1289 hints
.ai_addrlen
= 0;
1290 hints
.ai_addr
= NULL
;
1291 hints
.ai_canonname
= NULL
;
1292 hints
.ai_next
= NULL
;
1294 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1297 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1299 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1300 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1301 return RPC_S_CANT_CREATE_ENDPOINT
;
1304 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1306 RpcConnection_tcp
*tcpc
;
1307 RPC_STATUS create_status
;
1308 struct sockaddr_storage sa
;
1310 char service
[NI_MAXSERV
];
1313 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1315 TRACE("skipping non-IP/IPv6 address family\n");
1322 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1323 host
, sizeof(host
), service
, sizeof(service
),
1324 NI_NUMERICHOST
| NI_NUMERICSERV
);
1325 TRACE("trying %s:%s\n", host
, service
);
1328 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1331 WARN("socket() failed: %s\n", strerror(errno
));
1332 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1336 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1339 WARN("bind failed: %s\n", strerror(errno
));
1341 if (errno
== EADDRINUSE
)
1342 status
= RPC_S_DUPLICATE_ENDPOINT
;
1344 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1348 sa_len
= sizeof(sa
);
1349 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1351 WARN("getsockname() failed: %s\n", strerror(errno
));
1353 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1357 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1358 NULL
, 0, service
, sizeof(service
),
1362 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1364 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1368 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1369 protseq
->Protseq
, NULL
,
1370 service
, NULL
, NULL
, NULL
, NULL
);
1371 if (create_status
!= RPC_S_OK
)
1374 status
= create_status
;
1379 ret
= listen(sock
, protseq
->MaxCalls
);
1382 WARN("listen failed: %s\n", strerror(errno
));
1383 RPCRT4_ReleaseConnection(&tcpc
->common
);
1384 status
= RPC_S_OUT_OF_RESOURCES
;
1387 /* need a non-blocking socket, otherwise accept() has a potential
1388 * race-condition (poll() says it is readable, connection drops,
1389 * and accept() blocks until the next connection comes...)
1392 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1395 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1396 RPCRT4_ReleaseConnection(&tcpc
->common
);
1397 status
= RPC_S_OUT_OF_RESOURCES
;
1401 tcpc
->common
.Next
= first_connection
;
1402 first_connection
= &tcpc
->common
;
1404 /* since IPv4 and IPv6 share the same port space, we only need one
1405 * successful bind to listen for both */
1411 /* if at least one connection was created for an endpoint then
1413 if (first_connection
)
1415 RpcConnection
*conn
;
1417 /* find last element in list */
1418 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1421 EnterCriticalSection(&protseq
->cs
);
1422 conn
->Next
= protseq
->conn
;
1423 protseq
->conn
= first_connection
;
1424 LeaveCriticalSection(&protseq
->cs
);
1426 TRACE("listening on %s\n", endpoint
);
1430 ERR("couldn't listen on port %s\n", endpoint
);
1434 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1437 struct sockaddr_in address
;
1439 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1440 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1443 addrsize
= sizeof(address
);
1444 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1447 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1448 return RPC_S_OUT_OF_RESOURCES
;
1451 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1453 TRACE("Accepted a new TCP connection\n");
1457 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1458 void *buffer
, unsigned int count
)
1460 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1462 while (bytes_read
!= count
)
1464 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1469 else if (errno
== EINTR
)
1471 else if (errno
!= EAGAIN
)
1473 WARN("recv() failed: %s\n", strerror(errno
));
1478 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1482 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1486 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1487 const void *buffer
, unsigned int count
)
1489 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1490 int bytes_written
= 0;
1491 while (bytes_written
!= count
)
1493 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1496 else if (errno
== EINTR
)
1498 else if (errno
!= EAGAIN
)
1502 if (!rpcrt4_sock_wait_for_send(tcpc
))
1506 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1507 return bytes_written
;
1510 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1512 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1514 TRACE("%d\n", tcpc
->sock
);
1516 if (tcpc
->sock
!= -1)
1517 closesocket(tcpc
->sock
);
1519 rpcrt4_sock_wait_destroy(tcpc
);
1523 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1525 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1526 TRACE("%p\n", Connection
);
1527 rpcrt4_sock_wait_cancel(tcpc
);
1530 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1532 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1534 TRACE("%p\n", Connection
);
1536 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1541 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1542 const char *networkaddr
,
1543 const char *endpoint
)
1545 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1546 EPM_PROTOCOL_TCP
, endpoint
);
1549 #ifdef HAVE_SOCKETPAIR
1551 typedef struct _RpcServerProtseq_sock
1553 RpcServerProtseq common
;
1556 } RpcServerProtseq_sock
;
1558 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1560 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1564 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1566 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1567 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1568 ps
->mgr_event_rcv
= fds
[0];
1569 ps
->mgr_event_snd
= fds
[1];
1573 ERR("socketpair failed with error %s\n", strerror(errno
));
1574 HeapFree(GetProcessHeap(), 0, ps
);
1581 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1583 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1585 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1588 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1590 struct pollfd
*poll_info
= prev_array
;
1591 RpcConnection_tcp
*conn
;
1592 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1594 EnterCriticalSection(&protseq
->cs
);
1596 /* open and count connections */
1598 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1600 if (conn
->sock
!= -1)
1602 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1605 /* make array of connections */
1607 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1609 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1612 ERR("couldn't allocate poll_info\n");
1613 LeaveCriticalSection(&protseq
->cs
);
1617 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1618 poll_info
[0].events
= POLLIN
;
1620 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1622 if (conn
->sock
!= -1)
1624 poll_info
[*count
].fd
= conn
->sock
;
1625 poll_info
[*count
].events
= POLLIN
;
1628 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1630 LeaveCriticalSection(&protseq
->cs
);
1634 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1636 HeapFree(GetProcessHeap(), 0, array
);
1639 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1641 struct pollfd
*poll_info
= wait_array
;
1644 RpcConnection
*cconn
;
1645 RpcConnection_tcp
*conn
;
1650 ret
= poll(poll_info
, count
, -1);
1653 ERR("poll failed with error %d\n", ret
);
1657 for (i
= 0; i
< count
; i
++)
1658 if (poll_info
[i
].revents
& POLLIN
)
1660 /* RPC server event */
1664 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1668 /* find which connection got a RPC */
1669 EnterCriticalSection(&protseq
->cs
);
1670 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1672 if (poll_info
[i
].fd
== conn
->sock
) break;
1673 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1677 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1679 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1680 LeaveCriticalSection(&protseq
->cs
);
1682 RPCRT4_new_client(cconn
);
1690 #else /* HAVE_SOCKETPAIR */
1692 typedef struct _RpcServerProtseq_sock
1694 RpcServerProtseq common
;
1696 } RpcServerProtseq_sock
;
1698 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1700 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1703 static BOOL wsa_inited
;
1707 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1708 /* Note: WSAStartup can be called more than once so we don't bother with
1709 * making accesses to wsa_inited thread-safe */
1712 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1717 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1719 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1720 SetEvent(sockps
->mgr_event
);
1723 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1725 HANDLE
*objs
= prev_array
;
1726 RpcConnection_tcp
*conn
;
1727 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1729 EnterCriticalSection(&protseq
->cs
);
1731 /* open and count connections */
1733 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1736 if (conn
->sock
!= -1)
1738 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1741 /* make array of connections */
1743 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1745 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1748 ERR("couldn't allocate objs\n");
1749 LeaveCriticalSection(&protseq
->cs
);
1753 objs
[0] = sockps
->mgr_event
;
1755 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1758 if (conn
->sock
!= -1)
1760 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1761 if (res
== SOCKET_ERROR
)
1762 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1765 objs
[*count
] = conn
->sock_event
;
1769 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1771 LeaveCriticalSection(&protseq
->cs
);
1775 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1777 HeapFree(GetProcessHeap(), 0, array
);
1780 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1783 HANDLE
*objs
= wait_array
;
1785 RpcConnection
*cconn
;
1786 RpcConnection_tcp
*conn
;
1793 /* an alertable wait isn't strictly necessary, but due to our
1794 * overlapped I/O implementation in Wine we need to free some memory
1795 * by the file user APC being called, even if no completion routine was
1796 * specified at the time of starting the async operation */
1797 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1798 } while (res
== WAIT_IO_COMPLETION
);
1800 if (res
== WAIT_OBJECT_0
)
1802 else if (res
== WAIT_FAILED
)
1804 ERR("wait failed with error %d\n", GetLastError());
1809 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1810 /* find which connection got a RPC */
1811 EnterCriticalSection(&protseq
->cs
);
1812 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1815 if (b_handle
== conn
->sock_event
) break;
1816 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1820 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1822 ERR("failed to locate connection for handle %p\n", b_handle
);
1823 LeaveCriticalSection(&protseq
->cs
);
1826 RPCRT4_new_client(cconn
);
1833 #endif /* HAVE_SOCKETPAIR */
1835 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1840 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1841 networkaddr
, EPM_PROTOCOL_TCP
,
1845 /**** ncacn_http support ****/
1847 /* 60 seconds is the period native uses */
1848 #define HTTP_IDLE_TIME 60000
1850 /* reference counted to avoid a race between a cancelled call's connection
1851 * being destroyed and the asynchronous InternetReadFileEx call being
1853 typedef struct _RpcHttpAsyncData
1856 HANDLE completion_event
;
1858 INTERNET_BUFFERSA inet_buffers
;
1859 CRITICAL_SECTION cs
;
1862 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1864 return InterlockedIncrement(&data
->refs
);
1867 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1869 ULONG refs
= InterlockedDecrement(&data
->refs
);
1872 TRACE("destroying async data %p\n", data
);
1873 CloseHandle(data
->completion_event
);
1874 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1875 data
->cs
.DebugInfo
->Spare
[0] = 0;
1876 DeleteCriticalSection(&data
->cs
);
1877 HeapFree(GetProcessHeap(), 0, data
);
1882 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
1884 ResetEvent(async_data
->completion_event
);
1885 RpcHttpAsyncData_AddRef(async_data
);
1888 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
1890 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
1894 RpcHttpAsyncData_Release(async_data
);
1898 if(GetLastError() != ERROR_IO_PENDING
) {
1899 RpcHttpAsyncData_Release(async_data
);
1900 ERR("Request failed with error %d\n", GetLastError());
1901 return RPC_S_SERVER_UNAVAILABLE
;
1904 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
1905 if(res
!= WAIT_OBJECT_0
) {
1906 TRACE("Cancelled\n");
1907 return RPC_S_CALL_CANCELLED
;
1910 if(async_data
->async_result
) {
1911 ERR("Async request failed with error %d\n", async_data
->async_result
);
1912 return RPC_S_SERVER_UNAVAILABLE
;
1927 unsigned int data_len
;
1928 BOOL finished
; /* finished authenticating */
1931 typedef struct _RpcConnection_http
1933 RpcConnection common
;
1936 HINTERNET in_request
;
1937 HINTERNET out_request
;
1939 HANDLE timer_cancelled
;
1940 HANDLE cancel_event
;
1941 DWORD last_sent_time
;
1942 ULONG bytes_received
;
1943 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1944 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1945 UUID connection_uuid
;
1948 RpcHttpAsyncData
*async_data
;
1949 } RpcConnection_http
;
1951 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1953 RpcConnection_http
*httpc
;
1954 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1955 if (!httpc
) return NULL
;
1956 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1957 if (!httpc
->async_data
)
1959 HeapFree(GetProcessHeap(), 0, httpc
);
1962 TRACE("async data = %p\n", httpc
->async_data
);
1963 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1964 httpc
->async_data
->refs
= 1;
1965 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1966 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1967 InitializeCriticalSection(&httpc
->async_data
->cs
);
1968 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
1969 return &httpc
->common
;
1972 typedef struct _HttpTimerThreadData
1975 DWORD
*last_sent_time
;
1976 HANDLE timer_cancelled
;
1977 } HttpTimerThreadData
;
1979 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1981 HINTERNET in_request
= param
;
1982 RpcPktHdr
*idle_pkt
;
1984 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1988 DWORD bytes_written
;
1989 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1990 RPCRT4_FreeHeader(idle_pkt
);
1994 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1996 DWORD cur_time
= GetTickCount();
1997 DWORD cached_last_sent_time
= *last_sent_time
;
1998 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2001 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2003 HttpTimerThreadData
*data_in
= param
;
2004 HttpTimerThreadData data
;
2008 HeapFree(GetProcessHeap(), 0, data_in
);
2010 for (timeout
= HTTP_IDLE_TIME
;
2011 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2012 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2014 /* are we too soon after last send? */
2015 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2017 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2020 CloseHandle(data
.timer_cancelled
);
2024 static VOID WINAPI
rpcrt4_http_internet_callback(
2025 HINTERNET hInternet
,
2026 DWORD_PTR dwContext
,
2027 DWORD dwInternetStatus
,
2028 LPVOID lpvStatusInformation
,
2029 DWORD dwStatusInformationLength
)
2031 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2033 switch (dwInternetStatus
)
2035 case INTERNET_STATUS_REQUEST_COMPLETE
:
2036 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2039 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2041 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2042 SetEvent(async_data
->completion_event
);
2043 RpcHttpAsyncData_Release(async_data
);
2049 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2056 WCHAR
*status_text
= buf
;
2060 size
= sizeof(status_code
);
2061 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2063 return GetLastError();
2064 if (status_code
== HTTP_STATUS_OK
)
2068 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2069 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2071 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2072 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2075 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2076 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2078 if (status_code
== HTTP_STATUS_DENIED
)
2079 return ERROR_ACCESS_DENIED
;
2080 return RPC_S_SERVER_UNAVAILABLE
;
2083 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2085 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2086 LPWSTR proxy
= NULL
;
2088 LPWSTR password
= NULL
;
2089 LPWSTR servername
= NULL
;
2090 const WCHAR
*option
;
2093 if (httpc
->common
.QOS
&&
2094 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2096 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2097 if (http_cred
->TransportCredentials
)
2100 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2101 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2102 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2104 return RPC_S_OUT_OF_RESOURCES
;
2106 if (cred
->DomainLength
)
2108 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2109 p
+= cred
->DomainLength
;
2113 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2114 p
[cred
->UserLength
] = 0;
2116 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2120 for (option
= httpc
->common
.NetworkOptions
; option
;
2121 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2123 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2124 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2126 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2128 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2129 const WCHAR
*value_end
;
2132 value_end
= strchrW(option
, ',');
2134 value_end
= value_start
+ strlenW(value_start
);
2135 for (p
= value_start
; p
< value_end
; p
++)
2142 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2143 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2145 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2147 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2148 const WCHAR
*value_end
;
2150 value_end
= strchrW(option
, ',');
2152 value_end
= value_start
+ strlenW(value_start
);
2153 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2154 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2157 FIXME("unhandled option %s\n", debugstr_w(option
));
2160 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2161 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2162 if (!httpc
->app_info
)
2164 HeapFree(GetProcessHeap(), 0, password
);
2165 HeapFree(GetProcessHeap(), 0, user
);
2166 HeapFree(GetProcessHeap(), 0, proxy
);
2167 HeapFree(GetProcessHeap(), 0, servername
);
2168 ERR("InternetOpenW failed with error %d\n", GetLastError());
2169 return RPC_S_SERVER_UNAVAILABLE
;
2171 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2173 /* if no RpcProxy option specified, set the HTTP server address to the
2174 * RPC server address */
2177 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2180 HeapFree(GetProcessHeap(), 0, password
);
2181 HeapFree(GetProcessHeap(), 0, user
);
2182 HeapFree(GetProcessHeap(), 0, proxy
);
2183 return RPC_S_OUT_OF_RESOURCES
;
2185 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2188 port
= (httpc
->common
.QOS
&&
2189 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2190 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2191 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2193 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2194 INTERNET_SERVICE_HTTP
, 0, 0);
2196 HeapFree(GetProcessHeap(), 0, password
);
2197 HeapFree(GetProcessHeap(), 0, user
);
2198 HeapFree(GetProcessHeap(), 0, proxy
);
2200 if (!httpc
->session
)
2202 ERR("InternetConnectW failed with error %d\n", GetLastError());
2203 HeapFree(GetProcessHeap(), 0, servername
);
2204 return RPC_S_SERVER_UNAVAILABLE
;
2206 httpc
->servername
= servername
;
2210 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2217 TRACE("sending echo request to server\n");
2219 prepare_async_request(async_data
);
2220 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2221 status
= wait_async_request(async_data
, ret
, cancel_event
);
2222 if (status
!= RPC_S_OK
) return status
;
2224 status
= rpcrt4_http_check_response(req
);
2225 if (status
!= RPC_S_OK
) return status
;
2227 InternetReadFile(req
, buf
, sizeof(buf
), &bytes_read
);
2228 /* FIXME: do something with retrieved data */
2233 /* prepare the in pipe for use by RPC packets */
2234 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2235 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2236 const UUID
*association_uuid
, BOOL authorized
)
2241 INTERNET_BUFFERSW buffers_in
;
2242 DWORD bytes_written
;
2246 /* ask wininet to authorize, if necessary */
2247 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2248 if (status
!= RPC_S_OK
) return status
;
2250 memset(&buffers_in
, 0, sizeof(buffers_in
));
2251 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2252 /* FIXME: get this from the registry */
2253 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2254 prepare_async_request(async_data
);
2255 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2256 status
= wait_async_request(async_data
, ret
, cancel_event
);
2257 if (status
!= RPC_S_OK
) return status
;
2259 TRACE("sending HTTP connect header to server\n");
2260 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2261 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2262 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2263 RPCRT4_FreeHeader(hdr
);
2266 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2267 return RPC_S_SERVER_UNAVAILABLE
;
2273 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2277 unsigned short data_len
;
2279 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2281 return RPC_S_SERVER_UNAVAILABLE
;
2282 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2284 ERR("wrong packet type received %d or wrong frag_len %d\n",
2285 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2286 return RPC_S_PROTOCOL_ERROR
;
2289 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2291 return RPC_S_SERVER_UNAVAILABLE
;
2293 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2296 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2298 return RPC_S_OUT_OF_RESOURCES
;
2299 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2302 HeapFree(GetProcessHeap(), 0, *data
);
2303 return RPC_S_SERVER_UNAVAILABLE
;
2309 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2311 ERR("invalid http packet\n");
2312 return RPC_S_PROTOCOL_ERROR
;
2318 /* prepare the out pipe for use by RPC packets */
2319 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2320 HANDLE cancel_event
, const UUID
*connection_uuid
,
2321 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2327 BYTE
*data_from_server
;
2328 RpcPktHdr pkt_from_server
;
2329 ULONG field1
, field3
;
2335 /* ask wininet to authorize, if necessary */
2336 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2337 if (status
!= RPC_S_OK
) return status
;
2340 InternetReadFile(out_request
, buf
, sizeof(buf
), &bytes_read
);
2342 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2343 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2345 TRACE("sending HTTP connect header to server\n");
2346 prepare_async_request(async_data
);
2347 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2348 status
= wait_async_request(async_data
, ret
, cancel_event
);
2349 RPCRT4_FreeHeader(hdr
);
2350 if (status
!= RPC_S_OK
) return status
;
2352 status
= rpcrt4_http_check_response(out_request
);
2353 if (status
!= RPC_S_OK
) return status
;
2355 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2357 if (status
!= RPC_S_OK
) return status
;
2358 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2360 HeapFree(GetProcessHeap(), 0, data_from_server
);
2361 if (status
!= RPC_S_OK
) return status
;
2362 TRACE("received (%d) from first prepare header\n", field1
);
2366 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2368 if (status
!= RPC_S_OK
) return status
;
2369 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2371 TRACE("http idle packet, waiting for real packet\n");
2372 HeapFree(GetProcessHeap(), 0, data_from_server
);
2373 if (pkt_from_server
.http
.num_data_items
!= 0)
2375 ERR("HTTP idle packet should have no data items instead of %d\n",
2376 pkt_from_server
.http
.num_data_items
);
2377 return RPC_S_PROTOCOL_ERROR
;
2380 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2381 &field1
, flow_control_increment
,
2383 HeapFree(GetProcessHeap(), 0, data_from_server
);
2384 if (status
!= RPC_S_OK
) return status
;
2385 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2390 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2392 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2397 /* first 6 bits, all from bin[0] */
2398 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2399 x
= (bin
[0] & 3) << 4;
2401 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2404 base64
[i
++] = enc
[x
];
2409 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2410 x
= (bin
[1] & 0x0f) << 2;
2412 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2415 base64
[i
++] = enc
[x
];
2419 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2421 /* last 6 bits, all from bin [2] */
2422 base64
[i
++] = enc
[bin
[2] & 0x3f];
2430 static inline char decode_char( WCHAR c
)
2432 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2433 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2434 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2435 if (c
== '+') return 62;
2436 if (c
== '/') return 63;
2440 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2443 char c0
, c1
, c2
, c3
;
2444 const WCHAR
*p
= base64
;
2448 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2449 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2450 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2451 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2455 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2456 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2457 buf
[i
+ 2] = (c2
<< 6) | c3
;
2465 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2466 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2468 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2471 else if (p
[3] == '=')
2473 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2474 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2475 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2479 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2480 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2486 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2487 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2488 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2489 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2493 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2494 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2495 buf
[i
+ 2] = (c2
<< 6) | c3
;
2502 static struct authinfo
*alloc_authinfo(void)
2504 struct authinfo
*ret
;
2506 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2508 SecInvalidateHandle(&ret
->cred
);
2509 SecInvalidateHandle(&ret
->ctx
);
2510 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2516 ret
->finished
= FALSE
;
2520 static void destroy_authinfo(struct authinfo
*info
)
2524 if (SecIsValidHandle(&info
->ctx
))
2525 DeleteSecurityContext(&info
->ctx
);
2526 if (SecIsValidHandle(&info
->cred
))
2527 FreeCredentialsHandle(&info
->cred
);
2529 HeapFree(GetProcessHeap(), 0, info
->data
);
2530 HeapFree(GetProcessHeap(), 0, info
);
2533 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2534 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2535 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2536 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2537 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2547 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2548 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2549 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2550 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2551 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2553 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2555 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2558 for (i
= 0; i
< num_auth_schemes
; i
++)
2560 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2561 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2566 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2568 DWORD len
, index
= 0;
2572 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2573 if (auth_scheme_from_header(buffer
) == scheme
) break;
2578 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2579 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2581 struct authinfo
*info
= *auth_ptr
;
2582 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2583 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2585 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2587 switch (creds
->AuthnSchemes
[0])
2589 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2591 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2592 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2594 info
->data_len
= userlen
+ passlen
+ 1;
2595 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2597 status
= RPC_S_OUT_OF_MEMORY
;
2600 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2601 info
->data
[userlen
] = ':';
2602 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2604 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2605 info
->finished
= TRUE
;
2609 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2610 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2613 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2614 SECURITY_STATUS ret
;
2615 SecBufferDesc out_desc
, in_desc
;
2617 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2621 WCHAR auth_value
[2048];
2622 DWORD size
= sizeof(auth_value
);
2625 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2626 else scheme
= negotiateW
;
2627 scheme_len
= strlenW( scheme
);
2632 SecPkgInfoW
*pkg_info
;
2634 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2635 if (ret
!= SEC_E_OK
) break;
2637 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2638 if (ret
!= SEC_E_OK
) break;
2640 info
->max_token
= pkg_info
->cbMaxToken
;
2641 FreeContextBuffer(pkg_info
);
2646 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2647 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2649 ERR("authentication scheme changed\n");
2653 in
.BufferType
= SECBUFFER_TOKEN
;
2657 in_desc
.ulVersion
= 0;
2658 in_desc
.cBuffers
= 1;
2659 in_desc
.pBuffers
= &in
;
2661 p
= auth_value
+ scheme_len
;
2662 if (!first
&& *p
== ' ')
2664 int len
= strlenW(++p
);
2665 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2666 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2667 decode_base64(p
, len
, in
.pvBuffer
);
2669 out
.BufferType
= SECBUFFER_TOKEN
;
2670 out
.cbBuffer
= info
->max_token
;
2671 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2673 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2676 out_desc
.ulVersion
= 0;
2677 out_desc
.cBuffers
= 1;
2678 out_desc
.pBuffers
= &out
;
2680 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2681 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2682 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2683 &info
->attr
, &info
->exp
);
2684 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2685 if (ret
== SEC_E_OK
)
2687 HeapFree(GetProcessHeap(), 0, info
->data
);
2688 info
->data
= out
.pvBuffer
;
2689 info
->data_len
= out
.cbBuffer
;
2690 info
->finished
= TRUE
;
2691 TRACE("sending last auth packet\n");
2694 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2696 HeapFree(GetProcessHeap(), 0, info
->data
);
2697 info
->data
= out
.pvBuffer
;
2698 info
->data_len
= out
.cbBuffer
;
2699 TRACE("sending next auth packet\n");
2704 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2705 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2708 info
->scheme
= creds
->AuthnSchemes
[0];
2712 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2716 if (status
!= RPC_S_OK
)
2718 destroy_authinfo(info
);
2726 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2728 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2729 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2730 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2731 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2732 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2733 const WCHAR
*scheme_str
;
2734 WCHAR
*header
, *ptr
;
2735 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2739 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2740 scheme_str
= basicW
;
2741 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2743 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2744 scheme_str
= negotiateW
;
2745 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2747 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2749 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2752 ERR("unknown scheme %u\n", scheme
);
2753 return RPC_S_SERVER_UNAVAILABLE
;
2755 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2757 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2758 ptr
= header
+ auth_len
;
2759 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2761 len
= encode_base64(data
, data_len
, ptr
);
2765 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2767 HeapFree(GetProcessHeap(), 0, header
);
2772 static void drain_content(HINTERNET request
)
2774 DWORD count
, len
= 0, size
= sizeof(len
);
2777 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2781 count
= min(sizeof(buf
), len
);
2782 if (!InternetReadFile(request
, buf
, count
, &count
) || !count
) return;
2787 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2789 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2790 struct authinfo
*info
= NULL
;
2796 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2797 if (status
!= RPC_S_OK
) break;
2799 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2800 if (status
!= RPC_S_OK
) break;
2802 prepare_async_request(httpc
->async_data
);
2803 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2804 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2805 if (status
!= RPC_S_OK
|| info
->finished
) break;
2807 status
= rpcrt4_http_check_response(request
);
2808 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
2809 drain_content(request
);
2812 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2813 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
);
2815 destroy_authinfo(info
);
2819 static RPC_STATUS
insert_cookie_header(HINTERNET request
, const WCHAR
*value
)
2821 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
2822 WCHAR
*header
, *ptr
;
2824 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2826 if (!value
) return RPC_S_OK
;
2828 len
= strlenW(value
);
2829 if ((header
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookieW
) + (len
+ 3) * sizeof(WCHAR
))))
2831 memcpy(header
, cookieW
, sizeof(cookieW
));
2832 ptr
= header
+ sizeof(cookieW
) / sizeof(cookieW
[0]);
2833 memcpy(ptr
, value
, len
* sizeof(WCHAR
));
2837 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW
))) status
= RPC_S_OK
;
2838 HeapFree(GetProcessHeap(), 0, header
);
2843 static BOOL
has_credentials(RpcConnection_http
*httpc
)
2845 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
2846 SEC_WINNT_AUTH_IDENTITY_W
*id
;
2848 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
2851 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2852 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
2855 id
= creds
->TransportCredentials
;
2856 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
2861 static BOOL
is_secure(RpcConnection_http
*httpc
)
2863 return httpc
->common
.QOS
&&
2864 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2865 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2868 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2870 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2871 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2872 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2873 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2874 static const WCHAR wszColon
[] = {':',0};
2875 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2876 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2880 BOOL secure
, credentials
;
2881 HttpTimerThreadData
*timer_data
;
2884 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2886 if (Connection
->server
)
2888 ERR("ncacn_http servers not supported yet\n");
2889 return RPC_S_SERVER_UNAVAILABLE
;
2892 if (httpc
->in_request
)
2895 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2897 status
= UuidCreate(&httpc
->connection_uuid
);
2898 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2899 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2901 status
= rpcrt4_http_internet_connect(httpc
);
2902 if (status
!= RPC_S_OK
)
2905 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2907 return RPC_S_OUT_OF_MEMORY
;
2908 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2909 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2910 strcatW(url
, wszColon
);
2911 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2913 secure
= is_secure(httpc
);
2914 credentials
= has_credentials(httpc
);
2916 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
2917 INTERNET_FLAG_NO_AUTO_REDIRECT
;
2918 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
2919 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
2921 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
2922 flags
, (DWORD_PTR
)httpc
->async_data
);
2923 if (!httpc
->in_request
)
2925 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2926 HeapFree(GetProcessHeap(), 0, url
);
2927 return RPC_S_SERVER_UNAVAILABLE
;
2929 status
= insert_cookie_header(httpc
->in_request
, Connection
->CookieAuth
);
2930 if (status
!= RPC_S_OK
)
2932 HeapFree(GetProcessHeap(), 0, url
);
2937 status
= authorize_request(httpc
, httpc
->in_request
);
2938 if (status
!= RPC_S_OK
)
2940 HeapFree(GetProcessHeap(), 0, url
);
2943 status
= rpcrt4_http_check_response(httpc
->in_request
);
2944 if (status
!= RPC_S_OK
)
2946 HeapFree(GetProcessHeap(), 0, url
);
2949 drain_content(httpc
->in_request
);
2952 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
2953 flags
, (DWORD_PTR
)httpc
->async_data
);
2954 HeapFree(GetProcessHeap(), 0, url
);
2955 if (!httpc
->out_request
)
2957 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2958 return RPC_S_SERVER_UNAVAILABLE
;
2960 status
= insert_cookie_header(httpc
->out_request
, Connection
->CookieAuth
);
2961 if (status
!= RPC_S_OK
)
2966 status
= authorize_request(httpc
, httpc
->out_request
);
2967 if (status
!= RPC_S_OK
)
2971 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
,
2972 &httpc
->connection_uuid
, &httpc
->in_pipe_uuid
,
2973 &Connection
->assoc
->http_uuid
, credentials
);
2974 if (status
!= RPC_S_OK
)
2977 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
,
2978 &httpc
->connection_uuid
, &httpc
->out_pipe_uuid
,
2979 &httpc
->flow_control_increment
, credentials
);
2980 if (status
!= RPC_S_OK
)
2983 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2984 httpc
->last_sent_time
= GetTickCount();
2985 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2987 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2989 return ERROR_OUTOFMEMORY
;
2990 timer_data
->timer_param
= httpc
->in_request
;
2991 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2992 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2993 /* FIXME: should use CreateTimerQueueTimer when implemented */
2994 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2997 HeapFree(GetProcessHeap(), 0, timer_data
);
2998 return GetLastError();
3000 CloseHandle(thread
);
3005 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
3008 return RPC_S_SERVER_UNAVAILABLE
;
3011 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
3012 void *buffer
, unsigned int count
)
3014 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3017 unsigned int bytes_left
= count
;
3018 RPC_STATUS status
= RPC_S_OK
;
3020 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
3024 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
3025 prepare_async_request(httpc
->async_data
);
3026 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
3027 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3028 if(status
!= RPC_S_OK
) {
3029 if(status
== RPC_S_CALL_CANCELLED
)
3030 TRACE("call cancelled\n");
3034 if(!httpc
->async_data
->inet_buffers
.dwBufferLength
)
3036 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
3037 httpc
->async_data
->inet_buffers
.dwBufferLength
);
3039 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
3040 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
3043 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
3044 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
3046 TRACE("%p %p %u -> %u\n", httpc
->out_request
, buffer
, count
, status
);
3047 return status
== RPC_S_OK
? count
: -1;
3050 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
3052 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3056 RpcPktCommonHdr common_hdr
;
3060 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
3063 /* read packet common header */
3064 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
3065 if (dwRead
!= sizeof(common_hdr
)) {
3066 WARN("Short read of header, %d bytes\n", dwRead
);
3067 status
= RPC_S_PROTOCOL_ERROR
;
3070 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
3071 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
3073 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
3074 status
= RPC_S_PROTOCOL_ERROR
;
3078 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
3079 if (status
!= RPC_S_OK
) goto fail
;
3081 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
3082 if (hdr_length
== 0) {
3083 WARN("header length == 0\n");
3084 status
= RPC_S_PROTOCOL_ERROR
;
3088 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
3091 status
= RPC_S_OUT_OF_RESOURCES
;
3094 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
3096 /* read the rest of packet header */
3097 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
3098 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
3099 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
3100 status
= RPC_S_PROTOCOL_ERROR
;
3104 if (common_hdr
.frag_len
- hdr_length
)
3106 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
3109 status
= RPC_S_OUT_OF_RESOURCES
;
3113 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
3114 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
3116 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
3117 status
= RPC_S_PROTOCOL_ERROR
;
3124 if ((*Header
)->common
.ptype
== PKT_HTTP
)
3126 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
3128 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
3129 status
= RPC_S_PROTOCOL_ERROR
;
3132 if ((*Header
)->http
.flags
== 0x0001)
3134 TRACE("http idle packet, waiting for real packet\n");
3135 if ((*Header
)->http
.num_data_items
!= 0)
3137 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
3138 status
= RPC_S_PROTOCOL_ERROR
;
3142 else if ((*Header
)->http
.flags
== 0x0002)
3144 ULONG bytes_transmitted
;
3145 ULONG flow_control_increment
;
3147 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
3150 &flow_control_increment
,
3152 if (status
!= RPC_S_OK
)
3154 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
3155 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
3156 /* FIXME: do something with parsed data */
3160 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
3161 status
= RPC_S_PROTOCOL_ERROR
;
3164 RPCRT4_FreeHeader(*Header
);
3166 HeapFree(GetProcessHeap(), 0, *Payload
);
3174 httpc
->bytes_received
+= common_hdr
.frag_len
;
3176 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
3178 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
3180 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
3181 httpc
->bytes_received
,
3182 httpc
->flow_control_increment
,
3183 &httpc
->out_pipe_uuid
);
3186 DWORD bytes_written
;
3188 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
3189 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
3190 RPCRT4_FreeHeader(hdr
);
3192 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
3197 if (status
!= RPC_S_OK
) {
3198 RPCRT4_FreeHeader(*Header
);
3200 HeapFree(GetProcessHeap(), 0, *Payload
);
3206 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
3207 const void *buffer
, unsigned int count
)
3209 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3210 DWORD bytes_written
;
3213 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
3214 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
3215 httpc
->last_sent_time
= GetTickCount();
3216 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
3217 return ret
? bytes_written
: -1;
3220 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
3222 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3226 SetEvent(httpc
->timer_cancelled
);
3227 if (httpc
->in_request
)
3228 InternetCloseHandle(httpc
->in_request
);
3229 httpc
->in_request
= NULL
;
3230 if (httpc
->out_request
)
3231 InternetCloseHandle(httpc
->out_request
);
3232 httpc
->out_request
= NULL
;
3233 if (httpc
->app_info
)
3234 InternetCloseHandle(httpc
->app_info
);
3235 httpc
->app_info
= NULL
;
3237 InternetCloseHandle(httpc
->session
);
3238 httpc
->session
= NULL
;
3239 RpcHttpAsyncData_Release(httpc
->async_data
);
3240 if (httpc
->cancel_event
)
3241 CloseHandle(httpc
->cancel_event
);
3242 HeapFree(GetProcessHeap(), 0, httpc
->servername
);
3243 httpc
->servername
= NULL
;
3248 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
3250 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3252 SetEvent(httpc
->cancel_event
);
3255 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
3257 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3261 prepare_async_request(httpc
->async_data
);
3262 ret
= InternetQueryDataAvailable(httpc
->out_request
,
3263 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
3264 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3265 return status
== RPC_S_OK
? 0 : -1;
3268 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
3269 const char *networkaddr
,
3270 const char *endpoint
)
3272 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
3273 EPM_PROTOCOL_HTTP
, endpoint
);
3276 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
3281 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
3282 networkaddr
, EPM_PROTOCOL_HTTP
,
3286 static const struct connection_ops conn_protseq_list
[] = {
3288 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
3289 rpcrt4_conn_np_alloc
,
3290 rpcrt4_ncacn_np_open
,
3291 rpcrt4_ncacn_np_handoff
,
3292 rpcrt4_conn_np_read
,
3293 rpcrt4_conn_np_write
,
3294 rpcrt4_conn_np_close
,
3295 rpcrt4_conn_np_cancel_call
,
3296 rpcrt4_conn_np_wait_for_incoming_data
,
3297 rpcrt4_ncacn_np_get_top_of_tower
,
3298 rpcrt4_ncacn_np_parse_top_of_tower
,
3300 RPCRT4_default_is_authorized
,
3301 RPCRT4_default_authorize
,
3302 RPCRT4_default_secure_packet
,
3303 rpcrt4_conn_np_impersonate_client
,
3304 rpcrt4_conn_np_revert_to_self
,
3305 RPCRT4_default_inquire_auth_client
,
3308 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
3309 rpcrt4_conn_np_alloc
,
3310 rpcrt4_ncalrpc_open
,
3311 rpcrt4_ncalrpc_handoff
,
3312 rpcrt4_conn_np_read
,
3313 rpcrt4_conn_np_write
,
3314 rpcrt4_conn_np_close
,
3315 rpcrt4_conn_np_cancel_call
,
3316 rpcrt4_conn_np_wait_for_incoming_data
,
3317 rpcrt4_ncalrpc_get_top_of_tower
,
3318 rpcrt4_ncalrpc_parse_top_of_tower
,
3320 rpcrt4_ncalrpc_is_authorized
,
3321 rpcrt4_ncalrpc_authorize
,
3322 rpcrt4_ncalrpc_secure_packet
,
3323 rpcrt4_conn_np_impersonate_client
,
3324 rpcrt4_conn_np_revert_to_self
,
3325 rpcrt4_ncalrpc_inquire_auth_client
,
3328 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
3329 rpcrt4_conn_tcp_alloc
,
3330 rpcrt4_ncacn_ip_tcp_open
,
3331 rpcrt4_conn_tcp_handoff
,
3332 rpcrt4_conn_tcp_read
,
3333 rpcrt4_conn_tcp_write
,
3334 rpcrt4_conn_tcp_close
,
3335 rpcrt4_conn_tcp_cancel_call
,
3336 rpcrt4_conn_tcp_wait_for_incoming_data
,
3337 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
3338 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
3340 RPCRT4_default_is_authorized
,
3341 RPCRT4_default_authorize
,
3342 RPCRT4_default_secure_packet
,
3343 RPCRT4_default_impersonate_client
,
3344 RPCRT4_default_revert_to_self
,
3345 RPCRT4_default_inquire_auth_client
,
3348 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
3349 rpcrt4_ncacn_http_alloc
,
3350 rpcrt4_ncacn_http_open
,
3351 rpcrt4_ncacn_http_handoff
,
3352 rpcrt4_ncacn_http_read
,
3353 rpcrt4_ncacn_http_write
,
3354 rpcrt4_ncacn_http_close
,
3355 rpcrt4_ncacn_http_cancel_call
,
3356 rpcrt4_ncacn_http_wait_for_incoming_data
,
3357 rpcrt4_ncacn_http_get_top_of_tower
,
3358 rpcrt4_ncacn_http_parse_top_of_tower
,
3359 rpcrt4_ncacn_http_receive_fragment
,
3360 RPCRT4_default_is_authorized
,
3361 RPCRT4_default_authorize
,
3362 RPCRT4_default_secure_packet
,
3363 RPCRT4_default_impersonate_client
,
3364 RPCRT4_default_revert_to_self
,
3365 RPCRT4_default_inquire_auth_client
,
3370 static const struct protseq_ops protseq_list
[] =
3374 rpcrt4_protseq_np_alloc
,
3375 rpcrt4_protseq_np_signal_state_changed
,
3376 rpcrt4_protseq_np_get_wait_array
,
3377 rpcrt4_protseq_np_free_wait_array
,
3378 rpcrt4_protseq_np_wait_for_new_connection
,
3379 rpcrt4_protseq_ncacn_np_open_endpoint
,
3383 rpcrt4_protseq_np_alloc
,
3384 rpcrt4_protseq_np_signal_state_changed
,
3385 rpcrt4_protseq_np_get_wait_array
,
3386 rpcrt4_protseq_np_free_wait_array
,
3387 rpcrt4_protseq_np_wait_for_new_connection
,
3388 rpcrt4_protseq_ncalrpc_open_endpoint
,
3392 rpcrt4_protseq_sock_alloc
,
3393 rpcrt4_protseq_sock_signal_state_changed
,
3394 rpcrt4_protseq_sock_get_wait_array
,
3395 rpcrt4_protseq_sock_free_wait_array
,
3396 rpcrt4_protseq_sock_wait_for_new_connection
,
3397 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
3401 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3404 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3405 if (!strcmp(protseq_list
[i
].name
, protseq
))
3406 return &protseq_list
[i
];
3410 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3413 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3414 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3415 return &conn_protseq_list
[i
];
3419 /**** interface to rest of code ****/
3421 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3423 TRACE("(Connection == ^%p)\n", Connection
);
3425 assert(!Connection
->server
);
3426 return Connection
->ops
->open_connection_client(Connection
);
3429 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3431 TRACE("(Connection == ^%p)\n", Connection
);
3432 if (SecIsValidHandle(&Connection
->ctx
))
3434 DeleteSecurityContext(&Connection
->ctx
);
3435 SecInvalidateHandle(&Connection
->ctx
);
3437 rpcrt4_conn_close(Connection
);
3441 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3442 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3443 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3445 static LONG next_id
;
3446 const struct connection_ops
*ops
;
3447 RpcConnection
* NewConnection
;
3449 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3452 FIXME("not supported for protseq %s\n", Protseq
);
3453 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3456 NewConnection
= ops
->alloc();
3457 NewConnection
->ref
= 1;
3458 NewConnection
->Next
= NULL
;
3459 NewConnection
->server_binding
= NULL
;
3460 NewConnection
->server
= server
;
3461 NewConnection
->ops
= ops
;
3462 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3463 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3464 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3465 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3466 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3467 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
3468 NewConnection
->NextCallId
= 1;
3470 SecInvalidateHandle(&NewConnection
->ctx
);
3471 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
3472 NewConnection
->attr
= 0;
3473 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3474 NewConnection
->AuthInfo
= AuthInfo
;
3475 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3476 NewConnection
->encryption_auth_len
= 0;
3477 NewConnection
->signature_auth_len
= 0;
3478 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3479 NewConnection
->QOS
= QOS
;
3481 list_init(&NewConnection
->conn_pool_entry
);
3482 NewConnection
->async_state
= NULL
;
3484 TRACE("connection: %p\n", NewConnection
);
3485 *Connection
= NewConnection
;
3490 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3494 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
, rpcrt4_conn_get_name(OldConnection
),
3495 OldConnection
->NetworkAddr
, OldConnection
->Endpoint
, NULL
,
3496 OldConnection
->AuthInfo
, OldConnection
->QOS
, OldConnection
->CookieAuth
);
3497 if (err
== RPC_S_OK
)
3498 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3502 RpcConnection
*RPCRT4_GrabConnection( RpcConnection
*conn
)
3504 InterlockedIncrement( &conn
->ref
);
3508 RPC_STATUS
RPCRT4_ReleaseConnection(RpcConnection
* Connection
)
3510 if (InterlockedDecrement( &Connection
->ref
) > 0) return RPC_S_OK
;
3512 TRACE("destroying connection %p\n", Connection
);
3514 RPCRT4_CloseConnection(Connection
);
3515 RPCRT4_strfree(Connection
->Endpoint
);
3516 RPCRT4_strfree(Connection
->NetworkAddr
);
3517 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3518 HeapFree(GetProcessHeap(), 0, Connection
->CookieAuth
);
3519 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3520 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3523 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3525 HeapFree(GetProcessHeap(), 0, Connection
);
3529 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3531 const char *protseq
,
3532 const char *networkaddr
,
3533 const char *endpoint
)
3535 twr_empty_floor_t
*protocol_floor
;
3536 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3541 return RPC_S_INVALID_RPC_PROTSEQ
;
3545 *tower_size
= sizeof(*protocol_floor
);
3546 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3550 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3551 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3552 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3553 protocol_floor
->count_rhs
= 0;
3555 tower_data
+= sizeof(*protocol_floor
);
3557 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3559 return EPT_S_NOT_REGISTERED
;
3561 *tower_size
+= sizeof(*protocol_floor
);
3566 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3572 const twr_empty_floor_t
*protocol_floor
;
3573 const twr_empty_floor_t
*floor4
;
3574 const struct connection_ops
*protseq_ops
= NULL
;
3578 if (tower_size
< sizeof(*protocol_floor
))
3579 return EPT_S_NOT_REGISTERED
;
3581 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3582 tower_data
+= sizeof(*protocol_floor
);
3583 tower_size
-= sizeof(*protocol_floor
);
3584 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3585 (protocol_floor
->count_rhs
> tower_size
))
3586 return EPT_S_NOT_REGISTERED
;
3587 tower_data
+= protocol_floor
->count_rhs
;
3588 tower_size
-= protocol_floor
->count_rhs
;
3590 floor4
= (const twr_empty_floor_t
*)tower_data
;
3591 if ((tower_size
< sizeof(*floor4
)) ||
3592 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3593 return EPT_S_NOT_REGISTERED
;
3595 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3596 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3597 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3599 protseq_ops
= &conn_protseq_list
[i
];
3604 return EPT_S_NOT_REGISTERED
;
3606 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3608 if ((status
== RPC_S_OK
) && protseq
)
3610 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3611 strcpy(*protseq
, protseq_ops
->name
);
3617 /***********************************************************************
3618 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3620 * Checks if the given protocol sequence is known by the RPC system.
3621 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3624 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3628 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3629 ps
, sizeof ps
, NULL
, NULL
);
3630 if (rpcrt4_get_conn_protseq_ops(ps
))
3633 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3635 return RPC_S_INVALID_RPC_PROTSEQ
;
3638 /***********************************************************************
3639 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3641 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3643 UNICODE_STRING protseqW
;
3645 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3647 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3648 RtlFreeUnicodeString(&protseqW
);
3651 return RPC_S_OUT_OF_MEMORY
;
3654 /***********************************************************************
3655 * RpcProtseqVectorFreeA (RPCRT4.@)
3657 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3659 TRACE("(%p)\n", protseqs
);
3664 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3665 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3666 HeapFree(GetProcessHeap(), 0, *protseqs
);
3672 /***********************************************************************
3673 * RpcProtseqVectorFreeW (RPCRT4.@)
3675 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3677 TRACE("(%p)\n", protseqs
);
3682 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3683 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3684 HeapFree(GetProcessHeap(), 0, *protseqs
);
3690 /***********************************************************************
3691 * RpcNetworkInqProtseqsW (RPCRT4.@)
3693 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3695 RPC_PROTSEQ_VECTORW
*pvector
;
3697 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3699 TRACE("(%p)\n", protseqs
);
3701 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3704 pvector
= *protseqs
;
3706 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3708 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3709 if (pvector
->Protseq
[i
] == NULL
)
3711 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3712 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3718 if (status
!= RPC_S_OK
)
3719 RpcProtseqVectorFreeW(protseqs
);
3723 /***********************************************************************
3724 * RpcNetworkInqProtseqsA (RPCRT4.@)
3726 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3728 RPC_PROTSEQ_VECTORA
*pvector
;
3730 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3732 TRACE("(%p)\n", protseqs
);
3734 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3737 pvector
= *protseqs
;
3739 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3741 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3742 if (pvector
->Protseq
[i
] == NULL
)
3744 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3750 if (status
!= RPC_S_OK
)
3751 RpcProtseqVectorFreeA(protseqs
);