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
;
121 if (ConnectNamedPipe(npc
->pipe
, &npc
->ovl
))
124 if (GetLastError() == ERROR_PIPE_CONNECTED
) {
125 SetEvent(npc
->ovl
.hEvent
);
128 if (GetLastError() == ERROR_IO_PENDING
) {
129 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
132 npc
->listening
= FALSE
;
133 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
134 return RPC_S_OUT_OF_RESOURCES
;
137 static RPC_STATUS
rpcrt4_conn_create_pipe(RpcConnection
*Connection
, LPCSTR pname
)
139 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
140 TRACE("listening on %s\n", pname
);
142 npc
->pipe
= CreateNamedPipeA(pname
, PIPE_ACCESS_DUPLEX
,
143 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
,
144 PIPE_UNLIMITED_INSTANCES
,
145 RPC_MAX_PACKET_SIZE
, RPC_MAX_PACKET_SIZE
, 5000, NULL
);
146 if (npc
->pipe
== INVALID_HANDLE_VALUE
) {
147 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
148 if (GetLastError() == ERROR_FILE_EXISTS
)
149 return RPC_S_DUPLICATE_ENDPOINT
;
151 return RPC_S_CANT_CREATE_ENDPOINT
;
154 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
155 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
157 /* Note: we don't call ConnectNamedPipe here because it must be done in the
158 * server thread as the thread must be alertable */
162 static RPC_STATUS
rpcrt4_conn_open_pipe(RpcConnection
*Connection
, LPCSTR pname
, BOOL wait
)
164 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
168 TRACE("connecting to %s\n", pname
);
174 dwFlags
= SECURITY_SQOS_PRESENT
;
175 switch (Connection
->QOS
->qos
->ImpersonationType
)
177 case RPC_C_IMP_LEVEL_DEFAULT
:
178 /* FIXME: what to do here? */
180 case RPC_C_IMP_LEVEL_ANONYMOUS
:
181 dwFlags
|= SECURITY_ANONYMOUS
;
183 case RPC_C_IMP_LEVEL_IDENTIFY
:
184 dwFlags
|= SECURITY_IDENTIFICATION
;
186 case RPC_C_IMP_LEVEL_IMPERSONATE
:
187 dwFlags
|= SECURITY_IMPERSONATION
;
189 case RPC_C_IMP_LEVEL_DELEGATE
:
190 dwFlags
|= SECURITY_DELEGATION
;
193 if (Connection
->QOS
->qos
->IdentityTracking
== RPC_C_QOS_IDENTIFY_DYNAMIC
)
194 dwFlags
|= SECURITY_CONTEXT_TRACKING
;
196 pipe
= CreateFileA(pname
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
197 OPEN_EXISTING
, dwFlags
, 0);
198 if (pipe
!= INVALID_HANDLE_VALUE
) break;
199 err
= GetLastError();
200 if (err
== ERROR_PIPE_BUSY
) {
201 TRACE("connection failed, error=%x\n", err
);
202 return RPC_S_SERVER_TOO_BUSY
;
205 return RPC_S_SERVER_UNAVAILABLE
;
206 if (!WaitNamedPipeA(pname
, NMPWAIT_WAIT_FOREVER
)) {
207 err
= GetLastError();
208 WARN("connection failed, error=%x\n", err
);
209 return RPC_S_SERVER_UNAVAILABLE
;
214 memset(&npc
->ovl
, 0, sizeof(npc
->ovl
));
215 /* pipe is connected; change to message-read mode. */
216 dwMode
= PIPE_READMODE_MESSAGE
;
217 SetNamedPipeHandleState(pipe
, &dwMode
, NULL
, NULL
);
218 npc
->ovl
.hEvent
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
224 static RPC_STATUS
rpcrt4_ncalrpc_open(RpcConnection
* Connection
)
226 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
227 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
231 /* already connected? */
235 /* protseq=ncalrpc: supposed to use NT LPC ports,
236 * but we'll implement it with named pipes for now */
237 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
238 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
239 r
= rpcrt4_conn_open_pipe(Connection
, pname
, TRUE
);
245 static RPC_STATUS
rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq
* protseq
, LPSTR endpoint
)
247 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
250 RpcConnection
*Connection
;
252 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
253 endpoint
, NULL
, NULL
, NULL
);
257 /* protseq=ncalrpc: supposed to use NT LPC ports,
258 * but we'll implement it with named pipes for now */
259 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
260 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
261 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
264 EnterCriticalSection(&protseq
->cs
);
265 Connection
->Next
= protseq
->conn
;
266 protseq
->conn
= Connection
;
267 LeaveCriticalSection(&protseq
->cs
);
272 static RPC_STATUS
rpcrt4_ncacn_np_open(RpcConnection
* Connection
)
274 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
275 static const char prefix
[] = "\\\\.";
279 /* already connected? */
283 /* protseq=ncacn_np: named pipes */
284 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
285 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
286 r
= rpcrt4_conn_open_pipe(Connection
, pname
, FALSE
);
292 static RPC_STATUS
rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
294 static const char prefix
[] = "\\\\.";
297 RpcConnection
*Connection
;
299 r
= RPCRT4_CreateConnection(&Connection
, TRUE
, protseq
->Protseq
, NULL
,
300 endpoint
, NULL
, NULL
, NULL
);
304 /* protseq=ncacn_np: named pipes */
305 pname
= I_RpcAllocate(strlen(prefix
) + strlen(Connection
->Endpoint
) + 1);
306 strcat(strcpy(pname
, prefix
), Connection
->Endpoint
);
307 r
= rpcrt4_conn_create_pipe(Connection
, pname
);
310 EnterCriticalSection(&protseq
->cs
);
311 Connection
->Next
= protseq
->conn
;
312 protseq
->conn
= Connection
;
313 LeaveCriticalSection(&protseq
->cs
);
318 static void rpcrt4_conn_np_handoff(RpcConnection_np
*old_npc
, RpcConnection_np
*new_npc
)
320 /* because of the way named pipes work, we'll transfer the connected pipe
321 * to the child, then reopen the server binding to continue listening */
323 new_npc
->pipe
= old_npc
->pipe
;
324 new_npc
->ovl
= old_npc
->ovl
;
326 memset(&old_npc
->ovl
, 0, sizeof(old_npc
->ovl
));
327 old_npc
->listening
= FALSE
;
330 static RPC_STATUS
rpcrt4_ncacn_np_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
334 static const char prefix
[] = "\\\\.";
336 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
338 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
339 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
340 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
346 static RPC_STATUS
rpcrt4_ncalrpc_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
350 static const char prefix
[] = "\\\\.\\pipe\\lrpc\\";
352 TRACE("%s\n", old_conn
->Endpoint
);
354 rpcrt4_conn_np_handoff((RpcConnection_np
*)old_conn
, (RpcConnection_np
*)new_conn
);
356 pname
= I_RpcAllocate(strlen(prefix
) + strlen(old_conn
->Endpoint
) + 1);
357 strcat(strcpy(pname
, prefix
), old_conn
->Endpoint
);
358 status
= rpcrt4_conn_create_pipe(old_conn
, pname
);
364 static int rpcrt4_conn_np_read(RpcConnection
*Connection
,
365 void *buffer
, unsigned int count
)
367 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
370 unsigned int bytes_left
= count
;
375 ret
= ReadFile(npc
->pipe
, buf
, bytes_left
, &bytes_read
, NULL
);
376 if (!ret
|| !bytes_read
)
378 bytes_left
-= bytes_read
;
381 return ret
? count
: -1;
384 static int rpcrt4_conn_np_write(RpcConnection
*Connection
,
385 const void *buffer
, unsigned int count
)
387 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
388 const char *buf
= buffer
;
390 unsigned int bytes_left
= count
;
395 ret
= WriteFile(npc
->pipe
, buf
, bytes_left
, &bytes_written
, NULL
);
396 if (!ret
|| !bytes_written
)
398 bytes_left
-= bytes_written
;
399 buf
+= bytes_written
;
401 return ret
? count
: -1;
404 static int rpcrt4_conn_np_close(RpcConnection
*Connection
)
406 RpcConnection_np
*npc
= (RpcConnection_np
*) Connection
;
408 FlushFileBuffers(npc
->pipe
);
409 CloseHandle(npc
->pipe
);
412 if (npc
->ovl
.hEvent
) {
413 CloseHandle(npc
->ovl
.hEvent
);
419 static void rpcrt4_conn_np_cancel_call(RpcConnection
*Connection
)
421 /* FIXME: implement when named pipe writes use overlapped I/O */
424 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection
*Connection
)
426 /* FIXME: implement when named pipe writes use overlapped I/O */
430 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data
,
431 const char *networkaddr
,
432 const char *endpoint
)
434 twr_empty_floor_t
*smb_floor
;
435 twr_empty_floor_t
*nb_floor
;
437 size_t networkaddr_size
;
438 size_t endpoint_size
;
440 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
442 networkaddr_size
= networkaddr
? strlen(networkaddr
) + 1 : 1;
443 endpoint_size
= endpoint
? strlen(endpoint
) + 1 : 1;
444 size
= sizeof(*smb_floor
) + endpoint_size
+ sizeof(*nb_floor
) + networkaddr_size
;
449 smb_floor
= (twr_empty_floor_t
*)tower_data
;
451 tower_data
+= sizeof(*smb_floor
);
453 smb_floor
->count_lhs
= sizeof(smb_floor
->protid
);
454 smb_floor
->protid
= EPM_PROTOCOL_SMB
;
455 smb_floor
->count_rhs
= endpoint_size
;
458 memcpy(tower_data
, endpoint
, endpoint_size
);
461 tower_data
+= endpoint_size
;
463 nb_floor
= (twr_empty_floor_t
*)tower_data
;
465 tower_data
+= sizeof(*nb_floor
);
467 nb_floor
->count_lhs
= sizeof(nb_floor
->protid
);
468 nb_floor
->protid
= EPM_PROTOCOL_NETBIOS
;
469 nb_floor
->count_rhs
= networkaddr_size
;
472 memcpy(tower_data
, networkaddr
, networkaddr_size
);
475 tower_data
+= networkaddr_size
;
480 static RPC_STATUS
rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data
,
485 const twr_empty_floor_t
*smb_floor
= (const twr_empty_floor_t
*)tower_data
;
486 const twr_empty_floor_t
*nb_floor
;
488 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
490 if (tower_size
< sizeof(*smb_floor
))
491 return EPT_S_NOT_REGISTERED
;
493 tower_data
+= sizeof(*smb_floor
);
494 tower_size
-= sizeof(*smb_floor
);
496 if ((smb_floor
->count_lhs
!= sizeof(smb_floor
->protid
)) ||
497 (smb_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
498 (smb_floor
->count_rhs
> tower_size
))
499 return EPT_S_NOT_REGISTERED
;
503 *endpoint
= I_RpcAllocate(smb_floor
->count_rhs
);
505 return RPC_S_OUT_OF_RESOURCES
;
506 memcpy(*endpoint
, tower_data
, smb_floor
->count_rhs
);
508 tower_data
+= smb_floor
->count_rhs
;
509 tower_size
-= smb_floor
->count_rhs
;
511 if (tower_size
< sizeof(*nb_floor
))
512 return EPT_S_NOT_REGISTERED
;
514 nb_floor
= (const twr_empty_floor_t
*)tower_data
;
516 tower_data
+= sizeof(*nb_floor
);
517 tower_size
-= sizeof(*nb_floor
);
519 if ((nb_floor
->count_lhs
!= sizeof(nb_floor
->protid
)) ||
520 (nb_floor
->protid
!= EPM_PROTOCOL_NETBIOS
) ||
521 (nb_floor
->count_rhs
> tower_size
))
522 return EPT_S_NOT_REGISTERED
;
526 *networkaddr
= I_RpcAllocate(nb_floor
->count_rhs
);
531 I_RpcFree(*endpoint
);
534 return RPC_S_OUT_OF_RESOURCES
;
536 memcpy(*networkaddr
, tower_data
, nb_floor
->count_rhs
);
542 typedef struct _RpcServerProtseq_np
544 RpcServerProtseq common
;
546 } RpcServerProtseq_np
;
548 static RpcServerProtseq
*rpcrt4_protseq_np_alloc(void)
550 RpcServerProtseq_np
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
552 ps
->mgr_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
556 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq
*protseq
)
558 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
559 SetEvent(npps
->mgr_event
);
562 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
564 HANDLE
*objs
= prev_array
;
565 RpcConnection_np
*conn
;
566 RpcServerProtseq_np
*npps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_np
, common
);
568 EnterCriticalSection(&protseq
->cs
);
570 /* open and count connections */
572 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
574 rpcrt4_conn_listen_pipe(conn
);
575 if (conn
->ovl
.hEvent
)
577 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
580 /* make array of connections */
582 objs
= HeapReAlloc(GetProcessHeap(), 0, objs
, *count
*sizeof(HANDLE
));
584 objs
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(HANDLE
));
587 ERR("couldn't allocate objs\n");
588 LeaveCriticalSection(&protseq
->cs
);
592 objs
[0] = npps
->mgr_event
;
594 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
596 if ((objs
[*count
] = conn
->ovl
.hEvent
))
598 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
600 LeaveCriticalSection(&protseq
->cs
);
604 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
606 HeapFree(GetProcessHeap(), 0, array
);
609 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
612 HANDLE
*objs
= wait_array
;
614 RpcConnection
*cconn
;
615 RpcConnection_np
*conn
;
622 /* an alertable wait isn't strictly necessary, but due to our
623 * overlapped I/O implementation in Wine we need to free some memory
624 * by the file user APC being called, even if no completion routine was
625 * specified at the time of starting the async operation */
626 res
= WaitForMultipleObjectsEx(count
, objs
, FALSE
, INFINITE
, TRUE
);
627 } while (res
== WAIT_IO_COMPLETION
);
629 if (res
== WAIT_OBJECT_0
)
631 else if (res
== WAIT_FAILED
)
633 ERR("wait failed with error %d\n", GetLastError());
638 b_handle
= objs
[res
- WAIT_OBJECT_0
];
639 /* find which connection got a RPC */
640 EnterCriticalSection(&protseq
->cs
);
641 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_np
, common
);
643 if (b_handle
== conn
->ovl
.hEvent
) break;
644 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_np
, common
);
648 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
650 ERR("failed to locate connection for handle %p\n", b_handle
);
651 LeaveCriticalSection(&protseq
->cs
);
654 RPCRT4_new_client(cconn
);
661 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data
,
662 const char *networkaddr
,
663 const char *endpoint
)
665 twr_empty_floor_t
*pipe_floor
;
667 size_t endpoint_size
;
669 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
671 endpoint_size
= strlen(networkaddr
) + 1;
672 size
= sizeof(*pipe_floor
) + endpoint_size
;
677 pipe_floor
= (twr_empty_floor_t
*)tower_data
;
679 tower_data
+= sizeof(*pipe_floor
);
681 pipe_floor
->count_lhs
= sizeof(pipe_floor
->protid
);
682 pipe_floor
->protid
= EPM_PROTOCOL_SMB
;
683 pipe_floor
->count_rhs
= endpoint_size
;
685 memcpy(tower_data
, endpoint
, endpoint_size
);
686 tower_data
+= endpoint_size
;
691 static RPC_STATUS
rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data
,
696 const twr_empty_floor_t
*pipe_floor
= (const twr_empty_floor_t
*)tower_data
;
698 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
703 if (tower_size
< sizeof(*pipe_floor
))
704 return EPT_S_NOT_REGISTERED
;
706 tower_data
+= sizeof(*pipe_floor
);
707 tower_size
-= sizeof(*pipe_floor
);
709 if ((pipe_floor
->count_lhs
!= sizeof(pipe_floor
->protid
)) ||
710 (pipe_floor
->protid
!= EPM_PROTOCOL_SMB
) ||
711 (pipe_floor
->count_rhs
> tower_size
))
712 return EPT_S_NOT_REGISTERED
;
716 *endpoint
= I_RpcAllocate(pipe_floor
->count_rhs
);
718 return RPC_S_OUT_OF_RESOURCES
;
719 memcpy(*endpoint
, tower_data
, pipe_floor
->count_rhs
);
725 /**** ncacn_ip_tcp support ****/
727 typedef struct _RpcConnection_tcp
729 RpcConnection common
;
734 static RpcConnection
*rpcrt4_conn_tcp_alloc(void)
736 RpcConnection_tcp
*tcpc
;
737 tcpc
= HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp
));
741 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, tcpc
->cancel_fds
) < 0)
743 ERR("socketpair() failed: %s\n", strerror(errno
));
744 HeapFree(GetProcessHeap(), 0, tcpc
);
747 return &tcpc
->common
;
750 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_open(RpcConnection
* Connection
)
752 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
756 struct addrinfo
*ai_cur
;
757 struct addrinfo hints
;
759 TRACE("(%s, %s)\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
761 if (tcpc
->sock
!= -1)
765 hints
.ai_family
= PF_UNSPEC
;
766 hints
.ai_socktype
= SOCK_STREAM
;
767 hints
.ai_protocol
= IPPROTO_TCP
;
768 hints
.ai_addrlen
= 0;
769 hints
.ai_addr
= NULL
;
770 hints
.ai_canonname
= NULL
;
771 hints
.ai_next
= NULL
;
773 ret
= getaddrinfo(Connection
->NetworkAddr
, Connection
->Endpoint
, &hints
, &ai
);
776 ERR("getaddrinfo for %s:%s failed: %s\n", Connection
->NetworkAddr
,
777 Connection
->Endpoint
, gai_strerror(ret
));
778 return RPC_S_SERVER_UNAVAILABLE
;
781 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
789 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
790 host
, sizeof(host
), service
, sizeof(service
),
791 NI_NUMERICHOST
| NI_NUMERICSERV
);
792 TRACE("trying %s:%s\n", host
, service
);
795 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
798 WARN("socket() failed: %s\n", strerror(errno
));
802 if (0>connect(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
))
804 WARN("connect() failed: %s\n", strerror(errno
));
809 /* RPC depends on having minimal latency so disable the Nagle algorithm */
811 setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
812 fcntl(sock
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
817 TRACE("connected\n");
822 ERR("couldn't connect to %s:%s\n", Connection
->NetworkAddr
, Connection
->Endpoint
);
823 return RPC_S_SERVER_UNAVAILABLE
;
826 static RPC_STATUS
rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq
*protseq
, LPSTR endpoint
)
828 RPC_STATUS status
= RPC_S_CANT_CREATE_ENDPOINT
;
832 struct addrinfo
*ai_cur
;
833 struct addrinfo hints
;
834 RpcConnection
*first_connection
= NULL
;
836 TRACE("(%p, %s)\n", protseq
, endpoint
);
838 hints
.ai_flags
= AI_PASSIVE
/* for non-localhost addresses */;
839 hints
.ai_family
= PF_UNSPEC
;
840 hints
.ai_socktype
= SOCK_STREAM
;
841 hints
.ai_protocol
= IPPROTO_TCP
;
842 hints
.ai_addrlen
= 0;
843 hints
.ai_addr
= NULL
;
844 hints
.ai_canonname
= NULL
;
845 hints
.ai_next
= NULL
;
847 ret
= getaddrinfo(NULL
, endpoint
, &hints
, &ai
);
850 ERR("getaddrinfo for port %s failed: %s\n", endpoint
,
852 if ((ret
== EAI_SERVICE
) || (ret
== EAI_NONAME
))
853 return RPC_S_INVALID_ENDPOINT_FORMAT
;
854 return RPC_S_CANT_CREATE_ENDPOINT
;
857 for (ai_cur
= ai
; ai_cur
; ai_cur
= ai_cur
->ai_next
)
859 RpcConnection_tcp
*tcpc
;
860 RPC_STATUS create_status
;
866 getnameinfo(ai_cur
->ai_addr
, ai_cur
->ai_addrlen
,
867 host
, sizeof(host
), service
, sizeof(service
),
868 NI_NUMERICHOST
| NI_NUMERICSERV
);
869 TRACE("trying %s:%s\n", host
, service
);
872 sock
= socket(ai_cur
->ai_family
, ai_cur
->ai_socktype
, ai_cur
->ai_protocol
);
875 WARN("socket() failed: %s\n", strerror(errno
));
876 status
= RPC_S_CANT_CREATE_ENDPOINT
;
880 ret
= bind(sock
, ai_cur
->ai_addr
, ai_cur
->ai_addrlen
);
883 WARN("bind failed: %s\n", strerror(errno
));
885 if (errno
== EADDRINUSE
)
886 status
= RPC_S_DUPLICATE_ENDPOINT
;
888 status
= RPC_S_CANT_CREATE_ENDPOINT
;
891 create_status
= RPCRT4_CreateConnection((RpcConnection
**)&tcpc
, TRUE
,
892 protseq
->Protseq
, NULL
,
893 endpoint
, NULL
, NULL
, NULL
);
894 if (create_status
!= RPC_S_OK
)
897 status
= create_status
;
902 ret
= listen(sock
, protseq
->MaxCalls
);
905 WARN("listen failed: %s\n", strerror(errno
));
906 RPCRT4_DestroyConnection(&tcpc
->common
);
907 status
= RPC_S_OUT_OF_RESOURCES
;
910 /* need a non-blocking socket, otherwise accept() has a potential
911 * race-condition (poll() says it is readable, connection drops,
912 * and accept() blocks until the next connection comes...)
914 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
917 WARN("couldn't make socket non-blocking, error %d\n", ret
);
918 RPCRT4_DestroyConnection(&tcpc
->common
);
919 status
= RPC_S_OUT_OF_RESOURCES
;
923 tcpc
->common
.Next
= first_connection
;
924 first_connection
= &tcpc
->common
;
929 /* if at least one connection was created for an endpoint then
931 if (first_connection
)
935 /* find last element in list */
936 for (conn
= first_connection
; conn
->Next
; conn
= conn
->Next
)
939 EnterCriticalSection(&protseq
->cs
);
940 conn
->Next
= protseq
->conn
;
941 protseq
->conn
= first_connection
;
942 LeaveCriticalSection(&protseq
->cs
);
944 TRACE("listening on %s\n", endpoint
);
948 ERR("couldn't listen on port %s\n", endpoint
);
952 static RPC_STATUS
rpcrt4_conn_tcp_handoff(RpcConnection
*old_conn
, RpcConnection
*new_conn
)
955 struct sockaddr_in address
;
957 RpcConnection_tcp
*server
= (RpcConnection_tcp
*) old_conn
;
958 RpcConnection_tcp
*client
= (RpcConnection_tcp
*) new_conn
;
960 addrsize
= sizeof(address
);
961 ret
= accept(server
->sock
, (struct sockaddr
*) &address
, &addrsize
);
964 ERR("Failed to accept a TCP connection: error %d\n", ret
);
965 return RPC_S_OUT_OF_RESOURCES
;
967 /* reset to blocking behaviour */
968 fcntl(ret
, F_SETFL
, 0);
970 TRACE("Accepted a new TCP connection\n");
974 static int rpcrt4_conn_tcp_read(RpcConnection
*Connection
,
975 void *buffer
, unsigned int count
)
977 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
981 int r
= recv(tcpc
->sock
, (char *)buffer
+ bytes_read
, count
- bytes_read
, 0);
986 else if (errno
!= EAGAIN
)
988 WARN("recv() failed: %s\n", strerror(errno
));
993 struct pollfd pfds
[2];
994 pfds
[0].fd
= tcpc
->sock
;
995 pfds
[0].events
= POLLIN
;
996 pfds
[1].fd
= tcpc
->cancel_fds
[0];
997 pfds
[1].events
= POLLIN
;
998 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1000 ERR("poll() failed: %s\n", strerror(errno
));
1003 if (pfds
[1].revents
& POLLIN
) /* canceled */
1006 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1010 } while (bytes_read
!= count
);
1011 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_read
);
1015 static int rpcrt4_conn_tcp_write(RpcConnection
*Connection
,
1016 const void *buffer
, unsigned int count
)
1018 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1019 int bytes_written
= 0;
1022 int r
= send(tcpc
->sock
, (const char *)buffer
+ bytes_written
, count
- bytes_written
, 0);
1025 else if (errno
!= EAGAIN
)
1030 pfd
.fd
= tcpc
->sock
;
1031 pfd
.events
= POLLOUT
;
1032 if (poll(&pfd
, 1, -1 /* infinite */) == -1 && errno
!= EINTR
)
1034 ERR("poll() failed: %s\n", strerror(errno
));
1038 } while (bytes_written
!= count
);
1039 TRACE("%d %p %u -> %d\n", tcpc
->sock
, buffer
, count
, bytes_written
);
1040 return bytes_written
;
1043 static int rpcrt4_conn_tcp_close(RpcConnection
*Connection
)
1045 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1047 TRACE("%d\n", tcpc
->sock
);
1049 if (tcpc
->sock
!= -1)
1050 closesocket(tcpc
->sock
);
1052 close(tcpc
->cancel_fds
[0]);
1053 close(tcpc
->cancel_fds
[1]);
1057 static void rpcrt4_conn_tcp_cancel_call(RpcConnection
*Connection
)
1059 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1062 TRACE("%p\n", Connection
);
1064 write(tcpc
->cancel_fds
[1], &dummy
, 1);
1067 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection
*Connection
)
1069 RpcConnection_tcp
*tcpc
= (RpcConnection_tcp
*) Connection
;
1070 struct pollfd pfds
[2];
1072 TRACE("%p\n", Connection
);
1074 pfds
[0].fd
= tcpc
->sock
;
1075 pfds
[0].events
= POLLIN
;
1076 pfds
[1].fd
= tcpc
->cancel_fds
[0];
1077 pfds
[1].events
= POLLIN
;
1078 if (poll(pfds
, 2, -1 /* infinite */) == -1 && errno
!= EINTR
)
1080 ERR("poll() failed: %s\n", strerror(errno
));
1083 if (pfds
[1].revents
& POLLIN
) /* canceled */
1086 read(pfds
[1].fd
, &dummy
, sizeof(dummy
));
1093 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data
,
1094 const char *networkaddr
,
1095 const char *endpoint
)
1097 twr_tcp_floor_t
*tcp_floor
;
1098 twr_ipv4_floor_t
*ipv4_floor
;
1099 struct addrinfo
*ai
;
1100 struct addrinfo hints
;
1102 size_t size
= sizeof(*tcp_floor
) + sizeof(*ipv4_floor
);
1104 TRACE("(%p, %s, %s)\n", tower_data
, networkaddr
, endpoint
);
1109 tcp_floor
= (twr_tcp_floor_t
*)tower_data
;
1110 tower_data
+= sizeof(*tcp_floor
);
1112 ipv4_floor
= (twr_ipv4_floor_t
*)tower_data
;
1114 tcp_floor
->count_lhs
= sizeof(tcp_floor
->protid
);
1115 tcp_floor
->protid
= EPM_PROTOCOL_TCP
;
1116 tcp_floor
->count_rhs
= sizeof(tcp_floor
->port
);
1118 ipv4_floor
->count_lhs
= sizeof(ipv4_floor
->protid
);
1119 ipv4_floor
->protid
= EPM_PROTOCOL_IP
;
1120 ipv4_floor
->count_rhs
= sizeof(ipv4_floor
->ipv4addr
);
1122 hints
.ai_flags
= AI_NUMERICHOST
;
1123 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1124 hints
.ai_family
= PF_INET
;
1125 hints
.ai_socktype
= SOCK_STREAM
;
1126 hints
.ai_protocol
= IPPROTO_TCP
;
1127 hints
.ai_addrlen
= 0;
1128 hints
.ai_addr
= NULL
;
1129 hints
.ai_canonname
= NULL
;
1130 hints
.ai_next
= NULL
;
1132 ret
= getaddrinfo(networkaddr
, endpoint
, &hints
, &ai
);
1135 ret
= getaddrinfo("0.0.0.0", endpoint
, &hints
, &ai
);
1138 ERR("getaddrinfo failed: %s\n", gai_strerror(ret
));
1143 if (ai
->ai_family
== PF_INET
)
1145 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)ai
->ai_addr
;
1146 tcp_floor
->port
= sin
->sin_port
;
1147 ipv4_floor
->ipv4addr
= sin
->sin_addr
.s_addr
;
1151 ERR("unexpected protocol family %d\n", ai
->ai_family
);
1160 static RPC_STATUS
rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data
,
1165 const twr_tcp_floor_t
*tcp_floor
= (const twr_tcp_floor_t
*)tower_data
;
1166 const twr_ipv4_floor_t
*ipv4_floor
;
1167 struct in_addr in_addr
;
1169 TRACE("(%p, %d, %p, %p)\n", tower_data
, (int)tower_size
, networkaddr
, endpoint
);
1171 if (tower_size
< sizeof(*tcp_floor
))
1172 return EPT_S_NOT_REGISTERED
;
1174 tower_data
+= sizeof(*tcp_floor
);
1175 tower_size
-= sizeof(*tcp_floor
);
1177 if (tower_size
< sizeof(*ipv4_floor
))
1178 return EPT_S_NOT_REGISTERED
;
1180 ipv4_floor
= (const twr_ipv4_floor_t
*)tower_data
;
1182 if ((tcp_floor
->count_lhs
!= sizeof(tcp_floor
->protid
)) ||
1183 (tcp_floor
->protid
!= EPM_PROTOCOL_TCP
) ||
1184 (tcp_floor
->count_rhs
!= sizeof(tcp_floor
->port
)) ||
1185 (ipv4_floor
->count_lhs
!= sizeof(ipv4_floor
->protid
)) ||
1186 (ipv4_floor
->protid
!= EPM_PROTOCOL_IP
) ||
1187 (ipv4_floor
->count_rhs
!= sizeof(ipv4_floor
->ipv4addr
)))
1188 return EPT_S_NOT_REGISTERED
;
1192 *endpoint
= I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1194 return RPC_S_OUT_OF_RESOURCES
;
1195 sprintf(*endpoint
, "%u", ntohs(tcp_floor
->port
));
1200 *networkaddr
= I_RpcAllocate(INET_ADDRSTRLEN
);
1205 I_RpcFree(*endpoint
);
1208 return RPC_S_OUT_OF_RESOURCES
;
1210 in_addr
.s_addr
= ipv4_floor
->ipv4addr
;
1211 if (!inet_ntop(AF_INET
, &in_addr
, *networkaddr
, INET_ADDRSTRLEN
))
1213 ERR("inet_ntop: %s\n", strerror(errno
));
1214 I_RpcFree(*networkaddr
);
1215 *networkaddr
= NULL
;
1218 I_RpcFree(*endpoint
);
1221 return EPT_S_NOT_REGISTERED
;
1228 typedef struct _RpcServerProtseq_sock
1230 RpcServerProtseq common
;
1233 } RpcServerProtseq_sock
;
1235 static RpcServerProtseq
*rpcrt4_protseq_sock_alloc(void)
1237 RpcServerProtseq_sock
*ps
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ps
));
1241 if (!socketpair(PF_UNIX
, SOCK_DGRAM
, 0, fds
))
1243 fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1244 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
);
1245 ps
->mgr_event_rcv
= fds
[0];
1246 ps
->mgr_event_snd
= fds
[1];
1250 ERR("socketpair failed with error %s\n", strerror(errno
));
1251 HeapFree(GetProcessHeap(), 0, ps
);
1258 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq
*protseq
)
1260 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1262 write(sockps
->mgr_event_snd
, &dummy
, sizeof(dummy
));
1265 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq
*protseq
, void *prev_array
, unsigned int *count
)
1267 struct pollfd
*poll_info
= prev_array
;
1268 RpcConnection_tcp
*conn
;
1269 RpcServerProtseq_sock
*sockps
= CONTAINING_RECORD(protseq
, RpcServerProtseq_sock
, common
);
1271 EnterCriticalSection(&protseq
->cs
);
1273 /* open and count connections */
1275 conn
= (RpcConnection_tcp
*)protseq
->conn
;
1277 if (conn
->sock
!= -1)
1279 conn
= (RpcConnection_tcp
*)conn
->common
.Next
;
1282 /* make array of connections */
1284 poll_info
= HeapReAlloc(GetProcessHeap(), 0, poll_info
, *count
*sizeof(*poll_info
));
1286 poll_info
= HeapAlloc(GetProcessHeap(), 0, *count
*sizeof(*poll_info
));
1289 ERR("couldn't allocate poll_info\n");
1290 LeaveCriticalSection(&protseq
->cs
);
1294 poll_info
[0].fd
= sockps
->mgr_event_rcv
;
1295 poll_info
[0].events
= POLLIN
;
1297 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1299 if (conn
->sock
!= -1)
1301 poll_info
[*count
].fd
= conn
->sock
;
1302 poll_info
[*count
].events
= POLLIN
;
1305 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1307 LeaveCriticalSection(&protseq
->cs
);
1311 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq
*protseq
, void *array
)
1313 HeapFree(GetProcessHeap(), 0, array
);
1316 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq
*protseq
, unsigned int count
, void *wait_array
)
1318 struct pollfd
*poll_info
= wait_array
;
1320 RpcConnection
*cconn
;
1321 RpcConnection_tcp
*conn
;
1326 ret
= poll(poll_info
, count
, -1);
1329 ERR("poll failed with error %d\n", ret
);
1333 for (i
= 0; i
< count
; i
++)
1334 if (poll_info
[i
].revents
& POLLIN
)
1336 /* RPC server event */
1340 read(poll_info
[0].fd
, &dummy
, sizeof(dummy
));
1344 /* find which connection got a RPC */
1345 EnterCriticalSection(&protseq
->cs
);
1346 conn
= CONTAINING_RECORD(protseq
->conn
, RpcConnection_tcp
, common
);
1348 if (poll_info
[i
].fd
== conn
->sock
) break;
1349 conn
= CONTAINING_RECORD(conn
->common
.Next
, RpcConnection_tcp
, common
);
1353 RPCRT4_SpawnConnection(&cconn
, &conn
->common
);
1355 ERR("failed to locate connection for fd %d\n", poll_info
[i
].fd
);
1356 LeaveCriticalSection(&protseq
->cs
);
1358 RPCRT4_new_client(cconn
);
1366 static const struct connection_ops conn_protseq_list
[] = {
1368 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_SMB
},
1369 rpcrt4_conn_np_alloc
,
1370 rpcrt4_ncacn_np_open
,
1371 rpcrt4_ncacn_np_handoff
,
1372 rpcrt4_conn_np_read
,
1373 rpcrt4_conn_np_write
,
1374 rpcrt4_conn_np_close
,
1375 rpcrt4_conn_np_cancel_call
,
1376 rpcrt4_conn_np_wait_for_incoming_data
,
1377 rpcrt4_ncacn_np_get_top_of_tower
,
1378 rpcrt4_ncacn_np_parse_top_of_tower
,
1381 { EPM_PROTOCOL_NCALRPC
, EPM_PROTOCOL_PIPE
},
1382 rpcrt4_conn_np_alloc
,
1383 rpcrt4_ncalrpc_open
,
1384 rpcrt4_ncalrpc_handoff
,
1385 rpcrt4_conn_np_read
,
1386 rpcrt4_conn_np_write
,
1387 rpcrt4_conn_np_close
,
1388 rpcrt4_conn_np_cancel_call
,
1389 rpcrt4_conn_np_wait_for_incoming_data
,
1390 rpcrt4_ncalrpc_get_top_of_tower
,
1391 rpcrt4_ncalrpc_parse_top_of_tower
,
1394 { EPM_PROTOCOL_NCACN
, EPM_PROTOCOL_TCP
},
1395 rpcrt4_conn_tcp_alloc
,
1396 rpcrt4_ncacn_ip_tcp_open
,
1397 rpcrt4_conn_tcp_handoff
,
1398 rpcrt4_conn_tcp_read
,
1399 rpcrt4_conn_tcp_write
,
1400 rpcrt4_conn_tcp_close
,
1401 rpcrt4_conn_tcp_cancel_call
,
1402 rpcrt4_conn_tcp_wait_for_incoming_data
,
1403 rpcrt4_ncacn_ip_tcp_get_top_of_tower
,
1404 rpcrt4_ncacn_ip_tcp_parse_top_of_tower
,
1409 static const struct protseq_ops protseq_list
[] =
1413 rpcrt4_protseq_np_alloc
,
1414 rpcrt4_protseq_np_signal_state_changed
,
1415 rpcrt4_protseq_np_get_wait_array
,
1416 rpcrt4_protseq_np_free_wait_array
,
1417 rpcrt4_protseq_np_wait_for_new_connection
,
1418 rpcrt4_protseq_ncacn_np_open_endpoint
,
1422 rpcrt4_protseq_np_alloc
,
1423 rpcrt4_protseq_np_signal_state_changed
,
1424 rpcrt4_protseq_np_get_wait_array
,
1425 rpcrt4_protseq_np_free_wait_array
,
1426 rpcrt4_protseq_np_wait_for_new_connection
,
1427 rpcrt4_protseq_ncalrpc_open_endpoint
,
1431 rpcrt4_protseq_sock_alloc
,
1432 rpcrt4_protseq_sock_signal_state_changed
,
1433 rpcrt4_protseq_sock_get_wait_array
,
1434 rpcrt4_protseq_sock_free_wait_array
,
1435 rpcrt4_protseq_sock_wait_for_new_connection
,
1436 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint
,
1440 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1442 const struct protseq_ops
*rpcrt4_get_protseq_ops(const char *protseq
)
1445 for(i
=0; i
<ARRAYSIZE(protseq_list
); i
++)
1446 if (!strcmp(protseq_list
[i
].name
, protseq
))
1447 return &protseq_list
[i
];
1451 static const struct connection_ops
*rpcrt4_get_conn_protseq_ops(const char *protseq
)
1454 for(i
=0; i
<ARRAYSIZE(conn_protseq_list
); i
++)
1455 if (!strcmp(conn_protseq_list
[i
].name
, protseq
))
1456 return &conn_protseq_list
[i
];
1460 /**** interface to rest of code ****/
1462 RPC_STATUS
RPCRT4_OpenClientConnection(RpcConnection
* Connection
)
1464 TRACE("(Connection == ^%p)\n", Connection
);
1466 assert(!Connection
->server
);
1467 return Connection
->ops
->open_connection_client(Connection
);
1470 RPC_STATUS
RPCRT4_CloseConnection(RpcConnection
* Connection
)
1472 TRACE("(Connection == ^%p)\n", Connection
);
1473 if (SecIsValidHandle(&Connection
->ctx
))
1475 DeleteSecurityContext(&Connection
->ctx
);
1476 SecInvalidateHandle(&Connection
->ctx
);
1478 rpcrt4_conn_close(Connection
);
1482 RPC_STATUS
RPCRT4_CreateConnection(RpcConnection
** Connection
, BOOL server
,
1483 LPCSTR Protseq
, LPCSTR NetworkAddr
, LPCSTR Endpoint
,
1484 LPCWSTR NetworkOptions
, RpcAuthInfo
* AuthInfo
, RpcQualityOfService
*QOS
)
1486 const struct connection_ops
*ops
;
1487 RpcConnection
* NewConnection
;
1489 ops
= rpcrt4_get_conn_protseq_ops(Protseq
);
1492 FIXME("not supported for protseq %s\n", Protseq
);
1493 return RPC_S_PROTSEQ_NOT_SUPPORTED
;
1496 NewConnection
= ops
->alloc();
1497 NewConnection
->Next
= NULL
;
1498 NewConnection
->server_binding
= NULL
;
1499 NewConnection
->server
= server
;
1500 NewConnection
->ops
= ops
;
1501 NewConnection
->NetworkAddr
= RPCRT4_strdupA(NetworkAddr
);
1502 NewConnection
->Endpoint
= RPCRT4_strdupA(Endpoint
);
1503 NewConnection
->NetworkOptions
= RPCRT4_strdupW(NetworkOptions
);
1504 NewConnection
->MaxTransmissionSize
= RPC_MAX_PACKET_SIZE
;
1505 memset(&NewConnection
->ActiveInterface
, 0, sizeof(NewConnection
->ActiveInterface
));
1506 NewConnection
->NextCallId
= 1;
1508 SecInvalidateHandle(&NewConnection
->ctx
);
1509 memset(&NewConnection
->exp
, 0, sizeof(NewConnection
->exp
));
1510 NewConnection
->attr
= 0;
1511 if (AuthInfo
) RpcAuthInfo_AddRef(AuthInfo
);
1512 NewConnection
->AuthInfo
= AuthInfo
;
1513 NewConnection
->encryption_auth_len
= 0;
1514 NewConnection
->signature_auth_len
= 0;
1515 if (QOS
) RpcQualityOfService_AddRef(QOS
);
1516 NewConnection
->QOS
= QOS
;
1518 list_init(&NewConnection
->conn_pool_entry
);
1519 NewConnection
->async_state
= NULL
;
1521 TRACE("connection: %p\n", NewConnection
);
1522 *Connection
= NewConnection
;
1528 RPC_STATUS
RPCRT4_SpawnConnection(RpcConnection
** Connection
, RpcConnection
* OldConnection
)
1532 err
= RPCRT4_CreateConnection(Connection
, OldConnection
->server
,
1533 rpcrt4_conn_get_name(OldConnection
),
1534 OldConnection
->NetworkAddr
,
1535 OldConnection
->Endpoint
, NULL
,
1536 OldConnection
->AuthInfo
, OldConnection
->QOS
);
1537 if (err
== RPC_S_OK
)
1538 rpcrt4_conn_handoff(OldConnection
, *Connection
);
1542 RPC_STATUS
RPCRT4_DestroyConnection(RpcConnection
* Connection
)
1544 TRACE("connection: %p\n", Connection
);
1546 RPCRT4_CloseConnection(Connection
);
1547 RPCRT4_strfree(Connection
->Endpoint
);
1548 RPCRT4_strfree(Connection
->NetworkAddr
);
1549 HeapFree(GetProcessHeap(), 0, Connection
->NetworkOptions
);
1550 if (Connection
->AuthInfo
) RpcAuthInfo_Release(Connection
->AuthInfo
);
1551 if (Connection
->QOS
) RpcQualityOfService_Release(Connection
->QOS
);
1554 if (Connection
->server_binding
) RPCRT4_DestroyBinding(Connection
->server_binding
);
1556 HeapFree(GetProcessHeap(), 0, Connection
);
1560 RPC_STATUS
RpcTransport_GetTopOfTower(unsigned char *tower_data
,
1562 const char *protseq
,
1563 const char *networkaddr
,
1564 const char *endpoint
)
1566 twr_empty_floor_t
*protocol_floor
;
1567 const struct connection_ops
*protseq_ops
= rpcrt4_get_conn_protseq_ops(protseq
);
1572 return RPC_S_INVALID_RPC_PROTSEQ
;
1576 *tower_size
= sizeof(*protocol_floor
);
1577 *tower_size
+= protseq_ops
->get_top_of_tower(NULL
, networkaddr
, endpoint
);
1581 protocol_floor
= (twr_empty_floor_t
*)tower_data
;
1582 protocol_floor
->count_lhs
= sizeof(protocol_floor
->protid
);
1583 protocol_floor
->protid
= protseq_ops
->epm_protocols
[0];
1584 protocol_floor
->count_rhs
= 0;
1586 tower_data
+= sizeof(*protocol_floor
);
1588 *tower_size
= protseq_ops
->get_top_of_tower(tower_data
, networkaddr
, endpoint
);
1590 return EPT_S_NOT_REGISTERED
;
1592 *tower_size
+= sizeof(*protocol_floor
);
1597 RPC_STATUS
RpcTransport_ParseTopOfTower(const unsigned char *tower_data
,
1603 const twr_empty_floor_t
*protocol_floor
;
1604 const twr_empty_floor_t
*floor4
;
1605 const struct connection_ops
*protseq_ops
= NULL
;
1609 if (tower_size
< sizeof(*protocol_floor
))
1610 return EPT_S_NOT_REGISTERED
;
1612 protocol_floor
= (const twr_empty_floor_t
*)tower_data
;
1613 tower_data
+= sizeof(*protocol_floor
);
1614 tower_size
-= sizeof(*protocol_floor
);
1615 if ((protocol_floor
->count_lhs
!= sizeof(protocol_floor
->protid
)) ||
1616 (protocol_floor
->count_rhs
> tower_size
))
1617 return EPT_S_NOT_REGISTERED
;
1618 tower_data
+= protocol_floor
->count_rhs
;
1619 tower_size
-= protocol_floor
->count_rhs
;
1621 floor4
= (const twr_empty_floor_t
*)tower_data
;
1622 if ((tower_size
< sizeof(*floor4
)) ||
1623 (floor4
->count_lhs
!= sizeof(floor4
->protid
)))
1624 return EPT_S_NOT_REGISTERED
;
1626 for(i
= 0; i
< ARRAYSIZE(conn_protseq_list
); i
++)
1627 if ((protocol_floor
->protid
== conn_protseq_list
[i
].epm_protocols
[0]) &&
1628 (floor4
->protid
== conn_protseq_list
[i
].epm_protocols
[1]))
1630 protseq_ops
= &conn_protseq_list
[i
];
1635 return EPT_S_NOT_REGISTERED
;
1637 status
= protseq_ops
->parse_top_of_tower(tower_data
, tower_size
, networkaddr
, endpoint
);
1639 if ((status
== RPC_S_OK
) && protseq
)
1641 *protseq
= I_RpcAllocate(strlen(protseq_ops
->name
) + 1);
1642 strcpy(*protseq
, protseq_ops
->name
);
1648 /***********************************************************************
1649 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1651 * Checks if the given protocol sequence is known by the RPC system.
1652 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1655 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidW(RPC_WSTR protseq
)
1659 WideCharToMultiByte(CP_ACP
, 0, protseq
, -1,
1660 ps
, sizeof ps
, NULL
, NULL
);
1661 if (rpcrt4_get_conn_protseq_ops(ps
))
1664 FIXME("Unknown protseq %s\n", debugstr_w(protseq
));
1666 return RPC_S_INVALID_RPC_PROTSEQ
;
1669 /***********************************************************************
1670 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1672 RPC_STATUS WINAPI
RpcNetworkIsProtseqValidA(RPC_CSTR protseq
)
1674 UNICODE_STRING protseqW
;
1676 if (RtlCreateUnicodeStringFromAsciiz(&protseqW
, (char*)protseq
))
1678 RPC_STATUS ret
= RpcNetworkIsProtseqValidW(protseqW
.Buffer
);
1679 RtlFreeUnicodeString(&protseqW
);
1682 return RPC_S_OUT_OF_MEMORY
;