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
;
213 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
214 err
= GetLastError();
215 WARN("connection failed, error=%x\n", err
);
216 return RPC_S_SERVER_UNAVAILABLE
;
221 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
222 /* pipe is connected; change to message-read mode. */
223 dwMode
= PIPE_READMODE_MESSAGE
;
224 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
225 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
231 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
233 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
234 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
238 /* already connected? */
242 /* protseq=ncalrpc: supposed to use NT LPC ports,
243 * but we'll implement it with named pipes for now */
244 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
245 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
246 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
252 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, LPSTR endpoint
)
254 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
257 RpcConnection
*Connection
;
259 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
260 endpoint
, NULL
, NULL
, NULL
);
264 /* protseq=ncalrpc: supposed to use NT LPC ports,
265 * but we'll implement it with named pipes for now */
266 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
267 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
268 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
271 EnterCriticalSection(&protseq
->cs
);
272 Connection
->Next
= protseq
->conn
;
273 protseq
->conn
= Connection
;
274 LeaveCriticalSection(&protseq
->cs
);
279 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
281 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
282 static const char prefix
[] = "\\\\.";
286 /* already connected? */
290 /* protseq=ncacn_np: named pipes */
291 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
292 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
293 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
299 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
301 static const char prefix
[] = "\\\\.";
304 RpcConnection
*Connection
;
306 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
307 endpoint
, NULL
, NULL
, NULL
);
311 /* protseq=ncacn_np: named pipes */
312 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
313 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
314 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
317 EnterCriticalSection(&protseq
->cs
);
318 Connection
->Next
= protseq
->conn
;
319 protseq
->conn
= Connection
;
320 LeaveCriticalSection(&protseq
->cs
);
325 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
327 /* because of the way named pipes work, we'll transfer the connected pipe
328 * to the child, then reopen the server binding to continue listening */
330 new_npc
->pipe
= old_npc
->pipe
;
331 new_npc
->ovl
= old_npc
->ovl
;
333 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
334 old_npc
->listening
= FALSE
;
337 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
341 static const char prefix
[] = "\\\\.";
343 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
345 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
346 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
347 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
353 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
357 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
359 TRACE("%s\n", old_conn
->Endpoint
);
361 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
363 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
364 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
365 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
371 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
372 void *buffer
, unsigned int count
)
374 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
377 unsigned int bytes_left
= count
;
382 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
383 if (!ret
|| !bytes_read
)
385 bytes_left
-= bytes_read
;
388 return ret
? count
: -1;
391 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
392 const void *buffer
, unsigned int count
)
394 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
395 const char *buf
= buffer
;
397 unsigned int bytes_left
= count
;
402 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
403 if (!ret
|| !bytes_written
)
405 bytes_left
-= bytes_written
;
406 buf
+= bytes_written
;
408 return ret
? count
: -1;
411 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
413 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
415 FlushFileBuffers(npc
->pipe
);
416 CloseHandle(npc
->pipe
);
419 if (npc
->ovl
.hEvent
) {
420 CloseHandle(npc
->ovl
.hEvent
);
426 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
428 /* FIXME: implement when named pipe writes use overlapped I/O */
431 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
433 /* FIXME: implement when named pipe writes use overlapped I/O */
437 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
438 const char *networkaddr
,
439 const char *endpoint
)
441 twr_empty_floor_t
*smb_floor
;
442 twr_empty_floor_t
*nb_floor
;
444 size_t networkaddr_size
;
445 size_t endpoint_size
;
447 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
449 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
450 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
451 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
456 smb_floor
= (twr_empty_floor_t
*)tower_data
;
458 tower_data
+= sizeof(*smb_floor
);
460 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
461 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
462 smb_floor
->count_rhs
= endpoint_size
;
465 memcpy(tower_data
, endpoint
, endpoint_size
);
468 tower_data
+= endpoint_size
;
470 nb_floor
= (twr_empty_floor_t
*)tower_data
;
472 tower_data
+= sizeof(*nb_floor
);
474 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
475 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
476 nb_floor
->count_rhs
= networkaddr_size
;
479 memcpy(tower_data
, networkaddr
, networkaddr_size
);
482 tower_data
+= networkaddr_size
;
487 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
492 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
493 const twr_empty_floor_t
*nb_floor
;
495 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
497 if (tower_size
< sizeof(*smb_floor
))
498 return EPT_S_NOT_REGISTERED
;
500 tower_data
+= sizeof(*smb_floor
);
501 tower_size
-= sizeof(*smb_floor
);
503 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
504 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
505 (smb_floor
->count_rhs
> tower_size
) ||
506 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
507 return EPT_S_NOT_REGISTERED
;
511 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
513 return RPC_S_OUT_OF_RESOURCES
;
514 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
516 tower_data
+= smb_floor
->count_rhs
;
517 tower_size
-= smb_floor
->count_rhs
;
519 if (tower_size
< sizeof(*nb_floor
))
520 return EPT_S_NOT_REGISTERED
;
522 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
524 tower_data
+= sizeof(*nb_floor
);
525 tower_size
-= sizeof(*nb_floor
);
527 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
528 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
529 (nb_floor
->count_rhs
> tower_size
) ||
530 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
531 return EPT_S_NOT_REGISTERED
;
535 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
540 I_RpcFree(*endpoint
);
543 return RPC_S_OUT_OF_RESOURCES
;
545 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
551 typedef struct _RpcServerProtseq_np
553 RpcServerProtseq common
;
555 } RpcServerProtseq_np
;
557 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
559 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
561 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
565 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
567 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
568 SetEvent(npps
->mgr_event
);
571 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
573 HANDLE
*objs
= prev_array
;
574 RpcConnection_np
*conn
;
575 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
577 EnterCriticalSection(&protseq
->cs
);
579 /* open and count connections */
581 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
583 rpcrt4_conn_listen_pipe(conn
);
584 if (conn
->ovl
.hEvent
)
586 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
589 /* make array of connections */
591 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
593 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
596 ERR("couldn't allocate objs\n");
597 LeaveCriticalSection(&protseq
->cs
);
601 objs
[0] = npps
->mgr_event
;
603 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
605 if ((objs
[*count
] = conn
->ovl
.hEvent
))
607 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
609 LeaveCriticalSection(&protseq
->cs
);
613 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
615 HeapFree(GetProcessHeap(), 0, array
);
618 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
621 HANDLE
*objs
= wait_array
;
623 RpcConnection
*cconn
;
624 RpcConnection_np
*conn
;
631 /* an alertable wait isn't strictly necessary, but due to our
632 * overlapped I/O implementation in Wine we need to free some memory
633 * by the file user APC being called, even if no completion routine was
634 * specified at the time of starting the async operation */
635 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
636 } while (res
== WAIT_IO_COMPLETION
);
638 if (res
== WAIT_OBJECT_0
)
640 else if (res
== WAIT_FAILED
)
642 ERR("wait failed with error %d\n", GetLastError());
647 b_handle
= objs
[res
- WAIT_OBJECT_0
];
648 /* find which connection got a RPC */
649 EnterCriticalSection(&protseq
->cs
);
650 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
652 if (b_handle
== conn
->ovl
.hEvent
) break;
653 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
657 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
659 ERR("failed to locate connection for handle %p\n", b_handle
);
660 LeaveCriticalSection(&protseq
->cs
);
663 RPCRT4_new_client(cconn
);
670 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
671 const char *networkaddr
,
672 const char *endpoint
)
674 twr_empty_floor_t
*pipe_floor
;
676 size_t endpoint_size
;
678 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
680 endpoint_size
= strlen(endpoint
) + 1;
681 size
= sizeof(*pipe_floor
) + endpoint_size
;
686 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
688 tower_data
+= sizeof(*pipe_floor
);
690 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
691 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
692 pipe_floor
->count_rhs
= endpoint_size
;
694 memcpy(tower_data
, endpoint
, endpoint_size
);
695 tower_data
+= endpoint_size
;
700 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
705 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
707 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
709 if (tower_size
< sizeof(*pipe_floor
))
710 return EPT_S_NOT_REGISTERED
;
712 tower_data
+= sizeof(*pipe_floor
);
713 tower_size
-= sizeof(*pipe_floor
);
715 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
716 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
717 (pipe_floor
->count_rhs
> tower_size
) ||
718 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
719 return EPT_S_NOT_REGISTERED
;
726 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
728 return RPC_S_OUT_OF_RESOURCES
;
729 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
735 /**** ncacn_ip_tcp support ****/
737 typedef struct _RpcConnection_tcp
739 RpcConnection common
;
744 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
746 RpcConnection_tcp
*tcpc
;
747 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
751 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
753 ERR("socketpair() failed: %s\n", strerror(errno
));
754 HeapFree(GetProcessHeap(), 0, tcpc
);
757 return &tcpc
->common
;
760 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
762 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
766 struct addrinfo
*ai_cur
;
767 struct addrinfo hints
;
769 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
771 if (tcpc
->sock
!= -1)
775 hints
.ai_family
= PF_UNSPEC
;
776 hints
.ai_socktype
= SOCK_STREAM
;
777 hints
.ai_protocol
= IPPROTO_TCP
;
778 hints
.ai_addrlen
= 0;
779 hints
.ai_addr
= NULL
;
780 hints
.ai_canonname
= NULL
;
781 hints
.ai_next
= NULL
;
783 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
786 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
787 Connection
->Endpoint
, gai_strerror(ret
));
788 return RPC_S_SERVER_UNAVAILABLE
;
791 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
799 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
800 host
, sizeof(host
), service
, sizeof(service
),
801 NI_NUMERICHOST
| NI_NUMERICSERV
);
802 TRACE("trying %s:%s\n", host
, service
);
805 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
808 WARN("socket() failed: %s\n", strerror(errno
));
812 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
814 WARN("connect() failed: %s\n", strerror(errno
));
819 /* RPC depends on having minimal latency so disable the Nagle algorithm */
821 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
822 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
827 TRACE("connected\n");
832 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
833 return RPC_S_SERVER_UNAVAILABLE
;
836 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
838 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
842 struct addrinfo
*ai_cur
;
843 struct addrinfo hints
;
844 RpcConnection
*first_connection
= NULL
;
846 TRACE("(%p, %s)\n", protseq
, endpoint
);
848 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
849 hints
.ai_family
= PF_UNSPEC
;
850 hints
.ai_socktype
= SOCK_STREAM
;
851 hints
.ai_protocol
= IPPROTO_TCP
;
852 hints
.ai_addrlen
= 0;
853 hints
.ai_addr
= NULL
;
854 hints
.ai_canonname
= NULL
;
855 hints
.ai_next
= NULL
;
857 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
860 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
862 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
863 return RPC_S_INVALID_ENDPOINT_FORMAT
;
864 return RPC_S_CANT_CREATE_ENDPOINT
;
867 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
869 RpcConnection_tcp
*tcpc
;
870 RPC_STATUS create_status
;
876 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
877 host
, sizeof(host
), service
, sizeof(service
),
878 NI_NUMERICHOST
| NI_NUMERICSERV
);
879 TRACE("trying %s:%s\n", host
, service
);
882 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
885 WARN("socket() failed: %s\n", strerror(errno
));
886 status
= RPC_S_CANT_CREATE_ENDPOINT
;
890 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
893 WARN("bind failed: %s\n", strerror(errno
));
895 if (errno
== EADDRINUSE
)
896 status
= RPC_S_DUPLICATE_ENDPOINT
;
898 status
= RPC_S_CANT_CREATE_ENDPOINT
;
901 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
902 protseq
->Protseq
, NULL
,
903 endpoint
, NULL
, NULL
, NULL
);
904 if (create_status
!= RPC_S_OK
)
907 status
= create_status
;
912 ret
= listen(sock
, protseq
->MaxCalls
);
915 WARN("listen failed: %s\n", strerror(errno
));
916 RPCRT4_DestroyConnection(&tcpc
->common
);
917 status
= RPC_S_OUT_OF_RESOURCES
;
920 /* need a non-blocking socket, otherwise accept() has a potential
921 * race-condition (poll() says it is readable, connection drops,
922 * and accept() blocks until the next connection comes...)
924 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
927 WARN("couldn't make socket non-blocking, error %d\n", ret
);
928 RPCRT4_DestroyConnection(&tcpc
->common
);
929 status
= RPC_S_OUT_OF_RESOURCES
;
933 tcpc
->common
.Next
= first_connection
;
934 first_connection
= &tcpc
->common
;
939 /* if at least one connection was created for an endpoint then
941 if (first_connection
)
945 /* find last element in list */
946 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
949 EnterCriticalSection(&protseq
->cs
);
950 conn
->Next
= protseq
->conn
;
951 protseq
->conn
= first_connection
;
952 LeaveCriticalSection(&protseq
->cs
);
954 TRACE("listening on %s\n", endpoint
);
958 ERR("couldn't listen on port %s\n", endpoint
);
962 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
965 struct sockaddr_in address
;
967 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
968 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
970 addrsize
= sizeof(address
);
971 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
974 ERR("Failed to accept a TCP connection: error %d\n", ret
);
975 return RPC_S_OUT_OF_RESOURCES
;
977 /* reset to blocking behaviour */
978 fcntl(ret
, F_SETFL
, 0);
980 TRACE("Accepted a new TCP connection\n");
984 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
985 void *buffer
, unsigned int count
)
987 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
991 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
996 else if (errno
!= EAGAIN
)
998 WARN("recv() failed: %s\n", strerror(errno
));
1003 struct pollfd pfds
[2];
1004 pfds
[0].fd
= tcpc
->sock
;
1005 pfds
[0].events
= POLLIN
;
1006 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1007 pfds
[1].events
= POLLIN
;
1008 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1010 ERR("poll() failed: %s\n", strerror(errno
));
1013 if (pfds
[1].revents
& POLLIN
) /* canceled */
1016 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1020 } while (bytes_read
!= count
);
1021 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1025 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1026 const void *buffer
, unsigned int count
)
1028 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1029 int bytes_written
= 0;
1032 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1035 else if (errno
!= EAGAIN
)
1040 pfd
.fd
= tcpc
->sock
;
1041 pfd
.events
= POLLOUT
;
1042 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1044 ERR("poll() failed: %s\n", strerror(errno
));
1048 } while (bytes_written
!= count
);
1049 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1050 return bytes_written
;
1053 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1055 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1057 TRACE("%d\n", tcpc
->sock
);
1059 if (tcpc
->sock
!= -1)
1060 closesocket(tcpc
->sock
);
1062 close(tcpc
->cancel_fds
[0]);
1063 close(tcpc
->cancel_fds
[1]);
1067 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1069 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1072 TRACE("%p\n", Connection
);
1074 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1077 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1079 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1080 struct pollfd pfds
[2];
1082 TRACE("%p\n", Connection
);
1084 pfds
[0].fd
= tcpc
->sock
;
1085 pfds
[0].events
= POLLIN
;
1086 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1087 pfds
[1].events
= POLLIN
;
1088 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1090 ERR("poll() failed: %s\n", strerror(errno
));
1093 if (pfds
[1].revents
& POLLIN
) /* canceled */
1096 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1103 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1104 const char *networkaddr
,
1105 const char *endpoint
)
1107 twr_tcp_floor_t
*tcp_floor
;
1108 twr_ipv4_floor_t
*ipv4_floor
;
1109 struct addrinfo
*ai
;
1110 struct addrinfo hints
;
1112 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1114 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1119 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1120 tower_data
+= sizeof(*tcp_floor
);
1122 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1124 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1125 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1126 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1128 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1129 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1130 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1132 hints
.ai_flags
= AI_NUMERICHOST
;
1133 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1134 hints
.ai_family
= PF_INET
;
1135 hints
.ai_socktype
= SOCK_STREAM
;
1136 hints
.ai_protocol
= IPPROTO_TCP
;
1137 hints
.ai_addrlen
= 0;
1138 hints
.ai_addr
= NULL
;
1139 hints
.ai_canonname
= NULL
;
1140 hints
.ai_next
= NULL
;
1142 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1145 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1148 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1153 if (ai
->ai_family
== PF_INET
)
1155 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1156 tcp_floor
->port
= sin
->sin_port
;
1157 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1161 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1170 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1175 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1176 const twr_ipv4_floor_t
*ipv4_floor
;
1177 struct in_addr in_addr
;
1179 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1181 if (tower_size
< sizeof(*tcp_floor
))
1182 return EPT_S_NOT_REGISTERED
;
1184 tower_data
+= sizeof(*tcp_floor
);
1185 tower_size
-= sizeof(*tcp_floor
);
1187 if (tower_size
< sizeof(*ipv4_floor
))
1188 return EPT_S_NOT_REGISTERED
;
1190 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1192 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1193 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1194 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1195 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1196 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1197 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1198 return EPT_S_NOT_REGISTERED
;
1202 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1204 return RPC_S_OUT_OF_RESOURCES
;
1205 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1210 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1215 I_RpcFree(*endpoint
);
1218 return RPC_S_OUT_OF_RESOURCES
;
1220 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1221 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1223 ERR("inet_ntop: %s\n", strerror(errno
));
1224 I_RpcFree(*networkaddr
);
1225 *networkaddr
= NULL
;
1228 I_RpcFree(*endpoint
);
1231 return EPT_S_NOT_REGISTERED
;
1238 typedef struct _RpcServerProtseq_sock
1240 RpcServerProtseq common
;
1243 } RpcServerProtseq_sock
;
1245 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1247 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1251 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1253 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1254 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1255 ps
->mgr_event_rcv
= fds
[0];
1256 ps
->mgr_event_snd
= fds
[1];
1260 ERR("socketpair failed with error %s\n", strerror(errno
));
1261 HeapFree(GetProcessHeap(), 0, ps
);
1268 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1270 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1272 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1275 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1277 struct pollfd
*poll_info
= prev_array
;
1278 RpcConnection_tcp
*conn
;
1279 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1281 EnterCriticalSection(&protseq
->cs
);
1283 /* open and count connections */
1285 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1287 if (conn
->sock
!= -1)
1289 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1292 /* make array of connections */
1294 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1296 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1299 ERR("couldn't allocate poll_info\n");
1300 LeaveCriticalSection(&protseq
->cs
);
1304 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1305 poll_info
[0].events
= POLLIN
;
1307 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1309 if (conn
->sock
!= -1)
1311 poll_info
[*count
].fd
= conn
->sock
;
1312 poll_info
[*count
].events
= POLLIN
;
1315 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1317 LeaveCriticalSection(&protseq
->cs
);
1321 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1323 HeapFree(GetProcessHeap(), 0, array
);
1326 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1328 struct pollfd
*poll_info
= wait_array
;
1330 RpcConnection
*cconn
;
1331 RpcConnection_tcp
*conn
;
1336 ret
= poll(poll_info
, count
, -1);
1339 ERR("poll failed with error %d\n", ret
);
1343 for (i
= 0; i
< count
; i
++)
1344 if (poll_info
[i
].revents
& POLLIN
)
1346 /* RPC server event */
1350 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1354 /* find which connection got a RPC */
1355 EnterCriticalSection(&protseq
->cs
);
1356 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1358 if (poll_info
[i
].fd
== conn
->sock
) break;
1359 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1363 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1365 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1366 LeaveCriticalSection(&protseq
->cs
);
1368 RPCRT4_new_client(cconn
);
1376 static const struct connection_ops conn_protseq_list
[] = {
1378 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1379 rpcrt4_conn_np_alloc
,
1380 rpcrt4_ncacn_np_open
,
1381 rpcrt4_ncacn_np_handoff
,
1382 rpcrt4_conn_np_read
,
1383 rpcrt4_conn_np_write
,
1384 rpcrt4_conn_np_close
,
1385 rpcrt4_conn_np_cancel_call
,
1386 rpcrt4_conn_np_wait_for_incoming_data
,
1387 rpcrt4_ncacn_np_get_top_of_tower
,
1388 rpcrt4_ncacn_np_parse_top_of_tower
,
1391 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1392 rpcrt4_conn_np_alloc
,
1393 rpcrt4_ncalrpc_open
,
1394 rpcrt4_ncalrpc_handoff
,
1395 rpcrt4_conn_np_read
,
1396 rpcrt4_conn_np_write
,
1397 rpcrt4_conn_np_close
,
1398 rpcrt4_conn_np_cancel_call
,
1399 rpcrt4_conn_np_wait_for_incoming_data
,
1400 rpcrt4_ncalrpc_get_top_of_tower
,
1401 rpcrt4_ncalrpc_parse_top_of_tower
,
1404 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1405 rpcrt4_conn_tcp_alloc
,
1406 rpcrt4_ncacn_ip_tcp_open
,
1407 rpcrt4_conn_tcp_handoff
,
1408 rpcrt4_conn_tcp_read
,
1409 rpcrt4_conn_tcp_write
,
1410 rpcrt4_conn_tcp_close
,
1411 rpcrt4_conn_tcp_cancel_call
,
1412 rpcrt4_conn_tcp_wait_for_incoming_data
,
1413 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1414 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1419 static const struct protseq_ops protseq_list
[] =
1423 rpcrt4_protseq_np_alloc
,
1424 rpcrt4_protseq_np_signal_state_changed
,
1425 rpcrt4_protseq_np_get_wait_array
,
1426 rpcrt4_protseq_np_free_wait_array
,
1427 rpcrt4_protseq_np_wait_for_new_connection
,
1428 rpcrt4_protseq_ncacn_np_open_endpoint
,
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_ncalrpc_open_endpoint
,
1441 rpcrt4_protseq_sock_alloc
,
1442 rpcrt4_protseq_sock_signal_state_changed
,
1443 rpcrt4_protseq_sock_get_wait_array
,
1444 rpcrt4_protseq_sock_free_wait_array
,
1445 rpcrt4_protseq_sock_wait_for_new_connection
,
1446 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1450 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1452 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1455 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1456 if (!strcmp(protseq_list
[i
].name
, protseq
))
1457 return &protseq_list
[i
];
1461 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1464 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1465 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1466 return &conn_protseq_list
[i
];
1470 /**** interface to rest of code ****/
1472 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1474 TRACE("(Connection == ^%p)\n", Connection
);
1476 assert(!Connection
->server
);
1477 return Connection
->ops
->open_connection_client(Connection
);
1480 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1482 TRACE("(Connection == ^%p)\n", Connection
);
1483 if (SecIsValidHandle(&Connection
->ctx
))
1485 DeleteSecurityContext(&Connection
->ctx
);
1486 SecInvalidateHandle(&Connection
->ctx
);
1488 rpcrt4_conn_close(Connection
);
1492 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1493 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1494 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1496 const struct connection_ops
*ops
;
1497 RpcConnection
* NewConnection
;
1499 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1502 FIXME("not supported for protseq %s\n", Protseq
);
1503 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1506 NewConnection
= ops
->alloc();
1507 NewConnection
->Next
= NULL
;
1508 NewConnection
->server_binding
= NULL
;
1509 NewConnection
->server
= server
;
1510 NewConnection
->ops
= ops
;
1511 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1512 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1513 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1514 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1515 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1516 NewConnection
->NextCallId
= 1;
1518 SecInvalidateHandle(&NewConnection
->ctx
);
1519 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1520 NewConnection
->attr
= 0;
1521 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1522 NewConnection
->AuthInfo
= AuthInfo
;
1523 NewConnection
->encryption_auth_len
= 0;
1524 NewConnection
->signature_auth_len
= 0;
1525 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1526 NewConnection
->QOS
= QOS
;
1528 list_init(&NewConnection
->conn_pool_entry
);
1529 NewConnection
->async_state
= NULL
;
1531 TRACE("connection: %p\n", NewConnection
);
1532 *Connection
= NewConnection
;
1538 RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1542 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1543 rpcrt4_conn_get_name(OldConnection
),
1544 OldConnection
->NetworkAddr
,
1545 OldConnection
->Endpoint
, NULL
,
1546 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1547 if (err
== RPC_S_OK
)
1548 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1552 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1554 TRACE("connection: %p\n", Connection
);
1556 RPCRT4_CloseConnection(Connection
);
1557 RPCRT4_strfree(Connection
->Endpoint
);
1558 RPCRT4_strfree(Connection
->NetworkAddr
);
1559 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1560 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1561 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1564 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
1566 HeapFree(GetProcessHeap(), 0, Connection
);
1570 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1572 const char *protseq
,
1573 const char *networkaddr
,
1574 const char *endpoint
)
1576 twr_empty_floor_t
*protocol_floor
;
1577 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1582 return RPC_S_INVALID_RPC_PROTSEQ
;
1586 *tower_size
= sizeof(*protocol_floor
);
1587 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1591 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1592 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1593 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1594 protocol_floor
->count_rhs
= 0;
1596 tower_data
+= sizeof(*protocol_floor
);
1598 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1600 return EPT_S_NOT_REGISTERED
;
1602 *tower_size
+= sizeof(*protocol_floor
);
1607 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1613 const twr_empty_floor_t
*protocol_floor
;
1614 const twr_empty_floor_t
*floor4
;
1615 const struct connection_ops
*protseq_ops
= NULL
;
1619 if (tower_size
< sizeof(*protocol_floor
))
1620 return EPT_S_NOT_REGISTERED
;
1622 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1623 tower_data
+= sizeof(*protocol_floor
);
1624 tower_size
-= sizeof(*protocol_floor
);
1625 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1626 (protocol_floor
->count_rhs
> tower_size
))
1627 return EPT_S_NOT_REGISTERED
;
1628 tower_data
+= protocol_floor
->count_rhs
;
1629 tower_size
-= protocol_floor
->count_rhs
;
1631 floor4
= (const twr_empty_floor_t
*)tower_data
;
1632 if ((tower_size
< sizeof(*floor4
)) ||
1633 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1634 return EPT_S_NOT_REGISTERED
;
1636 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1637 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1638 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1640 protseq_ops
= &conn_protseq_list
[i
];
1645 return EPT_S_NOT_REGISTERED
;
1647 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1649 if ((status
== RPC_S_OK
) && protseq
)
1651 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1652 strcpy(*protseq
, protseq_ops
->name
);
1658 /***********************************************************************
1659 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1661 * Checks if the given protocol sequence is known by the RPC system.
1662 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1665 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1669 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1670 ps
, sizeof ps
, NULL
, NULL
);
1671 if (rpcrt4_get_conn_protseq_ops(ps
))
1674 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1676 return RPC_S_INVALID_RPC_PROTSEQ
;
1679 /***********************************************************************
1680 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1682 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1684 UNICODE_STRING protseqW
;
1686 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1688 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1689 RtlFreeUnicodeString(&protseqW
);
1692 return RPC_S_OUT_OF_MEMORY
;