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) */
78 #include "wine/unicode.h"
83 #include "wine/debug.h"
85 #include "rpc_binding.h"
86 #include "rpc_message.h"
87 #include "rpc_server.h"
88 #include "epm_towers.h"
90 #include "unix_func.h"
93 # define SOL_TCP IPPROTO_TCP
96 WINE_DEFAULT_DEBUG_CHANNEL(rpc
);
98 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
);
100 /**** ncacn_np support ****/
102 typedef struct _RpcConnection_np
104 RpcConnection common
;
110 static RpcConnection
*rpcrt4_conn_np_alloc(void)
112 RpcConnection_np
*npc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np
));
116 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
117 npc
->listening
= FALSE
;
122 static RPC_STATUS
rpcrt4_conn_listen_pipe(RpcConnection_np
*npc
)
127 npc
->listening
= TRUE
;
130 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
[0]))
133 switch(GetLastError())
135 case ERROR_PIPE_CONNECTED
:
136 SetEvent(npc
->ovl
[0].hEvent
);
138 case ERROR_IO_PENDING
:
139 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
141 case ERROR_NO_DATA_DETECTED
:
142 /* client has disconnected, retry */
143 DisconnectNamedPipe( npc
->pipe
);
146 npc
->listening
= FALSE
;
147 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
148 return RPC_S_OUT_OF_RESOURCES
;
153 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
155 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
156 TRACE("listening on %s\n", pname
);
158 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
159 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
160 PIPE_UNLIMITED_INSTANCES
,
161 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
162 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
163 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
164 if (GetLastError() == ERROR_FILE_EXISTS
)
165 return RPC_S_DUPLICATE_ENDPOINT
;
167 return RPC_S_CANT_CREATE_ENDPOINT
;
170 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
171 npc
->ovl
[0].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
172 npc
->ovl
[1].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
174 /* Note: we don't call ConnectNamedPipe here because it must be done in the
175 * server thread as the thread must be alertable */
179 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
181 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
185 TRACE("connecting to %s\n", pname
);
191 dwFlags
= SECURITY_SQOS_PRESENT
;
192 switch (Connection
->QOS
->qos
->ImpersonationType
)
194 case RPC_C_IMP_LEVEL_DEFAULT
:
195 /* FIXME: what to do here? */
197 case RPC_C_IMP_LEVEL_ANONYMOUS
:
198 dwFlags
|= SECURITY_ANONYMOUS
;
200 case RPC_C_IMP_LEVEL_IDENTIFY
:
201 dwFlags
|= SECURITY_IDENTIFICATION
;
203 case RPC_C_IMP_LEVEL_IMPERSONATE
:
204 dwFlags
|= SECURITY_IMPERSONATION
;
206 case RPC_C_IMP_LEVEL_DELEGATE
:
207 dwFlags
|= SECURITY_DELEGATION
;
210 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTITY_DYNAMIC
)
211 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
213 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
214 OPEN_EXISTING
, dwFlags
, 0);
215 if (pipe
!= INVALID_HANDLE_VALUE
) break;
216 err
= GetLastError();
217 if (err
== ERROR_PIPE_BUSY
) {
218 TRACE("connection failed, error=%x\n", err
);
219 return RPC_S_SERVER_TOO_BUSY
;
221 if (!wait
|| !WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
222 err
= GetLastError();
223 WARN("connection failed, error=%x\n", err
);
224 return RPC_S_SERVER_UNAVAILABLE
;
229 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
230 /* pipe is connected; change to message-read mode. */
231 dwMode
= PIPE_READMODE_MESSAGE
;
232 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
233 npc
->ovl
[0].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
234 npc
->ovl
[1].hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
240 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
242 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
243 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
247 /* already connected? */
251 /* protseq=ncalrpc: supposed to use NT LPC ports,
252 * but we'll implement it with named pipes for now */
253 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
254 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
255 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
261 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, LPSTR endpoint
)
263 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
266 RpcConnection
*Connection
;
268 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
269 endpoint
, NULL
, NULL
, NULL
);
273 /* protseq=ncalrpc: supposed to use NT LPC ports,
274 * but we'll implement it with named pipes for now */
275 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
276 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
277 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
280 EnterCriticalSection(&protseq
->cs
);
281 Connection
->Next
= protseq
->conn
;
282 protseq
->conn
= Connection
;
283 LeaveCriticalSection(&protseq
->cs
);
288 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
290 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
291 static const char prefix
[] = "\\\\.";
295 /* already connected? */
299 /* protseq=ncacn_np: named pipes */
300 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
301 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
302 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
308 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
310 static const char prefix
[] = "\\\\.";
313 RpcConnection
*Connection
;
315 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
316 endpoint
, NULL
, NULL
, NULL
);
320 /* protseq=ncacn_np: named pipes */
321 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
322 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
323 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
326 EnterCriticalSection(&protseq
->cs
);
327 Connection
->Next
= protseq
->conn
;
328 protseq
->conn
= Connection
;
329 LeaveCriticalSection(&protseq
->cs
);
334 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
336 /* because of the way named pipes work, we'll transfer the connected pipe
337 * to the child, then reopen the server binding to continue listening */
339 new_npc
->pipe
= old_npc
->pipe
;
340 new_npc
->ovl
[0] = old_npc
->ovl
[0];
341 new_npc
->ovl
[1] = old_npc
->ovl
[1];
343 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
344 old_npc
->listening
= FALSE
;
347 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
351 static const char prefix
[] = "\\\\.";
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 RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
367 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
369 TRACE("%s\n", old_conn
->Endpoint
);
371 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
373 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
374 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
375 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
381 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
382 void *buffer
, unsigned int count
)
384 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
387 unsigned int bytes_left
= count
;
392 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, &npc
->ovl
[0]);
393 if ((!ret
|| !bytes_read
) && (GetLastError() != ERROR_IO_PENDING
))
396 ret
= GetOverlappedResult(npc
->pipe
, &npc
->ovl
[0], &bytes_read
, TRUE
);
397 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
400 bytes_left
-= bytes_read
;
403 return ret
? count
: -1;
406 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
407 const void *buffer
, unsigned int count
)
409 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
410 const char *buf
= buffer
;
412 unsigned int bytes_left
= count
;
417 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, &npc
->ovl
[1]);
418 if ((!ret
|| !bytes_written
) && (GetLastError() != ERROR_IO_PENDING
))
421 ret
= GetOverlappedResult(npc
->pipe
, &npc
->ovl
[1], &bytes_written
, TRUE
);
422 if (!ret
&& (GetLastError() != ERROR_MORE_DATA
))
425 bytes_left
-= bytes_written
;
426 buf
+= bytes_written
;
428 return ret
? count
: -1;
431 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
433 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
435 FlushFileBuffers(npc
->pipe
);
436 CloseHandle(npc
->pipe
);
439 if (npc
->ovl
[0].hEvent
) {
440 CloseHandle(npc
->ovl
[0].hEvent
);
441 npc
->ovl
[0].hEvent
= 0;
443 if (npc
->ovl
[1].hEvent
) {
444 CloseHandle(npc
->ovl
[1].hEvent
);
445 npc
->ovl
[1].hEvent
= 0;
450 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
452 /* FIXME: implement when named pipe writes use overlapped I/O */
455 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
457 /* FIXME: implement when named pipe writes use overlapped I/O */
461 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
462 const char *networkaddr
,
463 const char *endpoint
)
465 twr_empty_floor_t
*smb_floor
;
466 twr_empty_floor_t
*nb_floor
;
468 size_t networkaddr_size
;
469 size_t endpoint_size
;
471 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
473 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
474 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
475 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
480 smb_floor
= (twr_empty_floor_t
*)tower_data
;
482 tower_data
+= sizeof(*smb_floor
);
484 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
485 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
486 smb_floor
->count_rhs
= endpoint_size
;
489 memcpy(tower_data
, endpoint
, endpoint_size
);
492 tower_data
+= endpoint_size
;
494 nb_floor
= (twr_empty_floor_t
*)tower_data
;
496 tower_data
+= sizeof(*nb_floor
);
498 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
499 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
500 nb_floor
->count_rhs
= networkaddr_size
;
503 memcpy(tower_data
, networkaddr
, networkaddr_size
);
510 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
515 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
516 const twr_empty_floor_t
*nb_floor
;
518 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
520 if (tower_size
< sizeof(*smb_floor
))
521 return EPT_S_NOT_REGISTERED
;
523 tower_data
+= sizeof(*smb_floor
);
524 tower_size
-= sizeof(*smb_floor
);
526 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
527 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
528 (smb_floor
->count_rhs
> tower_size
) ||
529 (tower_data
[smb_floor
->count_rhs
- 1] != '\0'))
530 return EPT_S_NOT_REGISTERED
;
534 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
536 return RPC_S_OUT_OF_RESOURCES
;
537 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
539 tower_data
+= smb_floor
->count_rhs
;
540 tower_size
-= smb_floor
->count_rhs
;
542 if (tower_size
< sizeof(*nb_floor
))
543 return EPT_S_NOT_REGISTERED
;
545 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
547 tower_data
+= sizeof(*nb_floor
);
548 tower_size
-= sizeof(*nb_floor
);
550 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
551 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
552 (nb_floor
->count_rhs
> tower_size
) ||
553 (tower_data
[nb_floor
->count_rhs
- 1] != '\0'))
554 return EPT_S_NOT_REGISTERED
;
558 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
563 I_RpcFree(*endpoint
);
566 return RPC_S_OUT_OF_RESOURCES
;
568 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
574 typedef struct _RpcServerProtseq_np
576 RpcServerProtseq common
;
578 } RpcServerProtseq_np
;
580 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
582 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
584 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
588 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
590 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
591 SetEvent(npps
->mgr_event
);
594 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
596 HANDLE
*objs
= prev_array
;
597 RpcConnection_np
*conn
;
598 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
600 EnterCriticalSection(&protseq
->cs
);
602 /* open and count connections */
604 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
606 rpcrt4_conn_listen_pipe(conn
);
607 if (conn
->ovl
[0].hEvent
)
609 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
612 /* make array of connections */
614 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
616 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
619 ERR("couldn't allocate objs\n");
620 LeaveCriticalSection(&protseq
->cs
);
624 objs
[0] = npps
->mgr_event
;
626 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
628 if ((objs
[*count
] = conn
->ovl
[0].hEvent
))
630 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
632 LeaveCriticalSection(&protseq
->cs
);
636 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
638 HeapFree(GetProcessHeap(), 0, array
);
641 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
644 HANDLE
*objs
= wait_array
;
646 RpcConnection
*cconn
;
647 RpcConnection_np
*conn
;
654 /* an alertable wait isn't strictly necessary, but due to our
655 * overlapped I/O implementation in Wine we need to free some memory
656 * by the file user APC being called, even if no completion routine was
657 * specified at the time of starting the async operation */
658 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
659 } while (res
== WAIT_IO_COMPLETION
);
661 if (res
== WAIT_OBJECT_0
)
663 else if (res
== WAIT_FAILED
)
665 ERR("wait failed with error %d\n", GetLastError());
670 b_handle
= objs
[res
- WAIT_OBJECT_0
];
671 /* find which connection got a RPC */
672 EnterCriticalSection(&protseq
->cs
);
673 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
675 if (b_handle
== conn
->ovl
[0].hEvent
) break;
676 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
680 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
682 ERR("failed to locate connection for handle %p\n", b_handle
);
683 LeaveCriticalSection(&protseq
->cs
);
686 RPCRT4_new_client(cconn
);
693 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
694 const char *networkaddr
,
695 const char *endpoint
)
697 twr_empty_floor_t
*pipe_floor
;
699 size_t endpoint_size
;
701 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
703 endpoint_size
= strlen(endpoint
) + 1;
704 size
= sizeof(*pipe_floor
) + endpoint_size
;
709 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
711 tower_data
+= sizeof(*pipe_floor
);
713 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
714 pipe_floor
->protid
= EPM_PROTOCOL_PIPE
;
715 pipe_floor
->count_rhs
= endpoint_size
;
717 memcpy(tower_data
, endpoint
, endpoint_size
);
722 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
727 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
729 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
731 if (tower_size
< sizeof(*pipe_floor
))
732 return EPT_S_NOT_REGISTERED
;
734 tower_data
+= sizeof(*pipe_floor
);
735 tower_size
-= sizeof(*pipe_floor
);
737 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
738 (pipe_floor
->protid
!= EPM_PROTOCOL_PIPE
) ||
739 (pipe_floor
->count_rhs
> tower_size
) ||
740 (tower_data
[pipe_floor
->count_rhs
- 1] != '\0'))
741 return EPT_S_NOT_REGISTERED
;
748 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
750 return RPC_S_OUT_OF_RESOURCES
;
751 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
757 /**** ncacn_ip_tcp support ****/
759 #ifdef HAVE_SOCKETPAIR
761 typedef struct _RpcConnection_tcp
763 RpcConnection common
;
765 SOCKET cancel_fds
[2];
768 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
770 RpcConnection_tcp
*tcpc
;
771 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
775 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
777 ERR("socketpair() failed: %s\n", strerror(errno
));
778 HeapFree(GetProcessHeap(), 0, tcpc
);
781 return &tcpc
->common
;
784 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
786 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
790 struct addrinfo
*ai_cur
;
791 struct addrinfo hints
;
793 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
795 if (tcpc
->sock
!= -1)
799 hints
.ai_family
= PF_UNSPEC
;
800 hints
.ai_socktype
= SOCK_STREAM
;
801 hints
.ai_protocol
= IPPROTO_TCP
;
802 hints
.ai_addrlen
= 0;
803 hints
.ai_addr
= NULL
;
804 hints
.ai_canonname
= NULL
;
805 hints
.ai_next
= NULL
;
807 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
810 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
811 Connection
->Endpoint
, gai_strerror(ret
));
812 return RPC_S_SERVER_UNAVAILABLE
;
815 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
823 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
824 host
, sizeof(host
), service
, sizeof(service
),
825 NI_NUMERICHOST
| NI_NUMERICSERV
);
826 TRACE("trying %s:%s\n", host
, service
);
829 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
832 WARN("socket() failed: %s\n", strerror(errno
));
836 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
838 WARN("connect() failed: %s\n", strerror(errno
));
843 /* RPC depends on having minimal latency so disable the Nagle algorithm */
845 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
846 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
851 TRACE("connected\n");
856 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
857 return RPC_S_SERVER_UNAVAILABLE
;
860 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
862 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
866 struct addrinfo
*ai_cur
;
867 struct addrinfo hints
;
868 RpcConnection
*first_connection
= NULL
;
870 TRACE("(%p, %s)\n", protseq
, endpoint
);
872 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
873 hints
.ai_family
= PF_UNSPEC
;
874 hints
.ai_socktype
= SOCK_STREAM
;
875 hints
.ai_protocol
= IPPROTO_TCP
;
876 hints
.ai_addrlen
= 0;
877 hints
.ai_addr
= NULL
;
878 hints
.ai_canonname
= NULL
;
879 hints
.ai_next
= NULL
;
881 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
884 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
886 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
887 return RPC_S_INVALID_ENDPOINT_FORMAT
;
888 return RPC_S_CANT_CREATE_ENDPOINT
;
891 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
893 RpcConnection_tcp
*tcpc
;
894 RPC_STATUS create_status
;
900 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
901 host
, sizeof(host
), service
, sizeof(service
),
902 NI_NUMERICHOST
| NI_NUMERICSERV
);
903 TRACE("trying %s:%s\n", host
, service
);
906 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
909 WARN("socket() failed: %s\n", strerror(errno
));
910 status
= RPC_S_CANT_CREATE_ENDPOINT
;
914 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
917 WARN("bind failed: %s\n", strerror(errno
));
919 if (errno
== EADDRINUSE
)
920 status
= RPC_S_DUPLICATE_ENDPOINT
;
922 status
= RPC_S_CANT_CREATE_ENDPOINT
;
925 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
926 protseq
->Protseq
, NULL
,
927 endpoint
, NULL
, NULL
, NULL
);
928 if (create_status
!= RPC_S_OK
)
931 status
= create_status
;
936 ret
= listen(sock
, protseq
->MaxCalls
);
939 WARN("listen failed: %s\n", strerror(errno
));
940 RPCRT4_DestroyConnection(&tcpc
->common
);
941 status
= RPC_S_OUT_OF_RESOURCES
;
944 /* need a non-blocking socket, otherwise accept() has a potential
945 * race-condition (poll() says it is readable, connection drops,
946 * and accept() blocks until the next connection comes...)
948 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
951 WARN("couldn't make socket non-blocking, error %d\n", ret
);
952 RPCRT4_DestroyConnection(&tcpc
->common
);
953 status
= RPC_S_OUT_OF_RESOURCES
;
957 tcpc
->common
.Next
= first_connection
;
958 first_connection
= &tcpc
->common
;
963 /* if at least one connection was created for an endpoint then
965 if (first_connection
)
969 /* find last element in list */
970 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
973 EnterCriticalSection(&protseq
->cs
);
974 conn
->Next
= protseq
->conn
;
975 protseq
->conn
= first_connection
;
976 LeaveCriticalSection(&protseq
->cs
);
978 TRACE("listening on %s\n", endpoint
);
982 ERR("couldn't listen on port %s\n", endpoint
);
986 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
989 struct sockaddr_in address
;
991 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
992 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
994 addrsize
= sizeof(address
);
995 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
998 ERR("Failed to accept a TCP connection: error %d\n", ret
);
999 return RPC_S_OUT_OF_RESOURCES
;
1001 /* reset to blocking behaviour */
1002 fcntl(ret
, F_SETFL
, 0);
1004 TRACE("Accepted a new TCP connection\n");
1008 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
1009 void *buffer
, unsigned int count
)
1011 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1015 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
1020 else if (errno
!= EAGAIN
)
1022 WARN("recv() failed: %s\n", strerror(errno
));
1027 struct pollfd pfds
[2];
1028 pfds
[0].fd
= tcpc
->sock
;
1029 pfds
[0].events
= POLLIN
;
1030 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1031 pfds
[1].events
= POLLIN
;
1032 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1034 ERR("poll() failed: %s\n", strerror(errno
));
1037 if (pfds
[1].revents
& POLLIN
) /* canceled */
1040 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1044 } while (bytes_read
!= count
);
1045 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1049 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1050 const void *buffer
, unsigned int count
)
1052 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1053 int bytes_written
= 0;
1056 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1059 else if (errno
!= EAGAIN
)
1064 pfd
.fd
= tcpc
->sock
;
1065 pfd
.events
= POLLOUT
;
1066 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1068 ERR("poll() failed: %s\n", strerror(errno
));
1072 } while (bytes_written
!= count
);
1073 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1074 return bytes_written
;
1077 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1079 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1081 TRACE("%d\n", tcpc
->sock
);
1083 if (tcpc
->sock
!= -1)
1084 closesocket(tcpc
->sock
);
1086 close(tcpc
->cancel_fds
[0]);
1087 close(tcpc
->cancel_fds
[1]);
1091 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1093 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1096 TRACE("%p\n", Connection
);
1098 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1101 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1103 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1104 struct pollfd pfds
[2];
1106 TRACE("%p\n", Connection
);
1108 pfds
[0].fd
= tcpc
->sock
;
1109 pfds
[0].events
= POLLIN
;
1110 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1111 pfds
[1].events
= POLLIN
;
1112 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1114 ERR("poll() failed: %s\n", strerror(errno
));
1117 if (pfds
[1].revents
& POLLIN
) /* canceled */
1120 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1127 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1128 const char *networkaddr
,
1129 const char *endpoint
)
1131 twr_tcp_floor_t
*tcp_floor
;
1132 twr_ipv4_floor_t
*ipv4_floor
;
1133 struct addrinfo
*ai
;
1134 struct addrinfo hints
;
1136 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1138 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1143 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1144 tower_data
+= sizeof(*tcp_floor
);
1146 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1148 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1149 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1150 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1152 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1153 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1154 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1156 hints
.ai_flags
= AI_NUMERICHOST
;
1157 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1158 hints
.ai_family
= PF_INET
;
1159 hints
.ai_socktype
= SOCK_STREAM
;
1160 hints
.ai_protocol
= IPPROTO_TCP
;
1161 hints
.ai_addrlen
= 0;
1162 hints
.ai_addr
= NULL
;
1163 hints
.ai_canonname
= NULL
;
1164 hints
.ai_next
= NULL
;
1166 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1169 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1172 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1177 if (ai
->ai_family
== PF_INET
)
1179 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1180 tcp_floor
->port
= sin
->sin_port
;
1181 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1185 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1194 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1199 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1200 const twr_ipv4_floor_t
*ipv4_floor
;
1201 struct in_addr in_addr
;
1203 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1205 if (tower_size
< sizeof(*tcp_floor
))
1206 return EPT_S_NOT_REGISTERED
;
1208 tower_data
+= sizeof(*tcp_floor
);
1209 tower_size
-= sizeof(*tcp_floor
);
1211 if (tower_size
< sizeof(*ipv4_floor
))
1212 return EPT_S_NOT_REGISTERED
;
1214 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1216 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1217 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1218 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1219 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1220 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1221 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1222 return EPT_S_NOT_REGISTERED
;
1226 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1228 return RPC_S_OUT_OF_RESOURCES
;
1229 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1234 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1239 I_RpcFree(*endpoint
);
1242 return RPC_S_OUT_OF_RESOURCES
;
1244 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1245 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1247 ERR("inet_ntop: %s\n", strerror(errno
));
1248 I_RpcFree(*networkaddr
);
1249 *networkaddr
= NULL
;
1252 I_RpcFree(*endpoint
);
1255 return EPT_S_NOT_REGISTERED
;
1262 typedef struct _RpcServerProtseq_sock
1264 RpcServerProtseq common
;
1267 } RpcServerProtseq_sock
;
1269 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1271 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1275 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1277 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1278 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1279 ps
->mgr_event_rcv
= fds
[0];
1280 ps
->mgr_event_snd
= fds
[1];
1284 ERR("socketpair failed with error %s\n", strerror(errno
));
1285 HeapFree(GetProcessHeap(), 0, ps
);
1292 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1294 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1296 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1299 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1301 struct pollfd
*poll_info
= prev_array
;
1302 RpcConnection_tcp
*conn
;
1303 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1305 EnterCriticalSection(&protseq
->cs
);
1307 /* open and count connections */
1309 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1311 if (conn
->sock
!= -1)
1313 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1316 /* make array of connections */
1318 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1320 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1323 ERR("couldn't allocate poll_info\n");
1324 LeaveCriticalSection(&protseq
->cs
);
1328 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1329 poll_info
[0].events
= POLLIN
;
1331 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1333 if (conn
->sock
!= -1)
1335 poll_info
[*count
].fd
= conn
->sock
;
1336 poll_info
[*count
].events
= POLLIN
;
1339 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1341 LeaveCriticalSection(&protseq
->cs
);
1345 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1347 HeapFree(GetProcessHeap(), 0, array
);
1350 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1352 struct pollfd
*poll_info
= wait_array
;
1355 RpcConnection
*cconn
;
1356 RpcConnection_tcp
*conn
;
1361 ret
= poll(poll_info
, count
, -1);
1364 ERR("poll failed with error %d\n", ret
);
1368 for (i
= 0; i
< count
; i
++)
1369 if (poll_info
[i
].revents
& POLLIN
)
1371 /* RPC server event */
1375 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1379 /* find which connection got a RPC */
1380 EnterCriticalSection(&protseq
->cs
);
1381 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1383 if (poll_info
[i
].fd
== conn
->sock
) break;
1384 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1388 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1390 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1391 LeaveCriticalSection(&protseq
->cs
);
1393 RPCRT4_new_client(cconn
);
1401 #endif /* HAVE_SOCKETPAIR */
1403 static const struct connection_ops conn_protseq_list
[] = {
1405 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1406 rpcrt4_conn_np_alloc
,
1407 rpcrt4_ncacn_np_open
,
1408 rpcrt4_ncacn_np_handoff
,
1409 rpcrt4_conn_np_read
,
1410 rpcrt4_conn_np_write
,
1411 rpcrt4_conn_np_close
,
1412 rpcrt4_conn_np_cancel_call
,
1413 rpcrt4_conn_np_wait_for_incoming_data
,
1414 rpcrt4_ncacn_np_get_top_of_tower
,
1415 rpcrt4_ncacn_np_parse_top_of_tower
,
1418 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1419 rpcrt4_conn_np_alloc
,
1420 rpcrt4_ncalrpc_open
,
1421 rpcrt4_ncalrpc_handoff
,
1422 rpcrt4_conn_np_read
,
1423 rpcrt4_conn_np_write
,
1424 rpcrt4_conn_np_close
,
1425 rpcrt4_conn_np_cancel_call
,
1426 rpcrt4_conn_np_wait_for_incoming_data
,
1427 rpcrt4_ncalrpc_get_top_of_tower
,
1428 rpcrt4_ncalrpc_parse_top_of_tower
,
1430 #ifdef HAVE_SOCKETPAIR
1432 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1433 rpcrt4_conn_tcp_alloc
,
1434 rpcrt4_ncacn_ip_tcp_open
,
1435 rpcrt4_conn_tcp_handoff
,
1436 rpcrt4_conn_tcp_read
,
1437 rpcrt4_conn_tcp_write
,
1438 rpcrt4_conn_tcp_close
,
1439 rpcrt4_conn_tcp_cancel_call
,
1440 rpcrt4_conn_tcp_wait_for_incoming_data
,
1441 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1442 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1448 static const struct protseq_ops protseq_list
[] =
1452 rpcrt4_protseq_np_alloc
,
1453 rpcrt4_protseq_np_signal_state_changed
,
1454 rpcrt4_protseq_np_get_wait_array
,
1455 rpcrt4_protseq_np_free_wait_array
,
1456 rpcrt4_protseq_np_wait_for_new_connection
,
1457 rpcrt4_protseq_ncacn_np_open_endpoint
,
1461 rpcrt4_protseq_np_alloc
,
1462 rpcrt4_protseq_np_signal_state_changed
,
1463 rpcrt4_protseq_np_get_wait_array
,
1464 rpcrt4_protseq_np_free_wait_array
,
1465 rpcrt4_protseq_np_wait_for_new_connection
,
1466 rpcrt4_protseq_ncalrpc_open_endpoint
,
1468 #ifdef HAVE_SOCKETPAIR
1471 rpcrt4_protseq_sock_alloc
,
1472 rpcrt4_protseq_sock_signal_state_changed
,
1473 rpcrt4_protseq_sock_get_wait_array
,
1474 rpcrt4_protseq_sock_free_wait_array
,
1475 rpcrt4_protseq_sock_wait_for_new_connection
,
1476 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1481 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1483 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1486 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1487 if (!strcmp(protseq_list
[i
].name
, protseq
))
1488 return &protseq_list
[i
];
1492 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1495 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1496 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1497 return &conn_protseq_list
[i
];
1501 /**** interface to rest of code ****/
1503 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1505 TRACE("(Connection == ^%p)\n", Connection
);
1507 assert(!Connection
->server
);
1508 return Connection
->ops
->open_connection_client(Connection
);
1511 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1513 TRACE("(Connection == ^%p)\n", Connection
);
1514 if (SecIsValidHandle(&Connection
->ctx
))
1516 DeleteSecurityContext(&Connection
->ctx
);
1517 SecInvalidateHandle(&Connection
->ctx
);
1519 rpcrt4_conn_close(Connection
);
1523 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1524 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1525 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1527 const struct connection_ops
*ops
;
1528 RpcConnection
* NewConnection
;
1530 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1533 FIXME("not supported for protseq %s\n", Protseq
);
1534 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1537 NewConnection
= ops
->alloc();
1538 NewConnection
->Next
= NULL
;
1539 NewConnection
->server_binding
= NULL
;
1540 NewConnection
->server
= server
;
1541 NewConnection
->ops
= ops
;
1542 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1543 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1544 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1545 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1546 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1547 NewConnection
->NextCallId
= 1;
1549 SecInvalidateHandle(&NewConnection
->ctx
);
1550 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1551 NewConnection
->attr
= 0;
1552 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1553 NewConnection
->AuthInfo
= AuthInfo
;
1554 NewConnection
->encryption_auth_len
= 0;
1555 NewConnection
->signature_auth_len
= 0;
1556 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1557 NewConnection
->QOS
= QOS
;
1559 list_init(&NewConnection
->conn_pool_entry
);
1560 NewConnection
->async_state
= NULL
;
1562 TRACE("connection: %p\n", NewConnection
);
1563 *Connection
= NewConnection
;
1568 static RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1572 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1573 rpcrt4_conn_get_name(OldConnection
),
1574 OldConnection
->NetworkAddr
,
1575 OldConnection
->Endpoint
, NULL
,
1576 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1577 if (err
== RPC_S_OK
)
1578 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1582 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1584 TRACE("connection: %p\n", Connection
);
1586 RPCRT4_CloseConnection(Connection
);
1587 RPCRT4_strfree(Connection
->Endpoint
);
1588 RPCRT4_strfree(Connection
->NetworkAddr
);
1589 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1590 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1591 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1594 if (Connection
->server_binding
) RPCRT4_ReleaseBinding(Connection
->server_binding
);
1596 HeapFree(GetProcessHeap(), 0, Connection
);
1600 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1602 const char *protseq
,
1603 const char *networkaddr
,
1604 const char *endpoint
)
1606 twr_empty_floor_t
*protocol_floor
;
1607 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1612 return RPC_S_INVALID_RPC_PROTSEQ
;
1616 *tower_size
= sizeof(*protocol_floor
);
1617 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1621 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1622 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1623 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1624 protocol_floor
->count_rhs
= 0;
1626 tower_data
+= sizeof(*protocol_floor
);
1628 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1630 return EPT_S_NOT_REGISTERED
;
1632 *tower_size
+= sizeof(*protocol_floor
);
1637 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1643 const twr_empty_floor_t
*protocol_floor
;
1644 const twr_empty_floor_t
*floor4
;
1645 const struct connection_ops
*protseq_ops
= NULL
;
1649 if (tower_size
< sizeof(*protocol_floor
))
1650 return EPT_S_NOT_REGISTERED
;
1652 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1653 tower_data
+= sizeof(*protocol_floor
);
1654 tower_size
-= sizeof(*protocol_floor
);
1655 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1656 (protocol_floor
->count_rhs
> tower_size
))
1657 return EPT_S_NOT_REGISTERED
;
1658 tower_data
+= protocol_floor
->count_rhs
;
1659 tower_size
-= protocol_floor
->count_rhs
;
1661 floor4
= (const twr_empty_floor_t
*)tower_data
;
1662 if ((tower_size
< sizeof(*floor4
)) ||
1663 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1664 return EPT_S_NOT_REGISTERED
;
1666 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1667 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1668 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1670 protseq_ops
= &conn_protseq_list
[i
];
1675 return EPT_S_NOT_REGISTERED
;
1677 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1679 if ((status
== RPC_S_OK
) && protseq
)
1681 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1682 strcpy(*protseq
, protseq_ops
->name
);
1688 /***********************************************************************
1689 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1691 * Checks if the given protocol sequence is known by the RPC system.
1692 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1695 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1699 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1700 ps
, sizeof ps
, NULL
, NULL
);
1701 if (rpcrt4_get_conn_protseq_ops(ps
))
1704 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1706 return RPC_S_INVALID_RPC_PROTSEQ
;
1709 /***********************************************************************
1710 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1712 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1714 UNICODE_STRING protseqW
;
1716 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1718 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1719 RtlFreeUnicodeString(&protseqW
);
1722 return RPC_S_OUT_OF_MEMORY
;