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
[] = ".";
311 BOOL bUseLocalName
= TRUE
;
312 CHAR ComputerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
313 DWORD bufLen
= sizeof(ComputerName
)/sizeof(ComputerName
[0]);
318 /* already connected? */
322 /* protseq=ncacn_np: named pipes */
323 size
= strlen(prefix
);
325 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
327 bUseLocalName
= TRUE
;
328 size
+= strlen(local
);
332 if (GetComputerNameA(ComputerName
, &bufLen
))
334 if (stricmp(ComputerName
, Connection
->NetworkAddr
) == 0)
336 bUseLocalName
= TRUE
;
337 size
+= strlen(local
);
341 bUseLocalName
= FALSE
;
342 size
+= strlen(Connection
->NetworkAddr
);
347 bUseLocalName
= FALSE
;
348 size
+= strlen(Connection
->NetworkAddr
);
352 size
+= strlen(Connection
->Endpoint
) + 1;
354 pname
= I_RpcAllocate(size
);
355 strcpy(pname
, prefix
);
357 strcat(pname
, local
);
359 strcat(pname
, Connection
->NetworkAddr
);
360 strcat(pname
, Connection
->Endpoint
);
361 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
367 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
369 static const char prefix
[] = "\\\\.";
372 RpcConnection
*Connection
;
373 char generated_endpoint
[21];
377 static LONG np_nameless_id
;
378 DWORD process_id
= GetCurrentProcessId();
379 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
380 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
381 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
382 endpoint
= generated_endpoint
;
385 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
386 endpoint
, NULL
, NULL
, NULL
, NULL
);
390 /* protseq=ncacn_np: named pipes */
391 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
392 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
393 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
396 EnterCriticalSection(&protseq
->cs
);
397 Connection
->Next
= protseq
->conn
;
398 protseq
->conn
= Connection
;
399 LeaveCriticalSection(&protseq
->cs
);
404 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
406 /* because of the way named pipes work, we'll transfer the connected pipe
407 * to the child, then reopen the server binding to continue listening */
409 new_npc
->pipe
= old_npc
->pipe
;
410 new_npc
->ovl
= old_npc
->ovl
;
412 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
413 old_npc
->listening
= FALSE
;
416 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
420 static const char prefix
[] = "\\\\.";
422 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
424 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
425 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
426 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
432 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
436 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
438 TRACE("%s\n", old_conn
->Endpoint
);
440 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
442 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
443 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
444 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
450 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
451 void *buffer
, unsigned int count
)
453 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
456 unsigned int bytes_left
= count
;
459 ZeroMemory(&ovl
, sizeof(ovl
));
460 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
465 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
466 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
467 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
468 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
470 if (!ret
|| !bytes_read
)
472 bytes_left
-= bytes_read
;
475 CloseHandle(ovl
.hEvent
);
476 return ret
? count
: -1;
479 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
480 const void *buffer
, unsigned int count
)
482 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
483 const char *buf
= buffer
;
485 unsigned int bytes_left
= count
;
488 ZeroMemory(&ovl
, sizeof(ovl
));
489 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
494 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
495 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
496 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
497 if (!ret
|| !bytes_written
)
499 bytes_left
-= bytes_written
;
500 buf
+= bytes_written
;
502 CloseHandle(ovl
.hEvent
);
503 return ret
? count
: -1;
506 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
508 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
510 FlushFileBuffers(npc
->pipe
);
511 CloseHandle(npc
->pipe
);
514 if (npc
->ovl
.hEvent
) {
515 CloseHandle(npc
->ovl
.hEvent
);
521 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
523 /* FIXME: implement when named pipe writes use overlapped I/O */
526 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
528 /* FIXME: implement when named pipe writes use overlapped I/O */
532 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
533 const char *networkaddr
,
534 const char *endpoint
)
536 twr_empty_floor_t
*smb_floor
;
537 twr_empty_floor_t
*nb_floor
;
539 size_t networkaddr_size
;
540 size_t endpoint_size
;
542 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
544 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
545 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
546 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
551 smb_floor
= (twr_empty_floor_t
*)tower_data
;
553 tower_data
+= sizeof(*smb_floor
);
555 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
556 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
557 smb_floor
->count_rhs
= endpoint_size
;
560 memcpy(tower_data
, endpoint
, endpoint_size
);
563 tower_data
+= endpoint_size
;
565 nb_floor
= (twr_empty_floor_t
*)tower_data
;
567 tower_data
+= sizeof(*nb_floor
);
569 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
570 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
571 nb_floor
->count_rhs
= networkaddr_size
;
574 memcpy(tower_data
, networkaddr
, networkaddr_size
);
581 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
586 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
587 const twr_empty_floor_t
*nb_floor
;
589 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
591 if (tower_size
< sizeof(*smb_floor
))
592 return EPT_S_NOT_REGISTERED
;
594 tower_data
+= sizeof(*smb_floor
);
595 tower_size
-= sizeof(*smb_floor
);
597 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
598 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
599 (smb_floor
->count_rhs
> tower_size
) ||
600 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
601 return EPT_S_NOT_REGISTERED
;
605 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
607 return RPC_S_OUT_OF_RESOURCES
;
608 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
610 tower_data
+= smb_floor
->count_rhs
;
611 tower_size
-= smb_floor
->count_rhs
;
613 if (tower_size
< sizeof(*nb_floor
))
614 return EPT_S_NOT_REGISTERED
;
616 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
618 tower_data
+= sizeof(*nb_floor
);
619 tower_size
-= sizeof(*nb_floor
);
621 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
622 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
623 (nb_floor
->count_rhs
> tower_size
) ||
624 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
625 return EPT_S_NOT_REGISTERED
;
629 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
634 I_RpcFree(*endpoint
);
637 return RPC_S_OUT_OF_RESOURCES
;
639 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
645 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
647 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
650 TRACE("(%p)\n", conn
);
652 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
653 return RPCRT4_default_impersonate_client(conn
);
655 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
658 DWORD error
= GetLastError();
659 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
662 case ERROR_CANNOT_IMPERSONATE
:
663 return RPC_S_NO_CONTEXT_AVAILABLE
;
669 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
673 TRACE("(%p)\n", conn
);
675 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
676 return RPCRT4_default_revert_to_self(conn
);
678 ret
= RevertToSelf();
681 WARN("RevertToSelf failed with error %u\n", GetLastError());
682 return RPC_S_NO_CONTEXT_AVAILABLE
;
687 typedef struct _RpcServerProtseq_np
689 RpcServerProtseq common
;
691 } RpcServerProtseq_np
;
693 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
695 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
697 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
701 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
703 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
704 SetEvent(npps
->mgr_event
);
707 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
709 HANDLE
*objs
= prev_array
;
710 RpcConnection_np
*conn
;
711 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
713 EnterCriticalSection(&protseq
->cs
);
715 /* open and count connections */
717 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
719 rpcrt4_conn_listen_pipe(conn
);
720 if (conn
->ovl
.hEvent
)
722 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
725 /* make array of connections */
727 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
729 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
732 ERR("couldn't allocate objs\n");
733 LeaveCriticalSection(&protseq
->cs
);
737 objs
[0] = npps
->mgr_event
;
739 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
741 if ((objs
[*count
] = conn
->ovl
.hEvent
))
743 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
745 LeaveCriticalSection(&protseq
->cs
);
749 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
751 HeapFree(GetProcessHeap(), 0, array
);
754 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
757 HANDLE
*objs
= wait_array
;
759 RpcConnection
*cconn
;
760 RpcConnection_np
*conn
;
767 /* an alertable wait isn't strictly necessary, but due to our
768 * overlapped I/O implementation in Wine we need to free some memory
769 * by the file user APC being called, even if no completion routine was
770 * specified at the time of starting the async operation */
771 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
772 } while (res
== WAIT_IO_COMPLETION
);
774 if (res
== WAIT_OBJECT_0
)
776 else if (res
== WAIT_FAILED
)
778 ERR("wait failed with error %d\n", GetLastError());
783 b_handle
= objs
[res
- WAIT_OBJECT_0
];
784 /* find which connection got a RPC */
785 EnterCriticalSection(&protseq
->cs
);
786 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
788 if (b_handle
== conn
->ovl
.hEvent
) break;
789 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
793 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
795 ERR("failed to locate connection for handle %p\n", b_handle
);
796 LeaveCriticalSection(&protseq
->cs
);
799 RPCRT4_new_client(cconn
);
806 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
807 const char *networkaddr
,
808 const char *endpoint
)
810 twr_empty_floor_t
*pipe_floor
;
812 size_t endpoint_size
;
814 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
816 endpoint_size
= strlen(endpoint
) + 1;
817 size
= sizeof(*pipe_floor
) + endpoint_size
;
822 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
824 tower_data
+= sizeof(*pipe_floor
);
826 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
827 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
828 pipe_floor
->count_rhs
= endpoint_size
;
830 memcpy(tower_data
, endpoint
, endpoint_size
);
835 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
840 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
842 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
844 if (tower_size
< sizeof(*pipe_floor
))
845 return EPT_S_NOT_REGISTERED
;
847 tower_data
+= sizeof(*pipe_floor
);
848 tower_size
-= sizeof(*pipe_floor
);
850 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
851 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
852 (pipe_floor
->count_rhs
> tower_size
) ||
853 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
854 return EPT_S_NOT_REGISTERED
;
861 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
863 return RPC_S_OUT_OF_RESOURCES
;
864 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
870 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
875 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
876 unsigned char *in_buffer
,
877 unsigned int in_size
,
878 unsigned char *out_buffer
,
879 unsigned int *out_size
)
881 /* since this protocol is local to the machine there is no need to
882 * authenticate the caller */
887 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
888 enum secure_packet_direction dir
,
889 RpcPktHdr
*hdr
, unsigned int hdr_size
,
890 unsigned char *stub_data
, unsigned int stub_data_size
,
891 RpcAuthVerifier
*auth_hdr
,
892 unsigned char *auth_value
, unsigned int auth_value_size
)
894 /* since this protocol is local to the machine there is no need to secure
899 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
900 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
901 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
903 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
904 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
908 FIXME("privs not implemented\n");
911 if (server_princ_name
)
913 FIXME("server_princ_name not implemented\n");
914 *server_princ_name
= NULL
;
916 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
917 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
920 FIXME("authorization service not implemented\n");
921 *authz_svc
= RPC_C_AUTHZ_NONE
;
924 FIXME("flags 0x%x not implemented\n", flags
);
929 /**** ncacn_ip_tcp support ****/
931 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
932 const char *networkaddr
,
933 unsigned char tcp_protid
,
934 const char *endpoint
)
936 twr_tcp_floor_t
*tcp_floor
;
937 twr_ipv4_floor_t
*ipv4_floor
;
939 struct addrinfo hints
;
941 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
943 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
948 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
949 tower_data
+= sizeof(*tcp_floor
);
951 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
953 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
954 tcp_floor
->protid
= tcp_protid
;
955 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
957 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
958 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
959 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
961 hints
.ai_flags
= AI_NUMERICHOST
;
962 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
963 hints
.ai_family
= PF_INET
;
964 hints
.ai_socktype
= SOCK_STREAM
;
965 hints
.ai_protocol
= IPPROTO_TCP
;
966 hints
.ai_addrlen
= 0;
967 hints
.ai_addr
= NULL
;
968 hints
.ai_canonname
= NULL
;
969 hints
.ai_next
= NULL
;
971 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
974 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
977 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
982 if (ai
->ai_family
== PF_INET
)
984 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
985 tcp_floor
->port
= sin
->sin_port
;
986 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
990 ERR("unexpected protocol family %d\n", ai
->ai_family
);
999 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1002 unsigned char tcp_protid
,
1005 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1006 const twr_ipv4_floor_t
*ipv4_floor
;
1007 struct in_addr in_addr
;
1009 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1011 if (tower_size
< sizeof(*tcp_floor
))
1012 return EPT_S_NOT_REGISTERED
;
1014 tower_data
+= sizeof(*tcp_floor
);
1015 tower_size
-= sizeof(*tcp_floor
);
1017 if (tower_size
< sizeof(*ipv4_floor
))
1018 return EPT_S_NOT_REGISTERED
;
1020 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1022 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1023 (tcp_floor
->protid
!= tcp_protid
) ||
1024 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1025 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1026 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1027 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1028 return EPT_S_NOT_REGISTERED
;
1032 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1034 return RPC_S_OUT_OF_RESOURCES
;
1035 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1040 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1045 I_RpcFree(*endpoint
);
1048 return RPC_S_OUT_OF_RESOURCES
;
1050 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1051 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1053 ERR("inet_ntop: %s\n", strerror(errno
));
1054 I_RpcFree(*networkaddr
);
1055 *networkaddr
= NULL
;
1058 I_RpcFree(*endpoint
);
1061 return EPT_S_NOT_REGISTERED
;
1068 typedef struct _RpcConnection_tcp
1070 RpcConnection common
;
1072 #ifdef HAVE_SOCKETPAIR
1076 HANDLE cancel_event
;
1078 } RpcConnection_tcp
;
1080 #ifdef HAVE_SOCKETPAIR
1082 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1084 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1086 ERR("socketpair() failed: %s\n", strerror(errno
));
1092 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1094 struct pollfd pfds
[2];
1095 pfds
[0].fd
= tcpc
->sock
;
1096 pfds
[0].events
= POLLIN
;
1097 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1098 pfds
[1].events
= POLLIN
;
1099 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1101 ERR("poll() failed: %s\n", strerror(errno
));
1104 if (pfds
[1].revents
& POLLIN
) /* canceled */
1107 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1113 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1116 pfd
.fd
= tcpc
->sock
;
1117 pfd
.events
= POLLOUT
;
1118 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1120 ERR("poll() failed: %s\n", strerror(errno
));
1126 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1130 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1133 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1135 close(tcpc
->cancel_fds
[0]);
1136 close(tcpc
->cancel_fds
[1]);
1139 #else /* HAVE_SOCKETPAIR */
1141 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1143 static BOOL wsa_inited
;
1147 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1148 /* Note: WSAStartup can be called more than once so we don't bother with
1149 * making accesses to wsa_inited thread-safe */
1152 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1153 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1154 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1156 ERR("event creation failed\n");
1157 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1163 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1165 HANDLE wait_handles
[2];
1167 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1169 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1172 wait_handles
[0] = tcpc
->sock_event
;
1173 wait_handles
[1] = tcpc
->cancel_event
;
1174 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1179 case WAIT_OBJECT_0
+ 1:
1182 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1187 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1190 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1192 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1195 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1201 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1206 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1208 SetEvent(tcpc
->cancel_event
);
1211 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1213 CloseHandle(tcpc
->sock_event
);
1214 CloseHandle(tcpc
->cancel_event
);
1219 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1221 RpcConnection_tcp
*tcpc
;
1222 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1226 if (!rpcrt4_sock_wait_init(tcpc
))
1228 HeapFree(GetProcessHeap(), 0, tcpc
);
1231 return &tcpc
->common
;
1234 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1236 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1239 struct addrinfo
*ai
;
1240 struct addrinfo
*ai_cur
;
1241 struct addrinfo hints
;
1243 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1245 if (tcpc
->sock
!= -1)
1249 hints
.ai_family
= PF_UNSPEC
;
1250 hints
.ai_socktype
= SOCK_STREAM
;
1251 hints
.ai_protocol
= IPPROTO_TCP
;
1252 hints
.ai_addrlen
= 0;
1253 hints
.ai_addr
= NULL
;
1254 hints
.ai_canonname
= NULL
;
1255 hints
.ai_next
= NULL
;
1257 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1260 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1261 Connection
->Endpoint
, gai_strerror(ret
));
1262 return RPC_S_SERVER_UNAVAILABLE
;
1265 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1270 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1272 TRACE("skipping non-IP/IPv6 address family\n");
1280 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1281 host
, sizeof(host
), service
, sizeof(service
),
1282 NI_NUMERICHOST
| NI_NUMERICSERV
);
1283 TRACE("trying %s:%s\n", host
, service
);
1286 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1289 WARN("socket() failed: %s\n", strerror(errno
));
1293 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1295 WARN("connect() failed: %s\n", strerror(errno
));
1300 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1302 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1304 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1309 TRACE("connected\n");
1314 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1315 return RPC_S_SERVER_UNAVAILABLE
;
1318 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1320 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1323 struct addrinfo
*ai
;
1324 struct addrinfo
*ai_cur
;
1325 struct addrinfo hints
;
1326 RpcConnection
*first_connection
= NULL
;
1328 TRACE("(%p, %s)\n", protseq
, endpoint
);
1330 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1331 hints
.ai_family
= PF_UNSPEC
;
1332 hints
.ai_socktype
= SOCK_STREAM
;
1333 hints
.ai_protocol
= IPPROTO_TCP
;
1334 hints
.ai_addrlen
= 0;
1335 hints
.ai_addr
= NULL
;
1336 hints
.ai_canonname
= NULL
;
1337 hints
.ai_next
= NULL
;
1339 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1342 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1344 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1345 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1346 return RPC_S_CANT_CREATE_ENDPOINT
;
1349 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1351 RpcConnection_tcp
*tcpc
;
1352 RPC_STATUS create_status
;
1353 struct sockaddr_storage sa
;
1355 char service
[NI_MAXSERV
];
1358 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1360 TRACE("skipping non-IP/IPv6 address family\n");
1367 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1368 host
, sizeof(host
), service
, sizeof(service
),
1369 NI_NUMERICHOST
| NI_NUMERICSERV
);
1370 TRACE("trying %s:%s\n", host
, service
);
1373 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1376 WARN("socket() failed: %s\n", strerror(errno
));
1377 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1381 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1384 WARN("bind failed: %s\n", strerror(errno
));
1386 if (errno
== EADDRINUSE
)
1387 status
= RPC_S_DUPLICATE_ENDPOINT
;
1389 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1393 sa_len
= sizeof(sa
);
1394 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1396 WARN("getsockname() failed: %s\n", strerror(errno
));
1398 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1402 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1403 NULL
, 0, service
, sizeof(service
),
1407 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1409 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1413 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1414 protseq
->Protseq
, NULL
,
1415 service
, NULL
, NULL
, NULL
, NULL
);
1416 if (create_status
!= RPC_S_OK
)
1419 status
= create_status
;
1424 ret
= listen(sock
, protseq
->MaxCalls
);
1427 WARN("listen failed: %s\n", strerror(errno
));
1428 RPCRT4_ReleaseConnection(&tcpc
->common
);
1429 status
= RPC_S_OUT_OF_RESOURCES
;
1432 /* need a non-blocking socket, otherwise accept() has a potential
1433 * race-condition (poll() says it is readable, connection drops,
1434 * and accept() blocks until the next connection comes...)
1437 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1440 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1441 RPCRT4_ReleaseConnection(&tcpc
->common
);
1442 status
= RPC_S_OUT_OF_RESOURCES
;
1446 tcpc
->common
.Next
= first_connection
;
1447 first_connection
= &tcpc
->common
;
1449 /* since IPv4 and IPv6 share the same port space, we only need one
1450 * successful bind to listen for both */
1456 /* if at least one connection was created for an endpoint then
1458 if (first_connection
)
1460 RpcConnection
*conn
;
1462 /* find last element in list */
1463 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1466 EnterCriticalSection(&protseq
->cs
);
1467 conn
->Next
= protseq
->conn
;
1468 protseq
->conn
= first_connection
;
1469 LeaveCriticalSection(&protseq
->cs
);
1471 TRACE("listening on %s\n", endpoint
);
1475 ERR("couldn't listen on port %s\n", endpoint
);
1479 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1482 struct sockaddr_in address
;
1484 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1485 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1488 addrsize
= sizeof(address
);
1489 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1492 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1493 return RPC_S_OUT_OF_RESOURCES
;
1496 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1498 TRACE("Accepted a new TCP connection\n");
1502 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1503 void *buffer
, unsigned int count
)
1505 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1507 while (bytes_read
!= count
)
1509 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1514 else if (errno
!= EAGAIN
)
1516 WARN("recv() failed: %s\n", strerror(errno
));
1521 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1525 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1529 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1530 const void *buffer
, unsigned int count
)
1532 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1533 int bytes_written
= 0;
1534 while (bytes_written
!= count
)
1536 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1539 else if (errno
!= EAGAIN
)
1543 if (!rpcrt4_sock_wait_for_send(tcpc
))
1547 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1548 return bytes_written
;
1551 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1553 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1555 TRACE("%d\n", tcpc
->sock
);
1557 if (tcpc
->sock
!= -1)
1558 closesocket(tcpc
->sock
);
1560 rpcrt4_sock_wait_destroy(tcpc
);
1564 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1566 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1567 TRACE("%p\n", Connection
);
1568 rpcrt4_sock_wait_cancel(tcpc
);
1571 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1573 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1575 TRACE("%p\n", Connection
);
1577 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1582 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1583 const char *networkaddr
,
1584 const char *endpoint
)
1586 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1587 EPM_PROTOCOL_TCP
, endpoint
);
1590 #ifdef HAVE_SOCKETPAIR
1592 typedef struct _RpcServerProtseq_sock
1594 RpcServerProtseq common
;
1597 } RpcServerProtseq_sock
;
1599 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1601 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1605 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1607 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1608 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1609 ps
->mgr_event_rcv
= fds
[0];
1610 ps
->mgr_event_snd
= fds
[1];
1614 ERR("socketpair failed with error %s\n", strerror(errno
));
1615 HeapFree(GetProcessHeap(), 0, ps
);
1622 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1624 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1626 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1629 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1631 struct pollfd
*poll_info
= prev_array
;
1632 RpcConnection_tcp
*conn
;
1633 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1635 EnterCriticalSection(&protseq
->cs
);
1637 /* open and count connections */
1639 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1641 if (conn
->sock
!= -1)
1643 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1646 /* make array of connections */
1648 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1650 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1653 ERR("couldn't allocate poll_info\n");
1654 LeaveCriticalSection(&protseq
->cs
);
1658 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1659 poll_info
[0].events
= POLLIN
;
1661 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1663 if (conn
->sock
!= -1)
1665 poll_info
[*count
].fd
= conn
->sock
;
1666 poll_info
[*count
].events
= POLLIN
;
1669 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1671 LeaveCriticalSection(&protseq
->cs
);
1675 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1677 HeapFree(GetProcessHeap(), 0, array
);
1680 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1682 struct pollfd
*poll_info
= wait_array
;
1685 RpcConnection
*cconn
;
1686 RpcConnection_tcp
*conn
;
1691 ret
= poll(poll_info
, count
, -1);
1694 ERR("poll failed with error %d\n", ret
);
1698 for (i
= 0; i
< count
; i
++)
1699 if (poll_info
[i
].revents
& POLLIN
)
1701 /* RPC server event */
1705 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1709 /* find which connection got a RPC */
1710 EnterCriticalSection(&protseq
->cs
);
1711 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1713 if (poll_info
[i
].fd
== conn
->sock
) break;
1714 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1718 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1720 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1721 LeaveCriticalSection(&protseq
->cs
);
1723 RPCRT4_new_client(cconn
);
1731 #else /* HAVE_SOCKETPAIR */
1733 typedef struct _RpcServerProtseq_sock
1735 RpcServerProtseq common
;
1737 } RpcServerProtseq_sock
;
1739 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1741 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1744 static BOOL wsa_inited
;
1748 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1749 /* Note: WSAStartup can be called more than once so we don't bother with
1750 * making accesses to wsa_inited thread-safe */
1753 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1758 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1760 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1761 SetEvent(sockps
->mgr_event
);
1764 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1766 HANDLE
*objs
= prev_array
;
1767 RpcConnection_tcp
*conn
;
1768 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1770 EnterCriticalSection(&protseq
->cs
);
1772 /* open and count connections */
1774 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1777 if (conn
->sock
!= -1)
1779 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1782 /* make array of connections */
1784 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1786 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1789 ERR("couldn't allocate objs\n");
1790 LeaveCriticalSection(&protseq
->cs
);
1794 objs
[0] = sockps
->mgr_event
;
1796 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1799 if (conn
->sock
!= -1)
1801 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1802 if (res
== SOCKET_ERROR
)
1803 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1806 objs
[*count
] = conn
->sock_event
;
1810 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1812 LeaveCriticalSection(&protseq
->cs
);
1816 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1818 HeapFree(GetProcessHeap(), 0, array
);
1821 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1824 HANDLE
*objs
= wait_array
;
1826 RpcConnection
*cconn
;
1827 RpcConnection_tcp
*conn
;
1834 /* an alertable wait isn't strictly necessary, but due to our
1835 * overlapped I/O implementation in Wine we need to free some memory
1836 * by the file user APC being called, even if no completion routine was
1837 * specified at the time of starting the async operation */
1838 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1839 } while (res
== WAIT_IO_COMPLETION
);
1841 if (res
== WAIT_OBJECT_0
)
1843 else if (res
== WAIT_FAILED
)
1845 ERR("wait failed with error %d\n", GetLastError());
1850 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1851 /* find which connection got a RPC */
1852 EnterCriticalSection(&protseq
->cs
);
1853 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1856 if (b_handle
== conn
->sock_event
) break;
1857 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1861 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1863 ERR("failed to locate connection for handle %p\n", b_handle
);
1864 LeaveCriticalSection(&protseq
->cs
);
1867 RPCRT4_new_client(cconn
);
1874 #endif /* HAVE_SOCKETPAIR */
1876 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1881 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1882 networkaddr
, EPM_PROTOCOL_TCP
,
1886 /**** ncacn_http support ****/
1888 /* 60 seconds is the period native uses */
1889 #define HTTP_IDLE_TIME 60000
1891 /* reference counted to avoid a race between a cancelled call's connection
1892 * being destroyed and the asynchronous InternetReadFileEx call being
1894 typedef struct _RpcHttpAsyncData
1897 HANDLE completion_event
;
1899 INTERNET_BUFFERSA inet_buffers
;
1900 CRITICAL_SECTION cs
;
1903 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1905 return InterlockedIncrement(&data
->refs
);
1908 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1910 ULONG refs
= InterlockedDecrement(&data
->refs
);
1913 TRACE("destroying async data %p\n", data
);
1914 CloseHandle(data
->completion_event
);
1915 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1916 data
->cs
.DebugInfo
->Spare
[0] = 0;
1917 DeleteCriticalSection(&data
->cs
);
1918 HeapFree(GetProcessHeap(), 0, data
);
1923 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
1925 ResetEvent(async_data
->completion_event
);
1926 RpcHttpAsyncData_AddRef(async_data
);
1929 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
1931 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
1935 RpcHttpAsyncData_Release(async_data
);
1939 if(GetLastError() != ERROR_IO_PENDING
) {
1940 RpcHttpAsyncData_Release(async_data
);
1941 ERR("Request failed with error %d\n", GetLastError());
1942 return RPC_S_SERVER_UNAVAILABLE
;
1945 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
1946 if(res
!= WAIT_OBJECT_0
) {
1947 TRACE("Cancelled\n");
1948 return RPC_S_CALL_CANCELLED
;
1951 if(async_data
->async_result
) {
1952 ERR("Async request failed with error %d\n", async_data
->async_result
);
1953 return RPC_S_SERVER_UNAVAILABLE
;
1968 unsigned int data_len
;
1969 BOOL finished
; /* finished authenticating */
1972 typedef struct _RpcConnection_http
1974 RpcConnection common
;
1977 HINTERNET in_request
;
1978 HINTERNET out_request
;
1980 HANDLE timer_cancelled
;
1981 HANDLE cancel_event
;
1982 DWORD last_sent_time
;
1983 ULONG bytes_received
;
1984 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1985 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1986 UUID connection_uuid
;
1989 RpcHttpAsyncData
*async_data
;
1990 } RpcConnection_http
;
1992 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1994 RpcConnection_http
*httpc
;
1995 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1996 if (!httpc
) return NULL
;
1997 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1998 if (!httpc
->async_data
)
2000 HeapFree(GetProcessHeap(), 0, httpc
);
2003 TRACE("async data = %p\n", httpc
->async_data
);
2004 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2005 httpc
->async_data
->refs
= 1;
2006 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
2007 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2008 InitializeCriticalSection(&httpc
->async_data
->cs
);
2009 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
2010 return &httpc
->common
;
2013 typedef struct _HttpTimerThreadData
2016 DWORD
*last_sent_time
;
2017 HANDLE timer_cancelled
;
2018 } HttpTimerThreadData
;
2020 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
2022 HINTERNET in_request
= param
;
2023 RpcPktHdr
*idle_pkt
;
2025 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
2029 DWORD bytes_written
;
2030 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
2031 RPCRT4_FreeHeader(idle_pkt
);
2035 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
2037 DWORD cur_time
= GetTickCount();
2038 DWORD cached_last_sent_time
= *last_sent_time
;
2039 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
2042 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
2044 HttpTimerThreadData
*data_in
= param
;
2045 HttpTimerThreadData data
;
2049 HeapFree(GetProcessHeap(), 0, data_in
);
2051 for (timeout
= HTTP_IDLE_TIME
;
2052 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
2053 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
2055 /* are we too soon after last send? */
2056 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
2058 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2061 CloseHandle(data
.timer_cancelled
);
2065 static VOID WINAPI
rpcrt4_http_internet_callback(
2066 HINTERNET hInternet
,
2067 DWORD_PTR dwContext
,
2068 DWORD dwInternetStatus
,
2069 LPVOID lpvStatusInformation
,
2070 DWORD dwStatusInformationLength
)
2072 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2074 switch (dwInternetStatus
)
2076 case INTERNET_STATUS_REQUEST_COMPLETE
:
2077 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2080 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2082 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2083 SetEvent(async_data
->completion_event
);
2084 RpcHttpAsyncData_Release(async_data
);
2090 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2097 WCHAR
*status_text
= buf
;
2101 size
= sizeof(status_code
);
2102 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2104 return GetLastError();
2105 if (status_code
== HTTP_STATUS_OK
)
2109 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2110 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2112 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2113 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2116 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2117 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2119 if (status_code
== HTTP_STATUS_DENIED
)
2120 return ERROR_ACCESS_DENIED
;
2121 return RPC_S_SERVER_UNAVAILABLE
;
2124 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2126 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2127 LPWSTR proxy
= NULL
;
2129 LPWSTR password
= NULL
;
2130 LPWSTR servername
= NULL
;
2131 const WCHAR
*option
;
2134 if (httpc
->common
.QOS
&&
2135 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2137 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2138 if (http_cred
->TransportCredentials
)
2141 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2142 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2143 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2145 return RPC_S_OUT_OF_RESOURCES
;
2147 if (cred
->DomainLength
)
2149 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2150 p
+= cred
->DomainLength
;
2154 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2155 p
[cred
->UserLength
] = 0;
2157 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2161 for (option
= httpc
->common
.NetworkOptions
; option
;
2162 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2164 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2165 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2167 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2169 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2170 const WCHAR
*value_end
;
2173 value_end
= strchrW(option
, ',');
2175 value_end
= value_start
+ strlenW(value_start
);
2176 for (p
= value_start
; p
< value_end
; p
++)
2183 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2184 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2186 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2188 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2189 const WCHAR
*value_end
;
2191 value_end
= strchrW(option
, ',');
2193 value_end
= value_start
+ strlenW(value_start
);
2194 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2195 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2198 FIXME("unhandled option %s\n", debugstr_w(option
));
2201 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2202 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2203 if (!httpc
->app_info
)
2205 HeapFree(GetProcessHeap(), 0, password
);
2206 HeapFree(GetProcessHeap(), 0, user
);
2207 HeapFree(GetProcessHeap(), 0, proxy
);
2208 HeapFree(GetProcessHeap(), 0, servername
);
2209 ERR("InternetOpenW failed with error %d\n", GetLastError());
2210 return RPC_S_SERVER_UNAVAILABLE
;
2212 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2214 /* if no RpcProxy option specified, set the HTTP server address to the
2215 * RPC server address */
2218 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2221 HeapFree(GetProcessHeap(), 0, password
);
2222 HeapFree(GetProcessHeap(), 0, user
);
2223 HeapFree(GetProcessHeap(), 0, proxy
);
2224 return RPC_S_OUT_OF_RESOURCES
;
2226 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2229 port
= (httpc
->common
.QOS
&&
2230 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2231 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2232 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2234 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2235 INTERNET_SERVICE_HTTP
, 0, 0);
2237 HeapFree(GetProcessHeap(), 0, password
);
2238 HeapFree(GetProcessHeap(), 0, user
);
2239 HeapFree(GetProcessHeap(), 0, proxy
);
2241 if (!httpc
->session
)
2243 ERR("InternetConnectW failed with error %d\n", GetLastError());
2244 HeapFree(GetProcessHeap(), 0, servername
);
2245 return RPC_S_SERVER_UNAVAILABLE
;
2247 httpc
->servername
= servername
;
2251 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2258 TRACE("sending echo request to server\n");
2260 prepare_async_request(async_data
);
2261 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2262 status
= wait_async_request(async_data
, ret
, cancel_event
);
2263 if (status
!= RPC_S_OK
) return status
;
2265 status
= rpcrt4_http_check_response(req
);
2266 if (status
!= RPC_S_OK
) return status
;
2268 InternetReadFile(req
, buf
, sizeof(buf
), &bytes_read
);
2269 /* FIXME: do something with retrieved data */
2274 /* prepare the in pipe for use by RPC packets */
2275 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2276 const UUID
*connection_uuid
, const UUID
*in_pipe_uuid
,
2277 const UUID
*association_uuid
, BOOL authorized
)
2282 INTERNET_BUFFERSW buffers_in
;
2283 DWORD bytes_written
;
2287 /* ask wininet to authorize, if necessary */
2288 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2289 if (status
!= RPC_S_OK
) return status
;
2291 memset(&buffers_in
, 0, sizeof(buffers_in
));
2292 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2293 /* FIXME: get this from the registry */
2294 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2295 prepare_async_request(async_data
);
2296 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2297 status
= wait_async_request(async_data
, ret
, cancel_event
);
2298 if (status
!= RPC_S_OK
) return status
;
2300 TRACE("sending HTTP connect header to server\n");
2301 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2302 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2303 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2304 RPCRT4_FreeHeader(hdr
);
2307 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2308 return RPC_S_SERVER_UNAVAILABLE
;
2314 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2318 unsigned short data_len
;
2320 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2322 return RPC_S_SERVER_UNAVAILABLE
;
2323 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2325 ERR("wrong packet type received %d or wrong frag_len %d\n",
2326 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2327 return RPC_S_PROTOCOL_ERROR
;
2330 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2332 return RPC_S_SERVER_UNAVAILABLE
;
2334 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2337 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2339 return RPC_S_OUT_OF_RESOURCES
;
2340 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2343 HeapFree(GetProcessHeap(), 0, *data
);
2344 return RPC_S_SERVER_UNAVAILABLE
;
2350 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2352 ERR("invalid http packet\n");
2353 return RPC_S_PROTOCOL_ERROR
;
2359 /* prepare the out pipe for use by RPC packets */
2360 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
, RpcHttpAsyncData
*async_data
,
2361 HANDLE cancel_event
, const UUID
*connection_uuid
,
2362 const UUID
*out_pipe_uuid
, ULONG
*flow_control_increment
,
2368 BYTE
*data_from_server
;
2369 RpcPktHdr pkt_from_server
;
2370 ULONG field1
, field3
;
2376 /* ask wininet to authorize, if necessary */
2377 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2378 if (status
!= RPC_S_OK
) return status
;
2381 InternetReadFile(out_request
, buf
, sizeof(buf
), &bytes_read
);
2383 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2384 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2386 TRACE("sending HTTP connect header to server\n");
2387 prepare_async_request(async_data
);
2388 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2389 status
= wait_async_request(async_data
, ret
, cancel_event
);
2390 RPCRT4_FreeHeader(hdr
);
2391 if (status
!= RPC_S_OK
) return status
;
2393 status
= rpcrt4_http_check_response(out_request
);
2394 if (status
!= RPC_S_OK
) return status
;
2396 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2398 if (status
!= RPC_S_OK
) return status
;
2399 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2401 HeapFree(GetProcessHeap(), 0, data_from_server
);
2402 if (status
!= RPC_S_OK
) return status
;
2403 TRACE("received (%d) from first prepare header\n", field1
);
2407 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2409 if (status
!= RPC_S_OK
) return status
;
2410 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2412 TRACE("http idle packet, waiting for real packet\n");
2413 HeapFree(GetProcessHeap(), 0, data_from_server
);
2414 if (pkt_from_server
.http
.num_data_items
!= 0)
2416 ERR("HTTP idle packet should have no data items instead of %d\n",
2417 pkt_from_server
.http
.num_data_items
);
2418 return RPC_S_PROTOCOL_ERROR
;
2421 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2422 &field1
, flow_control_increment
,
2424 HeapFree(GetProcessHeap(), 0, data_from_server
);
2425 if (status
!= RPC_S_OK
) return status
;
2426 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2431 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2433 static const char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2438 /* first 6 bits, all from bin[0] */
2439 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2440 x
= (bin
[0] & 3) << 4;
2442 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2445 base64
[i
++] = enc
[x
];
2450 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2451 x
= (bin
[1] & 0x0f) << 2;
2453 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2456 base64
[i
++] = enc
[x
];
2460 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2462 /* last 6 bits, all from bin [2] */
2463 base64
[i
++] = enc
[bin
[2] & 0x3f];
2471 static inline char decode_char( WCHAR c
)
2473 if (c
>= 'A' && c
<= 'Z') return c
- 'A';
2474 if (c
>= 'a' && c
<= 'z') return c
- 'a' + 26;
2475 if (c
>= '0' && c
<= '9') return c
- '0' + 52;
2476 if (c
== '+') return 62;
2477 if (c
== '/') return 63;
2481 static unsigned int decode_base64( const WCHAR
*base64
, unsigned int len
, char *buf
)
2484 char c0
, c1
, c2
, c3
;
2485 const WCHAR
*p
= base64
;
2489 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2490 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2491 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2492 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2496 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2497 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2498 buf
[i
+ 2] = (c2
<< 6) | c3
;
2506 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2507 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2509 if (buf
) buf
[i
] = (c0
<< 2) | (c1
>> 4);
2512 else if (p
[3] == '=')
2514 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2515 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2516 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2520 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2521 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2527 if ((c0
= decode_char( p
[0] )) > 63) return 0;
2528 if ((c1
= decode_char( p
[1] )) > 63) return 0;
2529 if ((c2
= decode_char( p
[2] )) > 63) return 0;
2530 if ((c3
= decode_char( p
[3] )) > 63) return 0;
2534 buf
[i
+ 0] = (c0
<< 2) | (c1
>> 4);
2535 buf
[i
+ 1] = (c1
<< 4) | (c2
>> 2);
2536 buf
[i
+ 2] = (c2
<< 6) | c3
;
2543 static struct authinfo
*alloc_authinfo(void)
2545 struct authinfo
*ret
;
2547 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
) ))) return NULL
;
2549 SecInvalidateHandle(&ret
->cred
);
2550 SecInvalidateHandle(&ret
->ctx
);
2551 memset(&ret
->exp
, 0, sizeof(ret
->exp
));
2557 ret
->finished
= FALSE
;
2561 static void destroy_authinfo(struct authinfo
*info
)
2565 if (SecIsValidHandle(&info
->ctx
))
2566 DeleteSecurityContext(&info
->ctx
);
2567 if (SecIsValidHandle(&info
->cred
))
2568 FreeCredentialsHandle(&info
->cred
);
2570 HeapFree(GetProcessHeap(), 0, info
->data
);
2571 HeapFree(GetProcessHeap(), 0, info
);
2574 static const WCHAR basicW
[] = {'B','a','s','i','c',0};
2575 static const WCHAR ntlmW
[] = {'N','T','L','M',0};
2576 static const WCHAR passportW
[] = {'P','a','s','s','p','o','r','t',0};
2577 static const WCHAR digestW
[] = {'D','i','g','e','s','t',0};
2578 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2588 { basicW
, ARRAYSIZE(basicW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_BASIC
},
2589 { ntlmW
, ARRAYSIZE(ntlmW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NTLM
},
2590 { passportW
, ARRAYSIZE(passportW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_PASSPORT
},
2591 { digestW
, ARRAYSIZE(digestW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_DIGEST
},
2592 { negotiateW
, ARRAYSIZE(negotiateW
) - 1, RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
}
2594 static const unsigned int num_auth_schemes
= sizeof(auth_schemes
)/sizeof(auth_schemes
[0]);
2596 static DWORD
auth_scheme_from_header( const WCHAR
*header
)
2599 for (i
= 0; i
< num_auth_schemes
; i
++)
2601 if (!strncmpiW( header
, auth_schemes
[i
].str
, auth_schemes
[i
].len
) &&
2602 (header
[auth_schemes
[i
].len
] == ' ' || !header
[auth_schemes
[i
].len
])) return auth_schemes
[i
].scheme
;
2607 static BOOL
get_authvalue(HINTERNET request
, DWORD scheme
, WCHAR
*buffer
, DWORD buflen
)
2609 DWORD len
, index
= 0;
2613 if (!HttpQueryInfoW(request
, HTTP_QUERY_WWW_AUTHENTICATE
, buffer
, &len
, &index
)) return FALSE
;
2614 if (auth_scheme_from_header(buffer
) == scheme
) break;
2619 static RPC_STATUS
do_authorization(HINTERNET request
, SEC_WCHAR
*servername
,
2620 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
, struct authinfo
**auth_ptr
)
2622 struct authinfo
*info
= *auth_ptr
;
2623 SEC_WINNT_AUTH_IDENTITY_W
*id
= creds
->TransportCredentials
;
2624 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2626 if ((!info
&& !(info
= alloc_authinfo()))) return RPC_S_SERVER_UNAVAILABLE
;
2628 switch (creds
->AuthnSchemes
[0])
2630 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2632 int userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2633 int passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2635 info
->data_len
= userlen
+ passlen
+ 1;
2636 if (!(info
->data
= HeapAlloc(GetProcessHeap(), 0, info
->data_len
)))
2638 status
= RPC_S_OUT_OF_MEMORY
;
2641 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, info
->data
, userlen
, NULL
, NULL
);
2642 info
->data
[userlen
] = ':';
2643 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, info
->data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2645 info
->scheme
= RPC_C_HTTP_AUTHN_SCHEME_BASIC
;
2646 info
->finished
= TRUE
;
2650 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2651 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2654 static SEC_WCHAR ntlmW
[] = {'N','T','L','M',0}, negotiateW
[] = {'N','e','g','o','t','i','a','t','e',0};
2655 SECURITY_STATUS ret
;
2656 SecBufferDesc out_desc
, in_desc
;
2658 ULONG flags
= ISC_REQ_CONNECTION
|ISC_REQ_USE_DCE_STYLE
|ISC_REQ_MUTUAL_AUTH
|ISC_REQ_DELEGATE
;
2662 WCHAR auth_value
[2048];
2663 DWORD size
= sizeof(auth_value
);
2666 if (creds
->AuthnSchemes
[0] == RPC_C_HTTP_AUTHN_SCHEME_NTLM
) scheme
= ntlmW
;
2667 else scheme
= negotiateW
;
2668 scheme_len
= strlenW( scheme
);
2673 SecPkgInfoW
*pkg_info
;
2675 ret
= AcquireCredentialsHandleW(NULL
, scheme
, SECPKG_CRED_OUTBOUND
, NULL
, id
, NULL
, NULL
, &info
->cred
, &exp
);
2676 if (ret
!= SEC_E_OK
) break;
2678 ret
= QuerySecurityPackageInfoW(scheme
, &pkg_info
);
2679 if (ret
!= SEC_E_OK
) break;
2681 info
->max_token
= pkg_info
->cbMaxToken
;
2682 FreeContextBuffer(pkg_info
);
2687 if (info
->finished
|| !get_authvalue(request
, creds
->AuthnSchemes
[0], auth_value
, size
)) break;
2688 if (auth_scheme_from_header(auth_value
) != info
->scheme
)
2690 ERR("authentication scheme changed\n");
2694 in
.BufferType
= SECBUFFER_TOKEN
;
2698 in_desc
.ulVersion
= 0;
2699 in_desc
.cBuffers
= 1;
2700 in_desc
.pBuffers
= &in
;
2702 p
= auth_value
+ scheme_len
;
2703 if (!first
&& *p
== ' ')
2705 int len
= strlenW(++p
);
2706 in
.cbBuffer
= decode_base64(p
, len
, NULL
);
2707 if (!(in
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, in
.cbBuffer
))) break;
2708 decode_base64(p
, len
, in
.pvBuffer
);
2710 out
.BufferType
= SECBUFFER_TOKEN
;
2711 out
.cbBuffer
= info
->max_token
;
2712 if (!(out
.pvBuffer
= HeapAlloc(GetProcessHeap(), 0, out
.cbBuffer
)))
2714 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2717 out_desc
.ulVersion
= 0;
2718 out_desc
.cBuffers
= 1;
2719 out_desc
.pBuffers
= &out
;
2721 ret
= InitializeSecurityContextW(first
? &info
->cred
: NULL
, first
? NULL
: &info
->ctx
,
2722 first
? servername
: NULL
, flags
, 0, SECURITY_NETWORK_DREP
,
2723 in
.pvBuffer
? &in_desc
: NULL
, 0, &info
->ctx
, &out_desc
,
2724 &info
->attr
, &info
->exp
);
2725 HeapFree(GetProcessHeap(), 0, in
.pvBuffer
);
2726 if (ret
== SEC_E_OK
)
2728 HeapFree(GetProcessHeap(), 0, info
->data
);
2729 info
->data
= out
.pvBuffer
;
2730 info
->data_len
= out
.cbBuffer
;
2731 info
->finished
= TRUE
;
2732 TRACE("sending last auth packet\n");
2735 else if (ret
== SEC_I_CONTINUE_NEEDED
)
2737 HeapFree(GetProcessHeap(), 0, info
->data
);
2738 info
->data
= out
.pvBuffer
;
2739 info
->data_len
= out
.cbBuffer
;
2740 TRACE("sending next auth packet\n");
2745 ERR("InitializeSecurityContextW failed with error 0x%08x\n", ret
);
2746 HeapFree(GetProcessHeap(), 0, out
.pvBuffer
);
2749 info
->scheme
= creds
->AuthnSchemes
[0];
2753 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2757 if (status
!= RPC_S_OK
)
2759 destroy_authinfo(info
);
2767 static RPC_STATUS
insert_authorization_header(HINTERNET request
, ULONG scheme
, char *data
, int data_len
)
2769 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' '};
2770 static const WCHAR basicW
[] = {'B','a','s','i','c',' '};
2771 static const WCHAR negotiateW
[] = {'N','e','g','o','t','i','a','t','e',' '};
2772 static const WCHAR ntlmW
[] = {'N','T','L','M',' '};
2773 int scheme_len
, auth_len
= sizeof(authW
) / sizeof(authW
[0]), len
= ((data_len
+ 2) * 4) / 3;
2774 const WCHAR
*scheme_str
;
2775 WCHAR
*header
, *ptr
;
2776 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2780 case RPC_C_HTTP_AUTHN_SCHEME_BASIC
:
2781 scheme_str
= basicW
;
2782 scheme_len
= sizeof(basicW
) / sizeof(basicW
[0]);
2784 case RPC_C_HTTP_AUTHN_SCHEME_NEGOTIATE
:
2785 scheme_str
= negotiateW
;
2786 scheme_len
= sizeof(negotiateW
) / sizeof(negotiateW
[0]);
2788 case RPC_C_HTTP_AUTHN_SCHEME_NTLM
:
2790 scheme_len
= sizeof(ntlmW
) / sizeof(ntlmW
[0]);
2793 ERR("unknown scheme %u\n", scheme
);
2794 return RPC_S_SERVER_UNAVAILABLE
;
2796 if ((header
= HeapAlloc(GetProcessHeap(), 0, (auth_len
+ scheme_len
+ len
+ 2) * sizeof(WCHAR
))))
2798 memcpy(header
, authW
, auth_len
* sizeof(WCHAR
));
2799 ptr
= header
+ auth_len
;
2800 memcpy(ptr
, scheme_str
, scheme_len
* sizeof(WCHAR
));
2802 len
= encode_base64(data
, data_len
, ptr
);
2806 if (HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD
|HTTP_ADDREQ_FLAG_REPLACE
))
2808 HeapFree(GetProcessHeap(), 0, header
);
2813 static void drain_content(HINTERNET request
)
2815 DWORD count
, len
= 0, size
= sizeof(len
);
2818 HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
, &len
, &size
, NULL
);
2822 count
= min(sizeof(buf
), len
);
2823 if (!InternetReadFile(request
, buf
, count
, &count
) || !count
) return;
2828 static RPC_STATUS
authorize_request(RpcConnection_http
*httpc
, HINTERNET request
)
2830 static const WCHAR authW
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',':','\r','\n',0};
2831 struct authinfo
*info
= NULL
;
2837 status
= do_authorization(request
, httpc
->servername
, httpc
->common
.QOS
->qos
->u
.HttpCredentials
, &info
);
2838 if (status
!= RPC_S_OK
) break;
2840 status
= insert_authorization_header(request
, info
->scheme
, info
->data
, info
->data_len
);
2841 if (status
!= RPC_S_OK
) break;
2843 prepare_async_request(httpc
->async_data
);
2844 ret
= HttpSendRequestW(request
, NULL
, 0, NULL
, 0);
2845 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2846 if (status
!= RPC_S_OK
|| info
->finished
) break;
2848 status
= rpcrt4_http_check_response(request
);
2849 if (status
!= RPC_S_OK
&& status
!= ERROR_ACCESS_DENIED
) break;
2850 drain_content(request
);
2853 if (info
->scheme
!= RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2854 HttpAddRequestHeadersW(request
, authW
, -1, HTTP_ADDREQ_FLAG_REPLACE
);
2856 destroy_authinfo(info
);
2860 static RPC_STATUS
insert_cookie_header(HINTERNET request
, const WCHAR
*value
)
2862 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
2863 WCHAR
*header
, *ptr
;
2865 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2867 if (!value
) return RPC_S_OK
;
2869 len
= strlenW(value
);
2870 if ((header
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookieW
) + (len
+ 3) * sizeof(WCHAR
))))
2872 memcpy(header
, cookieW
, sizeof(cookieW
));
2873 ptr
= header
+ sizeof(cookieW
) / sizeof(cookieW
[0]);
2874 memcpy(ptr
, value
, len
* sizeof(WCHAR
));
2878 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW
))) status
= RPC_S_OK
;
2879 HeapFree(GetProcessHeap(), 0, header
);
2884 static BOOL
has_credentials(RpcConnection_http
*httpc
)
2886 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
2887 SEC_WINNT_AUTH_IDENTITY_W
*id
;
2889 if (!httpc
->common
.QOS
|| httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
2892 creds
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2893 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
2896 id
= creds
->TransportCredentials
;
2897 if (!id
|| !id
->User
|| !id
->Password
) return FALSE
;
2902 static BOOL
is_secure(RpcConnection_http
*httpc
)
2904 return httpc
->common
.QOS
&&
2905 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2906 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2909 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2911 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2912 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2913 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2914 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2915 static const WCHAR wszColon
[] = {':',0};
2916 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2917 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2921 BOOL secure
, credentials
;
2922 HttpTimerThreadData
*timer_data
;
2925 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2927 if (Connection
->server
)
2929 ERR("ncacn_http servers not supported yet\n");
2930 return RPC_S_SERVER_UNAVAILABLE
;
2933 if (httpc
->in_request
)
2936 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2938 status
= UuidCreate(&httpc
->connection_uuid
);
2939 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2940 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2942 status
= rpcrt4_http_internet_connect(httpc
);
2943 if (status
!= RPC_S_OK
)
2946 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2948 return RPC_S_OUT_OF_MEMORY
;
2949 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2950 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2951 strcatW(url
, wszColon
);
2952 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2954 secure
= is_secure(httpc
);
2955 credentials
= has_credentials(httpc
);
2957 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
2958 INTERNET_FLAG_NO_AUTO_REDIRECT
;
2959 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
2960 if (credentials
) flags
|= INTERNET_FLAG_NO_AUTH
;
2962 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
2963 flags
, (DWORD_PTR
)httpc
->async_data
);
2964 if (!httpc
->in_request
)