Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / rpcrt4 / rpc_transport.c
blobc3d1ef3cf6045ec22896753ad4798eaf9f2ac6fd
1 /*
2 * RPC transport layer
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
26 #include "config.h"
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <errno.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46 #ifdef HAVE_NETINET_TCP_H
47 # include <netinet/tcp.h>
48 #endif
49 #ifdef HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
51 #endif
52 #ifdef HAVE_NETDB_H
53 #include <netdb.h>
54 #endif
55 #ifdef HAVE_SYS_POLL_H
56 #include <sys/poll.h>
57 #endif
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winnls.h"
62 #include "winerror.h"
63 #include "winternl.h"
64 #include "wine/unicode.h"
66 #include "rpc.h"
67 #include "rpcndr.h"
69 #include "wine/debug.h"
71 #include "rpc_binding.h"
72 #include "rpc_message.h"
73 #include "rpc_server.h"
74 #include "epm_towers.h"
76 #ifndef SOL_TCP
77 # define SOL_TCP IPPROTO_TCP
78 #endif
80 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
82 static CRITICAL_SECTION assoc_list_cs;
83 static CRITICAL_SECTION_DEBUG assoc_list_cs_debug =
85 0, 0, &assoc_list_cs,
86 { &assoc_list_cs_debug.ProcessLocksList, &assoc_list_cs_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": assoc_list_cs") }
89 static CRITICAL_SECTION assoc_list_cs = { &assoc_list_cs_debug, -1, 0, 0, 0, 0 };
91 static struct list assoc_list = LIST_INIT(assoc_list);
93 /**** ncacn_np support ****/
95 typedef struct _RpcConnection_np
97 RpcConnection common;
98 HANDLE pipe;
99 OVERLAPPED ovl;
100 BOOL listening;
101 } RpcConnection_np;
103 static RpcConnection *rpcrt4_conn_np_alloc(void)
105 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
106 if (npc)
108 npc->pipe = NULL;
109 memset(&npc->ovl, 0, sizeof(npc->ovl));
110 npc->listening = FALSE;
112 return &npc->common;
115 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
117 if (npc->listening)
118 return RPC_S_OK;
120 npc->listening = TRUE;
121 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
122 return RPC_S_OK;
124 if (GetLastError() == ERROR_PIPE_CONNECTED) {
125 SetEvent(npc->ovl.hEvent);
126 return RPC_S_OK;
128 if (GetLastError() == ERROR_IO_PENDING) {
129 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
130 return RPC_S_OK;
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;
150 else
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 */
159 return RPC_S_OK;
162 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
164 RpcConnection_np *npc = (RpcConnection_np *) Connection;
165 HANDLE pipe;
166 DWORD err, dwMode;
168 TRACE("connecting to %s\n", pname);
170 while (TRUE) {
171 DWORD dwFlags = 0;
172 if (Connection->QOS)
174 dwFlags = SECURITY_SQOS_PRESENT;
175 switch (Connection->QOS->qos->ImpersonationType)
177 case RPC_C_IMP_LEVEL_DEFAULT:
178 /* FIXME: what to do here? */
179 break;
180 case RPC_C_IMP_LEVEL_ANONYMOUS:
181 dwFlags |= SECURITY_ANONYMOUS;
182 break;
183 case RPC_C_IMP_LEVEL_IDENTIFY:
184 dwFlags |= SECURITY_IDENTIFICATION;
185 break;
186 case RPC_C_IMP_LEVEL_IMPERSONATE:
187 dwFlags |= SECURITY_IMPERSONATION;
188 break;
189 case RPC_C_IMP_LEVEL_DELEGATE:
190 dwFlags |= SECURITY_DELEGATION;
191 break;
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;
204 if (!wait)
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;
213 /* success */
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);
219 npc->pipe = pipe;
221 return RPC_S_OK;
224 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
226 RpcConnection_np *npc = (RpcConnection_np *) Connection;
227 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
228 RPC_STATUS r;
229 LPSTR pname;
231 /* already connected? */
232 if (npc->pipe)
233 return RPC_S_OK;
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);
240 I_RpcFree(pname);
242 return r;
245 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
247 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
248 RPC_STATUS r;
249 LPSTR pname;
250 RpcConnection *Connection;
252 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
253 endpoint, NULL, NULL, NULL, NULL);
254 if (r != RPC_S_OK)
255 return r;
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);
262 I_RpcFree(pname);
264 EnterCriticalSection(&protseq->cs);
265 Connection->Next = protseq->conn;
266 protseq->conn = Connection;
267 LeaveCriticalSection(&protseq->cs);
269 return r;
272 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
274 RpcConnection_np *npc = (RpcConnection_np *) Connection;
275 static const char prefix[] = "\\\\.";
276 RPC_STATUS r;
277 LPSTR pname;
279 /* already connected? */
280 if (npc->pipe)
281 return RPC_S_OK;
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);
287 I_RpcFree(pname);
289 return r;
292 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
294 static const char prefix[] = "\\\\.";
295 RPC_STATUS r;
296 LPSTR pname;
297 RpcConnection *Connection;
299 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
300 endpoint, NULL, NULL, NULL, NULL);
301 if (r != RPC_S_OK)
302 return r;
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);
308 I_RpcFree(pname);
310 EnterCriticalSection(&protseq->cs);
311 Connection->Next = protseq->conn;
312 protseq->conn = Connection;
313 LeaveCriticalSection(&protseq->cs);
315 return r;
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;
325 old_npc->pipe = 0;
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)
332 RPC_STATUS status;
333 LPSTR pname;
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);
341 I_RpcFree(pname);
343 return status;
346 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
348 RPC_STATUS status;
349 LPSTR pname;
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);
359 I_RpcFree(pname);
361 return status;
364 static int rpcrt4_conn_np_read(RpcConnection *Connection,
365 void *buffer, unsigned int count)
367 RpcConnection_np *npc = (RpcConnection_np *) Connection;
368 char *buf = buffer;
369 BOOL ret = TRUE;
370 unsigned int bytes_left = count;
372 while (bytes_left)
374 DWORD bytes_read;
375 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
376 if (!ret || !bytes_read)
377 break;
378 bytes_left -= bytes_read;
379 buf += 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;
389 BOOL ret = TRUE;
390 unsigned int bytes_left = count;
392 while (bytes_left)
394 DWORD bytes_written;
395 ret = WriteFile(npc->pipe, buf, count, &bytes_written, NULL);
396 if (!ret || !bytes_written)
397 break;
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;
407 if (npc->pipe) {
408 FlushFileBuffers(npc->pipe);
409 CloseHandle(npc->pipe);
410 npc->pipe = 0;
412 if (npc->ovl.hEvent) {
413 CloseHandle(npc->ovl.hEvent);
414 npc->ovl.hEvent = 0;
416 return 0;
419 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
420 const char *networkaddr,
421 const char *endpoint)
423 twr_empty_floor_t *smb_floor;
424 twr_empty_floor_t *nb_floor;
425 size_t size;
426 size_t networkaddr_size;
427 size_t endpoint_size;
429 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
431 networkaddr_size = strlen(networkaddr) + 1;
432 endpoint_size = strlen(endpoint) + 1;
433 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
435 if (!tower_data)
436 return size;
438 smb_floor = (twr_empty_floor_t *)tower_data;
440 tower_data += sizeof(*smb_floor);
442 smb_floor->count_lhs = sizeof(smb_floor->protid);
443 smb_floor->protid = EPM_PROTOCOL_SMB;
444 smb_floor->count_rhs = endpoint_size;
446 memcpy(tower_data, endpoint, endpoint_size);
447 tower_data += endpoint_size;
449 nb_floor = (twr_empty_floor_t *)tower_data;
451 tower_data += sizeof(*nb_floor);
453 nb_floor->count_lhs = sizeof(nb_floor->protid);
454 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
455 nb_floor->count_rhs = networkaddr_size;
457 memcpy(tower_data, networkaddr, networkaddr_size);
458 tower_data += networkaddr_size;
460 return size;
463 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
464 size_t tower_size,
465 char **networkaddr,
466 char **endpoint)
468 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
469 const twr_empty_floor_t *nb_floor;
471 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
473 if (tower_size < sizeof(*smb_floor))
474 return EPT_S_NOT_REGISTERED;
476 tower_data += sizeof(*smb_floor);
477 tower_size -= sizeof(*smb_floor);
479 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
480 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
481 (smb_floor->count_rhs > tower_size))
482 return EPT_S_NOT_REGISTERED;
484 if (endpoint)
486 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
487 if (!*endpoint)
488 return RPC_S_OUT_OF_RESOURCES;
489 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
491 tower_data += smb_floor->count_rhs;
492 tower_size -= smb_floor->count_rhs;
494 if (tower_size < sizeof(*nb_floor))
495 return EPT_S_NOT_REGISTERED;
497 nb_floor = (const twr_empty_floor_t *)tower_data;
499 tower_data += sizeof(*nb_floor);
500 tower_size -= sizeof(*nb_floor);
502 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
503 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
504 (nb_floor->count_rhs > tower_size))
505 return EPT_S_NOT_REGISTERED;
507 if (networkaddr)
509 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
510 if (!*networkaddr)
512 if (endpoint)
514 I_RpcFree(*endpoint);
515 *endpoint = NULL;
517 return RPC_S_OUT_OF_RESOURCES;
519 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
522 return RPC_S_OK;
525 typedef struct _RpcServerProtseq_np
527 RpcServerProtseq common;
528 HANDLE mgr_event;
529 } RpcServerProtseq_np;
531 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
533 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
534 if (ps)
535 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
536 return &ps->common;
539 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
541 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
542 SetEvent(npps->mgr_event);
545 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
547 HANDLE *objs = prev_array;
548 RpcConnection_np *conn;
549 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
551 EnterCriticalSection(&protseq->cs);
553 /* open and count connections */
554 *count = 1;
555 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
556 while (conn) {
557 rpcrt4_conn_listen_pipe(conn);
558 if (conn->ovl.hEvent)
559 (*count)++;
560 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
563 /* make array of connections */
564 if (objs)
565 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
566 else
567 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
568 if (!objs)
570 ERR("couldn't allocate objs\n");
571 LeaveCriticalSection(&protseq->cs);
572 return NULL;
575 objs[0] = npps->mgr_event;
576 *count = 1;
577 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
578 while (conn) {
579 if ((objs[*count] = conn->ovl.hEvent))
580 (*count)++;
581 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
583 LeaveCriticalSection(&protseq->cs);
584 return objs;
587 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
589 HeapFree(GetProcessHeap(), 0, array);
592 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
594 HANDLE b_handle;
595 HANDLE *objs = wait_array;
596 DWORD res;
597 RpcConnection *cconn;
598 RpcConnection_np *conn;
600 if (!objs)
601 return -1;
603 res = WaitForMultipleObjects(count, objs, FALSE, INFINITE);
604 if (res == WAIT_OBJECT_0)
605 return 0;
606 else if (res == WAIT_FAILED)
608 ERR("wait failed with error %d\n", GetLastError());
609 return -1;
611 else
613 b_handle = objs[res - WAIT_OBJECT_0];
614 /* find which connection got a RPC */
615 EnterCriticalSection(&protseq->cs);
616 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
617 while (conn) {
618 if (b_handle == conn->ovl.hEvent) break;
619 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
621 cconn = NULL;
622 if (conn)
623 RPCRT4_SpawnConnection(&cconn, &conn->common);
624 else
625 ERR("failed to locate connection for handle %p\n", b_handle);
626 LeaveCriticalSection(&protseq->cs);
627 if (cconn)
629 RPCRT4_new_client(cconn);
630 return 1;
632 else return -1;
636 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
637 const char *networkaddr,
638 const char *endpoint)
640 twr_empty_floor_t *pipe_floor;
641 size_t size;
642 size_t endpoint_size;
644 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
646 endpoint_size = strlen(networkaddr) + 1;
647 size = sizeof(*pipe_floor) + endpoint_size;
649 if (!tower_data)
650 return size;
652 pipe_floor = (twr_empty_floor_t *)tower_data;
654 tower_data += sizeof(*pipe_floor);
656 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
657 pipe_floor->protid = EPM_PROTOCOL_SMB;
658 pipe_floor->count_rhs = endpoint_size;
660 memcpy(tower_data, endpoint, endpoint_size);
661 tower_data += endpoint_size;
663 return size;
666 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
667 size_t tower_size,
668 char **networkaddr,
669 char **endpoint)
671 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
673 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
675 *networkaddr = NULL;
676 *endpoint = NULL;
678 if (tower_size < sizeof(*pipe_floor))
679 return EPT_S_NOT_REGISTERED;
681 tower_data += sizeof(*pipe_floor);
682 tower_size -= sizeof(*pipe_floor);
684 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
685 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
686 (pipe_floor->count_rhs > tower_size))
687 return EPT_S_NOT_REGISTERED;
689 if (endpoint)
691 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
692 if (!*endpoint)
693 return RPC_S_OUT_OF_RESOURCES;
694 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
697 return RPC_S_OK;
700 /**** ncacn_ip_tcp support ****/
702 typedef struct _RpcConnection_tcp
704 RpcConnection common;
705 int sock;
706 } RpcConnection_tcp;
708 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
710 RpcConnection_tcp *tcpc;
711 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
712 if (tcpc == NULL)
713 return NULL;
714 tcpc->sock = -1;
715 return &tcpc->common;
718 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
720 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
721 int sock;
722 int ret;
723 struct addrinfo *ai;
724 struct addrinfo *ai_cur;
725 struct addrinfo hints;
727 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
729 if (tcpc->sock != -1)
730 return RPC_S_OK;
732 hints.ai_flags = 0;
733 hints.ai_family = PF_UNSPEC;
734 hints.ai_socktype = SOCK_STREAM;
735 hints.ai_protocol = IPPROTO_TCP;
736 hints.ai_addrlen = 0;
737 hints.ai_addr = NULL;
738 hints.ai_canonname = NULL;
739 hints.ai_next = NULL;
741 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
742 if (ret)
744 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
745 Connection->Endpoint, gai_strerror(ret));
746 return RPC_S_SERVER_UNAVAILABLE;
749 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
751 int val;
753 if (TRACE_ON(rpc))
755 char host[256];
756 char service[256];
757 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
758 host, sizeof(host), service, sizeof(service),
759 NI_NUMERICHOST | NI_NUMERICSERV);
760 TRACE("trying %s:%s\n", host, service);
763 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
764 if (sock < 0)
766 WARN("socket() failed: %s\n", strerror(errno));
767 continue;
770 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
772 WARN("connect() failed: %s\n", strerror(errno));
773 close(sock);
774 continue;
777 /* RPC depends on having minimal latency so disable the Nagle algorithm */
778 val = 1;
779 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
781 tcpc->sock = sock;
783 freeaddrinfo(ai);
784 TRACE("connected\n");
785 return RPC_S_OK;
788 freeaddrinfo(ai);
789 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
790 return RPC_S_SERVER_UNAVAILABLE;
793 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
795 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
796 int sock;
797 int ret;
798 struct addrinfo *ai;
799 struct addrinfo *ai_cur;
800 struct addrinfo hints;
801 RpcConnection *first_connection = NULL;
803 TRACE("(%p, %s)\n", protseq, endpoint);
805 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
806 hints.ai_family = PF_UNSPEC;
807 hints.ai_socktype = SOCK_STREAM;
808 hints.ai_protocol = IPPROTO_TCP;
809 hints.ai_addrlen = 0;
810 hints.ai_addr = NULL;
811 hints.ai_canonname = NULL;
812 hints.ai_next = NULL;
814 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
815 if (ret)
817 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
818 gai_strerror(ret));
819 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
820 return RPC_S_INVALID_ENDPOINT_FORMAT;
821 return RPC_S_CANT_CREATE_ENDPOINT;
824 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
826 RpcConnection_tcp *tcpc;
827 RPC_STATUS create_status;
829 if (TRACE_ON(rpc))
831 char host[256];
832 char service[256];
833 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
834 host, sizeof(host), service, sizeof(service),
835 NI_NUMERICHOST | NI_NUMERICSERV);
836 TRACE("trying %s:%s\n", host, service);
839 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
840 if (sock < 0)
842 WARN("socket() failed: %s\n", strerror(errno));
843 status = RPC_S_CANT_CREATE_ENDPOINT;
844 continue;
847 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
848 if (ret < 0)
850 WARN("bind failed: %s\n", strerror(errno));
851 close(sock);
852 if (errno == EADDRINUSE)
853 status = RPC_S_DUPLICATE_ENDPOINT;
854 else
855 status = RPC_S_CANT_CREATE_ENDPOINT;
856 continue;
858 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
859 protseq->Protseq, NULL,
860 endpoint, NULL, NULL, NULL,
861 NULL);
862 if (create_status != RPC_S_OK)
864 close(sock);
865 status = create_status;
866 continue;
869 tcpc->sock = sock;
870 ret = listen(sock, protseq->MaxCalls);
871 if (ret < 0)
873 WARN("listen failed: %s\n", strerror(errno));
874 RPCRT4_DestroyConnection(&tcpc->common);
875 status = RPC_S_OUT_OF_RESOURCES;
876 continue;
878 /* need a non-blocking socket, otherwise accept() has a potential
879 * race-condition (poll() says it is readable, connection drops,
880 * and accept() blocks until the next connection comes...)
882 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
883 if (ret < 0)
885 WARN("couldn't make socket non-blocking, error %d\n", ret);
886 RPCRT4_DestroyConnection(&tcpc->common);
887 status = RPC_S_OUT_OF_RESOURCES;
888 continue;
891 tcpc->common.Next = first_connection;
892 first_connection = &tcpc->common;
895 freeaddrinfo(ai);
897 /* if at least one connection was created for an endpoint then
898 * return success */
899 if (first_connection)
901 RpcConnection *conn;
903 /* find last element in list */
904 for (conn = first_connection; conn->Next; conn = conn->Next)
907 EnterCriticalSection(&protseq->cs);
908 conn->Next = protseq->conn;
909 protseq->conn = first_connection;
910 LeaveCriticalSection(&protseq->cs);
912 TRACE("listening on %s\n", endpoint);
913 return RPC_S_OK;
916 ERR("couldn't listen on port %s\n", endpoint);
917 return status;
920 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
922 int ret;
923 struct sockaddr_in address;
924 socklen_t addrsize;
925 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
926 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
928 addrsize = sizeof(address);
929 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
930 if (ret < 0)
932 ERR("Failed to accept a TCP connection: error %d\n", ret);
933 return RPC_S_OUT_OF_RESOURCES;
935 /* reset to blocking behaviour */
936 fcntl(ret, F_SETFL, 0);
937 client->sock = ret;
938 TRACE("Accepted a new TCP connection\n");
939 return RPC_S_OK;
942 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
943 void *buffer, unsigned int count)
945 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
946 int r = recv(tcpc->sock, buffer, count, MSG_WAITALL);
947 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, r);
948 return r;
951 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
952 const void *buffer, unsigned int count)
954 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
955 int r = write(tcpc->sock, buffer, count);
956 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, r);
957 return r;
960 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
962 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
964 TRACE("%d\n", tcpc->sock);
966 if (tcpc->sock != -1)
967 close(tcpc->sock);
968 tcpc->sock = -1;
969 return 0;
972 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
973 const char *networkaddr,
974 const char *endpoint)
976 twr_tcp_floor_t *tcp_floor;
977 twr_ipv4_floor_t *ipv4_floor;
978 struct addrinfo *ai;
979 struct addrinfo hints;
980 int ret;
981 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
983 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
985 if (!tower_data)
986 return size;
988 tcp_floor = (twr_tcp_floor_t *)tower_data;
989 tower_data += sizeof(*tcp_floor);
991 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
993 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
994 tcp_floor->protid = EPM_PROTOCOL_TCP;
995 tcp_floor->count_rhs = sizeof(tcp_floor->port);
997 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
998 ipv4_floor->protid = EPM_PROTOCOL_IP;
999 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1001 hints.ai_flags = AI_NUMERICHOST;
1002 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1003 hints.ai_family = PF_INET;
1004 hints.ai_socktype = SOCK_STREAM;
1005 hints.ai_protocol = IPPROTO_TCP;
1006 hints.ai_addrlen = 0;
1007 hints.ai_addr = NULL;
1008 hints.ai_canonname = NULL;
1009 hints.ai_next = NULL;
1011 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1012 if (ret)
1014 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1015 if (ret)
1017 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1018 return 0;
1022 if (ai->ai_family == PF_INET)
1024 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1025 tcp_floor->port = sin->sin_port;
1026 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1028 else
1030 ERR("unexpected protocol family %d\n", ai->ai_family);
1031 return 0;
1034 freeaddrinfo(ai);
1036 return size;
1039 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1040 size_t tower_size,
1041 char **networkaddr,
1042 char **endpoint)
1044 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1045 const twr_ipv4_floor_t *ipv4_floor;
1046 struct in_addr in_addr;
1048 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1050 if (tower_size < sizeof(*tcp_floor))
1051 return EPT_S_NOT_REGISTERED;
1053 tower_data += sizeof(*tcp_floor);
1054 tower_size -= sizeof(*tcp_floor);
1056 if (tower_size < sizeof(*ipv4_floor))
1057 return EPT_S_NOT_REGISTERED;
1059 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1061 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1062 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1063 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1064 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1065 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1066 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1067 return EPT_S_NOT_REGISTERED;
1069 if (endpoint)
1071 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1072 if (!*endpoint)
1073 return RPC_S_OUT_OF_RESOURCES;
1074 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1077 if (networkaddr)
1079 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1080 if (!*networkaddr)
1082 if (endpoint)
1084 I_RpcFree(*endpoint);
1085 *endpoint = NULL;
1087 return RPC_S_OUT_OF_RESOURCES;
1089 in_addr.s_addr = ipv4_floor->ipv4addr;
1090 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1092 ERR("inet_ntop: %s\n", strerror(errno));
1093 I_RpcFree(*networkaddr);
1094 *networkaddr = NULL;
1095 if (endpoint)
1097 I_RpcFree(*endpoint);
1098 *endpoint = NULL;
1100 return EPT_S_NOT_REGISTERED;
1104 return RPC_S_OK;
1107 typedef struct _RpcServerProtseq_sock
1109 RpcServerProtseq common;
1110 int mgr_event_rcv;
1111 int mgr_event_snd;
1112 } RpcServerProtseq_sock;
1114 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1116 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1117 if (ps)
1119 int fds[2];
1120 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1122 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1123 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1124 ps->mgr_event_rcv = fds[0];
1125 ps->mgr_event_snd = fds[1];
1127 else
1129 ERR("socketpair failed with error %s\n", strerror(errno));
1130 HeapFree(GetProcessHeap(), 0, ps);
1131 return NULL;
1134 return &ps->common;
1137 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1139 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1140 char dummy = 1;
1141 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1144 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1146 struct pollfd *poll_info = prev_array;
1147 RpcConnection_tcp *conn;
1148 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1150 EnterCriticalSection(&protseq->cs);
1152 /* open and count connections */
1153 *count = 1;
1154 conn = (RpcConnection_tcp *)protseq->conn;
1155 while (conn) {
1156 if (conn->sock != -1)
1157 (*count)++;
1158 conn = (RpcConnection_tcp *)conn->common.Next;
1161 /* make array of connections */
1162 if (poll_info)
1163 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1164 else
1165 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1166 if (!poll_info)
1168 ERR("couldn't allocate poll_info\n");
1169 LeaveCriticalSection(&protseq->cs);
1170 return NULL;
1173 poll_info[0].fd = sockps->mgr_event_rcv;
1174 poll_info[0].events = POLLIN;
1175 *count = 1;
1176 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1177 while (conn) {
1178 if (conn->sock != -1)
1180 poll_info[*count].fd = conn->sock;
1181 poll_info[*count].events = POLLIN;
1182 (*count)++;
1184 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1186 LeaveCriticalSection(&protseq->cs);
1187 return poll_info;
1190 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1192 HeapFree(GetProcessHeap(), 0, array);
1195 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1197 struct pollfd *poll_info = wait_array;
1198 int ret, i;
1199 RpcConnection *cconn;
1200 RpcConnection_tcp *conn;
1202 if (!poll_info)
1203 return -1;
1205 ret = poll(poll_info, count, -1);
1206 if (ret < 0)
1208 ERR("poll failed with error %d\n", ret);
1209 return -1;
1212 for (i = 0; i < count; i++)
1213 if (poll_info[i].revents & POLLIN)
1215 /* RPC server event */
1216 if (i == 0)
1218 char dummy;
1219 read(poll_info[0].fd, &dummy, sizeof(dummy));
1220 return 0;
1223 /* find which connection got a RPC */
1224 EnterCriticalSection(&protseq->cs);
1225 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1226 while (conn) {
1227 if (poll_info[i].fd == conn->sock) break;
1228 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1230 cconn = NULL;
1231 if (conn)
1232 RPCRT4_SpawnConnection(&cconn, &conn->common);
1233 else
1234 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1235 LeaveCriticalSection(&protseq->cs);
1236 if (cconn)
1237 RPCRT4_new_client(cconn);
1238 else
1239 return -1;
1242 return 1;
1245 static const struct connection_ops conn_protseq_list[] = {
1246 { "ncacn_np",
1247 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1248 rpcrt4_conn_np_alloc,
1249 rpcrt4_ncacn_np_open,
1250 rpcrt4_ncacn_np_handoff,
1251 rpcrt4_conn_np_read,
1252 rpcrt4_conn_np_write,
1253 rpcrt4_conn_np_close,
1254 rpcrt4_ncacn_np_get_top_of_tower,
1255 rpcrt4_ncacn_np_parse_top_of_tower,
1257 { "ncalrpc",
1258 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1259 rpcrt4_conn_np_alloc,
1260 rpcrt4_ncalrpc_open,
1261 rpcrt4_ncalrpc_handoff,
1262 rpcrt4_conn_np_read,
1263 rpcrt4_conn_np_write,
1264 rpcrt4_conn_np_close,
1265 rpcrt4_ncalrpc_get_top_of_tower,
1266 rpcrt4_ncalrpc_parse_top_of_tower,
1268 { "ncacn_ip_tcp",
1269 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1270 rpcrt4_conn_tcp_alloc,
1271 rpcrt4_ncacn_ip_tcp_open,
1272 rpcrt4_conn_tcp_handoff,
1273 rpcrt4_conn_tcp_read,
1274 rpcrt4_conn_tcp_write,
1275 rpcrt4_conn_tcp_close,
1276 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1277 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1282 static const struct protseq_ops protseq_list[] =
1285 "ncacn_np",
1286 rpcrt4_protseq_np_alloc,
1287 rpcrt4_protseq_np_signal_state_changed,
1288 rpcrt4_protseq_np_get_wait_array,
1289 rpcrt4_protseq_np_free_wait_array,
1290 rpcrt4_protseq_np_wait_for_new_connection,
1291 rpcrt4_protseq_ncacn_np_open_endpoint,
1294 "ncalrpc",
1295 rpcrt4_protseq_np_alloc,
1296 rpcrt4_protseq_np_signal_state_changed,
1297 rpcrt4_protseq_np_get_wait_array,
1298 rpcrt4_protseq_np_free_wait_array,
1299 rpcrt4_protseq_np_wait_for_new_connection,
1300 rpcrt4_protseq_ncalrpc_open_endpoint,
1303 "ncacn_ip_tcp",
1304 rpcrt4_protseq_sock_alloc,
1305 rpcrt4_protseq_sock_signal_state_changed,
1306 rpcrt4_protseq_sock_get_wait_array,
1307 rpcrt4_protseq_sock_free_wait_array,
1308 rpcrt4_protseq_sock_wait_for_new_connection,
1309 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1313 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1315 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1317 int i;
1318 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1319 if (!strcmp(protseq_list[i].name, protseq))
1320 return &protseq_list[i];
1321 return NULL;
1324 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1326 int i;
1327 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1328 if (!strcmp(conn_protseq_list[i].name, protseq))
1329 return &conn_protseq_list[i];
1330 return NULL;
1333 /**** interface to rest of code ****/
1335 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1337 TRACE("(Connection == ^%p)\n", Connection);
1339 assert(!Connection->server);
1340 return Connection->ops->open_connection_client(Connection);
1343 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1345 TRACE("(Connection == ^%p)\n", Connection);
1346 if (SecIsValidHandle(&Connection->ctx))
1348 DeleteSecurityContext(&Connection->ctx);
1349 SecInvalidateHandle(&Connection->ctx);
1351 rpcrt4_conn_close(Connection);
1352 return RPC_S_OK;
1355 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1356 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1357 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS,
1358 RpcBinding* Binding)
1360 const struct connection_ops *ops;
1361 RpcConnection* NewConnection;
1363 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1364 if (!ops)
1366 FIXME("not supported for protseq %s\n", Protseq);
1367 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1370 NewConnection = ops->alloc();
1371 NewConnection->Next = NULL;
1372 NewConnection->server = server;
1373 NewConnection->ops = ops;
1374 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1375 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1376 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1377 NewConnection->Used = Binding;
1378 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1379 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1380 NewConnection->NextCallId = 1;
1382 SecInvalidateHandle(&NewConnection->ctx);
1383 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1384 NewConnection->attr = 0;
1385 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1386 NewConnection->AuthInfo = AuthInfo;
1387 NewConnection->encryption_auth_len = 0;
1388 NewConnection->signature_auth_len = 0;
1389 if (QOS) RpcQualityOfService_AddRef(QOS);
1390 NewConnection->QOS = QOS;
1392 list_init(&NewConnection->conn_pool_entry);
1394 TRACE("connection: %p\n", NewConnection);
1395 *Connection = NewConnection;
1397 return RPC_S_OK;
1400 RPC_STATUS RPCRT4_GetAssociation(LPCSTR Protseq, LPCSTR NetworkAddr,
1401 LPCSTR Endpoint, LPCWSTR NetworkOptions,
1402 RpcAssoc **assoc_out)
1404 RpcAssoc *assoc;
1406 EnterCriticalSection(&assoc_list_cs);
1407 LIST_FOR_EACH_ENTRY(assoc, &assoc_list, RpcAssoc, entry)
1409 if (!strcmp(Protseq, assoc->Protseq) &&
1410 !strcmp(NetworkAddr, assoc->NetworkAddr) &&
1411 !strcmp(Endpoint, assoc->Endpoint) &&
1412 ((!assoc->NetworkOptions && !NetworkOptions) || !strcmpW(NetworkOptions, assoc->NetworkOptions)))
1414 assoc->refs++;
1415 *assoc_out = assoc;
1416 LeaveCriticalSection(&assoc_list_cs);
1417 TRACE("using existing assoc %p\n", assoc);
1418 return RPC_S_OK;
1422 assoc = HeapAlloc(GetProcessHeap(), 0, sizeof(*assoc));
1423 if (!assoc)
1425 LeaveCriticalSection(&assoc_list_cs);
1426 return RPC_S_OUT_OF_RESOURCES;
1428 assoc->refs = 1;
1429 list_init(&assoc->connection_pool);
1430 InitializeCriticalSection(&assoc->cs);
1431 assoc->Protseq = RPCRT4_strdupA(Protseq);
1432 assoc->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1433 assoc->Endpoint = RPCRT4_strdupA(Endpoint);
1434 assoc->NetworkOptions = NetworkOptions ? RPCRT4_strdupW(NetworkOptions) : NULL;
1435 list_add_head(&assoc_list, &assoc->entry);
1436 *assoc_out = assoc;
1438 LeaveCriticalSection(&assoc_list_cs);
1440 TRACE("new assoc %p\n", assoc);
1442 return RPC_S_OK;
1445 ULONG RpcAssoc_Release(RpcAssoc *assoc)
1447 ULONG refs;
1449 EnterCriticalSection(&assoc_list_cs);
1450 refs = --assoc->refs;
1451 if (!refs)
1452 list_remove(&assoc->entry);
1453 LeaveCriticalSection(&assoc_list_cs);
1455 if (!refs)
1457 RpcConnection *Connection, *cursor2;
1459 TRACE("destroying assoc %p\n", assoc);
1461 LIST_FOR_EACH_ENTRY_SAFE(Connection, cursor2, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1463 list_remove(&Connection->conn_pool_entry);
1464 RPCRT4_DestroyConnection(Connection);
1467 HeapFree(GetProcessHeap(), 0, assoc->NetworkOptions);
1468 HeapFree(GetProcessHeap(), 0, assoc->Endpoint);
1469 HeapFree(GetProcessHeap(), 0, assoc->NetworkAddr);
1470 HeapFree(GetProcessHeap(), 0, assoc->Protseq);
1472 HeapFree(GetProcessHeap(), 0, assoc);
1475 return refs;
1478 RpcConnection *RpcAssoc_GetIdleConnection(RpcAssoc *assoc,
1479 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1480 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, const RpcAuthInfo *AuthInfo,
1481 const RpcQualityOfService *QOS)
1483 RpcConnection *Connection;
1484 /* try to find a compatible connection from the connection pool */
1485 EnterCriticalSection(&assoc->cs);
1486 LIST_FOR_EACH_ENTRY(Connection, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1487 if ((Connection->AuthInfo == AuthInfo) &&
1488 (Connection->QOS == QOS) &&
1489 !memcmp(&Connection->ActiveInterface, InterfaceId,
1490 sizeof(RPC_SYNTAX_IDENTIFIER)))
1492 list_remove(&Connection->conn_pool_entry);
1493 LeaveCriticalSection(&assoc->cs);
1494 TRACE("got connection from pool %p\n", Connection);
1495 return Connection;
1498 LeaveCriticalSection(&assoc->cs);
1499 return NULL;
1502 void RpcAssoc_ReleaseIdleConnection(RpcAssoc *assoc, RpcConnection *Connection)
1504 assert(!Connection->server);
1505 EnterCriticalSection(&assoc->cs);
1506 list_add_head(&assoc->connection_pool, &Connection->conn_pool_entry);
1507 LeaveCriticalSection(&assoc->cs);
1511 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1513 RPC_STATUS err;
1515 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1516 rpcrt4_conn_get_name(OldConnection),
1517 OldConnection->NetworkAddr,
1518 OldConnection->Endpoint, NULL,
1519 OldConnection->AuthInfo, OldConnection->QOS,
1520 NULL);
1521 if (err == RPC_S_OK)
1522 rpcrt4_conn_handoff(OldConnection, *Connection);
1523 return err;
1526 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1528 TRACE("connection: %p\n", Connection);
1530 RPCRT4_CloseConnection(Connection);
1531 RPCRT4_strfree(Connection->Endpoint);
1532 RPCRT4_strfree(Connection->NetworkAddr);
1533 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1534 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1535 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1536 HeapFree(GetProcessHeap(), 0, Connection);
1537 return RPC_S_OK;
1540 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1541 size_t *tower_size,
1542 const char *protseq,
1543 const char *networkaddr,
1544 const char *endpoint)
1546 twr_empty_floor_t *protocol_floor;
1547 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1549 *tower_size = 0;
1551 if (!protseq_ops)
1552 return RPC_S_INVALID_RPC_PROTSEQ;
1554 if (!tower_data)
1556 *tower_size = sizeof(*protocol_floor);
1557 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1558 return RPC_S_OK;
1561 protocol_floor = (twr_empty_floor_t *)tower_data;
1562 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1563 protocol_floor->protid = protseq_ops->epm_protocols[0];
1564 protocol_floor->count_rhs = 0;
1566 tower_data += sizeof(*protocol_floor);
1568 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1569 if (!*tower_size)
1570 return EPT_S_NOT_REGISTERED;
1572 *tower_size += sizeof(*protocol_floor);
1574 return RPC_S_OK;
1577 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1578 size_t tower_size,
1579 char **protseq,
1580 char **networkaddr,
1581 char **endpoint)
1583 const twr_empty_floor_t *protocol_floor;
1584 const twr_empty_floor_t *floor4;
1585 const struct connection_ops *protseq_ops = NULL;
1586 RPC_STATUS status;
1587 int i;
1589 if (tower_size < sizeof(*protocol_floor))
1590 return EPT_S_NOT_REGISTERED;
1592 protocol_floor = (const twr_empty_floor_t *)tower_data;
1593 tower_data += sizeof(*protocol_floor);
1594 tower_size -= sizeof(*protocol_floor);
1595 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1596 (protocol_floor->count_rhs > tower_size))
1597 return EPT_S_NOT_REGISTERED;
1598 tower_data += protocol_floor->count_rhs;
1599 tower_size -= protocol_floor->count_rhs;
1601 floor4 = (const twr_empty_floor_t *)tower_data;
1602 if ((tower_size < sizeof(*floor4)) ||
1603 (floor4->count_lhs != sizeof(floor4->protid)))
1604 return EPT_S_NOT_REGISTERED;
1606 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1607 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1608 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1610 protseq_ops = &conn_protseq_list[i];
1611 break;
1614 if (!protseq_ops)
1615 return EPT_S_NOT_REGISTERED;
1617 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1619 if ((status == RPC_S_OK) && protseq)
1621 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1622 strcpy(*protseq, protseq_ops->name);
1625 return status;
1628 /***********************************************************************
1629 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1631 * Checks if the given protocol sequence is known by the RPC system.
1632 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1635 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1637 char ps[0x10];
1639 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1640 ps, sizeof ps, NULL, NULL);
1641 if (rpcrt4_get_conn_protseq_ops(ps))
1642 return RPC_S_OK;
1644 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1646 return RPC_S_INVALID_RPC_PROTSEQ;
1649 /***********************************************************************
1650 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1652 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1654 UNICODE_STRING protseqW;
1656 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1658 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1659 RtlFreeUnicodeString(&protseqW);
1660 return ret;
1662 return RPC_S_OUT_OF_MEMORY;