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 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
84 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
86 /**** ncacn_np support ****/
88 typedef struct _RpcConnection_np
96 static RpcConnection
*rpcrt4_conn_np_alloc(void)
98 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcConnection_np
));
102 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
103 npc
->listening
= FALSE
;
108 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
113 npc
->listening
= TRUE
;
116 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
119 switch(GetLastError())
121 case ERROR_PIPE_CONNECTED
:
122 SetEvent(npc
->ovl
.hEvent
);
124 case ERROR_IO_PENDING
:
125 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
127 case ERROR_NO_DATA_DETECTED
:
128 /* client has disconnected, retry */
129 DisconnectNamedPipe( npc
->pipe
);
132 npc
->listening
= FALSE
;
133 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
134 return RPC_S_OUT_OF_RESOURCES
;
139 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
141 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
142 TRACE("listening on %s\n", pname
);
144 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
145 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
146 PIPE_UNLIMITED_INSTANCES
,
147 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
148 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
149 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
150 if (GetLastError() == ERROR_FILE_EXISTS
)
151 return RPC_S_DUPLICATE_ENDPOINT
;
153 return RPC_S_CANT_CREATE_ENDPOINT
;
156 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
157 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
159 /* Note: we don't call ConnectNamedPipe here because it must be done in the
160 * server thread as the thread must be alertable */
164 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
166 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
170 TRACE("connecting to %s\n", pname
);
176 dwFlags
= SECURITY_SQOS_PRESENT
;
177 switch (Connection
->QOS
->qos
->ImpersonationType
)
179 case RPC_C_IMP_LEVEL_DEFAULT
:
180 /* FIXME: what to do here? */
182 case RPC_C_IMP_LEVEL_ANONYMOUS
:
183 dwFlags
|= SECURITY_ANONYMOUS
;
185 case RPC_C_IMP_LEVEL_IDENTIFY
:
186 dwFlags
|= SECURITY_IDENTIFICATION
;
188 case RPC_C_IMP_LEVEL_IMPERSONATE
:
189 dwFlags
|= SECURITY_IMPERSONATION
;
191 case RPC_C_IMP_LEVEL_DELEGATE
:
192 dwFlags
|= SECURITY_DELEGATION
;
195 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
196 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
198 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
199 OPEN_EXISTING
, dwFlags
, 0);
200 if (pipe
!= INVALID_HANDLE_VALUE
) break;
201 err
= GetLastError();
202 if (err
== ERROR_PIPE_BUSY
) {
203 TRACE("connection failed, error=%x\n", err
);
204 return RPC_S_SERVER_TOO_BUSY
;
205 } else if (err
== ERROR_BAD_NETPATH
) {
206 TRACE("connection failed, error=%x\n", err
);
207 return RPC_S_SERVER_UNAVAILABLE
;
209 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
210 err
= GetLastError();
211 WARN("connection failed, error=%x\n", err
);
212 return RPC_S_SERVER_UNAVAILABLE
;
217 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
218 /* pipe is connected; change to message-read mode. */
219 dwMode
= PIPE_READMODE_MESSAGE
;
220 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
221 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
227 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
229 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
230 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
234 /* already connected? */
238 /* protseq=ncalrpc: supposed to use NT LPC ports,
239 * but we'll implement it with named pipes for now */
240 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
241 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
242 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
248 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
250 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
253 RpcConnection
*Connection
;
254 char generated_endpoint
[22];
258 static LONG lrpc_nameless_id
;
259 DWORD process_id
= GetCurrentProcessId();
260 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
261 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
262 "LRPC%08x.%08x", process_id
, id
);
263 endpoint
= generated_endpoint
;
266 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
267 endpoint
, NULL
, NULL
, NULL
, NULL
);
271 /* protseq=ncalrpc: supposed to use NT LPC ports,
272 * but we'll implement it with named pipes for now */
273 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
274 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
275 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
278 EnterCriticalSection(&protseq
->cs
);
279 Connection
->Next
= protseq
->conn
;
280 protseq
->conn
= Connection
;
281 LeaveCriticalSection(&protseq
->cs
);
286 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
288 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
289 static const char prefix
[] = "\\\\";
290 static const char local
[] =".";
295 /* already connected? */
299 /* protseq=ncacn_np: named pipes */
300 size
= strlen(prefix
);
301 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
302 size
+= strlen(local
);
304 size
+= strlen(Connection
->NetworkAddr
);
305 size
+= strlen(Connection
->Endpoint
) + 1;
307 pname
= I_RpcAllocate(size
);
308 strcpy(pname
, prefix
);
309 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
310 strcat(pname
, local
);
312 strcat(pname
, Connection
->NetworkAddr
);
313 strcat(pname
, Connection
->Endpoint
);
314 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
320 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
322 static const char prefix
[] = "\\\\.";
325 RpcConnection
*Connection
;
326 char generated_endpoint
[21];
330 static LONG np_nameless_id
;
331 DWORD process_id
= GetCurrentProcessId();
332 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
333 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
334 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
335 endpoint
= generated_endpoint
;
338 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
339 endpoint
, NULL
, NULL
, NULL
, NULL
);
343 /* protseq=ncacn_np: named pipes */
344 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
345 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
346 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
349 EnterCriticalSection(&protseq
->cs
);
350 Connection
->Next
= protseq
->conn
;
351 protseq
->conn
= Connection
;
352 LeaveCriticalSection(&protseq
->cs
);
357 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
359 /* because of the way named pipes work, we'll transfer the connected pipe
360 * to the child, then reopen the server binding to continue listening */
362 new_npc
->pipe
= old_npc
->pipe
;
363 new_npc
->ovl
= old_npc
->ovl
;
365 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
366 old_npc
->listening
= FALSE
;
369 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
373 static const char prefix
[] = "\\\\.";
375 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
377 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
378 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
379 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
385 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
389 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
391 TRACE("%s\n", old_conn
->Endpoint
);
393 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
395 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
396 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
397 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
403 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
404 void *buffer
, unsigned int count
)
406 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
409 unsigned int bytes_left
= count
;
412 ZeroMemory(&ovl
, sizeof(ovl
));
413 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
418 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
419 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
420 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
421 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
423 if (!ret
|| !bytes_read
)
425 bytes_left
-= bytes_read
;
428 CloseHandle(ovl
.hEvent
);
429 return ret
? count
: -1;
432 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
433 const void *buffer
, unsigned int count
)
435 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
436 const char *buf
= buffer
;
438 unsigned int bytes_left
= count
;
441 ZeroMemory(&ovl
, sizeof(ovl
));
442 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
447 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
448 if (!ret
&& GetLastError() == ERROR_IO_PENDING
)
449 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
450 if (!ret
|| !bytes_written
)
452 bytes_left
-= bytes_written
;
453 buf
+= bytes_written
;
455 CloseHandle(ovl
.hEvent
);
456 return ret
? count
: -1;
459 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
461 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
463 FlushFileBuffers(npc
->pipe
);
464 CloseHandle(npc
->pipe
);
467 if (npc
->ovl
.hEvent
) {
468 CloseHandle(npc
->ovl
.hEvent
);
474 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
476 /* FIXME: implement when named pipe writes use overlapped I/O */
479 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
481 /* FIXME: implement when named pipe writes use overlapped I/O */
485 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
486 const char *networkaddr
,
487 const char *endpoint
)
489 twr_empty_floor_t
*smb_floor
;
490 twr_empty_floor_t
*nb_floor
;
492 size_t networkaddr_size
;
493 size_t endpoint_size
;
495 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
497 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
498 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
499 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
504 smb_floor
= (twr_empty_floor_t
*)tower_data
;
506 tower_data
+= sizeof(*smb_floor
);
508 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
509 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
510 smb_floor
->count_rhs
= endpoint_size
;
513 memcpy(tower_data
, endpoint
, endpoint_size
);
516 tower_data
+= endpoint_size
;
518 nb_floor
= (twr_empty_floor_t
*)tower_data
;
520 tower_data
+= sizeof(*nb_floor
);
522 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
523 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
524 nb_floor
->count_rhs
= networkaddr_size
;
527 memcpy(tower_data
, networkaddr
, networkaddr_size
);
534 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
539 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
540 const twr_empty_floor_t
*nb_floor
;
542 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
544 if (tower_size
< sizeof(*smb_floor
))
545 return EPT_S_NOT_REGISTERED
;
547 tower_data
+= sizeof(*smb_floor
);
548 tower_size
-= sizeof(*smb_floor
);
550 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
551 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
552 (smb_floor
->count_rhs
> tower_size
) ||
553 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
554 return EPT_S_NOT_REGISTERED
;
558 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
560 return RPC_S_OUT_OF_RESOURCES
;
561 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
563 tower_data
+= smb_floor
->count_rhs
;
564 tower_size
-= smb_floor
->count_rhs
;
566 if (tower_size
< sizeof(*nb_floor
))
567 return EPT_S_NOT_REGISTERED
;
569 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
571 tower_data
+= sizeof(*nb_floor
);
572 tower_size
-= sizeof(*nb_floor
);
574 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
575 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
576 (nb_floor
->count_rhs
> tower_size
) ||
577 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
578 return EPT_S_NOT_REGISTERED
;
582 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
587 I_RpcFree(*endpoint
);
590 return RPC_S_OUT_OF_RESOURCES
;
592 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
598 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
600 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
603 TRACE("(%p)\n", conn
);
605 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
606 return RPCRT4_default_impersonate_client(conn
);
608 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
611 DWORD error
= GetLastError();
612 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
615 case ERROR_CANNOT_IMPERSONATE
:
616 return RPC_S_NO_CONTEXT_AVAILABLE
;
622 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
626 TRACE("(%p)\n", conn
);
628 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
629 return RPCRT4_default_revert_to_self(conn
);
631 ret
= RevertToSelf();
634 WARN("RevertToSelf failed with error %u\n", GetLastError());
635 return RPC_S_NO_CONTEXT_AVAILABLE
;
640 typedef struct _RpcServerProtseq_np
642 RpcServerProtseq common
;
644 } RpcServerProtseq_np
;
646 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
648 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
650 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
654 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
656 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
657 SetEvent(npps
->mgr_event
);
660 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
662 HANDLE
*objs
= prev_array
;
663 RpcConnection_np
*conn
;
664 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
666 EnterCriticalSection(&protseq
->cs
);
668 /* open and count connections */
670 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
672 rpcrt4_conn_listen_pipe(conn
);
673 if (conn
->ovl
.hEvent
)
675 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
678 /* make array of connections */
680 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
682 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
685 ERR("couldn't allocate objs\n");
686 LeaveCriticalSection(&protseq
->cs
);
690 objs
[0] = npps
->mgr_event
;
692 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
694 if ((objs
[*count
] = conn
->ovl
.hEvent
))
696 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
698 LeaveCriticalSection(&protseq
->cs
);
702 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
704 HeapFree(GetProcessHeap(), 0, array
);
707 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
710 HANDLE
*objs
= wait_array
;
712 RpcConnection
*cconn
;
713 RpcConnection_np
*conn
;
720 /* an alertable wait isn't strictly necessary, but due to our
721 * overlapped I/O implementation in Wine we need to free some memory
722 * by the file user APC being called, even if no completion routine was
723 * specified at the time of starting the async operation */
724 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
725 } while (res
== WAIT_IO_COMPLETION
);
727 if (res
== WAIT_OBJECT_0
)
729 else if (res
== WAIT_FAILED
)
731 ERR("wait failed with error %d\n", GetLastError());
736 b_handle
= objs
[res
- WAIT_OBJECT_0
];
737 /* find which connection got a RPC */
738 EnterCriticalSection(&protseq
->cs
);
739 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
741 if (b_handle
== conn
->ovl
.hEvent
) break;
742 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
746 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
748 ERR("failed to locate connection for handle %p\n", b_handle
);
749 LeaveCriticalSection(&protseq
->cs
);
752 RPCRT4_new_client(cconn
);
759 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
760 const char *networkaddr
,
761 const char *endpoint
)
763 twr_empty_floor_t
*pipe_floor
;
765 size_t endpoint_size
;
767 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
769 endpoint_size
= strlen(endpoint
) + 1;
770 size
= sizeof(*pipe_floor
) + endpoint_size
;
775 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
777 tower_data
+= sizeof(*pipe_floor
);
779 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
780 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
781 pipe_floor
->count_rhs
= endpoint_size
;
783 memcpy(tower_data
, endpoint
, endpoint_size
);
788 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
793 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
795 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
797 if (tower_size
< sizeof(*pipe_floor
))
798 return EPT_S_NOT_REGISTERED
;
800 tower_data
+= sizeof(*pipe_floor
);
801 tower_size
-= sizeof(*pipe_floor
);
803 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
804 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
805 (pipe_floor
->count_rhs
> tower_size
) ||
806 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
807 return EPT_S_NOT_REGISTERED
;
814 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
816 return RPC_S_OUT_OF_RESOURCES
;
817 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
823 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
828 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
829 unsigned char *in_buffer
,
830 unsigned int in_size
,
831 unsigned char *out_buffer
,
832 unsigned int *out_size
)
834 /* since this protocol is local to the machine there is no need to
835 * authenticate the caller */
840 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
841 enum secure_packet_direction dir
,
842 RpcPktHdr
*hdr
, unsigned int hdr_size
,
843 unsigned char *stub_data
, unsigned int stub_data_size
,
844 RpcAuthVerifier
*auth_hdr
,
845 unsigned char *auth_value
, unsigned int auth_value_size
)
847 /* since this protocol is local to the machine there is no need to secure
852 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
853 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
854 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
856 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
857 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
861 FIXME("privs not implemented\n");
864 if (server_princ_name
)
866 FIXME("server_princ_name not implemented\n");
867 *server_princ_name
= NULL
;
869 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
870 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
873 FIXME("authorization service not implemented\n");
874 *authz_svc
= RPC_C_AUTHZ_NONE
;
877 FIXME("flags 0x%x not implemented\n", flags
);
882 /**** ncacn_ip_tcp support ****/
884 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
885 const char *networkaddr
,
886 unsigned char tcp_protid
,
887 const char *endpoint
)
889 twr_tcp_floor_t
*tcp_floor
;
890 twr_ipv4_floor_t
*ipv4_floor
;
892 struct addrinfo hints
;
894 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
896 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
901 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
902 tower_data
+= sizeof(*tcp_floor
);
904 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
906 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
907 tcp_floor
->protid
= tcp_protid
;
908 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
910 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
911 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
912 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
914 hints
.ai_flags
= AI_NUMERICHOST
;
915 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
916 hints
.ai_family
= PF_INET
;
917 hints
.ai_socktype
= SOCK_STREAM
;
918 hints
.ai_protocol
= IPPROTO_TCP
;
919 hints
.ai_addrlen
= 0;
920 hints
.ai_addr
= NULL
;
921 hints
.ai_canonname
= NULL
;
922 hints
.ai_next
= NULL
;
924 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
927 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
930 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
935 if (ai
->ai_family
== PF_INET
)
937 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
938 tcp_floor
->port
= sin
->sin_port
;
939 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
943 ERR("unexpected protocol family %d\n", ai
->ai_family
);
952 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
955 unsigned char tcp_protid
,
958 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
959 const twr_ipv4_floor_t
*ipv4_floor
;
960 struct in_addr in_addr
;
962 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
964 if (tower_size
< sizeof(*tcp_floor
))
965 return EPT_S_NOT_REGISTERED
;
967 tower_data
+= sizeof(*tcp_floor
);
968 tower_size
-= sizeof(*tcp_floor
);
970 if (tower_size
< sizeof(*ipv4_floor
))
971 return EPT_S_NOT_REGISTERED
;
973 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
975 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
976 (tcp_floor
->protid
!= tcp_protid
) ||
977 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
978 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
979 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
980 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
981 return EPT_S_NOT_REGISTERED
;
985 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
987 return RPC_S_OUT_OF_RESOURCES
;
988 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
993 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
998 I_RpcFree(*endpoint
);
1001 return RPC_S_OUT_OF_RESOURCES
;
1003 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1004 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1006 ERR("inet_ntop: %s\n", strerror(errno
));
1007 I_RpcFree(*networkaddr
);
1008 *networkaddr
= NULL
;
1011 I_RpcFree(*endpoint
);
1014 return EPT_S_NOT_REGISTERED
;
1021 typedef struct _RpcConnection_tcp
1023 RpcConnection common
;
1025 #ifdef HAVE_SOCKETPAIR
1029 HANDLE cancel_event
;
1031 } RpcConnection_tcp
;
1033 #ifdef HAVE_SOCKETPAIR
1035 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1037 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1039 ERR("socketpair() failed: %s\n", strerror(errno
));
1045 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1047 struct pollfd pfds
[2];
1048 pfds
[0].fd
= tcpc
->sock
;
1049 pfds
[0].events
= POLLIN
;
1050 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1051 pfds
[1].events
= POLLIN
;
1052 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1054 ERR("poll() failed: %s\n", strerror(errno
));
1057 if (pfds
[1].revents
& POLLIN
) /* canceled */
1060 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1066 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1069 pfd
.fd
= tcpc
->sock
;
1070 pfd
.events
= POLLOUT
;
1071 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1073 ERR("poll() failed: %s\n", strerror(errno
));
1079 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1083 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1086 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1088 close(tcpc
->cancel_fds
[0]);
1089 close(tcpc
->cancel_fds
[1]);
1092 #else /* HAVE_SOCKETPAIR */
1094 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1096 static BOOL wsa_inited
;
1100 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1101 /* Note: WSAStartup can be called more than once so we don't bother with
1102 * making accesses to wsa_inited thread-safe */
1105 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1106 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1107 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1109 ERR("event creation failed\n");
1110 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1116 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1118 HANDLE wait_handles
[2];
1120 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1122 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1125 wait_handles
[0] = tcpc
->sock_event
;
1126 wait_handles
[1] = tcpc
->cancel_event
;
1127 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1132 case WAIT_OBJECT_0
+ 1:
1135 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1140 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1143 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1145 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1148 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1154 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1159 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1161 SetEvent(tcpc
->cancel_event
);
1164 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1166 CloseHandle(tcpc
->sock_event
);
1167 CloseHandle(tcpc
->cancel_event
);
1172 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1174 RpcConnection_tcp
*tcpc
;
1175 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1179 if (!rpcrt4_sock_wait_init(tcpc
))
1181 HeapFree(GetProcessHeap(), 0, tcpc
);
1184 return &tcpc
->common
;
1187 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1189 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1192 struct addrinfo
*ai
;
1193 struct addrinfo
*ai_cur
;
1194 struct addrinfo hints
;
1196 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1198 if (tcpc
->sock
!= -1)
1202 hints
.ai_family
= PF_UNSPEC
;
1203 hints
.ai_socktype
= SOCK_STREAM
;
1204 hints
.ai_protocol
= IPPROTO_TCP
;
1205 hints
.ai_addrlen
= 0;
1206 hints
.ai_addr
= NULL
;
1207 hints
.ai_canonname
= NULL
;
1208 hints
.ai_next
= NULL
;
1210 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1213 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1214 Connection
->Endpoint
, gai_strerror(ret
));
1215 return RPC_S_SERVER_UNAVAILABLE
;
1218 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1223 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1225 TRACE("skipping non-IP/IPv6 address family\n");
1233 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1234 host
, sizeof(host
), service
, sizeof(service
),
1235 NI_NUMERICHOST
| NI_NUMERICSERV
);
1236 TRACE("trying %s:%s\n", host
, service
);
1239 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1242 WARN("socket() failed: %s\n", strerror(errno
));
1246 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1248 WARN("connect() failed: %s\n", strerror(errno
));
1253 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1255 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1257 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1262 TRACE("connected\n");
1267 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1268 return RPC_S_SERVER_UNAVAILABLE
;
1271 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1273 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1276 struct addrinfo
*ai
;
1277 struct addrinfo
*ai_cur
;
1278 struct addrinfo hints
;
1279 RpcConnection
*first_connection
= NULL
;
1281 TRACE("(%p, %s)\n", protseq
, endpoint
);
1283 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1284 hints
.ai_family
= PF_UNSPEC
;
1285 hints
.ai_socktype
= SOCK_STREAM
;
1286 hints
.ai_protocol
= IPPROTO_TCP
;
1287 hints
.ai_addrlen
= 0;
1288 hints
.ai_addr
= NULL
;
1289 hints
.ai_canonname
= NULL
;
1290 hints
.ai_next
= NULL
;
1292 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1295 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1297 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1298 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1299 return RPC_S_CANT_CREATE_ENDPOINT
;
1302 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1304 RpcConnection_tcp
*tcpc
;
1305 RPC_STATUS create_status
;
1306 struct sockaddr_storage sa
;
1308 char service
[NI_MAXSERV
];
1311 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1313 TRACE("skipping non-IP/IPv6 address family\n");
1320 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1321 host
, sizeof(host
), service
, sizeof(service
),
1322 NI_NUMERICHOST
| NI_NUMERICSERV
);
1323 TRACE("trying %s:%s\n", host
, service
);
1326 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1329 WARN("socket() failed: %s\n", strerror(errno
));
1330 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1334 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1337 WARN("bind failed: %s\n", strerror(errno
));
1339 if (errno
== EADDRINUSE
)
1340 status
= RPC_S_DUPLICATE_ENDPOINT
;
1342 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1346 sa_len
= sizeof(sa
);
1347 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1349 WARN("getsockname() failed: %s\n", strerror(errno
));
1351 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1355 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1356 NULL
, 0, service
, sizeof(service
),
1360 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1362 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1366 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1367 protseq
->Protseq
, NULL
,
1368 service
, NULL
, NULL
, NULL
, NULL
);
1369 if (create_status
!= RPC_S_OK
)
1372 status
= create_status
;
1377 ret
= listen(sock
, protseq
->MaxCalls
);
1380 WARN("listen failed: %s\n", strerror(errno
));
1381 RPCRT4_ReleaseConnection(&tcpc
->common
);
1382 status
= RPC_S_OUT_OF_RESOURCES
;
1385 /* need a non-blocking socket, otherwise accept() has a potential
1386 * race-condition (poll() says it is readable, connection drops,
1387 * and accept() blocks until the next connection comes...)
1390 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1393 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1394 RPCRT4_ReleaseConnection(&tcpc
->common
);
1395 status
= RPC_S_OUT_OF_RESOURCES
;
1399 tcpc
->common
.Next
= first_connection
;
1400 first_connection
= &tcpc
->common
;
1402 /* since IPv4 and IPv6 share the same port space, we only need one
1403 * successful bind to listen for both */
1409 /* if at least one connection was created for an endpoint then
1411 if (first_connection
)
1413 RpcConnection
*conn
;
1415 /* find last element in list */
1416 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1419 EnterCriticalSection(&protseq
->cs
);
1420 conn
->Next
= protseq
->conn
;
1421 protseq
->conn
= first_connection
;
1422 LeaveCriticalSection(&protseq
->cs
);
1424 TRACE("listening on %s\n", endpoint
);
1428 ERR("couldn't listen on port %s\n", endpoint
);
1432 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1435 struct sockaddr_in address
;
1437 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1438 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1441 addrsize
= sizeof(address
);
1442 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1445 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1446 return RPC_S_OUT_OF_RESOURCES
;
1449 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1451 TRACE("Accepted a new TCP connection\n");
1455 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1456 void *buffer
, unsigned int count
)
1458 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1460 while (bytes_read
!= count
)
1462 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1467 else if (errno
!= EAGAIN
)
1469 WARN("recv() failed: %s\n", strerror(errno
));
1474 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1478 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1482 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1483 const void *buffer
, unsigned int count
)
1485 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1486 int bytes_written
= 0;
1487 while (bytes_written
!= count
)
1489 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1492 else if (errno
!= EAGAIN
)
1496 if (!rpcrt4_sock_wait_for_send(tcpc
))
1500 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1501 return bytes_written
;
1504 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1506 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1508 TRACE("%d\n", tcpc
->sock
);
1510 if (tcpc
->sock
!= -1)
1511 closesocket(tcpc
->sock
);
1513 rpcrt4_sock_wait_destroy(tcpc
);
1517 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1519 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1520 TRACE("%p\n", Connection
);
1521 rpcrt4_sock_wait_cancel(tcpc
);
1524 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1526 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1528 TRACE("%p\n", Connection
);
1530 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1535 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1536 const char *networkaddr
,
1537 const char *endpoint
)
1539 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1540 EPM_PROTOCOL_TCP
, endpoint
);
1543 #ifdef HAVE_SOCKETPAIR
1545 typedef struct _RpcServerProtseq_sock
1547 RpcServerProtseq common
;
1550 } RpcServerProtseq_sock
;
1552 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1554 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1558 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1560 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1561 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1562 ps
->mgr_event_rcv
= fds
[0];
1563 ps
->mgr_event_snd
= fds
[1];
1567 ERR("socketpair failed with error %s\n", strerror(errno
));
1568 HeapFree(GetProcessHeap(), 0, ps
);
1575 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1577 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1579 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1582 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1584 struct pollfd
*poll_info
= prev_array
;
1585 RpcConnection_tcp
*conn
;
1586 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1588 EnterCriticalSection(&protseq
->cs
);
1590 /* open and count connections */
1592 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1594 if (conn
->sock
!= -1)
1596 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1599 /* make array of connections */
1601 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1603 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1606 ERR("couldn't allocate poll_info\n");
1607 LeaveCriticalSection(&protseq
->cs
);
1611 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1612 poll_info
[0].events
= POLLIN
;
1614 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1616 if (conn
->sock
!= -1)
1618 poll_info
[*count
].fd
= conn
->sock
;
1619 poll_info
[*count
].events
= POLLIN
;
1622 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1624 LeaveCriticalSection(&protseq
->cs
);
1628 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1630 HeapFree(GetProcessHeap(), 0, array
);
1633 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1635 struct pollfd
*poll_info
= wait_array
;
1638 RpcConnection
*cconn
;
1639 RpcConnection_tcp
*conn
;
1644 ret
= poll(poll_info
, count
, -1);
1647 ERR("poll failed with error %d\n", ret
);
1651 for (i
= 0; i
< count
; i
++)
1652 if (poll_info
[i
].revents
& POLLIN
)
1654 /* RPC server event */
1658 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1662 /* find which connection got a RPC */
1663 EnterCriticalSection(&protseq
->cs
);
1664 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1666 if (poll_info
[i
].fd
== conn
->sock
) break;
1667 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1671 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1673 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1674 LeaveCriticalSection(&protseq
->cs
);
1676 RPCRT4_new_client(cconn
);
1684 #else /* HAVE_SOCKETPAIR */
1686 typedef struct _RpcServerProtseq_sock
1688 RpcServerProtseq common
;
1690 } RpcServerProtseq_sock
;
1692 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1694 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1697 static BOOL wsa_inited
;
1701 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1702 /* Note: WSAStartup can be called more than once so we don't bother with
1703 * making accesses to wsa_inited thread-safe */
1706 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1711 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1713 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1714 SetEvent(sockps
->mgr_event
);
1717 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1719 HANDLE
*objs
= prev_array
;
1720 RpcConnection_tcp
*conn
;
1721 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1723 EnterCriticalSection(&protseq
->cs
);
1725 /* open and count connections */
1727 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1730 if (conn
->sock
!= -1)
1732 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1735 /* make array of connections */
1737 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1739 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1742 ERR("couldn't allocate objs\n");
1743 LeaveCriticalSection(&protseq
->cs
);
1747 objs
[0] = sockps
->mgr_event
;
1749 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1752 if (conn
->sock
!= -1)
1754 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1755 if (res
== SOCKET_ERROR
)
1756 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1759 objs
[*count
] = conn
->sock_event
;
1763 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1765 LeaveCriticalSection(&protseq
->cs
);
1769 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1771 HeapFree(GetProcessHeap(), 0, array
);
1774 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1777 HANDLE
*objs
= wait_array
;
1779 RpcConnection
*cconn
;
1780 RpcConnection_tcp
*conn
;
1787 /* an alertable wait isn't strictly necessary, but due to our
1788 * overlapped I/O implementation in Wine we need to free some memory
1789 * by the file user APC being called, even if no completion routine was
1790 * specified at the time of starting the async operation */
1791 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1792 } while (res
== WAIT_IO_COMPLETION
);
1794 if (res
== WAIT_OBJECT_0
)
1796 else if (res
== WAIT_FAILED
)
1798 ERR("wait failed with error %d\n", GetLastError());
1803 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1804 /* find which connection got a RPC */
1805 EnterCriticalSection(&protseq
->cs
);
1806 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1809 if (b_handle
== conn
->sock_event
) break;
1810 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1814 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1816 ERR("failed to locate connection for handle %p\n", b_handle
);
1817 LeaveCriticalSection(&protseq
->cs
);
1820 RPCRT4_new_client(cconn
);
1827 #endif /* HAVE_SOCKETPAIR */
1829 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1834 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1835 networkaddr
, EPM_PROTOCOL_TCP
,
1839 /**** ncacn_http support ****/
1841 /* 60 seconds is the period native uses */
1842 #define HTTP_IDLE_TIME 60000
1844 /* reference counted to avoid a race between a cancelled call's connection
1845 * being destroyed and the asynchronous InternetReadFileEx call being
1847 typedef struct _RpcHttpAsyncData
1850 HANDLE completion_event
;
1852 INTERNET_BUFFERSA inet_buffers
;
1853 CRITICAL_SECTION cs
;
1856 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1858 return InterlockedIncrement(&data
->refs
);
1861 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1863 ULONG refs
= InterlockedDecrement(&data
->refs
);
1866 TRACE("destroying async data %p\n", data
);
1867 CloseHandle(data
->completion_event
);
1868 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1869 data
->cs
.DebugInfo
->Spare
[0] = 0;
1870 DeleteCriticalSection(&data
->cs
);
1871 HeapFree(GetProcessHeap(), 0, data
);
1876 static void prepare_async_request(RpcHttpAsyncData
*async_data
)
1878 ResetEvent(async_data
->completion_event
);
1879 RpcHttpAsyncData_AddRef(async_data
);
1882 static RPC_STATUS
wait_async_request(RpcHttpAsyncData
*async_data
, BOOL call_ret
, HANDLE cancel_event
)
1884 HANDLE handles
[2] = { async_data
->completion_event
, cancel_event
};
1888 RpcHttpAsyncData_Release(async_data
);
1892 if(GetLastError() != ERROR_IO_PENDING
) {
1893 RpcHttpAsyncData_Release(async_data
);
1894 ERR("Request failed with error %d\n", GetLastError());
1895 return RPC_S_SERVER_UNAVAILABLE
;
1898 res
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
1899 if(res
!= WAIT_OBJECT_0
) {
1900 TRACE("Cancelled\n");
1901 return RPC_S_CALL_CANCELLED
;
1904 if(async_data
->async_result
) {
1905 ERR("Async request failed with error %d\n", async_data
->async_result
);
1906 return RPC_S_SERVER_UNAVAILABLE
;
1912 typedef struct _RpcConnection_http
1914 RpcConnection common
;
1917 HINTERNET in_request
;
1918 HINTERNET out_request
;
1919 HANDLE timer_cancelled
;
1920 HANDLE cancel_event
;
1921 DWORD last_sent_time
;
1922 ULONG bytes_received
;
1923 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1924 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1925 UUID connection_uuid
;
1928 RpcHttpAsyncData
*async_data
;
1929 } RpcConnection_http
;
1931 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1933 RpcConnection_http
*httpc
;
1934 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1935 if (!httpc
) return NULL
;
1936 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1937 if (!httpc
->async_data
)
1939 HeapFree(GetProcessHeap(), 0, httpc
);
1942 TRACE("async data = %p\n", httpc
->async_data
);
1943 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1944 httpc
->async_data
->refs
= 1;
1945 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1946 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1947 InitializeCriticalSection(&httpc
->async_data
->cs
);
1948 httpc
->async_data
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": RpcHttpAsyncData.cs");
1949 return &httpc
->common
;
1952 typedef struct _HttpTimerThreadData
1955 DWORD
*last_sent_time
;
1956 HANDLE timer_cancelled
;
1957 } HttpTimerThreadData
;
1959 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1961 HINTERNET in_request
= param
;
1962 RpcPktHdr
*idle_pkt
;
1964 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1968 DWORD bytes_written
;
1969 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1970 RPCRT4_FreeHeader(idle_pkt
);
1974 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1976 DWORD cur_time
= GetTickCount();
1977 DWORD cached_last_sent_time
= *last_sent_time
;
1978 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1981 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1983 HttpTimerThreadData
*data_in
= param
;
1984 HttpTimerThreadData data
;
1988 HeapFree(GetProcessHeap(), 0, data_in
);
1990 for (timeout
= HTTP_IDLE_TIME
;
1991 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1992 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1994 /* are we too soon after last send? */
1995 if (GetTickCount() - *data
.last_sent_time
< HTTP_IDLE_TIME
)
1997 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
2000 CloseHandle(data
.timer_cancelled
);
2004 static VOID WINAPI
rpcrt4_http_internet_callback(
2005 HINTERNET hInternet
,
2006 DWORD_PTR dwContext
,
2007 DWORD dwInternetStatus
,
2008 LPVOID lpvStatusInformation
,
2009 DWORD dwStatusInformationLength
)
2011 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
2013 switch (dwInternetStatus
)
2015 case INTERNET_STATUS_REQUEST_COMPLETE
:
2016 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2019 INTERNET_ASYNC_RESULT
*async_result
= lpvStatusInformation
;
2021 async_data
->async_result
= async_result
->dwResult
? ERROR_SUCCESS
: async_result
->dwError
;
2022 SetEvent(async_data
->completion_event
);
2023 RpcHttpAsyncData_Release(async_data
);
2029 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2036 WCHAR
*status_text
= buf
;
2040 size
= sizeof(status_code
);
2041 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2043 return GetLastError();
2044 if (status_code
== HTTP_STATUS_OK
)
2048 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2049 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2051 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2052 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2055 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2056 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2058 if (status_code
== HTTP_STATUS_DENIED
)
2059 return ERROR_ACCESS_DENIED
;
2060 return RPC_S_SERVER_UNAVAILABLE
;
2063 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2065 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2066 LPWSTR proxy
= NULL
;
2068 LPWSTR password
= NULL
;
2069 LPWSTR servername
= NULL
;
2070 const WCHAR
*option
;
2073 if (httpc
->common
.QOS
&&
2074 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2076 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2077 if (http_cred
->TransportCredentials
)
2080 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2081 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2082 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2084 return RPC_S_OUT_OF_RESOURCES
;
2086 if (cred
->DomainLength
)
2088 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2089 p
+= cred
->DomainLength
;
2093 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2094 p
[cred
->UserLength
] = 0;
2096 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2100 for (option
= httpc
->common
.NetworkOptions
; option
;
2101 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2103 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2104 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2106 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2108 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2109 const WCHAR
*value_end
;
2112 value_end
= strchrW(option
, ',');
2114 value_end
= value_start
+ strlenW(value_start
);
2115 for (p
= value_start
; p
< value_end
; p
++)
2122 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2123 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2125 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2127 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2128 const WCHAR
*value_end
;
2130 value_end
= strchrW(option
, ',');
2132 value_end
= value_start
+ strlenW(value_start
);
2133 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2134 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2137 FIXME("unhandled option %s\n", debugstr_w(option
));
2140 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2141 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2142 if (!httpc
->app_info
)
2144 HeapFree(GetProcessHeap(), 0, password
);
2145 HeapFree(GetProcessHeap(), 0, user
);
2146 HeapFree(GetProcessHeap(), 0, proxy
);
2147 HeapFree(GetProcessHeap(), 0, servername
);
2148 ERR("InternetOpenW failed with error %d\n", GetLastError());
2149 return RPC_S_SERVER_UNAVAILABLE
;
2151 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2153 /* if no RpcProxy option specified, set the HTTP server address to the
2154 * RPC server address */
2157 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2160 HeapFree(GetProcessHeap(), 0, password
);
2161 HeapFree(GetProcessHeap(), 0, user
);
2162 HeapFree(GetProcessHeap(), 0, proxy
);
2163 return RPC_S_OUT_OF_RESOURCES
;
2165 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2168 port
= (httpc
->common
.QOS
&&
2169 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2170 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
)) ?
2171 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
2173 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2174 INTERNET_SERVICE_HTTP
, 0, 0);
2176 HeapFree(GetProcessHeap(), 0, password
);
2177 HeapFree(GetProcessHeap(), 0, user
);
2178 HeapFree(GetProcessHeap(), 0, proxy
);
2179 HeapFree(GetProcessHeap(), 0, servername
);
2181 if (!httpc
->session
)
2183 ERR("InternetConnectW failed with error %d\n", GetLastError());
2184 return RPC_S_SERVER_UNAVAILABLE
;
2190 static RPC_STATUS
send_echo_request(HINTERNET req
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
)
2197 prepare_async_request(async_data
);
2198 ret
= HttpSendRequestW(req
, NULL
, 0, NULL
, 0);
2199 status
= wait_async_request(async_data
, ret
, cancel_event
);
2200 if (status
!= RPC_S_OK
) return status
;
2202 status
= rpcrt4_http_check_response(req
);
2203 if (status
!= RPC_S_OK
) return status
;
2205 InternetReadFile(req
, buf
, sizeof(buf
), &bytes_read
);
2206 /* FIXME: do something with retrieved data */
2211 /* prepare the in pipe for use by RPC packets */
2212 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
, HANDLE cancel_event
,
2213 const UUID
*connection_uuid
,
2214 const UUID
*in_pipe_uuid
,
2215 const UUID
*association_uuid
)
2220 INTERNET_BUFFERSW buffers_in
;
2221 DWORD bytes_written
;
2223 /* prepare in pipe */
2224 status
= send_echo_request(in_request
, async_data
, cancel_event
);
2225 if (status
!= RPC_S_OK
) return status
;
2227 memset(&buffers_in
, 0, sizeof(buffers_in
));
2228 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2229 /* FIXME: get this from the registry */
2230 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2231 prepare_async_request(async_data
);
2232 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2233 status
= wait_async_request(async_data
, ret
, cancel_event
);
2234 if (status
!= RPC_S_OK
) return status
;
2236 TRACE("sending HTTP connect header to server\n");
2237 hdr
= RPCRT4_BuildHttpConnectHeader(FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2238 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2239 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2240 RPCRT4_FreeHeader(hdr
);
2243 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2244 return RPC_S_SERVER_UNAVAILABLE
;
2250 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2254 unsigned short data_len
;
2256 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2258 return RPC_S_SERVER_UNAVAILABLE
;
2259 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2261 ERR("wrong packet type received %d or wrong frag_len %d\n",
2262 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2263 return RPC_S_PROTOCOL_ERROR
;
2266 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2268 return RPC_S_SERVER_UNAVAILABLE
;
2270 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2273 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2275 return RPC_S_OUT_OF_RESOURCES
;
2276 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2279 HeapFree(GetProcessHeap(), 0, *data
);
2280 return RPC_S_SERVER_UNAVAILABLE
;
2286 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2288 ERR("invalid http packet\n");
2289 return RPC_S_PROTOCOL_ERROR
;
2295 /* prepare the out pipe for use by RPC packets */
2296 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
,
2297 RpcHttpAsyncData
*async_data
,
2298 HANDLE cancel_event
,
2299 const UUID
*connection_uuid
,
2300 const UUID
*out_pipe_uuid
,
2301 ULONG
*flow_control_increment
)
2306 BYTE
*data_from_server
;
2307 RpcPktHdr pkt_from_server
;
2308 ULONG field1
, field3
;
2310 status
= send_echo_request(out_request
, async_data
, cancel_event
);
2311 if (status
!= RPC_S_OK
) return status
;
2313 hdr
= RPCRT4_BuildHttpConnectHeader(TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2314 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2316 prepare_async_request(async_data
);
2317 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2318 status
= wait_async_request(async_data
, ret
, cancel_event
);
2319 RPCRT4_FreeHeader(hdr
);
2320 if (status
!= RPC_S_OK
) return status
;
2322 status
= rpcrt4_http_check_response(out_request
);
2323 if (status
!= RPC_S_OK
) return status
;
2325 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2327 if (status
!= RPC_S_OK
) return status
;
2328 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2330 HeapFree(GetProcessHeap(), 0, data_from_server
);
2331 if (status
!= RPC_S_OK
) return status
;
2332 TRACE("received (%d) from first prepare header\n", field1
);
2336 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2338 if (status
!= RPC_S_OK
) return status
;
2339 if (pkt_from_server
.http
.flags
!= 0x0001) break;
2341 TRACE("http idle packet, waiting for real packet\n");
2342 HeapFree(GetProcessHeap(), 0, data_from_server
);
2343 if (pkt_from_server
.http
.num_data_items
!= 0)
2345 ERR("HTTP idle packet should have no data items instead of %d\n",
2346 pkt_from_server
.http
.num_data_items
);
2347 return RPC_S_PROTOCOL_ERROR
;
2350 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2351 &field1
, flow_control_increment
,
2353 HeapFree(GetProcessHeap(), 0, data_from_server
);
2354 if (status
!= RPC_S_OK
) return status
;
2355 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2360 static UINT
encode_base64(const char *bin
, unsigned int len
, WCHAR
*base64
)
2362 static char enc
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2367 /* first 6 bits, all from bin[0] */
2368 base64
[i
++] = enc
[(bin
[0] & 0xfc) >> 2];
2369 x
= (bin
[0] & 3) << 4;
2371 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
2374 base64
[i
++] = enc
[x
];
2379 base64
[i
++] = enc
[x
| ((bin
[1] & 0xf0) >> 4)];
2380 x
= (bin
[1] & 0x0f) << 2;
2382 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
2385 base64
[i
++] = enc
[x
];
2389 base64
[i
++] = enc
[x
| ((bin
[2] & 0xc0) >> 6)];
2391 /* last 6 bits, all from bin [2] */
2392 base64
[i
++] = enc
[bin
[2] & 0x3f];
2400 static RPC_STATUS
insert_authorization_header(HINTERNET request
, RpcQualityOfService
*qos
)
2402 static const WCHAR basicW
[] =
2403 {'A','u','t','h','o','r','i','z','a','t','i','o','n',':',' ','B','a','s','i','c',' '};
2404 RPC_HTTP_TRANSPORT_CREDENTIALS_W
*creds
;
2405 SEC_WINNT_AUTH_IDENTITY_W
*id
;
2406 int len
, datalen
, userlen
, passlen
;
2407 WCHAR
*header
, *ptr
;
2409 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2411 if (!qos
|| qos
->qos
->AdditionalSecurityInfoType
!= RPC_C_AUTHN_INFO_TYPE_HTTP
)
2414 creds
= qos
->qos
->u
.HttpCredentials
;
2415 if (creds
->AuthenticationTarget
!= RPC_C_HTTP_AUTHN_TARGET_SERVER
|| !creds
->NumberOfAuthnSchemes
)
2418 if (creds
->AuthnSchemes
[0] != RPC_C_HTTP_AUTHN_SCHEME_BASIC
)
2420 FIXME("scheme %u not supported\n", creds
->AuthnSchemes
[0]);
2423 id
= creds
->TransportCredentials
;
2424 if (!id
->User
|| !id
->Password
) return RPC_S_OK
;
2426 userlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, NULL
, 0, NULL
, NULL
);
2427 passlen
= WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, NULL
, 0, NULL
, NULL
);
2429 datalen
= userlen
+ passlen
+ 1;
2430 if (!(data
= HeapAlloc(GetProcessHeap(), 0, datalen
))) return RPC_S_OUT_OF_MEMORY
;
2432 WideCharToMultiByte(CP_UTF8
, 0, id
->User
, id
->UserLength
, data
, userlen
, NULL
, NULL
);
2433 data
[userlen
] = ':';
2434 WideCharToMultiByte(CP_UTF8
, 0, id
->Password
, id
->PasswordLength
, data
+ userlen
+ 1, passlen
, NULL
, NULL
);
2436 len
= ((datalen
+ 2) * 4) / 3;
2437 if ((header
= HeapAlloc(GetProcessHeap(), 0, sizeof(basicW
) + (len
+ 2) * sizeof(WCHAR
))))
2439 memcpy(header
, basicW
, sizeof(basicW
));
2440 ptr
= header
+ sizeof(basicW
) / sizeof(basicW
[0]);
2441 len
= encode_base64(data
, datalen
, ptr
);
2445 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW
))) status
= RPC_S_OK
;
2446 HeapFree(GetProcessHeap(), 0, header
);
2448 HeapFree(GetProcessHeap(), 0, data
);
2452 static RPC_STATUS
insert_cookie_header(HINTERNET request
, const WCHAR
*value
)
2454 static const WCHAR cookieW
[] = {'C','o','o','k','i','e',':',' '};
2455 WCHAR
*header
, *ptr
;
2457 RPC_STATUS status
= RPC_S_SERVER_UNAVAILABLE
;
2459 if (!value
) return RPC_S_OK
;
2461 len
= strlenW(value
);
2462 if ((header
= HeapAlloc(GetProcessHeap(), 0, sizeof(cookieW
) + (len
+ 3) * sizeof(WCHAR
))))
2464 memcpy(header
, cookieW
, sizeof(cookieW
));
2465 ptr
= header
+ sizeof(cookieW
) / sizeof(cookieW
[0]);
2466 memcpy(ptr
, value
, len
* sizeof(WCHAR
));
2470 if ((HttpAddRequestHeadersW(request
, header
, -1, HTTP_ADDREQ_FLAG_ADD_IF_NEW
))) status
= RPC_S_OK
;
2471 HeapFree(GetProcessHeap(), 0, header
);
2476 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2478 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2479 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2480 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2481 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2482 static const WCHAR wszColon
[] = {':',0};
2483 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2484 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2489 HttpTimerThreadData
*timer_data
;
2492 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2494 if (Connection
->server
)
2496 ERR("ncacn_http servers not supported yet\n");
2497 return RPC_S_SERVER_UNAVAILABLE
;
2500 if (httpc
->in_request
)
2503 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2505 status
= UuidCreate(&httpc
->connection_uuid
);
2506 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2507 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2509 status
= rpcrt4_http_internet_connect(httpc
);
2510 if (status
!= RPC_S_OK
)
2513 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2515 return RPC_S_OUT_OF_MEMORY
;
2516 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2517 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2518 strcatW(url
, wszColon
);
2519 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2521 secure
= httpc
->common
.QOS
&&
2522 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2523 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2525 flags
= INTERNET_FLAG_KEEP_CONNECTION
| INTERNET_FLAG_PRAGMA_NOCACHE
| INTERNET_FLAG_NO_CACHE_WRITE
|
2526 INTERNET_FLAG_NO_AUTO_REDIRECT
;
2527 if (secure
) flags
|= INTERNET_FLAG_SECURE
;
2529 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
, wszAcceptTypes
,
2530 flags
, (DWORD_PTR
)httpc
->async_data
);
2531 if (!httpc
->in_request
)
2533 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2534 HeapFree(GetProcessHeap(), 0, url
);
2535 return RPC_S_SERVER_UNAVAILABLE
;
2537 status
= insert_authorization_header(httpc
->in_request
, httpc
->common
.QOS
);
2538 if (status
!= RPC_S_OK
)
2541 status
= insert_cookie_header(httpc
->in_request
, Connection
->CookieAuth
);
2542 if (status
!= RPC_S_OK
)
2545 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
, wszAcceptTypes
,
2546 flags
, (DWORD_PTR
)httpc
->async_data
);
2547 HeapFree(GetProcessHeap(), 0, url
);
2548 if (!httpc
->out_request
)
2550 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2551 return RPC_S_SERVER_UNAVAILABLE
;
2553 status
= insert_authorization_header(httpc
->out_request
, httpc
->common
.QOS
);
2554 if (status
!= RPC_S_OK
)
2557 status
= insert_cookie_header(httpc
->out_request
, Connection
->CookieAuth
);
2558 if (status
!= RPC_S_OK
)
2561 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
,
2563 httpc
->cancel_event
,
2564 &httpc
->connection_uuid
,
2565 &httpc
->in_pipe_uuid
,
2566 &Connection
->assoc
->http_uuid
);
2567 if (status
!= RPC_S_OK
)
2570 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
,
2572 httpc
->cancel_event
,
2573 &httpc
->connection_uuid
,
2574 &httpc
->out_pipe_uuid
,
2575 &httpc
->flow_control_increment
);
2576 if (status
!= RPC_S_OK
)
2579 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2580 httpc
->last_sent_time
= GetTickCount();
2581 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2583 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2585 return ERROR_OUTOFMEMORY
;
2586 timer_data
->timer_param
= httpc
->in_request
;
2587 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2588 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2589 /* FIXME: should use CreateTimerQueueTimer when implemented */
2590 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2593 HeapFree(GetProcessHeap(), 0, timer_data
);
2594 return GetLastError();
2596 CloseHandle(thread
);
2601 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2604 return RPC_S_SERVER_UNAVAILABLE
;
2607 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2608 void *buffer
, unsigned int count
)
2610 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2613 unsigned int bytes_left
= count
;
2614 RPC_STATUS status
= RPC_S_OK
;
2616 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, count
);
2620 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2621 prepare_async_request(httpc
->async_data
);
2622 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
2623 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2624 if(status
!= RPC_S_OK
) {
2625 if(status
== RPC_S_CALL_CANCELLED
)
2626 TRACE("call cancelled\n");
2630 if(!httpc
->async_data
->inet_buffers
.dwBufferLength
)
2632 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
2633 httpc
->async_data
->inet_buffers
.dwBufferLength
);
2635 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2636 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2639 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2640 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2642 TRACE("%p %p %u -> %u\n", httpc
->out_request
, buffer
, count
, status
);
2643 return status
== RPC_S_OK
? count
: -1;
2646 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2648 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2652 RpcPktCommonHdr common_hdr
;
2656 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2659 /* read packet common header */
2660 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2661 if (dwRead
!= sizeof(common_hdr
)) {
2662 WARN("Short read of header, %d bytes\n", dwRead
);
2663 status
= RPC_S_PROTOCOL_ERROR
;
2666 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2667 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2669 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2670 status
= RPC_S_PROTOCOL_ERROR
;
2674 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2675 if (status
!= RPC_S_OK
) goto fail
;
2677 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2678 if (hdr_length
== 0) {
2679 WARN("header length == 0\n");
2680 status
= RPC_S_PROTOCOL_ERROR
;
2684 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2687 status
= RPC_S_OUT_OF_RESOURCES
;
2690 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2692 /* read the rest of packet header */
2693 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2694 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2695 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2696 status
= RPC_S_PROTOCOL_ERROR
;
2700 if (common_hdr
.frag_len
- hdr_length
)
2702 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2705 status
= RPC_S_OUT_OF_RESOURCES
;
2709 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2710 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2712 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2713 status
= RPC_S_PROTOCOL_ERROR
;
2720 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2722 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2724 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2725 status
= RPC_S_PROTOCOL_ERROR
;
2728 if ((*Header
)->http
.flags
== 0x0001)
2730 TRACE("http idle packet, waiting for real packet\n");
2731 if ((*Header
)->http
.num_data_items
!= 0)
2733 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2734 status
= RPC_S_PROTOCOL_ERROR
;
2738 else if ((*Header
)->http
.flags
== 0x0002)
2740 ULONG bytes_transmitted
;
2741 ULONG flow_control_increment
;
2743 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2746 &flow_control_increment
,
2748 if (status
!= RPC_S_OK
)
2750 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2751 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2752 /* FIXME: do something with parsed data */
2756 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
2757 status
= RPC_S_PROTOCOL_ERROR
;
2760 RPCRT4_FreeHeader(*Header
);
2762 HeapFree(GetProcessHeap(), 0, *Payload
);
2770 httpc
->bytes_received
+= common_hdr
.frag_len
;
2772 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
2774 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
2776 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
2777 httpc
->bytes_received
,
2778 httpc
->flow_control_increment
,
2779 &httpc
->out_pipe_uuid
);
2782 DWORD bytes_written
;
2784 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
2785 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2786 RPCRT4_FreeHeader(hdr
);
2788 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
2793 if (status
!= RPC_S_OK
) {
2794 RPCRT4_FreeHeader(*Header
);
2796 HeapFree(GetProcessHeap(), 0, *Payload
);
2802 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
2803 const void *buffer
, unsigned int count
)
2805 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2806 DWORD bytes_written
;
2809 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
2810 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
2811 httpc
->last_sent_time
= GetTickCount();
2812 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2813 return ret
? bytes_written
: -1;
2816 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
2818 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2822 SetEvent(httpc
->timer_cancelled
);
2823 if (httpc
->in_request
)
2824 InternetCloseHandle(httpc
->in_request
);
2825 httpc
->in_request
= NULL
;
2826 if (httpc
->out_request
)
2827 InternetCloseHandle(httpc
->out_request
);
2828 httpc
->out_request
= NULL
;
2829 if (httpc
->app_info
)
2830 InternetCloseHandle(httpc
->app_info
);
2831 httpc
->app_info
= NULL
;
2833 InternetCloseHandle(httpc
->session
);
2834 httpc
->session
= NULL
;
2835 RpcHttpAsyncData_Release(httpc
->async_data
);
2836 if (httpc
->cancel_event
)
2837 CloseHandle(httpc
->cancel_event
);
2842 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
2844 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2846 SetEvent(httpc
->cancel_event
);
2849 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
2851 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2855 prepare_async_request(httpc
->async_data
);
2856 ret
= InternetQueryDataAvailable(httpc
->out_request
,
2857 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
2858 status
= wait_async_request(httpc
->async_data
, ret
, httpc
->cancel_event
);
2859 return status
== RPC_S_OK
? 0 : -1;
2862 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
2863 const char *networkaddr
,
2864 const char *endpoint
)
2866 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
2867 EPM_PROTOCOL_HTTP
, endpoint
);
2870 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
2875 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
2876 networkaddr
, EPM_PROTOCOL_HTTP
,
2880 static const struct connection_ops conn_protseq_list
[] = {
2882 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
2883 rpcrt4_conn_np_alloc
,
2884 rpcrt4_ncacn_np_open
,
2885 rpcrt4_ncacn_np_handoff
,
2886 rpcrt4_conn_np_read
,
2887 rpcrt4_conn_np_write
,
2888 rpcrt4_conn_np_close
,
2889 rpcrt4_conn_np_cancel_call
,
2890 rpcrt4_conn_np_wait_for_incoming_data
,
2891 rpcrt4_ncacn_np_get_top_of_tower
,
2892 rpcrt4_ncacn_np_parse_top_of_tower
,
2894 RPCRT4_default_is_authorized
,
2895 RPCRT4_default_authorize
,
2896 RPCRT4_default_secure_packet
,
2897 rpcrt4_conn_np_impersonate_client
,
2898 rpcrt4_conn_np_revert_to_self
,
2899 RPCRT4_default_inquire_auth_client
,
2902 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
2903 rpcrt4_conn_np_alloc
,
2904 rpcrt4_ncalrpc_open
,
2905 rpcrt4_ncalrpc_handoff
,
2906 rpcrt4_conn_np_read
,
2907 rpcrt4_conn_np_write
,
2908 rpcrt4_conn_np_close
,
2909 rpcrt4_conn_np_cancel_call
,
2910 rpcrt4_conn_np_wait_for_incoming_data
,
2911 rpcrt4_ncalrpc_get_top_of_tower
,
2912 rpcrt4_ncalrpc_parse_top_of_tower
,
2914 rpcrt4_ncalrpc_is_authorized
,
2915 rpcrt4_ncalrpc_authorize
,
2916 rpcrt4_ncalrpc_secure_packet
,
2917 rpcrt4_conn_np_impersonate_client
,
2918 rpcrt4_conn_np_revert_to_self
,
2919 rpcrt4_ncalrpc_inquire_auth_client
,
2922 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
2923 rpcrt4_conn_tcp_alloc
,
2924 rpcrt4_ncacn_ip_tcp_open
,
2925 rpcrt4_conn_tcp_handoff
,
2926 rpcrt4_conn_tcp_read
,
2927 rpcrt4_conn_tcp_write
,
2928 rpcrt4_conn_tcp_close
,
2929 rpcrt4_conn_tcp_cancel_call
,
2930 rpcrt4_conn_tcp_wait_for_incoming_data
,
2931 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
2932 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
2934 RPCRT4_default_is_authorized
,
2935 RPCRT4_default_authorize
,
2936 RPCRT4_default_secure_packet
,
2937 RPCRT4_default_impersonate_client
,
2938 RPCRT4_default_revert_to_self
,
2939 RPCRT4_default_inquire_auth_client
,
2942 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
2943 rpcrt4_ncacn_http_alloc
,
2944 rpcrt4_ncacn_http_open
,
2945 rpcrt4_ncacn_http_handoff
,
2946 rpcrt4_ncacn_http_read
,
2947 rpcrt4_ncacn_http_write
,
2948 rpcrt4_ncacn_http_close
,
2949 rpcrt4_ncacn_http_cancel_call
,
2950 rpcrt4_ncacn_http_wait_for_incoming_data
,
2951 rpcrt4_ncacn_http_get_top_of_tower
,
2952 rpcrt4_ncacn_http_parse_top_of_tower
,
2953 rpcrt4_ncacn_http_receive_fragment
,
2954 RPCRT4_default_is_authorized
,
2955 RPCRT4_default_authorize
,
2956 RPCRT4_default_secure_packet
,
2957 RPCRT4_default_impersonate_client
,
2958 RPCRT4_default_revert_to_self
,
2959 RPCRT4_default_inquire_auth_client
,
2964 static const struct protseq_ops protseq_list
[] =
2968 rpcrt4_protseq_np_alloc
,
2969 rpcrt4_protseq_np_signal_state_changed
,
2970 rpcrt4_protseq_np_get_wait_array
,
2971 rpcrt4_protseq_np_free_wait_array
,
2972 rpcrt4_protseq_np_wait_for_new_connection
,
2973 rpcrt4_protseq_ncacn_np_open_endpoint
,
2977 rpcrt4_protseq_np_alloc
,
2978 rpcrt4_protseq_np_signal_state_changed
,
2979 rpcrt4_protseq_np_get_wait_array
,
2980 rpcrt4_protseq_np_free_wait_array
,
2981 rpcrt4_protseq_np_wait_for_new_connection
,
2982 rpcrt4_protseq_ncalrpc_open_endpoint
,
2986 rpcrt4_protseq_sock_alloc
,
2987 rpcrt4_protseq_sock_signal_state_changed
,
2988 rpcrt4_protseq_sock_get_wait_array
,
2989 rpcrt4_protseq_sock_free_wait_array
,
2990 rpcrt4_protseq_sock_wait_for_new_connection
,
2991 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
2995 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2997 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
3000 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
3001 if (!strcmp(protseq_list
[i
].name
, protseq
))
3002 return &protseq_list
[i
];
3006 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
3009 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
3010 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
3011 return &conn_protseq_list
[i
];
3015 /**** interface to rest of code ****/
3017 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
3019 TRACE("(Connection == ^%p)\n", Connection
);
3021 assert(!Connection
->server
);
3022 return Connection
->ops
->open_connection_client(Connection
);
3025 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
3027 TRACE("(Connection == ^%p)\n", Connection
);
3028 if (SecIsValidHandle(&Connection
->ctx
))
3030 DeleteSecurityContext(&Connection
->ctx
);
3031 SecInvalidateHandle(&Connection
->ctx
);
3033 rpcrt4_conn_close(Connection
);
3037 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
3038 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
3039 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
, LPCWSTR CookieAuth
)
3041 static LONG next_id
;
3042 const struct connection_ops
*ops
;
3043 RpcConnection
* NewConnection
;
3045 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
3048 FIXME("not supported for protseq %s\n", Protseq
);
3049 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
3052 NewConnection
= ops
->alloc();
3053 NewConnection
->ref
= 1;
3054 NewConnection
->Next
= NULL
;
3055 NewConnection
->server_binding
= NULL
;
3056 NewConnection
->server
= server
;
3057 NewConnection
->ops
= ops
;
3058 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
3059 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
3060 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
3061 NewConnection
->CookieAuth
= RPCRT4_strdupW(CookieAuth
);
3062 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
3063 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
3064 NewConnection
->NextCallId
= 1;
3066 SecInvalidateHandle(&NewConnection
->ctx
);
3067 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
3068 NewConnection
->attr
= 0;
3069 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
3070 NewConnection
->AuthInfo
= AuthInfo
;
3071 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
3072 NewConnection
->encryption_auth_len
= 0;
3073 NewConnection
->signature_auth_len
= 0;
3074 if (QOS
) RpcQualityOfService_AddRef(QOS
);
3075 NewConnection
->QOS
= QOS
;
3077 list_init(&NewConnection
->conn_pool_entry
);
3078 NewConnection
->async_state
= NULL
;
3080 TRACE("connection: %p\n", NewConnection
);
3081 *Connection
= NewConnection
;
3086 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3090 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
, rpcrt4_conn_get_name(OldConnection
),
3091 OldConnection
->NetworkAddr
, OldConnection
->Endpoint
, NULL
,
3092 OldConnection
->AuthInfo
, OldConnection
->QOS
, OldConnection
->CookieAuth
);
3093 if (err
== RPC_S_OK
)
3094 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3098 RpcConnection
*RPCRT4_GrabConnection( RpcConnection
*conn
)
3100 InterlockedIncrement( &conn
->ref
);
3104 RPC_STATUS
RPCRT4_ReleaseConnection(RpcConnection
* Connection
)
3106 if (InterlockedDecrement( &Connection
->ref
) > 0) return RPC_S_OK
;
3108 TRACE("destroying connection %p\n", Connection
);
3110 RPCRT4_CloseConnection(Connection
);
3111 RPCRT4_strfree(Connection
->Endpoint
);
3112 RPCRT4_strfree(Connection
->NetworkAddr
);
3113 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3114 HeapFree(GetProcessHeap(), 0, Connection
->CookieAuth
);
3115 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3116 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3119 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3121 HeapFree(GetProcessHeap(), 0, Connection
);
3125 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3127 const char *protseq
,
3128 const char *networkaddr
,
3129 const char *endpoint
)
3131 twr_empty_floor_t
*protocol_floor
;
3132 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3137 return RPC_S_INVALID_RPC_PROTSEQ
;
3141 *tower_size
= sizeof(*protocol_floor
);
3142 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3146 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3147 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3148 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3149 protocol_floor
->count_rhs
= 0;
3151 tower_data
+= sizeof(*protocol_floor
);
3153 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3155 return EPT_S_NOT_REGISTERED
;
3157 *tower_size
+= sizeof(*protocol_floor
);
3162 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3168 const twr_empty_floor_t
*protocol_floor
;
3169 const twr_empty_floor_t
*floor4
;
3170 const struct connection_ops
*protseq_ops
= NULL
;
3174 if (tower_size
< sizeof(*protocol_floor
))
3175 return EPT_S_NOT_REGISTERED
;
3177 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3178 tower_data
+= sizeof(*protocol_floor
);
3179 tower_size
-= sizeof(*protocol_floor
);
3180 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3181 (protocol_floor
->count_rhs
> tower_size
))
3182 return EPT_S_NOT_REGISTERED
;
3183 tower_data
+= protocol_floor
->count_rhs
;
3184 tower_size
-= protocol_floor
->count_rhs
;
3186 floor4
= (const twr_empty_floor_t
*)tower_data
;
3187 if ((tower_size
< sizeof(*floor4
)) ||
3188 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3189 return EPT_S_NOT_REGISTERED
;
3191 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3192 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3193 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3195 protseq_ops
= &conn_protseq_list
[i
];
3200 return EPT_S_NOT_REGISTERED
;
3202 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3204 if ((status
== RPC_S_OK
) && protseq
)
3206 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3207 strcpy(*protseq
, protseq_ops
->name
);
3213 /***********************************************************************
3214 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3216 * Checks if the given protocol sequence is known by the RPC system.
3217 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3220 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3224 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3225 ps
, sizeof ps
, NULL
, NULL
);
3226 if (rpcrt4_get_conn_protseq_ops(ps
))
3229 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3231 return RPC_S_INVALID_RPC_PROTSEQ
;
3234 /***********************************************************************
3235 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3237 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3239 UNICODE_STRING protseqW
;
3241 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3243 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3244 RtlFreeUnicodeString(&protseqW
);
3247 return RPC_S_OUT_OF_MEMORY
;
3250 /***********************************************************************
3251 * RpcProtseqVectorFreeA (RPCRT4.@)
3253 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3255 TRACE("(%p)\n", protseqs
);
3260 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3261 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3262 HeapFree(GetProcessHeap(), 0, *protseqs
);
3268 /***********************************************************************
3269 * RpcProtseqVectorFreeW (RPCRT4.@)
3271 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3273 TRACE("(%p)\n", protseqs
);
3278 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3279 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3280 HeapFree(GetProcessHeap(), 0, *protseqs
);
3286 /***********************************************************************
3287 * RpcNetworkInqProtseqsW (RPCRT4.@)
3289 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3291 RPC_PROTSEQ_VECTORW
*pvector
;
3293 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3295 TRACE("(%p)\n", protseqs
);
3297 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3300 pvector
= *protseqs
;
3302 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3304 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3305 if (pvector
->Protseq
[i
] == NULL
)
3307 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3308 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3314 if (status
!= RPC_S_OK
)
3315 RpcProtseqVectorFreeW(protseqs
);
3319 /***********************************************************************
3320 * RpcNetworkInqProtseqsA (RPCRT4.@)
3322 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3324 RPC_PROTSEQ_VECTORA
*pvector
;
3326 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3328 TRACE("(%p)\n", protseqs
);
3330 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3333 pvector
= *protseqs
;
3335 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3337 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3338 if (pvector
->Protseq
[i
] == NULL
)
3340 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3346 if (status
!= RPC_S_OK
)
3347 RpcProtseqVectorFreeA(protseqs
);