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 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
74 #include "wine/unicode.h"
79 #include "wine/debug.h"
81 #include "rpc_binding.h"
82 #include "rpc_message.h"
83 #include "rpc_server.h"
84 #include "epm_towers.h"
87 # define SOL_TCP IPPROTO_TCP
90 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
92 /**** ncacn_np support ****/
94 typedef struct _RpcConnection_np
102 static RpcConnection
*rpcrt4_conn_np_alloc(void)
104 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
108 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
109 npc
->listening
= FALSE
;
114 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
119 npc
->listening
= TRUE
;
120 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
123 if (GetLastError() == ERROR_PIPE_CONNECTED
) {
124 SetEvent(npc
->ovl
.hEvent
);
127 if (GetLastError() == ERROR_IO_PENDING
) {
128 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
131 npc
->listening
= FALSE
;
132 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
133 return RPC_S_OUT_OF_RESOURCES
;
136 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
138 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
139 TRACE("listening on %s\n", pname
);
141 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
142 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
143 PIPE_UNLIMITED_INSTANCES
,
144 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
145 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
146 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
147 if (GetLastError() == ERROR_FILE_EXISTS
)
148 return RPC_S_DUPLICATE_ENDPOINT
;
150 return RPC_S_CANT_CREATE_ENDPOINT
;
153 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
154 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
156 /* Note: we don't call ConnectNamedPipe here because it must be done in the
157 * server thread as the thread must be alertable */
161 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
163 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
167 TRACE("connecting to %s\n", pname
);
173 dwFlags
= SECURITY_SQOS_PRESENT
;
174 switch (Connection
->QOS
->qos
->ImpersonationType
)
176 case RPC_C_IMP_LEVEL_DEFAULT
:
177 /* FIXME: what to do here? */
179 case RPC_C_IMP_LEVEL_ANONYMOUS
:
180 dwFlags
|= SECURITY_ANONYMOUS
;
182 case RPC_C_IMP_LEVEL_IDENTIFY
:
183 dwFlags
|= SECURITY_IDENTIFICATION
;
185 case RPC_C_IMP_LEVEL_IMPERSONATE
:
186 dwFlags
|= SECURITY_IMPERSONATION
;
188 case RPC_C_IMP_LEVEL_DELEGATE
:
189 dwFlags
|= SECURITY_DELEGATION
;
192 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
193 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
195 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
196 OPEN_EXISTING
, dwFlags
, 0);
197 if (pipe
!= INVALID_HANDLE_VALUE
) break;
198 err
= GetLastError();
199 if (err
== ERROR_PIPE_BUSY
) {
200 TRACE("connection failed, error=%x\n", err
);
201 return RPC_S_SERVER_TOO_BUSY
;
204 return RPC_S_SERVER_UNAVAILABLE
;
205 if (!WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
206 err
= GetLastError();
207 WARN("connection failed, error=%x\n", err
);
208 return RPC_S_SERVER_UNAVAILABLE
;
213 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
214 /* pipe is connected; change to message-read mode. */
215 dwMode
= PIPE_READMODE_MESSAGE
;
216 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
217 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
223 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
225 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
226 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
230 /* already connected? */
234 /* protseq=ncalrpc: supposed to use NT LPC ports,
235 * but we'll implement it with named pipes for now */
236 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
237 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
238 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
244 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, LPSTR endpoint
)
246 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
249 RpcConnection
*Connection
;
251 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
252 endpoint
, NULL
, NULL
, NULL
);
256 /* protseq=ncalrpc: supposed to use NT LPC ports,
257 * but we'll implement it with named pipes for now */
258 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
259 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
260 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
263 EnterCriticalSection(&protseq
->cs
);
264 Connection
->Next
= protseq
->conn
;
265 protseq
->conn
= Connection
;
266 LeaveCriticalSection(&protseq
->cs
);
271 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
273 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
274 static const char prefix
[] = "\\\\.";
278 /* already connected? */
282 /* protseq=ncacn_np: named pipes */
283 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
284 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
285 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
291 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
293 static const char prefix
[] = "\\\\.";
296 RpcConnection
*Connection
;
298 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
299 endpoint
, NULL
, NULL
, NULL
);
303 /* protseq=ncacn_np: named pipes */
304 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
305 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
306 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
309 EnterCriticalSection(&protseq
->cs
);
310 Connection
->Next
= protseq
->conn
;
311 protseq
->conn
= Connection
;
312 LeaveCriticalSection(&protseq
->cs
);
317 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
319 /* because of the way named pipes work, we'll transfer the connected pipe
320 * to the child, then reopen the server binding to continue listening */
322 new_npc
->pipe
= old_npc
->pipe
;
323 new_npc
->ovl
= old_npc
->ovl
;
325 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
326 old_npc
->listening
= FALSE
;
329 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
333 static const char prefix
[] = "\\\\.";
335 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
337 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
338 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
339 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
345 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
349 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
351 TRACE("%s\n", old_conn
->Endpoint
);
353 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
355 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
356 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
357 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
363 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
364 void *buffer
, unsigned int count
)
366 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
369 unsigned int bytes_left
= count
;
374 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
375 if (!ret
|| !bytes_read
)
377 bytes_left
-= bytes_read
;
380 return ret
? count
: -1;
383 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
384 const void *buffer
, unsigned int count
)
386 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
387 const char *buf
= buffer
;
389 unsigned int bytes_left
= count
;
394 ret
= WriteFile(npc
->pipe
, buf
, count
, &bytes_written
, NULL
);
395 if (!ret
|| !bytes_written
)
397 bytes_left
-= bytes_written
;
398 buf
+= bytes_written
;
400 return ret
? count
: -1;
403 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
405 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
407 FlushFileBuffers(npc
->pipe
);
408 CloseHandle(npc
->pipe
);
411 if (npc
->ovl
.hEvent
) {
412 CloseHandle(npc
->ovl
.hEvent
);
418 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
420 /* FIXME: implement when named pipe writes use overlapped I/O */
423 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
425 /* FIXME: implement when named pipe writes use overlapped I/O */
429 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
430 const char *networkaddr
,
431 const char *endpoint
)
433 twr_empty_floor_t
*smb_floor
;
434 twr_empty_floor_t
*nb_floor
;
436 size_t networkaddr_size
;
437 size_t endpoint_size
;
439 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
441 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
442 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
443 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
448 smb_floor
= (twr_empty_floor_t
*)tower_data
;
450 tower_data
+= sizeof(*smb_floor
);
452 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
453 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
454 smb_floor
->count_rhs
= endpoint_size
;
457 memcpy(tower_data
, endpoint
, endpoint_size
);
460 tower_data
+= endpoint_size
;
462 nb_floor
= (twr_empty_floor_t
*)tower_data
;
464 tower_data
+= sizeof(*nb_floor
);
466 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
467 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
468 nb_floor
->count_rhs
= networkaddr_size
;
471 memcpy(tower_data
, networkaddr
, networkaddr_size
);
474 tower_data
+= networkaddr_size
;
479 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
484 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
485 const twr_empty_floor_t
*nb_floor
;
487 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
489 if (tower_size
< sizeof(*smb_floor
))
490 return EPT_S_NOT_REGISTERED
;
492 tower_data
+= sizeof(*smb_floor
);
493 tower_size
-= sizeof(*smb_floor
);
495 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
496 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
497 (smb_floor
->count_rhs
> tower_size
))
498 return EPT_S_NOT_REGISTERED
;
502 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
504 return RPC_S_OUT_OF_RESOURCES
;
505 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
507 tower_data
+= smb_floor
->count_rhs
;
508 tower_size
-= smb_floor
->count_rhs
;
510 if (tower_size
< sizeof(*nb_floor
))
511 return EPT_S_NOT_REGISTERED
;
513 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
515 tower_data
+= sizeof(*nb_floor
);
516 tower_size
-= sizeof(*nb_floor
);
518 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
519 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
520 (nb_floor
->count_rhs
> tower_size
))
521 return EPT_S_NOT_REGISTERED
;
525 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
530 I_RpcFree(*endpoint
);
533 return RPC_S_OUT_OF_RESOURCES
;
535 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
541 typedef struct _RpcServerProtseq_np
543 RpcServerProtseq common
;
545 } RpcServerProtseq_np
;
547 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
549 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
551 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
555 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
557 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
558 SetEvent(npps
->mgr_event
);
561 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
563 HANDLE
*objs
= prev_array
;
564 RpcConnection_np
*conn
;
565 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
567 EnterCriticalSection(&protseq
->cs
);
569 /* open and count connections */
571 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
573 rpcrt4_conn_listen_pipe(conn
);
574 if (conn
->ovl
.hEvent
)
576 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
579 /* make array of connections */
581 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
583 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
586 ERR("couldn't allocate objs\n");
587 LeaveCriticalSection(&protseq
->cs
);
591 objs
[0] = npps
->mgr_event
;
593 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
595 if ((objs
[*count
] = conn
->ovl
.hEvent
))
597 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
599 LeaveCriticalSection(&protseq
->cs
);
603 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
605 HeapFree(GetProcessHeap(), 0, array
);
608 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
611 HANDLE
*objs
= wait_array
;
613 RpcConnection
*cconn
;
614 RpcConnection_np
*conn
;
621 /* an alertable wait isn't strictly necessary, but due to our
622 * overlapped I/O implementation in Wine we need to free some memory
623 * by the file user APC being called, even if no completion routine was
624 * specified at the time of starting the async operation */
625 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
626 } while (res
== WAIT_IO_COMPLETION
);
628 if (res
== WAIT_OBJECT_0
)
630 else if (res
== WAIT_FAILED
)
632 ERR("wait failed with error %d\n", GetLastError());
637 b_handle
= objs
[res
- WAIT_OBJECT_0
];
638 /* find which connection got a RPC */
639 EnterCriticalSection(&protseq
->cs
);
640 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
642 if (b_handle
== conn
->ovl
.hEvent
) break;
643 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
647 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
649 ERR("failed to locate connection for handle %p\n", b_handle
);
650 LeaveCriticalSection(&protseq
->cs
);
653 RPCRT4_new_client(cconn
);
660 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
661 const char *networkaddr
,
662 const char *endpoint
)
664 twr_empty_floor_t
*pipe_floor
;
666 size_t endpoint_size
;
668 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
670 endpoint_size
= strlen(networkaddr
) + 1;
671 size
= sizeof(*pipe_floor
) + endpoint_size
;
676 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
678 tower_data
+= sizeof(*pipe_floor
);
680 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
681 pipe_floor
->protid
= EPM_PROTOCOL_SMB
;
682 pipe_floor
->count_rhs
= endpoint_size
;
684 memcpy(tower_data
, endpoint
, endpoint_size
);
685 tower_data
+= endpoint_size
;
690 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
695 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
697 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
702 if (tower_size
< sizeof(*pipe_floor
))
703 return EPT_S_NOT_REGISTERED
;
705 tower_data
+= sizeof(*pipe_floor
);
706 tower_size
-= sizeof(*pipe_floor
);
708 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
709 (pipe_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
710 (pipe_floor
->count_rhs
> tower_size
))
711 return EPT_S_NOT_REGISTERED
;
715 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
717 return RPC_S_OUT_OF_RESOURCES
;
718 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
724 /**** ncacn_ip_tcp support ****/
726 typedef struct _RpcConnection_tcp
728 RpcConnection common
;
733 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
735 RpcConnection_tcp
*tcpc
;
736 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
740 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
742 ERR("socketpair() failed: %s\n", strerror(errno
));
743 HeapFree(GetProcessHeap(), 0, tcpc
);
746 return &tcpc
->common
;
749 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
751 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
755 struct addrinfo
*ai_cur
;
756 struct addrinfo hints
;
758 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
760 if (tcpc
->sock
!= -1)
764 hints
.ai_family
= PF_UNSPEC
;
765 hints
.ai_socktype
= SOCK_STREAM
;
766 hints
.ai_protocol
= IPPROTO_TCP
;
767 hints
.ai_addrlen
= 0;
768 hints
.ai_addr
= NULL
;
769 hints
.ai_canonname
= NULL
;
770 hints
.ai_next
= NULL
;
772 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
775 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
776 Connection
->Endpoint
, gai_strerror(ret
));
777 return RPC_S_SERVER_UNAVAILABLE
;
780 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
788 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
789 host
, sizeof(host
), service
, sizeof(service
),
790 NI_NUMERICHOST
| NI_NUMERICSERV
);
791 TRACE("trying %s:%s\n", host
, service
);
794 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
797 WARN("socket() failed: %s\n", strerror(errno
));
801 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
803 WARN("connect() failed: %s\n", strerror(errno
));
808 /* RPC depends on having minimal latency so disable the Nagle algorithm */
810 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
811 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
816 TRACE("connected\n");
821 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
822 return RPC_S_SERVER_UNAVAILABLE
;
825 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
827 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
831 struct addrinfo
*ai_cur
;
832 struct addrinfo hints
;
833 RpcConnection
*first_connection
= NULL
;
835 TRACE("(%p, %s)\n", protseq
, endpoint
);
837 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
838 hints
.ai_family
= PF_UNSPEC
;
839 hints
.ai_socktype
= SOCK_STREAM
;
840 hints
.ai_protocol
= IPPROTO_TCP
;
841 hints
.ai_addrlen
= 0;
842 hints
.ai_addr
= NULL
;
843 hints
.ai_canonname
= NULL
;
844 hints
.ai_next
= NULL
;
846 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
849 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
851 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
852 return RPC_S_INVALID_ENDPOINT_FORMAT
;
853 return RPC_S_CANT_CREATE_ENDPOINT
;
856 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
858 RpcConnection_tcp
*tcpc
;
859 RPC_STATUS create_status
;
865 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
866 host
, sizeof(host
), service
, sizeof(service
),
867 NI_NUMERICHOST
| NI_NUMERICSERV
);
868 TRACE("trying %s:%s\n", host
, service
);
871 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
874 WARN("socket() failed: %s\n", strerror(errno
));
875 status
= RPC_S_CANT_CREATE_ENDPOINT
;
879 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
882 WARN("bind failed: %s\n", strerror(errno
));
884 if (errno
== EADDRINUSE
)
885 status
= RPC_S_DUPLICATE_ENDPOINT
;
887 status
= RPC_S_CANT_CREATE_ENDPOINT
;
890 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
891 protseq
->Protseq
, NULL
,
892 endpoint
, NULL
, NULL
, NULL
);
893 if (create_status
!= RPC_S_OK
)
896 status
= create_status
;
901 ret
= listen(sock
, protseq
->MaxCalls
);
904 WARN("listen failed: %s\n", strerror(errno
));
905 RPCRT4_DestroyConnection(&tcpc
->common
);
906 status
= RPC_S_OUT_OF_RESOURCES
;
909 /* need a non-blocking socket, otherwise accept() has a potential
910 * race-condition (poll() says it is readable, connection drops,
911 * and accept() blocks until the next connection comes...)
913 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
916 WARN("couldn't make socket non-blocking, error %d\n", ret
);
917 RPCRT4_DestroyConnection(&tcpc
->common
);
918 status
= RPC_S_OUT_OF_RESOURCES
;
922 tcpc
->common
.Next
= first_connection
;
923 first_connection
= &tcpc
->common
;
928 /* if at least one connection was created for an endpoint then
930 if (first_connection
)
934 /* find last element in list */
935 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
938 EnterCriticalSection(&protseq
->cs
);
939 conn
->Next
= protseq
->conn
;
940 protseq
->conn
= first_connection
;
941 LeaveCriticalSection(&protseq
->cs
);
943 TRACE("listening on %s\n", endpoint
);
947 ERR("couldn't listen on port %s\n", endpoint
);
951 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
954 struct sockaddr_in address
;
956 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
957 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
959 addrsize
= sizeof(address
);
960 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
963 ERR("Failed to accept a TCP connection: error %d\n", ret
);
964 return RPC_S_OUT_OF_RESOURCES
;
966 /* reset to blocking behaviour */
967 fcntl(ret
, F_SETFL
, 0);
969 TRACE("Accepted a new TCP connection\n");
973 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
974 void *buffer
, unsigned int count
)
976 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
980 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
985 else if (errno
!= EAGAIN
)
987 WARN("recv() failed: %s\n", strerror(errno
));
992 struct pollfd pfds
[2];
993 pfds
[0].fd
= tcpc
->sock
;
994 pfds
[0].events
= POLLIN
;
995 pfds
[1].fd
= tcpc
->cancel_fds
[0];
996 pfds
[1].events
= POLLIN
;
997 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
999 ERR("poll() failed: %s\n", strerror(errno
));
1002 if (pfds
[1].revents
& POLLIN
) /* canceled */
1005 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1009 } while (bytes_read
!= count
);
1010 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1014 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1015 const void *buffer
, unsigned int count
)
1017 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1018 int bytes_written
= 0;
1021 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1024 else if (errno
!= EAGAIN
)
1029 pfd
.fd
= tcpc
->sock
;
1030 pfd
.events
= POLLOUT
;
1031 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1033 ERR("poll() failed: %s\n", strerror(errno
));
1037 } while (bytes_written
!= count
);
1038 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1039 return bytes_written
;
1042 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1044 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1046 TRACE("%d\n", tcpc
->sock
);
1048 if (tcpc
->sock
!= -1)
1051 close(tcpc
->cancel_fds
[0]);
1052 close(tcpc
->cancel_fds
[1]);
1056 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1058 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1061 TRACE("%p\n", Connection
);
1063 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1066 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1068 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1069 struct pollfd pfds
[2];
1071 TRACE("%p\n", Connection
);
1073 pfds
[0].fd
= tcpc
->sock
;
1074 pfds
[0].events
= POLLIN
;
1075 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1076 pfds
[1].events
= POLLIN
;
1077 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1079 ERR("poll() failed: %s\n", strerror(errno
));
1082 if (pfds
[1].revents
& POLLIN
) /* canceled */
1085 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1092 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1093 const char *networkaddr
,
1094 const char *endpoint
)
1096 twr_tcp_floor_t
*tcp_floor
;
1097 twr_ipv4_floor_t
*ipv4_floor
;
1098 struct addrinfo
*ai
;
1099 struct addrinfo hints
;
1101 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1103 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1108 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1109 tower_data
+= sizeof(*tcp_floor
);
1111 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1113 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1114 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1115 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1117 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1118 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1119 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1121 hints
.ai_flags
= AI_NUMERICHOST
;
1122 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1123 hints
.ai_family
= PF_INET
;
1124 hints
.ai_socktype
= SOCK_STREAM
;
1125 hints
.ai_protocol
= IPPROTO_TCP
;
1126 hints
.ai_addrlen
= 0;
1127 hints
.ai_addr
= NULL
;
1128 hints
.ai_canonname
= NULL
;
1129 hints
.ai_next
= NULL
;
1131 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1134 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1137 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1142 if (ai
->ai_family
== PF_INET
)
1144 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1145 tcp_floor
->port
= sin
->sin_port
;
1146 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1150 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1159 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1164 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1165 const twr_ipv4_floor_t
*ipv4_floor
;
1166 struct in_addr in_addr
;
1168 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1170 if (tower_size
< sizeof(*tcp_floor
))
1171 return EPT_S_NOT_REGISTERED
;
1173 tower_data
+= sizeof(*tcp_floor
);
1174 tower_size
-= sizeof(*tcp_floor
);
1176 if (tower_size
< sizeof(*ipv4_floor
))
1177 return EPT_S_NOT_REGISTERED
;
1179 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1181 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1182 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1183 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1184 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1185 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1186 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1187 return EPT_S_NOT_REGISTERED
;
1191 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1193 return RPC_S_OUT_OF_RESOURCES
;
1194 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1199 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1204 I_RpcFree(*endpoint
);
1207 return RPC_S_OUT_OF_RESOURCES
;
1209 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1210 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1212 ERR("inet_ntop: %s\n", strerror(errno
));
1213 I_RpcFree(*networkaddr
);
1214 *networkaddr
= NULL
;
1217 I_RpcFree(*endpoint
);
1220 return EPT_S_NOT_REGISTERED
;
1227 typedef struct _RpcServerProtseq_sock
1229 RpcServerProtseq common
;
1232 } RpcServerProtseq_sock
;
1234 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1236 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1240 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1242 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1243 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1244 ps
->mgr_event_rcv
= fds
[0];
1245 ps
->mgr_event_snd
= fds
[1];
1249 ERR("socketpair failed with error %s\n", strerror(errno
));
1250 HeapFree(GetProcessHeap(), 0, ps
);
1257 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1259 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1261 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1264 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1266 struct pollfd
*poll_info
= prev_array
;
1267 RpcConnection_tcp
*conn
;
1268 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1270 EnterCriticalSection(&protseq
->cs
);
1272 /* open and count connections */
1274 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1276 if (conn
->sock
!= -1)
1278 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1281 /* make array of connections */
1283 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1285 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1288 ERR("couldn't allocate poll_info\n");
1289 LeaveCriticalSection(&protseq
->cs
);
1293 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1294 poll_info
[0].events
= POLLIN
;
1296 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1298 if (conn
->sock
!= -1)
1300 poll_info
[*count
].fd
= conn
->sock
;
1301 poll_info
[*count
].events
= POLLIN
;
1304 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1306 LeaveCriticalSection(&protseq
->cs
);
1310 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1312 HeapFree(GetProcessHeap(), 0, array
);
1315 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1317 struct pollfd
*poll_info
= wait_array
;
1319 RpcConnection
*cconn
;
1320 RpcConnection_tcp
*conn
;
1325 ret
= poll(poll_info
, count
, -1);
1328 ERR("poll failed with error %d\n", ret
);
1332 for (i
= 0; i
< count
; i
++)
1333 if (poll_info
[i
].revents
& POLLIN
)
1335 /* RPC server event */
1339 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1343 /* find which connection got a RPC */
1344 EnterCriticalSection(&protseq
->cs
);
1345 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1347 if (poll_info
[i
].fd
== conn
->sock
) break;
1348 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1352 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1354 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1355 LeaveCriticalSection(&protseq
->cs
);
1357 RPCRT4_new_client(cconn
);
1365 static const struct connection_ops conn_protseq_list
[] = {
1367 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1368 rpcrt4_conn_np_alloc
,
1369 rpcrt4_ncacn_np_open
,
1370 rpcrt4_ncacn_np_handoff
,
1371 rpcrt4_conn_np_read
,
1372 rpcrt4_conn_np_write
,
1373 rpcrt4_conn_np_close
,
1374 rpcrt4_conn_np_cancel_call
,
1375 rpcrt4_conn_np_wait_for_incoming_data
,
1376 rpcrt4_ncacn_np_get_top_of_tower
,
1377 rpcrt4_ncacn_np_parse_top_of_tower
,
1380 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1381 rpcrt4_conn_np_alloc
,
1382 rpcrt4_ncalrpc_open
,
1383 rpcrt4_ncalrpc_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_ncalrpc_get_top_of_tower
,
1390 rpcrt4_ncalrpc_parse_top_of_tower
,
1393 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1394 rpcrt4_conn_tcp_alloc
,
1395 rpcrt4_ncacn_ip_tcp_open
,
1396 rpcrt4_conn_tcp_handoff
,
1397 rpcrt4_conn_tcp_read
,
1398 rpcrt4_conn_tcp_write
,
1399 rpcrt4_conn_tcp_close
,
1400 rpcrt4_conn_tcp_cancel_call
,
1401 rpcrt4_conn_tcp_wait_for_incoming_data
,
1402 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1403 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1408 static const struct protseq_ops protseq_list
[] =
1412 rpcrt4_protseq_np_alloc
,
1413 rpcrt4_protseq_np_signal_state_changed
,
1414 rpcrt4_protseq_np_get_wait_array
,
1415 rpcrt4_protseq_np_free_wait_array
,
1416 rpcrt4_protseq_np_wait_for_new_connection
,
1417 rpcrt4_protseq_ncacn_np_open_endpoint
,
1421 rpcrt4_protseq_np_alloc
,
1422 rpcrt4_protseq_np_signal_state_changed
,
1423 rpcrt4_protseq_np_get_wait_array
,
1424 rpcrt4_protseq_np_free_wait_array
,
1425 rpcrt4_protseq_np_wait_for_new_connection
,
1426 rpcrt4_protseq_ncalrpc_open_endpoint
,
1430 rpcrt4_protseq_sock_alloc
,
1431 rpcrt4_protseq_sock_signal_state_changed
,
1432 rpcrt4_protseq_sock_get_wait_array
,
1433 rpcrt4_protseq_sock_free_wait_array
,
1434 rpcrt4_protseq_sock_wait_for_new_connection
,
1435 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1439 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1441 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1444 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1445 if (!strcmp(protseq_list
[i
].name
, protseq
))
1446 return &protseq_list
[i
];
1450 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1453 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1454 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1455 return &conn_protseq_list
[i
];
1459 /**** interface to rest of code ****/
1461 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1463 TRACE("(Connection == ^%p)\n", Connection
);
1465 assert(!Connection
->server
);
1466 return Connection
->ops
->open_connection_client(Connection
);
1469 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1471 TRACE("(Connection == ^%p)\n", Connection
);
1472 if (SecIsValidHandle(&Connection
->ctx
))
1474 DeleteSecurityContext(&Connection
->ctx
);
1475 SecInvalidateHandle(&Connection
->ctx
);
1477 rpcrt4_conn_close(Connection
);
1481 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1482 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1483 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1485 const struct connection_ops
*ops
;
1486 RpcConnection
* NewConnection
;
1488 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1491 FIXME("not supported for protseq %s\n", Protseq
);
1492 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1495 NewConnection
= ops
->alloc();
1496 NewConnection
->Next
= NULL
;
1497 NewConnection
->server_binding
= NULL
;
1498 NewConnection
->server
= server
;
1499 NewConnection
->ops
= ops
;
1500 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1501 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1502 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1503 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1504 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1505 NewConnection
->NextCallId
= 1;
1507 SecInvalidateHandle(&NewConnection
->ctx
);
1508 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1509 NewConnection
->attr
= 0;
1510 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1511 NewConnection
->AuthInfo
= AuthInfo
;
1512 NewConnection
->encryption_auth_len
= 0;
1513 NewConnection
->signature_auth_len
= 0;
1514 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1515 NewConnection
->QOS
= QOS
;
1517 list_init(&NewConnection
->conn_pool_entry
);
1518 NewConnection
->async_state
= NULL
;
1520 TRACE("connection: %p\n", NewConnection
);
1521 *Connection
= NewConnection
;
1527 RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1531 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1532 rpcrt4_conn_get_name(OldConnection
),
1533 OldConnection
->NetworkAddr
,
1534 OldConnection
->Endpoint
, NULL
,
1535 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1536 if (err
== RPC_S_OK
)
1537 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1541 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1543 TRACE("connection: %p\n", Connection
);
1545 RPCRT4_CloseConnection(Connection
);
1546 RPCRT4_strfree(Connection
->Endpoint
);
1547 RPCRT4_strfree(Connection
->NetworkAddr
);
1548 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1549 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1550 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1553 if (Connection
->server_binding
) RPCRT4_DestroyBinding(Connection
->server_binding
);
1555 HeapFree(GetProcessHeap(), 0, Connection
);
1559 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1561 const char *protseq
,
1562 const char *networkaddr
,
1563 const char *endpoint
)
1565 twr_empty_floor_t
*protocol_floor
;
1566 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1571 return RPC_S_INVALID_RPC_PROTSEQ
;
1575 *tower_size
= sizeof(*protocol_floor
);
1576 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1580 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1581 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1582 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1583 protocol_floor
->count_rhs
= 0;
1585 tower_data
+= sizeof(*protocol_floor
);
1587 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1589 return EPT_S_NOT_REGISTERED
;
1591 *tower_size
+= sizeof(*protocol_floor
);
1596 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1602 const twr_empty_floor_t
*protocol_floor
;
1603 const twr_empty_floor_t
*floor4
;
1604 const struct connection_ops
*protseq_ops
= NULL
;
1608 if (tower_size
< sizeof(*protocol_floor
))
1609 return EPT_S_NOT_REGISTERED
;
1611 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1612 tower_data
+= sizeof(*protocol_floor
);
1613 tower_size
-= sizeof(*protocol_floor
);
1614 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1615 (protocol_floor
->count_rhs
> tower_size
))
1616 return EPT_S_NOT_REGISTERED
;
1617 tower_data
+= protocol_floor
->count_rhs
;
1618 tower_size
-= protocol_floor
->count_rhs
;
1620 floor4
= (const twr_empty_floor_t
*)tower_data
;
1621 if ((tower_size
< sizeof(*floor4
)) ||
1622 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1623 return EPT_S_NOT_REGISTERED
;
1625 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1626 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1627 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1629 protseq_ops
= &conn_protseq_list
[i
];
1634 return EPT_S_NOT_REGISTERED
;
1636 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1638 if ((status
== RPC_S_OK
) && protseq
)
1640 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1641 strcpy(*protseq
, protseq_ops
->name
);
1647 /***********************************************************************
1648 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1650 * Checks if the given protocol sequence is known by the RPC system.
1651 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1654 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1658 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1659 ps
, sizeof ps
, NULL
, NULL
);
1660 if (rpcrt4_get_conn_protseq_ops(ps
))
1663 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1665 return RPC_S_INVALID_RPC_PROTSEQ
;
1668 /***********************************************************************
1669 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1671 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1673 UNICODE_STRING protseqW
;
1675 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1677 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1678 RtlFreeUnicodeString(&protseqW
);
1681 return RPC_S_OUT_OF_MEMORY
;