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
33 #include <sys/types.h>
35 #if defined(__MINGW32__) || defined (_MSC_VER)
36 # include <ws2tcpip.h>
38 # define EADDRINUSE WSAEADDRINUSE
41 # define EAGAIN WSAEWOULDBLOCK
44 # define errno WSAGetLastError()
51 # ifdef HAVE_SYS_SOCKET_H
52 # include <sys/socket.h>
54 # ifdef HAVE_NETINET_IN_H
55 # include <netinet/in.h>
57 # ifdef HAVE_NETINET_TCP_H
58 # include <netinet/tcp.h>
60 # ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
66 # ifdef HAVE_SYS_POLL_H
67 # include <sys/poll.h>
69 # ifdef HAVE_SYS_FILIO_H
70 # include <sys/filio.h>
72 # ifdef HAVE_SYS_IOCTL_H
73 # include <sys/ioctl.h>
75 # define closesocket close
76 # define ioctlsocket ioctl
77 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
85 #include "wine/unicode.h"
90 #include "wine/debug.h"
92 #include "rpc_binding.h"
93 #include "rpc_assoc.h"
94 #include "rpc_message.h"
95 #include "rpc_server.h"
96 #include "epm_towers.h"
99 # define SOL_TCP IPPROTO_TCP
102 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
104 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
109 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
111 /**** ncacn_np support ****/
113 typedef struct _RpcConnection_np
115 RpcConnection common
;
121 static RpcConnection
*rpcrt4_conn_np_alloc(void)
123 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
127 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
128 npc
->listening
= FALSE
;
133 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
138 npc
->listening
= TRUE
;
141 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
144 switch(GetLastError())
146 case ERROR_PIPE_CONNECTED
:
147 SetEvent(npc
->ovl
.hEvent
);
149 case ERROR_IO_PENDING
:
150 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
152 case ERROR_NO_DATA_DETECTED
:
153 /* client has disconnected, retry */
154 DisconnectNamedPipe( npc
->pipe
);
157 npc
->listening
= FALSE
;
158 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
159 return RPC_S_OUT_OF_RESOURCES
;
164 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
166 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
167 TRACE("listening on %s\n", pname
);
169 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
170 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
171 PIPE_UNLIMITED_INSTANCES
,
172 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
173 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
174 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
175 if (GetLastError() == ERROR_FILE_EXISTS
)
176 return RPC_S_DUPLICATE_ENDPOINT
;
178 return RPC_S_CANT_CREATE_ENDPOINT
;
181 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
182 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
184 /* Note: we don't call ConnectNamedPipe here because it must be done in the
185 * server thread as the thread must be alertable */
189 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
191 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
195 TRACE("connecting to %s\n", pname
);
201 dwFlags
= SECURITY_SQOS_PRESENT
;
202 switch (Connection
->QOS
->qos
->ImpersonationType
)
204 case RPC_C_IMP_LEVEL_DEFAULT
:
205 /* FIXME: what to do here? */
207 case RPC_C_IMP_LEVEL_ANONYMOUS
:
208 dwFlags
|= SECURITY_ANONYMOUS
;
210 case RPC_C_IMP_LEVEL_IDENTIFY
:
211 dwFlags
|= SECURITY_IDENTIFICATION
;
213 case RPC_C_IMP_LEVEL_IMPERSONATE
:
214 dwFlags
|= SECURITY_IMPERSONATION
;
216 case RPC_C_IMP_LEVEL_DELEGATE
:
217 dwFlags
|= SECURITY_DELEGATION
;
220 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
221 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
223 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
224 OPEN_EXISTING
, dwFlags
, 0);
225 if (pipe
!= INVALID_HANDLE_VALUE
) break;
226 err
= GetLastError();
227 if (err
== ERROR_PIPE_BUSY
) {
228 ERR("connection to %s failed, error=%x\n", pname
, err
);
229 return RPC_S_SERVER_TOO_BUSY
;
231 if(wait
) ERR("Waiting for pipe instance");
234 if (!WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
235 err
= GetLastError();
236 ERR("connection to %s failed, error=%x, wait %x\n", pname
, err
, wait
);
237 return RPC_S_SERVER_UNAVAILABLE
;
241 ERR("Pipe Instance Ready!!!!!!!!!!!!!!!!!!\n");
247 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
248 /* pipe is connected; change to message-read mode. */
249 dwMode
= PIPE_READMODE_MESSAGE
;
250 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
251 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
257 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
259 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
260 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
264 /* already connected? */
268 /* protseq=ncalrpc: supposed to use NT LPC ports,
269 * but we'll implement it with named pipes for now */
270 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
271 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
272 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
278 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
280 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
283 RpcConnection
*Connection
;
284 char generated_endpoint
[22];
288 static LONG lrpc_nameless_id
;
289 DWORD process_id
= GetCurrentProcessId();
290 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
291 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
292 "LRPC%08x.%08x", process_id
, id
);
293 endpoint
= generated_endpoint
;
296 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
297 endpoint
, NULL
, NULL
, NULL
);
301 /* protseq=ncalrpc: supposed to use NT LPC ports,
302 * but we'll implement it with named pipes for now */
303 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
304 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
305 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
308 EnterCriticalSection(&protseq
->cs
);
309 Connection
->Next
= protseq
->conn
;
310 protseq
->conn
= Connection
;
311 LeaveCriticalSection(&protseq
->cs
);
316 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
318 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
319 static const char prefix
[] = "\\\\.";
323 /* already connected? */
327 /* protseq=ncacn_np: named pipes */
328 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
329 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
330 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
336 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
338 static const char prefix
[] = "\\\\.";
341 RpcConnection
*Connection
;
342 char generated_endpoint
[21];
346 static LONG np_nameless_id
;
347 DWORD process_id
= GetCurrentProcessId();
348 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
349 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
350 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
351 endpoint
= generated_endpoint
;
354 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
355 endpoint
, NULL
, NULL
, NULL
);
359 /* protseq=ncacn_np: named pipes */
360 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
361 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
362 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
365 EnterCriticalSection(&protseq
->cs
);
366 Connection
->Next
= protseq
->conn
;
367 protseq
->conn
= Connection
;
368 LeaveCriticalSection(&protseq
->cs
);
373 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
375 /* because of the way named pipes work, we'll transfer the connected pipe
376 * to the child, then reopen the server binding to continue listening */
378 new_npc
->pipe
= old_npc
->pipe
;
379 new_npc
->ovl
= old_npc
->ovl
;
381 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
382 old_npc
->listening
= FALSE
;
385 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
389 static const char prefix
[] = "\\\\.";
391 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
393 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
394 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
395 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
401 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
405 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
407 TRACE("%s\n", old_conn
->Endpoint
);
409 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
411 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
412 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
413 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
419 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
420 void *buffer
, unsigned int count
)
422 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
425 unsigned int bytes_left
= count
;
428 ZeroMemory(&ovl
, sizeof(ovl
));
429 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
434 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
435 if ((!ret
|| !bytes_read
) && (GetLastError() != ERROR_IO_PENDING
))
437 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
438 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
440 bytes_left
-= bytes_read
;
443 CloseHandle(ovl
.hEvent
);
444 return ret
? count
: -1;
447 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
448 const void *buffer
, unsigned int count
)
450 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
451 const char *buf
= buffer
;
453 unsigned int bytes_left
= count
;
456 ZeroMemory(&ovl
, sizeof(ovl
));
457 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
462 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
463 if ((!ret
|| !bytes_written
) && (GetLastError() != ERROR_IO_PENDING
))
466 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
467 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
469 bytes_left
-= bytes_written
;
470 buf
+= bytes_written
;
472 return ret
? count
: -1;
475 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
477 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
479 FlushFileBuffers(npc
->pipe
);
480 CloseHandle(npc
->pipe
);
483 if (npc
->ovl
.hEvent
) {
484 CloseHandle(npc
->ovl
.hEvent
);
490 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
492 /* FIXME: implement when named pipe writes use overlapped I/O */
495 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
497 /* FIXME: implement when named pipe writes use overlapped I/O */
501 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
502 const char *networkaddr
,
503 const char *endpoint
)
505 twr_empty_floor_t
*smb_floor
;
506 twr_empty_floor_t
*nb_floor
;
508 size_t networkaddr_size
;
509 size_t endpoint_size
;
511 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
513 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
514 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
515 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
520 smb_floor
= (twr_empty_floor_t
*)tower_data
;
522 tower_data
+= sizeof(*smb_floor
);
524 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
525 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
526 smb_floor
->count_rhs
= endpoint_size
;
529 memcpy(tower_data
, endpoint
, endpoint_size
);
532 tower_data
+= endpoint_size
;
534 nb_floor
= (twr_empty_floor_t
*)tower_data
;
536 tower_data
+= sizeof(*nb_floor
);
538 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
539 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
540 nb_floor
->count_rhs
= networkaddr_size
;
543 memcpy(tower_data
, networkaddr
, networkaddr_size
);
550 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
555 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
556 const twr_empty_floor_t
*nb_floor
;
558 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
560 if (tower_size
< sizeof(*smb_floor
))
561 return EPT_S_NOT_REGISTERED
;
563 tower_data
+= sizeof(*smb_floor
);
564 tower_size
-= sizeof(*smb_floor
);
566 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
567 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
568 (smb_floor
->count_rhs
> tower_size
) ||
569 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
570 return EPT_S_NOT_REGISTERED
;
574 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
576 return RPC_S_OUT_OF_RESOURCES
;
577 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
579 tower_data
+= smb_floor
->count_rhs
;
580 tower_size
-= smb_floor
->count_rhs
;
582 if (tower_size
< sizeof(*nb_floor
))
583 return EPT_S_NOT_REGISTERED
;
585 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
587 tower_data
+= sizeof(*nb_floor
);
588 tower_size
-= sizeof(*nb_floor
);
590 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
591 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
592 (nb_floor
->count_rhs
> tower_size
) ||
593 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
594 return EPT_S_NOT_REGISTERED
;
598 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
603 I_RpcFree(*endpoint
);
606 return RPC_S_OUT_OF_RESOURCES
;
608 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
614 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
616 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
619 TRACE("(%p)\n", conn
);
621 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
622 return RPCRT4_default_impersonate_client(conn
);
624 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
627 DWORD error
= GetLastError();
628 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
631 case ERROR_CANNOT_IMPERSONATE
:
632 return RPC_S_NO_CONTEXT_AVAILABLE
;
638 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
642 TRACE("(%p)\n", conn
);
644 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
645 return RPCRT4_default_revert_to_self(conn
);
647 ret
= RevertToSelf();
650 WARN("RevertToSelf failed with error %u\n", GetLastError());
651 return RPC_S_NO_CONTEXT_AVAILABLE
;
656 typedef struct _RpcServerProtseq_np
658 RpcServerProtseq common
;
660 } RpcServerProtseq_np
;
662 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
664 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
666 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
670 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
672 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
673 SetEvent(npps
->mgr_event
);
676 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
678 HANDLE
*objs
= prev_array
;
679 RpcConnection_np
*conn
;
680 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
682 EnterCriticalSection(&protseq
->cs
);
684 /* open and count connections */
686 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
688 rpcrt4_conn_listen_pipe(conn
);
689 if (conn
->ovl
.hEvent
)
691 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
694 /* make array of connections */
696 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
698 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
701 ERR("couldn't allocate objs\n");
702 LeaveCriticalSection(&protseq
->cs
);
706 objs
[0] = npps
->mgr_event
;
708 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
710 if ((objs
[*count
] = conn
->ovl
.hEvent
))
712 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
714 LeaveCriticalSection(&protseq
->cs
);
718 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
720 HeapFree(GetProcessHeap(), 0, array
);
723 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
726 HANDLE
*objs
= wait_array
;
728 RpcConnection
*cconn
;
729 RpcConnection_np
*conn
;
736 /* an alertable wait isn't strictly necessary, but due to our
737 * overlapped I/O implementation in Wine we need to free some memory
738 * by the file user APC being called, even if no completion routine was
739 * specified at the time of starting the async operation */
740 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
741 } while (res
== WAIT_IO_COMPLETION
);
743 if (res
== WAIT_OBJECT_0
)
745 else if (res
== WAIT_FAILED
)
747 ERR("wait failed with error %d\n", GetLastError());
752 b_handle
= objs
[res
- WAIT_OBJECT_0
];
753 /* find which connection got a RPC */
754 EnterCriticalSection(&protseq
->cs
);
755 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
757 if (b_handle
== conn
->ovl
.hEvent
) break;
758 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
762 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
764 ERR("failed to locate connection for handle %p\n", b_handle
);
765 LeaveCriticalSection(&protseq
->cs
);
768 RPCRT4_new_client(cconn
);
775 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
776 const char *networkaddr
,
777 const char *endpoint
)
779 twr_empty_floor_t
*pipe_floor
;
781 size_t endpoint_size
;
783 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
785 endpoint_size
= strlen(endpoint
) + 1;
786 size
= sizeof(*pipe_floor
) + endpoint_size
;
791 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
793 tower_data
+= sizeof(*pipe_floor
);
795 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
796 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
797 pipe_floor
->count_rhs
= endpoint_size
;
799 memcpy(tower_data
, endpoint
, endpoint_size
);
804 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
809 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
811 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
813 if (tower_size
< sizeof(*pipe_floor
))
814 return EPT_S_NOT_REGISTERED
;
816 tower_data
+= sizeof(*pipe_floor
);
817 tower_size
-= sizeof(*pipe_floor
);
819 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
820 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
821 (pipe_floor
->count_rhs
> tower_size
) ||
822 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
823 return EPT_S_NOT_REGISTERED
;
830 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
832 return RPC_S_OUT_OF_RESOURCES
;
833 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
839 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
844 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
845 unsigned char *in_buffer
,
846 unsigned int in_size
,
847 unsigned char *out_buffer
,
848 unsigned int *out_size
)
850 /* since this protocol is local to the machine there is no need to
851 * authenticate the caller */
856 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
857 enum secure_packet_direction dir
,
858 RpcPktHdr
*hdr
, unsigned int hdr_size
,
859 unsigned char *stub_data
, unsigned int stub_data_size
,
860 RpcAuthVerifier
*auth_hdr
,
861 unsigned char *auth_value
, unsigned int auth_value_size
)
863 /* since this protocol is local to the machine there is no need to secure
868 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
869 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
870 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
872 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
873 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
877 FIXME("privs not implemented\n");
880 if (server_princ_name
)
882 FIXME("server_princ_name not implemented\n");
883 *server_princ_name
= NULL
;
885 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
886 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
889 FIXME("authorization service not implemented\n");
890 *authz_svc
= RPC_C_AUTHZ_NONE
;
893 FIXME("flags 0x%x not implemented\n", flags
);
898 /**** ncacn_ip_tcp support ****/
900 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
901 const char *networkaddr
,
902 unsigned char tcp_protid
,
903 const char *endpoint
)
905 twr_tcp_floor_t
*tcp_floor
;
906 twr_ipv4_floor_t
*ipv4_floor
;
908 struct addrinfo hints
;
910 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
912 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
917 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
918 tower_data
+= sizeof(*tcp_floor
);
920 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
922 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
923 tcp_floor
->protid
= tcp_protid
;
924 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
926 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
927 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
928 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
930 hints
.ai_flags
= AI_NUMERICHOST
;
931 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
932 hints
.ai_family
= PF_INET
;
933 hints
.ai_socktype
= SOCK_STREAM
;
934 hints
.ai_protocol
= IPPROTO_TCP
;
935 hints
.ai_addrlen
= 0;
936 hints
.ai_addr
= NULL
;
937 hints
.ai_canonname
= NULL
;
938 hints
.ai_next
= NULL
;
940 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
943 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
946 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
951 if (ai
->ai_family
== PF_INET
)
953 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
954 tcp_floor
->port
= sin
->sin_port
;
955 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
959 ERR("unexpected protocol family %d\n", ai
->ai_family
);
968 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
971 unsigned char tcp_protid
,
974 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
975 const twr_ipv4_floor_t
*ipv4_floor
;
976 struct in_addr in_addr
;
978 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
980 if (tower_size
< sizeof(*tcp_floor
))
981 return EPT_S_NOT_REGISTERED
;
983 tower_data
+= sizeof(*tcp_floor
);
984 tower_size
-= sizeof(*tcp_floor
);
986 if (tower_size
< sizeof(*ipv4_floor
))
987 return EPT_S_NOT_REGISTERED
;
989 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
991 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
992 (tcp_floor
->protid
!= tcp_protid
) ||
993 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
994 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
995 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
996 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
997 return EPT_S_NOT_REGISTERED
;
1001 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1003 return RPC_S_OUT_OF_RESOURCES
;
1004 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1009 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1014 I_RpcFree(*endpoint
);
1017 return RPC_S_OUT_OF_RESOURCES
;
1019 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1020 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1022 ERR("inet_ntop: %s\n", strerror(errno
));
1023 I_RpcFree(*networkaddr
);
1024 *networkaddr
= NULL
;
1027 I_RpcFree(*endpoint
);
1030 return EPT_S_NOT_REGISTERED
;
1037 typedef struct _RpcConnection_tcp
1039 RpcConnection common
;
1041 #ifdef HAVE_SOCKETPAIR
1045 HANDLE cancel_event
;
1047 } RpcConnection_tcp
;
1049 #ifdef HAVE_SOCKETPAIR
1051 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1053 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1055 ERR("socketpair() failed: %s\n", strerror(errno
));
1061 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1063 struct pollfd pfds
[2];
1064 pfds
[0].fd
= tcpc
->sock
;
1065 pfds
[0].events
= POLLIN
;
1066 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1067 pfds
[1].events
= POLLIN
;
1068 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1070 ERR("poll() failed: %s\n", strerror(errno
));
1073 if (pfds
[1].revents
& POLLIN
) /* canceled */
1076 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1082 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1085 pfd
.fd
= tcpc
->sock
;
1086 pfd
.events
= POLLOUT
;
1087 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1089 ERR("poll() failed: %s\n", strerror(errno
));
1095 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1099 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1102 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1104 close(tcpc
->cancel_fds
[0]);
1105 close(tcpc
->cancel_fds
[1]);
1108 #else /* HAVE_SOCKETPAIR */
1110 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1112 static BOOL wsa_inited
;
1116 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1117 /* Note: WSAStartup can be called more than once so we don't bother with
1118 * making accesses to wsa_inited thread-safe */
1121 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1122 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1123 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1125 ERR("event creation failed\n");
1126 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1132 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1134 HANDLE wait_handles
[2];
1136 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1138 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1141 wait_handles
[0] = tcpc
->sock_event
;
1142 wait_handles
[1] = tcpc
->cancel_event
;
1143 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1148 case WAIT_OBJECT_0
+ 1:
1151 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1156 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1159 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1161 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1164 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1170 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1175 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1177 SetEvent(tcpc
->cancel_event
);
1180 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1182 CloseHandle(tcpc
->sock_event
);
1183 CloseHandle(tcpc
->cancel_event
);
1188 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1190 RpcConnection_tcp
*tcpc
;
1191 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1195 if (!rpcrt4_sock_wait_init(tcpc
))
1197 HeapFree(GetProcessHeap(), 0, tcpc
);
1200 return &tcpc
->common
;
1203 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1205 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1208 struct addrinfo
*ai
;
1209 struct addrinfo
*ai_cur
;
1210 struct addrinfo hints
;
1212 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1214 if (tcpc
->sock
!= -1)
1218 hints
.ai_family
= PF_UNSPEC
;
1219 hints
.ai_socktype
= SOCK_STREAM
;
1220 hints
.ai_protocol
= IPPROTO_TCP
;
1221 hints
.ai_addrlen
= 0;
1222 hints
.ai_addr
= NULL
;
1223 hints
.ai_canonname
= NULL
;
1224 hints
.ai_next
= NULL
;
1226 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1229 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1230 Connection
->Endpoint
, gai_strerror(ret
));
1231 return RPC_S_SERVER_UNAVAILABLE
;
1234 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1239 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1241 TRACE("skipping non-IP/IPv6 address family\n");
1249 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1250 host
, sizeof(host
), service
, sizeof(service
),
1251 NI_NUMERICHOST
| NI_NUMERICSERV
);
1252 TRACE("trying %s:%s\n", host
, service
);
1255 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1258 WARN("socket() failed: %s\n", strerror(errno
));
1262 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1264 WARN("connect() failed: %s\n", strerror(errno
));
1269 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1271 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1273 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1278 TRACE("connected\n");
1283 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1284 return RPC_S_SERVER_UNAVAILABLE
;
1287 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1289 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1292 struct addrinfo
*ai
;
1293 struct addrinfo
*ai_cur
;
1294 struct addrinfo hints
;
1295 RpcConnection
*first_connection
= NULL
;
1297 TRACE("(%p, %s)\n", protseq
, endpoint
);
1299 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1300 hints
.ai_family
= PF_UNSPEC
;
1301 hints
.ai_socktype
= SOCK_STREAM
;
1302 hints
.ai_protocol
= IPPROTO_TCP
;
1303 hints
.ai_addrlen
= 0;
1304 hints
.ai_addr
= NULL
;
1305 hints
.ai_canonname
= NULL
;
1306 hints
.ai_next
= NULL
;
1308 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1311 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1313 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1314 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1315 return RPC_S_CANT_CREATE_ENDPOINT
;
1318 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1320 RpcConnection_tcp
*tcpc
;
1321 RPC_STATUS create_status
;
1322 struct sockaddr_storage sa
;
1324 char service
[NI_MAXSERV
];
1327 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1329 TRACE("skipping non-IP/IPv6 address family\n");
1336 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1337 host
, sizeof(host
), service
, sizeof(service
),
1338 NI_NUMERICHOST
| NI_NUMERICSERV
);
1339 TRACE("trying %s:%s\n", host
, service
);
1342 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1345 WARN("socket() failed: %s\n", strerror(errno
));
1346 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1350 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1353 WARN("bind failed: %s\n", strerror(errno
));
1355 if (errno
== EADDRINUSE
)
1356 status
= RPC_S_DUPLICATE_ENDPOINT
;
1358 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1362 sa_len
= sizeof(sa
);
1363 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1365 WARN("getsockname() failed: %s\n", strerror(errno
));
1366 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1370 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1371 NULL
, 0, service
, sizeof(service
),
1375 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1376 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1380 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1381 protseq
->Protseq
, NULL
,
1382 service
, NULL
, NULL
, NULL
);
1383 if (create_status
!= RPC_S_OK
)
1386 status
= create_status
;
1391 ret
= listen(sock
, protseq
->MaxCalls
);
1394 WARN("listen failed: %s\n", strerror(errno
));
1395 RPCRT4_DestroyConnection(&tcpc
->common
);
1396 status
= RPC_S_OUT_OF_RESOURCES
;
1399 /* need a non-blocking socket, otherwise accept() has a potential
1400 * race-condition (poll() says it is readable, connection drops,
1401 * and accept() blocks until the next connection comes...)
1404 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1407 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1408 RPCRT4_DestroyConnection(&tcpc
->common
);
1409 status
= RPC_S_OUT_OF_RESOURCES
;
1413 tcpc
->common
.Next
= first_connection
;
1414 first_connection
= &tcpc
->common
;
1416 /* since IPv4 and IPv6 share the same port space, we only need one
1417 * successful bind to listen for both */
1423 /* if at least one connection was created for an endpoint then
1425 if (first_connection
)
1427 RpcConnection
*conn
;
1429 /* find last element in list */
1430 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1433 EnterCriticalSection(&protseq
->cs
);
1434 conn
->Next
= protseq
->conn
;
1435 protseq
->conn
= first_connection
;
1436 LeaveCriticalSection(&protseq
->cs
);
1438 TRACE("listening on %s\n", endpoint
);
1442 ERR("couldn't listen on port %s\n", endpoint
);
1446 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1449 struct sockaddr_in address
;
1451 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1452 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1455 addrsize
= sizeof(address
);
1456 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1459 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1460 return RPC_S_OUT_OF_RESOURCES
;
1463 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1465 TRACE("Accepted a new TCP connection\n");
1469 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1470 void *buffer
, unsigned int count
)
1472 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1474 while (bytes_read
!= count
)
1476 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1481 else if (errno
!= EAGAIN
)
1483 WARN("recv() failed: %s\n", strerror(errno
));
1488 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1492 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1496 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1497 const void *buffer
, unsigned int count
)
1499 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1500 int bytes_written
= 0;
1501 while (bytes_written
!= count
)
1503 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1506 else if (errno
!= EAGAIN
)
1510 if (!rpcrt4_sock_wait_for_send(tcpc
))
1514 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1515 return bytes_written
;
1518 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1520 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1522 TRACE("%d\n", tcpc
->sock
);
1524 if (tcpc
->sock
!= -1)
1525 closesocket(tcpc
->sock
);
1527 rpcrt4_sock_wait_destroy(tcpc
);
1531 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1533 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1534 TRACE("%p\n", Connection
);
1535 rpcrt4_sock_wait_cancel(tcpc
);
1538 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1540 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1542 TRACE("%p\n", Connection
);
1544 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1549 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1550 const char *networkaddr
,
1551 const char *endpoint
)
1553 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1554 EPM_PROTOCOL_TCP
, endpoint
);
1557 #ifdef HAVE_SOCKETPAIR
1559 typedef struct _RpcServerProtseq_sock
1561 RpcServerProtseq common
;
1564 } RpcServerProtseq_sock
;
1566 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1568 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1572 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1574 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1575 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1576 ps
->mgr_event_rcv
= fds
[0];
1577 ps
->mgr_event_snd
= fds
[1];
1581 ERR("socketpair failed with error %s\n", strerror(errno
));
1582 HeapFree(GetProcessHeap(), 0, ps
);
1589 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1591 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1593 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1596 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1598 struct pollfd
*poll_info
= prev_array
;
1599 RpcConnection_tcp
*conn
;
1600 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1602 EnterCriticalSection(&protseq
->cs
);
1604 /* open and count connections */
1606 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1608 if (conn
->sock
!= -1)
1610 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1613 /* make array of connections */
1615 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1617 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1620 ERR("couldn't allocate poll_info\n");
1621 LeaveCriticalSection(&protseq
->cs
);
1625 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1626 poll_info
[0].events
= POLLIN
;
1628 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1630 if (conn
->sock
!= -1)
1632 poll_info
[*count
].fd
= conn
->sock
;
1633 poll_info
[*count
].events
= POLLIN
;
1636 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1638 LeaveCriticalSection(&protseq
->cs
);
1642 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1644 HeapFree(GetProcessHeap(), 0, array
);
1647 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1649 struct pollfd
*poll_info
= wait_array
;
1652 RpcConnection
*cconn
;
1653 RpcConnection_tcp
*conn
;
1658 ret
= poll(poll_info
, count
, -1);
1661 ERR("poll failed with error %d\n", ret
);
1665 for (i
= 0; i
< count
; i
++)
1666 if (poll_info
[i
].revents
& POLLIN
)
1668 /* RPC server event */
1672 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1676 /* find which connection got a RPC */
1677 EnterCriticalSection(&protseq
->cs
);
1678 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1680 if (poll_info
[i
].fd
== conn
->sock
) break;
1681 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1685 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1687 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1688 LeaveCriticalSection(&protseq
->cs
);
1690 RPCRT4_new_client(cconn
);
1698 #else /* HAVE_SOCKETPAIR */
1700 typedef struct _RpcServerProtseq_sock
1702 RpcServerProtseq common
;
1704 } RpcServerProtseq_sock
;
1706 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1708 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1711 static BOOL wsa_inited
;
1715 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1716 /* Note: WSAStartup can be called more than once so we don't bother with
1717 * making accesses to wsa_inited thread-safe */
1720 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1725 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1727 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1728 SetEvent(sockps
->mgr_event
);
1731 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1733 HANDLE
*objs
= prev_array
;
1734 RpcConnection_tcp
*conn
;
1735 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1737 EnterCriticalSection(&protseq
->cs
);
1739 /* open and count connections */
1741 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1744 if (conn
->sock
!= -1)
1746 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1749 /* make array of connections */
1751 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1753 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1756 ERR("couldn't allocate objs\n");
1757 LeaveCriticalSection(&protseq
->cs
);
1761 objs
[0] = sockps
->mgr_event
;
1763 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1766 if (conn
->sock
!= -1)
1768 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1769 if (res
== SOCKET_ERROR
)
1770 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1773 objs
[*count
] = conn
->sock_event
;
1777 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1779 LeaveCriticalSection(&protseq
->cs
);
1783 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1785 HeapFree(GetProcessHeap(), 0, array
);
1788 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1791 HANDLE
*objs
= wait_array
;
1793 RpcConnection
*cconn
;
1794 RpcConnection_tcp
*conn
;
1801 /* an alertable wait isn't strictly necessary, but due to our
1802 * overlapped I/O implementation in Wine we need to free some memory
1803 * by the file user APC being called, even if no completion routine was
1804 * specified at the time of starting the async operation */
1805 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1806 } while (res
== WAIT_IO_COMPLETION
);
1808 if (res
== WAIT_OBJECT_0
)
1810 else if (res
== WAIT_FAILED
)
1812 ERR("wait failed with error %d\n", GetLastError());
1817 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1818 /* find which connection got a RPC */
1819 EnterCriticalSection(&protseq
->cs
);
1820 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1823 if (b_handle
== conn
->sock_event
) break;
1824 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1828 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1830 ERR("failed to locate connection for handle %p\n", b_handle
);
1831 LeaveCriticalSection(&protseq
->cs
);
1834 RPCRT4_new_client(cconn
);
1841 #endif /* HAVE_SOCKETPAIR */
1843 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1848 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1849 networkaddr
, EPM_PROTOCOL_TCP
,
1853 /**** ncacn_http support ****/
1855 /* 60 seconds is the period native uses */
1856 #define HTTP_IDLE_TIME 60000
1858 /* reference counted to avoid a race between a cancelled call's connection
1859 * being destroyed and the asynchronous InternetReadFileEx call being
1861 typedef struct _RpcHttpAsyncData
1864 HANDLE completion_event
;
1865 INTERNET_BUFFERSA inet_buffers
;
1866 void *destination_buffer
; /* the address that inet_buffers.lpvBuffer will be
1867 * copied into when the call completes */
1868 CRITICAL_SECTION cs
;
1871 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1873 return InterlockedIncrement(&data
->refs
);
1876 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1878 ULONG refs
= InterlockedDecrement(&data
->refs
);
1881 TRACE("destroying async data %p\n", data
);
1882 CloseHandle(data
->completion_event
);
1883 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1884 DeleteCriticalSection(&data
->cs
);
1885 HeapFree(GetProcessHeap(), 0, data
);
1890 typedef struct _RpcConnection_http
1892 RpcConnection common
;
1895 HINTERNET in_request
;
1896 HINTERNET out_request
;
1897 HANDLE timer_cancelled
;
1898 HANDLE cancel_event
;
1899 DWORD last_sent_time
;
1900 ULONG bytes_received
;
1901 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1902 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1903 UUID connection_uuid
;
1906 RpcHttpAsyncData
*async_data
;
1907 } RpcConnection_http
;
1909 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1911 RpcConnection_http
*httpc
;
1912 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1913 if (!httpc
) return NULL
;
1914 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1915 if (!httpc
->async_data
)
1917 HeapFree(GetProcessHeap(), 0, httpc
);
1920 TRACE("async data = %p\n", httpc
->async_data
);
1921 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1922 httpc
->async_data
->refs
= 1;
1923 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1924 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1925 httpc
->async_data
->destination_buffer
= NULL
;
1926 InitializeCriticalSection(&httpc
->async_data
->cs
);
1927 return &httpc
->common
;
1930 typedef struct _HttpTimerThreadData
1933 DWORD
*last_sent_time
;
1934 HANDLE timer_cancelled
;
1935 } HttpTimerThreadData
;
1937 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1939 HINTERNET in_request
= param
;
1940 RpcPktHdr
*idle_pkt
;
1942 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1946 DWORD bytes_written
;
1947 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1948 RPCRT4_FreeHeader(idle_pkt
);
1952 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1954 DWORD cur_time
= GetTickCount();
1955 DWORD cached_last_sent_time
= *last_sent_time
;
1956 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1959 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1961 HttpTimerThreadData
*data_in
= param
;
1962 HttpTimerThreadData data
;
1966 HeapFree(GetProcessHeap(), 0, data_in
);
1968 for (timeout
= HTTP_IDLE_TIME
;
1969 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1970 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1972 /* are we too soon after last send? */
1973 if (GetTickCount() - HTTP_IDLE_TIME
< *data
.last_sent_time
)
1975 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
1978 CloseHandle(data
.timer_cancelled
);
1982 static VOID WINAPI
rpcrt4_http_internet_callback(
1983 HINTERNET hInternet
,
1984 DWORD_PTR dwContext
,
1985 DWORD dwInternetStatus
,
1986 LPVOID lpvStatusInformation
,
1987 DWORD dwStatusInformationLength
)
1989 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
1991 switch (dwInternetStatus
)
1993 case INTERNET_STATUS_REQUEST_COMPLETE
:
1994 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1997 if (async_data
->inet_buffers
.lpvBuffer
)
1999 EnterCriticalSection(&async_data
->cs
);
2000 if (async_data
->destination_buffer
)
2002 memcpy(async_data
->destination_buffer
,
2003 async_data
->inet_buffers
.lpvBuffer
,
2004 async_data
->inet_buffers
.dwBufferLength
);
2005 async_data
->destination_buffer
= NULL
;
2007 LeaveCriticalSection(&async_data
->cs
);
2009 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2010 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2011 SetEvent(async_data
->completion_event
);
2012 RpcHttpAsyncData_Release(async_data
);
2018 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2025 WCHAR
*status_text
= buf
;
2029 size
= sizeof(status_code
);
2030 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2032 return GetLastError();
2033 if (status_code
< 400)
2037 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2038 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2040 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2041 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2044 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2045 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2047 if (status_code
== HTTP_STATUS_DENIED
)
2048 return ERROR_ACCESS_DENIED
;
2049 return RPC_S_SERVER_UNAVAILABLE
;
2052 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2054 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2055 LPWSTR proxy
= NULL
;
2057 LPWSTR password
= NULL
;
2058 LPWSTR servername
= NULL
;
2059 const WCHAR
*option
;
2060 INTERNET_PORT port
= INTERNET_INVALID_PORT_NUMBER
; /* use default port */
2062 if (httpc
->common
.QOS
&&
2063 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2065 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2066 if (http_cred
->TransportCredentials
)
2069 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2070 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2071 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2073 return RPC_S_OUT_OF_RESOURCES
;
2075 if (cred
->DomainLength
)
2077 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2078 p
+= cred
->DomainLength
;
2082 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2083 p
[cred
->UserLength
] = 0;
2085 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2089 for (option
= httpc
->common
.NetworkOptions
; option
;
2090 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2092 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2093 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2095 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2097 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2098 const WCHAR
*value_end
;
2101 value_end
= strchrW(option
, ',');
2103 value_end
= value_start
+ strlenW(value_start
);
2104 for (p
= value_start
; p
< value_end
; p
++)
2111 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2112 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2114 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2116 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2117 const WCHAR
*value_end
;
2119 value_end
= strchrW(option
, ',');
2121 value_end
= value_start
+ strlenW(value_start
);
2122 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2123 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2126 FIXME("unhandled option %s\n", debugstr_w(option
));
2129 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2130 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2131 if (!httpc
->app_info
)
2133 HeapFree(GetProcessHeap(), 0, password
);
2134 HeapFree(GetProcessHeap(), 0, user
);
2135 ERR("InternetOpenW failed with error %d\n", GetLastError());
2136 return RPC_S_SERVER_UNAVAILABLE
;
2138 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2140 /* if no RpcProxy option specified, set the HTTP server address to the
2141 * RPC server address */
2144 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2147 HeapFree(GetProcessHeap(), 0, password
);
2148 HeapFree(GetProcessHeap(), 0, user
);
2149 return RPC_S_OUT_OF_RESOURCES
;
2151 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2154 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2155 INTERNET_SERVICE_HTTP
, 0, 0);
2157 HeapFree(GetProcessHeap(), 0, password
);
2158 HeapFree(GetProcessHeap(), 0, user
);
2159 HeapFree(GetProcessHeap(), 0, servername
);
2161 if (!httpc
->session
)
2163 ERR("InternetConnectW failed with error %d\n", GetLastError());
2164 return RPC_S_SERVER_UNAVAILABLE
;
2170 /* prepare the in pipe for use by RPC packets */
2171 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
,
2172 const UUID
*connection_uuid
,
2173 const UUID
*in_pipe_uuid
,
2174 const UUID
*association_uuid
)
2180 INTERNET_BUFFERSW buffers_in
;
2181 DWORD bytes_read
, bytes_written
;
2183 /* prepare in pipe */
2184 ResetEvent(async_data
->completion_event
);
2185 RpcHttpAsyncData_AddRef(async_data
);
2186 ret
= HttpSendRequestW(in_request
, NULL
, 0, NULL
, 0);
2189 if (GetLastError() == ERROR_IO_PENDING
)
2190 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2193 RpcHttpAsyncData_Release(async_data
);
2194 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2195 return RPC_S_SERVER_UNAVAILABLE
;
2198 status
= rpcrt4_http_check_response(in_request
);
2199 if (status
!= RPC_S_OK
) return status
;
2201 InternetReadFile(in_request
, packet
, 20, &bytes_read
);
2202 /* FIXME: do something with retrieved data */
2204 memset(&buffers_in
, 0, sizeof(buffers_in
));
2205 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2206 /* FIXME: get this from the registry */
2207 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2208 ResetEvent(async_data
->completion_event
);
2209 RpcHttpAsyncData_AddRef(async_data
);
2210 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2213 if (GetLastError() == ERROR_IO_PENDING
)
2214 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2217 RpcHttpAsyncData_Release(async_data
);
2218 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
2219 return RPC_S_SERVER_UNAVAILABLE
;
2223 TRACE("sending HTTP connect header to server\n");
2224 hdr
= RPCRT4_BuildHttpConnectHeader(0, FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2225 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2226 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2227 RPCRT4_FreeHeader(hdr
);
2230 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2231 return RPC_S_SERVER_UNAVAILABLE
;
2237 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2241 unsigned short data_len
;
2243 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2245 return RPC_S_SERVER_UNAVAILABLE
;
2246 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2248 ERR("wrong packet type received %d or wrong frag_len %d\n",
2249 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2250 return RPC_S_PROTOCOL_ERROR
;
2253 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2255 return RPC_S_SERVER_UNAVAILABLE
;
2257 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2260 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2262 return RPC_S_OUT_OF_RESOURCES
;
2263 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2266 HeapFree(GetProcessHeap(), 0, *data
);
2267 return RPC_S_SERVER_UNAVAILABLE
;
2273 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2275 ERR("invalid http packet\n");
2276 return RPC_S_PROTOCOL_ERROR
;
2282 /* prepare the out pipe for use by RPC packets */
2283 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
,
2284 RpcHttpAsyncData
*async_data
,
2285 const UUID
*connection_uuid
,
2286 const UUID
*out_pipe_uuid
,
2287 ULONG
*flow_control_increment
)
2294 BYTE
*data_from_server
;
2295 RpcPktHdr pkt_from_server
;
2296 ULONG field1
, field3
;
2298 ResetEvent(async_data
->completion_event
);
2299 RpcHttpAsyncData_AddRef(async_data
);
2300 ret
= HttpSendRequestW(out_request
, NULL
, 0, NULL
, 0);
2303 if (GetLastError() == ERROR_IO_PENDING
)
2304 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2307 RpcHttpAsyncData_Release(async_data
);
2308 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2309 return RPC_S_SERVER_UNAVAILABLE
;
2312 status
= rpcrt4_http_check_response(out_request
);
2313 if (status
!= RPC_S_OK
) return status
;
2315 InternetReadFile(out_request
, packet
, 20, &bytes_read
);
2316 /* FIXME: do something with retrieved data */
2318 hdr
= RPCRT4_BuildHttpConnectHeader(0, TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2319 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2320 ResetEvent(async_data
->completion_event
);
2321 RpcHttpAsyncData_AddRef(async_data
);
2322 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2325 if (GetLastError() == ERROR_IO_PENDING
)
2326 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2329 RpcHttpAsyncData_Release(async_data
);
2330 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2331 RPCRT4_FreeHeader(hdr
);
2332 return RPC_S_SERVER_UNAVAILABLE
;
2335 RPCRT4_FreeHeader(hdr
);
2336 status
= rpcrt4_http_check_response(out_request
);
2337 if (status
!= RPC_S_OK
) return status
;
2339 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2341 if (status
!= RPC_S_OK
) return status
;
2342 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2344 HeapFree(GetProcessHeap(), 0, data_from_server
);
2345 if (status
!= RPC_S_OK
) return status
;
2346 TRACE("received (%d) from first prepare header\n", field1
);
2348 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2350 if (status
!= RPC_S_OK
) return status
;
2351 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2352 &field1
, flow_control_increment
,
2354 HeapFree(GetProcessHeap(), 0, data_from_server
);
2355 if (status
!= RPC_S_OK
) return status
;
2356 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2361 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2363 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2364 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2365 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2366 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2367 static const WCHAR wszColon
[] = {':',0};
2368 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2369 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2373 HttpTimerThreadData
*timer_data
;
2376 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2378 if (Connection
->server
)
2380 ERR("ncacn_http servers not supported yet\n");
2381 return RPC_S_SERVER_UNAVAILABLE
;
2384 if (httpc
->in_request
)
2387 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2389 status
= UuidCreate(&httpc
->connection_uuid
);
2390 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2391 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2393 status
= rpcrt4_http_internet_connect(httpc
);
2394 if (status
!= RPC_S_OK
)
2397 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2399 return RPC_S_OUT_OF_MEMORY
;
2400 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2401 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2402 strcatW(url
, wszColon
);
2403 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2405 secure
= httpc
->common
.QOS
&&
2406 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2407 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2409 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
,
2411 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2412 (DWORD_PTR
)httpc
->async_data
);
2413 if (!httpc
->in_request
)
2415 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2416 return RPC_S_SERVER_UNAVAILABLE
;
2418 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
,
2420 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2421 (DWORD_PTR
)httpc
->async_data
);
2422 if (!httpc
->out_request
)
2424 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2425 return RPC_S_SERVER_UNAVAILABLE
;
2428 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
,
2430 &httpc
->connection_uuid
,
2431 &httpc
->in_pipe_uuid
,
2432 &Connection
->assoc
->http_uuid
);
2433 if (status
!= RPC_S_OK
)
2436 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
,
2438 &httpc
->connection_uuid
,
2439 &httpc
->out_pipe_uuid
,
2440 &httpc
->flow_control_increment
);
2441 if (status
!= RPC_S_OK
)
2444 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2445 httpc
->last_sent_time
= GetTickCount();
2446 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2448 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2450 return ERROR_OUTOFMEMORY
;
2451 timer_data
->timer_param
= httpc
->in_request
;
2452 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2453 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2454 /* FIXME: should use CreateTimerQueueTimer when implemented */
2455 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2458 HeapFree(GetProcessHeap(), 0, timer_data
);
2459 return GetLastError();
2461 CloseHandle(thread
);
2466 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2469 return RPC_S_SERVER_UNAVAILABLE
;
2472 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2473 void *buffer
, unsigned int count
)
2475 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2478 unsigned int bytes_left
= count
;
2480 ResetEvent(httpc
->async_data
->completion_event
);
2483 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2484 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2485 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, bytes_left
);
2486 httpc
->async_data
->destination_buffer
= buf
;
2487 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
2490 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2492 RpcHttpAsyncData_Release(httpc
->async_data
);
2493 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
2494 httpc
->async_data
->inet_buffers
.dwBufferLength
);
2495 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2496 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2497 httpc
->async_data
->destination_buffer
= NULL
;
2501 if (GetLastError() == ERROR_IO_PENDING
)
2503 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2504 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2505 if (result
== WAIT_OBJECT_0
)
2509 TRACE("call cancelled\n");
2510 EnterCriticalSection(&httpc
->async_data
->cs
);
2511 httpc
->async_data
->destination_buffer
= NULL
;
2512 LeaveCriticalSection(&httpc
->async_data
->cs
);
2518 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2519 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2520 httpc
->async_data
->destination_buffer
= NULL
;
2521 RpcHttpAsyncData_Release(httpc
->async_data
);
2525 if (!httpc
->async_data
->inet_buffers
.dwBufferLength
)
2527 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2528 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2530 TRACE("%p %p %u -> %s\n", httpc
->out_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2531 return ret
? count
: -1;
2534 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2536 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2540 RpcPktCommonHdr common_hdr
;
2544 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2547 /* read packet common header */
2548 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2549 if (dwRead
!= sizeof(common_hdr
)) {
2550 WARN("Short read of header, %d bytes\n", dwRead
);
2551 status
= RPC_S_PROTOCOL_ERROR
;
2554 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2555 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2557 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2558 status
= RPC_S_PROTOCOL_ERROR
;
2562 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2563 if (status
!= RPC_S_OK
) goto fail
;
2565 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2566 if (hdr_length
== 0) {
2567 WARN("header length == 0\n");
2568 status
= RPC_S_PROTOCOL_ERROR
;
2572 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2575 status
= RPC_S_OUT_OF_RESOURCES
;
2578 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2580 /* read the rest of packet header */
2581 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2582 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2583 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2584 status
= RPC_S_PROTOCOL_ERROR
;
2588 if (common_hdr
.frag_len
- hdr_length
)
2590 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2593 status
= RPC_S_OUT_OF_RESOURCES
;
2597 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2598 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2600 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2601 status
= RPC_S_PROTOCOL_ERROR
;
2608 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2610 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2612 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2613 status
= RPC_S_PROTOCOL_ERROR
;
2616 if ((*Header
)->http
.flags
== 0x0001)
2618 TRACE("http idle packet, waiting for real packet\n");
2619 if ((*Header
)->http
.num_data_items
!= 0)
2621 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2622 status
= RPC_S_PROTOCOL_ERROR
;
2626 else if ((*Header
)->http
.flags
== 0x0002)
2628 ULONG bytes_transmitted
;
2629 ULONG flow_control_increment
;
2631 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2634 &flow_control_increment
,
2636 if (status
!= RPC_S_OK
)
2638 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2639 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2640 /* FIXME: do something with parsed data */
2644 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
2645 status
= RPC_S_PROTOCOL_ERROR
;
2648 RPCRT4_FreeHeader(*Header
);
2650 HeapFree(GetProcessHeap(), 0, *Payload
);
2658 httpc
->bytes_received
+= common_hdr
.frag_len
;
2660 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
2662 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
2664 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
2665 httpc
->bytes_received
,
2666 httpc
->flow_control_increment
,
2667 &httpc
->out_pipe_uuid
);
2670 DWORD bytes_written
;
2672 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
2673 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2674 RPCRT4_FreeHeader(hdr
);
2676 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
2681 if (status
!= RPC_S_OK
) {
2682 RPCRT4_FreeHeader(*Header
);
2684 HeapFree(GetProcessHeap(), 0, *Payload
);
2690 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
2691 const void *buffer
, unsigned int count
)
2693 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2694 DWORD bytes_written
;
2697 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
2698 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
2699 httpc
->last_sent_time
= GetTickCount();
2700 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2701 return ret
? bytes_written
: -1;
2704 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
2706 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2710 SetEvent(httpc
->timer_cancelled
);
2711 if (httpc
->in_request
)
2712 InternetCloseHandle(httpc
->in_request
);
2713 httpc
->in_request
= NULL
;
2714 if (httpc
->out_request
)
2715 InternetCloseHandle(httpc
->out_request
);
2716 httpc
->out_request
= NULL
;
2717 if (httpc
->app_info
)
2718 InternetCloseHandle(httpc
->app_info
);
2719 httpc
->app_info
= NULL
;
2721 InternetCloseHandle(httpc
->session
);
2722 httpc
->session
= NULL
;
2723 RpcHttpAsyncData_Release(httpc
->async_data
);
2724 if (httpc
->cancel_event
)
2725 CloseHandle(httpc
->cancel_event
);
2730 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
2732 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2734 SetEvent(httpc
->cancel_event
);
2737 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
2740 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2742 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2743 ret
= InternetQueryDataAvailable(httpc
->out_request
,
2744 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
2747 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2749 RpcHttpAsyncData_Release(httpc
->async_data
);
2753 if (GetLastError() == ERROR_IO_PENDING
)
2755 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2756 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2757 if (result
!= WAIT_OBJECT_0
)
2759 TRACE("call cancelled\n");
2765 RpcHttpAsyncData_Release(httpc
->async_data
);
2774 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
2775 const char *networkaddr
,
2776 const char *endpoint
)
2778 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
2779 EPM_PROTOCOL_HTTP
, endpoint
);
2782 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
2787 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
2788 networkaddr
, EPM_PROTOCOL_HTTP
,
2792 static const struct connection_ops conn_protseq_list
[] = {
2794 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
2795 rpcrt4_conn_np_alloc
,
2796 rpcrt4_ncacn_np_open
,
2797 rpcrt4_ncacn_np_handoff
,
2798 rpcrt4_conn_np_read
,
2799 rpcrt4_conn_np_write
,
2800 rpcrt4_conn_np_close
,
2801 rpcrt4_conn_np_cancel_call
,
2802 rpcrt4_conn_np_wait_for_incoming_data
,
2803 rpcrt4_ncacn_np_get_top_of_tower
,
2804 rpcrt4_ncacn_np_parse_top_of_tower
,
2806 RPCRT4_default_is_authorized
,
2807 RPCRT4_default_authorize
,
2808 RPCRT4_default_secure_packet
,
2809 rpcrt4_conn_np_impersonate_client
,
2810 rpcrt4_conn_np_revert_to_self
,
2811 RPCRT4_default_inquire_auth_client
,
2814 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
2815 rpcrt4_conn_np_alloc
,
2816 rpcrt4_ncalrpc_open
,
2817 rpcrt4_ncalrpc_handoff
,
2818 rpcrt4_conn_np_read
,
2819 rpcrt4_conn_np_write
,
2820 rpcrt4_conn_np_close
,
2821 rpcrt4_conn_np_cancel_call
,
2822 rpcrt4_conn_np_wait_for_incoming_data
,
2823 rpcrt4_ncalrpc_get_top_of_tower
,
2824 rpcrt4_ncalrpc_parse_top_of_tower
,
2826 rpcrt4_ncalrpc_is_authorized
,
2827 rpcrt4_ncalrpc_authorize
,
2828 rpcrt4_ncalrpc_secure_packet
,
2829 rpcrt4_conn_np_impersonate_client
,
2830 rpcrt4_conn_np_revert_to_self
,
2831 rpcrt4_ncalrpc_inquire_auth_client
,
2834 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
2835 rpcrt4_conn_tcp_alloc
,
2836 rpcrt4_ncacn_ip_tcp_open
,
2837 rpcrt4_conn_tcp_handoff
,
2838 rpcrt4_conn_tcp_read
,
2839 rpcrt4_conn_tcp_write
,
2840 rpcrt4_conn_tcp_close
,
2841 rpcrt4_conn_tcp_cancel_call
,
2842 rpcrt4_conn_tcp_wait_for_incoming_data
,
2843 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
2844 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
2846 RPCRT4_default_is_authorized
,
2847 RPCRT4_default_authorize
,
2848 RPCRT4_default_secure_packet
,
2849 RPCRT4_default_impersonate_client
,
2850 RPCRT4_default_revert_to_self
,
2851 RPCRT4_default_inquire_auth_client
,
2854 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
2855 rpcrt4_ncacn_http_alloc
,
2856 rpcrt4_ncacn_http_open
,
2857 rpcrt4_ncacn_http_handoff
,
2858 rpcrt4_ncacn_http_read
,
2859 rpcrt4_ncacn_http_write
,
2860 rpcrt4_ncacn_http_close
,
2861 rpcrt4_ncacn_http_cancel_call
,
2862 rpcrt4_ncacn_http_wait_for_incoming_data
,
2863 rpcrt4_ncacn_http_get_top_of_tower
,
2864 rpcrt4_ncacn_http_parse_top_of_tower
,
2865 rpcrt4_ncacn_http_receive_fragment
,
2866 RPCRT4_default_is_authorized
,
2867 RPCRT4_default_authorize
,
2868 RPCRT4_default_secure_packet
,
2869 RPCRT4_default_impersonate_client
,
2870 RPCRT4_default_revert_to_self
,
2871 RPCRT4_default_inquire_auth_client
,
2876 static const struct protseq_ops protseq_list
[] =
2880 rpcrt4_protseq_np_alloc
,
2881 rpcrt4_protseq_np_signal_state_changed
,
2882 rpcrt4_protseq_np_get_wait_array
,
2883 rpcrt4_protseq_np_free_wait_array
,
2884 rpcrt4_protseq_np_wait_for_new_connection
,
2885 rpcrt4_protseq_ncacn_np_open_endpoint
,
2889 rpcrt4_protseq_np_alloc
,
2890 rpcrt4_protseq_np_signal_state_changed
,
2891 rpcrt4_protseq_np_get_wait_array
,
2892 rpcrt4_protseq_np_free_wait_array
,
2893 rpcrt4_protseq_np_wait_for_new_connection
,
2894 rpcrt4_protseq_ncalrpc_open_endpoint
,
2898 rpcrt4_protseq_sock_alloc
,
2899 rpcrt4_protseq_sock_signal_state_changed
,
2900 rpcrt4_protseq_sock_get_wait_array
,
2901 rpcrt4_protseq_sock_free_wait_array
,
2902 rpcrt4_protseq_sock_wait_for_new_connection
,
2903 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
2907 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2909 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
2912 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
2913 if (!strcmp(protseq_list
[i
].name
, protseq
))
2914 return &protseq_list
[i
];
2918 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
2921 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
2922 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
2923 return &conn_protseq_list
[i
];
2927 /**** interface to rest of code ****/
2929 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
2931 TRACE("(Connection == ^%p)\n", Connection
);
2933 assert(!Connection
->server
);
2934 return Connection
->ops
->open_connection_client(Connection
);
2937 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
2939 TRACE("(Connection == ^%p)\n", Connection
);
2940 if (SecIsValidHandle(&Connection
->ctx
))
2942 DeleteSecurityContext(&Connection
->ctx
);
2943 SecInvalidateHandle(&Connection
->ctx
);
2945 rpcrt4_conn_close(Connection
);
2949 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
2950 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
2951 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
2953 static LONG next_id
;
2954 const struct connection_ops
*ops
;
2955 RpcConnection
* NewConnection
;
2957 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
2960 FIXME("not supported for protseq %s\n", Protseq
);
2961 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
2964 NewConnection
= ops
->alloc();
2965 NewConnection
->Next
= NULL
;
2966 NewConnection
->server_binding
= NULL
;
2967 NewConnection
->server
= server
;
2968 NewConnection
->ops
= ops
;
2969 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
2970 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
2971 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
2972 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
2973 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
2974 NewConnection
->NextCallId
= 1;
2976 SecInvalidateHandle(&NewConnection
->ctx
);
2977 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
2978 NewConnection
->attr
= 0;
2979 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
2980 NewConnection
->AuthInfo
= AuthInfo
;
2981 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
2982 NewConnection
->encryption_auth_len
= 0;
2983 NewConnection
->signature_auth_len
= 0;
2984 if (QOS
) RpcQualityOfService_AddRef(QOS
);
2985 NewConnection
->QOS
= QOS
;
2987 list_init(&NewConnection
->conn_pool_entry
);
2988 NewConnection
->async_state
= NULL
;
2990 TRACE("connection: %p\n", NewConnection
);
2991 *Connection
= NewConnection
;
2996 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3000 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
3001 rpcrt4_conn_get_name(OldConnection
),
3002 OldConnection
->NetworkAddr
,
3003 OldConnection
->Endpoint
, NULL
,
3004 OldConnection
->AuthInfo
, OldConnection
->QOS
);
3005 if (err
== RPC_S_OK
)
3006 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3010 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
3012 TRACE("connection: %p\n", Connection
);
3014 RPCRT4_CloseConnection(Connection
);
3015 RPCRT4_strfree(Connection
->Endpoint
);
3016 RPCRT4_strfree(Connection
->NetworkAddr
);
3017 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3018 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3019 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3022 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3024 HeapFree(GetProcessHeap(), 0, Connection
);
3028 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3030 const char *protseq
,
3031 const char *networkaddr
,
3032 const char *endpoint
)
3034 twr_empty_floor_t
*protocol_floor
;
3035 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3040 return RPC_S_INVALID_RPC_PROTSEQ
;
3044 *tower_size
= sizeof(*protocol_floor
);
3045 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3049 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3050 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3051 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3052 protocol_floor
->count_rhs
= 0;
3054 tower_data
+= sizeof(*protocol_floor
);
3056 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3058 return EPT_S_NOT_REGISTERED
;
3060 *tower_size
+= sizeof(*protocol_floor
);
3065 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3071 const twr_empty_floor_t
*protocol_floor
;
3072 const twr_empty_floor_t
*floor4
;
3073 const struct connection_ops
*protseq_ops
= NULL
;
3077 if (tower_size
< sizeof(*protocol_floor
))
3078 return EPT_S_NOT_REGISTERED
;
3080 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3081 tower_data
+= sizeof(*protocol_floor
);
3082 tower_size
-= sizeof(*protocol_floor
);
3083 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3084 (protocol_floor
->count_rhs
> tower_size
))
3085 return EPT_S_NOT_REGISTERED
;
3086 tower_data
+= protocol_floor
->count_rhs
;
3087 tower_size
-= protocol_floor
->count_rhs
;
3089 floor4
= (const twr_empty_floor_t
*)tower_data
;
3090 if ((tower_size
< sizeof(*floor4
)) ||
3091 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3092 return EPT_S_NOT_REGISTERED
;
3094 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3095 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3096 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3098 protseq_ops
= &conn_protseq_list
[i
];
3103 return EPT_S_NOT_REGISTERED
;
3105 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3107 if ((status
== RPC_S_OK
) && protseq
)
3109 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3110 strcpy(*protseq
, protseq_ops
->name
);
3116 /***********************************************************************
3117 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3119 * Checks if the given protocol sequence is known by the RPC system.
3120 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3123 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3127 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3128 ps
, sizeof ps
, NULL
, NULL
);
3129 if (rpcrt4_get_conn_protseq_ops(ps
))
3132 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3134 return RPC_S_INVALID_RPC_PROTSEQ
;
3137 /***********************************************************************
3138 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3140 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3142 UNICODE_STRING protseqW
;
3144 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3146 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3147 RtlFreeUnicodeString(&protseqW
);
3150 return RPC_S_OUT_OF_MEMORY
;
3153 /***********************************************************************
3154 * RpcProtseqVectorFreeA (RPCRT4.@)
3156 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3158 TRACE("(%p)\n", protseqs
);
3163 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3164 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3165 HeapFree(GetProcessHeap(), 0, *protseqs
);
3171 /***********************************************************************
3172 * RpcProtseqVectorFreeW (RPCRT4.@)
3174 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3176 TRACE("(%p)\n", protseqs
);
3181 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3182 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3183 HeapFree(GetProcessHeap(), 0, *protseqs
);
3189 /***********************************************************************
3190 * RpcNetworkInqProtseqsW (RPCRT4.@)
3192 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3194 RPC_PROTSEQ_VECTORW
*pvector
;
3196 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3198 TRACE("(%p)\n", protseqs
);
3200 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3203 pvector
= *protseqs
;
3205 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3207 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3208 if (pvector
->Protseq
[i
] == NULL
)
3210 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3211 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3217 if (status
!= RPC_S_OK
)
3218 RpcProtseqVectorFreeW(protseqs
);
3222 /***********************************************************************
3223 * RpcNetworkInqProtseqsA (RPCRT4.@)
3225 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3227 RPC_PROTSEQ_VECTORA
*pvector
;
3229 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3231 TRACE("(%p)\n", protseqs
);
3233 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3236 pvector
= *protseqs
;
3238 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3240 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3241 if (pvector
->Protseq
[i
] == NULL
)
3243 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3249 if (status
!= RPC_S_OK
)
3250 RpcProtseqVectorFreeA(protseqs
);