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 /**** ncacn_np support ****/
95 typedef struct _RpcConnection_np
103 static RpcConnection
*rpcrt4_conn_np_alloc(void)
105 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
109 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
110 npc
->listening
= FALSE
;
115 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
120 npc
->listening
= TRUE
;
123 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
126 switch(GetLastError())
128 case ERROR_PIPE_CONNECTED
:
129 SetEvent(npc
->ovl
.hEvent
);
131 case ERROR_IO_PENDING
:
132 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
134 case ERROR_NO_DATA_DETECTED
:
135 /* client has disconnected, retry */
136 DisconnectNamedPipe( npc
->pipe
);
139 npc
->listening
= FALSE
;
140 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
141 return RPC_S_OUT_OF_RESOURCES
;
146 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
148 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
149 TRACE("listening on %s\n", pname
);
151 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
152 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
153 PIPE_UNLIMITED_INSTANCES
,
154 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
155 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
156 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
157 if (GetLastError() == ERROR_FILE_EXISTS
)
158 return RPC_S_DUPLICATE_ENDPOINT
;
160 return RPC_S_CANT_CREATE_ENDPOINT
;
163 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
164 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
166 /* Note: we don't call ConnectNamedPipe here because it must be done in the
167 * server thread as the thread must be alertable */
171 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
173 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
177 TRACE("connecting to %s\n", pname
);
183 dwFlags
= SECURITY_SQOS_PRESENT
;
184 switch (Connection
->QOS
->qos
->ImpersonationType
)
186 case RPC_C_IMP_LEVEL_DEFAULT
:
187 /* FIXME: what to do here? */
189 case RPC_C_IMP_LEVEL_ANONYMOUS
:
190 dwFlags
|= SECURITY_ANONYMOUS
;
192 case RPC_C_IMP_LEVEL_IDENTIFY
:
193 dwFlags
|= SECURITY_IDENTIFICATION
;
195 case RPC_C_IMP_LEVEL_IMPERSONATE
:
196 dwFlags
|= SECURITY_IMPERSONATION
;
198 case RPC_C_IMP_LEVEL_DELEGATE
:
199 dwFlags
|= SECURITY_DELEGATION
;
202 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
203 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
205 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
206 OPEN_EXISTING
, dwFlags
, 0);
207 if (pipe
!= INVALID_HANDLE_VALUE
) break;
208 err
= GetLastError();
209 if (err
== ERROR_PIPE_BUSY
) {
210 TRACE("connection failed, error=%x\n", err
);
211 return RPC_S_SERVER_TOO_BUSY
;
214 return RPC_S_SERVER_UNAVAILABLE
;
215 if (!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
);
484 tower_data
+= networkaddr_size
;
489 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
494 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
495 const twr_empty_floor_t
*nb_floor
;
497 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
499 if (tower_size
< sizeof(*smb_floor
))
500 return EPT_S_NOT_REGISTERED
;
502 tower_data
+= sizeof(*smb_floor
);
503 tower_size
-= sizeof(*smb_floor
);
505 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
506 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
507 (smb_floor
->count_rhs
> tower_size
) ||
508 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
509 return EPT_S_NOT_REGISTERED
;
513 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
515 return RPC_S_OUT_OF_RESOURCES
;
516 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
518 tower_data
+= smb_floor
->count_rhs
;
519 tower_size
-= smb_floor
->count_rhs
;
521 if (tower_size
< sizeof(*nb_floor
))
522 return EPT_S_NOT_REGISTERED
;
524 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
526 tower_data
+= sizeof(*nb_floor
);
527 tower_size
-= sizeof(*nb_floor
);
529 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
530 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
531 (nb_floor
->count_rhs
> tower_size
) ||
532 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
533 return EPT_S_NOT_REGISTERED
;
537 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
542 I_RpcFree(*endpoint
);
545 return RPC_S_OUT_OF_RESOURCES
;
547 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
553 typedef struct _RpcServerProtseq_np
555 RpcServerProtseq common
;
557 } RpcServerProtseq_np
;
559 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
561 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
563 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
567 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
569 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
570 SetEvent(npps
->mgr_event
);
573 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
575 HANDLE
*objs
= prev_array
;
576 RpcConnection_np
*conn
;
577 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
579 EnterCriticalSection(&protseq
->cs
);
581 /* open and count connections */
583 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
585 rpcrt4_conn_listen_pipe(conn
);
586 if (conn
->ovl
.hEvent
)
588 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
591 /* make array of connections */
593 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
595 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
598 ERR("couldn't allocate objs\n");
599 LeaveCriticalSection(&protseq
->cs
);
603 objs
[0] = npps
->mgr_event
;
605 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
607 if ((objs
[*count
] = conn
->ovl
.hEvent
))
609 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
611 LeaveCriticalSection(&protseq
->cs
);
615 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
617 HeapFree(GetProcessHeap(), 0, array
);
620 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
623 HANDLE
*objs
= wait_array
;
625 RpcConnection
*cconn
;
626 RpcConnection_np
*conn
;
633 /* an alertable wait isn't strictly necessary, but due to our
634 * overlapped I/O implementation in Wine we need to free some memory
635 * by the file user APC being called, even if no completion routine was
636 * specified at the time of starting the async operation */
637 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
638 } while (res
== WAIT_IO_COMPLETION
);
640 if (res
== WAIT_OBJECT_0
)
642 else if (res
== WAIT_FAILED
)
644 ERR("wait failed with error %d\n", GetLastError());
649 b_handle
= objs
[res
- WAIT_OBJECT_0
];
650 /* find which connection got a RPC */
651 EnterCriticalSection(&protseq
->cs
);
652 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
654 if (b_handle
== conn
->ovl
.hEvent
) break;
655 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
659 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
661 ERR("failed to locate connection for handle %p\n", b_handle
);
662 LeaveCriticalSection(&protseq
->cs
);
665 RPCRT4_new_client(cconn
);
672 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
673 const char *networkaddr
,
674 const char *endpoint
)
676 twr_empty_floor_t
*pipe_floor
;
678 size_t endpoint_size
;
680 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
682 endpoint_size
= strlen(endpoint
) + 1;
683 size
= sizeof(*pipe_floor
) + endpoint_size
;
688 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
690 tower_data
+= sizeof(*pipe_floor
);
692 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
693 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
694 pipe_floor
->count_rhs
= endpoint_size
;
696 memcpy(tower_data
, endpoint
, endpoint_size
);
697 tower_data
+= 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 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
;
1332 RpcConnection
*cconn
;
1333 RpcConnection_tcp
*conn
;
1338 ret
= poll(poll_info
, count
, -1);
1341 ERR("poll failed with error %d\n", ret
);
1345 for (i
= 0; i
< count
; i
++)
1346 if (poll_info
[i
].revents
& POLLIN
)
1348 /* RPC server event */
1352 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1356 /* find which connection got a RPC */
1357 EnterCriticalSection(&protseq
->cs
);
1358 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1360 if (poll_info
[i
].fd
== conn
->sock
) break;
1361 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1365 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1367 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1368 LeaveCriticalSection(&protseq
->cs
);
1370 RPCRT4_new_client(cconn
);
1378 static const struct connection_ops conn_protseq_list
[] = {
1380 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1381 rpcrt4_conn_np_alloc
,
1382 rpcrt4_ncacn_np_open
,
1383 rpcrt4_ncacn_np_handoff
,
1384 rpcrt4_conn_np_read
,
1385 rpcrt4_conn_np_write
,
1386 rpcrt4_conn_np_close
,
1387 rpcrt4_conn_np_cancel_call
,
1388 rpcrt4_conn_np_wait_for_incoming_data
,
1389 rpcrt4_ncacn_np_get_top_of_tower
,
1390 rpcrt4_ncacn_np_parse_top_of_tower
,
1393 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1394 rpcrt4_conn_np_alloc
,
1395 rpcrt4_ncalrpc_open
,
1396 rpcrt4_ncalrpc_handoff
,
1397 rpcrt4_conn_np_read
,
1398 rpcrt4_conn_np_write
,
1399 rpcrt4_conn_np_close
,
1400 rpcrt4_conn_np_cancel_call
,
1401 rpcrt4_conn_np_wait_for_incoming_data
,
1402 rpcrt4_ncalrpc_get_top_of_tower
,
1403 rpcrt4_ncalrpc_parse_top_of_tower
,
1406 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1407 rpcrt4_conn_tcp_alloc
,
1408 rpcrt4_ncacn_ip_tcp_open
,
1409 rpcrt4_conn_tcp_handoff
,
1410 rpcrt4_conn_tcp_read
,
1411 rpcrt4_conn_tcp_write
,
1412 rpcrt4_conn_tcp_close
,
1413 rpcrt4_conn_tcp_cancel_call
,
1414 rpcrt4_conn_tcp_wait_for_incoming_data
,
1415 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1416 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1421 static const struct protseq_ops protseq_list
[] =
1425 rpcrt4_protseq_np_alloc
,
1426 rpcrt4_protseq_np_signal_state_changed
,
1427 rpcrt4_protseq_np_get_wait_array
,
1428 rpcrt4_protseq_np_free_wait_array
,
1429 rpcrt4_protseq_np_wait_for_new_connection
,
1430 rpcrt4_protseq_ncacn_np_open_endpoint
,
1434 rpcrt4_protseq_np_alloc
,
1435 rpcrt4_protseq_np_signal_state_changed
,
1436 rpcrt4_protseq_np_get_wait_array
,
1437 rpcrt4_protseq_np_free_wait_array
,
1438 rpcrt4_protseq_np_wait_for_new_connection
,
1439 rpcrt4_protseq_ncalrpc_open_endpoint
,
1443 rpcrt4_protseq_sock_alloc
,
1444 rpcrt4_protseq_sock_signal_state_changed
,
1445 rpcrt4_protseq_sock_get_wait_array
,
1446 rpcrt4_protseq_sock_free_wait_array
,
1447 rpcrt4_protseq_sock_wait_for_new_connection
,
1448 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1452 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1454 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1457 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1458 if (!strcmp(protseq_list
[i
].name
, protseq
))
1459 return &protseq_list
[i
];
1463 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1466 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1467 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1468 return &conn_protseq_list
[i
];
1472 /**** interface to rest of code ****/
1474 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1476 TRACE("(Connection == ^%p)\n", Connection
);
1478 assert(!Connection
->server
);
1479 return Connection
->ops
->open_connection_client(Connection
);
1482 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1484 TRACE("(Connection == ^%p)\n", Connection
);
1485 if (SecIsValidHandle(&Connection
->ctx
))
1487 DeleteSecurityContext(&Connection
->ctx
);
1488 SecInvalidateHandle(&Connection
->ctx
);
1490 rpcrt4_conn_close(Connection
);
1494 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1495 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1496 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1498 const struct connection_ops
*ops
;
1499 RpcConnection
* NewConnection
;
1501 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1504 FIXME("not supported for protseq %s\n", Protseq
);
1505 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1508 NewConnection
= ops
->alloc();
1509 NewConnection
->Next
= NULL
;
1510 NewConnection
->server_binding
= NULL
;
1511 NewConnection
->server
= server
;
1512 NewConnection
->ops
= ops
;
1513 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1514 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1515 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1516 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1517 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1518 NewConnection
->NextCallId
= 1;
1520 SecInvalidateHandle(&NewConnection
->ctx
);
1521 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1522 NewConnection
->attr
= 0;
1523 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1524 NewConnection
->AuthInfo
= AuthInfo
;
1525 NewConnection
->encryption_auth_len
= 0;
1526 NewConnection
->signature_auth_len
= 0;
1527 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1528 NewConnection
->QOS
= QOS
;
1530 list_init(&NewConnection
->conn_pool_entry
);
1531 NewConnection
->async_state
= NULL
;
1533 TRACE("connection: %p\n", NewConnection
);
1534 *Connection
= NewConnection
;
1540 RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1544 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1545 rpcrt4_conn_get_name(OldConnection
),
1546 OldConnection
->NetworkAddr
,
1547 OldConnection
->Endpoint
, NULL
,
1548 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1549 if (err
== RPC_S_OK
)
1550 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1554 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1556 TRACE("connection: %p\n", Connection
);
1558 RPCRT4_CloseConnection(Connection
);
1559 RPCRT4_strfree(Connection
->Endpoint
);
1560 RPCRT4_strfree(Connection
->NetworkAddr
);
1561 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1562 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1563 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1566 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
1568 HeapFree(GetProcessHeap(), 0, Connection
);
1572 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1574 const char *protseq
,
1575 const char *networkaddr
,
1576 const char *endpoint
)
1578 twr_empty_floor_t
*protocol_floor
;
1579 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1584 return RPC_S_INVALID_RPC_PROTSEQ
;
1588 *tower_size
= sizeof(*protocol_floor
);
1589 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1593 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1594 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1595 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1596 protocol_floor
->count_rhs
= 0;
1598 tower_data
+= sizeof(*protocol_floor
);
1600 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1602 return EPT_S_NOT_REGISTERED
;
1604 *tower_size
+= sizeof(*protocol_floor
);
1609 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1615 const twr_empty_floor_t
*protocol_floor
;
1616 const twr_empty_floor_t
*floor4
;
1617 const struct connection_ops
*protseq_ops
= NULL
;
1621 if (tower_size
< sizeof(*protocol_floor
))
1622 return EPT_S_NOT_REGISTERED
;
1624 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1625 tower_data
+= sizeof(*protocol_floor
);
1626 tower_size
-= sizeof(*protocol_floor
);
1627 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1628 (protocol_floor
->count_rhs
> tower_size
))
1629 return EPT_S_NOT_REGISTERED
;
1630 tower_data
+= protocol_floor
->count_rhs
;
1631 tower_size
-= protocol_floor
->count_rhs
;
1633 floor4
= (const twr_empty_floor_t
*)tower_data
;
1634 if ((tower_size
< sizeof(*floor4
)) ||
1635 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1636 return EPT_S_NOT_REGISTERED
;
1638 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1639 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1640 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1642 protseq_ops
= &conn_protseq_list
[i
];
1647 return EPT_S_NOT_REGISTERED
;
1649 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1651 if ((status
== RPC_S_OK
) && protseq
)
1653 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1654 strcpy(*protseq
, protseq_ops
->name
);
1660 /***********************************************************************
1661 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1663 * Checks if the given protocol sequence is known by the RPC system.
1664 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1667 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1671 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1672 ps
, sizeof ps
, NULL
, NULL
);
1673 if (rpcrt4_get_conn_protseq_ops(ps
))
1676 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1678 return RPC_S_INVALID_RPC_PROTSEQ
;
1681 /***********************************************************************
1682 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1684 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1686 UNICODE_STRING protseqW
;
1688 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1690 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1691 RtlFreeUnicodeString(&protseqW
);
1694 return RPC_S_OUT_OF_MEMORY
;