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
);
106 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
108 /**** ncacn_np support ****/
110 typedef struct _RpcConnection_np
112 RpcConnection common
;
118 static RpcConnection
*rpcrt4_conn_np_alloc(void)
120 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
124 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
125 npc
->listening
= FALSE
;
130 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
135 npc
->listening
= TRUE
;
138 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
141 switch(GetLastError())
143 case ERROR_PIPE_CONNECTED
:
144 SetEvent(npc
->ovl
.hEvent
);
146 case ERROR_IO_PENDING
:
147 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
149 case ERROR_NO_DATA_DETECTED
:
150 /* client has disconnected, retry */
151 DisconnectNamedPipe( npc
->pipe
);
154 npc
->listening
= FALSE
;
155 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
156 return RPC_S_OUT_OF_RESOURCES
;
161 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
163 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
164 TRACE("listening on %s\n", pname
);
166 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
167 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
168 PIPE_UNLIMITED_INSTANCES
,
169 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
170 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
171 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
172 if (GetLastError() == ERROR_FILE_EXISTS
)
173 return RPC_S_DUPLICATE_ENDPOINT
;
175 return RPC_S_CANT_CREATE_ENDPOINT
;
178 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
179 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
181 /* Note: we don't call ConnectNamedPipe here because it must be done in the
182 * server thread as the thread must be alertable */
186 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
188 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
192 TRACE("connecting to %s\n", pname
);
198 dwFlags
= SECURITY_SQOS_PRESENT
;
199 switch (Connection
->QOS
->qos
->ImpersonationType
)
201 case RPC_C_IMP_LEVEL_DEFAULT
:
202 /* FIXME: what to do here? */
204 case RPC_C_IMP_LEVEL_ANONYMOUS
:
205 dwFlags
|= SECURITY_ANONYMOUS
;
207 case RPC_C_IMP_LEVEL_IDENTIFY
:
208 dwFlags
|= SECURITY_IDENTIFICATION
;
210 case RPC_C_IMP_LEVEL_IMPERSONATE
:
211 dwFlags
|= SECURITY_IMPERSONATION
;
213 case RPC_C_IMP_LEVEL_DELEGATE
:
214 dwFlags
|= SECURITY_DELEGATION
;
217 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
218 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
220 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
221 OPEN_EXISTING
, dwFlags
, 0);
222 if (pipe
!= INVALID_HANDLE_VALUE
) break;
223 err
= GetLastError();
224 if (err
== ERROR_PIPE_BUSY
) {
225 TRACE("connection failed, error=%x\n", err
);
226 return RPC_S_SERVER_TOO_BUSY
;
228 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
229 err
= GetLastError();
230 WARN("connection failed, error=%x\n", err
);
231 return RPC_S_SERVER_UNAVAILABLE
;
236 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
237 /* pipe is connected; change to message-read mode. */
238 dwMode
= PIPE_READMODE_MESSAGE
;
239 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
240 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
246 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
248 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
249 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
253 /* already connected? */
257 /* protseq=ncalrpc: supposed to use NT LPC ports,
258 * but we'll implement it with named pipes for now */
259 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
260 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
261 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
267 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
269 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
272 RpcConnection
*Connection
;
273 char generated_endpoint
[22];
277 static LONG lrpc_nameless_id
;
278 DWORD process_id
= GetCurrentProcessId();
279 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
280 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
281 "LRPC%08x.%08x", process_id
, id
);
282 endpoint
= generated_endpoint
;
285 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
286 endpoint
, NULL
, NULL
, NULL
);
290 /* protseq=ncalrpc: supposed to use NT LPC ports,
291 * but we'll implement it with named pipes for now */
292 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
293 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
294 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
297 EnterCriticalSection(&protseq
->cs
);
298 Connection
->Next
= protseq
->conn
;
299 protseq
->conn
= Connection
;
300 LeaveCriticalSection(&protseq
->cs
);
305 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
307 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
308 static const char prefix
[] = "\\\\.";
312 /* already connected? */
316 /* protseq=ncacn_np: named pipes */
317 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
318 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
319 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
325 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
327 static const char prefix
[] = "\\\\.";
330 RpcConnection
*Connection
;
331 char generated_endpoint
[21];
335 static LONG np_nameless_id
;
336 DWORD process_id
= GetCurrentProcessId();
337 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
338 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
339 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
340 endpoint
= generated_endpoint
;
343 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
344 endpoint
, NULL
, NULL
, NULL
);
348 /* protseq=ncacn_np: named pipes */
349 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
350 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
351 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
354 EnterCriticalSection(&protseq
->cs
);
355 Connection
->Next
= protseq
->conn
;
356 protseq
->conn
= Connection
;
357 LeaveCriticalSection(&protseq
->cs
);
362 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
364 /* because of the way named pipes work, we'll transfer the connected pipe
365 * to the child, then reopen the server binding to continue listening */
367 new_npc
->pipe
= old_npc
->pipe
;
368 new_npc
->ovl
= old_npc
->ovl
;
370 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
371 old_npc
->listening
= FALSE
;
374 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
378 static const char prefix
[] = "\\\\.";
380 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
382 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
383 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
384 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
390 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
394 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
396 TRACE("%s\n", old_conn
->Endpoint
);
398 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
400 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
401 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
402 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
408 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
409 void *buffer
, unsigned int count
)
411 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
414 unsigned int bytes_left
= count
;
419 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
420 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
422 if (!ret
|| !bytes_read
)
424 bytes_left
-= bytes_read
;
427 return ret
? count
: -1;
430 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
431 const void *buffer
, unsigned int count
)
433 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
434 const char *buf
= buffer
;
436 unsigned int bytes_left
= count
;
441 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
442 if (!ret
|| !bytes_written
)
444 bytes_left
-= bytes_written
;
445 buf
+= bytes_written
;
447 return ret
? count
: -1;
450 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
452 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
454 FlushFileBuffers(npc
->pipe
);
455 CloseHandle(npc
->pipe
);
458 if (npc
->ovl
.hEvent
) {
459 CloseHandle(npc
->ovl
.hEvent
);
465 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
467 /* FIXME: implement when named pipe writes use overlapped I/O */
470 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
472 /* FIXME: implement when named pipe writes use overlapped I/O */
476 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
477 const char *networkaddr
,
478 const char *endpoint
)
480 twr_empty_floor_t
*smb_floor
;
481 twr_empty_floor_t
*nb_floor
;
483 size_t networkaddr_size
;
484 size_t endpoint_size
;
486 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
488 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
489 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
490 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
495 smb_floor
= (twr_empty_floor_t
*)tower_data
;
497 tower_data
+= sizeof(*smb_floor
);
499 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
500 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
501 smb_floor
->count_rhs
= endpoint_size
;
504 memcpy(tower_data
, endpoint
, endpoint_size
);
507 tower_data
+= endpoint_size
;
509 nb_floor
= (twr_empty_floor_t
*)tower_data
;
511 tower_data
+= sizeof(*nb_floor
);
513 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
514 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
515 nb_floor
->count_rhs
= networkaddr_size
;
518 memcpy(tower_data
, networkaddr
, networkaddr_size
);
525 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
530 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
531 const twr_empty_floor_t
*nb_floor
;
533 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
535 if (tower_size
< sizeof(*smb_floor
))
536 return EPT_S_NOT_REGISTERED
;
538 tower_data
+= sizeof(*smb_floor
);
539 tower_size
-= sizeof(*smb_floor
);
541 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
542 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
543 (smb_floor
->count_rhs
> tower_size
) ||
544 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
545 return EPT_S_NOT_REGISTERED
;
549 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
551 return RPC_S_OUT_OF_RESOURCES
;
552 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
554 tower_data
+= smb_floor
->count_rhs
;
555 tower_size
-= smb_floor
->count_rhs
;
557 if (tower_size
< sizeof(*nb_floor
))
558 return EPT_S_NOT_REGISTERED
;
560 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
562 tower_data
+= sizeof(*nb_floor
);
563 tower_size
-= sizeof(*nb_floor
);
565 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
566 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
567 (nb_floor
->count_rhs
> tower_size
) ||
568 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
569 return EPT_S_NOT_REGISTERED
;
573 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
578 I_RpcFree(*endpoint
);
581 return RPC_S_OUT_OF_RESOURCES
;
583 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
589 typedef struct _RpcServerProtseq_np
591 RpcServerProtseq common
;
593 } RpcServerProtseq_np
;
595 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
597 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
599 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
603 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
605 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
606 SetEvent(npps
->mgr_event
);
609 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
611 HANDLE
*objs
= prev_array
;
612 RpcConnection_np
*conn
;
613 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
615 EnterCriticalSection(&protseq
->cs
);
617 /* open and count connections */
619 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
621 rpcrt4_conn_listen_pipe(conn
);
622 if (conn
->ovl
.hEvent
)
624 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
627 /* make array of connections */
629 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
631 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
634 ERR("couldn't allocate objs\n");
635 LeaveCriticalSection(&protseq
->cs
);
639 objs
[0] = npps
->mgr_event
;
641 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
643 if ((objs
[*count
] = conn
->ovl
.hEvent
))
645 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
647 LeaveCriticalSection(&protseq
->cs
);
651 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
653 HeapFree(GetProcessHeap(), 0, array
);
656 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
659 HANDLE
*objs
= wait_array
;
661 RpcConnection
*cconn
;
662 RpcConnection_np
*conn
;
669 /* an alertable wait isn't strictly necessary, but due to our
670 * overlapped I/O implementation in Wine we need to free some memory
671 * by the file user APC being called, even if no completion routine was
672 * specified at the time of starting the async operation */
673 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
674 } while (res
== WAIT_IO_COMPLETION
);
676 if (res
== WAIT_OBJECT_0
)
678 else if (res
== WAIT_FAILED
)
680 ERR("wait failed with error %d\n", GetLastError());
685 b_handle
= objs
[res
- WAIT_OBJECT_0
];
686 /* find which connection got a RPC */
687 EnterCriticalSection(&protseq
->cs
);
688 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
690 if (b_handle
== conn
->ovl
.hEvent
) break;
691 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
695 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
697 ERR("failed to locate connection for handle %p\n", b_handle
);
698 LeaveCriticalSection(&protseq
->cs
);
701 RPCRT4_new_client(cconn
);
708 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
709 const char *networkaddr
,
710 const char *endpoint
)
712 twr_empty_floor_t
*pipe_floor
;
714 size_t endpoint_size
;
716 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
718 endpoint_size
= strlen(endpoint
) + 1;
719 size
= sizeof(*pipe_floor
) + endpoint_size
;
724 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
726 tower_data
+= sizeof(*pipe_floor
);
728 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
729 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
730 pipe_floor
->count_rhs
= endpoint_size
;
732 memcpy(tower_data
, endpoint
, endpoint_size
);
737 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
742 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
744 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
746 if (tower_size
< sizeof(*pipe_floor
))
747 return EPT_S_NOT_REGISTERED
;
749 tower_data
+= sizeof(*pipe_floor
);
750 tower_size
-= sizeof(*pipe_floor
);
752 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
753 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
754 (pipe_floor
->count_rhs
> tower_size
) ||
755 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
756 return EPT_S_NOT_REGISTERED
;
763 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
765 return RPC_S_OUT_OF_RESOURCES
;
766 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
772 /**** ncacn_ip_tcp support ****/
774 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
775 const char *networkaddr
,
776 unsigned char tcp_protid
,
777 const char *endpoint
)
779 twr_tcp_floor_t
*tcp_floor
;
780 twr_ipv4_floor_t
*ipv4_floor
;
782 struct addrinfo hints
;
784 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
786 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
791 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
792 tower_data
+= sizeof(*tcp_floor
);
794 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
796 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
797 tcp_floor
->protid
= tcp_protid
;
798 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
800 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
801 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
802 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
804 hints
.ai_flags
= AI_NUMERICHOST
;
805 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
806 hints
.ai_family
= PF_INET
;
807 hints
.ai_socktype
= SOCK_STREAM
;
808 hints
.ai_protocol
= IPPROTO_TCP
;
809 hints
.ai_addrlen
= 0;
810 hints
.ai_addr
= NULL
;
811 hints
.ai_canonname
= NULL
;
812 hints
.ai_next
= NULL
;
814 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
817 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
820 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
825 if (ai
->ai_family
== PF_INET
)
827 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
828 tcp_floor
->port
= sin
->sin_port
;
829 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
833 ERR("unexpected protocol family %d\n", ai
->ai_family
);
842 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
845 unsigned char tcp_protid
,
848 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
849 const twr_ipv4_floor_t
*ipv4_floor
;
850 struct in_addr in_addr
;
852 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
854 if (tower_size
< sizeof(*tcp_floor
))
855 return EPT_S_NOT_REGISTERED
;
857 tower_data
+= sizeof(*tcp_floor
);
858 tower_size
-= sizeof(*tcp_floor
);
860 if (tower_size
< sizeof(*ipv4_floor
))
861 return EPT_S_NOT_REGISTERED
;
863 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
865 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
866 (tcp_floor
->protid
!= tcp_protid
) ||
867 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
868 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
869 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
870 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
871 return EPT_S_NOT_REGISTERED
;
875 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
877 return RPC_S_OUT_OF_RESOURCES
;
878 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
883 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
888 I_RpcFree(*endpoint
);
891 return RPC_S_OUT_OF_RESOURCES
;
893 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
894 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
896 ERR("inet_ntop: %s\n", strerror(errno
));
897 I_RpcFree(*networkaddr
);
901 I_RpcFree(*endpoint
);
904 return EPT_S_NOT_REGISTERED
;
911 typedef struct _RpcConnection_tcp
913 RpcConnection common
;
915 #ifdef HAVE_SOCKETPAIR
923 #ifdef HAVE_SOCKETPAIR
925 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
927 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
929 ERR("socketpair() failed: %s\n", strerror(errno
));
935 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
937 struct pollfd pfds
[2];
938 pfds
[0].fd
= tcpc
->sock
;
939 pfds
[0].events
= POLLIN
;
940 pfds
[1].fd
= tcpc
->cancel_fds
[0];
941 pfds
[1].events
= POLLIN
;
942 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
944 ERR("poll() failed: %s\n", strerror(errno
));
947 if (pfds
[1].revents
& POLLIN
) /* canceled */
950 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
956 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
960 pfd
.events
= POLLOUT
;
961 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
963 ERR("poll() failed: %s\n", strerror(errno
));
969 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
973 write(tcpc
->cancel_fds
[1], &dummy
, 1);
976 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
978 close(tcpc
->cancel_fds
[0]);
979 close(tcpc
->cancel_fds
[1]);
982 #else /* HAVE_SOCKETPAIR */
984 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
986 static BOOL wsa_inited
;
990 WSAStartup(MAKEWORD(2, 2), &wsadata
);
991 /* Note: WSAStartup can be called more than once so we don't bother with
992 * making accesses to wsa_inited thread-safe */
995 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
996 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
997 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
999 ERR("event creation failed\n");
1000 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1006 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1008 HANDLE wait_handles
[2];
1010 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1012 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1015 wait_handles
[0] = tcpc
->sock_event
;
1016 wait_handles
[1] = tcpc
->cancel_event
;
1017 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1022 case WAIT_OBJECT_0
+ 1:
1025 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1030 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1033 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1035 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1038 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1044 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1049 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1051 SetEvent(tcpc
->cancel_event
);
1054 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1056 CloseHandle(tcpc
->sock_event
);
1057 CloseHandle(tcpc
->cancel_event
);
1062 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1064 RpcConnection_tcp
*tcpc
;
1065 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1069 if (!rpcrt4_sock_wait_init(tcpc
))
1071 HeapFree(GetProcessHeap(), 0, tcpc
);
1074 return &tcpc
->common
;
1077 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1079 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1082 struct addrinfo
*ai
;
1083 struct addrinfo
*ai_cur
;
1084 struct addrinfo hints
;
1086 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1088 if (tcpc
->sock
!= -1)
1092 hints
.ai_family
= PF_UNSPEC
;
1093 hints
.ai_socktype
= SOCK_STREAM
;
1094 hints
.ai_protocol
= IPPROTO_TCP
;
1095 hints
.ai_addrlen
= 0;
1096 hints
.ai_addr
= NULL
;
1097 hints
.ai_canonname
= NULL
;
1098 hints
.ai_next
= NULL
;
1100 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1103 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1104 Connection
->Endpoint
, gai_strerror(ret
));
1105 return RPC_S_SERVER_UNAVAILABLE
;
1108 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1113 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1115 TRACE("skipping non-IP/IPv6 address family\n");
1123 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1124 host
, sizeof(host
), service
, sizeof(service
),
1125 NI_NUMERICHOST
| NI_NUMERICSERV
);
1126 TRACE("trying %s:%s\n", host
, service
);
1129 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1132 WARN("socket() failed: %s\n", strerror(errno
));
1136 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1138 WARN("connect() failed: %s\n", strerror(errno
));
1143 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1145 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1147 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1152 TRACE("connected\n");
1157 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1158 return RPC_S_SERVER_UNAVAILABLE
;
1161 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1163 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1166 struct addrinfo
*ai
;
1167 struct addrinfo
*ai_cur
;
1168 struct addrinfo hints
;
1169 RpcConnection
*first_connection
= NULL
;
1171 TRACE("(%p, %s)\n", protseq
, endpoint
);
1173 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1174 hints
.ai_family
= PF_UNSPEC
;
1175 hints
.ai_socktype
= SOCK_STREAM
;
1176 hints
.ai_protocol
= IPPROTO_TCP
;
1177 hints
.ai_addrlen
= 0;
1178 hints
.ai_addr
= NULL
;
1179 hints
.ai_canonname
= NULL
;
1180 hints
.ai_next
= NULL
;
1182 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1185 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1187 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1188 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1189 return RPC_S_CANT_CREATE_ENDPOINT
;
1192 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1194 RpcConnection_tcp
*tcpc
;
1195 RPC_STATUS create_status
;
1196 struct sockaddr_storage sa
;
1198 char service
[NI_MAXSERV
];
1201 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1203 TRACE("skipping non-IP/IPv6 address family\n");
1210 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1211 host
, sizeof(host
), service
, sizeof(service
),
1212 NI_NUMERICHOST
| NI_NUMERICSERV
);
1213 TRACE("trying %s:%s\n", host
, service
);
1216 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1219 WARN("socket() failed: %s\n", strerror(errno
));
1220 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1224 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1227 WARN("bind failed: %s\n", strerror(errno
));
1229 if (errno
== EADDRINUSE
)
1230 status
= RPC_S_DUPLICATE_ENDPOINT
;
1232 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1236 sa_len
= sizeof(sa
);
1237 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1239 WARN("getsockname() failed: %s\n", strerror(errno
));
1240 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1244 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1245 NULL
, 0, service
, sizeof(service
),
1249 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1250 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1254 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1255 protseq
->Protseq
, NULL
,
1256 service
, NULL
, NULL
, NULL
);
1257 if (create_status
!= RPC_S_OK
)
1260 status
= create_status
;
1265 ret
= listen(sock
, protseq
->MaxCalls
);
1268 WARN("listen failed: %s\n", strerror(errno
));
1269 RPCRT4_DestroyConnection(&tcpc
->common
);
1270 status
= RPC_S_OUT_OF_RESOURCES
;
1273 /* need a non-blocking socket, otherwise accept() has a potential
1274 * race-condition (poll() says it is readable, connection drops,
1275 * and accept() blocks until the next connection comes...)
1278 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1281 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1282 RPCRT4_DestroyConnection(&tcpc
->common
);
1283 status
= RPC_S_OUT_OF_RESOURCES
;
1287 tcpc
->common
.Next
= first_connection
;
1288 first_connection
= &tcpc
->common
;
1290 /* since IPv4 and IPv6 share the same port space, we only need one
1291 * successful bind to listen for both */
1297 /* if at least one connection was created for an endpoint then
1299 if (first_connection
)
1301 RpcConnection
*conn
;
1303 /* find last element in list */
1304 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1307 EnterCriticalSection(&protseq
->cs
);
1308 conn
->Next
= protseq
->conn
;
1309 protseq
->conn
= first_connection
;
1310 LeaveCriticalSection(&protseq
->cs
);
1312 TRACE("listening on %s\n", endpoint
);
1316 ERR("couldn't listen on port %s\n", endpoint
);
1320 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1323 struct sockaddr_in address
;
1325 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1326 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1329 addrsize
= sizeof(address
);
1330 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1333 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1334 return RPC_S_OUT_OF_RESOURCES
;
1337 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1339 TRACE("Accepted a new TCP connection\n");
1343 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1344 void *buffer
, unsigned int count
)
1346 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1350 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1355 else if (errno
!= EAGAIN
)
1357 WARN("recv() failed: %s\n", strerror(errno
));
1362 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1365 } while (bytes_read
!= count
);
1366 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1370 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1371 const void *buffer
, unsigned int count
)
1373 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1374 int bytes_written
= 0;
1377 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1380 else if (errno
!= EAGAIN
)
1384 if (!rpcrt4_sock_wait_for_send(tcpc
))
1387 } while (bytes_written
!= count
);
1388 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1389 return bytes_written
;
1392 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1394 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1396 TRACE("%d\n", tcpc
->sock
);
1398 if (tcpc
->sock
!= -1)
1399 closesocket(tcpc
->sock
);
1401 rpcrt4_sock_wait_destroy(tcpc
);
1405 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1407 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1408 TRACE("%p\n", Connection
);
1409 rpcrt4_sock_wait_cancel(tcpc
);
1412 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1414 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1416 TRACE("%p\n", Connection
);
1418 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1423 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1424 const char *networkaddr
,
1425 const char *endpoint
)
1427 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1428 EPM_PROTOCOL_TCP
, endpoint
);
1431 #ifdef HAVE_SOCKETPAIR
1433 typedef struct _RpcServerProtseq_sock
1435 RpcServerProtseq common
;
1438 } RpcServerProtseq_sock
;
1440 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1442 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1446 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1448 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1449 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1450 ps
->mgr_event_rcv
= fds
[0];
1451 ps
->mgr_event_snd
= fds
[1];
1455 ERR("socketpair failed with error %s\n", strerror(errno
));
1456 HeapFree(GetProcessHeap(), 0, ps
);
1463 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1465 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1467 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1470 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1472 struct pollfd
*poll_info
= prev_array
;
1473 RpcConnection_tcp
*conn
;
1474 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1476 EnterCriticalSection(&protseq
->cs
);
1478 /* open and count connections */
1480 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1482 if (conn
->sock
!= -1)
1484 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1487 /* make array of connections */
1489 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1491 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1494 ERR("couldn't allocate poll_info\n");
1495 LeaveCriticalSection(&protseq
->cs
);
1499 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1500 poll_info
[0].events
= POLLIN
;
1502 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1504 if (conn
->sock
!= -1)
1506 poll_info
[*count
].fd
= conn
->sock
;
1507 poll_info
[*count
].events
= POLLIN
;
1510 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1512 LeaveCriticalSection(&protseq
->cs
);
1516 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1518 HeapFree(GetProcessHeap(), 0, array
);
1521 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1523 struct pollfd
*poll_info
= wait_array
;
1526 RpcConnection
*cconn
;
1527 RpcConnection_tcp
*conn
;
1532 ret
= poll(poll_info
, count
, -1);
1535 ERR("poll failed with error %d\n", ret
);
1539 for (i
= 0; i
< count
; i
++)
1540 if (poll_info
[i
].revents
& POLLIN
)
1542 /* RPC server event */
1546 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1550 /* find which connection got a RPC */
1551 EnterCriticalSection(&protseq
->cs
);
1552 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1554 if (poll_info
[i
].fd
== conn
->sock
) break;
1555 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1559 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1561 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1562 LeaveCriticalSection(&protseq
->cs
);
1564 RPCRT4_new_client(cconn
);
1572 #else /* HAVE_SOCKETPAIR */
1574 typedef struct _RpcServerProtseq_sock
1576 RpcServerProtseq common
;
1578 } RpcServerProtseq_sock
;
1580 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1582 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1585 static BOOL wsa_inited
;
1589 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1590 /* Note: WSAStartup can be called more than once so we don't bother with
1591 * making accesses to wsa_inited thread-safe */
1594 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1599 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1601 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1602 SetEvent(sockps
->mgr_event
);
1605 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1607 HANDLE
*objs
= prev_array
;
1608 RpcConnection_tcp
*conn
;
1609 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1611 EnterCriticalSection(&protseq
->cs
);
1613 /* open and count connections */
1615 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1618 if (conn
->sock
!= -1)
1620 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1623 /* make array of connections */
1625 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1627 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1630 ERR("couldn't allocate objs\n");
1631 LeaveCriticalSection(&protseq
->cs
);
1635 objs
[0] = sockps
->mgr_event
;
1637 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1640 if (conn
->sock
!= -1)
1642 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1643 if (res
== SOCKET_ERROR
)
1644 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1647 objs
[*count
] = conn
->sock_event
;
1651 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1653 LeaveCriticalSection(&protseq
->cs
);
1657 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1659 HeapFree(GetProcessHeap(), 0, array
);
1662 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1665 HANDLE
*objs
= wait_array
;
1667 RpcConnection
*cconn
;
1668 RpcConnection_tcp
*conn
;
1675 /* an alertable wait isn't strictly necessary, but due to our
1676 * overlapped I/O implementation in Wine we need to free some memory
1677 * by the file user APC being called, even if no completion routine was
1678 * specified at the time of starting the async operation */
1679 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1680 } while (res
== WAIT_IO_COMPLETION
);
1682 if (res
== WAIT_OBJECT_0
)
1684 else if (res
== WAIT_FAILED
)
1686 ERR("wait failed with error %d\n", GetLastError());
1691 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1692 /* find which connection got a RPC */
1693 EnterCriticalSection(&protseq
->cs
);
1694 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1697 if (b_handle
== conn
->sock_event
) break;
1698 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1702 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1704 ERR("failed to locate connection for handle %p\n", b_handle
);
1705 LeaveCriticalSection(&protseq
->cs
);
1708 RPCRT4_new_client(cconn
);
1715 #endif /* HAVE_SOCKETPAIR */
1717 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1722 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1723 networkaddr
, EPM_PROTOCOL_TCP
,
1727 /**** ncacn_http support ****/
1729 /* 60 seconds is the period native uses */
1730 #define HTTP_IDLE_TIME 60000
1732 /* reference counted to avoid a race between a cancelled call's connection
1733 * being destroyed and the asynchronous InternetReadFileEx call being
1735 typedef struct _RpcHttpAsyncData
1738 HANDLE completion_event
;
1739 INTERNET_BUFFERSA inet_buffers
;
1740 void *destination_buffer
; /* the address that inet_buffers.lpvBuffer will be
1741 * copied into when the call completes */
1742 CRITICAL_SECTION cs
;
1745 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1747 return InterlockedIncrement(&data
->refs
);
1750 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1752 ULONG refs
= InterlockedDecrement(&data
->refs
);
1755 TRACE("destroying async data %p\n", data
);
1756 CloseHandle(data
->completion_event
);
1757 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1758 DeleteCriticalSection(&data
->cs
);
1759 HeapFree(GetProcessHeap(), 0, data
);
1764 typedef struct _RpcConnection_http
1766 RpcConnection common
;
1769 HINTERNET in_request
;
1770 HINTERNET out_request
;
1771 HANDLE timer_cancelled
;
1772 HANDLE cancel_event
;
1773 DWORD last_sent_time
;
1774 ULONG bytes_received
;
1775 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1776 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1777 UUID connection_uuid
;
1780 RpcHttpAsyncData
*async_data
;
1781 } RpcConnection_http
;
1783 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1785 RpcConnection_http
*httpc
;
1786 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1787 if (!httpc
) return NULL
;
1788 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1789 if (!httpc
->async_data
)
1791 HeapFree(GetProcessHeap(), 0, httpc
);
1794 TRACE("async data = %p\n", httpc
->async_data
);
1795 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1796 httpc
->async_data
->refs
= 1;
1797 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1798 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1799 httpc
->async_data
->destination_buffer
= NULL
;
1800 InitializeCriticalSection(&httpc
->async_data
->cs
);
1801 return &httpc
->common
;
1804 typedef struct _HttpTimerThreadData
1807 DWORD
*last_sent_time
;
1808 HANDLE timer_cancelled
;
1809 } HttpTimerThreadData
;
1811 static VOID CALLBACK
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1813 HINTERNET in_request
= param
;
1814 RpcPktHdr
*idle_pkt
;
1816 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1820 DWORD bytes_written
;
1821 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1822 RPCRT4_FreeHeader(idle_pkt
);
1826 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1828 DWORD cur_time
= GetTickCount();
1829 DWORD cached_last_sent_time
= *last_sent_time
;
1830 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1833 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1835 HttpTimerThreadData
*data_in
= param
;
1836 HttpTimerThreadData data
;
1840 HeapFree(GetProcessHeap(), 0, data_in
);
1842 for (timeout
= HTTP_IDLE_TIME
;
1843 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1844 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1846 /* are we too soon after last send? */
1847 if (GetTickCount() - HTTP_IDLE_TIME
< *data
.last_sent_time
)
1849 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
1852 CloseHandle(data
.timer_cancelled
);
1856 static VOID WINAPI
rpcrt4_http_internet_callback(
1857 HINTERNET hInternet
,
1858 DWORD_PTR dwContext
,
1859 DWORD dwInternetStatus
,
1860 LPVOID lpvStatusInformation
,
1861 DWORD dwStatusInformationLength
)
1863 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
1865 switch (dwInternetStatus
)
1867 case INTERNET_STATUS_REQUEST_COMPLETE
:
1868 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1871 if (async_data
->inet_buffers
.lpvBuffer
)
1873 EnterCriticalSection(&async_data
->cs
);
1874 if (async_data
->destination_buffer
)
1876 memcpy(async_data
->destination_buffer
,
1877 async_data
->inet_buffers
.lpvBuffer
,
1878 async_data
->inet_buffers
.dwBufferLength
);
1879 async_data
->destination_buffer
= NULL
;
1881 LeaveCriticalSection(&async_data
->cs
);
1883 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
1884 async_data
->inet_buffers
.lpvBuffer
= NULL
;
1885 SetEvent(async_data
->completion_event
);
1886 RpcHttpAsyncData_Release(async_data
);
1892 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
1899 WCHAR
*status_text
= buf
;
1903 size
= sizeof(status_code
);
1904 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
1906 return GetLastError();
1907 if (status_code
< 400)
1911 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1912 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1914 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
1915 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1918 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
1919 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
1921 if (status_code
== HTTP_STATUS_DENIED
)
1922 return ERROR_ACCESS_DENIED
;
1923 return RPC_S_SERVER_UNAVAILABLE
;
1926 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
1928 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
1929 LPWSTR proxy
= NULL
;
1931 LPWSTR password
= NULL
;
1932 LPWSTR servername
= NULL
;
1933 const WCHAR
*option
;
1934 INTERNET_PORT port
= INTERNET_INVALID_PORT_NUMBER
; /* use default port */
1936 if (httpc
->common
.QOS
&&
1937 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
1939 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
1940 if (http_cred
->TransportCredentials
)
1943 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
1944 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
1945 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1947 return RPC_S_OUT_OF_RESOURCES
;
1949 if (cred
->DomainLength
)
1951 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
1952 p
+= cred
->DomainLength
;
1956 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
1957 p
[cred
->UserLength
] = 0;
1959 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
1963 for (option
= httpc
->common
.NetworkOptions
; option
;
1964 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
1966 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
1967 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
1969 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
1971 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
1972 const WCHAR
*value_end
;
1975 value_end
= strchrW(option
, ',');
1977 value_end
= value_start
+ strlenW(value_start
);
1978 for (p
= value_start
; p
< value_end
; p
++)
1985 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
1986 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
1988 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
1990 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
1991 const WCHAR
*value_end
;
1993 value_end
= strchrW(option
, ',');
1995 value_end
= value_start
+ strlenW(value_start
);
1996 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
1997 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2000 FIXME("unhandled option %s\n", debugstr_w(option
));
2003 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2004 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2005 if (!httpc
->app_info
)
2007 HeapFree(GetProcessHeap(), 0, password
);
2008 HeapFree(GetProcessHeap(), 0, user
);
2009 ERR("InternetOpenW failed with error %d\n", GetLastError());
2010 return RPC_S_SERVER_UNAVAILABLE
;
2012 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2014 /* if no RpcProxy option specified, set the HTTP server address to the
2015 * RPC server address */
2018 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2021 HeapFree(GetProcessHeap(), 0, password
);
2022 HeapFree(GetProcessHeap(), 0, user
);
2023 return RPC_S_OUT_OF_RESOURCES
;
2025 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2028 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2029 INTERNET_SERVICE_HTTP
, 0, 0);
2031 HeapFree(GetProcessHeap(), 0, password
);
2032 HeapFree(GetProcessHeap(), 0, user
);
2033 HeapFree(GetProcessHeap(), 0, servername
);
2035 if (!httpc
->session
)
2037 ERR("InternetConnectW failed with error %d\n", GetLastError());
2038 return RPC_S_SERVER_UNAVAILABLE
;
2044 /* prepare the in pipe for use by RPC packets */
2045 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
,
2046 const UUID
*connection_uuid
,
2047 const UUID
*in_pipe_uuid
,
2048 const UUID
*association_uuid
)
2054 INTERNET_BUFFERSW buffers_in
;
2055 DWORD bytes_read
, bytes_written
;
2057 /* prepare in pipe */
2058 ResetEvent(async_data
->completion_event
);
2059 RpcHttpAsyncData_AddRef(async_data
);
2060 ret
= HttpSendRequestW(in_request
, NULL
, 0, NULL
, 0);
2063 if (GetLastError() == ERROR_IO_PENDING
)
2064 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2067 RpcHttpAsyncData_Release(async_data
);
2068 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2069 return RPC_S_SERVER_UNAVAILABLE
;
2072 status
= rpcrt4_http_check_response(in_request
);
2073 if (status
!= RPC_S_OK
) return status
;
2075 InternetReadFile(in_request
, packet
, 20, &bytes_read
);
2076 /* FIXME: do something with retrieved data */
2078 memset(&buffers_in
, 0, sizeof(buffers_in
));
2079 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2080 /* FIXME: get this from the registry */
2081 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2082 ResetEvent(async_data
->completion_event
);
2083 RpcHttpAsyncData_AddRef(async_data
);
2084 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2087 if (GetLastError() == ERROR_IO_PENDING
)
2088 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2091 RpcHttpAsyncData_Release(async_data
);
2092 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
2093 return RPC_S_SERVER_UNAVAILABLE
;
2097 TRACE("sending HTTP connect header to server\n");
2098 hdr
= RPCRT4_BuildHttpConnectHeader(0, FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2099 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2100 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2101 RPCRT4_FreeHeader(hdr
);
2104 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2105 return RPC_S_SERVER_UNAVAILABLE
;
2111 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2115 unsigned short data_len
;
2117 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2119 return RPC_S_SERVER_UNAVAILABLE
;
2120 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2122 ERR("wrong packet type received %d or wrong frag_len %d\n",
2123 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2124 return RPC_S_PROTOCOL_ERROR
;
2127 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2129 return RPC_S_SERVER_UNAVAILABLE
;
2131 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2134 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2136 return RPC_S_OUT_OF_RESOURCES
;
2137 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2140 HeapFree(GetProcessHeap(), 0, *data
);
2141 return RPC_S_SERVER_UNAVAILABLE
;
2147 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2149 ERR("invalid http packet\n");
2150 return RPC_S_PROTOCOL_ERROR
;
2156 /* prepare the out pipe for use by RPC packets */
2157 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
,
2158 RpcHttpAsyncData
*async_data
,
2159 const UUID
*connection_uuid
,
2160 const UUID
*out_pipe_uuid
,
2161 ULONG
*flow_control_increment
)
2168 BYTE
*data_from_server
;
2169 RpcPktHdr pkt_from_server
;
2170 ULONG field1
, field3
;
2172 ResetEvent(async_data
->completion_event
);
2173 RpcHttpAsyncData_AddRef(async_data
);
2174 ret
= HttpSendRequestW(out_request
, NULL
, 0, NULL
, 0);
2177 if (GetLastError() == ERROR_IO_PENDING
)
2178 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2181 RpcHttpAsyncData_Release(async_data
);
2182 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2183 return RPC_S_SERVER_UNAVAILABLE
;
2186 status
= rpcrt4_http_check_response(out_request
);
2187 if (status
!= RPC_S_OK
) return status
;
2189 InternetReadFile(out_request
, packet
, 20, &bytes_read
);
2190 /* FIXME: do something with retrieved data */
2192 hdr
= RPCRT4_BuildHttpConnectHeader(0, TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2193 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2194 ResetEvent(async_data
->completion_event
);
2195 RpcHttpAsyncData_AddRef(async_data
);
2196 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2199 if (GetLastError() == ERROR_IO_PENDING
)
2200 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2203 RpcHttpAsyncData_Release(async_data
);
2204 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2205 RPCRT4_FreeHeader(hdr
);
2206 return RPC_S_SERVER_UNAVAILABLE
;
2209 RPCRT4_FreeHeader(hdr
);
2210 status
= rpcrt4_http_check_response(out_request
);
2211 if (status
!= RPC_S_OK
) return status
;
2213 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2215 if (status
!= RPC_S_OK
) return status
;
2216 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2218 HeapFree(GetProcessHeap(), 0, data_from_server
);
2219 if (status
!= RPC_S_OK
) return status
;
2220 TRACE("received (%d) from first prepare header\n", field1
);
2222 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2224 if (status
!= RPC_S_OK
) return status
;
2225 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2226 &field1
, flow_control_increment
,
2228 HeapFree(GetProcessHeap(), 0, data_from_server
);
2229 if (status
!= RPC_S_OK
) return status
;
2230 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2235 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2237 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2238 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2239 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2240 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2241 static const WCHAR wszColon
[] = {':',0};
2242 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2243 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2247 HttpTimerThreadData
*timer_data
;
2250 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2252 if (Connection
->server
)
2254 ERR("ncacn_http servers not supported yet\n");
2255 return RPC_S_SERVER_UNAVAILABLE
;
2258 if (httpc
->in_request
)
2261 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2263 status
= UuidCreate(&httpc
->connection_uuid
);
2264 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2265 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2267 status
= rpcrt4_http_internet_connect(httpc
);
2268 if (status
!= RPC_S_OK
)
2271 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2273 return RPC_S_OUT_OF_MEMORY
;
2274 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2275 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2276 strcatW(url
, wszColon
);
2277 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2279 secure
= httpc
->common
.QOS
&&
2280 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2281 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2283 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
,
2285 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2286 (DWORD_PTR
)httpc
->async_data
);
2287 if (!httpc
->in_request
)
2289 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2290 return RPC_S_SERVER_UNAVAILABLE
;
2292 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
,
2294 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2295 (DWORD_PTR
)httpc
->async_data
);
2296 if (!httpc
->out_request
)
2298 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2299 return RPC_S_SERVER_UNAVAILABLE
;
2302 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
,
2304 &httpc
->connection_uuid
,
2305 &httpc
->in_pipe_uuid
,
2306 &Connection
->assoc
->http_uuid
);
2307 if (status
!= RPC_S_OK
)
2310 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
,
2312 &httpc
->connection_uuid
,
2313 &httpc
->out_pipe_uuid
,
2314 &httpc
->flow_control_increment
);
2315 if (status
!= RPC_S_OK
)
2318 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2319 httpc
->last_sent_time
= GetTickCount();
2320 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2322 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2324 return ERROR_OUTOFMEMORY
;
2325 timer_data
->timer_param
= httpc
->in_request
;
2326 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2327 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2328 /* FIXME: should use CreateTimerQueueTimer when implemented */
2329 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2332 HeapFree(GetProcessHeap(), 0, timer_data
);
2333 return GetLastError();
2335 CloseHandle(thread
);
2340 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2343 return RPC_S_SERVER_UNAVAILABLE
;
2346 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2347 void *buffer
, unsigned int count
)
2349 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2352 unsigned int bytes_left
= count
;
2354 ResetEvent(httpc
->async_data
->completion_event
);
2357 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2358 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2359 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, bytes_left
);
2360 httpc
->async_data
->destination_buffer
= buf
;
2361 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
2364 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2366 RpcHttpAsyncData_Release(httpc
->async_data
);
2367 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
2368 httpc
->async_data
->inet_buffers
.dwBufferLength
);
2369 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2370 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2371 httpc
->async_data
->destination_buffer
= NULL
;
2375 if (GetLastError() == ERROR_IO_PENDING
)
2377 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2378 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2379 if (result
== WAIT_OBJECT_0
)
2383 TRACE("call cancelled\n");
2384 EnterCriticalSection(&httpc
->async_data
->cs
);
2385 httpc
->async_data
->destination_buffer
= NULL
;
2386 LeaveCriticalSection(&httpc
->async_data
->cs
);
2392 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2393 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2394 httpc
->async_data
->destination_buffer
= NULL
;
2395 RpcHttpAsyncData_Release(httpc
->async_data
);
2399 if (!httpc
->async_data
->inet_buffers
.dwBufferLength
)
2401 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2402 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2404 TRACE("%p %p %u -> %s\n", httpc
->out_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2405 return ret
? count
: -1;
2408 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2410 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2414 RpcPktCommonHdr common_hdr
;
2418 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2421 /* read packet common header */
2422 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2423 if (dwRead
!= sizeof(common_hdr
)) {
2424 WARN("Short read of header, %d bytes\n", dwRead
);
2425 status
= RPC_S_PROTOCOL_ERROR
;
2428 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2429 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2431 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2432 status
= RPC_S_PROTOCOL_ERROR
;
2436 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2437 if (status
!= RPC_S_OK
) goto fail
;
2439 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2440 if (hdr_length
== 0) {
2441 WARN("header length == 0\n");
2442 status
= RPC_S_PROTOCOL_ERROR
;
2446 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2449 status
= RPC_S_OUT_OF_RESOURCES
;
2452 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2454 /* read the rest of packet header */
2455 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2456 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2457 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2458 status
= RPC_S_PROTOCOL_ERROR
;
2462 if (common_hdr
.frag_len
- hdr_length
)
2464 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2467 status
= RPC_S_OUT_OF_RESOURCES
;
2471 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2472 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2474 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2475 status
= RPC_S_PROTOCOL_ERROR
;
2482 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2484 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2486 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2487 status
= RPC_S_PROTOCOL_ERROR
;
2490 if ((*Header
)->http
.flags
== 0x0001)
2492 TRACE("http idle packet, waiting for real packet\n");
2493 if ((*Header
)->http
.num_data_items
!= 0)
2495 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2496 status
= RPC_S_PROTOCOL_ERROR
;
2500 else if ((*Header
)->http
.flags
== 0x0002)
2502 ULONG bytes_transmitted
;
2503 ULONG flow_control_increment
;
2505 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2508 &flow_control_increment
,
2510 if (status
!= RPC_S_OK
)
2512 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2513 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2514 /* FIXME: do something with parsed data */
2518 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
2519 status
= RPC_S_PROTOCOL_ERROR
;
2522 RPCRT4_FreeHeader(*Header
);
2524 HeapFree(GetProcessHeap(), 0, *Payload
);
2532 httpc
->bytes_received
+= common_hdr
.frag_len
;
2534 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
2536 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
2538 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
2539 httpc
->bytes_received
,
2540 httpc
->flow_control_increment
,
2541 &httpc
->out_pipe_uuid
);
2544 DWORD bytes_written
;
2546 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
2547 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2548 RPCRT4_FreeHeader(hdr
);
2550 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
2555 if (status
!= RPC_S_OK
) {
2556 RPCRT4_FreeHeader(*Header
);
2558 HeapFree(GetProcessHeap(), 0, *Payload
);
2564 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
2565 const void *buffer
, unsigned int count
)
2567 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2568 DWORD bytes_written
;
2571 httpc
->last_sent_time
= ~0UL; /* disable idle packet sending */
2572 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
2573 httpc
->last_sent_time
= GetTickCount();
2574 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2575 return ret
? bytes_written
: -1;
2578 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
2580 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2584 SetEvent(httpc
->timer_cancelled
);
2585 if (httpc
->in_request
)
2586 InternetCloseHandle(httpc
->in_request
);
2587 httpc
->in_request
= NULL
;
2588 if (httpc
->out_request
)
2589 InternetCloseHandle(httpc
->out_request
);
2590 httpc
->out_request
= NULL
;
2591 if (httpc
->app_info
)
2592 InternetCloseHandle(httpc
->app_info
);
2593 httpc
->app_info
= NULL
;
2595 InternetCloseHandle(httpc
->session
);
2596 httpc
->session
= NULL
;
2597 RpcHttpAsyncData_Release(httpc
->async_data
);
2598 if (httpc
->cancel_event
)
2599 CloseHandle(httpc
->cancel_event
);
2604 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
2606 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2608 SetEvent(httpc
->cancel_event
);
2611 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
2614 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2616 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2617 ret
= InternetQueryDataAvailable(httpc
->out_request
,
2618 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
2621 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2623 RpcHttpAsyncData_Release(httpc
->async_data
);
2627 if (GetLastError() == ERROR_IO_PENDING
)
2629 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2630 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2631 if (result
!= WAIT_OBJECT_0
)
2633 TRACE("call cancelled\n");
2639 RpcHttpAsyncData_Release(httpc
->async_data
);
2648 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
2649 const char *networkaddr
,
2650 const char *endpoint
)
2652 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
2653 EPM_PROTOCOL_HTTP
, endpoint
);
2656 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
2661 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
2662 networkaddr
, EPM_PROTOCOL_HTTP
,
2666 static const struct connection_ops conn_protseq_list
[] = {
2668 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
2669 rpcrt4_conn_np_alloc
,
2670 rpcrt4_ncacn_np_open
,
2671 rpcrt4_ncacn_np_handoff
,
2672 rpcrt4_conn_np_read
,
2673 rpcrt4_conn_np_write
,
2674 rpcrt4_conn_np_close
,
2675 rpcrt4_conn_np_cancel_call
,
2676 rpcrt4_conn_np_wait_for_incoming_data
,
2677 rpcrt4_ncacn_np_get_top_of_tower
,
2678 rpcrt4_ncacn_np_parse_top_of_tower
,
2682 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
2683 rpcrt4_conn_np_alloc
,
2684 rpcrt4_ncalrpc_open
,
2685 rpcrt4_ncalrpc_handoff
,
2686 rpcrt4_conn_np_read
,
2687 rpcrt4_conn_np_write
,
2688 rpcrt4_conn_np_close
,
2689 rpcrt4_conn_np_cancel_call
,
2690 rpcrt4_conn_np_wait_for_incoming_data
,
2691 rpcrt4_ncalrpc_get_top_of_tower
,
2692 rpcrt4_ncalrpc_parse_top_of_tower
,
2696 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
2697 rpcrt4_conn_tcp_alloc
,
2698 rpcrt4_ncacn_ip_tcp_open
,
2699 rpcrt4_conn_tcp_handoff
,
2700 rpcrt4_conn_tcp_read
,
2701 rpcrt4_conn_tcp_write
,
2702 rpcrt4_conn_tcp_close
,
2703 rpcrt4_conn_tcp_cancel_call
,
2704 rpcrt4_conn_tcp_wait_for_incoming_data
,
2705 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
2706 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
2710 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
2711 rpcrt4_ncacn_http_alloc
,
2712 rpcrt4_ncacn_http_open
,
2713 rpcrt4_ncacn_http_handoff
,
2714 rpcrt4_ncacn_http_read
,
2715 rpcrt4_ncacn_http_write
,
2716 rpcrt4_ncacn_http_close
,
2717 rpcrt4_ncacn_http_cancel_call
,
2718 rpcrt4_ncacn_http_wait_for_incoming_data
,
2719 rpcrt4_ncacn_http_get_top_of_tower
,
2720 rpcrt4_ncacn_http_parse_top_of_tower
,
2721 rpcrt4_ncacn_http_receive_fragment
,
2726 static const struct protseq_ops protseq_list
[] =
2730 rpcrt4_protseq_np_alloc
,
2731 rpcrt4_protseq_np_signal_state_changed
,
2732 rpcrt4_protseq_np_get_wait_array
,
2733 rpcrt4_protseq_np_free_wait_array
,
2734 rpcrt4_protseq_np_wait_for_new_connection
,
2735 rpcrt4_protseq_ncacn_np_open_endpoint
,
2739 rpcrt4_protseq_np_alloc
,
2740 rpcrt4_protseq_np_signal_state_changed
,
2741 rpcrt4_protseq_np_get_wait_array
,
2742 rpcrt4_protseq_np_free_wait_array
,
2743 rpcrt4_protseq_np_wait_for_new_connection
,
2744 rpcrt4_protseq_ncalrpc_open_endpoint
,
2748 rpcrt4_protseq_sock_alloc
,
2749 rpcrt4_protseq_sock_signal_state_changed
,
2750 rpcrt4_protseq_sock_get_wait_array
,
2751 rpcrt4_protseq_sock_free_wait_array
,
2752 rpcrt4_protseq_sock_wait_for_new_connection
,
2753 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
2757 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2759 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
2762 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
2763 if (!strcmp(protseq_list
[i
].name
, protseq
))
2764 return &protseq_list
[i
];
2768 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
2771 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
2772 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
2773 return &conn_protseq_list
[i
];
2777 /**** interface to rest of code ****/
2779 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
2781 TRACE("(Connection == ^%p)\n", Connection
);
2783 assert(!Connection
->server
);
2784 return Connection
->ops
->open_connection_client(Connection
);
2787 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
2789 TRACE("(Connection == ^%p)\n", Connection
);
2790 if (SecIsValidHandle(&Connection
->ctx
))
2792 DeleteSecurityContext(&Connection
->ctx
);
2793 SecInvalidateHandle(&Connection
->ctx
);
2795 rpcrt4_conn_close(Connection
);
2799 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
2800 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
2801 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
2803 const struct connection_ops
*ops
;
2804 RpcConnection
* NewConnection
;
2806 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
2809 FIXME("not supported for protseq %s\n", Protseq
);
2810 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
2813 NewConnection
= ops
->alloc();
2814 NewConnection
->Next
= NULL
;
2815 NewConnection
->server_binding
= NULL
;
2816 NewConnection
->server
= server
;
2817 NewConnection
->ops
= ops
;
2818 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
2819 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
2820 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
2821 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
2822 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
2823 NewConnection
->NextCallId
= 1;
2825 SecInvalidateHandle(&NewConnection
->ctx
);
2826 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
2827 NewConnection
->attr
= 0;
2828 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
2829 NewConnection
->AuthInfo
= AuthInfo
;
2830 NewConnection
->encryption_auth_len
= 0;
2831 NewConnection
->signature_auth_len
= 0;
2832 if (QOS
) RpcQualityOfService_AddRef(QOS
);
2833 NewConnection
->QOS
= QOS
;
2835 list_init(&NewConnection
->conn_pool_entry
);
2836 NewConnection
->async_state
= NULL
;
2838 TRACE("connection: %p\n", NewConnection
);
2839 *Connection
= NewConnection
;
2844 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
2848 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
2849 rpcrt4_conn_get_name(OldConnection
),
2850 OldConnection
->NetworkAddr
,
2851 OldConnection
->Endpoint
, NULL
,
2852 OldConnection
->AuthInfo
, OldConnection
->QOS
);
2853 if (err
== RPC_S_OK
)
2854 rpcrt4_conn_handoff(OldConnection
, *Connection
);
2858 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
2860 TRACE("connection: %p\n", Connection
);
2862 RPCRT4_CloseConnection(Connection
);
2863 RPCRT4_strfree(Connection
->Endpoint
);
2864 RPCRT4_strfree(Connection
->NetworkAddr
);
2865 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
2866 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
2867 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
2870 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
2872 HeapFree(GetProcessHeap(), 0, Connection
);
2876 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
2878 const char *protseq
,
2879 const char *networkaddr
,
2880 const char *endpoint
)
2882 twr_empty_floor_t
*protocol_floor
;
2883 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
2888 return RPC_S_INVALID_RPC_PROTSEQ
;
2892 *tower_size
= sizeof(*protocol_floor
);
2893 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
2897 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
2898 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
2899 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
2900 protocol_floor
->count_rhs
= 0;
2902 tower_data
+= sizeof(*protocol_floor
);
2904 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
2906 return EPT_S_NOT_REGISTERED
;
2908 *tower_size
+= sizeof(*protocol_floor
);
2913 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
2919 const twr_empty_floor_t
*protocol_floor
;
2920 const twr_empty_floor_t
*floor4
;
2921 const struct connection_ops
*protseq_ops
= NULL
;
2925 if (tower_size
< sizeof(*protocol_floor
))
2926 return EPT_S_NOT_REGISTERED
;
2928 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
2929 tower_data
+= sizeof(*protocol_floor
);
2930 tower_size
-= sizeof(*protocol_floor
);
2931 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
2932 (protocol_floor
->count_rhs
> tower_size
))
2933 return EPT_S_NOT_REGISTERED
;
2934 tower_data
+= protocol_floor
->count_rhs
;
2935 tower_size
-= protocol_floor
->count_rhs
;
2937 floor4
= (const twr_empty_floor_t
*)tower_data
;
2938 if ((tower_size
< sizeof(*floor4
)) ||
2939 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
2940 return EPT_S_NOT_REGISTERED
;
2942 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
2943 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
2944 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
2946 protseq_ops
= &conn_protseq_list
[i
];
2951 return EPT_S_NOT_REGISTERED
;
2953 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
2955 if ((status
== RPC_S_OK
) && protseq
)
2957 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
2958 strcpy(*protseq
, protseq_ops
->name
);
2964 /***********************************************************************
2965 * RpcNetworkIsProtseqValidW (RPCRT4.@)
2967 * Checks if the given protocol sequence is known by the RPC system.
2968 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
2971 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
2975 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
2976 ps
, sizeof ps
, NULL
, NULL
);
2977 if (rpcrt4_get_conn_protseq_ops(ps
))
2980 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
2982 return RPC_S_INVALID_RPC_PROTSEQ
;
2985 /***********************************************************************
2986 * RpcNetworkIsProtseqValidA (RPCRT4.@)
2988 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
2990 UNICODE_STRING protseqW
;
2992 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
2994 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
2995 RtlFreeUnicodeString(&protseqW
);
2998 return RPC_S_OUT_OF_MEMORY
;