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
[0]))
141 switch(GetLastError())
143 case ERROR_PIPE_CONNECTED
:
144 SetEvent(npc
->ovl
[0].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
| FILE_FLAG_OVERLAPPED
,
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
[0].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
180 npc
->ovl
[1].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
182 /* Note: we don't call ConnectNamedPipe here because it must be done in the
183 * server thread as the thread must be alertable */
187 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
189 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
193 TRACE("connecting to %s\n", pname
);
199 dwFlags
= SECURITY_SQOS_PRESENT
;
200 switch (Connection
->QOS
->qos
->ImpersonationType
)
202 case RPC_C_IMP_LEVEL_DEFAULT
:
203 /* FIXME: what to do here? */
205 case RPC_C_IMP_LEVEL_ANONYMOUS
:
206 dwFlags
|= SECURITY_ANONYMOUS
;
208 case RPC_C_IMP_LEVEL_IDENTIFY
:
209 dwFlags
|= SECURITY_IDENTIFICATION
;
211 case RPC_C_IMP_LEVEL_IMPERSONATE
:
212 dwFlags
|= SECURITY_IMPERSONATION
;
214 case RPC_C_IMP_LEVEL_DELEGATE
:
215 dwFlags
|= SECURITY_DELEGATION
;
218 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
219 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
221 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
222 OPEN_EXISTING
, dwFlags
, 0);
223 if (pipe
!= INVALID_HANDLE_VALUE
) break;
224 err
= GetLastError();
225 if (err
== ERROR_PIPE_BUSY
) {
226 TRACE("connection failed, error=%x\n", err
);
227 return RPC_S_SERVER_TOO_BUSY
;
229 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
230 err
= GetLastError();
231 WARN("connection failed, error=%x\n", err
);
232 return RPC_S_SERVER_UNAVAILABLE
;
237 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
238 /* pipe is connected; change to message-read mode. */
239 dwMode
= PIPE_READMODE_MESSAGE
;
240 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
241 npc
->ovl
[0].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
242 npc
->ovl
[1].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
248 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
250 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
251 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
255 /* already connected? */
259 /* protseq=ncalrpc: supposed to use NT LPC ports,
260 * but we'll implement it with named pipes for now */
261 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
262 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
263 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
269 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
271 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
274 RpcConnection
*Connection
;
275 char generated_endpoint
[22];
279 static LONG lrpc_nameless_id
;
280 DWORD process_id
= GetCurrentProcessId();
281 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
282 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
283 "LRPC%08x.%08x", process_id
, id
);
284 endpoint
= generated_endpoint
;
287 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
288 endpoint
, NULL
, NULL
, NULL
);
292 /* protseq=ncalrpc: supposed to use NT LPC ports,
293 * but we'll implement it with named pipes for now */
294 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
295 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
296 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
299 EnterCriticalSection(&protseq
->cs
);
300 Connection
->Next
= protseq
->conn
;
301 protseq
->conn
= Connection
;
302 LeaveCriticalSection(&protseq
->cs
);
307 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
309 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
310 static const char prefix
[] = "\\\\.";
314 /* already connected? */
318 /* protseq=ncacn_np: named pipes */
319 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
320 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
321 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
327 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
329 static const char prefix
[] = "\\\\.";
332 RpcConnection
*Connection
;
333 char generated_endpoint
[21];
337 static LONG np_nameless_id
;
338 DWORD process_id
= GetCurrentProcessId();
339 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
340 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
341 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
342 endpoint
= generated_endpoint
;
345 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
346 endpoint
, NULL
, NULL
, NULL
);
350 /* protseq=ncacn_np: named pipes */
351 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
352 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
353 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
356 EnterCriticalSection(&protseq
->cs
);
357 Connection
->Next
= protseq
->conn
;
358 protseq
->conn
= Connection
;
359 LeaveCriticalSection(&protseq
->cs
);
364 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
366 /* because of the way named pipes work, we'll transfer the connected pipe
367 * to the child, then reopen the server binding to continue listening */
369 new_npc
->pipe
= old_npc
->pipe
;
370 new_npc
->ovl
[0] = old_npc
->ovl
[0];
371 new_npc
->ovl
[1] = old_npc
->ovl
[1];
373 memset(&old_npc
->ovl
[0], 0, sizeof(old_npc
->ovl
));
374 old_npc
->listening
= FALSE
;
377 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
381 static const char prefix
[] = "\\\\.";
383 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
385 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
386 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
387 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
393 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
397 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
399 TRACE("%s\n", old_conn
->Endpoint
);
401 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
403 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
404 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
405 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
411 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
412 void *buffer
, unsigned int count
)
414 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
417 unsigned int bytes_left
= count
;
422 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &npc
->ovl
[0]);
423 if ((!ret
|| !bytes_read
) && (GetLastError() != ERROR_IO_PENDING
))
425 ret
= GetOverlappedResult(npc
->pipe
, &npc
->ovl
[0], &bytes_read
, TRUE
);
426 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
428 if (!ret
|| !bytes_read
)
430 bytes_left
-= bytes_read
;
433 return ret
? count
: -1;
436 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
437 const void *buffer
, unsigned int count
)
439 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
440 const char *buf
= buffer
;
442 unsigned int bytes_left
= count
;
447 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &npc
->ovl
[1]);
448 if ((!ret
|| !bytes_written
) && (GetLastError() != ERROR_IO_PENDING
))
450 ret
= GetOverlappedResult(npc
->pipe
, &npc
->ovl
[1], &bytes_written
, TRUE
);
451 if (!ret
&& GetLastError() == ERROR_MORE_DATA
)
453 if (!ret
|| !bytes_written
)
455 bytes_left
-= bytes_written
;
456 buf
+= bytes_written
;
458 return ret
? count
: -1;
461 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
463 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
465 FlushFileBuffers(npc
->pipe
);
466 CloseHandle(npc
->pipe
);
469 if (npc
->ovl
[0].hEvent
) {
470 CloseHandle(npc
->ovl
[0].hEvent
);
471 npc
->ovl
[0].hEvent
= 0;
473 if (npc
->ovl
[1].hEvent
) {
474 CloseHandle(npc
->ovl
[1].hEvent
);
475 npc
->ovl
[1].hEvent
= 0;
480 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
482 /* FIXME: implement when named pipe writes use overlapped I/O */
485 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
487 /* FIXME: implement when named pipe writes use overlapped I/O */
491 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
492 const char *networkaddr
,
493 const char *endpoint
)
495 twr_empty_floor_t
*smb_floor
;
496 twr_empty_floor_t
*nb_floor
;
498 size_t networkaddr_size
;
499 size_t endpoint_size
;
501 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
503 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
504 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
505 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
510 smb_floor
= (twr_empty_floor_t
*)tower_data
;
512 tower_data
+= sizeof(*smb_floor
);
514 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
515 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
516 smb_floor
->count_rhs
= endpoint_size
;
519 memcpy(tower_data
, endpoint
, endpoint_size
);
522 tower_data
+= endpoint_size
;
524 nb_floor
= (twr_empty_floor_t
*)tower_data
;
526 tower_data
+= sizeof(*nb_floor
);
528 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
529 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
530 nb_floor
->count_rhs
= networkaddr_size
;
533 memcpy(tower_data
, networkaddr
, networkaddr_size
);
540 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
545 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
546 const twr_empty_floor_t
*nb_floor
;
548 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
550 if (tower_size
< sizeof(*smb_floor
))
551 return EPT_S_NOT_REGISTERED
;
553 tower_data
+= sizeof(*smb_floor
);
554 tower_size
-= sizeof(*smb_floor
);
556 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
557 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
558 (smb_floor
->count_rhs
> tower_size
) ||
559 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
560 return EPT_S_NOT_REGISTERED
;
564 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
566 return RPC_S_OUT_OF_RESOURCES
;
567 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
569 tower_data
+= smb_floor
->count_rhs
;
570 tower_size
-= smb_floor
->count_rhs
;
572 if (tower_size
< sizeof(*nb_floor
))
573 return EPT_S_NOT_REGISTERED
;
575 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
577 tower_data
+= sizeof(*nb_floor
);
578 tower_size
-= sizeof(*nb_floor
);
580 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
581 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
582 (nb_floor
->count_rhs
> tower_size
) ||
583 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
584 return EPT_S_NOT_REGISTERED
;
588 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
593 I_RpcFree(*endpoint
);
596 return RPC_S_OUT_OF_RESOURCES
;
598 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
604 typedef struct _RpcServerProtseq_np
606 RpcServerProtseq common
;
608 } RpcServerProtseq_np
;
610 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
612 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
614 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
618 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
620 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
621 SetEvent(npps
->mgr_event
);
624 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
626 HANDLE
*objs
= prev_array
;
627 RpcConnection_np
*conn
;
628 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
630 EnterCriticalSection(&protseq
->cs
);
632 /* open and count connections */
634 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
636 rpcrt4_conn_listen_pipe(conn
);
637 if (conn
->ovl
[0].hEvent
)
639 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
642 /* make array of connections */
644 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
646 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
649 ERR("couldn't allocate objs\n");
650 LeaveCriticalSection(&protseq
->cs
);
654 objs
[0] = npps
->mgr_event
;
656 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
658 if ((objs
[*count
] = conn
->ovl
[0].hEvent
))
660 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
662 LeaveCriticalSection(&protseq
->cs
);
666 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
668 HeapFree(GetProcessHeap(), 0, array
);
671 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
674 HANDLE
*objs
= wait_array
;
676 RpcConnection
*cconn
;
677 RpcConnection_np
*conn
;
684 /* an alertable wait isn't strictly necessary, but due to our
685 * overlapped I/O implementation in Wine we need to free some memory
686 * by the file user APC being called, even if no completion routine was
687 * specified at the time of starting the async operation */
688 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
689 } while (res
== WAIT_IO_COMPLETION
);
691 if (res
== WAIT_OBJECT_0
)
693 else if (res
== WAIT_FAILED
)
695 ERR("wait failed with error %d\n", GetLastError());
700 b_handle
= objs
[res
- WAIT_OBJECT_0
];
701 /* find which connection got a RPC */
702 EnterCriticalSection(&protseq
->cs
);
703 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
705 if (b_handle
== conn
->ovl
[0].hEvent
) break;
706 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
710 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
712 ERR("failed to locate connection for handle %p\n", b_handle
);
713 LeaveCriticalSection(&protseq
->cs
);
716 RPCRT4_new_client(cconn
);
723 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
724 const char *networkaddr
,
725 const char *endpoint
)
727 twr_empty_floor_t
*pipe_floor
;
729 size_t endpoint_size
;
731 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
733 endpoint_size
= strlen(endpoint
) + 1;
734 size
= sizeof(*pipe_floor
) + endpoint_size
;
739 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
741 tower_data
+= sizeof(*pipe_floor
);
743 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
744 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
745 pipe_floor
->count_rhs
= endpoint_size
;
747 memcpy(tower_data
, endpoint
, endpoint_size
);
752 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
757 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
759 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
761 if (tower_size
< sizeof(*pipe_floor
))
762 return EPT_S_NOT_REGISTERED
;
764 tower_data
+= sizeof(*pipe_floor
);
765 tower_size
-= sizeof(*pipe_floor
);
767 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
768 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
769 (pipe_floor
->count_rhs
> tower_size
) ||
770 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
771 return EPT_S_NOT_REGISTERED
;
778 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
780 return RPC_S_OUT_OF_RESOURCES
;
781 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
787 /**** ncacn_ip_tcp support ****/
789 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
790 const char *networkaddr
,
791 unsigned char tcp_protid
,
792 const char *endpoint
)
794 twr_tcp_floor_t
*tcp_floor
;
795 twr_ipv4_floor_t
*ipv4_floor
;
797 struct addrinfo hints
;
799 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
801 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
806 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
807 tower_data
+= sizeof(*tcp_floor
);
809 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
811 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
812 tcp_floor
->protid
= tcp_protid
;
813 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
815 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
816 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
817 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
819 hints
.ai_flags
= AI_NUMERICHOST
;
820 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
821 hints
.ai_family
= PF_INET
;
822 hints
.ai_socktype
= SOCK_STREAM
;
823 hints
.ai_protocol
= IPPROTO_TCP
;
824 hints
.ai_addrlen
= 0;
825 hints
.ai_addr
= NULL
;
826 hints
.ai_canonname
= NULL
;
827 hints
.ai_next
= NULL
;
829 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
832 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
835 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
840 if (ai
->ai_family
== PF_INET
)
842 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
843 tcp_floor
->port
= sin
->sin_port
;
844 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
848 ERR("unexpected protocol family %d\n", ai
->ai_family
);
857 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
860 unsigned char tcp_protid
,
863 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
864 const twr_ipv4_floor_t
*ipv4_floor
;
865 struct in_addr in_addr
;
867 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
869 if (tower_size
< sizeof(*tcp_floor
))
870 return EPT_S_NOT_REGISTERED
;
872 tower_data
+= sizeof(*tcp_floor
);
873 tower_size
-= sizeof(*tcp_floor
);
875 if (tower_size
< sizeof(*ipv4_floor
))
876 return EPT_S_NOT_REGISTERED
;
878 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
880 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
881 (tcp_floor
->protid
!= tcp_protid
) ||
882 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
883 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
884 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
885 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
886 return EPT_S_NOT_REGISTERED
;
890 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
892 return RPC_S_OUT_OF_RESOURCES
;
893 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
898 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
903 I_RpcFree(*endpoint
);
906 return RPC_S_OUT_OF_RESOURCES
;
908 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
909 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
911 ERR("inet_ntop: %s\n", strerror(errno
));
912 I_RpcFree(*networkaddr
);
916 I_RpcFree(*endpoint
);
919 return EPT_S_NOT_REGISTERED
;
926 typedef struct _RpcConnection_tcp
928 RpcConnection common
;
930 #ifdef HAVE_SOCKETPAIR
938 #ifdef HAVE_SOCKETPAIR
940 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
942 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
944 ERR("socketpair() failed: %s\n", strerror(errno
));
950 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
952 struct pollfd pfds
[2];
953 pfds
[0].fd
= tcpc
->sock
;
954 pfds
[0].events
= POLLIN
;
955 pfds
[1].fd
= tcpc
->cancel_fds
[0];
956 pfds
[1].events
= POLLIN
;
957 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
959 ERR("poll() failed: %s\n", strerror(errno
));
962 if (pfds
[1].revents
& POLLIN
) /* canceled */
965 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
971 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
975 pfd
.events
= POLLOUT
;
976 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
978 ERR("poll() failed: %s\n", strerror(errno
));
984 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
988 write(tcpc
->cancel_fds
[1], &dummy
, 1);
991 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
993 close(tcpc
->cancel_fds
[0]);
994 close(tcpc
->cancel_fds
[1]);
997 #else /* HAVE_SOCKETPAIR */
999 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1001 static BOOL wsa_inited
;
1005 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1006 /* Note: WSAStartup can be called more than once so we don't bother with
1007 * making accesses to wsa_inited thread-safe */
1010 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1011 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1012 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1014 ERR("event creation failed\n");
1015 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1021 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1023 HANDLE wait_handles
[2];
1025 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1027 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1030 wait_handles
[0] = tcpc
->sock_event
;
1031 wait_handles
[1] = tcpc
->cancel_event
;
1032 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1037 case WAIT_OBJECT_0
+ 1:
1040 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1045 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1048 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1050 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1053 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1059 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1064 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1066 SetEvent(tcpc
->cancel_event
);
1069 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1071 CloseHandle(tcpc
->sock_event
);
1072 CloseHandle(tcpc
->cancel_event
);
1077 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1079 RpcConnection_tcp
*tcpc
;
1080 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1084 if (!rpcrt4_sock_wait_init(tcpc
))
1086 HeapFree(GetProcessHeap(), 0, tcpc
);
1089 return &tcpc
->common
;
1092 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1094 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1097 struct addrinfo
*ai
;
1098 struct addrinfo
*ai_cur
;
1099 struct addrinfo hints
;
1101 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1103 if (tcpc
->sock
!= -1)
1107 hints
.ai_family
= PF_UNSPEC
;
1108 hints
.ai_socktype
= SOCK_STREAM
;
1109 hints
.ai_protocol
= IPPROTO_TCP
;
1110 hints
.ai_addrlen
= 0;
1111 hints
.ai_addr
= NULL
;
1112 hints
.ai_canonname
= NULL
;
1113 hints
.ai_next
= NULL
;
1115 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1118 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1119 Connection
->Endpoint
, gai_strerror(ret
));
1120 return RPC_S_SERVER_UNAVAILABLE
;
1123 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1128 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1130 TRACE("skipping non-IP/IPv6 address family\n");
1138 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1139 host
, sizeof(host
), service
, sizeof(service
),
1140 NI_NUMERICHOST
| NI_NUMERICSERV
);
1141 TRACE("trying %s:%s\n", host
, service
);
1144 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1147 WARN("socket() failed: %s\n", strerror(errno
));
1151 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1153 WARN("connect() failed: %s\n", strerror(errno
));
1158 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1160 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1162 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1167 TRACE("connected\n");
1172 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1173 return RPC_S_SERVER_UNAVAILABLE
;
1176 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1178 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1181 struct addrinfo
*ai
;
1182 struct addrinfo
*ai_cur
;
1183 struct addrinfo hints
;
1184 RpcConnection
*first_connection
= NULL
;
1186 TRACE("(%p, %s)\n", protseq
, endpoint
);
1188 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1189 hints
.ai_family
= PF_UNSPEC
;
1190 hints
.ai_socktype
= SOCK_STREAM
;
1191 hints
.ai_protocol
= IPPROTO_TCP
;
1192 hints
.ai_addrlen
= 0;
1193 hints
.ai_addr
= NULL
;
1194 hints
.ai_canonname
= NULL
;
1195 hints
.ai_next
= NULL
;
1197 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1200 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1202 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1203 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1204 return RPC_S_CANT_CREATE_ENDPOINT
;
1207 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1209 RpcConnection_tcp
*tcpc
;
1210 RPC_STATUS create_status
;
1211 struct sockaddr_storage sa
;
1213 char service
[NI_MAXSERV
];
1216 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1218 TRACE("skipping non-IP/IPv6 address family\n");
1225 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1226 host
, sizeof(host
), service
, sizeof(service
),
1227 NI_NUMERICHOST
| NI_NUMERICSERV
);
1228 TRACE("trying %s:%s\n", host
, service
);
1231 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1234 WARN("socket() failed: %s\n", strerror(errno
));
1235 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1239 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1242 WARN("bind failed: %s\n", strerror(errno
));
1244 if (errno
== EADDRINUSE
)
1245 status
= RPC_S_DUPLICATE_ENDPOINT
;
1247 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1251 sa_len
= sizeof(sa
);
1252 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1254 WARN("getsockname() failed: %s\n", strerror(errno
));
1255 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1259 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1260 NULL
, 0, service
, sizeof(service
),
1264 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1265 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1269 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1270 protseq
->Protseq
, NULL
,
1271 service
, NULL
, NULL
, NULL
);
1272 if (create_status
!= RPC_S_OK
)
1275 status
= create_status
;
1280 ret
= listen(sock
, protseq
->MaxCalls
);
1283 WARN("listen failed: %s\n", strerror(errno
));
1284 RPCRT4_DestroyConnection(&tcpc
->common
);
1285 status
= RPC_S_OUT_OF_RESOURCES
;
1288 /* need a non-blocking socket, otherwise accept() has a potential
1289 * race-condition (poll() says it is readable, connection drops,
1290 * and accept() blocks until the next connection comes...)
1293 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1296 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1297 RPCRT4_DestroyConnection(&tcpc
->common
);
1298 status
= RPC_S_OUT_OF_RESOURCES
;
1302 tcpc
->common
.Next
= first_connection
;
1303 first_connection
= &tcpc
->common
;
1305 /* since IPv4 and IPv6 share the same port space, we only need one
1306 * successful bind to listen for both */
1312 /* if at least one connection was created for an endpoint then
1314 if (first_connection
)
1316 RpcConnection
*conn
;
1318 /* find last element in list */
1319 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1322 EnterCriticalSection(&protseq
->cs
);
1323 conn
->Next
= protseq
->conn
;
1324 protseq
->conn
= first_connection
;
1325 LeaveCriticalSection(&protseq
->cs
);
1327 TRACE("listening on %s\n", endpoint
);
1331 ERR("couldn't listen on port %s\n", endpoint
);
1335 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1338 struct sockaddr_in address
;
1340 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1341 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1344 addrsize
= sizeof(address
);
1345 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1348 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1349 return RPC_S_OUT_OF_RESOURCES
;
1352 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1354 TRACE("Accepted a new TCP connection\n");
1358 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1359 void *buffer
, unsigned int count
)
1361 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1365 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1370 else if (errno
!= EAGAIN
)
1372 WARN("recv() failed: %s\n", strerror(errno
));
1377 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1380 } while (bytes_read
!= count
);
1381 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1385 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1386 const void *buffer
, unsigned int count
)
1388 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1389 int bytes_written
= 0;
1392 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1395 else if (errno
!= EAGAIN
)
1399 if (!rpcrt4_sock_wait_for_send(tcpc
))
1402 } while (bytes_written
!= count
);
1403 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1404 return bytes_written
;
1407 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1409 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1411 TRACE("%d\n", tcpc
->sock
);
1413 if (tcpc
->sock
!= -1)
1414 closesocket(tcpc
->sock
);
1416 rpcrt4_sock_wait_destroy(tcpc
);
1420 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1422 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1423 TRACE("%p\n", Connection
);
1424 rpcrt4_sock_wait_cancel(tcpc
);
1427 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1429 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1431 TRACE("%p\n", Connection
);
1433 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1438 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1439 const char *networkaddr
,
1440 const char *endpoint
)
1442 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1443 EPM_PROTOCOL_TCP
, endpoint
);
1446 #ifdef HAVE_SOCKETPAIR
1448 typedef struct _RpcServerProtseq_sock
1450 RpcServerProtseq common
;
1453 } RpcServerProtseq_sock
;
1455 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1457 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1461 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1463 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1464 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1465 ps
->mgr_event_rcv
= fds
[0];
1466 ps
->mgr_event_snd
= fds
[1];
1470 ERR("socketpair failed with error %s\n", strerror(errno
));
1471 HeapFree(GetProcessHeap(), 0, ps
);
1478 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1480 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1482 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1485 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1487 struct pollfd
*poll_info
= prev_array
;
1488 RpcConnection_tcp
*conn
;
1489 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1491 EnterCriticalSection(&protseq
->cs
);
1493 /* open and count connections */
1495 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1497 if (conn
->sock
!= -1)
1499 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1502 /* make array of connections */
1504 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1506 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1509 ERR("couldn't allocate poll_info\n");
1510 LeaveCriticalSection(&protseq
->cs
);
1514 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1515 poll_info
[0].events
= POLLIN
;
1517 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1519 if (conn
->sock
!= -1)
1521 poll_info
[*count
].fd
= conn
->sock
;
1522 poll_info
[*count
].events
= POLLIN
;
1525 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1527 LeaveCriticalSection(&protseq
->cs
);
1531 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1533 HeapFree(GetProcessHeap(), 0, array
);
1536 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1538 struct pollfd
*poll_info
= wait_array
;
1541 RpcConnection
*cconn
;
1542 RpcConnection_tcp
*conn
;
1547 ret
= poll(poll_info
, count
, -1);
1550 ERR("poll failed with error %d\n", ret
);
1554 for (i
= 0; i
< count
; i
++)
1555 if (poll_info
[i
].revents
& POLLIN
)
1557 /* RPC server event */
1561 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1565 /* find which connection got a RPC */
1566 EnterCriticalSection(&protseq
->cs
);
1567 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1569 if (poll_info
[i
].fd
== conn
->sock
) break;
1570 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1574 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1576 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1577 LeaveCriticalSection(&protseq
->cs
);
1579 RPCRT4_new_client(cconn
);
1587 #else /* HAVE_SOCKETPAIR */
1589 typedef struct _RpcServerProtseq_sock
1591 RpcServerProtseq common
;
1593 } RpcServerProtseq_sock
;
1595 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1597 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1600 static BOOL wsa_inited
;
1604 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1605 /* Note: WSAStartup can be called more than once so we don't bother with
1606 * making accesses to wsa_inited thread-safe */
1609 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1614 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1616 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1617 SetEvent(sockps
->mgr_event
);
1620 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1622 HANDLE
*objs
= prev_array
;
1623 RpcConnection_tcp
*conn
;
1624 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1626 EnterCriticalSection(&protseq
->cs
);
1628 /* open and count connections */
1630 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1633 if (conn
->sock
!= -1)
1635 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1638 /* make array of connections */
1640 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1642 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1645 ERR("couldn't allocate objs\n");
1646 LeaveCriticalSection(&protseq
->cs
);
1650 objs
[0] = sockps
->mgr_event
;
1652 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1655 if (conn
->sock
!= -1)
1657 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1658 if (res
== SOCKET_ERROR
)
1659 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1662 objs
[*count
] = conn
->sock_event
;
1666 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1668 LeaveCriticalSection(&protseq
->cs
);
1672 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1674 HeapFree(GetProcessHeap(), 0, array
);
1677 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1680 HANDLE
*objs
= wait_array
;
1682 RpcConnection
*cconn
;
1683 RpcConnection_tcp
*conn
;
1690 /* an alertable wait isn't strictly necessary, but due to our
1691 * overlapped I/O implementation in Wine we need to free some memory
1692 * by the file user APC being called, even if no completion routine was
1693 * specified at the time of starting the async operation */
1694 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1695 } while (res
== WAIT_IO_COMPLETION
);
1697 if (res
== WAIT_OBJECT_0
)
1699 else if (res
== WAIT_FAILED
)
1701 ERR("wait failed with error %d\n", GetLastError());
1706 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1707 /* find which connection got a RPC */
1708 EnterCriticalSection(&protseq
->cs
);
1709 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1712 if (b_handle
== conn
->sock_event
) break;
1713 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1717 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1719 ERR("failed to locate connection for handle %p\n", b_handle
);
1720 LeaveCriticalSection(&protseq
->cs
);
1723 RPCRT4_new_client(cconn
);
1730 #endif /* HAVE_SOCKETPAIR */
1732 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1737 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1738 networkaddr
, EPM_PROTOCOL_TCP
,
1742 /**** ncacn_http support ****/
1744 /* 60 seconds is the period native uses */
1745 #define HTTP_IDLE_TIME 60000
1747 /* reference counted to avoid a race between a cancelled call's connection
1748 * being destroyed and the asynchronous InternetReadFileEx call being
1750 typedef struct _RpcHttpAsyncData
1753 HANDLE completion_event
;
1754 INTERNET_BUFFERSA inet_buffers
;
1755 void *destination_buffer
; /* the address that inet_buffers.lpvBuffer will be
1756 * copied into when the call completes */
1757 CRITICAL_SECTION cs
;
1760 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1762 return InterlockedIncrement(&data
->refs
);
1765 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1767 ULONG refs
= InterlockedDecrement(&data
->refs
);
1770 TRACE("destroying async data %p\n", data
);
1771 CloseHandle(data
->completion_event
);
1772 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1773 DeleteCriticalSection(&data
->cs
);
1774 HeapFree(GetProcessHeap(), 0, data
);
1779 typedef struct _RpcConnection_http
1781 RpcConnection common
;
1784 HINTERNET in_request
;
1785 HINTERNET out_request
;
1786 HANDLE timer_cancelled
;
1787 HANDLE cancel_event
;
1788 DWORD last_sent_time
;
1789 ULONG bytes_received
;
1790 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1791 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1792 UUID connection_uuid
;
1795 RpcHttpAsyncData
*async_data
;
1796 } RpcConnection_http
;
1798 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1800 RpcConnection_http
*httpc
;
1801 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1802 if (!httpc
) return NULL
;
1803 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1804 if (!httpc
->async_data
)
1806 HeapFree(GetProcessHeap(), 0, httpc
);
1809 TRACE("async data = %p\n", httpc
->async_data
);
1810 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1811 httpc
->async_data
->refs
= 1;
1812 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1813 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1814 httpc
->async_data
->destination_buffer
= NULL
;
1815 InitializeCriticalSection(&httpc
->async_data
->cs
);
1816 return &httpc
->common
;
1819 typedef struct _HttpTimerThreadData
1822 DWORD
*last_sent_time
;
1823 HANDLE timer_cancelled
;
1824 } HttpTimerThreadData
;
1826 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1828 HINTERNET in_request
= param
;
1829 RpcPktHdr
*idle_pkt
;
1831 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1835 DWORD bytes_written
;
1836 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1837 RPCRT4_FreeHeader(idle_pkt
);
1841 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1843 DWORD cur_time
= GetTickCount();
1844 DWORD cached_last_sent_time
= *last_sent_time
;
1845 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1848 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1850 HttpTimerThreadData
*data_in
= param
;
1851 HttpTimerThreadData data
;
1855 HeapFree(GetProcessHeap(), 0, data_in
);
1857 for (timeout
= HTTP_IDLE_TIME
;
1858 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1859 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1861 /* are we too soon after last send? */
1862 if (GetTickCount() - HTTP_IDLE_TIME
< *data
.last_sent_time
)
1864 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
1867 CloseHandle(data
.timer_cancelled
);
1871 static VOID WINAPI
rpcrt4_http_internet_callback(
1872 HINTERNET hInternet
,
1873 DWORD_PTR dwContext
,
1874 DWORD dwInternetStatus
,
1875 LPVOID lpvStatusInformation
,
1876 DWORD dwStatusInformationLength
)
1878 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
1880 switch (dwInternetStatus
)
1882 case INTERNET_STATUS_REQUEST_COMPLETE
:
1883 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1886 if (async_data
->inet_buffers
.lpvBuffer
)
1888 EnterCriticalSection(&async_data
->cs
);
1889 if (async_data
->destination_buffer
)
1891 memcpy(async_data
->destination_buffer
,
1892 async_data
->inet_buffers
.lpvBuffer
,
1893 async_data
->inet_buffers
.dwBufferLength
);
1894 async_data
->destination_buffer
= NULL
;
1896 LeaveCriticalSection(&async_data
->cs
);
1898 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
1899 async_data
->inet_buffers
.lpvBuffer
= NULL
;
1900 SetEvent(async_data
->completion_event
);
1901 RpcHttpAsyncData_Release(async_data
);
1907 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
1914 WCHAR
*status_text
= buf
;
1918 size
= sizeof(status_code
);
1919 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
1921 return GetLastError();
1922 if (status_code
< 400)
1926 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1927 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
1929 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
1930 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
1933 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
1934 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
1936 if (status_code
== HTTP_STATUS_DENIED
)
1937 return ERROR_ACCESS_DENIED
;
1938 return RPC_S_SERVER_UNAVAILABLE
;
1941 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
1943 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
1944 LPWSTR proxy
= NULL
;
1946 LPWSTR password
= NULL
;
1947 LPWSTR servername
= NULL
;
1948 const WCHAR
*option
;
1949 INTERNET_PORT port
= INTERNET_INVALID_PORT_NUMBER
; /* use default port */
1951 if (httpc
->common
.QOS
&&
1952 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
1954 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
1955 if (http_cred
->TransportCredentials
)
1958 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
1959 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
1960 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1962 return RPC_S_OUT_OF_RESOURCES
;
1964 if (cred
->DomainLength
)
1966 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
1967 p
+= cred
->DomainLength
;
1971 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
1972 p
[cred
->UserLength
] = 0;
1974 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
1978 for (option
= httpc
->common
.NetworkOptions
; option
;
1979 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
1981 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
1982 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
1984 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
1986 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
1987 const WCHAR
*value_end
;
1990 value_end
= strchrW(option
, ',');
1992 value_end
= value_start
+ strlenW(value_start
);
1993 for (p
= value_start
; p
< value_end
; p
++)
2000 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2001 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2003 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2005 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2006 const WCHAR
*value_end
;
2008 value_end
= strchrW(option
, ',');
2010 value_end
= value_start
+ strlenW(value_start
);
2011 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2012 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2015 FIXME("unhandled option %s\n", debugstr_w(option
));
2018 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2019 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2020 if (!httpc
->app_info
)
2022 HeapFree(GetProcessHeap(), 0, password
);
2023 HeapFree(GetProcessHeap(), 0, user
);
2024 ERR("InternetOpenW failed with error %d\n", GetLastError());
2025 return RPC_S_SERVER_UNAVAILABLE
;
2027 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2029 /* if no RpcProxy option specified, set the HTTP server address to the
2030 * RPC server address */
2033 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2036 HeapFree(GetProcessHeap(), 0, password
);
2037 HeapFree(GetProcessHeap(), 0, user
);
2038 return RPC_S_OUT_OF_RESOURCES
;
2040 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2043 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2044 INTERNET_SERVICE_HTTP
, 0, 0);
2046 HeapFree(GetProcessHeap(), 0, password
);
2047 HeapFree(GetProcessHeap(), 0, user
);
2048 HeapFree(GetProcessHeap(), 0, servername
);
2050 if (!httpc
->session
)
2052 ERR("InternetConnectW failed with error %d\n", GetLastError());
2053 return RPC_S_SERVER_UNAVAILABLE
;
2059 /* prepare the in pipe for use by RPC packets */
2060 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
,
2061 const UUID
*connection_uuid
,
2062 const UUID
*in_pipe_uuid
,
2063 const UUID
*association_uuid
)
2069 INTERNET_BUFFERSW buffers_in
;
2070 DWORD bytes_read
, bytes_written
;
2072 /* prepare in pipe */
2073 ResetEvent(async_data
->completion_event
);
2074 RpcHttpAsyncData_AddRef(async_data
);
2075 ret
= HttpSendRequestW(in_request
, NULL
, 0, NULL
, 0);
2078 if (GetLastError() == ERROR_IO_PENDING
)
2079 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2082 RpcHttpAsyncData_Release(async_data
);
2083 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2084 return RPC_S_SERVER_UNAVAILABLE
;
2087 status
= rpcrt4_http_check_response(in_request
);
2088 if (status
!= RPC_S_OK
) return status
;
2090 InternetReadFile(in_request
, packet
, 20, &bytes_read
);
2091 /* FIXME: do something with retrieved data */
2093 memset(&buffers_in
, 0, sizeof(buffers_in
));
2094 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2095 /* FIXME: get this from the registry */
2096 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2097 ResetEvent(async_data
->completion_event
);
2098 RpcHttpAsyncData_AddRef(async_data
);
2099 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2102 if (GetLastError() == ERROR_IO_PENDING
)
2103 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2106 RpcHttpAsyncData_Release(async_data
);
2107 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
2108 return RPC_S_SERVER_UNAVAILABLE
;
2112 TRACE("sending HTTP connect header to server\n");
2113 hdr
= RPCRT4_BuildHttpConnectHeader(0, FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2114 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2115 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2116 RPCRT4_FreeHeader(hdr
);
2119 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2120 return RPC_S_SERVER_UNAVAILABLE
;
2126 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2130 unsigned short data_len
;
2132 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2134 return RPC_S_SERVER_UNAVAILABLE
;
2135 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2137 ERR("wrong packet type received %d or wrong frag_len %d\n",
2138 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2139 return RPC_S_PROTOCOL_ERROR
;
2142 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2144 return RPC_S_SERVER_UNAVAILABLE
;
2146 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2149 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2151 return RPC_S_OUT_OF_RESOURCES
;
2152 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2155 HeapFree(GetProcessHeap(), 0, *data
);
2156 return RPC_S_SERVER_UNAVAILABLE
;
2162 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2164 ERR("invalid http packet\n");
2165 return RPC_S_PROTOCOL_ERROR
;
2171 /* prepare the out pipe for use by RPC packets */
2172 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
,
2173 RpcHttpAsyncData
*async_data
,
2174 const UUID
*connection_uuid
,
2175 const UUID
*out_pipe_uuid
,
2176 ULONG
*flow_control_increment
)
2183 BYTE
*data_from_server
;
2184 RpcPktHdr pkt_from_server
;
2185 ULONG field1
, field3
;
2187 ResetEvent(async_data
->completion_event
);
2188 RpcHttpAsyncData_AddRef(async_data
);
2189 ret
= HttpSendRequestW(out_request
, NULL
, 0, NULL
, 0);
2192 if (GetLastError() == ERROR_IO_PENDING
)
2193 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2196 RpcHttpAsyncData_Release(async_data
);
2197 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2198 return RPC_S_SERVER_UNAVAILABLE
;
2201 status
= rpcrt4_http_check_response(out_request
);
2202 if (status
!= RPC_S_OK
) return status
;
2204 InternetReadFile(out_request
, packet
, 20, &bytes_read
);
2205 /* FIXME: do something with retrieved data */
2207 hdr
= RPCRT4_BuildHttpConnectHeader(0, TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2208 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2209 ResetEvent(async_data
->completion_event
);
2210 RpcHttpAsyncData_AddRef(async_data
);
2211 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2214 if (GetLastError() == ERROR_IO_PENDING
)
2215 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2218 RpcHttpAsyncData_Release(async_data
);
2219 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2220 RPCRT4_FreeHeader(hdr
);
2221 return RPC_S_SERVER_UNAVAILABLE
;
2224 RPCRT4_FreeHeader(hdr
);
2225 status
= rpcrt4_http_check_response(out_request
);
2226 if (status
!= RPC_S_OK
) return status
;
2228 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2230 if (status
!= RPC_S_OK
) return status
;
2231 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2233 HeapFree(GetProcessHeap(), 0, data_from_server
);
2234 if (status
!= RPC_S_OK
) return status
;
2235 TRACE("received (%d) from first prepare header\n", field1
);
2237 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2239 if (status
!= RPC_S_OK
) return status
;
2240 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2241 &field1
, flow_control_increment
,
2243 HeapFree(GetProcessHeap(), 0, data_from_server
);
2244 if (status
!= RPC_S_OK
) return status
;
2245 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2250 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2252 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2253 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2254 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2255 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2256 static const WCHAR wszColon
[] = {':',0};
2257 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2258 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2262 HttpTimerThreadData
*timer_data
;
2265 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2267 if (Connection
->server
)
2269 ERR("ncacn_http servers not supported yet\n");
2270 return RPC_S_SERVER_UNAVAILABLE
;
2273 if (httpc
->in_request
)
2276 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2278 status
= UuidCreate(&httpc
->connection_uuid
);
2279 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2280 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2282 status
= rpcrt4_http_internet_connect(httpc
);
2283 if (status
!= RPC_S_OK
)
2286 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2288 return RPC_S_OUT_OF_MEMORY
;
2289 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2290 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2291 strcatW(url
, wszColon
);
2292 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2294 secure
= httpc
->common
.QOS
&&
2295 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2296 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2298 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
,
2300 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2301 (DWORD_PTR
)httpc
->async_data
);
2302 if (!httpc
->in_request
)
2304 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2305 return RPC_S_SERVER_UNAVAILABLE
;
2307 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
,
2309 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2310 (DWORD_PTR
)httpc
->async_data
);
2311 if (!httpc
->out_request
)
2313 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2314 return RPC_S_SERVER_UNAVAILABLE
;
2317 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
,
2319 &httpc
->connection_uuid
,
2320 &httpc
->in_pipe_uuid
,
2321 &Connection
->assoc
->http_uuid
);
2322 if (status
!= RPC_S_OK
)
2325 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
,
2327 &httpc
->connection_uuid
,
2328 &httpc
->out_pipe_uuid
,
2329 &httpc
->flow_control_increment
);
2330 if (status
!= RPC_S_OK
)
2333 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2334 httpc
->last_sent_time
= GetTickCount();
2335 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2337 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2339 return ERROR_OUTOFMEMORY
;
2340 timer_data
->timer_param
= httpc
->in_request
;
2341 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2342 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2343 /* FIXME: should use CreateTimerQueueTimer when implemented */
2344 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2347 HeapFree(GetProcessHeap(), 0, timer_data
);
2348 return GetLastError();
2350 CloseHandle(thread
);
2355 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2358 return RPC_S_SERVER_UNAVAILABLE
;
2361 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2362 void *buffer
, unsigned int count
)
2364 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2367 unsigned int bytes_left
= count
;
2369 ResetEvent(httpc
->async_data
->completion_event
);
2372 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2373 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2374 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, bytes_left
);
2375 httpc
->async_data
->destination_buffer
= buf
;
2376 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
2379 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2381 RpcHttpAsyncData_Release(httpc
->async_data
);
2382 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
2383 httpc
->async_data
->inet_buffers
.dwBufferLength
);
2384 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2385 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2386 httpc
->async_data
->destination_buffer
= NULL
;
2390 if (GetLastError() == ERROR_IO_PENDING
)
2392 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2393 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2394 if (result
== WAIT_OBJECT_0
)
2398 TRACE("call cancelled\n");
2399 EnterCriticalSection(&httpc
->async_data
->cs
);
2400 httpc
->async_data
->destination_buffer
= NULL
;
2401 LeaveCriticalSection(&httpc
->async_data
->cs
);
2407 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2408 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2409 httpc
->async_data
->destination_buffer
= NULL
;
2410 RpcHttpAsyncData_Release(httpc
->async_data
);
2414 if (!httpc
->async_data
->inet_buffers
.dwBufferLength
)
2416 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2417 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2419 TRACE("%p %p %u -> %s\n", httpc
->out_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2420 return ret
? count
: -1;
2423 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2425 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2429 RpcPktCommonHdr common_hdr
;
2433 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2436 /* read packet common header */
2437 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2438 if (dwRead
!= sizeof(common_hdr
)) {
2439 WARN("Short read of header, %d bytes\n", dwRead
);
2440 status
= RPC_S_PROTOCOL_ERROR
;
2443 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2444 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2446 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2447 status
= RPC_S_PROTOCOL_ERROR
;
2451 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2452 if (status
!= RPC_S_OK
) goto fail
;
2454 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2455 if (hdr_length
== 0) {
2456 WARN("header length == 0\n");
2457 status
= RPC_S_PROTOCOL_ERROR
;
2461 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2464 status
= RPC_S_OUT_OF_RESOURCES
;
2467 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2469 /* read the rest of packet header */
2470 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2471 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2472 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2473 status
= RPC_S_PROTOCOL_ERROR
;
2477 if (common_hdr
.frag_len
- hdr_length
)
2479 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2482 status
= RPC_S_OUT_OF_RESOURCES
;
2486 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2487 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2489 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2490 status
= RPC_S_PROTOCOL_ERROR
;
2497 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2499 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2501 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2502 status
= RPC_S_PROTOCOL_ERROR
;
2505 if ((*Header
)->http
.flags
== 0x0001)
2507 TRACE("http idle packet, waiting for real packet\n");
2508 if ((*Header
)->http
.num_data_items
!= 0)
2510 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2511 status
= RPC_S_PROTOCOL_ERROR
;
2515 else if ((*Header
)->http
.flags
== 0x0002)
2517 ULONG bytes_transmitted
;
2518 ULONG flow_control_increment
;
2520 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2523 &flow_control_increment
,
2525 if (status
!= RPC_S_OK
)
2527 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2528 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2529 /* FIXME: do something with parsed data */
2533 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
2534 status
= RPC_S_PROTOCOL_ERROR
;
2537 RPCRT4_FreeHeader(*Header
);
2539 HeapFree(GetProcessHeap(), 0, *Payload
);
2547 httpc
->bytes_received
+= common_hdr
.frag_len
;
2549 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
2551 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
2553 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
2554 httpc
->bytes_received
,
2555 httpc
->flow_control_increment
,
2556 &httpc
->out_pipe_uuid
);
2559 DWORD bytes_written
;
2561 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
2562 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2563 RPCRT4_FreeHeader(hdr
);
2565 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
2570 if (status
!= RPC_S_OK
) {
2571 RPCRT4_FreeHeader(*Header
);
2573 HeapFree(GetProcessHeap(), 0, *Payload
);
2579 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
2580 const void *buffer
, unsigned int count
)
2582 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2583 DWORD bytes_written
;
2586 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
2587 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
2588 httpc
->last_sent_time
= GetTickCount();
2589 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2590 return ret
? bytes_written
: -1;
2593 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
2595 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2599 SetEvent(httpc
->timer_cancelled
);
2600 if (httpc
->in_request
)
2601 InternetCloseHandle(httpc
->in_request
);
2602 httpc
->in_request
= NULL
;
2603 if (httpc
->out_request
)
2604 InternetCloseHandle(httpc
->out_request
);
2605 httpc
->out_request
= NULL
;
2606 if (httpc
->app_info
)
2607 InternetCloseHandle(httpc
->app_info
);
2608 httpc
->app_info
= NULL
;
2610 InternetCloseHandle(httpc
->session
);
2611 httpc
->session
= NULL
;
2612 RpcHttpAsyncData_Release(httpc
->async_data
);
2613 if (httpc
->cancel_event
)
2614 CloseHandle(httpc
->cancel_event
);
2619 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
2621 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2623 SetEvent(httpc
->cancel_event
);
2626 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
2629 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2631 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2632 ret
= InternetQueryDataAvailable(httpc
->out_request
,
2633 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
2636 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2638 RpcHttpAsyncData_Release(httpc
->async_data
);
2642 if (GetLastError() == ERROR_IO_PENDING
)
2644 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2645 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2646 if (result
!= WAIT_OBJECT_0
)
2648 TRACE("call cancelled\n");
2654 RpcHttpAsyncData_Release(httpc
->async_data
);
2663 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
2664 const char *networkaddr
,
2665 const char *endpoint
)
2667 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
2668 EPM_PROTOCOL_HTTP
, endpoint
);
2671 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
2676 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
2677 networkaddr
, EPM_PROTOCOL_HTTP
,
2681 static const struct connection_ops conn_protseq_list
[] = {
2683 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
2684 rpcrt4_conn_np_alloc
,
2685 rpcrt4_ncacn_np_open
,
2686 rpcrt4_ncacn_np_handoff
,
2687 rpcrt4_conn_np_read
,
2688 rpcrt4_conn_np_write
,
2689 rpcrt4_conn_np_close
,
2690 rpcrt4_conn_np_cancel_call
,
2691 rpcrt4_conn_np_wait_for_incoming_data
,
2692 rpcrt4_ncacn_np_get_top_of_tower
,
2693 rpcrt4_ncacn_np_parse_top_of_tower
,
2697 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
2698 rpcrt4_conn_np_alloc
,
2699 rpcrt4_ncalrpc_open
,
2700 rpcrt4_ncalrpc_handoff
,
2701 rpcrt4_conn_np_read
,
2702 rpcrt4_conn_np_write
,
2703 rpcrt4_conn_np_close
,
2704 rpcrt4_conn_np_cancel_call
,
2705 rpcrt4_conn_np_wait_for_incoming_data
,
2706 rpcrt4_ncalrpc_get_top_of_tower
,
2707 rpcrt4_ncalrpc_parse_top_of_tower
,
2711 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
2712 rpcrt4_conn_tcp_alloc
,
2713 rpcrt4_ncacn_ip_tcp_open
,
2714 rpcrt4_conn_tcp_handoff
,
2715 rpcrt4_conn_tcp_read
,
2716 rpcrt4_conn_tcp_write
,
2717 rpcrt4_conn_tcp_close
,
2718 rpcrt4_conn_tcp_cancel_call
,
2719 rpcrt4_conn_tcp_wait_for_incoming_data
,
2720 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
2721 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
2726 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
2727 rpcrt4_ncacn_http_alloc
,
2728 rpcrt4_ncacn_http_open
,
2729 rpcrt4_ncacn_http_handoff
,
2730 rpcrt4_ncacn_http_read
,
2731 rpcrt4_ncacn_http_write
,
2732 rpcrt4_ncacn_http_close
,
2733 rpcrt4_ncacn_http_cancel_call
,
2734 rpcrt4_ncacn_http_wait_for_incoming_data
,
2735 rpcrt4_ncacn_http_get_top_of_tower
,
2736 rpcrt4_ncacn_http_parse_top_of_tower
,
2737 rpcrt4_ncacn_http_receive_fragment
,
2743 static const struct protseq_ops protseq_list
[] =
2747 rpcrt4_protseq_np_alloc
,
2748 rpcrt4_protseq_np_signal_state_changed
,
2749 rpcrt4_protseq_np_get_wait_array
,
2750 rpcrt4_protseq_np_free_wait_array
,
2751 rpcrt4_protseq_np_wait_for_new_connection
,
2752 rpcrt4_protseq_ncacn_np_open_endpoint
,
2756 rpcrt4_protseq_np_alloc
,
2757 rpcrt4_protseq_np_signal_state_changed
,
2758 rpcrt4_protseq_np_get_wait_array
,
2759 rpcrt4_protseq_np_free_wait_array
,
2760 rpcrt4_protseq_np_wait_for_new_connection
,
2761 rpcrt4_protseq_ncalrpc_open_endpoint
,
2765 rpcrt4_protseq_sock_alloc
,
2766 rpcrt4_protseq_sock_signal_state_changed
,
2767 rpcrt4_protseq_sock_get_wait_array
,
2768 rpcrt4_protseq_sock_free_wait_array
,
2769 rpcrt4_protseq_sock_wait_for_new_connection
,
2770 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
2774 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2776 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
2779 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
2780 if (!strcmp(protseq_list
[i
].name
, protseq
))
2781 return &protseq_list
[i
];
2785 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
2788 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
2789 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
2790 return &conn_protseq_list
[i
];
2794 /**** interface to rest of code ****/
2796 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
2798 TRACE("(Connection == ^%p)\n", Connection
);
2800 assert(!Connection
->server
);
2801 return Connection
->ops
->open_connection_client(Connection
);
2804 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
2806 TRACE("(Connection == ^%p)\n", Connection
);
2807 if (SecIsValidHandle(&Connection
->ctx
))
2809 DeleteSecurityContext(&Connection
->ctx
);
2810 SecInvalidateHandle(&Connection
->ctx
);
2812 rpcrt4_conn_close(Connection
);
2816 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
2817 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
2818 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
2820 static LONG next_id
;
2821 const struct connection_ops
*ops
;
2822 RpcConnection
* NewConnection
;
2824 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
2827 FIXME("not supported for protseq %s\n", Protseq
);
2828 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
2831 NewConnection
= ops
->alloc();
2832 NewConnection
->Next
= NULL
;
2833 NewConnection
->server_binding
= NULL
;
2834 NewConnection
->server
= server
;
2835 NewConnection
->ops
= ops
;
2836 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
2837 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
2838 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
2839 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
2840 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
2841 NewConnection
->NextCallId
= 1;
2843 SecInvalidateHandle(&NewConnection
->ctx
);
2844 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
2845 NewConnection
->attr
= 0;
2846 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
2847 NewConnection
->AuthInfo
= AuthInfo
;
2848 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
2849 NewConnection
->encryption_auth_len
= 0;
2850 NewConnection
->signature_auth_len
= 0;
2851 if (QOS
) RpcQualityOfService_AddRef(QOS
);
2852 NewConnection
->QOS
= QOS
;
2854 list_init(&NewConnection
->conn_pool_entry
);
2855 NewConnection
->async_state
= NULL
;
2857 TRACE("connection: %p\n", NewConnection
);
2858 *Connection
= NewConnection
;
2863 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
2867 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
2868 rpcrt4_conn_get_name(OldConnection
),
2869 OldConnection
->NetworkAddr
,
2870 OldConnection
->Endpoint
, NULL
,
2871 OldConnection
->AuthInfo
, OldConnection
->QOS
);
2872 if (err
== RPC_S_OK
)
2873 rpcrt4_conn_handoff(OldConnection
, *Connection
);
2877 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
2879 TRACE("connection: %p\n", Connection
);
2881 RPCRT4_CloseConnection(Connection
);
2882 RPCRT4_strfree(Connection
->Endpoint
);
2883 RPCRT4_strfree(Connection
->NetworkAddr
);
2884 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
2885 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
2886 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
2889 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
2891 HeapFree(GetProcessHeap(), 0, Connection
);
2895 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
2897 const char *protseq
,
2898 const char *networkaddr
,
2899 const char *endpoint
)
2901 twr_empty_floor_t
*protocol_floor
;
2902 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
2907 return RPC_S_INVALID_RPC_PROTSEQ
;
2911 *tower_size
= sizeof(*protocol_floor
);
2912 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
2916 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
2917 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
2918 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
2919 protocol_floor
->count_rhs
= 0;
2921 tower_data
+= sizeof(*protocol_floor
);
2923 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
2925 return EPT_S_NOT_REGISTERED
;
2927 *tower_size
+= sizeof(*protocol_floor
);
2932 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
2938 const twr_empty_floor_t
*protocol_floor
;
2939 const twr_empty_floor_t
*floor4
;
2940 const struct connection_ops
*protseq_ops
= NULL
;
2944 if (tower_size
< sizeof(*protocol_floor
))
2945 return EPT_S_NOT_REGISTERED
;
2947 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
2948 tower_data
+= sizeof(*protocol_floor
);
2949 tower_size
-= sizeof(*protocol_floor
);
2950 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
2951 (protocol_floor
->count_rhs
> tower_size
))
2952 return EPT_S_NOT_REGISTERED
;
2953 tower_data
+= protocol_floor
->count_rhs
;
2954 tower_size
-= protocol_floor
->count_rhs
;
2956 floor4
= (const twr_empty_floor_t
*)tower_data
;
2957 if ((tower_size
< sizeof(*floor4
)) ||
2958 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
2959 return EPT_S_NOT_REGISTERED
;
2961 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
2962 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
2963 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
2965 protseq_ops
= &conn_protseq_list
[i
];
2970 return EPT_S_NOT_REGISTERED
;
2972 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
2974 if ((status
== RPC_S_OK
) && protseq
)
2976 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
2977 strcpy(*protseq
, protseq_ops
->name
);
2983 /***********************************************************************
2984 * RpcNetworkIsProtseqValidW (RPCRT4.@)
2986 * Checks if the given protocol sequence is known by the RPC system.
2987 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
2990 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
2994 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
2995 ps
, sizeof ps
, NULL
, NULL
);
2996 if (rpcrt4_get_conn_protseq_ops(ps
))
2999 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3001 return RPC_S_INVALID_RPC_PROTSEQ
;
3004 /***********************************************************************
3005 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3007 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3009 UNICODE_STRING protseqW
;
3011 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3013 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3014 RtlFreeUnicodeString(&protseqW
);
3017 return RPC_S_OUT_OF_MEMORY
;