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
&& GetLastError() == ERROR_MORE_DATA
)
387 if (!ret
|| !bytes_read
)
389 bytes_left
-= bytes_read
;
392 return ret
? count
: -1;
395 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
396 const void *buffer
, unsigned int count
)
398 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
399 const char *buf
= buffer
;
401 unsigned int bytes_left
= count
;
406 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
407 if (!ret
|| !bytes_written
)
409 bytes_left
-= bytes_written
;
410 buf
+= bytes_written
;
412 return ret
? count
: -1;
415 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
417 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
419 FlushFileBuffers(npc
->pipe
);
420 CloseHandle(npc
->pipe
);
423 if (npc
->ovl
.hEvent
) {
424 CloseHandle(npc
->ovl
.hEvent
);
430 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
432 /* FIXME: implement when named pipe writes use overlapped I/O */
435 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
437 /* FIXME: implement when named pipe writes use overlapped I/O */
441 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
442 const char *networkaddr
,
443 const char *endpoint
)
445 twr_empty_floor_t
*smb_floor
;
446 twr_empty_floor_t
*nb_floor
;
448 size_t networkaddr_size
;
449 size_t endpoint_size
;
451 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
453 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
454 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
455 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
460 smb_floor
= (twr_empty_floor_t
*)tower_data
;
462 tower_data
+= sizeof(*smb_floor
);
464 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
465 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
466 smb_floor
->count_rhs
= endpoint_size
;
469 memcpy(tower_data
, endpoint
, endpoint_size
);
472 tower_data
+= endpoint_size
;
474 nb_floor
= (twr_empty_floor_t
*)tower_data
;
476 tower_data
+= sizeof(*nb_floor
);
478 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
479 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
480 nb_floor
->count_rhs
= networkaddr_size
;
483 memcpy(tower_data
, networkaddr
, networkaddr_size
);
490 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
495 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
496 const twr_empty_floor_t
*nb_floor
;
498 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
500 if (tower_size
< sizeof(*smb_floor
))
501 return EPT_S_NOT_REGISTERED
;
503 tower_data
+= sizeof(*smb_floor
);
504 tower_size
-= sizeof(*smb_floor
);
506 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
507 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
508 (smb_floor
->count_rhs
> tower_size
) ||
509 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
510 return EPT_S_NOT_REGISTERED
;
514 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
516 return RPC_S_OUT_OF_RESOURCES
;
517 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
519 tower_data
+= smb_floor
->count_rhs
;
520 tower_size
-= smb_floor
->count_rhs
;
522 if (tower_size
< sizeof(*nb_floor
))
523 return EPT_S_NOT_REGISTERED
;
525 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
527 tower_data
+= sizeof(*nb_floor
);
528 tower_size
-= sizeof(*nb_floor
);
530 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
531 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
532 (nb_floor
->count_rhs
> tower_size
) ||
533 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
534 return EPT_S_NOT_REGISTERED
;
538 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
543 I_RpcFree(*endpoint
);
546 return RPC_S_OUT_OF_RESOURCES
;
548 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
554 typedef struct _RpcServerProtseq_np
556 RpcServerProtseq common
;
558 } RpcServerProtseq_np
;
560 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
562 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
564 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
568 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
570 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
571 SetEvent(npps
->mgr_event
);
574 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
576 HANDLE
*objs
= prev_array
;
577 RpcConnection_np
*conn
;
578 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
580 EnterCriticalSection(&protseq
->cs
);
582 /* open and count connections */
584 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
586 rpcrt4_conn_listen_pipe(conn
);
587 if (conn
->ovl
.hEvent
)
589 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
592 /* make array of connections */
594 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
596 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
599 ERR("couldn't allocate objs\n");
600 LeaveCriticalSection(&protseq
->cs
);
604 objs
[0] = npps
->mgr_event
;
606 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
608 if ((objs
[*count
] = conn
->ovl
.hEvent
))
610 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
612 LeaveCriticalSection(&protseq
->cs
);
616 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
618 HeapFree(GetProcessHeap(), 0, array
);
621 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
624 HANDLE
*objs
= wait_array
;
626 RpcConnection
*cconn
;
627 RpcConnection_np
*conn
;
634 /* an alertable wait isn't strictly necessary, but due to our
635 * overlapped I/O implementation in Wine we need to free some memory
636 * by the file user APC being called, even if no completion routine was
637 * specified at the time of starting the async operation */
638 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
639 } while (res
== WAIT_IO_COMPLETION
);
641 if (res
== WAIT_OBJECT_0
)
643 else if (res
== WAIT_FAILED
)
645 ERR("wait failed with error %d\n", GetLastError());
650 b_handle
= objs
[res
- WAIT_OBJECT_0
];
651 /* find which connection got a RPC */
652 EnterCriticalSection(&protseq
->cs
);
653 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
655 if (b_handle
== conn
->ovl
.hEvent
) break;
656 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
660 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
662 ERR("failed to locate connection for handle %p\n", b_handle
);
663 LeaveCriticalSection(&protseq
->cs
);
666 RPCRT4_new_client(cconn
);
673 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
674 const char *networkaddr
,
675 const char *endpoint
)
677 twr_empty_floor_t
*pipe_floor
;
679 size_t endpoint_size
;
681 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
683 endpoint_size
= strlen(endpoint
) + 1;
684 size
= sizeof(*pipe_floor
) + endpoint_size
;
689 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
691 tower_data
+= sizeof(*pipe_floor
);
693 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
694 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
695 pipe_floor
->count_rhs
= endpoint_size
;
697 memcpy(tower_data
, endpoint
, endpoint_size
);
702 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
707 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
709 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
711 if (tower_size
< sizeof(*pipe_floor
))
712 return EPT_S_NOT_REGISTERED
;
714 tower_data
+= sizeof(*pipe_floor
);
715 tower_size
-= sizeof(*pipe_floor
);
717 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
718 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
719 (pipe_floor
->count_rhs
> tower_size
) ||
720 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
721 return EPT_S_NOT_REGISTERED
;
728 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
730 return RPC_S_OUT_OF_RESOURCES
;
731 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
737 /**** ncacn_ip_tcp support ****/
739 #ifdef HAVE_SOCKETPAIR
741 typedef struct _RpcConnection_tcp
743 RpcConnection common
;
748 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
750 RpcConnection_tcp
*tcpc
;
751 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
755 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
757 ERR("socketpair() failed: %s\n", strerror(errno
));
758 HeapFree(GetProcessHeap(), 0, tcpc
);
761 return &tcpc
->common
;
764 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
766 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
770 struct addrinfo
*ai_cur
;
771 struct addrinfo hints
;
773 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
775 if (tcpc
->sock
!= -1)
779 hints
.ai_family
= PF_UNSPEC
;
780 hints
.ai_socktype
= SOCK_STREAM
;
781 hints
.ai_protocol
= IPPROTO_TCP
;
782 hints
.ai_addrlen
= 0;
783 hints
.ai_addr
= NULL
;
784 hints
.ai_canonname
= NULL
;
785 hints
.ai_next
= NULL
;
787 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
790 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
791 Connection
->Endpoint
, gai_strerror(ret
));
792 return RPC_S_SERVER_UNAVAILABLE
;
795 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
803 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
804 host
, sizeof(host
), service
, sizeof(service
),
805 NI_NUMERICHOST
| NI_NUMERICSERV
);
806 TRACE("trying %s:%s\n", host
, service
);
809 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
812 WARN("socket() failed: %s\n", strerror(errno
));
816 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
818 WARN("connect() failed: %s\n", strerror(errno
));
823 /* RPC depends on having minimal latency so disable the Nagle algorithm */
825 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
826 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
831 TRACE("connected\n");
836 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
837 return RPC_S_SERVER_UNAVAILABLE
;
840 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
842 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
846 struct addrinfo
*ai_cur
;
847 struct addrinfo hints
;
848 RpcConnection
*first_connection
= NULL
;
850 TRACE("(%p, %s)\n", protseq
, endpoint
);
852 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
853 hints
.ai_family
= PF_UNSPEC
;
854 hints
.ai_socktype
= SOCK_STREAM
;
855 hints
.ai_protocol
= IPPROTO_TCP
;
856 hints
.ai_addrlen
= 0;
857 hints
.ai_addr
= NULL
;
858 hints
.ai_canonname
= NULL
;
859 hints
.ai_next
= NULL
;
861 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
864 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
866 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
867 return RPC_S_INVALID_ENDPOINT_FORMAT
;
868 return RPC_S_CANT_CREATE_ENDPOINT
;
871 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
873 RpcConnection_tcp
*tcpc
;
874 RPC_STATUS create_status
;
880 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
881 host
, sizeof(host
), service
, sizeof(service
),
882 NI_NUMERICHOST
| NI_NUMERICSERV
);
883 TRACE("trying %s:%s\n", host
, service
);
886 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
889 WARN("socket() failed: %s\n", strerror(errno
));
890 status
= RPC_S_CANT_CREATE_ENDPOINT
;
894 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
897 WARN("bind failed: %s\n", strerror(errno
));
899 if (errno
== EADDRINUSE
)
900 status
= RPC_S_DUPLICATE_ENDPOINT
;
902 status
= RPC_S_CANT_CREATE_ENDPOINT
;
905 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
906 protseq
->Protseq
, NULL
,
907 endpoint
, NULL
, NULL
, NULL
);
908 if (create_status
!= RPC_S_OK
)
911 status
= create_status
;
916 ret
= listen(sock
, protseq
->MaxCalls
);
919 WARN("listen failed: %s\n", strerror(errno
));
920 RPCRT4_DestroyConnection(&tcpc
->common
);
921 status
= RPC_S_OUT_OF_RESOURCES
;
924 /* need a non-blocking socket, otherwise accept() has a potential
925 * race-condition (poll() says it is readable, connection drops,
926 * and accept() blocks until the next connection comes...)
928 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
931 WARN("couldn't make socket non-blocking, error %d\n", ret
);
932 RPCRT4_DestroyConnection(&tcpc
->common
);
933 status
= RPC_S_OUT_OF_RESOURCES
;
937 tcpc
->common
.Next
= first_connection
;
938 first_connection
= &tcpc
->common
;
943 /* if at least one connection was created for an endpoint then
945 if (first_connection
)
949 /* find last element in list */
950 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
953 EnterCriticalSection(&protseq
->cs
);
954 conn
->Next
= protseq
->conn
;
955 protseq
->conn
= first_connection
;
956 LeaveCriticalSection(&protseq
->cs
);
958 TRACE("listening on %s\n", endpoint
);
962 ERR("couldn't listen on port %s\n", endpoint
);
966 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
969 struct sockaddr_in address
;
971 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
972 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
974 addrsize
= sizeof(address
);
975 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
978 ERR("Failed to accept a TCP connection: error %d\n", ret
);
979 return RPC_S_OUT_OF_RESOURCES
;
981 /* reset to blocking behaviour */
982 fcntl(ret
, F_SETFL
, 0);
984 TRACE("Accepted a new TCP connection\n");
988 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
989 void *buffer
, unsigned int count
)
991 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
995 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1000 else if (errno
!= EAGAIN
)
1002 WARN("recv() failed: %s\n", strerror(errno
));
1007 struct pollfd pfds
[2];
1008 pfds
[0].fd
= tcpc
->sock
;
1009 pfds
[0].events
= POLLIN
;
1010 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1011 pfds
[1].events
= POLLIN
;
1012 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1014 ERR("poll() failed: %s\n", strerror(errno
));
1017 if (pfds
[1].revents
& POLLIN
) /* canceled */
1020 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1024 } while (bytes_read
!= count
);
1025 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1029 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1030 const void *buffer
, unsigned int count
)
1032 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1033 int bytes_written
= 0;
1036 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1039 else if (errno
!= EAGAIN
)
1044 pfd
.fd
= tcpc
->sock
;
1045 pfd
.events
= POLLOUT
;
1046 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1048 ERR("poll() failed: %s\n", strerror(errno
));
1052 } while (bytes_written
!= count
);
1053 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1054 return bytes_written
;
1057 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1059 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1061 TRACE("%d\n", tcpc
->sock
);
1063 if (tcpc
->sock
!= -1)
1064 closesocket(tcpc
->sock
);
1066 close(tcpc
->cancel_fds
[0]);
1067 close(tcpc
->cancel_fds
[1]);
1071 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1073 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1076 TRACE("%p\n", Connection
);
1078 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1081 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1083 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1084 struct pollfd pfds
[2];
1086 TRACE("%p\n", Connection
);
1088 pfds
[0].fd
= tcpc
->sock
;
1089 pfds
[0].events
= POLLIN
;
1090 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1091 pfds
[1].events
= POLLIN
;
1092 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1094 ERR("poll() failed: %s\n", strerror(errno
));
1097 if (pfds
[1].revents
& POLLIN
) /* canceled */
1100 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1107 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1108 const char *networkaddr
,
1109 const char *endpoint
)
1111 twr_tcp_floor_t
*tcp_floor
;
1112 twr_ipv4_floor_t
*ipv4_floor
;
1113 struct addrinfo
*ai
;
1114 struct addrinfo hints
;
1116 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1118 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1123 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1124 tower_data
+= sizeof(*tcp_floor
);
1126 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1128 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1129 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1130 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1132 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1133 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1134 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1136 hints
.ai_flags
= AI_NUMERICHOST
;
1137 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1138 hints
.ai_family
= PF_INET
;
1139 hints
.ai_socktype
= SOCK_STREAM
;
1140 hints
.ai_protocol
= IPPROTO_TCP
;
1141 hints
.ai_addrlen
= 0;
1142 hints
.ai_addr
= NULL
;
1143 hints
.ai_canonname
= NULL
;
1144 hints
.ai_next
= NULL
;
1146 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1149 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1152 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1157 if (ai
->ai_family
== PF_INET
)
1159 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1160 tcp_floor
->port
= sin
->sin_port
;
1161 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1165 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1174 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1179 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1180 const twr_ipv4_floor_t
*ipv4_floor
;
1181 struct in_addr in_addr
;
1183 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1185 if (tower_size
< sizeof(*tcp_floor
))
1186 return EPT_S_NOT_REGISTERED
;
1188 tower_data
+= sizeof(*tcp_floor
);
1189 tower_size
-= sizeof(*tcp_floor
);
1191 if (tower_size
< sizeof(*ipv4_floor
))
1192 return EPT_S_NOT_REGISTERED
;
1194 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1196 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1197 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1198 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1199 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1200 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1201 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1202 return EPT_S_NOT_REGISTERED
;
1206 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1208 return RPC_S_OUT_OF_RESOURCES
;
1209 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1214 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1219 I_RpcFree(*endpoint
);
1222 return RPC_S_OUT_OF_RESOURCES
;
1224 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1225 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1227 ERR("inet_ntop: %s\n", strerror(errno
));
1228 I_RpcFree(*networkaddr
);
1229 *networkaddr
= NULL
;
1232 I_RpcFree(*endpoint
);
1235 return EPT_S_NOT_REGISTERED
;
1242 typedef struct _RpcServerProtseq_sock
1244 RpcServerProtseq common
;
1247 } RpcServerProtseq_sock
;
1249 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1251 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1255 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1257 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1258 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1259 ps
->mgr_event_rcv
= fds
[0];
1260 ps
->mgr_event_snd
= fds
[1];
1264 ERR("socketpair failed with error %s\n", strerror(errno
));
1265 HeapFree(GetProcessHeap(), 0, ps
);
1272 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1274 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1276 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1279 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1281 struct pollfd
*poll_info
= prev_array
;
1282 RpcConnection_tcp
*conn
;
1283 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1285 EnterCriticalSection(&protseq
->cs
);
1287 /* open and count connections */
1289 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1291 if (conn
->sock
!= -1)
1293 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1296 /* make array of connections */
1298 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1300 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1303 ERR("couldn't allocate poll_info\n");
1304 LeaveCriticalSection(&protseq
->cs
);
1308 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1309 poll_info
[0].events
= POLLIN
;
1311 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1313 if (conn
->sock
!= -1)
1315 poll_info
[*count
].fd
= conn
->sock
;
1316 poll_info
[*count
].events
= POLLIN
;
1319 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1321 LeaveCriticalSection(&protseq
->cs
);
1325 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1327 HeapFree(GetProcessHeap(), 0, array
);
1330 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1332 struct pollfd
*poll_info
= wait_array
;
1335 RpcConnection
*cconn
;
1336 RpcConnection_tcp
*conn
;
1341 ret
= poll(poll_info
, count
, -1);
1344 ERR("poll failed with error %d\n", ret
);
1348 for (i
= 0; i
< count
; i
++)
1349 if (poll_info
[i
].revents
& POLLIN
)
1351 /* RPC server event */
1355 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1359 /* find which connection got a RPC */
1360 EnterCriticalSection(&protseq
->cs
);
1361 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1363 if (poll_info
[i
].fd
== conn
->sock
) break;
1364 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1368 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1370 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1371 LeaveCriticalSection(&protseq
->cs
);
1373 RPCRT4_new_client(cconn
);
1381 #endif /* HAVE_SOCKETPAIR */
1383 static const struct connection_ops conn_protseq_list
[] = {
1385 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1386 rpcrt4_conn_np_alloc
,
1387 rpcrt4_ncacn_np_open
,
1388 rpcrt4_ncacn_np_handoff
,
1389 rpcrt4_conn_np_read
,
1390 rpcrt4_conn_np_write
,
1391 rpcrt4_conn_np_close
,
1392 rpcrt4_conn_np_cancel_call
,
1393 rpcrt4_conn_np_wait_for_incoming_data
,
1394 rpcrt4_ncacn_np_get_top_of_tower
,
1395 rpcrt4_ncacn_np_parse_top_of_tower
,
1398 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1399 rpcrt4_conn_np_alloc
,
1400 rpcrt4_ncalrpc_open
,
1401 rpcrt4_ncalrpc_handoff
,
1402 rpcrt4_conn_np_read
,
1403 rpcrt4_conn_np_write
,
1404 rpcrt4_conn_np_close
,
1405 rpcrt4_conn_np_cancel_call
,
1406 rpcrt4_conn_np_wait_for_incoming_data
,
1407 rpcrt4_ncalrpc_get_top_of_tower
,
1408 rpcrt4_ncalrpc_parse_top_of_tower
,
1410 #ifdef HAVE_SOCKETPAIR
1412 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1413 rpcrt4_conn_tcp_alloc
,
1414 rpcrt4_ncacn_ip_tcp_open
,
1415 rpcrt4_conn_tcp_handoff
,
1416 rpcrt4_conn_tcp_read
,
1417 rpcrt4_conn_tcp_write
,
1418 rpcrt4_conn_tcp_close
,
1419 rpcrt4_conn_tcp_cancel_call
,
1420 rpcrt4_conn_tcp_wait_for_incoming_data
,
1421 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1422 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1428 static const struct protseq_ops protseq_list
[] =
1432 rpcrt4_protseq_np_alloc
,
1433 rpcrt4_protseq_np_signal_state_changed
,
1434 rpcrt4_protseq_np_get_wait_array
,
1435 rpcrt4_protseq_np_free_wait_array
,
1436 rpcrt4_protseq_np_wait_for_new_connection
,
1437 rpcrt4_protseq_ncacn_np_open_endpoint
,
1441 rpcrt4_protseq_np_alloc
,
1442 rpcrt4_protseq_np_signal_state_changed
,
1443 rpcrt4_protseq_np_get_wait_array
,
1444 rpcrt4_protseq_np_free_wait_array
,
1445 rpcrt4_protseq_np_wait_for_new_connection
,
1446 rpcrt4_protseq_ncalrpc_open_endpoint
,
1448 #ifdef HAVE_SOCKETPAIR
1451 rpcrt4_protseq_sock_alloc
,
1452 rpcrt4_protseq_sock_signal_state_changed
,
1453 rpcrt4_protseq_sock_get_wait_array
,
1454 rpcrt4_protseq_sock_free_wait_array
,
1455 rpcrt4_protseq_sock_wait_for_new_connection
,
1456 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1461 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1463 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1466 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1467 if (!strcmp(protseq_list
[i
].name
, protseq
))
1468 return &protseq_list
[i
];
1472 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1475 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1476 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1477 return &conn_protseq_list
[i
];
1481 /**** interface to rest of code ****/
1483 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1485 TRACE("(Connection == ^%p)\n", Connection
);
1487 assert(!Connection
->server
);
1488 return Connection
->ops
->open_connection_client(Connection
);
1491 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1493 TRACE("(Connection == ^%p)\n", Connection
);
1494 if (SecIsValidHandle(&Connection
->ctx
))
1496 DeleteSecurityContext(&Connection
->ctx
);
1497 SecInvalidateHandle(&Connection
->ctx
);
1499 rpcrt4_conn_close(Connection
);
1503 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1504 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1505 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1507 const struct connection_ops
*ops
;
1508 RpcConnection
* NewConnection
;
1510 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1513 FIXME("not supported for protseq %s\n", Protseq
);
1514 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1517 NewConnection
= ops
->alloc();
1518 NewConnection
->Next
= NULL
;
1519 NewConnection
->server_binding
= NULL
;
1520 NewConnection
->server
= server
;
1521 NewConnection
->ops
= ops
;
1522 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1523 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1524 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1525 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1526 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1527 NewConnection
->NextCallId
= 1;
1529 SecInvalidateHandle(&NewConnection
->ctx
);
1530 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1531 NewConnection
->attr
= 0;
1532 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1533 NewConnection
->AuthInfo
= AuthInfo
;
1534 NewConnection
->encryption_auth_len
= 0;
1535 NewConnection
->signature_auth_len
= 0;
1536 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1537 NewConnection
->QOS
= QOS
;
1539 list_init(&NewConnection
->conn_pool_entry
);
1540 NewConnection
->async_state
= NULL
;
1542 TRACE("connection: %p\n", NewConnection
);
1543 *Connection
= NewConnection
;
1548 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1552 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1553 rpcrt4_conn_get_name(OldConnection
),
1554 OldConnection
->NetworkAddr
,
1555 OldConnection
->Endpoint
, NULL
,
1556 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1557 if (err
== RPC_S_OK
)
1558 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1562 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1564 TRACE("connection: %p\n", Connection
);
1566 RPCRT4_CloseConnection(Connection
);
1567 RPCRT4_strfree(Connection
->Endpoint
);
1568 RPCRT4_strfree(Connection
->NetworkAddr
);
1569 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1570 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1571 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1574 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
1576 HeapFree(GetProcessHeap(), 0, Connection
);
1580 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1582 const char *protseq
,
1583 const char *networkaddr
,
1584 const char *endpoint
)
1586 twr_empty_floor_t
*protocol_floor
;
1587 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1592 return RPC_S_INVALID_RPC_PROTSEQ
;
1596 *tower_size
= sizeof(*protocol_floor
);
1597 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1601 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1602 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1603 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1604 protocol_floor
->count_rhs
= 0;
1606 tower_data
+= sizeof(*protocol_floor
);
1608 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1610 return EPT_S_NOT_REGISTERED
;
1612 *tower_size
+= sizeof(*protocol_floor
);
1617 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1623 const twr_empty_floor_t
*protocol_floor
;
1624 const twr_empty_floor_t
*floor4
;
1625 const struct connection_ops
*protseq_ops
= NULL
;
1629 if (tower_size
< sizeof(*protocol_floor
))
1630 return EPT_S_NOT_REGISTERED
;
1632 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1633 tower_data
+= sizeof(*protocol_floor
);
1634 tower_size
-= sizeof(*protocol_floor
);
1635 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1636 (protocol_floor
->count_rhs
> tower_size
))
1637 return EPT_S_NOT_REGISTERED
;
1638 tower_data
+= protocol_floor
->count_rhs
;
1639 tower_size
-= protocol_floor
->count_rhs
;
1641 floor4
= (const twr_empty_floor_t
*)tower_data
;
1642 if ((tower_size
< sizeof(*floor4
)) ||
1643 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1644 return EPT_S_NOT_REGISTERED
;
1646 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1647 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1648 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1650 protseq_ops
= &conn_protseq_list
[i
];
1655 return EPT_S_NOT_REGISTERED
;
1657 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1659 if ((status
== RPC_S_OK
) && protseq
)
1661 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1662 strcpy(*protseq
, protseq_ops
->name
);
1668 /***********************************************************************
1669 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1671 * Checks if the given protocol sequence is known by the RPC system.
1672 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1675 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1679 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1680 ps
, sizeof ps
, NULL
, NULL
);
1681 if (rpcrt4_get_conn_protseq_ops(ps
))
1684 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1686 return RPC_S_INVALID_RPC_PROTSEQ
;
1689 /***********************************************************************
1690 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1692 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1694 UNICODE_STRING protseqW
;
1696 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1698 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1699 RtlFreeUnicodeString(&protseqW
);
1702 return RPC_S_OUT_OF_MEMORY
;