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
| 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
.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_IDENTITY_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
;
227 } else if (err
== ERROR_BAD_NETPATH
) {
228 TRACE("connection failed, error=%x\n", err
);
229 return RPC_S_SERVER_UNAVAILABLE
;
231 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
232 err
= GetLastError();
233 WARN("connection failed, error=%x\n", err
);
234 return RPC_S_SERVER_UNAVAILABLE
;
239 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
240 /* pipe is connected; change to message-read mode. */
241 dwMode
= PIPE_READMODE_MESSAGE
;
242 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
243 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
249 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
251 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
252 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
256 /* already connected? */
260 /* protseq=ncalrpc: supposed to use NT LPC ports,
261 * but we'll implement it with named pipes for now */
262 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
263 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
264 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
270 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, const char *endpoint
)
272 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
275 RpcConnection
*Connection
;
276 char generated_endpoint
[22];
280 static LONG lrpc_nameless_id
;
281 DWORD process_id
= GetCurrentProcessId();
282 ULONG id
= InterlockedIncrement(&lrpc_nameless_id
);
283 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
284 "LRPC%08x.%08x", process_id
, id
);
285 endpoint
= generated_endpoint
;
288 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
289 endpoint
, NULL
, NULL
, NULL
);
293 /* protseq=ncalrpc: supposed to use NT LPC ports,
294 * but we'll implement it with named pipes for now */
295 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
296 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
297 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
300 EnterCriticalSection(&protseq
->cs
);
301 Connection
->Next
= protseq
->conn
;
302 protseq
->conn
= Connection
;
303 LeaveCriticalSection(&protseq
->cs
);
308 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
310 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
311 static const char prefix
[] = "\\\\";
312 static const char local
[] =".";
317 /* already connected? */
321 /* protseq=ncacn_np: named pipes */
322 size
= strlen(prefix
);
323 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
324 size
+= strlen(local
);
326 size
+= strlen(Connection
->NetworkAddr
);
327 size
+= strlen(Connection
->Endpoint
) + 1;
329 pname
= I_RpcAllocate(size
);
330 strcpy(pname
, prefix
);
331 if (Connection
->NetworkAddr
== NULL
|| strlen(Connection
->NetworkAddr
) == 0)
332 strcat(pname
, local
);
334 strcat(pname
, Connection
->NetworkAddr
);
335 strcat(pname
, Connection
->Endpoint
);
336 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
342 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
344 static const char prefix
[] = "\\\\.";
347 RpcConnection
*Connection
;
348 char generated_endpoint
[21];
352 static LONG np_nameless_id
;
353 DWORD process_id
= GetCurrentProcessId();
354 ULONG id
= InterlockedExchangeAdd(&np_nameless_id
, 1 );
355 snprintf(generated_endpoint
, sizeof(generated_endpoint
),
356 "\\\\pipe\\\\%08x.%03x", process_id
, id
);
357 endpoint
= generated_endpoint
;
360 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
361 endpoint
, NULL
, NULL
, NULL
);
365 /* protseq=ncacn_np: named pipes */
366 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
367 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
368 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
371 EnterCriticalSection(&protseq
->cs
);
372 Connection
->Next
= protseq
->conn
;
373 protseq
->conn
= Connection
;
374 LeaveCriticalSection(&protseq
->cs
);
379 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
381 /* because of the way named pipes work, we'll transfer the connected pipe
382 * to the child, then reopen the server binding to continue listening */
384 new_npc
->pipe
= old_npc
->pipe
;
385 new_npc
->ovl
= old_npc
->ovl
;
387 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
388 old_npc
->listening
= FALSE
;
391 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
395 static const char prefix
[] = "\\\\.";
397 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
399 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
400 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
401 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
407 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
411 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
413 TRACE("%s\n", old_conn
->Endpoint
);
415 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
417 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
418 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
419 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
425 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
426 void *buffer
, unsigned int count
)
428 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
431 unsigned int bytes_left
= count
;
434 ZeroMemory(&ovl
, sizeof(ovl
));
435 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
440 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &ovl
);
441 if ((!ret
|| !bytes_read
) && (GetLastError() != ERROR_IO_PENDING
))
443 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_read
, TRUE
);
444 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
446 bytes_left
-= bytes_read
;
449 CloseHandle(ovl
.hEvent
);
450 return ret
? count
: -1;
453 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
454 const void *buffer
, unsigned int count
)
456 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
457 const char *buf
= buffer
;
459 unsigned int bytes_left
= count
;
462 ZeroMemory(&ovl
, sizeof(ovl
));
463 ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
468 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &ovl
);
469 if ((!ret
|| !bytes_written
) && (GetLastError() != ERROR_IO_PENDING
))
471 ret
= GetOverlappedResult(npc
->pipe
, &ovl
, &bytes_written
, TRUE
);
472 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
474 bytes_left
-= bytes_written
;
475 buf
+= bytes_written
;
477 CloseHandle(ovl
.hEvent
);
478 return ret
? count
: -1;
481 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
483 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
485 FlushFileBuffers(npc
->pipe
);
486 CloseHandle(npc
->pipe
);
489 if (npc
->ovl
.hEvent
) {
490 CloseHandle(npc
->ovl
.hEvent
);
496 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
498 /* FIXME: implement when named pipe writes use overlapped I/O */
501 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
503 /* FIXME: implement when named pipe writes use overlapped I/O */
507 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
508 const char *networkaddr
,
509 const char *endpoint
)
511 twr_empty_floor_t
*smb_floor
;
512 twr_empty_floor_t
*nb_floor
;
514 size_t networkaddr_size
;
515 size_t endpoint_size
;
517 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
519 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
520 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
521 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
526 smb_floor
= (twr_empty_floor_t
*)tower_data
;
528 tower_data
+= sizeof(*smb_floor
);
530 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
531 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
532 smb_floor
->count_rhs
= endpoint_size
;
535 memcpy(tower_data
, endpoint
, endpoint_size
);
538 tower_data
+= endpoint_size
;
540 nb_floor
= (twr_empty_floor_t
*)tower_data
;
542 tower_data
+= sizeof(*nb_floor
);
544 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
545 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
546 nb_floor
->count_rhs
= networkaddr_size
;
549 memcpy(tower_data
, networkaddr
, networkaddr_size
);
556 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
561 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
562 const twr_empty_floor_t
*nb_floor
;
564 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
566 if (tower_size
< sizeof(*smb_floor
))
567 return EPT_S_NOT_REGISTERED
;
569 tower_data
+= sizeof(*smb_floor
);
570 tower_size
-= sizeof(*smb_floor
);
572 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
573 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
574 (smb_floor
->count_rhs
> tower_size
) ||
575 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
576 return EPT_S_NOT_REGISTERED
;
580 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
582 return RPC_S_OUT_OF_RESOURCES
;
583 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
585 tower_data
+= smb_floor
->count_rhs
;
586 tower_size
-= smb_floor
->count_rhs
;
588 if (tower_size
< sizeof(*nb_floor
))
589 return EPT_S_NOT_REGISTERED
;
591 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
593 tower_data
+= sizeof(*nb_floor
);
594 tower_size
-= sizeof(*nb_floor
);
596 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
597 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
598 (nb_floor
->count_rhs
> tower_size
) ||
599 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
600 return EPT_S_NOT_REGISTERED
;
604 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
609 I_RpcFree(*endpoint
);
612 return RPC_S_OUT_OF_RESOURCES
;
614 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
620 static RPC_STATUS
rpcrt4_conn_np_impersonate_client(RpcConnection
*conn
)
622 RpcConnection_np
*npc
= (RpcConnection_np
*)conn
;
625 TRACE("(%p)\n", conn
);
627 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
628 return RPCRT4_default_impersonate_client(conn
);
630 ret
= ImpersonateNamedPipeClient(npc
->pipe
);
633 DWORD error
= GetLastError();
634 WARN("ImpersonateNamedPipeClient failed with error %u\n", error
);
637 case ERROR_CANNOT_IMPERSONATE
:
638 return RPC_S_NO_CONTEXT_AVAILABLE
;
644 static RPC_STATUS
rpcrt4_conn_np_revert_to_self(RpcConnection
*conn
)
648 TRACE("(%p)\n", conn
);
650 if (conn
->AuthInfo
&& SecIsValidHandle(&conn
->ctx
))
651 return RPCRT4_default_revert_to_self(conn
);
653 ret
= RevertToSelf();
656 WARN("RevertToSelf failed with error %u\n", GetLastError());
657 return RPC_S_NO_CONTEXT_AVAILABLE
;
662 typedef struct _RpcServerProtseq_np
664 RpcServerProtseq common
;
666 } RpcServerProtseq_np
;
668 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
670 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
672 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
676 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
678 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
679 SetEvent(npps
->mgr_event
);
682 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
684 HANDLE
*objs
= prev_array
;
685 RpcConnection_np
*conn
;
686 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
688 EnterCriticalSection(&protseq
->cs
);
690 /* open and count connections */
692 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
694 rpcrt4_conn_listen_pipe(conn
);
695 if (conn
->ovl
.hEvent
)
697 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
700 /* make array of connections */
702 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
704 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
707 ERR("couldn't allocate objs\n");
708 LeaveCriticalSection(&protseq
->cs
);
712 objs
[0] = npps
->mgr_event
;
714 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
716 if ((objs
[*count
] = conn
->ovl
.hEvent
))
718 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
720 LeaveCriticalSection(&protseq
->cs
);
724 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
726 HeapFree(GetProcessHeap(), 0, array
);
729 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
732 HANDLE
*objs
= wait_array
;
734 RpcConnection
*cconn
;
735 RpcConnection_np
*conn
;
742 /* an alertable wait isn't strictly necessary, but due to our
743 * overlapped I/O implementation in Wine we need to free some memory
744 * by the file user APC being called, even if no completion routine was
745 * specified at the time of starting the async operation */
746 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
747 } while (res
== WAIT_IO_COMPLETION
);
749 if (res
== WAIT_OBJECT_0
)
751 else if (res
== WAIT_FAILED
)
753 ERR("wait failed with error %d\n", GetLastError());
758 b_handle
= objs
[res
- WAIT_OBJECT_0
];
759 /* find which connection got a RPC */
760 EnterCriticalSection(&protseq
->cs
);
761 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
763 if (b_handle
== conn
->ovl
.hEvent
) break;
764 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
768 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
770 ERR("failed to locate connection for handle %p\n", b_handle
);
771 LeaveCriticalSection(&protseq
->cs
);
774 RPCRT4_new_client(cconn
);
781 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
782 const char *networkaddr
,
783 const char *endpoint
)
785 twr_empty_floor_t
*pipe_floor
;
787 size_t endpoint_size
;
789 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
791 endpoint_size
= strlen(endpoint
) + 1;
792 size
= sizeof(*pipe_floor
) + endpoint_size
;
797 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
799 tower_data
+= sizeof(*pipe_floor
);
801 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
802 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
803 pipe_floor
->count_rhs
= endpoint_size
;
805 memcpy(tower_data
, endpoint
, endpoint_size
);
810 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
815 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
817 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
819 if (tower_size
< sizeof(*pipe_floor
))
820 return EPT_S_NOT_REGISTERED
;
822 tower_data
+= sizeof(*pipe_floor
);
823 tower_size
-= sizeof(*pipe_floor
);
825 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
826 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
827 (pipe_floor
->count_rhs
> tower_size
) ||
828 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
829 return EPT_S_NOT_REGISTERED
;
836 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
838 return RPC_S_OUT_OF_RESOURCES
;
839 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
845 static BOOL
rpcrt4_ncalrpc_is_authorized(RpcConnection
*conn
)
850 static RPC_STATUS
rpcrt4_ncalrpc_authorize(RpcConnection
*conn
, BOOL first_time
,
851 unsigned char *in_buffer
,
852 unsigned int in_size
,
853 unsigned char *out_buffer
,
854 unsigned int *out_size
)
856 /* since this protocol is local to the machine there is no need to
857 * authenticate the caller */
862 static RPC_STATUS
rpcrt4_ncalrpc_secure_packet(RpcConnection
*conn
,
863 enum secure_packet_direction dir
,
864 RpcPktHdr
*hdr
, unsigned int hdr_size
,
865 unsigned char *stub_data
, unsigned int stub_data_size
,
866 RpcAuthVerifier
*auth_hdr
,
867 unsigned char *auth_value
, unsigned int auth_value_size
)
869 /* since this protocol is local to the machine there is no need to secure
874 static RPC_STATUS
rpcrt4_ncalrpc_inquire_auth_client(
875 RpcConnection
*conn
, RPC_AUTHZ_HANDLE
*privs
, RPC_WSTR
*server_princ_name
,
876 ULONG
*authn_level
, ULONG
*authn_svc
, ULONG
*authz_svc
, ULONG flags
)
878 TRACE("(%p, %p, %p, %p, %p, %p, 0x%x)\n", conn
, privs
,
879 server_princ_name
, authn_level
, authn_svc
, authz_svc
, flags
);
883 FIXME("privs not implemented\n");
886 if (server_princ_name
)
888 FIXME("server_princ_name not implemented\n");
889 *server_princ_name
= NULL
;
891 if (authn_level
) *authn_level
= RPC_C_AUTHN_LEVEL_PKT_PRIVACY
;
892 if (authn_svc
) *authn_svc
= RPC_C_AUTHN_WINNT
;
895 FIXME("authorization service not implemented\n");
896 *authz_svc
= RPC_C_AUTHZ_NONE
;
899 FIXME("flags 0x%x not implemented\n", flags
);
904 /**** ncacn_ip_tcp support ****/
906 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
907 const char *networkaddr
,
908 unsigned char tcp_protid
,
909 const char *endpoint
)
911 twr_tcp_floor_t
*tcp_floor
;
912 twr_ipv4_floor_t
*ipv4_floor
;
914 struct addrinfo hints
;
916 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
918 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
923 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
924 tower_data
+= sizeof(*tcp_floor
);
926 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
928 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
929 tcp_floor
->protid
= tcp_protid
;
930 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
932 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
933 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
934 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
936 hints
.ai_flags
= AI_NUMERICHOST
;
937 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
938 hints
.ai_family
= PF_INET
;
939 hints
.ai_socktype
= SOCK_STREAM
;
940 hints
.ai_protocol
= IPPROTO_TCP
;
941 hints
.ai_addrlen
= 0;
942 hints
.ai_addr
= NULL
;
943 hints
.ai_canonname
= NULL
;
944 hints
.ai_next
= NULL
;
946 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
949 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
952 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
957 if (ai
->ai_family
== PF_INET
)
959 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
960 tcp_floor
->port
= sin
->sin_port
;
961 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
965 ERR("unexpected protocol family %d\n", ai
->ai_family
);
974 static RPC_STATUS
rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
977 unsigned char tcp_protid
,
980 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
981 const twr_ipv4_floor_t
*ipv4_floor
;
982 struct in_addr in_addr
;
984 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
986 if (tower_size
< sizeof(*tcp_floor
))
987 return EPT_S_NOT_REGISTERED
;
989 tower_data
+= sizeof(*tcp_floor
);
990 tower_size
-= sizeof(*tcp_floor
);
992 if (tower_size
< sizeof(*ipv4_floor
))
993 return EPT_S_NOT_REGISTERED
;
995 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
997 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
998 (tcp_floor
->protid
!= tcp_protid
) ||
999 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1000 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1001 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1002 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1003 return EPT_S_NOT_REGISTERED
;
1007 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1009 return RPC_S_OUT_OF_RESOURCES
;
1010 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1015 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1020 I_RpcFree(*endpoint
);
1023 return RPC_S_OUT_OF_RESOURCES
;
1025 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1026 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1028 ERR("inet_ntop: %s\n", strerror(errno
));
1029 I_RpcFree(*networkaddr
);
1030 *networkaddr
= NULL
;
1033 I_RpcFree(*endpoint
);
1036 return EPT_S_NOT_REGISTERED
;
1043 typedef struct _RpcConnection_tcp
1045 RpcConnection common
;
1047 #ifdef HAVE_SOCKETPAIR
1051 HANDLE cancel_event
;
1053 } RpcConnection_tcp
;
1055 #ifdef HAVE_SOCKETPAIR
1057 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1059 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
1061 ERR("socketpair() failed: %s\n", strerror(errno
));
1067 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1069 struct pollfd pfds
[2];
1070 pfds
[0].fd
= tcpc
->sock
;
1071 pfds
[0].events
= POLLIN
;
1072 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1073 pfds
[1].events
= POLLIN
;
1074 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1076 ERR("poll() failed: %s\n", strerror(errno
));
1079 if (pfds
[1].revents
& POLLIN
) /* canceled */
1082 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1088 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1091 pfd
.fd
= tcpc
->sock
;
1092 pfd
.events
= POLLOUT
;
1093 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1095 ERR("poll() failed: %s\n", strerror(errno
));
1101 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1105 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1108 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1110 close(tcpc
->cancel_fds
[0]);
1111 close(tcpc
->cancel_fds
[1]);
1114 #else /* HAVE_SOCKETPAIR */
1116 static BOOL
rpcrt4_sock_wait_init(RpcConnection_tcp
*tcpc
)
1118 static BOOL wsa_inited
;
1122 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1123 /* Note: WSAStartup can be called more than once so we don't bother with
1124 * making accesses to wsa_inited thread-safe */
1127 tcpc
->sock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1128 tcpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1129 if (!tcpc
->sock_event
|| !tcpc
->cancel_event
)
1131 ERR("event creation failed\n");
1132 if (tcpc
->sock_event
) CloseHandle(tcpc
->sock_event
);
1138 static BOOL
rpcrt4_sock_wait_for_recv(RpcConnection_tcp
*tcpc
)
1140 HANDLE wait_handles
[2];
1142 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_READ
| FD_CLOSE
) == SOCKET_ERROR
)
1144 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1147 wait_handles
[0] = tcpc
->sock_event
;
1148 wait_handles
[1] = tcpc
->cancel_event
;
1149 res
= WaitForMultipleObjects(2, wait_handles
, FALSE
, INFINITE
);
1154 case WAIT_OBJECT_0
+ 1:
1157 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1162 static BOOL
rpcrt4_sock_wait_for_send(RpcConnection_tcp
*tcpc
)
1165 if (WSAEventSelect(tcpc
->sock
, tcpc
->sock_event
, FD_WRITE
| FD_CLOSE
) == SOCKET_ERROR
)
1167 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1170 res
= WaitForSingleObject(tcpc
->sock_event
, INFINITE
);
1176 ERR("WaitForMultipleObjects() failed with error %d\n", GetLastError());
1181 static void rpcrt4_sock_wait_cancel(RpcConnection_tcp
*tcpc
)
1183 SetEvent(tcpc
->cancel_event
);
1186 static void rpcrt4_sock_wait_destroy(RpcConnection_tcp
*tcpc
)
1188 CloseHandle(tcpc
->sock_event
);
1189 CloseHandle(tcpc
->cancel_event
);
1194 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
1196 RpcConnection_tcp
*tcpc
;
1197 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
1201 if (!rpcrt4_sock_wait_init(tcpc
))
1203 HeapFree(GetProcessHeap(), 0, tcpc
);
1206 return &tcpc
->common
;
1209 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
1211 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1214 struct addrinfo
*ai
;
1215 struct addrinfo
*ai_cur
;
1216 struct addrinfo hints
;
1218 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1220 if (tcpc
->sock
!= -1)
1224 hints
.ai_family
= PF_UNSPEC
;
1225 hints
.ai_socktype
= SOCK_STREAM
;
1226 hints
.ai_protocol
= IPPROTO_TCP
;
1227 hints
.ai_addrlen
= 0;
1228 hints
.ai_addr
= NULL
;
1229 hints
.ai_canonname
= NULL
;
1230 hints
.ai_next
= NULL
;
1232 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
1235 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
1236 Connection
->Endpoint
, gai_strerror(ret
));
1237 return RPC_S_SERVER_UNAVAILABLE
;
1240 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1245 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1247 TRACE("skipping non-IP/IPv6 address family\n");
1255 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1256 host
, sizeof(host
), service
, sizeof(service
),
1257 NI_NUMERICHOST
| NI_NUMERICSERV
);
1258 TRACE("trying %s:%s\n", host
, service
);
1261 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1264 WARN("socket() failed: %s\n", strerror(errno
));
1268 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
1270 WARN("connect() failed: %s\n", strerror(errno
));
1275 /* RPC depends on having minimal latency so disable the Nagle algorithm */
1277 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1279 ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1284 TRACE("connected\n");
1289 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
1290 return RPC_S_SERVER_UNAVAILABLE
;
1293 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, const char *endpoint
)
1295 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
1298 struct addrinfo
*ai
;
1299 struct addrinfo
*ai_cur
;
1300 struct addrinfo hints
;
1301 RpcConnection
*first_connection
= NULL
;
1303 TRACE("(%p, %s)\n", protseq
, endpoint
);
1305 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
1306 hints
.ai_family
= PF_UNSPEC
;
1307 hints
.ai_socktype
= SOCK_STREAM
;
1308 hints
.ai_protocol
= IPPROTO_TCP
;
1309 hints
.ai_addrlen
= 0;
1310 hints
.ai_addr
= NULL
;
1311 hints
.ai_canonname
= NULL
;
1312 hints
.ai_next
= NULL
;
1314 ret
= getaddrinfo(NULL
, endpoint
? endpoint
: "0", &hints
, &ai
);
1317 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
1319 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
1320 return RPC_S_INVALID_ENDPOINT_FORMAT
;
1321 return RPC_S_CANT_CREATE_ENDPOINT
;
1324 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
1326 RpcConnection_tcp
*tcpc
;
1327 RPC_STATUS create_status
;
1328 struct sockaddr_storage sa
;
1330 char service
[NI_MAXSERV
];
1333 if (ai_cur
->ai_family
!= AF_INET
&& ai_cur
->ai_family
!= AF_INET6
)
1335 TRACE("skipping non-IP/IPv6 address family\n");
1342 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
1343 host
, sizeof(host
), service
, sizeof(service
),
1344 NI_NUMERICHOST
| NI_NUMERICSERV
);
1345 TRACE("trying %s:%s\n", host
, service
);
1348 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
1351 WARN("socket() failed: %s\n", strerror(errno
));
1352 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1356 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
1359 WARN("bind failed: %s\n", strerror(errno
));
1361 if (errno
== EADDRINUSE
)
1362 status
= RPC_S_DUPLICATE_ENDPOINT
;
1364 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1368 sa_len
= sizeof(sa
);
1369 if (getsockname(sock
, (struct sockaddr
*)&sa
, &sa_len
))
1371 WARN("getsockname() failed: %s\n", strerror(errno
));
1372 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1376 ret
= getnameinfo((struct sockaddr
*)&sa
, sa_len
,
1377 NULL
, 0, service
, sizeof(service
),
1381 WARN("getnameinfo failed: %s\n", gai_strerror(ret
));
1382 status
= RPC_S_CANT_CREATE_ENDPOINT
;
1386 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
1387 protseq
->Protseq
, NULL
,
1388 service
, NULL
, NULL
, NULL
);
1389 if (create_status
!= RPC_S_OK
)
1392 status
= create_status
;
1397 ret
= listen(sock
, protseq
->MaxCalls
);
1400 WARN("listen failed: %s\n", strerror(errno
));
1401 RPCRT4_DestroyConnection(&tcpc
->common
);
1402 status
= RPC_S_OUT_OF_RESOURCES
;
1405 /* need a non-blocking socket, otherwise accept() has a potential
1406 * race-condition (poll() says it is readable, connection drops,
1407 * and accept() blocks until the next connection comes...)
1410 ret
= ioctlsocket(sock
, FIONBIO
, &nonblocking
);
1413 WARN("couldn't make socket non-blocking, error %d\n", ret
);
1414 RPCRT4_DestroyConnection(&tcpc
->common
);
1415 status
= RPC_S_OUT_OF_RESOURCES
;
1419 tcpc
->common
.Next
= first_connection
;
1420 first_connection
= &tcpc
->common
;
1422 /* since IPv4 and IPv6 share the same port space, we only need one
1423 * successful bind to listen for both */
1429 /* if at least one connection was created for an endpoint then
1431 if (first_connection
)
1433 RpcConnection
*conn
;
1435 /* find last element in list */
1436 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
1439 EnterCriticalSection(&protseq
->cs
);
1440 conn
->Next
= protseq
->conn
;
1441 protseq
->conn
= first_connection
;
1442 LeaveCriticalSection(&protseq
->cs
);
1444 TRACE("listening on %s\n", endpoint
);
1448 ERR("couldn't listen on port %s\n", endpoint
);
1452 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
1455 struct sockaddr_in address
;
1457 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
1458 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
1461 addrsize
= sizeof(address
);
1462 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
1465 ERR("Failed to accept a TCP connection: error %d\n", ret
);
1466 return RPC_S_OUT_OF_RESOURCES
;
1469 ioctlsocket(ret
, FIONBIO
, &nonblocking
);
1471 TRACE("Accepted a new TCP connection\n");
1475 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1476 void *buffer
, unsigned int count
)
1478 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1480 while (bytes_read
!= count
)
1482 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1487 else if (errno
!= EAGAIN
)
1489 WARN("recv() failed: %s\n", strerror(errno
));
1494 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1498 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1502 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1503 const void *buffer
, unsigned int count
)
1505 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1506 int bytes_written
= 0;
1507 while (bytes_written
!= count
)
1509 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1512 else if (errno
!= EAGAIN
)
1516 if (!rpcrt4_sock_wait_for_send(tcpc
))
1520 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1521 return bytes_written
;
1524 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1526 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1528 TRACE("%d\n", tcpc
->sock
);
1530 if (tcpc
->sock
!= -1)
1531 closesocket(tcpc
->sock
);
1533 rpcrt4_sock_wait_destroy(tcpc
);
1537 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1539 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1540 TRACE("%p\n", Connection
);
1541 rpcrt4_sock_wait_cancel(tcpc
);
1544 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1546 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1548 TRACE("%p\n", Connection
);
1550 if (!rpcrt4_sock_wait_for_recv(tcpc
))
1555 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1556 const char *networkaddr
,
1557 const char *endpoint
)
1559 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
1560 EPM_PROTOCOL_TCP
, endpoint
);
1563 #ifdef HAVE_SOCKETPAIR
1565 typedef struct _RpcServerProtseq_sock
1567 RpcServerProtseq common
;
1570 } RpcServerProtseq_sock
;
1572 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1574 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1578 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1580 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1581 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1582 ps
->mgr_event_rcv
= fds
[0];
1583 ps
->mgr_event_snd
= fds
[1];
1587 ERR("socketpair failed with error %s\n", strerror(errno
));
1588 HeapFree(GetProcessHeap(), 0, ps
);
1595 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1597 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1599 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1602 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1604 struct pollfd
*poll_info
= prev_array
;
1605 RpcConnection_tcp
*conn
;
1606 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1608 EnterCriticalSection(&protseq
->cs
);
1610 /* open and count connections */
1612 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1614 if (conn
->sock
!= -1)
1616 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1619 /* make array of connections */
1621 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1623 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1626 ERR("couldn't allocate poll_info\n");
1627 LeaveCriticalSection(&protseq
->cs
);
1631 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1632 poll_info
[0].events
= POLLIN
;
1634 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1636 if (conn
->sock
!= -1)
1638 poll_info
[*count
].fd
= conn
->sock
;
1639 poll_info
[*count
].events
= POLLIN
;
1642 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1644 LeaveCriticalSection(&protseq
->cs
);
1648 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1650 HeapFree(GetProcessHeap(), 0, array
);
1653 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1655 struct pollfd
*poll_info
= wait_array
;
1658 RpcConnection
*cconn
;
1659 RpcConnection_tcp
*conn
;
1664 ret
= poll(poll_info
, count
, -1);
1667 ERR("poll failed with error %d\n", ret
);
1671 for (i
= 0; i
< count
; i
++)
1672 if (poll_info
[i
].revents
& POLLIN
)
1674 /* RPC server event */
1678 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1682 /* find which connection got a RPC */
1683 EnterCriticalSection(&protseq
->cs
);
1684 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1686 if (poll_info
[i
].fd
== conn
->sock
) break;
1687 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1691 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1693 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1694 LeaveCriticalSection(&protseq
->cs
);
1696 RPCRT4_new_client(cconn
);
1704 #else /* HAVE_SOCKETPAIR */
1706 typedef struct _RpcServerProtseq_sock
1708 RpcServerProtseq common
;
1710 } RpcServerProtseq_sock
;
1712 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1714 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1717 static BOOL wsa_inited
;
1721 WSAStartup(MAKEWORD(2, 2), &wsadata
);
1722 /* Note: WSAStartup can be called more than once so we don't bother with
1723 * making accesses to wsa_inited thread-safe */
1726 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1731 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1733 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1734 SetEvent(sockps
->mgr_event
);
1737 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1739 HANDLE
*objs
= prev_array
;
1740 RpcConnection_tcp
*conn
;
1741 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1743 EnterCriticalSection(&protseq
->cs
);
1745 /* open and count connections */
1747 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1750 if (conn
->sock
!= -1)
1752 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1755 /* make array of connections */
1757 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
1759 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
1762 ERR("couldn't allocate objs\n");
1763 LeaveCriticalSection(&protseq
->cs
);
1767 objs
[0] = sockps
->mgr_event
;
1769 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1772 if (conn
->sock
!= -1)
1774 int res
= WSAEventSelect(conn
->sock
, conn
->sock_event
, FD_ACCEPT
);
1775 if (res
== SOCKET_ERROR
)
1776 ERR("WSAEventSelect() failed with error %d\n", WSAGetLastError());
1779 objs
[*count
] = conn
->sock_event
;
1783 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1785 LeaveCriticalSection(&protseq
->cs
);
1789 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1791 HeapFree(GetProcessHeap(), 0, array
);
1794 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1797 HANDLE
*objs
= wait_array
;
1799 RpcConnection
*cconn
;
1800 RpcConnection_tcp
*conn
;
1807 /* an alertable wait isn't strictly necessary, but due to our
1808 * overlapped I/O implementation in Wine we need to free some memory
1809 * by the file user APC being called, even if no completion routine was
1810 * specified at the time of starting the async operation */
1811 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
1812 } while (res
== WAIT_IO_COMPLETION
);
1814 if (res
== WAIT_OBJECT_0
)
1816 else if (res
== WAIT_FAILED
)
1818 ERR("wait failed with error %d\n", GetLastError());
1823 b_handle
= objs
[res
- WAIT_OBJECT_0
];
1824 /* find which connection got a RPC */
1825 EnterCriticalSection(&protseq
->cs
);
1826 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1829 if (b_handle
== conn
->sock_event
) break;
1830 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1834 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1836 ERR("failed to locate connection for handle %p\n", b_handle
);
1837 LeaveCriticalSection(&protseq
->cs
);
1840 RPCRT4_new_client(cconn
);
1847 #endif /* HAVE_SOCKETPAIR */
1849 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1854 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
1855 networkaddr
, EPM_PROTOCOL_TCP
,
1859 /**** ncacn_http support ****/
1861 /* 60 seconds is the period native uses */
1862 #define HTTP_IDLE_TIME 60000
1864 /* reference counted to avoid a race between a cancelled call's connection
1865 * being destroyed and the asynchronous InternetReadFileEx call being
1867 typedef struct _RpcHttpAsyncData
1870 HANDLE completion_event
;
1871 INTERNET_BUFFERSA inet_buffers
;
1872 void *destination_buffer
; /* the address that inet_buffers.lpvBuffer will be
1873 * copied into when the call completes */
1874 CRITICAL_SECTION cs
;
1877 static ULONG
RpcHttpAsyncData_AddRef(RpcHttpAsyncData
*data
)
1879 return InterlockedIncrement(&data
->refs
);
1882 static ULONG
RpcHttpAsyncData_Release(RpcHttpAsyncData
*data
)
1884 ULONG refs
= InterlockedDecrement(&data
->refs
);
1887 TRACE("destroying async data %p\n", data
);
1888 CloseHandle(data
->completion_event
);
1889 HeapFree(GetProcessHeap(), 0, data
->inet_buffers
.lpvBuffer
);
1890 DeleteCriticalSection(&data
->cs
);
1891 HeapFree(GetProcessHeap(), 0, data
);
1896 typedef struct _RpcConnection_http
1898 RpcConnection common
;
1901 HINTERNET in_request
;
1902 HINTERNET out_request
;
1903 HANDLE timer_cancelled
;
1904 HANDLE cancel_event
;
1905 DWORD last_sent_time
;
1906 ULONG bytes_received
;
1907 ULONG flow_control_mark
; /* send a control packet to the server when this many bytes received */
1908 ULONG flow_control_increment
; /* number of bytes to increment flow_control_mark by */
1909 UUID connection_uuid
;
1912 RpcHttpAsyncData
*async_data
;
1913 } RpcConnection_http
;
1915 static RpcConnection
*rpcrt4_ncacn_http_alloc(void)
1917 RpcConnection_http
*httpc
;
1918 httpc
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*httpc
));
1919 if (!httpc
) return NULL
;
1920 httpc
->async_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(RpcHttpAsyncData
));
1921 if (!httpc
->async_data
)
1923 HeapFree(GetProcessHeap(), 0, httpc
);
1926 TRACE("async data = %p\n", httpc
->async_data
);
1927 httpc
->cancel_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1928 httpc
->async_data
->refs
= 1;
1929 httpc
->async_data
->inet_buffers
.dwStructSize
= sizeof(INTERNET_BUFFERSA
);
1930 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
1931 httpc
->async_data
->destination_buffer
= NULL
;
1932 InitializeCriticalSection(&httpc
->async_data
->cs
);
1933 return &httpc
->common
;
1936 typedef struct _HttpTimerThreadData
1939 DWORD
*last_sent_time
;
1940 HANDLE timer_cancelled
;
1941 } HttpTimerThreadData
;
1943 static VOID
rpcrt4_http_keep_connection_active_timer_proc(PVOID param
, BOOLEAN dummy
)
1945 HINTERNET in_request
= param
;
1946 RpcPktHdr
*idle_pkt
;
1948 idle_pkt
= RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION
, 0x0001,
1952 DWORD bytes_written
;
1953 InternetWriteFile(in_request
, idle_pkt
, idle_pkt
->common
.frag_len
, &bytes_written
);
1954 RPCRT4_FreeHeader(idle_pkt
);
1958 static inline DWORD
rpcrt4_http_timer_calc_timeout(DWORD
*last_sent_time
)
1960 DWORD cur_time
= GetTickCount();
1961 DWORD cached_last_sent_time
= *last_sent_time
;
1962 return HTTP_IDLE_TIME
- (cur_time
- cached_last_sent_time
> HTTP_IDLE_TIME
? 0 : cur_time
- cached_last_sent_time
);
1965 static DWORD CALLBACK
rpcrt4_http_timer_thread(PVOID param
)
1967 HttpTimerThreadData
*data_in
= param
;
1968 HttpTimerThreadData data
;
1972 HeapFree(GetProcessHeap(), 0, data_in
);
1974 for (timeout
= HTTP_IDLE_TIME
;
1975 WaitForSingleObject(data
.timer_cancelled
, timeout
) == WAIT_TIMEOUT
;
1976 timeout
= rpcrt4_http_timer_calc_timeout(data
.last_sent_time
))
1978 /* are we too soon after last send? */
1979 if (GetTickCount() - HTTP_IDLE_TIME
< *data
.last_sent_time
)
1981 rpcrt4_http_keep_connection_active_timer_proc(data
.timer_param
, TRUE
);
1984 CloseHandle(data
.timer_cancelled
);
1988 static VOID WINAPI
rpcrt4_http_internet_callback(
1989 HINTERNET hInternet
,
1990 DWORD_PTR dwContext
,
1991 DWORD dwInternetStatus
,
1992 LPVOID lpvStatusInformation
,
1993 DWORD dwStatusInformationLength
)
1995 RpcHttpAsyncData
*async_data
= (RpcHttpAsyncData
*)dwContext
;
1997 switch (dwInternetStatus
)
1999 case INTERNET_STATUS_REQUEST_COMPLETE
:
2000 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
2003 if (async_data
->inet_buffers
.lpvBuffer
)
2005 EnterCriticalSection(&async_data
->cs
);
2006 if (async_data
->destination_buffer
)
2008 memcpy(async_data
->destination_buffer
,
2009 async_data
->inet_buffers
.lpvBuffer
,
2010 async_data
->inet_buffers
.dwBufferLength
);
2011 async_data
->destination_buffer
= NULL
;
2013 LeaveCriticalSection(&async_data
->cs
);
2015 HeapFree(GetProcessHeap(), 0, async_data
->inet_buffers
.lpvBuffer
);
2016 async_data
->inet_buffers
.lpvBuffer
= NULL
;
2017 SetEvent(async_data
->completion_event
);
2018 RpcHttpAsyncData_Release(async_data
);
2024 static RPC_STATUS
rpcrt4_http_check_response(HINTERNET hor
)
2031 WCHAR
*status_text
= buf
;
2035 size
= sizeof(status_code
);
2036 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_CODE
|HTTP_QUERY_FLAG_NUMBER
, &status_code
, &size
, &index
);
2038 return GetLastError();
2039 if (status_code
< 400)
2043 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2044 if (!ret
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2046 status_text
= HeapAlloc(GetProcessHeap(), 0, size
);
2047 ret
= HttpQueryInfoW(hor
, HTTP_QUERY_STATUS_TEXT
, status_text
, &size
, &index
);
2050 ERR("server returned: %d %s\n", status_code
, ret
? debugstr_w(status_text
) : "<status text unavailable>");
2051 if(status_text
!= buf
) HeapFree(GetProcessHeap(), 0, status_text
);
2053 if (status_code
== HTTP_STATUS_DENIED
)
2054 return ERROR_ACCESS_DENIED
;
2055 return RPC_S_SERVER_UNAVAILABLE
;
2058 static RPC_STATUS
rpcrt4_http_internet_connect(RpcConnection_http
*httpc
)
2060 static const WCHAR wszUserAgent
[] = {'M','S','R','P','C',0};
2061 LPWSTR proxy
= NULL
;
2063 LPWSTR password
= NULL
;
2064 LPWSTR servername
= NULL
;
2065 const WCHAR
*option
;
2066 INTERNET_PORT port
= INTERNET_INVALID_PORT_NUMBER
; /* use default port */
2068 if (httpc
->common
.QOS
&&
2069 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
))
2071 const RPC_HTTP_TRANSPORT_CREDENTIALS_W
*http_cred
= httpc
->common
.QOS
->qos
->u
.HttpCredentials
;
2072 if (http_cred
->TransportCredentials
)
2075 const SEC_WINNT_AUTH_IDENTITY_W
*cred
= http_cred
->TransportCredentials
;
2076 ULONG len
= cred
->DomainLength
+ 1 + cred
->UserLength
;
2077 user
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2079 return RPC_S_OUT_OF_RESOURCES
;
2081 if (cred
->DomainLength
)
2083 memcpy(p
, cred
->Domain
, cred
->DomainLength
* sizeof(WCHAR
));
2084 p
+= cred
->DomainLength
;
2088 memcpy(p
, cred
->User
, cred
->UserLength
* sizeof(WCHAR
));
2089 p
[cred
->UserLength
] = 0;
2091 password
= RPCRT4_strndupW(cred
->Password
, cred
->PasswordLength
);
2095 for (option
= httpc
->common
.NetworkOptions
; option
;
2096 option
= (strchrW(option
, ',') ? strchrW(option
, ',')+1 : NULL
))
2098 static const WCHAR wszRpcProxy
[] = {'R','p','c','P','r','o','x','y','=',0};
2099 static const WCHAR wszHttpProxy
[] = {'H','t','t','p','P','r','o','x','y','=',0};
2101 if (!strncmpiW(option
, wszRpcProxy
, sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1))
2103 const WCHAR
*value_start
= option
+ sizeof(wszRpcProxy
)/sizeof(wszRpcProxy
[0])-1;
2104 const WCHAR
*value_end
;
2107 value_end
= strchrW(option
, ',');
2109 value_end
= value_start
+ strlenW(value_start
);
2110 for (p
= value_start
; p
< value_end
; p
++)
2117 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2118 servername
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2120 else if (!strncmpiW(option
, wszHttpProxy
, sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1))
2122 const WCHAR
*value_start
= option
+ sizeof(wszHttpProxy
)/sizeof(wszHttpProxy
[0])-1;
2123 const WCHAR
*value_end
;
2125 value_end
= strchrW(option
, ',');
2127 value_end
= value_start
+ strlenW(value_start
);
2128 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start
, value_end
-value_start
));
2129 proxy
= RPCRT4_strndupW(value_start
, value_end
-value_start
);
2132 FIXME("unhandled option %s\n", debugstr_w(option
));
2135 httpc
->app_info
= InternetOpenW(wszUserAgent
, proxy
? INTERNET_OPEN_TYPE_PROXY
: INTERNET_OPEN_TYPE_PRECONFIG
,
2136 NULL
, NULL
, INTERNET_FLAG_ASYNC
);
2137 if (!httpc
->app_info
)
2139 HeapFree(GetProcessHeap(), 0, password
);
2140 HeapFree(GetProcessHeap(), 0, user
);
2141 ERR("InternetOpenW failed with error %d\n", GetLastError());
2142 return RPC_S_SERVER_UNAVAILABLE
;
2144 InternetSetStatusCallbackW(httpc
->app_info
, rpcrt4_http_internet_callback
);
2146 /* if no RpcProxy option specified, set the HTTP server address to the
2147 * RPC server address */
2150 servername
= HeapAlloc(GetProcessHeap(), 0, (strlen(httpc
->common
.NetworkAddr
) + 1)*sizeof(WCHAR
));
2153 HeapFree(GetProcessHeap(), 0, password
);
2154 HeapFree(GetProcessHeap(), 0, user
);
2155 return RPC_S_OUT_OF_RESOURCES
;
2157 MultiByteToWideChar(CP_ACP
, 0, httpc
->common
.NetworkAddr
, -1, servername
, strlen(httpc
->common
.NetworkAddr
) + 1);
2160 httpc
->session
= InternetConnectW(httpc
->app_info
, servername
, port
, user
, password
,
2161 INTERNET_SERVICE_HTTP
, 0, 0);
2163 HeapFree(GetProcessHeap(), 0, password
);
2164 HeapFree(GetProcessHeap(), 0, user
);
2165 HeapFree(GetProcessHeap(), 0, servername
);
2167 if (!httpc
->session
)
2169 ERR("InternetConnectW failed with error %d\n", GetLastError());
2170 return RPC_S_SERVER_UNAVAILABLE
;
2176 /* prepare the in pipe for use by RPC packets */
2177 static RPC_STATUS
rpcrt4_http_prepare_in_pipe(HINTERNET in_request
, RpcHttpAsyncData
*async_data
,
2178 const UUID
*connection_uuid
,
2179 const UUID
*in_pipe_uuid
,
2180 const UUID
*association_uuid
)
2186 INTERNET_BUFFERSW buffers_in
;
2187 DWORD bytes_read
, bytes_written
;
2189 /* prepare in pipe */
2190 ResetEvent(async_data
->completion_event
);
2191 RpcHttpAsyncData_AddRef(async_data
);
2192 ret
= HttpSendRequestW(in_request
, NULL
, 0, NULL
, 0);
2195 if (GetLastError() == ERROR_IO_PENDING
)
2196 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2199 RpcHttpAsyncData_Release(async_data
);
2200 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2201 return RPC_S_SERVER_UNAVAILABLE
;
2204 status
= rpcrt4_http_check_response(in_request
);
2205 if (status
!= RPC_S_OK
) return status
;
2207 InternetReadFile(in_request
, packet
, 20, &bytes_read
);
2208 /* FIXME: do something with retrieved data */
2210 memset(&buffers_in
, 0, sizeof(buffers_in
));
2211 buffers_in
.dwStructSize
= sizeof(buffers_in
);
2212 /* FIXME: get this from the registry */
2213 buffers_in
.dwBufferTotal
= 1024 * 1024 * 1024; /* 1Gb */
2214 ResetEvent(async_data
->completion_event
);
2215 RpcHttpAsyncData_AddRef(async_data
);
2216 ret
= HttpSendRequestExW(in_request
, &buffers_in
, NULL
, 0, 0);
2219 if (GetLastError() == ERROR_IO_PENDING
)
2220 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2223 RpcHttpAsyncData_Release(async_data
);
2224 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
2225 return RPC_S_SERVER_UNAVAILABLE
;
2229 TRACE("sending HTTP connect header to server\n");
2230 hdr
= RPCRT4_BuildHttpConnectHeader(0, FALSE
, connection_uuid
, in_pipe_uuid
, association_uuid
);
2231 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2232 ret
= InternetWriteFile(in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2233 RPCRT4_FreeHeader(hdr
);
2236 ERR("InternetWriteFile failed with error %d\n", GetLastError());
2237 return RPC_S_SERVER_UNAVAILABLE
;
2243 static RPC_STATUS
rpcrt4_http_read_http_packet(HINTERNET request
, RpcPktHdr
*hdr
, BYTE
**data
)
2247 unsigned short data_len
;
2249 ret
= InternetReadFile(request
, hdr
, sizeof(hdr
->common
), &bytes_read
);
2251 return RPC_S_SERVER_UNAVAILABLE
;
2252 if (hdr
->common
.ptype
!= PKT_HTTP
|| hdr
->common
.frag_len
< sizeof(hdr
->http
))
2254 ERR("wrong packet type received %d or wrong frag_len %d\n",
2255 hdr
->common
.ptype
, hdr
->common
.frag_len
);
2256 return RPC_S_PROTOCOL_ERROR
;
2259 ret
= InternetReadFile(request
, &hdr
->common
+ 1, sizeof(hdr
->http
) - sizeof(hdr
->common
), &bytes_read
);
2261 return RPC_S_SERVER_UNAVAILABLE
;
2263 data_len
= hdr
->common
.frag_len
- sizeof(hdr
->http
);
2266 *data
= HeapAlloc(GetProcessHeap(), 0, data_len
);
2268 return RPC_S_OUT_OF_RESOURCES
;
2269 ret
= InternetReadFile(request
, *data
, data_len
, &bytes_read
);
2272 HeapFree(GetProcessHeap(), 0, *data
);
2273 return RPC_S_SERVER_UNAVAILABLE
;
2279 if (!RPCRT4_IsValidHttpPacket(hdr
, *data
, data_len
))
2281 ERR("invalid http packet\n");
2282 return RPC_S_PROTOCOL_ERROR
;
2288 /* prepare the out pipe for use by RPC packets */
2289 static RPC_STATUS
rpcrt4_http_prepare_out_pipe(HINTERNET out_request
,
2290 RpcHttpAsyncData
*async_data
,
2291 const UUID
*connection_uuid
,
2292 const UUID
*out_pipe_uuid
,
2293 ULONG
*flow_control_increment
)
2300 BYTE
*data_from_server
;
2301 RpcPktHdr pkt_from_server
;
2302 ULONG field1
, field3
;
2304 ResetEvent(async_data
->completion_event
);
2305 RpcHttpAsyncData_AddRef(async_data
);
2306 ret
= HttpSendRequestW(out_request
, NULL
, 0, NULL
, 0);
2309 if (GetLastError() == ERROR_IO_PENDING
)
2310 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2313 RpcHttpAsyncData_Release(async_data
);
2314 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2315 return RPC_S_SERVER_UNAVAILABLE
;
2318 status
= rpcrt4_http_check_response(out_request
);
2319 if (status
!= RPC_S_OK
) return status
;
2321 InternetReadFile(out_request
, packet
, 20, &bytes_read
);
2322 /* FIXME: do something with retrieved data */
2324 hdr
= RPCRT4_BuildHttpConnectHeader(0, TRUE
, connection_uuid
, out_pipe_uuid
, NULL
);
2325 if (!hdr
) return RPC_S_OUT_OF_RESOURCES
;
2326 ResetEvent(async_data
->completion_event
);
2327 RpcHttpAsyncData_AddRef(async_data
);
2328 ret
= HttpSendRequestW(out_request
, NULL
, 0, hdr
, hdr
->common
.frag_len
);
2331 if (GetLastError() == ERROR_IO_PENDING
)
2332 WaitForSingleObject(async_data
->completion_event
, INFINITE
);
2335 RpcHttpAsyncData_Release(async_data
);
2336 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
2337 RPCRT4_FreeHeader(hdr
);
2338 return RPC_S_SERVER_UNAVAILABLE
;
2341 RPCRT4_FreeHeader(hdr
);
2342 status
= rpcrt4_http_check_response(out_request
);
2343 if (status
!= RPC_S_OK
) return status
;
2345 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2347 if (status
!= RPC_S_OK
) return status
;
2348 status
= RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server
, data_from_server
,
2350 HeapFree(GetProcessHeap(), 0, data_from_server
);
2351 if (status
!= RPC_S_OK
) return status
;
2352 TRACE("received (%d) from first prepare header\n", field1
);
2354 status
= rpcrt4_http_read_http_packet(out_request
, &pkt_from_server
,
2356 if (status
!= RPC_S_OK
) return status
;
2357 status
= RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server
, data_from_server
,
2358 &field1
, flow_control_increment
,
2360 HeapFree(GetProcessHeap(), 0, data_from_server
);
2361 if (status
!= RPC_S_OK
) return status
;
2362 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1
, *flow_control_increment
, field3
);
2367 static RPC_STATUS
rpcrt4_ncacn_http_open(RpcConnection
* Connection
)
2369 RpcConnection_http
*httpc
= (RpcConnection_http
*)Connection
;
2370 static const WCHAR wszVerbIn
[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
2371 static const WCHAR wszVerbOut
[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
2372 static const WCHAR wszRpcProxyPrefix
[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
2373 static const WCHAR wszColon
[] = {':',0};
2374 static const WCHAR wszAcceptType
[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
2375 LPCWSTR wszAcceptTypes
[] = { wszAcceptType
, NULL
};
2379 HttpTimerThreadData
*timer_data
;
2382 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
2384 if (Connection
->server
)
2386 ERR("ncacn_http servers not supported yet\n");
2387 return RPC_S_SERVER_UNAVAILABLE
;
2390 if (httpc
->in_request
)
2393 httpc
->async_data
->completion_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2395 status
= UuidCreate(&httpc
->connection_uuid
);
2396 status
= UuidCreate(&httpc
->in_pipe_uuid
);
2397 status
= UuidCreate(&httpc
->out_pipe_uuid
);
2399 status
= rpcrt4_http_internet_connect(httpc
);
2400 if (status
!= RPC_S_OK
)
2403 url
= HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix
) + (strlen(Connection
->NetworkAddr
) + 1 + strlen(Connection
->Endpoint
))*sizeof(WCHAR
));
2405 return RPC_S_OUT_OF_MEMORY
;
2406 memcpy(url
, wszRpcProxyPrefix
, sizeof(wszRpcProxyPrefix
));
2407 MultiByteToWideChar(CP_ACP
, 0, Connection
->NetworkAddr
, -1, url
+sizeof(wszRpcProxyPrefix
)/sizeof(wszRpcProxyPrefix
[0])-1, strlen(Connection
->NetworkAddr
)+1);
2408 strcatW(url
, wszColon
);
2409 MultiByteToWideChar(CP_ACP
, 0, Connection
->Endpoint
, -1, url
+strlenW(url
), strlen(Connection
->Endpoint
)+1);
2411 secure
= httpc
->common
.QOS
&&
2412 (httpc
->common
.QOS
->qos
->AdditionalSecurityInfoType
== RPC_C_AUTHN_INFO_TYPE_HTTP
) &&
2413 (httpc
->common
.QOS
->qos
->u
.HttpCredentials
->Flags
& RPC_C_HTTP_FLAG_USE_SSL
);
2415 httpc
->in_request
= HttpOpenRequestW(httpc
->session
, wszVerbIn
, url
, NULL
, NULL
,
2417 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2418 (DWORD_PTR
)httpc
->async_data
);
2419 if (!httpc
->in_request
)
2421 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2422 return RPC_S_SERVER_UNAVAILABLE
;
2424 httpc
->out_request
= HttpOpenRequestW(httpc
->session
, wszVerbOut
, url
, NULL
, NULL
,
2426 (secure
? INTERNET_FLAG_SECURE
: 0)|INTERNET_FLAG_KEEP_CONNECTION
|INTERNET_FLAG_PRAGMA_NOCACHE
,
2427 (DWORD_PTR
)httpc
->async_data
);
2428 if (!httpc
->out_request
)
2430 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2431 return RPC_S_SERVER_UNAVAILABLE
;
2434 status
= rpcrt4_http_prepare_in_pipe(httpc
->in_request
,
2436 &httpc
->connection_uuid
,
2437 &httpc
->in_pipe_uuid
,
2438 &Connection
->assoc
->http_uuid
);
2439 if (status
!= RPC_S_OK
)
2442 status
= rpcrt4_http_prepare_out_pipe(httpc
->out_request
,
2444 &httpc
->connection_uuid
,
2445 &httpc
->out_pipe_uuid
,
2446 &httpc
->flow_control_increment
);
2447 if (status
!= RPC_S_OK
)
2450 httpc
->flow_control_mark
= httpc
->flow_control_increment
/ 2;
2451 httpc
->last_sent_time
= GetTickCount();
2452 httpc
->timer_cancelled
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
2454 timer_data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data
));
2456 return ERROR_OUTOFMEMORY
;
2457 timer_data
->timer_param
= httpc
->in_request
;
2458 timer_data
->last_sent_time
= &httpc
->last_sent_time
;
2459 timer_data
->timer_cancelled
= httpc
->timer_cancelled
;
2460 /* FIXME: should use CreateTimerQueueTimer when implemented */
2461 thread
= CreateThread(NULL
, 0, rpcrt4_http_timer_thread
, timer_data
, 0, NULL
);
2464 HeapFree(GetProcessHeap(), 0, timer_data
);
2465 return GetLastError();
2467 CloseHandle(thread
);
2472 static RPC_STATUS
rpcrt4_ncacn_http_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
2475 return RPC_S_SERVER_UNAVAILABLE
;
2478 static int rpcrt4_ncacn_http_read(RpcConnection
*Connection
,
2479 void *buffer
, unsigned int count
)
2481 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2484 unsigned int bytes_left
= count
;
2486 ResetEvent(httpc
->async_data
->completion_event
);
2489 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2490 httpc
->async_data
->inet_buffers
.dwBufferLength
= bytes_left
;
2491 httpc
->async_data
->inet_buffers
.lpvBuffer
= HeapAlloc(GetProcessHeap(), 0, bytes_left
);
2492 httpc
->async_data
->destination_buffer
= buf
;
2493 ret
= InternetReadFileExA(httpc
->out_request
, &httpc
->async_data
->inet_buffers
, IRF_ASYNC
, 0);
2496 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2498 RpcHttpAsyncData_Release(httpc
->async_data
);
2499 memcpy(buf
, httpc
->async_data
->inet_buffers
.lpvBuffer
,
2500 httpc
->async_data
->inet_buffers
.dwBufferLength
);
2501 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2502 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2503 httpc
->async_data
->destination_buffer
= NULL
;
2507 if (GetLastError() == ERROR_IO_PENDING
)
2509 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2510 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2511 if (result
== WAIT_OBJECT_0
)
2515 TRACE("call cancelled\n");
2516 EnterCriticalSection(&httpc
->async_data
->cs
);
2517 httpc
->async_data
->destination_buffer
= NULL
;
2518 LeaveCriticalSection(&httpc
->async_data
->cs
);
2524 HeapFree(GetProcessHeap(), 0, httpc
->async_data
->inet_buffers
.lpvBuffer
);
2525 httpc
->async_data
->inet_buffers
.lpvBuffer
= NULL
;
2526 httpc
->async_data
->destination_buffer
= NULL
;
2527 RpcHttpAsyncData_Release(httpc
->async_data
);
2531 if (!httpc
->async_data
->inet_buffers
.dwBufferLength
)
2533 bytes_left
-= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2534 buf
+= httpc
->async_data
->inet_buffers
.dwBufferLength
;
2536 TRACE("%p %p %u -> %s\n", httpc
->out_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2537 return ret
? count
: -1;
2540 static RPC_STATUS
rpcrt4_ncacn_http_receive_fragment(RpcConnection
*Connection
, RpcPktHdr
**Header
, void **Payload
)
2542 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2546 RpcPktCommonHdr common_hdr
;
2550 TRACE("(%p, %p, %p)\n", Connection
, Header
, Payload
);
2553 /* read packet common header */
2554 dwRead
= rpcrt4_ncacn_http_read(Connection
, &common_hdr
, sizeof(common_hdr
));
2555 if (dwRead
!= sizeof(common_hdr
)) {
2556 WARN("Short read of header, %d bytes\n", dwRead
);
2557 status
= RPC_S_PROTOCOL_ERROR
;
2560 if (!memcmp(&common_hdr
, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2561 !memcmp(&common_hdr
, "HTTP/1.0", sizeof("HTTP/1.0")))
2563 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr
));
2564 status
= RPC_S_PROTOCOL_ERROR
;
2568 status
= RPCRT4_ValidateCommonHeader(&common_hdr
);
2569 if (status
!= RPC_S_OK
) goto fail
;
2571 hdr_length
= RPCRT4_GetHeaderSize((RpcPktHdr
*)&common_hdr
);
2572 if (hdr_length
== 0) {
2573 WARN("header length == 0\n");
2574 status
= RPC_S_PROTOCOL_ERROR
;
2578 *Header
= HeapAlloc(GetProcessHeap(), 0, hdr_length
);
2581 status
= RPC_S_OUT_OF_RESOURCES
;
2584 memcpy(*Header
, &common_hdr
, sizeof(common_hdr
));
2586 /* read the rest of packet header */
2587 dwRead
= rpcrt4_ncacn_http_read(Connection
, &(*Header
)->common
+ 1, hdr_length
- sizeof(common_hdr
));
2588 if (dwRead
!= hdr_length
- sizeof(common_hdr
)) {
2589 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead
, hdr_length
);
2590 status
= RPC_S_PROTOCOL_ERROR
;
2594 if (common_hdr
.frag_len
- hdr_length
)
2596 *Payload
= HeapAlloc(GetProcessHeap(), 0, common_hdr
.frag_len
- hdr_length
);
2599 status
= RPC_S_OUT_OF_RESOURCES
;
2603 dwRead
= rpcrt4_ncacn_http_read(Connection
, *Payload
, common_hdr
.frag_len
- hdr_length
);
2604 if (dwRead
!= common_hdr
.frag_len
- hdr_length
)
2606 WARN("bad data length, %d/%d\n", dwRead
, common_hdr
.frag_len
- hdr_length
);
2607 status
= RPC_S_PROTOCOL_ERROR
;
2614 if ((*Header
)->common
.ptype
== PKT_HTTP
)
2616 if (!RPCRT4_IsValidHttpPacket(*Header
, *Payload
, common_hdr
.frag_len
- hdr_length
))
2618 ERR("invalid http packet of length %d bytes\n", (*Header
)->common
.frag_len
);
2619 status
= RPC_S_PROTOCOL_ERROR
;
2622 if ((*Header
)->http
.flags
== 0x0001)
2624 TRACE("http idle packet, waiting for real packet\n");
2625 if ((*Header
)->http
.num_data_items
!= 0)
2627 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header
)->http
.num_data_items
);
2628 status
= RPC_S_PROTOCOL_ERROR
;
2632 else if ((*Header
)->http
.flags
== 0x0002)
2634 ULONG bytes_transmitted
;
2635 ULONG flow_control_increment
;
2637 status
= RPCRT4_ParseHttpFlowControlHeader(*Header
, *Payload
,
2640 &flow_control_increment
,
2642 if (status
!= RPC_S_OK
)
2644 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2645 bytes_transmitted
, flow_control_increment
, debugstr_guid(&pipe_uuid
));
2646 /* FIXME: do something with parsed data */
2650 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header
)->http
.flags
);
2651 status
= RPC_S_PROTOCOL_ERROR
;
2654 RPCRT4_FreeHeader(*Header
);
2656 HeapFree(GetProcessHeap(), 0, *Payload
);
2664 httpc
->bytes_received
+= common_hdr
.frag_len
;
2666 TRACE("httpc->bytes_received = 0x%x\n", httpc
->bytes_received
);
2668 if (httpc
->bytes_received
> httpc
->flow_control_mark
)
2670 RpcPktHdr
*hdr
= RPCRT4_BuildHttpFlowControlHeader(httpc
->common
.server
,
2671 httpc
->bytes_received
,
2672 httpc
->flow_control_increment
,
2673 &httpc
->out_pipe_uuid
);
2676 DWORD bytes_written
;
2678 TRACE("sending flow control packet at 0x%x\n", httpc
->bytes_received
);
2679 ret2
= InternetWriteFile(httpc
->in_request
, hdr
, hdr
->common
.frag_len
, &bytes_written
);
2680 RPCRT4_FreeHeader(hdr
);
2682 httpc
->flow_control_mark
= httpc
->bytes_received
+ httpc
->flow_control_increment
/ 2;
2687 if (status
!= RPC_S_OK
) {
2688 RPCRT4_FreeHeader(*Header
);
2690 HeapFree(GetProcessHeap(), 0, *Payload
);
2696 static int rpcrt4_ncacn_http_write(RpcConnection
*Connection
,
2697 const void *buffer
, unsigned int count
)
2699 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2700 DWORD bytes_written
;
2703 httpc
->last_sent_time
= ~0U; /* disable idle packet sending */
2704 ret
= InternetWriteFile(httpc
->in_request
, buffer
, count
, &bytes_written
);
2705 httpc
->last_sent_time
= GetTickCount();
2706 TRACE("%p %p %u -> %s\n", httpc
->in_request
, buffer
, count
, ret
? "TRUE" : "FALSE");
2707 return ret
? bytes_written
: -1;
2710 static int rpcrt4_ncacn_http_close(RpcConnection
*Connection
)
2712 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2716 SetEvent(httpc
->timer_cancelled
);
2717 if (httpc
->in_request
)
2718 InternetCloseHandle(httpc
->in_request
);
2719 httpc
->in_request
= NULL
;
2720 if (httpc
->out_request
)
2721 InternetCloseHandle(httpc
->out_request
);
2722 httpc
->out_request
= NULL
;
2723 if (httpc
->app_info
)
2724 InternetCloseHandle(httpc
->app_info
);
2725 httpc
->app_info
= NULL
;
2727 InternetCloseHandle(httpc
->session
);
2728 httpc
->session
= NULL
;
2729 RpcHttpAsyncData_Release(httpc
->async_data
);
2730 if (httpc
->cancel_event
)
2731 CloseHandle(httpc
->cancel_event
);
2736 static void rpcrt4_ncacn_http_cancel_call(RpcConnection
*Connection
)
2738 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2740 SetEvent(httpc
->cancel_event
);
2743 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection
*Connection
)
2746 RpcConnection_http
*httpc
= (RpcConnection_http
*) Connection
;
2748 RpcHttpAsyncData_AddRef(httpc
->async_data
);
2749 ret
= InternetQueryDataAvailable(httpc
->out_request
,
2750 &httpc
->async_data
->inet_buffers
.dwBufferLength
, IRF_ASYNC
, 0);
2753 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2755 RpcHttpAsyncData_Release(httpc
->async_data
);
2759 if (GetLastError() == ERROR_IO_PENDING
)
2761 HANDLE handles
[2] = { httpc
->async_data
->completion_event
, httpc
->cancel_event
};
2762 DWORD result
= WaitForMultipleObjects(2, handles
, FALSE
, DEFAULT_NCACN_HTTP_TIMEOUT
);
2763 if (result
!= WAIT_OBJECT_0
)
2765 TRACE("call cancelled\n");
2771 RpcHttpAsyncData_Release(httpc
->async_data
);
2780 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data
,
2781 const char *networkaddr
,
2782 const char *endpoint
)
2784 return rpcrt4_ip_tcp_get_top_of_tower(tower_data
, networkaddr
,
2785 EPM_PROTOCOL_HTTP
, endpoint
);
2788 static RPC_STATUS
rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data
,
2793 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data
, tower_size
,
2794 networkaddr
, EPM_PROTOCOL_HTTP
,
2798 static const struct connection_ops conn_protseq_list
[] = {
2800 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
2801 rpcrt4_conn_np_alloc
,
2802 rpcrt4_ncacn_np_open
,
2803 rpcrt4_ncacn_np_handoff
,
2804 rpcrt4_conn_np_read
,
2805 rpcrt4_conn_np_write
,
2806 rpcrt4_conn_np_close
,
2807 rpcrt4_conn_np_cancel_call
,
2808 rpcrt4_conn_np_wait_for_incoming_data
,
2809 rpcrt4_ncacn_np_get_top_of_tower
,
2810 rpcrt4_ncacn_np_parse_top_of_tower
,
2812 RPCRT4_default_is_authorized
,
2813 RPCRT4_default_authorize
,
2814 RPCRT4_default_secure_packet
,
2815 rpcrt4_conn_np_impersonate_client
,
2816 rpcrt4_conn_np_revert_to_self
,
2817 RPCRT4_default_inquire_auth_client
,
2820 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
2821 rpcrt4_conn_np_alloc
,
2822 rpcrt4_ncalrpc_open
,
2823 rpcrt4_ncalrpc_handoff
,
2824 rpcrt4_conn_np_read
,
2825 rpcrt4_conn_np_write
,
2826 rpcrt4_conn_np_close
,
2827 rpcrt4_conn_np_cancel_call
,
2828 rpcrt4_conn_np_wait_for_incoming_data
,
2829 rpcrt4_ncalrpc_get_top_of_tower
,
2830 rpcrt4_ncalrpc_parse_top_of_tower
,
2832 rpcrt4_ncalrpc_is_authorized
,
2833 rpcrt4_ncalrpc_authorize
,
2834 rpcrt4_ncalrpc_secure_packet
,
2835 rpcrt4_conn_np_impersonate_client
,
2836 rpcrt4_conn_np_revert_to_self
,
2837 rpcrt4_ncalrpc_inquire_auth_client
,
2840 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
2841 rpcrt4_conn_tcp_alloc
,
2842 rpcrt4_ncacn_ip_tcp_open
,
2843 rpcrt4_conn_tcp_handoff
,
2844 rpcrt4_conn_tcp_read
,
2845 rpcrt4_conn_tcp_write
,
2846 rpcrt4_conn_tcp_close
,
2847 rpcrt4_conn_tcp_cancel_call
,
2848 rpcrt4_conn_tcp_wait_for_incoming_data
,
2849 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
2850 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
2852 RPCRT4_default_is_authorized
,
2853 RPCRT4_default_authorize
,
2854 RPCRT4_default_secure_packet
,
2855 RPCRT4_default_impersonate_client
,
2856 RPCRT4_default_revert_to_self
,
2857 RPCRT4_default_inquire_auth_client
,
2860 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_HTTP
},
2861 rpcrt4_ncacn_http_alloc
,
2862 rpcrt4_ncacn_http_open
,
2863 rpcrt4_ncacn_http_handoff
,
2864 rpcrt4_ncacn_http_read
,
2865 rpcrt4_ncacn_http_write
,
2866 rpcrt4_ncacn_http_close
,
2867 rpcrt4_ncacn_http_cancel_call
,
2868 rpcrt4_ncacn_http_wait_for_incoming_data
,
2869 rpcrt4_ncacn_http_get_top_of_tower
,
2870 rpcrt4_ncacn_http_parse_top_of_tower
,
2871 rpcrt4_ncacn_http_receive_fragment
,
2872 RPCRT4_default_is_authorized
,
2873 RPCRT4_default_authorize
,
2874 RPCRT4_default_secure_packet
,
2875 RPCRT4_default_impersonate_client
,
2876 RPCRT4_default_revert_to_self
,
2877 RPCRT4_default_inquire_auth_client
,
2882 static const struct protseq_ops protseq_list
[] =
2886 rpcrt4_protseq_np_alloc
,
2887 rpcrt4_protseq_np_signal_state_changed
,
2888 rpcrt4_protseq_np_get_wait_array
,
2889 rpcrt4_protseq_np_free_wait_array
,
2890 rpcrt4_protseq_np_wait_for_new_connection
,
2891 rpcrt4_protseq_ncacn_np_open_endpoint
,
2895 rpcrt4_protseq_np_alloc
,
2896 rpcrt4_protseq_np_signal_state_changed
,
2897 rpcrt4_protseq_np_get_wait_array
,
2898 rpcrt4_protseq_np_free_wait_array
,
2899 rpcrt4_protseq_np_wait_for_new_connection
,
2900 rpcrt4_protseq_ncalrpc_open_endpoint
,
2904 rpcrt4_protseq_sock_alloc
,
2905 rpcrt4_protseq_sock_signal_state_changed
,
2906 rpcrt4_protseq_sock_get_wait_array
,
2907 rpcrt4_protseq_sock_free_wait_array
,
2908 rpcrt4_protseq_sock_wait_for_new_connection
,
2909 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
2913 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2915 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
2918 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
2919 if (!strcmp(protseq_list
[i
].name
, protseq
))
2920 return &protseq_list
[i
];
2924 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
2927 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
2928 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
2929 return &conn_protseq_list
[i
];
2933 /**** interface to rest of code ****/
2935 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
2937 TRACE("(Connection == ^%p)\n", Connection
);
2939 assert(!Connection
->server
);
2940 return Connection
->ops
->open_connection_client(Connection
);
2943 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
2945 TRACE("(Connection == ^%p)\n", Connection
);
2946 if (SecIsValidHandle(&Connection
->ctx
))
2948 DeleteSecurityContext(&Connection
->ctx
);
2949 SecInvalidateHandle(&Connection
->ctx
);
2951 rpcrt4_conn_close(Connection
);
2955 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
2956 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
2957 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
2959 static LONG next_id
;
2960 const struct connection_ops
*ops
;
2961 RpcConnection
* NewConnection
;
2963 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
2966 FIXME("not supported for protseq %s\n", Protseq
);
2967 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
2970 NewConnection
= ops
->alloc();
2971 NewConnection
->Next
= NULL
;
2972 NewConnection
->server_binding
= NULL
;
2973 NewConnection
->server
= server
;
2974 NewConnection
->ops
= ops
;
2975 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
2976 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
2977 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
2978 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
2979 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
2980 NewConnection
->NextCallId
= 1;
2982 SecInvalidateHandle(&NewConnection
->ctx
);
2983 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
2984 NewConnection
->attr
= 0;
2985 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
2986 NewConnection
->AuthInfo
= AuthInfo
;
2987 NewConnection
->auth_context_id
= InterlockedIncrement( &next_id
);
2988 NewConnection
->encryption_auth_len
= 0;
2989 NewConnection
->signature_auth_len
= 0;
2990 if (QOS
) RpcQualityOfService_AddRef(QOS
);
2991 NewConnection
->QOS
= QOS
;
2993 list_init(&NewConnection
->conn_pool_entry
);
2994 NewConnection
->async_state
= NULL
;
2996 TRACE("connection: %p\n", NewConnection
);
2997 *Connection
= NewConnection
;
3002 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
3006 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
3007 rpcrt4_conn_get_name(OldConnection
),
3008 OldConnection
->NetworkAddr
,
3009 OldConnection
->Endpoint
, NULL
,
3010 OldConnection
->AuthInfo
, OldConnection
->QOS
);
3011 if (err
== RPC_S_OK
)
3012 rpcrt4_conn_handoff(OldConnection
, *Connection
);
3016 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
3018 TRACE("connection: %p\n", Connection
);
3020 RPCRT4_CloseConnection(Connection
);
3021 RPCRT4_strfree(Connection
->Endpoint
);
3022 RPCRT4_strfree(Connection
->NetworkAddr
);
3023 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
3024 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
3025 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
3028 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
3030 HeapFree(GetProcessHeap(), 0, Connection
);
3034 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
3036 const char *protseq
,
3037 const char *networkaddr
,
3038 const char *endpoint
)
3040 twr_empty_floor_t
*protocol_floor
;
3041 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
3046 return RPC_S_INVALID_RPC_PROTSEQ
;
3050 *tower_size
= sizeof(*protocol_floor
);
3051 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
3055 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
3056 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
3057 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
3058 protocol_floor
->count_rhs
= 0;
3060 tower_data
+= sizeof(*protocol_floor
);
3062 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
3064 return EPT_S_NOT_REGISTERED
;
3066 *tower_size
+= sizeof(*protocol_floor
);
3071 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
3077 const twr_empty_floor_t
*protocol_floor
;
3078 const twr_empty_floor_t
*floor4
;
3079 const struct connection_ops
*protseq_ops
= NULL
;
3083 if (tower_size
< sizeof(*protocol_floor
))
3084 return EPT_S_NOT_REGISTERED
;
3086 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
3087 tower_data
+= sizeof(*protocol_floor
);
3088 tower_size
-= sizeof(*protocol_floor
);
3089 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
3090 (protocol_floor
->count_rhs
> tower_size
))
3091 return EPT_S_NOT_REGISTERED
;
3092 tower_data
+= protocol_floor
->count_rhs
;
3093 tower_size
-= protocol_floor
->count_rhs
;
3095 floor4
= (const twr_empty_floor_t
*)tower_data
;
3096 if ((tower_size
< sizeof(*floor4
)) ||
3097 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
3098 return EPT_S_NOT_REGISTERED
;
3100 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
3101 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
3102 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
3104 protseq_ops
= &conn_protseq_list
[i
];
3109 return EPT_S_NOT_REGISTERED
;
3111 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
3113 if ((status
== RPC_S_OK
) && protseq
)
3115 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
3116 strcpy(*protseq
, protseq_ops
->name
);
3122 /***********************************************************************
3123 * RpcNetworkIsProtseqValidW (RPCRT4.@)
3125 * Checks if the given protocol sequence is known by the RPC system.
3126 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
3129 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
3133 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
3134 ps
, sizeof ps
, NULL
, NULL
);
3135 if (rpcrt4_get_conn_protseq_ops(ps
))
3138 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
3140 return RPC_S_INVALID_RPC_PROTSEQ
;
3143 /***********************************************************************
3144 * RpcNetworkIsProtseqValidA (RPCRT4.@)
3146 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
3148 UNICODE_STRING protseqW
;
3150 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
3152 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
3153 RtlFreeUnicodeString(&protseqW
);
3156 return RPC_S_OUT_OF_MEMORY
;
3159 /***********************************************************************
3160 * RpcProtseqVectorFreeA (RPCRT4.@)
3162 RPC_STATUS WINAPI
RpcProtseqVectorFreeA(RPC_PROTSEQ_VECTORA
**protseqs
)
3164 TRACE("(%p)\n", protseqs
);
3169 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3170 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3171 HeapFree(GetProcessHeap(), 0, *protseqs
);
3177 /***********************************************************************
3178 * RpcProtseqVectorFreeW (RPCRT4.@)
3180 RPC_STATUS WINAPI
RpcProtseqVectorFreeW(RPC_PROTSEQ_VECTORW
**protseqs
)
3182 TRACE("(%p)\n", protseqs
);
3187 for (i
= 0; i
< (*protseqs
)->Count
; i
++)
3188 HeapFree(GetProcessHeap(), 0, (*protseqs
)->Protseq
[i
]);
3189 HeapFree(GetProcessHeap(), 0, *protseqs
);
3195 /***********************************************************************
3196 * RpcNetworkInqProtseqsW (RPCRT4.@)
3198 RPC_STATUS WINAPI
RpcNetworkInqProtseqsW( RPC_PROTSEQ_VECTORW
** protseqs
)
3200 RPC_PROTSEQ_VECTORW
*pvector
;
3202 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3204 TRACE("(%p)\n", protseqs
);
3206 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned short*)*ARRAYSIZE(protseq_list
)));
3209 pvector
= *protseqs
;
3211 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3213 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, (strlen(protseq_list
[i
].name
)+1)*sizeof(unsigned short));
3214 if (pvector
->Protseq
[i
] == NULL
)
3216 MultiByteToWideChar(CP_ACP
, 0, (CHAR
*)protseq_list
[i
].name
, -1,
3217 (WCHAR
*)pvector
->Protseq
[i
], strlen(protseq_list
[i
].name
) + 1);
3223 if (status
!= RPC_S_OK
)
3224 RpcProtseqVectorFreeW(protseqs
);
3228 /***********************************************************************
3229 * RpcNetworkInqProtseqsA (RPCRT4.@)
3231 RPC_STATUS WINAPI
RpcNetworkInqProtseqsA(RPC_PROTSEQ_VECTORA
** protseqs
)
3233 RPC_PROTSEQ_VECTORA
*pvector
;
3235 RPC_STATUS status
= RPC_S_OUT_OF_MEMORY
;
3237 TRACE("(%p)\n", protseqs
);
3239 *protseqs
= HeapAlloc(GetProcessHeap(), 0, sizeof(RPC_PROTSEQ_VECTORW
)+(sizeof(unsigned char*)*ARRAYSIZE(protseq_list
)));
3242 pvector
= *protseqs
;
3244 for (i
= 0; i
< ARRAYSIZE(protseq_list
); i
++)
3246 pvector
->Protseq
[i
] = HeapAlloc(GetProcessHeap(), 0, strlen(protseq_list
[i
].name
)+1);
3247 if (pvector
->Protseq
[i
] == NULL
)
3249 strcpy((char*)pvector
->Protseq
[i
], protseq_list
[i
].name
);
3255 if (status
!= RPC_S_OK
)
3256 RpcProtseqVectorFreeA(protseqs
);