4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 Mike McCormack
8 * Copyright 2006 Damjan Jovanovic
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #if defined(__MINGW32__) || defined (_MSC_VER)
29 # include <ws2tcpip.h>
31 # define EADDRINUSE WSAEADDRINUSE
34 # define EAGAIN WSAEWOULDBLOCK
37 # define errno WSAGetLastError()
44 # ifdef HAVE_SYS_SOCKET_H
45 # include <sys/socket.h>
47 # ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
50 # ifdef HAVE_NETINET_TCP_H
51 # include <netinet/tcp.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
59 # ifdef HAVE_SYS_POLL_H
60 # include <sys/poll.h>
62 # ifdef HAVE_SYS_FILIO_H
63 # include <sys/filio.h>
65 # ifdef HAVE_SYS_IOCTL_H
66 # include <sys/ioctl.h>
68 # define closesocket close
69 # define ioctlsocket ioctl
70 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
74 #include "epm_towers.h"
77 # define SOL_TCP IPPROTO_TCP
80 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
82 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
84 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
86 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
88 /**** ncacn_np support ****/
90 typedef struct _RpcConnection_np
98 static RpcConnection
*rpcrt4_conn_np_alloc(void)
100 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
104 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
105 npc
->listening
= FALSE
;
110 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
115 npc
->listening
= TRUE
;
118 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
121 switch(GetLastError())
123 case ERROR_PIPE_CONNECTED
:
124 SetEvent(npc
->ovl
.hEvent
);
126 case ERROR_IO_PENDING
:
127 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
129 case ERROR_NO_DATA_DETECTED
:
130 /* client has disconnected, retry */
131 DisconnectNamedPipe( npc
->pipe
);
134 npc
->listening
= FALSE
;
135 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
136 return RPC_S_OUT_OF_RESOURCES
;
142 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
147 npc
->listening
= TRUE
;
148 npc
->listen_thread
= CreateThread(NULL
, 0, listen_thread
, npc
, 0, NULL
);
149 if (!npc
->listen_thread
)
151 npc
->listening
= FALSE
;
152 ERR("Couldn't create listen thread (error was %d)\n", GetLastError());
153 return RPC_S_OUT_OF_RESOURCES
;
159 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
161 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
162 TRACE("listening on %s\n", pname
);
164 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
165 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
166 PIPE_UNLIMITED_INSTANCES
,
167 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
168 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
169 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
170 if (GetLastError() == ERROR_FILE_EXISTS
)
171 return RPC_S_DUPLICATE_ENDPOINT
;
173 return RPC_S_CANT_CREATE_ENDPOINT
;
176 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
177 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
179 /* Note: we don't call ConnectNamedPipe here because it must be done in the
180 * server thread as the thread must be alertable */
184 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
186 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
190 TRACE("connecting to %s\n", pname
);
196 dwFlags
= SECURITY_SQOS_PRESENT
;
197 switch (Connection
->QOS
->qos
->ImpersonationType
)
199 case RPC_C_IMP_LEVEL_DEFAULT
:
200 /* FIXME: what to do here? */
202 case RPC_C_IMP_LEVEL_ANONYMOUS
:
203 dwFlags
|= SECURITY_ANONYMOUS
;
205 case RPC_C_IMP_LEVEL_IDENTIFY
:
206 dwFlags
|= SECURITY_IDENTIFICATION
;
208 case RPC_C_IMP_LEVEL_IMPERSONATE
:
209 dwFlags
|= SECURITY_IMPERSONATION
;
211 case RPC_C_IMP_LEVEL_DELEGATE
:
212 dwFlags
|= SECURITY_DELEGATION
;
215 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
216 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
218 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
219 OPEN_EXISTING
, dwFlags
, 0);
220 if (pipe
!= INVALID_HANDLE_VALUE
) break;
221 err
= GetLastError();
222 if (err
== ERROR_PIPE_BUSY
) {
223 TRACE("connection failed, error=%x\n", err
);
224 return RPC_S_SERVER_TOO_BUSY
;
225 } else if (err
== ERROR_BAD_NETPATH
) {
226 TRACE("connection failed, error=%x\n", err
);
227 return RPC_S_SERVER_UNAVAILABLE
;
229 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
230 err
= GetLastError();
231 WARN("connection failed, error=%x\n", err
);
232 return RPC_S_SERVER_UNAVAILABLE
;
237 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
238 /* pipe is connected; change to message-read mode. */
239 dwMode
= PIPE_READMODE_MESSAGE
;
240 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
241 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
247 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
249 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
250 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
254 /* already connected? */
258 /* protseq=ncalrpc: supposed to use NT LPC ports,
259 * but we'll implement it with named pipes for now */
260 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
261 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
262 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
268 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
270 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
273 RpcConnection
*Connection
;
274 char generated_endpoint
[22];
278 static LONG lrpc_nameless_id
;
279 DWORD process_id
= GetCurrentProcessId();
280 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
281 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
282 "LRPC%08x.%08x", process_id
, id
);
283 endpoint
= generated_endpoint
;
286 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
287 endpoint
, NULL
, NULL
, NULL
, NULL
);
291 /* protseq=ncalrpc: supposed to use NT LPC ports,
292 * but we'll implement it with named pipes for now */
293 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
294 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
295 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
298 EnterCriticalSection(&protseq
->cs
);
299 Connection
->Next
= protseq
->conn
;
300 protseq
->conn
= Connection
;
301 LeaveCriticalSection(&protseq
->cs
);
306 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
308 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
309 static const char prefix
[] = "\\\\";
310 static const char local
[] =".";
315 /* already connected? */
319 /* protseq=ncacn_np: named pipes */
320 size
= strlen(prefix
);
321 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
322 size
+= strlen(local
);
324 size
+= strlen(Connection
->NetworkAddr
);
325 size
+= strlen(Connection
->Endpoint
) + 1;
327 pname
= I_RpcAllocate(size
);
328 strcpy(pname
, prefix
);
329 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
330 strcat(pname
, local
);
332 strcat(pname
, Connection
->NetworkAddr
);
333 strcat(pname
, Connection
->Endpoint
);
334 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
340 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
342 static const char prefix
[] = "\\\\.";
345 RpcConnection
*Connection
;
346 char generated_endpoint
[21];
350 static LONG np_nameless_id
;
351 DWORD process_id
= GetCurrentProcessId();
352 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
353 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
354 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
355 endpoint
= generated_endpoint
;
358 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
359 endpoint
, NULL
, NULL
, NULL
, NULL
);
363 /* protseq=ncacn_np: named pipes */
364 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
365 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
366 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
369 EnterCriticalSection(&protseq
->cs
);
370 Connection
->Next
= protseq
->conn
;
371 protseq
->conn
= Connection
;
372 LeaveCriticalSection(&protseq
->cs
);
377 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
379 /* because of the way named pipes work, we'll transfer the connected pipe
380 * to the child, then reopen the server binding to continue listening */
382 new_npc
->pipe
= old_npc
->pipe
;
383 new_npc
->ovl
= old_npc
->ovl
;
385 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
386 old_npc
->listening
= FALSE
;
389 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
393 static const char prefix
[] = "\\\\.";
395 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
397 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
398 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
399 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
405 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
409 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
411 TRACE("%s\n", old_conn
->Endpoint
);
413 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
415 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
416 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
417 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
423 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
424 void *buffer
, unsigned int count
)
426 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
429 unsigned int bytes_left
= count
;
432 ZeroMemory(&ovl
, sizeof(ovl
));
433 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
438 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
439 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
440 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
441 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
443 if (!ret
|| !bytes_read
)
445 bytes_left
-= bytes_read
;
448 CloseHandle(ovl
.hEvent
);
449 return ret
? count
: -1;
452 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
453 const void *buffer
, unsigned int count
)
455 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
456 const char *buf
= buffer
;
458 unsigned int bytes_left
= count
;
461 ZeroMemory(&ovl
, sizeof(ovl
));
462 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
467 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
468 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
469 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
470 if (!ret
|| !bytes_written
)
472 bytes_left
-= bytes_written
;
473 buf
+= bytes_written
;
475 CloseHandle(ovl
.hEvent
);
476 return ret
? count
: -1;
479 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
481 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
483 FlushFileBuffers(npc
->pipe
);
484 CloseHandle(npc
->pipe
);
487 if (npc
->ovl
.hEvent
) {
488 CloseHandle(npc
->ovl
.hEvent
);
494 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
496 /* FIXME: implement when named pipe writes use overlapped I/O */
499 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
501 /* FIXME: implement when named pipe writes use overlapped I/O */
505 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
506 const char *networkaddr
,
507 const char *endpoint
)
509 twr_empty_floor_t
*smb_floor
;
510 twr_empty_floor_t
*nb_floor
;
512 size_t networkaddr_size
;
513 size_t endpoint_size
;
515 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
517 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
518 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
519 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
524 smb_floor
= (twr_empty_floor_t
*)tower_data
;
526 tower_data
+= sizeof(*smb_floor
);
528 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
529 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
530 smb_floor
->count_rhs
= endpoint_size
;
533 memcpy(tower_data
, endpoint
, endpoint_size
);
536 tower_data
+= endpoint_size
;
538 nb_floor
= (twr_empty_floor_t
*)tower_data
;
540 tower_data
+= sizeof(*nb_floor
);
542 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
543 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
544 nb_floor
->count_rhs
= networkaddr_size
;
547 memcpy(tower_data
, networkaddr
, networkaddr_size
);
554 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
559 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
560 const twr_empty_floor_t
*nb_floor
;
562 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
564 if (tower_size
< sizeof(*smb_floor
))
565 return EPT_S_NOT_REGISTERED
;
567 tower_data
+= sizeof(*smb_floor
);
568 tower_size
-= sizeof(*smb_floor
);
570 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
571 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
572 (smb_floor
->count_rhs
> tower_size
) ||
573 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
574 return EPT_S_NOT_REGISTERED
;
578 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
580 return RPC_S_OUT_OF_RESOURCES
;
581 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
583 tower_data
+= smb_floor
->count_rhs
;
584 tower_size
-= smb_floor
->count_rhs
;
586 if (tower_size
< sizeof(*nb_floor
))
587 return EPT_S_NOT_REGISTERED
;
589 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
591 tower_data
+= sizeof(*nb_floor
);
592 tower_size
-= sizeof(*nb_floor
);
594 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
595 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
596 (nb_floor
->count_rhs
> tower_size
) ||
597 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
598 return EPT_S_NOT_REGISTERED
;
602 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
607 I_RpcFree(*endpoint
);
610 return RPC_S_OUT_OF_RESOURCES
;
612 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
618 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
620 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
623 TRACE("(%p)\n", conn
);
625 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
626 return RPCRT4_default_impersonate_client(conn
);
628 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
631 DWORD error
= GetLastError();
632 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
635 case ERROR_CANNOT_IMPERSONATE
:
636 return RPC_S_NO_CONTEXT_AVAILABLE
;
642 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
646 TRACE("(%p)\n", conn
);
648 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
649 return RPCRT4_default_revert_to_self(conn
);
651 ret
= RevertToSelf();
654 WARN("RevertToSelf failed with error %u\n", GetLastError());
655 return RPC_S_NO_CONTEXT_AVAILABLE
;
660 typedef struct _RpcServerProtseq_np
662 RpcServerProtseq common
;
664 } RpcServerProtseq_np
;
666 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
668 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
670 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
674 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
676 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
677 SetEvent(npps
->mgr_event
);
680 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
682 HANDLE
*objs
= prev_array
;
683 RpcConnection_np
*conn
;
684 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
686 EnterCriticalSection(&protseq
->cs
);
688 /* open and count connections */
690 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
692 rpcrt4_conn_listen_pipe(conn
);
693 if (conn
->ovl
.hEvent
)
695 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
698 /* make array of connections */
700 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
702 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
705 ERR("couldn't allocate objs\n");
706 LeaveCriticalSection(&protseq
->cs
);
710 objs
[0] = npps
->mgr_event
;
712 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
714 if ((objs
[*count
] = conn
->ovl
.hEvent
))
716 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
718 LeaveCriticalSection(&protseq
->cs
);
722 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
724 HeapFree(GetProcessHeap(), 0, array
);
727 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
730 HANDLE
*objs
= wait_array
;
732 RpcConnection
*cconn
;
733 RpcConnection_np
*conn
;
740 /* an alertable wait isn't strictly necessary, but due to our
741 * overlapped I/O implementation in Wine we need to free some memory
742 * by the file user APC being called, even if no completion routine was
743 * specified at the time of starting the async operation */
744 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
745 } while (res
== WAIT_IO_COMPLETION
);
747 if (res
== WAIT_OBJECT_0
)
749 else if (res
== WAIT_FAILED
)
751 ERR("wait failed with error %d\n", GetLastError());
756 b_handle
= objs
[res
- WAIT_OBJECT_0
];
757 /* find which connection got a RPC */
758 EnterCriticalSection(&protseq
->cs
);
759 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
761 if (b_handle
== conn
->ovl
.hEvent
) break;
762 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
766 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
768 ERR("failed to locate connection for handle %p\n", b_handle
);
769 LeaveCriticalSection(&protseq
->cs
);
772 RPCRT4_new_client(cconn
);
779 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
780 const char *networkaddr
,
781 const char *endpoint
)
783 twr_empty_floor_t
*pipe_floor
;
785 size_t endpoint_size
;
787 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
789 endpoint_size
= strlen(endpoint
) + 1;
790 size
= sizeof(*pipe_floor
) + endpoint_size
;
795 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
797 tower_data
+= sizeof(*pipe_floor
);
799 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
800 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
801 pipe_floor
->count_rhs
= endpoint_size
;
803 memcpy(tower_data
, endpoint
, endpoint_size
);
808 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
813 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
815 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
817 if (tower_size
< sizeof(*pipe_floor
))
818 return EPT_S_NOT_REGISTERED
;
820 tower_data
+= sizeof(*pipe_floor
);
821 tower_size
-= sizeof(*pipe_floor
);
823 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
824 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
825 (pipe_floor
->count_rhs
> tower_size
) ||
826 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
827 return EPT_S_NOT_REGISTERED
;
834 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
836 return RPC_S_OUT_OF_RESOURCES
;
837 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
843 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
848 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
849 unsigned char *in_buffer
,
850 unsigned int in_size
,
851 unsigned char *out_buffer
,
852 unsigned int *out_size
)
854 /* since this protocol is local to the machine there is no need to
855 * authenticate the caller */
860 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
861 enum secure_packet_direction dir
,
862 RpcPktHdr
*hdr
, unsigned int hdr_size
,
863 unsigned char *stub_data
, unsigned int stub_data_size
,
864 RpcAuthVerifier
*auth_hdr
,
865 unsigned char *auth_value
, unsigned int auth_value_size
)
867 /* since this protocol is local to the machine there is no need to secure
872 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
873 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
874 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
876 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
877 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
881 FIXME("privs not implemented\n");
884 if (server_princ_name
)
886 FIXME("server_princ_name not implemented\n");
887 *server_princ_name
= NULL
;
889 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
890 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
893 FIXME("authorization service not implemented\n");
894 *authz_svc
= RPC_C_AUTHZ_NONE
;
897 FIXME("flags 0x%x not implemented\n", flags
);
902 /**** ncacn_ip_tcp support ****/
904 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
905 const char *networkaddr
,
906 unsigned char tcp_protid
,
907 const char *endpoint
)
909 twr_tcp_floor_t
*tcp_floor
;
910 twr_ipv4_floor_t
*ipv4_floor
;
912 struct addrinfo hints
;
914 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
916 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
921 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
922 tower_data
+= sizeof(*tcp_floor
);
924 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
926 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
927 tcp_floor
->protid
= tcp_protid
;
928 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
930 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
931 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
932 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
934 hints
.ai_flags
= AI_NUMERICHOST
;
935 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
936 hints
.ai_family
= PF_INET
;
937 hints
.ai_socktype
= SOCK_STREAM
;
938 hints
.ai_protocol
= IPPROTO_TCP
;
939 hints
.ai_addrlen
= 0;
940 hints
.ai_addr
= NULL
;
941 hints
.ai_canonname
= NULL
;
942 hints
.ai_next
= NULL
;
944 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
947 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
950 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
955 if (ai
->ai_family
== PF_INET
)
957 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
958 tcp_floor
->port
= sin
->sin_port
;
959 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
963 ERR("unexpected protocol family %d\n", ai
->ai_family
);
972 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
975 unsigned char tcp_protid
,
978 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
979 const twr_ipv4_floor_t
*ipv4_floor
;
980 struct in_addr in_addr
;
982 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
984 if (tower_size
< sizeof(*tcp_floor
))
985 return EPT_S_NOT_REGISTERED
;
987 tower_data
+= sizeof(*tcp_floor
);
988 tower_size
-= sizeof(*tcp_floor
);
990 if (tower_size
< sizeof(*ipv4_floor
))
991 return EPT_S_NOT_REGISTERED
;
993 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
995 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
996 (tcp_floor
->protid
!= tcp_protid
) ||
997 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
998 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
999 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1000 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1001 return EPT_S_NOT_REGISTERED
;
1005 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1007 return RPC_S_OUT_OF_RESOURCES
;
1008 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1013 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1018 I_RpcFree(*endpoint
);
1021 return RPC_S_OUT_OF_RESOURCES
;
1023 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1024 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1026 ERR("inet_ntop: %s\n", strerror(errno
));
1027 I_RpcFree(*networkaddr
);
1028 *networkaddr
= NULL
;
1031 I_RpcFree(*endpoint
);
1034 return EPT_S_NOT_REGISTERED
;
1041 typedef struct _RpcConnection_tcp
1043 RpcConnection common
;
1045 #ifdef HAVE_SOCKETPAIR
1049 HANDLE cancel_event
;
1051 } RpcConnection_tcp
;
1053 #ifdef HAVE_SOCKETPAIR
1055 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1057 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1059 ERR("socketpair() failed: %s\n", strerror(errno
));
1065 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1067 struct pollfd pfds
[2];
1068 pfds
[0].fd
= tcpc
->sock
;
1069 pfds
[0].events
= POLLIN
;
1070 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1071 pfds
[1].events
= POLLIN
;
1072 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1074 ERR("poll() failed: %s\n", strerror(errno
));
1077 if (pfds
[1].revents
& POLLIN
) /* canceled */
1080 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1086 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1089 pfd
.fd
= tcpc
->sock
;
1090 pfd
.events
= POLLOUT
;
1091 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1093 ERR("poll() failed: %s\n", strerror(errno
));
1099 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1103 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1106 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1108 close(tcpc
->cancel_fds
[0]);
1109 close(tcpc
->cancel_fds
[1]);
1112 #else /* HAVE_SOCKETPAIR */
1114 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1116 static BOOL wsa_inited
;
1120 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1121 /* Note: WSAStartup can be called more than once so we don't bother with
1122 * making accesses to wsa_inited thread-safe */
1125 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1126 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1127 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1129 ERR("event creation failed\n");
1130 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1136 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1138 HANDLE wait_handles
[2];
1140 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1142 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1145 wait_handles
[0] = tcpc
->sock_event
;
1146 wait_handles
[1] = tcpc
->cancel_event
;
1147 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1152 case WAIT_OBJECT_0
+ 1:
1155 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1160 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1163 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1165 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1168 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1174 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1179 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1181 SetEvent(tcpc
->cancel_event
);
1184 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1186 CloseHandle(tcpc
->sock_event
);
1187 CloseHandle(tcpc
->cancel_event
);
1192 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1194 RpcConnection_tcp
*tcpc
;
1195 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1199 if (!rpcrt4_sock_wait_init(tcpc
))
1201 HeapFree(GetProcessHeap(), 0, tcpc
);
1204 return &tcpc
->common
;
1207 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1209 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1212 struct addrinfo
*ai
;
1213 struct addrinfo
*ai_cur
;
1214 struct addrinfo hints
;
1216 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1218 if (tcpc
->sock
!= -1)
1222 hints
.ai_family
= PF_UNSPEC
;
1223 hints
.ai_socktype
= SOCK_STREAM
;
1224 hints
.ai_protocol
= IPPROTO_TCP
;
1225 hints
.ai_addrlen
= 0;
1226 hints
.ai_addr
= NULL
;
1227 hints
.ai_canonname
= NULL
;
1228 hints
.ai_next
= NULL
;
1230 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1233 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1234 Connection
->Endpoint
, gai_strerror(ret
));
1235 return RPC_S_SERVER_UNAVAILABLE
;
1238 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1243 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1245 TRACE("skipping non-IP/IPv6 address family\n");
1253 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1254 host
, sizeof(host
), service
, sizeof(service
),
1255 NI_NUMERICHOST
| NI_NUMERICSERV
);
1256 TRACE("trying %s:%s\n", host
, service
);
1259 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1262 WARN("socket() failed: %s\n", strerror(errno
));
1266 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1268 WARN("connect() failed: %s\n", strerror(errno
));
1273 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1275 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1277 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1282 TRACE("connected\n");
1287 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1288 return RPC_S_SERVER_UNAVAILABLE
;
1291 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1293 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1296 struct addrinfo
*ai
;
1297 struct addrinfo
*ai_cur
;
1298 struct addrinfo hints
;
1299 RpcConnection
*first_connection
= NULL
;
1301 TRACE("(%p, %s)\n", protseq
, endpoint
);
1303 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1304 hints
.ai_family
= PF_UNSPEC
;
1305 hints
.ai_socktype
= SOCK_STREAM
;
1306 hints
.ai_protocol
= IPPROTO_TCP
;
1307 hints
.ai_addrlen
= 0;
1308 hints
.ai_addr
= NULL
;
1309 hints
.ai_canonname
= NULL
;
1310 hints
.ai_next
= NULL
;
1312 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1315 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1317 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1318 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1319 return RPC_S_CANT_CREATE_ENDPOINT
;
1322 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1324 RpcConnection_tcp
*tcpc
;
1325 RPC_STATUS create_status
;
1326 struct sockaddr_storage sa
;
1328 char service
[NI_MAXSERV
];
1331 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1333 TRACE("skipping non-IP/IPv6 address family\n");
1340 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1341 host
, sizeof(host
), service
, sizeof(service
),
1342 NI_NUMERICHOST
| NI_NUMERICSERV
);
1343 TRACE("trying %s:%s\n", host
, service
);
1346 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1349 WARN("socket() failed: %s\n", strerror(errno
));
1350 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1354 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1357 WARN("bind failed: %s\n", strerror(errno
));
1359 if (errno
== EADDRINUSE
)
1360 status
= RPC_S_DUPLICATE_ENDPOINT
;
1362 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1366 sa_len
= sizeof(sa
);
1367 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1369 WARN("getsockname() failed: %s\n", strerror(errno
));
1371 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1375 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1376 NULL
, 0, service
, sizeof(service
),
1380 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1382 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1386 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1387 protseq
->Protseq
, NULL
,
1388 service
, NULL
, NULL
, NULL
, NULL
);
1389 if (create_status
!= RPC_S_OK
)
1392 status
= create_status
;
1397 ret
= listen(sock
, protseq
->MaxCalls
);
1400 WARN("listen failed: %s\n", strerror(errno
));
1401 RPCRT4_ReleaseConnection(&tcpc
->common
);
1402 status
= RPC_S_OUT_OF_RESOURCES
;
1405 /* need a non-blocking socket, otherwise accept() has a potential
1406 * race-condition (poll() says it is readable, connection drops,
1407 * and accept() blocks until the next connection comes...)
1410 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1413 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1414 RPCRT4_ReleaseConnection(&tcpc
->common
);
1415 status
= RPC_S_OUT_OF_RESOURCES
;
1419 tcpc
->common
.Next
= first_connection
;
1420 first_connection
= &tcpc
->common
;
1422 /* since IPv4 and IPv6 share the same port space, we only need one
1423 * successful bind to listen for both */
1429 /* if at least one connection was created for an endpoint then
1431 if (first_connection
)
1433 RpcConnection
*conn
;
1435 /* find last element in list */
1436 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1439 EnterCriticalSection(&protseq
->cs
);
1440 conn
->Next
= protseq
->conn
;
1441 protseq
->conn
= first_connection
;
1442 LeaveCriticalSection(&protseq
->cs
);
1444 TRACE("listening on %s\n", endpoint
);
1448 ERR("couldn't listen on port %s\n", endpoint
);
1452 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1455 struct sockaddr_in address
;
1457 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1458 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1461 addrsize
= sizeof(address
);
1462 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1465 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1466 return RPC_S_OUT_OF_RESOURCES
;
1469 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1471 TRACE("Accepted a new TCP connection\n");
1475 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1476 void *buffer
, unsigned int count
)
1478 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1480 while (bytes_read
!= count
)
1482 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1487 else if (errno
!= EAGAIN
)
1489 WARN("recv() failed: %s\n", strerror(errno
));
1494 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1498 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1502 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1503 const void *buffer
, unsigned int count
)
1505 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1506 int bytes_written
= 0;
1507 while (bytes_written
!= count
)
1509 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1512 else if (errno
!= EAGAIN
)
1516 if (!rpcrt4_sock_wait_for_send(tcpc
))
1520 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1521 return bytes_written
;
1524 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1526 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1528 TRACE("%d\n", tcpc
->sock
);
1530 if (tcpc
->sock
!= -1)
1531 closesocket(tcpc
->sock
);
1533 rpcrt4_sock_wait_destroy(tcpc
);
1537 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1539 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1540 TRACE("%p\n", Connection
);
1541 rpcrt4_sock_wait_cancel(tcpc
);
1544 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1546 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1548 TRACE("%p\n", Connection
);
1550 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1555 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1556 const char *networkaddr
,
1557 const char *endpoint
)
1559 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1560 EPM_PROTOCOL_TCP
, endpoint
);
1563 #ifdef HAVE_SOCKETPAIR
1565 typedef struct _RpcServerProtseq_sock
1567 RpcServerProtseq common
;
1570 } RpcServerProtseq_sock
;
1572 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1574 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1578 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1580 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1581 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1582 ps
->mgr_event_rcv
= fds
[0];
1583 ps
->mgr_event_snd
= fds
[1];
1587 ERR("socketpair failed with error %s\n", strerror(errno
));
1588 HeapFree(GetProcessHeap(), 0, ps
);
1595 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1597 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1599 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1602 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1604 struct pollfd
*poll_info
= prev_array
;
1605 RpcConnection_tcp
*conn
;
1606 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1608 EnterCriticalSection(&protseq
->cs
);
1610 /* open and count connections */
1612 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1614 if (conn
->sock
!= -1)
1616 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1619 /* make array of connections */
1621 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1623 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1626 ERR("couldn't allocate poll_info\n");
1627 LeaveCriticalSection(&protseq
->cs
);
1631 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1632 poll_info
[0].events
= POLLIN
;
1634 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1636 if (conn
->sock
!= -1)
1638 poll_info
[*count
].fd
= conn
->sock
;
1639 poll_info
[*count
].events
= POLLIN
;
1642 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1644 LeaveCriticalSection(&protseq
->cs
);
1648 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1650 HeapFree(GetProcessHeap(), 0, array
);
1653 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1655 struct pollfd
*poll_info
= wait_array
;
1658 RpcConnection
*cconn
;
1659 RpcConnection_tcp
*conn
;
1664 ret
= poll(poll_info
, count
, -1);
1667 ERR("poll failed with error %d\n", ret
);
1671 for (i
= 0; i
< count
; i
++)
1672 if (poll_info
[i
].revents
& POLLIN
)
1674 /* RPC server event */
1678 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1682 /* find which connection got a RPC */
1683 EnterCriticalSection(&protseq
->cs
);
1684 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1686 if (poll_info
[i
].fd
== conn
->sock
) break;
1687 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1691 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1693 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1694 LeaveCriticalSection(&protseq
->cs
);
1696 RPCRT4_new_client(cconn
);
1704 #else /* HAVE_SOCKETPAIR */
1706 typedef struct _RpcServerProtseq_sock
1708 RpcServerProtseq common
;
1710 } RpcServerProtseq_sock
;
1712 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1714 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1717 static BOOL wsa_inited
;
1721 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1722 /* Note: WSAStartup can be called more than once so we don't bother with
1723 * making accesses to wsa_inited thread-safe */
1726 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1731 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1733 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1734 SetEvent(sockps
->mgr_event
);
1737 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1739 HANDLE
*objs
= prev_array
;
1740 RpcConnection_tcp
*conn
;
1741 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1743 EnterCriticalSection(&protseq
->cs
);
1745 /* open and count connections */
1747 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1750 if (conn
->sock
!= -1)
1752 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1755 /* make array of connections */
1757 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1759 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1762 ERR("couldn't allocate objs\n");
1763 LeaveCriticalSection(&protseq
->cs
);
1767 objs
[0] = sockps
->mgr_event
;
1769 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1772 if (conn
->sock
!= -1)
1774 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1775 if (res
== SOCKET_ERROR
)
1776 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1779 objs
[*count
] = conn
->sock_event
;
1783 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1785 LeaveCriticalSection(&protseq
->cs
);
1789 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1791 HeapFree(GetProcessHeap(), 0, array
);
1794 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1797 HANDLE
*objs
= wait_array
;
1799 RpcConnection
*cconn
;
1800 RpcConnection_tcp
*conn
;
1807 /* an alertable wait isn't strictly necessary, but due to our
1808 * overlapped I/O implementation in Wine we need to free some memory
1809 * by the file user APC being called, even if no completion routine was
1810 * specified at the time of starting the async operation */
1811 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1812 } while (res
== WAIT_IO_COMPLETION
);
1814 if (res
== WAIT_OBJECT_0
)
1816 else if (res
== WAIT_FAILED
)
1818 ERR("wait failed with error %d\n", GetLastError());
1823 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1824 /* find which connection got a RPC */
1825 EnterCriticalSection(&protseq
->cs
);
1826 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1829 if (b_handle
== conn
->sock_event
) break;
1830 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1834 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1836 ERR("failed to locate connection for handle %p\n", b_handle
);
1837 LeaveCriticalSection(&protseq
->cs
);
1840 RPCRT4_new_client(cconn
);
1847 #endif /* HAVE_SOCKETPAIR */
1849 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1854 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1855 networkaddr
, EPM_PROTOCOL_TCP
,
1859 /**** ncacn_http support ****/
1861 /* 60 seconds is the period native uses */
1862 #define HTTP_IDLE_TIME 60000
1864 /* reference counted to avoid a race between a cancelled call's connection
1865 * being destroyed and the asynchronous InternetReadFileEx call being
1867 typedef struct _RpcHttpAsyncData
1870 HANDLE completion_event
;
1872 INTERNET_BUFFERSA inet_buffers
;
1873 CRITICAL_SECTION cs
;
1876 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1878 return InterlockedIncrement(&data
->refs
);
1881 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1883 ULONG refs
= InterlockedDecrement(&data
->refs
);
1886 TRACE("destroying async data %p\n", data
);
1887 CloseHandle(data
->completion_event
);
1888 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1889 data
->cs
.DebugInfo
->Spare
[0] = 0;
1890 DeleteCriticalSection(&data
->cs
);
1891 HeapFree(GetProcessHeap(), 0, data
);
1896 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
1898 ResetEvent(async_data
->completion_event
);
1899 RpcHttpAsyncData_AddRef(async_data
);
1902 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
1904 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
1908 RpcHttpAsyncData_Release(async_data
);
1912 if(GetLastError() != ERROR_IO_PENDING
) {
1913 RpcHttpAsyncData_Release(async_data
);
1914 ERR("Request failed with error %d\n", GetLastError());
1915 return RPC_S_SERVER_UNAVAILABLE
;
1918 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
1919 if(res
!= WAIT_OBJECT_0
) {
1920 TRACE("Cancelled\n");
1921 return RPC_S_CALL_CANCELLED
;
1924 if(async_data
->async_result
) {
1925 ERR("Async request failed with error %d\n", async_data
->async_result
);
1926 return RPC_S_SERVER_UNAVAILABLE
;
1941 unsigned int data_len
;
1942 BOOL finished
; /* finished authenticating */
1945 typedef struct _RpcConnection_http
1947 RpcConnection common
;
1950 HINTERNET in_request
;
1951 HINTERNET out_request
;
1953 HANDLE timer_cancelled
;
1954 HANDLE cancel_event
;
1955 DWORD last_sent_time
;
1956 ULONG bytes_received
;
1957 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1958 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1959 UUID connection_uuid
;
1962 RpcHttpAsyncData
*async_data
;
1963 } RpcConnection_http
;
1965 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1967 RpcConnection_http
*httpc
;
1968 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1969 if (!httpc
) return NULL
;
1970 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1971 if (!httpc
->async_data
)
1973 HeapFree(GetProcessHeap(), 0, httpc
);
1976 TRACE("async data = %p\n", httpc
->async_data
);
1977 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1978 httpc
->async_data
->refs
= 1;
1979 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1980 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1981 InitializeCriticalSection(&httpc
->async_data
->cs
);
1982 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
1983 return &httpc
->common
;
1986 typedef struct _HttpTimerThreadData
1989 DWORD
*last_sent_time
;
1990 HANDLE timer_cancelled
;
1991 } HttpTimerThreadData
;
1993 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1995 HINTERNET in_request
= param
;
1996 RpcPktHdr
*idle_pkt
;
1998 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
2002 DWORD bytes_written
;
2003 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
2004 RPCRT4_FreeHeader(idle_pkt
);
2008 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
2010 DWORD cur_time
= GetTickCount();
2011 DWORD cached_last_sent_time
= *last_sent_time
;
2012 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2015 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2017 HttpTimerThreadData
*data_in
= param
;
2018 HttpTimerThreadData data
;
2022 HeapFree(GetProcessHeap(), 0, data_in
);
2024 for (timeout
= HTTP_IDLE_TIME
;
2025 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2026 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2028 /* are we too soon after last send? */
2029 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2031 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2034 CloseHandle(data
.timer_cancelled
);
2038 static VOID WINAPI
rpcrt4_http_internet_callback(
2039 HINTERNET hInternet
,
2040 DWORD_PTR dwContext
,
2041 DWORD dwInternetStatus
,
2042 LPVOID lpvStatusInformation
,
2043 DWORD dwStatusInformationLength
)
2045 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2047 switch (dwInternetStatus
)
2049 case INTERNET_STATUS_REQUEST_COMPLETE
:
2050 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2053 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2055 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2056 SetEvent(async_data
->completion_event
);
2057 RpcHttpAsyncData_Release(async_data
);
2063 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2070 WCHAR
*status_text
= buf
;
2074 size
= sizeof(status_code
);
2075 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2077 return GetLastError();
2078 if (status_code
== HTTP_STATUS_OK
)
2082 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2083 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2085 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2086 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2089 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2090 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2092 if (status_code
== HTTP_STATUS_DENIED
)
2093 return ERROR_ACCESS_DENIED
;
2094 return RPC_S_SERVER_UNAVAILABLE
;
2097 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2099 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2100 LPWSTR proxy
= NULL
;
2102 LPWSTR password
= NULL
;
2103 LPWSTR servername
= NULL
;
2104 const WCHAR
*option
;
2107 if (httpc
->common
.QOS
&&
2108 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2110 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2111 if (http_cred
->TransportCredentials
)
2114 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2115 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2116 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2118 return RPC_S_OUT_OF_RESOURCES
;
2120 if (cred
->DomainLength
)
2122 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2123 p
+= cred
->DomainLength
;
2127 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2128 p
[cred
->UserLength
] = 0;
2130 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2134 for (option
= httpc
->common
.NetworkOptions
; option
;
2135 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2137 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2138 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2140 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2142 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2143 const WCHAR
*value_end
;
2146 value_end
= strchrW(option
, ',');
2148 value_end
= value_start
+ strlenW(value_start
);
2149 for (p
= value_start
; p
< value_end
; p
++)
2156 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2157 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2159 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2161 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2162 const WCHAR
*value_end
;
2164 value_end
= strchrW(option
, ',');
2166 value_end
= value_start
+ strlenW(value_start
);
2167 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2168 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2171 FIXME("unhandled option %s\n", debugstr_w(option
));
2174 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2175 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2176 if (!httpc
->app_info
)
2178 HeapFree(GetProcessHeap(), 0, password
);
2179 HeapFree(GetProcessHeap(), 0, user
);
2180 HeapFree(GetProcessHeap(), 0, proxy
);
2181 HeapFree(GetProcessHeap(), 0, servername
);
2182 ERR("InternetOpenW failed with error %d\n", GetLastError());
2183 return RPC_S_SERVER_UNAVAILABLE
;
2185 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2187 /* if no RpcProxy option specified, set the HTTP server address to the
2188 * RPC server address */
2191 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2194 HeapFree(GetProcessHeap(), 0, password
);
2195 HeapFree(GetProcessHeap(), 0, user
);
2196 HeapFree(GetProcessHeap(), 0, proxy
);
2197 return RPC_S_OUT_OF_RESOURCES
;
2199 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2202 port
= (httpc
->common
.QOS
&&
2203 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2204 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2205 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2207 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2208 INTERNET_SERVICE_HTTP
, 0, 0);
2210 HeapFree(GetProcessHeap(), 0, password
);
2211 HeapFree(GetProcessHeap(), 0, user
);
2212 HeapFree(GetProcessHeap(), 0, proxy
);
2214 if (!httpc
->session
)
2216 ERR("InternetConnectW failed with error %d\n", GetLastError());
2217 HeapFree(GetProcessHeap(), 0, servername
);
2218 return RPC_S_SERVER_UNAVAILABLE
;
2220 httpc
->servername
= servername
;
2224 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2231 TRACE("sending echo request to server\n");
2233 prepare_async_request(async_data
);
2234 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2235 status
= wait_async_request(async_data
, ret
, cancel_event
);
2236 if (status
!= RPC_S_OK
) return status
;
2238 status
= rpcrt4_http_check_response(req
);
2239 if (status
!= RPC_S_OK
) return status
;
2241 InternetReadFile(req
, buf
, sizeof(buf
), &bytes_read
);
2242 /* FIXME: do something with retrieved data */
2247 /* prepare the in pipe for use by RPC packets */
2248 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2249 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2250 const UUID
*association_uuid
, BOOL authorized
)
2255 INTERNET_BUFFERSW buffers_in
;
2256 DWORD bytes_written
;
2260 /* ask wininet to authorize, if necessary */
2261 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2262 if (status
!= RPC_S_OK
) return status
;
2264 memset(&buffers_in
, 0, sizeof(buffers_in
));
2265 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2266 /* FIXME: get this from the registry */
2267 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2268 prepare_async_request(async_data
);
2269 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2270 status
= wait_async_request(async_data
, ret
, cancel_event
);
2271 if (status
!= RPC_S_OK
) return status
;
2273 TRACE("sending HTTP connect header to server\n");
2274 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2275 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2276 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2277 RPCRT4_FreeHeader(hdr
);
2280 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2281 return RPC_S_SERVER_UNAVAILABLE
;
2287 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2291 unsigned short data_len
;
2293 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2295 return RPC_S_SERVER_UNAVAILABLE
;
2296 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2298 ERR("wrong packet type received %d or wrong frag_len %d\n",
2299 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2300 return RPC_S_PROTOCOL_ERROR
;
2303 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2305 return RPC_S_SERVER_UNAVAILABLE
;
2307 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2310 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2312 return RPC_S_OUT_OF_RESOURCES
;
2313 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2316 HeapFree(GetProcessHeap(), 0, *data
);
2317 return RPC_S_SERVER_UNAVAILABLE
;
2323 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2325 ERR("invalid http packet\n");
2326 return RPC_S_PROTOCOL_ERROR
;
2332 /* prepare the out pipe for use by RPC packets */
2333 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2334 HANDLE cancel_event
, const UUID
*connection_uuid
,
2335 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2341 BYTE
*data_from_server
;
2342 RpcPktHdr pkt_from_server
;
2343 ULONG field1
, field3
;
2349 /* ask wininet to authorize, if necessary */
2350 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2351 if (status
!= RPC_S_OK
) return status
;
2354 InternetReadFile(out_request
, buf
, sizeof(buf
), &bytes_read
);
2356 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2357 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2359 TRACE("sending HTTP connect header to server\n");
2360 prepare_async_request(async_data
);
2361 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2362 status
= wait_async_request(async_data
, ret
, cancel_event
);
2363 RPCRT4_FreeHeader(hdr
);
2364 if (status
!= RPC_S_OK
) return status
;
2366 status
= rpcrt4_http_check_response(out_request
);
2367 if (status
!= RPC_S_OK
) return status
;
2369 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2371 if (status
!= RPC_S_OK
) return status
;
2372 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2374 HeapFree(GetProcessHeap(), 0, data_from_server
);
2375 if (status
!= RPC_S_OK
) return status
;
2376 TRACE("received (%d) from first prepare header\n", field1
);
2380 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2382 if (status
!= RPC_S_OK
) return status
;
2383 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2385 TRACE("http idle packet, waiting for real packet\n");
2386 HeapFree(GetProcessHeap(), 0, data_from_server
);
2387 if (pkt_from_server
.http
.num_data_items
!= 0)
2389 ERR("HTTP idle packet should have no data items instead of %d\n",
2390 pkt_from_server
.http
.num_data_items
);
2391 return RPC_S_PROTOCOL_ERROR
;
2394 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2395 &field1
, flow_control_increment
,
2397 HeapFree(GetProcessHeap(), 0, data_from_server
);
2398 if (status
!= RPC_S_OK
) return status
;
2399 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2404 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2406 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2411 /* first 6 bits, all from bin[0] */
2412 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2413 x
= (bin
[0] & 3) << 4;
2415 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2418 base64
[i
++] = enc
[x
];
2423 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2424 x
= (bin
[1] & 0x0f) << 2;
2426 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2429 base64
[i
++] = enc
[x
];
2433 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2435 /* last 6 bits, all from bin [2] */
2436 base64
[i
++] = enc
[bin
[2] & 0x3f];
2444 static inline char decode_char( WCHAR c
)
2446 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2447 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2448 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2449 if (c
== '+') return 62;
2450 if (c
== '/') return 63;
2454 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2457 char c0
, c1
, c2
, c3
;
2458 const WCHAR
*p
= base64
;
2462 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2463 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2464 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2465 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2469 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2470 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2471 buf
[i
+ 2] = (c2
<< 6) | c3
;
2479 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2480 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2482 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2485 else if (p
[3] == '=')
2487 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2488 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2489 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2493 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2494 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2500 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2501 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2502 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2503 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2507 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2508 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2509 buf
[i
+ 2] = (c2
<< 6) | c3
;
2516 static struct authinfo
*alloc_authinfo(void)
2518 struct authinfo
*ret
;
2520 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2522 SecInvalidateHandle(&ret
->cred
);
2523 SecInvalidateHandle(&ret
->ctx
);
2524 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2530 ret
->finished
= FALSE
;
2534 static void destroy_authinfo(struct authinfo
*info
)
2538 if (SecIsValidHandle(&info
->ctx
))
2539 DeleteSecurityContext(&info
->ctx
);
2540 if (SecIsValidHandle(&info
->cred
))
2541 FreeCredentialsHandle(&info
->cred
);
2543 HeapFree(GetProcessHeap(), 0, info
->data
);
2544 HeapFree(GetProcessHeap(), 0, info
);
2547 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2548 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2549 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2550 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2551 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2561 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2562 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2563 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2564 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2565 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2567 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2569 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2572 for (i
= 0; i
< num_auth_schemes
; i
++)
2574 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2575 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2580 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2582 DWORD len
, index
= 0;
2586 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2587 if (auth_scheme_from_header(buffer
) == scheme
) break;
2592 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2593 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2595 struct authinfo
*info
= *auth_ptr
;
2596 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2597 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2599 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2601 switch (creds
->AuthnSchemes
[0])
2603 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2605 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2606 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2608 info
->data_len
= userlen
+ passlen
+ 1;
2609 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2611 status
= RPC_S_OUT_OF_MEMORY
;
2614 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2615 info
->data
[userlen
] = ':';
2616 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2618 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2619 info
->finished
= TRUE
;
2623 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2624 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2627 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2628 SECURITY_STATUS ret
;
2629 SecBufferDesc out_desc
, in_desc
;
2631 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2635 WCHAR auth_value
[2048];
2636 DWORD size
= sizeof(auth_value
);
2639 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2640 else scheme
= negotiateW
;
2641 scheme_len
= strlenW( scheme
);
2646 SecPkgInfoW
*pkg_info
;
2648 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2649 if (ret
!= SEC_E_OK
) break;
2651 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2652 if (ret
!= SEC_E_OK
) break;
2654 info
->max_token
= pkg_info
->cbMaxToken
;
2655 FreeContextBuffer(pkg_info
);
2660 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2661 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2663 ERR("authentication scheme changed\n");
2667 in
.BufferType
= SECBUFFER_TOKEN
;
2671 in_desc
.ulVersion
= 0;
2672 in_desc
.cBuffers
= 1;
2673 in_desc
.pBuffers
= &in
;
2675 p
= auth_value
+ scheme_len
;
2676 if (!first
&& *p
== ' ')
2678 int len
= strlenW(++p
);
2679 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2680 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2681 decode_base64(p
, len
, in
.pvBuffer
);
2683 out
.BufferType
= SECBUFFER_TOKEN
;
2684 out
.cbBuffer
= info
->max_token
;
2685 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2687 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2690 out_desc
.ulVersion
= 0;
2691 out_desc
.cBuffers
= 1;
2692 out_desc
.pBuffers
= &out
;
2694 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2695 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2696 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2697 &info
->attr
, &info
->exp
);
2698 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2699 if (ret
== SEC_E_OK
)
2701 HeapFree(GetProcessHeap(), 0, info
->data
);
2702 info
->data
= out
.pvBuffer
;
2703 info
->data_len
= out
.cbBuffer
;
2704 info
->finished
= TRUE
;
2705 TRACE("sending last auth packet\n");
2708 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2710 HeapFree(GetProcessHeap(), 0, info
->data
);
2711 info
->data
= out
.pvBuffer
;
2712 info
->data_len
= out
.cbBuffer
;
2713 TRACE("sending next auth packet\n");
2718 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2719 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2722 info
->scheme
= creds
->AuthnSchemes
[0];
2726 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2730 if (status
!= RPC_S_OK
)
2732 destroy_authinfo(info
);
2740 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2742 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2743 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2744 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2745 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2746 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2747 const WCHAR
*scheme_str
;
2748 WCHAR
*header
, *ptr
;
2749 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2753 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2754 scheme_str
= basicW
;
2755 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2757 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2758 scheme_str
= negotiateW
;
2759 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2761 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2763 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2766 ERR("unknown scheme %u\n", scheme
);
2767 return RPC_S_SERVER_UNAVAILABLE
;
2769 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2771 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2772 ptr
= header
+ auth_len
;
2773 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2775 len
= encode_base64(data
, data_len
, ptr
);
2779 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2781 HeapFree(GetProcessHeap(), 0, header
);
2786 static void drain_content(HINTERNET request
)
2788 DWORD count
, len
= 0, size
= sizeof(len
);
2791 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2795 count
= min(sizeof(buf
), len
);
2796 if (!InternetReadFile(request
, buf
, count
, &count
) || !count
) return;
2801 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2803 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2804 struct authinfo
*info
= NULL
;
2810 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2811 if (status
!= RPC_S_OK
) break;
2813 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2814 if (status
!= RPC_S_OK
) break;
2816 prepare_async_request(httpc
->async_data
);
2817 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2818 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2819 if (status
!= RPC_S_OK
|| info
->finished
) break;
2821 status
= rpcrt4_http_check_response(request
);
2822 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
2823 drain_content(request
);
2826 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2827 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
);
2829 destroy_authinfo(info
);
2833 static RPC_STATUS
insert_cookie_header(HINTERNET request
, const WCHAR
*value
)
2835 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
2836 WCHAR
*header
, *ptr
;
2838 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2840 if (!value
) return RPC_S_OK
;
2842 len
= strlenW(value
);
2843 if ((header
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookieW
) + (len
+ 3) * sizeof(WCHAR
))))
2845 memcpy(header
, cookieW
, sizeof(cookieW
));
2846 ptr
= header
+ sizeof(cookieW
) / sizeof(cookieW
[0]);
2847 memcpy(ptr
, value
, len
* sizeof(WCHAR
));
2851 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW
))) status
= RPC_S_OK
;
2852 HeapFree(GetProcessHeap(), 0, header
);
2857 static BOOL
has_credentials(RpcConnection_http
*httpc
)
2859 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
2860 SEC_WINNT_AUTH_IDENTITY_W
*id
;
2862 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
2865 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2866 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
2869 id
= creds
->TransportCredentials
;
2870 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
2875 static BOOL
is_secure(RpcConnection_http
*httpc
)
2877 return httpc
->common
.QOS
&&
2878 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2879 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2882 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2884 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2885 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2886 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2887 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2888 static const WCHAR wszColon
[] = {':',0};
2889 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2890 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2894 BOOL secure
, credentials
;
2895 HttpTimerThreadData
*timer_data
;
2898 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2900 if (Connection
->server
)
2902 ERR("ncacn_http servers not supported yet\n");
2903 return RPC_S_SERVER_UNAVAILABLE
;
2906 if (httpc
->in_request
)
2909 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2911 status
= UuidCreate(&httpc
->connection_uuid
);
2912 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2913 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2915 status
= rpcrt4_http_internet_connect(httpc
);
2916 if (status
!= RPC_S_OK
)
2919 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2921 return RPC_S_OUT_OF_MEMORY
;
2922 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2923 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2924 strcatW(url
, wszColon
);
2925 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2927 secure
= is_secure(httpc
);
2928 credentials
= has_credentials(httpc
);
2930 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
2931 INTERNET_FLAG_NO_AUTO_REDIRECT
;
2932 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
2933 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
2935 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
2936 flags
, (DWORD_PTR
)httpc
->async_data
);
2937 if (!httpc
->in_request
)
2939 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2940 HeapFree(GetProcessHeap(), 0, url
);
2941 return RPC_S_SERVER_UNAVAILABLE
;
2943 status
= insert_cookie_header(httpc
->in_request
, Connection
->CookieAuth
);
2944 if (status
!= RPC_S_OK
)
2946 HeapFree(GetProcessHeap(), 0, url
);
2951 status
= authorize_request(httpc
, httpc
->in_request
);
2952 if (status
!= RPC_S_OK
)
2954 HeapFree(GetProcessHeap(), 0, url
);
2957 status
= rpcrt4_http_check_response(httpc
->in_request
);
2958 if (status
!= RPC_S_OK
)
2960 HeapFree(GetProcessHeap(), 0, url
);
2963 drain_content(httpc
->in_request
);
2966 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
2967 flags
, (DWORD_PTR
)httpc
->async_data
);
2968 HeapFree(GetProcessHeap(), 0, url
);
2969 if (!httpc
->out_request
)
2971 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2972 return RPC_S_SERVER_UNAVAILABLE
;
2974 status
= insert_cookie_header(httpc
->out_request
, Connection
->CookieAuth
);
2975 if (status
!= RPC_S_OK
)
2980 status
= authorize_request(httpc
, httpc
->out_request
);
2981 if (status
!= RPC_S_OK
)
2985 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
, httpc
->async_data
, httpc
->cancel_event
,
2986 &httpc
->connection_uuid
, &httpc
->in_pipe_uuid
,
2987 &Connection
->assoc
->http_uuid
, credentials
);
2988 if (status
!= RPC_S_OK
)
2991 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
, httpc
->async_data
, httpc
->cancel_event
,
2992 &httpc
->connection_uuid
, &httpc
->out_pipe_uuid
,
2993 &httpc
->flow_control_increment
, credentials
);
2994 if (status
!= RPC_S_OK
)
2997 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2998 httpc
->last_sent_time
= GetTickCount();
2999 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
3001 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
3003 return ERROR_OUTOFMEMORY
;
3004 timer_data
->timer_param
= httpc
->in_request
;
3005 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
3006 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
3007 /* FIXME: should use CreateTimerQueueTimer when implemented */
3008 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
3011 HeapFree(GetProcessHeap(), 0, timer_data
);
3012 return GetLastError();
3014 CloseHandle(thread
);
3019 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
3022 return RPC_S_SERVER_UNAVAILABLE
;
3025 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
3026 void *buffer
, unsigned int count
)
3028 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3031 unsigned int bytes_left
= count
;
3032 RPC_STATUS status
= RPC_S_OK
;
3034 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
3038 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
3039 prepare_async_request(httpc
->async_data
);
3040 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
3041 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3042 if(status
!= RPC_S_OK
) {
3043 if(status
== RPC_S_CALL_CANCELLED
)
3044 TRACE("call cancelled\n");
3048 if(!httpc
->async_data
->inet_buffers
.dwBufferLength
)
3050 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
3051 httpc
->async_data
->inet_buffers
.dwBufferLength
);
3053 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
3054 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
3057 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
3058 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
3060 TRACE("%p %p %u -> %u\n", httpc
->out_request
, buffer
, count
, status
);
3061 return status
== RPC_S_OK
? count
: -1;
3064 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
3066 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3070 RpcPktCommonHdr common_hdr
;
3074 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
3077 /* read packet common header */
3078 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
3079 if (dwRead
!= sizeof(common_hdr
)) {
3080 WARN("Short read of header, %d bytes\n", dwRead
);
3081 status
= RPC_S_PROTOCOL_ERROR
;
3084 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
3085 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
3087 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
3088 status
= RPC_S_PROTOCOL_ERROR
;
3092 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
3093 if (status
!= RPC_S_OK
) goto fail
;
3095 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
3096 if (hdr_length
== 0) {
3097 WARN("header length == 0\n");
3098 status
= RPC_S_PROTOCOL_ERROR
;
3102 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
3105 status
= RPC_S_OUT_OF_RESOURCES
;
3108 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
3110 /* read the rest of packet header */
3111 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
3112 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
3113 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
3114 status
= RPC_S_PROTOCOL_ERROR
;
3118 if (common_hdr
.frag_len
- hdr_length
)
3120 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
3123 status
= RPC_S_OUT_OF_RESOURCES
;
3127 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
3128 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
3130 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
3131 status
= RPC_S_PROTOCOL_ERROR
;
3138 if ((*Header
)->common
.ptype
== PKT_HTTP
)
3140 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
3142 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
3143 status
= RPC_S_PROTOCOL_ERROR
;
3146 if ((*Header
)->http
.flags
== 0x0001)
3148 TRACE("http idle packet, waiting for real packet\n");
3149 if ((*Header
)->http
.num_data_items
!= 0)
3151 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
3152 status
= RPC_S_PROTOCOL_ERROR
;
3156 else if ((*Header
)->http
.flags
== 0x0002)
3158 ULONG bytes_transmitted
;
3159 ULONG flow_control_increment
;
3161 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
3164 &flow_control_increment
,
3166 if (status
!= RPC_S_OK
)
3168 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
3169 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
3170 /* FIXME: do something with parsed data */
3174 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
3175 status
= RPC_S_PROTOCOL_ERROR
;
3178 RPCRT4_FreeHeader(*Header
);
3180 HeapFree(GetProcessHeap(), 0, *Payload
);
3188 httpc
->bytes_received
+= common_hdr
.frag_len
;
3190 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
3192 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
3194 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
3195 httpc
->bytes_received
,
3196 httpc
->flow_control_increment
,
3197 &httpc
->out_pipe_uuid
);
3200 DWORD bytes_written
;
3202 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
3203 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
3204 RPCRT4_FreeHeader(hdr
);
3206 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
3211 if (status
!= RPC_S_OK
) {
3212 RPCRT4_FreeHeader(*Header
);
3214 HeapFree(GetProcessHeap(), 0, *Payload
);
3220 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
3221 const void *buffer
, unsigned int count
)
3223 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3224 DWORD bytes_written
;
3227 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
3228 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
3229 httpc
->last_sent_time
= GetTickCount();
3230 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
3231 return ret
? bytes_written
: -1;
3234 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
3236 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3240 SetEvent(httpc
->timer_cancelled
);
3241 if (httpc
->in_request
)
3242 InternetCloseHandle(httpc
->in_request
);
3243 httpc
->in_request
= NULL
;
3244 if (httpc
->out_request
)
3245 InternetCloseHandle(httpc
->out_request
);
3246 httpc
->out_request
= NULL
;
3247 if (httpc
->app_info
)
3248 InternetCloseHandle(httpc
->app_info
);
3249 httpc
->app_info
= NULL
;
3251 InternetCloseHandle(httpc
->session
);
3252 httpc
->session
= NULL
;
3253 RpcHttpAsyncData_Release(httpc
->async_data
);
3254 if (httpc
->cancel_event
)
3255 CloseHandle(httpc
->cancel_event
);
3256 HeapFree(GetProcessHeap(), 0, httpc
->servername
);
3257 httpc
->servername
= NULL
;
3262 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
3264 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3266 SetEvent(httpc
->cancel_event
);
3269 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
3271 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
3275 prepare_async_request(httpc
->async_data
);
3276 ret
= InternetQueryDataAvailable(httpc
->out_request
,
3277 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
3278 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
3279 return status
== RPC_S_OK
? 0 : -1;
3282 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
3283 const char *networkaddr
,
3284 const char *endpoint
)
3286 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
3287 EPM_PROTOCOL_HTTP
, endpoint
);
3290 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
3295 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
3296 networkaddr
, EPM_PROTOCOL_HTTP
,
3300 static const struct connection_ops conn_protseq_list
[] = {
3302 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
3303 rpcrt4_conn_np_alloc
,
3304 rpcrt4_ncacn_np_open
,
3305 rpcrt4_ncacn_np_handoff
,
3306 rpcrt4_conn_np_read
,
3307 rpcrt4_conn_np_write
,
3308 rpcrt4_conn_np_close
,
3309 rpcrt4_conn_np_cancel_call
,
3310 rpcrt4_conn_np_wait_for_incoming_data
,
3311 rpcrt4_ncacn_np_get_top_of_tower
,
3312 rpcrt4_ncacn_np_parse_top_of_tower
,
3314 RPCRT4_default_is_authorized
,
3315 RPCRT4_default_authorize
,
3316 RPCRT4_default_secure_packet
,
3317 rpcrt4_conn_np_impersonate_client
,
3318 rpcrt4_conn_np_revert_to_self
,
3319 RPCRT4_default_inquire_auth_client
,
3322 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
3323 rpcrt4_conn_np_alloc
,
3324 rpcrt4_ncalrpc_open
,
3325 rpcrt4_ncalrpc_handoff
,
3326 rpcrt4_conn_np_read
,
3327 rpcrt4_conn_np_write
,
3328 rpcrt4_conn_np_close
,
3329 rpcrt4_conn_np_cancel_call
,
3330 rpcrt4_conn_np_wait_for_incoming_data
,
3331 rpcrt4_ncalrpc_get_top_of_tower
,
3332 rpcrt4_ncalrpc_parse_top_of_tower
,
3334 rpcrt4_ncalrpc_is_authorized
,
3335 rpcrt4_ncalrpc_authorize
,
3336 rpcrt4_ncalrpc_secure_packet
,
3337 rpcrt4_conn_np_impersonate_client
,
3338 rpcrt4_conn_np_revert_to_self
,
3339 rpcrt4_ncalrpc_inquire_auth_client
,
3342 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
3343 rpcrt4_conn_tcp_alloc
,
3344 rpcrt4_ncacn_ip_tcp_open
,
3345 rpcrt4_conn_tcp_handoff
,
3346 rpcrt4_conn_tcp_read
,
3347 rpcrt4_conn_tcp_write
,
3348 rpcrt4_conn_tcp_close
,
3349 rpcrt4_conn_tcp_cancel_call
,
3350 rpcrt4_conn_tcp_wait_for_incoming_data
,
3351 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
3352 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
3354 RPCRT4_default_is_authorized
,
3355 RPCRT4_default_authorize
,
3356 RPCRT4_default_secure_packet
,
3357 RPCRT4_default_impersonate_client
,
3358 RPCRT4_default_revert_to_self
,
3359 RPCRT4_default_inquire_auth_client
,
3362 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
3363 rpcrt4_ncacn_http_alloc
,
3364 rpcrt4_ncacn_http_open
,
3365 rpcrt4_ncacn_http_handoff
,
3366 rpcrt4_ncacn_http_read
,
3367 rpcrt4_ncacn_http_write
,
3368 rpcrt4_ncacn_http_close
,
3369 rpcrt4_ncacn_http_cancel_call
,
3370 rpcrt4_ncacn_http_wait_for_incoming_data
,
3371 rpcrt4_ncacn_http_get_top_of_tower
,
3372 rpcrt4_ncacn_http_parse_top_of_tower
,
3373 rpcrt4_ncacn_http_receive_fragment
,
3374 RPCRT4_default_is_authorized
,
3375 RPCRT4_default_authorize
,
3376 RPCRT4_default_secure_packet
,
3377 RPCRT4_default_impersonate_client
,
3378 RPCRT4_default_revert_to_self
,
3379 RPCRT4_default_inquire_auth_client
,
3384 static const struct protseq_ops protseq_list
[] =
3388 rpcrt4_protseq_np_alloc
,
3389 rpcrt4_protseq_np_signal_state_changed
,
3390 rpcrt4_protseq_np_get_wait_array
,
3391 rpcrt4_protseq_np_free_wait_array
,
3392 rpcrt4_protseq_np_wait_for_new_connection
,
3393 rpcrt4_protseq_ncacn_np_open_endpoint
,
3397 rpcrt4_protseq_np_alloc
,
3398 rpcrt4_protseq_np_signal_state_changed
,
3399 rpcrt4_protseq_np_get_wait_array
,
3400 rpcrt4_protseq_np_free_wait_array
,
3401 rpcrt4_protseq_np_wait_for_new_connection
,
3402 rpcrt4_protseq_ncalrpc_open_endpoint
,
3406 rpcrt4_protseq_sock_alloc
,
3407 rpcrt4_protseq_sock_signal_state_changed
,
3408 rpcrt4_protseq_sock_get_wait_array
,
3409 rpcrt4_protseq_sock_free_wait_array
,
3410 rpcrt4_protseq_sock_wait_for_new_connection
,
3411 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
3415 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3418 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3419 if (!strcmp(protseq_list
[i
].name
, protseq
))
3420 return &protseq_list
[i
];
3424 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3427 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3428 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3429 return &conn_protseq_list
[i
];
3433 /**** interface to rest of code ****/
3435 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3437 TRACE("(Connection == ^%p)\n", Connection
);
3439 assert(!Connection
->server
);
3440 return Connection
->ops
->open_connection_client(Connection
);
3443 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3445 TRACE("(Connection == ^%p)\n", Connection
);
3446 if (SecIsValidHandle(&Connection
->ctx
))
3448 DeleteSecurityContext(&Connection
->ctx
);
3449 SecInvalidateHandle(&Connection
->ctx
);
3451 rpcrt4_conn_close(Connection
);
3455 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3456 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3457 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3459 static LONG next_id
;
3460 const struct connection_ops
*ops
;
3461 RpcConnection
* NewConnection
;
3463 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3466 FIXME("not supported for protseq %s\n", Protseq
);
3467 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3470 NewConnection
= ops
->alloc();
3471 NewConnection
->ref
= 1;
3472 NewConnection
->Next
= NULL
;
3473 NewConnection
->server_binding
= NULL
;
3474 NewConnection
->server
= server
;
3475 NewConnection
->ops
= ops
;
3476 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3477 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3478 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3479 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3480 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3481 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
3482 NewConnection
->NextCallId
= 1;
3484 SecInvalidateHandle(&NewConnection
->ctx
);
3485 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
3486 NewConnection
->attr
= 0;
3487 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3488 NewConnection
->AuthInfo
= AuthInfo
;
3489 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3490 NewConnection
->encryption_auth_len
= 0;
3491 NewConnection
->signature_auth_len
= 0;
3492 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3493 NewConnection
->QOS
= QOS
;
3495 list_init(&NewConnection
->conn_pool_entry
);
3496 NewConnection
->async_state
= NULL
;
3498 TRACE("connection: %p\n", NewConnection
);
3499 *Connection
= NewConnection
;
3504 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3508 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
, rpcrt4_conn_get_name(OldConnection
),
3509 OldConnection
->NetworkAddr
, OldConnection
->Endpoint
, NULL
,
3510 OldConnection
->AuthInfo
, OldConnection
->QOS
, OldConnection
->CookieAuth
);
3511 if (err
== RPC_S_OK
)
3512 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3516 RpcConnection
*RPCRT4_GrabConnection( RpcConnection
*conn
)
3518 InterlockedIncrement( &conn
->ref
);
3522 RPC_STATUS
RPCRT4_ReleaseConnection(RpcConnection
* Connection
)
3524 if (InterlockedDecrement( &Connection
->ref
) > 0) return RPC_S_OK
;
3526 TRACE("destroying connection %p\n", Connection
);
3528 RPCRT4_CloseConnection(Connection
);
3529 RPCRT4_strfree(Connection
->Endpoint
);
3530 RPCRT4_strfree(Connection
->NetworkAddr
);
3531 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3532 HeapFree(GetProcessHeap(), 0, Connection
->CookieAuth
);
3533 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3534 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3537 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3539 HeapFree(GetProcessHeap(), 0, Connection
);
3543 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3545 const char *protseq
,
3546 const char *networkaddr
,
3547 const char *endpoint
)
3549 twr_empty_floor_t
*protocol_floor
;
3550 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3555 return RPC_S_INVALID_RPC_PROTSEQ
;
3559 *tower_size
= sizeof(*protocol_floor
);
3560 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3564 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3565 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3566 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3567 protocol_floor
->count_rhs
= 0;
3569 tower_data
+= sizeof(*protocol_floor
);
3571 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3573 return EPT_S_NOT_REGISTERED
;
3575 *tower_size
+= sizeof(*protocol_floor
);
3580 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3586 const twr_empty_floor_t
*protocol_floor
;
3587 const twr_empty_floor_t
*floor4
;
3588 const struct connection_ops
*protseq_ops
= NULL
;
3592 if (tower_size
< sizeof(*protocol_floor
))
3593 return EPT_S_NOT_REGISTERED
;
3595 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3596 tower_data
+= sizeof(*protocol_floor
);
3597 tower_size
-= sizeof(*protocol_floor
);
3598 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3599 (protocol_floor
->count_rhs
> tower_size
))
3600 return EPT_S_NOT_REGISTERED
;
3601 tower_data
+= protocol_floor
->count_rhs
;
3602 tower_size
-= protocol_floor
->count_rhs
;
3604 floor4
= (const twr_empty_floor_t
*)tower_data
;
3605 if ((tower_size
< sizeof(*floor4
)) ||
3606 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3607 return EPT_S_NOT_REGISTERED
;
3609 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3610 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3611 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3613 protseq_ops
= &conn_protseq_list
[i
];
3618 return EPT_S_NOT_REGISTERED
;
3620 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3622 if ((status
== RPC_S_OK
) && protseq
)
3624 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3625 strcpy(*protseq
, protseq_ops
->name
);
3631 /***********************************************************************
3632 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3634 * Checks if the given protocol sequence is known by the RPC system.
3635 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3638 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3642 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3643 ps
, sizeof ps
, NULL
, NULL
);
3644 if (rpcrt4_get_conn_protseq_ops(ps
))
3647 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3649 return RPC_S_INVALID_RPC_PROTSEQ
;
3652 /***********************************************************************
3653 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3655 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3657 UNICODE_STRING protseqW
;
3659 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3661 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3662 RtlFreeUnicodeString(&protseqW
);
3665 return RPC_S_OUT_OF_MEMORY
;
3668 /***********************************************************************
3669 * RpcProtseqVectorFreeA (RPCRT4.@)
3671 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3673 TRACE("(%p)\n", protseqs
);
3678 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3679 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3680 HeapFree(GetProcessHeap(), 0, *protseqs
);
3686 /***********************************************************************
3687 * RpcProtseqVectorFreeW (RPCRT4.@)
3689 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3691 TRACE("(%p)\n", protseqs
);
3696 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3697 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3698 HeapFree(GetProcessHeap(), 0, *protseqs
);
3704 /***********************************************************************
3705 * RpcNetworkInqProtseqsW (RPCRT4.@)
3707 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3709 RPC_PROTSEQ_VECTORW
*pvector
;
3711 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3713 TRACE("(%p)\n", protseqs
);
3715 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3718 pvector
= *protseqs
;
3720 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3722 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3723 if (pvector
->Protseq
[i
] == NULL
)
3725 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3726 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3732 if (status
!= RPC_S_OK
)
3733 RpcProtseqVectorFreeW(protseqs
);
3737 /***********************************************************************
3738 * RpcNetworkInqProtseqsA (RPCRT4.@)
3740 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3742 RPC_PROTSEQ_VECTORA
*pvector
;
3744 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3746 TRACE("(%p)\n", protseqs
);
3748 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3751 pvector
= *protseqs
;
3753 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3755 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3756 if (pvector
->Protseq
[i
] == NULL
)
3758 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3764 if (status
!= RPC_S_OK
)
3765 RpcProtseqVectorFreeA(protseqs
);