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
49 # ifdef HAVE_SYS_SOCKET_H
50 # include <sys/socket.h>
52 # ifdef HAVE_NETINET_IN_H
53 # include <netinet/in.h>
55 # ifdef HAVE_NETINET_TCP_H
56 # include <netinet/tcp.h>
58 # ifdef HAVE_ARPA_INET_H
59 # include <arpa/inet.h>
64 # ifdef HAVE_SYS_POLL_H
65 # include <sys/poll.h>
67 # define closesocket close
68 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
75 #include "wine/unicode.h"
80 #include "wine/debug.h"
82 #include "rpc_binding.h"
83 #include "rpc_message.h"
84 #include "rpc_server.h"
85 #include "epm_towers.h"
88 # define SOL_TCP IPPROTO_TCP
91 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
93 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
95 /**** ncacn_np support ****/
97 typedef struct _RpcConnection_np
105 static RpcConnection
*rpcrt4_conn_np_alloc(void)
107 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
111 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
112 npc
->listening
= FALSE
;
117 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
122 npc
->listening
= TRUE
;
125 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
128 switch(GetLastError())
130 case ERROR_PIPE_CONNECTED
:
131 SetEvent(npc
->ovl
.hEvent
);
133 case ERROR_IO_PENDING
:
134 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
136 case ERROR_NO_DATA_DETECTED
:
137 /* client has disconnected, retry */
138 DisconnectNamedPipe( npc
->pipe
);
141 npc
->listening
= FALSE
;
142 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
143 return RPC_S_OUT_OF_RESOURCES
;
148 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
150 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
151 TRACE("listening on %s\n", pname
);
153 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
154 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
155 PIPE_UNLIMITED_INSTANCES
,
156 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
157 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
158 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
159 if (GetLastError() == ERROR_FILE_EXISTS
)
160 return RPC_S_DUPLICATE_ENDPOINT
;
162 return RPC_S_CANT_CREATE_ENDPOINT
;
165 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
166 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
168 /* Note: we don't call ConnectNamedPipe here because it must be done in the
169 * server thread as the thread must be alertable */
173 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
175 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
179 TRACE("connecting to %s\n", pname
);
185 dwFlags
= SECURITY_SQOS_PRESENT
;
186 switch (Connection
->QOS
->qos
->ImpersonationType
)
188 case RPC_C_IMP_LEVEL_DEFAULT
:
189 /* FIXME: what to do here? */
191 case RPC_C_IMP_LEVEL_ANONYMOUS
:
192 dwFlags
|= SECURITY_ANONYMOUS
;
194 case RPC_C_IMP_LEVEL_IDENTIFY
:
195 dwFlags
|= SECURITY_IDENTIFICATION
;
197 case RPC_C_IMP_LEVEL_IMPERSONATE
:
198 dwFlags
|= SECURITY_IMPERSONATION
;
200 case RPC_C_IMP_LEVEL_DELEGATE
:
201 dwFlags
|= SECURITY_DELEGATION
;
204 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
205 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
207 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
208 OPEN_EXISTING
, dwFlags
, 0);
209 if (pipe
!= INVALID_HANDLE_VALUE
) break;
210 err
= GetLastError();
211 if (err
== ERROR_PIPE_BUSY
) {
212 TRACE("connection failed, error=%x\n", err
);
213 return RPC_S_SERVER_TOO_BUSY
;
215 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
216 err
= GetLastError();
217 WARN("connection failed, error=%x\n", err
);
218 return RPC_S_SERVER_UNAVAILABLE
;
223 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
224 /* pipe is connected; change to message-read mode. */
225 dwMode
= PIPE_READMODE_MESSAGE
;
226 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
227 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
233 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
235 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
236 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
240 /* already connected? */
244 /* protseq=ncalrpc: supposed to use NT LPC ports,
245 * but we'll implement it with named pipes for now */
246 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
247 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
248 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
254 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, LPSTR endpoint
)
256 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
259 RpcConnection
*Connection
;
261 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
262 endpoint
, NULL
, NULL
, NULL
);
266 /* protseq=ncalrpc: supposed to use NT LPC ports,
267 * but we'll implement it with named pipes for now */
268 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
269 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
270 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
273 EnterCriticalSection(&protseq
->cs
);
274 Connection
->Next
= protseq
->conn
;
275 protseq
->conn
= Connection
;
276 LeaveCriticalSection(&protseq
->cs
);
281 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
283 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
284 static const char prefix
[] = "\\\\.";
288 /* already connected? */
292 /* protseq=ncacn_np: named pipes */
293 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
294 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
295 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
301 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
303 static const char prefix
[] = "\\\\.";
306 RpcConnection
*Connection
;
308 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
309 endpoint
, NULL
, NULL
, NULL
);
313 /* protseq=ncacn_np: named pipes */
314 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
315 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
316 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
319 EnterCriticalSection(&protseq
->cs
);
320 Connection
->Next
= protseq
->conn
;
321 protseq
->conn
= Connection
;
322 LeaveCriticalSection(&protseq
->cs
);
327 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
329 /* because of the way named pipes work, we'll transfer the connected pipe
330 * to the child, then reopen the server binding to continue listening */
332 new_npc
->pipe
= old_npc
->pipe
;
333 new_npc
->ovl
= old_npc
->ovl
;
335 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
336 old_npc
->listening
= FALSE
;
339 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
343 static const char prefix
[] = "\\\\.";
345 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
347 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
348 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
349 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
355 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
359 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
361 TRACE("%s\n", old_conn
->Endpoint
);
363 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
365 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
366 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
367 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
373 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
374 void *buffer
, unsigned int count
)
376 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
379 unsigned int bytes_left
= count
;
384 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
385 if (!ret
|| !bytes_read
)
387 bytes_left
-= bytes_read
;
390 return ret
? count
: -1;
393 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
394 const void *buffer
, unsigned int count
)
396 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
397 const char *buf
= buffer
;
399 unsigned int bytes_left
= count
;
404 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
405 if (!ret
|| !bytes_written
)
407 bytes_left
-= bytes_written
;
408 buf
+= bytes_written
;
410 return ret
? count
: -1;
413 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
415 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
417 FlushFileBuffers(npc
->pipe
);
418 CloseHandle(npc
->pipe
);
421 if (npc
->ovl
.hEvent
) {
422 CloseHandle(npc
->ovl
.hEvent
);
428 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
430 /* FIXME: implement when named pipe writes use overlapped I/O */
433 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
435 /* FIXME: implement when named pipe writes use overlapped I/O */
439 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
440 const char *networkaddr
,
441 const char *endpoint
)
443 twr_empty_floor_t
*smb_floor
;
444 twr_empty_floor_t
*nb_floor
;
446 size_t networkaddr_size
;
447 size_t endpoint_size
;
449 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
451 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
452 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
453 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
458 smb_floor
= (twr_empty_floor_t
*)tower_data
;
460 tower_data
+= sizeof(*smb_floor
);
462 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
463 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
464 smb_floor
->count_rhs
= endpoint_size
;
467 memcpy(tower_data
, endpoint
, endpoint_size
);
470 tower_data
+= endpoint_size
;
472 nb_floor
= (twr_empty_floor_t
*)tower_data
;
474 tower_data
+= sizeof(*nb_floor
);
476 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
477 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
478 nb_floor
->count_rhs
= networkaddr_size
;
481 memcpy(tower_data
, networkaddr
, networkaddr_size
);
488 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
493 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
494 const twr_empty_floor_t
*nb_floor
;
496 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
498 if (tower_size
< sizeof(*smb_floor
))
499 return EPT_S_NOT_REGISTERED
;
501 tower_data
+= sizeof(*smb_floor
);
502 tower_size
-= sizeof(*smb_floor
);
504 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
505 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
506 (smb_floor
->count_rhs
> tower_size
) ||
507 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
508 return EPT_S_NOT_REGISTERED
;
512 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
514 return RPC_S_OUT_OF_RESOURCES
;
515 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
517 tower_data
+= smb_floor
->count_rhs
;
518 tower_size
-= smb_floor
->count_rhs
;
520 if (tower_size
< sizeof(*nb_floor
))
521 return EPT_S_NOT_REGISTERED
;
523 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
525 tower_data
+= sizeof(*nb_floor
);
526 tower_size
-= sizeof(*nb_floor
);
528 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
529 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
530 (nb_floor
->count_rhs
> tower_size
) ||
531 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
532 return EPT_S_NOT_REGISTERED
;
536 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
541 I_RpcFree(*endpoint
);
544 return RPC_S_OUT_OF_RESOURCES
;
546 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
552 typedef struct _RpcServerProtseq_np
554 RpcServerProtseq common
;
556 } RpcServerProtseq_np
;
558 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
560 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
562 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
566 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
568 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
569 SetEvent(npps
->mgr_event
);
572 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
574 HANDLE
*objs
= prev_array
;
575 RpcConnection_np
*conn
;
576 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
578 EnterCriticalSection(&protseq
->cs
);
580 /* open and count connections */
582 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
584 rpcrt4_conn_listen_pipe(conn
);
585 if (conn
->ovl
.hEvent
)
587 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
590 /* make array of connections */
592 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
594 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
597 ERR("couldn't allocate objs\n");
598 LeaveCriticalSection(&protseq
->cs
);
602 objs
[0] = npps
->mgr_event
;
604 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
606 if ((objs
[*count
] = conn
->ovl
.hEvent
))
608 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
610 LeaveCriticalSection(&protseq
->cs
);
614 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
616 HeapFree(GetProcessHeap(), 0, array
);
619 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
622 HANDLE
*objs
= wait_array
;
624 RpcConnection
*cconn
;
625 RpcConnection_np
*conn
;
632 /* an alertable wait isn't strictly necessary, but due to our
633 * overlapped I/O implementation in Wine we need to free some memory
634 * by the file user APC being called, even if no completion routine was
635 * specified at the time of starting the async operation */
636 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
637 } while (res
== WAIT_IO_COMPLETION
);
639 if (res
== WAIT_OBJECT_0
)
641 else if (res
== WAIT_FAILED
)
643 ERR("wait failed with error %d\n", GetLastError());
648 b_handle
= objs
[res
- WAIT_OBJECT_0
];
649 /* find which connection got a RPC */
650 EnterCriticalSection(&protseq
->cs
);
651 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
653 if (b_handle
== conn
->ovl
.hEvent
) break;
654 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
658 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
660 ERR("failed to locate connection for handle %p\n", b_handle
);
661 LeaveCriticalSection(&protseq
->cs
);
664 RPCRT4_new_client(cconn
);
671 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
672 const char *networkaddr
,
673 const char *endpoint
)
675 twr_empty_floor_t
*pipe_floor
;
677 size_t endpoint_size
;
679 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
681 endpoint_size
= strlen(endpoint
) + 1;
682 size
= sizeof(*pipe_floor
) + endpoint_size
;
687 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
689 tower_data
+= sizeof(*pipe_floor
);
691 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
692 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
693 pipe_floor
->count_rhs
= endpoint_size
;
695 memcpy(tower_data
, endpoint
, endpoint_size
);
700 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
705 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
707 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
709 if (tower_size
< sizeof(*pipe_floor
))
710 return EPT_S_NOT_REGISTERED
;
712 tower_data
+= sizeof(*pipe_floor
);
713 tower_size
-= sizeof(*pipe_floor
);
715 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
716 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
717 (pipe_floor
->count_rhs
> tower_size
) ||
718 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
719 return EPT_S_NOT_REGISTERED
;
726 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
728 return RPC_S_OUT_OF_RESOURCES
;
729 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
735 /**** ncacn_ip_tcp support ****/
737 #ifdef HAVE_SOCKETPAIR
739 typedef struct _RpcConnection_tcp
741 RpcConnection common
;
746 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
748 RpcConnection_tcp
*tcpc
;
749 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
753 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
755 ERR("socketpair() failed: %s\n", strerror(errno
));
756 HeapFree(GetProcessHeap(), 0, tcpc
);
759 return &tcpc
->common
;
762 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
764 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
768 struct addrinfo
*ai_cur
;
769 struct addrinfo hints
;
771 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
773 if (tcpc
->sock
!= -1)
777 hints
.ai_family
= PF_UNSPEC
;
778 hints
.ai_socktype
= SOCK_STREAM
;
779 hints
.ai_protocol
= IPPROTO_TCP
;
780 hints
.ai_addrlen
= 0;
781 hints
.ai_addr
= NULL
;
782 hints
.ai_canonname
= NULL
;
783 hints
.ai_next
= NULL
;
785 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
788 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
789 Connection
->Endpoint
, gai_strerror(ret
));
790 return RPC_S_SERVER_UNAVAILABLE
;
793 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
801 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
802 host
, sizeof(host
), service
, sizeof(service
),
803 NI_NUMERICHOST
| NI_NUMERICSERV
);
804 TRACE("trying %s:%s\n", host
, service
);
807 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
810 WARN("socket() failed: %s\n", strerror(errno
));
814 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
816 WARN("connect() failed: %s\n", strerror(errno
));
821 /* RPC depends on having minimal latency so disable the Nagle algorithm */
823 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
824 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
829 TRACE("connected\n");
834 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
835 return RPC_S_SERVER_UNAVAILABLE
;
838 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
840 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
844 struct addrinfo
*ai_cur
;
845 struct addrinfo hints
;
846 RpcConnection
*first_connection
= NULL
;
848 TRACE("(%p, %s)\n", protseq
, endpoint
);
850 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
851 hints
.ai_family
= PF_UNSPEC
;
852 hints
.ai_socktype
= SOCK_STREAM
;
853 hints
.ai_protocol
= IPPROTO_TCP
;
854 hints
.ai_addrlen
= 0;
855 hints
.ai_addr
= NULL
;
856 hints
.ai_canonname
= NULL
;
857 hints
.ai_next
= NULL
;
859 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
862 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
864 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
865 return RPC_S_INVALID_ENDPOINT_FORMAT
;
866 return RPC_S_CANT_CREATE_ENDPOINT
;
869 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
871 RpcConnection_tcp
*tcpc
;
872 RPC_STATUS create_status
;
878 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
879 host
, sizeof(host
), service
, sizeof(service
),
880 NI_NUMERICHOST
| NI_NUMERICSERV
);
881 TRACE("trying %s:%s\n", host
, service
);
884 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
887 WARN("socket() failed: %s\n", strerror(errno
));
888 status
= RPC_S_CANT_CREATE_ENDPOINT
;
892 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
895 WARN("bind failed: %s\n", strerror(errno
));
897 if (errno
== EADDRINUSE
)
898 status
= RPC_S_DUPLICATE_ENDPOINT
;
900 status
= RPC_S_CANT_CREATE_ENDPOINT
;
903 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
904 protseq
->Protseq
, NULL
,
905 endpoint
, NULL
, NULL
, NULL
);
906 if (create_status
!= RPC_S_OK
)
909 status
= create_status
;
914 ret
= listen(sock
, protseq
->MaxCalls
);
917 WARN("listen failed: %s\n", strerror(errno
));
918 RPCRT4_DestroyConnection(&tcpc
->common
);
919 status
= RPC_S_OUT_OF_RESOURCES
;
922 /* need a non-blocking socket, otherwise accept() has a potential
923 * race-condition (poll() says it is readable, connection drops,
924 * and accept() blocks until the next connection comes...)
926 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
929 WARN("couldn't make socket non-blocking, error %d\n", ret
);
930 RPCRT4_DestroyConnection(&tcpc
->common
);
931 status
= RPC_S_OUT_OF_RESOURCES
;
935 tcpc
->common
.Next
= first_connection
;
936 first_connection
= &tcpc
->common
;
941 /* if at least one connection was created for an endpoint then
943 if (first_connection
)
947 /* find last element in list */
948 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
951 EnterCriticalSection(&protseq
->cs
);
952 conn
->Next
= protseq
->conn
;
953 protseq
->conn
= first_connection
;
954 LeaveCriticalSection(&protseq
->cs
);
956 TRACE("listening on %s\n", endpoint
);
960 ERR("couldn't listen on port %s\n", endpoint
);
964 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
967 struct sockaddr_in address
;
969 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
970 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
972 addrsize
= sizeof(address
);
973 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
976 ERR("Failed to accept a TCP connection: error %d\n", ret
);
977 return RPC_S_OUT_OF_RESOURCES
;
979 /* reset to blocking behaviour */
980 fcntl(ret
, F_SETFL
, 0);
982 TRACE("Accepted a new TCP connection\n");
986 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
987 void *buffer
, unsigned int count
)
989 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
993 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
998 else if (errno
!= EAGAIN
)
1000 WARN("recv() failed: %s\n", strerror(errno
));
1005 struct pollfd pfds
[2];
1006 pfds
[0].fd
= tcpc
->sock
;
1007 pfds
[0].events
= POLLIN
;
1008 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1009 pfds
[1].events
= POLLIN
;
1010 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1012 ERR("poll() failed: %s\n", strerror(errno
));
1015 if (pfds
[1].revents
& POLLIN
) /* canceled */
1018 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1022 } while (bytes_read
!= count
);
1023 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1027 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1028 const void *buffer
, unsigned int count
)
1030 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1031 int bytes_written
= 0;
1034 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1037 else if (errno
!= EAGAIN
)
1042 pfd
.fd
= tcpc
->sock
;
1043 pfd
.events
= POLLOUT
;
1044 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1046 ERR("poll() failed: %s\n", strerror(errno
));
1050 } while (bytes_written
!= count
);
1051 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1052 return bytes_written
;
1055 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1057 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1059 TRACE("%d\n", tcpc
->sock
);
1061 if (tcpc
->sock
!= -1)
1062 closesocket(tcpc
->sock
);
1064 close(tcpc
->cancel_fds
[0]);
1065 close(tcpc
->cancel_fds
[1]);
1069 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1071 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1074 TRACE("%p\n", Connection
);
1076 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1079 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1081 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1082 struct pollfd pfds
[2];
1084 TRACE("%p\n", Connection
);
1086 pfds
[0].fd
= tcpc
->sock
;
1087 pfds
[0].events
= POLLIN
;
1088 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1089 pfds
[1].events
= POLLIN
;
1090 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1092 ERR("poll() failed: %s\n", strerror(errno
));
1095 if (pfds
[1].revents
& POLLIN
) /* canceled */
1098 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1105 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1106 const char *networkaddr
,
1107 const char *endpoint
)
1109 twr_tcp_floor_t
*tcp_floor
;
1110 twr_ipv4_floor_t
*ipv4_floor
;
1111 struct addrinfo
*ai
;
1112 struct addrinfo hints
;
1114 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1116 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1121 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1122 tower_data
+= sizeof(*tcp_floor
);
1124 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1126 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1127 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1128 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1130 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1131 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1132 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1134 hints
.ai_flags
= AI_NUMERICHOST
;
1135 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1136 hints
.ai_family
= PF_INET
;
1137 hints
.ai_socktype
= SOCK_STREAM
;
1138 hints
.ai_protocol
= IPPROTO_TCP
;
1139 hints
.ai_addrlen
= 0;
1140 hints
.ai_addr
= NULL
;
1141 hints
.ai_canonname
= NULL
;
1142 hints
.ai_next
= NULL
;
1144 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1147 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1150 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1155 if (ai
->ai_family
== PF_INET
)
1157 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1158 tcp_floor
->port
= sin
->sin_port
;
1159 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1163 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1172 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1177 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1178 const twr_ipv4_floor_t
*ipv4_floor
;
1179 struct in_addr in_addr
;
1181 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1183 if (tower_size
< sizeof(*tcp_floor
))
1184 return EPT_S_NOT_REGISTERED
;
1186 tower_data
+= sizeof(*tcp_floor
);
1187 tower_size
-= sizeof(*tcp_floor
);
1189 if (tower_size
< sizeof(*ipv4_floor
))
1190 return EPT_S_NOT_REGISTERED
;
1192 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1194 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1195 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1196 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1197 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1198 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1199 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1200 return EPT_S_NOT_REGISTERED
;
1204 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1206 return RPC_S_OUT_OF_RESOURCES
;
1207 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1212 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1217 I_RpcFree(*endpoint
);
1220 return RPC_S_OUT_OF_RESOURCES
;
1222 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1223 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1225 ERR("inet_ntop: %s\n", strerror(errno
));
1226 I_RpcFree(*networkaddr
);
1227 *networkaddr
= NULL
;
1230 I_RpcFree(*endpoint
);
1233 return EPT_S_NOT_REGISTERED
;
1240 typedef struct _RpcServerProtseq_sock
1242 RpcServerProtseq common
;
1245 } RpcServerProtseq_sock
;
1247 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1249 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1253 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1255 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1256 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1257 ps
->mgr_event_rcv
= fds
[0];
1258 ps
->mgr_event_snd
= fds
[1];
1262 ERR("socketpair failed with error %s\n", strerror(errno
));
1263 HeapFree(GetProcessHeap(), 0, ps
);
1270 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1272 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1274 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1277 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1279 struct pollfd
*poll_info
= prev_array
;
1280 RpcConnection_tcp
*conn
;
1281 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1283 EnterCriticalSection(&protseq
->cs
);
1285 /* open and count connections */
1287 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1289 if (conn
->sock
!= -1)
1291 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1294 /* make array of connections */
1296 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1298 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1301 ERR("couldn't allocate poll_info\n");
1302 LeaveCriticalSection(&protseq
->cs
);
1306 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1307 poll_info
[0].events
= POLLIN
;
1309 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1311 if (conn
->sock
!= -1)
1313 poll_info
[*count
].fd
= conn
->sock
;
1314 poll_info
[*count
].events
= POLLIN
;
1317 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1319 LeaveCriticalSection(&protseq
->cs
);
1323 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1325 HeapFree(GetProcessHeap(), 0, array
);
1328 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1330 struct pollfd
*poll_info
= wait_array
;
1333 RpcConnection
*cconn
;
1334 RpcConnection_tcp
*conn
;
1339 ret
= poll(poll_info
, count
, -1);
1342 ERR("poll failed with error %d\n", ret
);
1346 for (i
= 0; i
< count
; i
++)
1347 if (poll_info
[i
].revents
& POLLIN
)
1349 /* RPC server event */
1353 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1357 /* find which connection got a RPC */
1358 EnterCriticalSection(&protseq
->cs
);
1359 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1361 if (poll_info
[i
].fd
== conn
->sock
) break;
1362 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1366 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1368 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1369 LeaveCriticalSection(&protseq
->cs
);
1371 RPCRT4_new_client(cconn
);
1379 #endif /* HAVE_SOCKETPAIR */
1381 static const struct connection_ops conn_protseq_list
[] = {
1383 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1384 rpcrt4_conn_np_alloc
,
1385 rpcrt4_ncacn_np_open
,
1386 rpcrt4_ncacn_np_handoff
,
1387 rpcrt4_conn_np_read
,
1388 rpcrt4_conn_np_write
,
1389 rpcrt4_conn_np_close
,
1390 rpcrt4_conn_np_cancel_call
,
1391 rpcrt4_conn_np_wait_for_incoming_data
,
1392 rpcrt4_ncacn_np_get_top_of_tower
,
1393 rpcrt4_ncacn_np_parse_top_of_tower
,
1396 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1397 rpcrt4_conn_np_alloc
,
1398 rpcrt4_ncalrpc_open
,
1399 rpcrt4_ncalrpc_handoff
,
1400 rpcrt4_conn_np_read
,
1401 rpcrt4_conn_np_write
,
1402 rpcrt4_conn_np_close
,
1403 rpcrt4_conn_np_cancel_call
,
1404 rpcrt4_conn_np_wait_for_incoming_data
,
1405 rpcrt4_ncalrpc_get_top_of_tower
,
1406 rpcrt4_ncalrpc_parse_top_of_tower
,
1408 #ifdef HAVE_SOCKETPAIR
1410 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1411 rpcrt4_conn_tcp_alloc
,
1412 rpcrt4_ncacn_ip_tcp_open
,
1413 rpcrt4_conn_tcp_handoff
,
1414 rpcrt4_conn_tcp_read
,
1415 rpcrt4_conn_tcp_write
,
1416 rpcrt4_conn_tcp_close
,
1417 rpcrt4_conn_tcp_cancel_call
,
1418 rpcrt4_conn_tcp_wait_for_incoming_data
,
1419 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1420 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1426 static const struct protseq_ops protseq_list
[] =
1430 rpcrt4_protseq_np_alloc
,
1431 rpcrt4_protseq_np_signal_state_changed
,
1432 rpcrt4_protseq_np_get_wait_array
,
1433 rpcrt4_protseq_np_free_wait_array
,
1434 rpcrt4_protseq_np_wait_for_new_connection
,
1435 rpcrt4_protseq_ncacn_np_open_endpoint
,
1439 rpcrt4_protseq_np_alloc
,
1440 rpcrt4_protseq_np_signal_state_changed
,
1441 rpcrt4_protseq_np_get_wait_array
,
1442 rpcrt4_protseq_np_free_wait_array
,
1443 rpcrt4_protseq_np_wait_for_new_connection
,
1444 rpcrt4_protseq_ncalrpc_open_endpoint
,
1446 #ifdef HAVE_SOCKETPAIR
1449 rpcrt4_protseq_sock_alloc
,
1450 rpcrt4_protseq_sock_signal_state_changed
,
1451 rpcrt4_protseq_sock_get_wait_array
,
1452 rpcrt4_protseq_sock_free_wait_array
,
1453 rpcrt4_protseq_sock_wait_for_new_connection
,
1454 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1459 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1461 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1464 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1465 if (!strcmp(protseq_list
[i
].name
, protseq
))
1466 return &protseq_list
[i
];
1470 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1473 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1474 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1475 return &conn_protseq_list
[i
];
1479 /**** interface to rest of code ****/
1481 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1483 TRACE("(Connection == ^%p)\n", Connection
);
1485 assert(!Connection
->server
);
1486 return Connection
->ops
->open_connection_client(Connection
);
1489 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1491 TRACE("(Connection == ^%p)\n", Connection
);
1492 if (SecIsValidHandle(&Connection
->ctx
))
1494 DeleteSecurityContext(&Connection
->ctx
);
1495 SecInvalidateHandle(&Connection
->ctx
);
1497 rpcrt4_conn_close(Connection
);
1501 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1502 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1503 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1505 const struct connection_ops
*ops
;
1506 RpcConnection
* NewConnection
;
1508 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1511 FIXME("not supported for protseq %s\n", Protseq
);
1512 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1515 NewConnection
= ops
->alloc();
1516 NewConnection
->Next
= NULL
;
1517 NewConnection
->server_binding
= NULL
;
1518 NewConnection
->server
= server
;
1519 NewConnection
->ops
= ops
;
1520 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1521 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1522 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1523 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1524 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1525 NewConnection
->NextCallId
= 1;
1527 SecInvalidateHandle(&NewConnection
->ctx
);
1528 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1529 NewConnection
->attr
= 0;
1530 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1531 NewConnection
->AuthInfo
= AuthInfo
;
1532 NewConnection
->encryption_auth_len
= 0;
1533 NewConnection
->signature_auth_len
= 0;
1534 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1535 NewConnection
->QOS
= QOS
;
1537 list_init(&NewConnection
->conn_pool_entry
);
1538 NewConnection
->async_state
= NULL
;
1540 TRACE("connection: %p\n", NewConnection
);
1541 *Connection
= NewConnection
;
1546 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1550 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1551 rpcrt4_conn_get_name(OldConnection
),
1552 OldConnection
->NetworkAddr
,
1553 OldConnection
->Endpoint
, NULL
,
1554 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1555 if (err
== RPC_S_OK
)
1556 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1560 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1562 TRACE("connection: %p\n", Connection
);
1564 RPCRT4_CloseConnection(Connection
);
1565 RPCRT4_strfree(Connection
->Endpoint
);
1566 RPCRT4_strfree(Connection
->NetworkAddr
);
1567 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1568 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1569 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1572 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
1574 HeapFree(GetProcessHeap(), 0, Connection
);
1578 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1580 const char *protseq
,
1581 const char *networkaddr
,
1582 const char *endpoint
)
1584 twr_empty_floor_t
*protocol_floor
;
1585 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1590 return RPC_S_INVALID_RPC_PROTSEQ
;
1594 *tower_size
= sizeof(*protocol_floor
);
1595 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1599 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1600 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1601 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1602 protocol_floor
->count_rhs
= 0;
1604 tower_data
+= sizeof(*protocol_floor
);
1606 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1608 return EPT_S_NOT_REGISTERED
;
1610 *tower_size
+= sizeof(*protocol_floor
);
1615 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1621 const twr_empty_floor_t
*protocol_floor
;
1622 const twr_empty_floor_t
*floor4
;
1623 const struct connection_ops
*protseq_ops
= NULL
;
1627 if (tower_size
< sizeof(*protocol_floor
))
1628 return EPT_S_NOT_REGISTERED
;
1630 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1631 tower_data
+= sizeof(*protocol_floor
);
1632 tower_size
-= sizeof(*protocol_floor
);
1633 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1634 (protocol_floor
->count_rhs
> tower_size
))
1635 return EPT_S_NOT_REGISTERED
;
1636 tower_data
+= protocol_floor
->count_rhs
;
1637 tower_size
-= protocol_floor
->count_rhs
;
1639 floor4
= (const twr_empty_floor_t
*)tower_data
;
1640 if ((tower_size
< sizeof(*floor4
)) ||
1641 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1642 return EPT_S_NOT_REGISTERED
;
1644 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1645 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1646 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1648 protseq_ops
= &conn_protseq_list
[i
];
1653 return EPT_S_NOT_REGISTERED
;
1655 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1657 if ((status
== RPC_S_OK
) && protseq
)
1659 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1660 strcpy(*protseq
, protseq_ops
->name
);
1666 /***********************************************************************
1667 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1669 * Checks if the given protocol sequence is known by the RPC system.
1670 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1673 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1677 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1678 ps
, sizeof ps
, NULL
, NULL
);
1679 if (rpcrt4_get_conn_protseq_ops(ps
))
1682 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1684 return RPC_S_INVALID_RPC_PROTSEQ
;
1687 /***********************************************************************
1688 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1690 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1692 UNICODE_STRING protseqW
;
1694 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1696 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1697 RtlFreeUnicodeString(&protseqW
);
1700 return RPC_S_OUT_OF_MEMORY
;