rpcrt4: Fix a typo in rpcrt4_conn_tcp_read.
[wine/multimedia.git] / dlls / rpcrt4 / rpc_transport.c
blobe4d12c334650ed9be8513639b78e87d7e13da72a
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);
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);
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 void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
421 /* FIXME: implement when named pipe writes use overlapped I/O */
424 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
425 const char *networkaddr,
426 const char *endpoint)
428 twr_empty_floor_t *smb_floor;
429 twr_empty_floor_t *nb_floor;
430 size_t size;
431 size_t networkaddr_size;
432 size_t endpoint_size;
434 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
436 networkaddr_size = strlen(networkaddr) + 1;
437 endpoint_size = strlen(endpoint) + 1;
438 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
440 if (!tower_data)
441 return size;
443 smb_floor = (twr_empty_floor_t *)tower_data;
445 tower_data += sizeof(*smb_floor);
447 smb_floor->count_lhs = sizeof(smb_floor->protid);
448 smb_floor->protid = EPM_PROTOCOL_SMB;
449 smb_floor->count_rhs = endpoint_size;
451 memcpy(tower_data, endpoint, endpoint_size);
452 tower_data += endpoint_size;
454 nb_floor = (twr_empty_floor_t *)tower_data;
456 tower_data += sizeof(*nb_floor);
458 nb_floor->count_lhs = sizeof(nb_floor->protid);
459 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
460 nb_floor->count_rhs = networkaddr_size;
462 memcpy(tower_data, networkaddr, networkaddr_size);
463 tower_data += networkaddr_size;
465 return size;
468 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
469 size_t tower_size,
470 char **networkaddr,
471 char **endpoint)
473 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
474 const twr_empty_floor_t *nb_floor;
476 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
478 if (tower_size < sizeof(*smb_floor))
479 return EPT_S_NOT_REGISTERED;
481 tower_data += sizeof(*smb_floor);
482 tower_size -= sizeof(*smb_floor);
484 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
485 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
486 (smb_floor->count_rhs > tower_size))
487 return EPT_S_NOT_REGISTERED;
489 if (endpoint)
491 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
492 if (!*endpoint)
493 return RPC_S_OUT_OF_RESOURCES;
494 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
496 tower_data += smb_floor->count_rhs;
497 tower_size -= smb_floor->count_rhs;
499 if (tower_size < sizeof(*nb_floor))
500 return EPT_S_NOT_REGISTERED;
502 nb_floor = (const twr_empty_floor_t *)tower_data;
504 tower_data += sizeof(*nb_floor);
505 tower_size -= sizeof(*nb_floor);
507 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
508 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
509 (nb_floor->count_rhs > tower_size))
510 return EPT_S_NOT_REGISTERED;
512 if (networkaddr)
514 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
515 if (!*networkaddr)
517 if (endpoint)
519 I_RpcFree(*endpoint);
520 *endpoint = NULL;
522 return RPC_S_OUT_OF_RESOURCES;
524 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
527 return RPC_S_OK;
530 typedef struct _RpcServerProtseq_np
532 RpcServerProtseq common;
533 HANDLE mgr_event;
534 } RpcServerProtseq_np;
536 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
538 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
539 if (ps)
540 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
541 return &ps->common;
544 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
546 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
547 SetEvent(npps->mgr_event);
550 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
552 HANDLE *objs = prev_array;
553 RpcConnection_np *conn;
554 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
556 EnterCriticalSection(&protseq->cs);
558 /* open and count connections */
559 *count = 1;
560 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
561 while (conn) {
562 rpcrt4_conn_listen_pipe(conn);
563 if (conn->ovl.hEvent)
564 (*count)++;
565 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
568 /* make array of connections */
569 if (objs)
570 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
571 else
572 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
573 if (!objs)
575 ERR("couldn't allocate objs\n");
576 LeaveCriticalSection(&protseq->cs);
577 return NULL;
580 objs[0] = npps->mgr_event;
581 *count = 1;
582 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
583 while (conn) {
584 if ((objs[*count] = conn->ovl.hEvent))
585 (*count)++;
586 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
588 LeaveCriticalSection(&protseq->cs);
589 return objs;
592 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
594 HeapFree(GetProcessHeap(), 0, array);
597 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
599 HANDLE b_handle;
600 HANDLE *objs = wait_array;
601 DWORD res;
602 RpcConnection *cconn;
603 RpcConnection_np *conn;
605 if (!objs)
606 return -1;
610 /* an alertable wait isn't strictly necessary, but due to our
611 * overlapped I/O implementation in Wine we need to free some memory
612 * by the file user APC being called, even if no completion routine was
613 * specified at the time of starting the async operation */
614 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
615 } while (res == WAIT_IO_COMPLETION);
617 if (res == WAIT_OBJECT_0)
618 return 0;
619 else if (res == WAIT_FAILED)
621 ERR("wait failed with error %d\n", GetLastError());
622 return -1;
624 else
626 b_handle = objs[res - WAIT_OBJECT_0];
627 /* find which connection got a RPC */
628 EnterCriticalSection(&protseq->cs);
629 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
630 while (conn) {
631 if (b_handle == conn->ovl.hEvent) break;
632 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
634 cconn = NULL;
635 if (conn)
636 RPCRT4_SpawnConnection(&cconn, &conn->common);
637 else
638 ERR("failed to locate connection for handle %p\n", b_handle);
639 LeaveCriticalSection(&protseq->cs);
640 if (cconn)
642 RPCRT4_new_client(cconn);
643 return 1;
645 else return -1;
649 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
650 const char *networkaddr,
651 const char *endpoint)
653 twr_empty_floor_t *pipe_floor;
654 size_t size;
655 size_t endpoint_size;
657 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
659 endpoint_size = strlen(networkaddr) + 1;
660 size = sizeof(*pipe_floor) + endpoint_size;
662 if (!tower_data)
663 return size;
665 pipe_floor = (twr_empty_floor_t *)tower_data;
667 tower_data += sizeof(*pipe_floor);
669 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
670 pipe_floor->protid = EPM_PROTOCOL_SMB;
671 pipe_floor->count_rhs = endpoint_size;
673 memcpy(tower_data, endpoint, endpoint_size);
674 tower_data += endpoint_size;
676 return size;
679 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
680 size_t tower_size,
681 char **networkaddr,
682 char **endpoint)
684 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
686 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
688 *networkaddr = NULL;
689 *endpoint = NULL;
691 if (tower_size < sizeof(*pipe_floor))
692 return EPT_S_NOT_REGISTERED;
694 tower_data += sizeof(*pipe_floor);
695 tower_size -= sizeof(*pipe_floor);
697 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
698 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
699 (pipe_floor->count_rhs > tower_size))
700 return EPT_S_NOT_REGISTERED;
702 if (endpoint)
704 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
705 if (!*endpoint)
706 return RPC_S_OUT_OF_RESOURCES;
707 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
710 return RPC_S_OK;
713 /**** ncacn_ip_tcp support ****/
715 typedef struct _RpcConnection_tcp
717 RpcConnection common;
718 int sock;
719 int cancel_fds[2];
720 } RpcConnection_tcp;
722 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
724 RpcConnection_tcp *tcpc;
725 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
726 if (tcpc == NULL)
727 return NULL;
728 tcpc->sock = -1;
729 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
731 ERR("socketpair() failed: %s\n", strerror(errno));
732 HeapFree(GetProcessHeap(), 0, tcpc);
733 return NULL;
735 return &tcpc->common;
738 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
740 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
741 int sock;
742 int ret;
743 struct addrinfo *ai;
744 struct addrinfo *ai_cur;
745 struct addrinfo hints;
747 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
749 if (tcpc->sock != -1)
750 return RPC_S_OK;
752 hints.ai_flags = 0;
753 hints.ai_family = PF_UNSPEC;
754 hints.ai_socktype = SOCK_STREAM;
755 hints.ai_protocol = IPPROTO_TCP;
756 hints.ai_addrlen = 0;
757 hints.ai_addr = NULL;
758 hints.ai_canonname = NULL;
759 hints.ai_next = NULL;
761 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
762 if (ret)
764 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
765 Connection->Endpoint, gai_strerror(ret));
766 return RPC_S_SERVER_UNAVAILABLE;
769 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
771 int val;
773 if (TRACE_ON(rpc))
775 char host[256];
776 char service[256];
777 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
778 host, sizeof(host), service, sizeof(service),
779 NI_NUMERICHOST | NI_NUMERICSERV);
780 TRACE("trying %s:%s\n", host, service);
783 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
784 if (sock == -1)
786 WARN("socket() failed: %s\n", strerror(errno));
787 continue;
790 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
792 WARN("connect() failed: %s\n", strerror(errno));
793 close(sock);
794 continue;
797 /* RPC depends on having minimal latency so disable the Nagle algorithm */
798 val = 1;
799 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
800 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
802 tcpc->sock = sock;
804 freeaddrinfo(ai);
805 TRACE("connected\n");
806 return RPC_S_OK;
809 freeaddrinfo(ai);
810 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
811 return RPC_S_SERVER_UNAVAILABLE;
814 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
816 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
817 int sock;
818 int ret;
819 struct addrinfo *ai;
820 struct addrinfo *ai_cur;
821 struct addrinfo hints;
822 RpcConnection *first_connection = NULL;
824 TRACE("(%p, %s)\n", protseq, endpoint);
826 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
827 hints.ai_family = PF_UNSPEC;
828 hints.ai_socktype = SOCK_STREAM;
829 hints.ai_protocol = IPPROTO_TCP;
830 hints.ai_addrlen = 0;
831 hints.ai_addr = NULL;
832 hints.ai_canonname = NULL;
833 hints.ai_next = NULL;
835 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
836 if (ret)
838 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
839 gai_strerror(ret));
840 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
841 return RPC_S_INVALID_ENDPOINT_FORMAT;
842 return RPC_S_CANT_CREATE_ENDPOINT;
845 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
847 RpcConnection_tcp *tcpc;
848 RPC_STATUS create_status;
850 if (TRACE_ON(rpc))
852 char host[256];
853 char service[256];
854 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
855 host, sizeof(host), service, sizeof(service),
856 NI_NUMERICHOST | NI_NUMERICSERV);
857 TRACE("trying %s:%s\n", host, service);
860 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
861 if (sock == -1)
863 WARN("socket() failed: %s\n", strerror(errno));
864 status = RPC_S_CANT_CREATE_ENDPOINT;
865 continue;
868 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
869 if (ret < 0)
871 WARN("bind failed: %s\n", strerror(errno));
872 close(sock);
873 if (errno == EADDRINUSE)
874 status = RPC_S_DUPLICATE_ENDPOINT;
875 else
876 status = RPC_S_CANT_CREATE_ENDPOINT;
877 continue;
879 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
880 protseq->Protseq, NULL,
881 endpoint, NULL, NULL, NULL);
882 if (create_status != RPC_S_OK)
884 close(sock);
885 status = create_status;
886 continue;
889 tcpc->sock = sock;
890 ret = listen(sock, protseq->MaxCalls);
891 if (ret < 0)
893 WARN("listen failed: %s\n", strerror(errno));
894 RPCRT4_DestroyConnection(&tcpc->common);
895 status = RPC_S_OUT_OF_RESOURCES;
896 continue;
898 /* need a non-blocking socket, otherwise accept() has a potential
899 * race-condition (poll() says it is readable, connection drops,
900 * and accept() blocks until the next connection comes...)
902 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
903 if (ret < 0)
905 WARN("couldn't make socket non-blocking, error %d\n", ret);
906 RPCRT4_DestroyConnection(&tcpc->common);
907 status = RPC_S_OUT_OF_RESOURCES;
908 continue;
911 tcpc->common.Next = first_connection;
912 first_connection = &tcpc->common;
915 freeaddrinfo(ai);
917 /* if at least one connection was created for an endpoint then
918 * return success */
919 if (first_connection)
921 RpcConnection *conn;
923 /* find last element in list */
924 for (conn = first_connection; conn->Next; conn = conn->Next)
927 EnterCriticalSection(&protseq->cs);
928 conn->Next = protseq->conn;
929 protseq->conn = first_connection;
930 LeaveCriticalSection(&protseq->cs);
932 TRACE("listening on %s\n", endpoint);
933 return RPC_S_OK;
936 ERR("couldn't listen on port %s\n", endpoint);
937 return status;
940 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
942 int ret;
943 struct sockaddr_in address;
944 socklen_t addrsize;
945 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
946 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
948 addrsize = sizeof(address);
949 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
950 if (ret < 0)
952 ERR("Failed to accept a TCP connection: error %d\n", ret);
953 return RPC_S_OUT_OF_RESOURCES;
955 /* reset to blocking behaviour */
956 fcntl(ret, F_SETFL, 0);
957 client->sock = ret;
958 TRACE("Accepted a new TCP connection\n");
959 return RPC_S_OK;
962 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
963 void *buffer, unsigned int count)
965 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
966 int bytes_read = 0;
969 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
970 if (r >= 0)
971 bytes_read += r;
972 else if (errno != EAGAIN)
973 return -1;
974 else
976 struct pollfd pfds[2];
977 pfds[0].fd = tcpc->sock;
978 pfds[0].events = POLLIN;
979 pfds[1].fd = tcpc->cancel_fds[0];
980 pfds[1].events = POLLIN;
981 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
983 ERR("poll() failed: %s\n", strerror(errno));
984 return -1;
986 if (pfds[1].revents & POLLIN) /* canceled */
988 char dummy;
989 read(pfds[1].fd, &dummy, sizeof(dummy));
990 return -1;
993 } while (bytes_read != count);
994 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
995 return bytes_read;
998 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
999 const void *buffer, unsigned int count)
1001 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1002 int bytes_written = 0;
1005 int r = write(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written);
1006 if (r >= 0)
1007 bytes_written += r;
1008 else if (errno != EAGAIN)
1009 return -1;
1010 else
1012 struct pollfd pfd;
1013 pfd.fd = tcpc->sock;
1014 pfd.events = POLLOUT;
1015 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1017 ERR("poll() failed: %s\n", strerror(errno));
1018 return -1;
1021 } while (bytes_written != count);
1022 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1023 return bytes_written;
1026 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1028 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1030 TRACE("%d\n", tcpc->sock);
1032 if (tcpc->sock != -1)
1033 close(tcpc->sock);
1034 tcpc->sock = -1;
1035 close(tcpc->cancel_fds[0]);
1036 close(tcpc->cancel_fds[1]);
1037 return 0;
1040 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1042 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1043 char dummy = 1;
1045 TRACE("%p\n", Connection);
1047 write(tcpc->cancel_fds[1], &dummy, 1);
1050 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1051 const char *networkaddr,
1052 const char *endpoint)
1054 twr_tcp_floor_t *tcp_floor;
1055 twr_ipv4_floor_t *ipv4_floor;
1056 struct addrinfo *ai;
1057 struct addrinfo hints;
1058 int ret;
1059 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1061 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1063 if (!tower_data)
1064 return size;
1066 tcp_floor = (twr_tcp_floor_t *)tower_data;
1067 tower_data += sizeof(*tcp_floor);
1069 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1071 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1072 tcp_floor->protid = EPM_PROTOCOL_TCP;
1073 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1075 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1076 ipv4_floor->protid = EPM_PROTOCOL_IP;
1077 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1079 hints.ai_flags = AI_NUMERICHOST;
1080 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1081 hints.ai_family = PF_INET;
1082 hints.ai_socktype = SOCK_STREAM;
1083 hints.ai_protocol = IPPROTO_TCP;
1084 hints.ai_addrlen = 0;
1085 hints.ai_addr = NULL;
1086 hints.ai_canonname = NULL;
1087 hints.ai_next = NULL;
1089 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1090 if (ret)
1092 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1093 if (ret)
1095 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1096 return 0;
1100 if (ai->ai_family == PF_INET)
1102 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1103 tcp_floor->port = sin->sin_port;
1104 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1106 else
1108 ERR("unexpected protocol family %d\n", ai->ai_family);
1109 return 0;
1112 freeaddrinfo(ai);
1114 return size;
1117 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1118 size_t tower_size,
1119 char **networkaddr,
1120 char **endpoint)
1122 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1123 const twr_ipv4_floor_t *ipv4_floor;
1124 struct in_addr in_addr;
1126 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1128 if (tower_size < sizeof(*tcp_floor))
1129 return EPT_S_NOT_REGISTERED;
1131 tower_data += sizeof(*tcp_floor);
1132 tower_size -= sizeof(*tcp_floor);
1134 if (tower_size < sizeof(*ipv4_floor))
1135 return EPT_S_NOT_REGISTERED;
1137 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1139 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1140 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1141 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1142 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1143 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1144 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1145 return EPT_S_NOT_REGISTERED;
1147 if (endpoint)
1149 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1150 if (!*endpoint)
1151 return RPC_S_OUT_OF_RESOURCES;
1152 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1155 if (networkaddr)
1157 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1158 if (!*networkaddr)
1160 if (endpoint)
1162 I_RpcFree(*endpoint);
1163 *endpoint = NULL;
1165 return RPC_S_OUT_OF_RESOURCES;
1167 in_addr.s_addr = ipv4_floor->ipv4addr;
1168 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1170 ERR("inet_ntop: %s\n", strerror(errno));
1171 I_RpcFree(*networkaddr);
1172 *networkaddr = NULL;
1173 if (endpoint)
1175 I_RpcFree(*endpoint);
1176 *endpoint = NULL;
1178 return EPT_S_NOT_REGISTERED;
1182 return RPC_S_OK;
1185 typedef struct _RpcServerProtseq_sock
1187 RpcServerProtseq common;
1188 int mgr_event_rcv;
1189 int mgr_event_snd;
1190 } RpcServerProtseq_sock;
1192 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1194 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1195 if (ps)
1197 int fds[2];
1198 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1200 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1201 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1202 ps->mgr_event_rcv = fds[0];
1203 ps->mgr_event_snd = fds[1];
1205 else
1207 ERR("socketpair failed with error %s\n", strerror(errno));
1208 HeapFree(GetProcessHeap(), 0, ps);
1209 return NULL;
1212 return &ps->common;
1215 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1217 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1218 char dummy = 1;
1219 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1222 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1224 struct pollfd *poll_info = prev_array;
1225 RpcConnection_tcp *conn;
1226 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1228 EnterCriticalSection(&protseq->cs);
1230 /* open and count connections */
1231 *count = 1;
1232 conn = (RpcConnection_tcp *)protseq->conn;
1233 while (conn) {
1234 if (conn->sock != -1)
1235 (*count)++;
1236 conn = (RpcConnection_tcp *)conn->common.Next;
1239 /* make array of connections */
1240 if (poll_info)
1241 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1242 else
1243 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1244 if (!poll_info)
1246 ERR("couldn't allocate poll_info\n");
1247 LeaveCriticalSection(&protseq->cs);
1248 return NULL;
1251 poll_info[0].fd = sockps->mgr_event_rcv;
1252 poll_info[0].events = POLLIN;
1253 *count = 1;
1254 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1255 while (conn) {
1256 if (conn->sock != -1)
1258 poll_info[*count].fd = conn->sock;
1259 poll_info[*count].events = POLLIN;
1260 (*count)++;
1262 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1264 LeaveCriticalSection(&protseq->cs);
1265 return poll_info;
1268 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1270 HeapFree(GetProcessHeap(), 0, array);
1273 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1275 struct pollfd *poll_info = wait_array;
1276 int ret, i;
1277 RpcConnection *cconn;
1278 RpcConnection_tcp *conn;
1280 if (!poll_info)
1281 return -1;
1283 ret = poll(poll_info, count, -1);
1284 if (ret < 0)
1286 ERR("poll failed with error %d\n", ret);
1287 return -1;
1290 for (i = 0; i < count; i++)
1291 if (poll_info[i].revents & POLLIN)
1293 /* RPC server event */
1294 if (i == 0)
1296 char dummy;
1297 read(poll_info[0].fd, &dummy, sizeof(dummy));
1298 return 0;
1301 /* find which connection got a RPC */
1302 EnterCriticalSection(&protseq->cs);
1303 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1304 while (conn) {
1305 if (poll_info[i].fd == conn->sock) break;
1306 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1308 cconn = NULL;
1309 if (conn)
1310 RPCRT4_SpawnConnection(&cconn, &conn->common);
1311 else
1312 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1313 LeaveCriticalSection(&protseq->cs);
1314 if (cconn)
1315 RPCRT4_new_client(cconn);
1316 else
1317 return -1;
1320 return 1;
1323 static const struct connection_ops conn_protseq_list[] = {
1324 { "ncacn_np",
1325 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1326 rpcrt4_conn_np_alloc,
1327 rpcrt4_ncacn_np_open,
1328 rpcrt4_ncacn_np_handoff,
1329 rpcrt4_conn_np_read,
1330 rpcrt4_conn_np_write,
1331 rpcrt4_conn_np_close,
1332 rpcrt4_conn_np_cancel_call,
1333 rpcrt4_ncacn_np_get_top_of_tower,
1334 rpcrt4_ncacn_np_parse_top_of_tower,
1336 { "ncalrpc",
1337 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1338 rpcrt4_conn_np_alloc,
1339 rpcrt4_ncalrpc_open,
1340 rpcrt4_ncalrpc_handoff,
1341 rpcrt4_conn_np_read,
1342 rpcrt4_conn_np_write,
1343 rpcrt4_conn_np_close,
1344 rpcrt4_conn_np_cancel_call,
1345 rpcrt4_ncalrpc_get_top_of_tower,
1346 rpcrt4_ncalrpc_parse_top_of_tower,
1348 { "ncacn_ip_tcp",
1349 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1350 rpcrt4_conn_tcp_alloc,
1351 rpcrt4_ncacn_ip_tcp_open,
1352 rpcrt4_conn_tcp_handoff,
1353 rpcrt4_conn_tcp_read,
1354 rpcrt4_conn_tcp_write,
1355 rpcrt4_conn_tcp_close,
1356 rpcrt4_conn_tcp_cancel_call,
1357 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1358 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1363 static const struct protseq_ops protseq_list[] =
1366 "ncacn_np",
1367 rpcrt4_protseq_np_alloc,
1368 rpcrt4_protseq_np_signal_state_changed,
1369 rpcrt4_protseq_np_get_wait_array,
1370 rpcrt4_protseq_np_free_wait_array,
1371 rpcrt4_protseq_np_wait_for_new_connection,
1372 rpcrt4_protseq_ncacn_np_open_endpoint,
1375 "ncalrpc",
1376 rpcrt4_protseq_np_alloc,
1377 rpcrt4_protseq_np_signal_state_changed,
1378 rpcrt4_protseq_np_get_wait_array,
1379 rpcrt4_protseq_np_free_wait_array,
1380 rpcrt4_protseq_np_wait_for_new_connection,
1381 rpcrt4_protseq_ncalrpc_open_endpoint,
1384 "ncacn_ip_tcp",
1385 rpcrt4_protseq_sock_alloc,
1386 rpcrt4_protseq_sock_signal_state_changed,
1387 rpcrt4_protseq_sock_get_wait_array,
1388 rpcrt4_protseq_sock_free_wait_array,
1389 rpcrt4_protseq_sock_wait_for_new_connection,
1390 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1394 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1396 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1398 int i;
1399 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1400 if (!strcmp(protseq_list[i].name, protseq))
1401 return &protseq_list[i];
1402 return NULL;
1405 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1407 int i;
1408 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1409 if (!strcmp(conn_protseq_list[i].name, protseq))
1410 return &conn_protseq_list[i];
1411 return NULL;
1414 /**** interface to rest of code ****/
1416 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1418 TRACE("(Connection == ^%p)\n", Connection);
1420 assert(!Connection->server);
1421 return Connection->ops->open_connection_client(Connection);
1424 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1426 TRACE("(Connection == ^%p)\n", Connection);
1427 if (SecIsValidHandle(&Connection->ctx))
1429 DeleteSecurityContext(&Connection->ctx);
1430 SecInvalidateHandle(&Connection->ctx);
1432 rpcrt4_conn_close(Connection);
1433 return RPC_S_OK;
1436 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1437 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1438 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1440 const struct connection_ops *ops;
1441 RpcConnection* NewConnection;
1443 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1444 if (!ops)
1446 FIXME("not supported for protseq %s\n", Protseq);
1447 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1450 NewConnection = ops->alloc();
1451 NewConnection->Next = NULL;
1452 NewConnection->server = server;
1453 NewConnection->ops = ops;
1454 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1455 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1456 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1457 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1458 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1459 NewConnection->NextCallId = 1;
1461 SecInvalidateHandle(&NewConnection->ctx);
1462 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1463 NewConnection->attr = 0;
1464 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1465 NewConnection->AuthInfo = AuthInfo;
1466 NewConnection->encryption_auth_len = 0;
1467 NewConnection->signature_auth_len = 0;
1468 if (QOS) RpcQualityOfService_AddRef(QOS);
1469 NewConnection->QOS = QOS;
1471 list_init(&NewConnection->conn_pool_entry);
1473 TRACE("connection: %p\n", NewConnection);
1474 *Connection = NewConnection;
1476 return RPC_S_OK;
1479 RPC_STATUS RPCRT4_GetAssociation(LPCSTR Protseq, LPCSTR NetworkAddr,
1480 LPCSTR Endpoint, LPCWSTR NetworkOptions,
1481 RpcAssoc **assoc_out)
1483 RpcAssoc *assoc;
1485 EnterCriticalSection(&assoc_list_cs);
1486 LIST_FOR_EACH_ENTRY(assoc, &assoc_list, RpcAssoc, entry)
1488 if (!strcmp(Protseq, assoc->Protseq) &&
1489 !strcmp(NetworkAddr, assoc->NetworkAddr) &&
1490 !strcmp(Endpoint, assoc->Endpoint) &&
1491 ((!assoc->NetworkOptions && !NetworkOptions) || !strcmpW(NetworkOptions, assoc->NetworkOptions)))
1493 assoc->refs++;
1494 *assoc_out = assoc;
1495 LeaveCriticalSection(&assoc_list_cs);
1496 TRACE("using existing assoc %p\n", assoc);
1497 return RPC_S_OK;
1501 assoc = HeapAlloc(GetProcessHeap(), 0, sizeof(*assoc));
1502 if (!assoc)
1504 LeaveCriticalSection(&assoc_list_cs);
1505 return RPC_S_OUT_OF_RESOURCES;
1507 assoc->refs = 1;
1508 list_init(&assoc->connection_pool);
1509 InitializeCriticalSection(&assoc->cs);
1510 assoc->Protseq = RPCRT4_strdupA(Protseq);
1511 assoc->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1512 assoc->Endpoint = RPCRT4_strdupA(Endpoint);
1513 assoc->NetworkOptions = NetworkOptions ? RPCRT4_strdupW(NetworkOptions) : NULL;
1514 assoc->assoc_group_id = 0;
1515 list_add_head(&assoc_list, &assoc->entry);
1516 *assoc_out = assoc;
1518 LeaveCriticalSection(&assoc_list_cs);
1520 TRACE("new assoc %p\n", assoc);
1522 return RPC_S_OK;
1525 ULONG RpcAssoc_Release(RpcAssoc *assoc)
1527 ULONG refs;
1529 EnterCriticalSection(&assoc_list_cs);
1530 refs = --assoc->refs;
1531 if (!refs)
1532 list_remove(&assoc->entry);
1533 LeaveCriticalSection(&assoc_list_cs);
1535 if (!refs)
1537 RpcConnection *Connection, *cursor2;
1539 TRACE("destroying assoc %p\n", assoc);
1541 LIST_FOR_EACH_ENTRY_SAFE(Connection, cursor2, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1543 list_remove(&Connection->conn_pool_entry);
1544 RPCRT4_DestroyConnection(Connection);
1547 HeapFree(GetProcessHeap(), 0, assoc->NetworkOptions);
1548 HeapFree(GetProcessHeap(), 0, assoc->Endpoint);
1549 HeapFree(GetProcessHeap(), 0, assoc->NetworkAddr);
1550 HeapFree(GetProcessHeap(), 0, assoc->Protseq);
1552 DeleteCriticalSection(&assoc->cs);
1554 HeapFree(GetProcessHeap(), 0, assoc);
1557 return refs;
1560 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
1562 static RPC_STATUS RpcAssoc_BindConnection(const RpcAssoc *assoc, RpcConnection *conn,
1563 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1564 const RPC_SYNTAX_IDENTIFIER *TransferSyntax)
1566 RpcPktHdr *hdr;
1567 RpcPktHdr *response_hdr;
1568 RPC_MESSAGE msg;
1569 RPC_STATUS status;
1571 TRACE("sending bind request to server\n");
1573 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
1574 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
1575 assoc->assoc_group_id,
1576 InterfaceId, TransferSyntax);
1578 status = RPCRT4_Send(conn, hdr, NULL, 0);
1579 RPCRT4_FreeHeader(hdr);
1580 if (status != RPC_S_OK)
1581 return status;
1583 status = RPCRT4_Receive(conn, &response_hdr, &msg);
1584 if (status != RPC_S_OK)
1586 ERR("receive failed\n");
1587 return status;
1590 switch (response_hdr->common.ptype)
1592 case PKT_BIND_ACK:
1594 RpcAddressString *server_address = msg.Buffer;
1595 if ((msg.BufferLength >= FIELD_OFFSET(RpcAddressString, string[0])) ||
1596 (msg.BufferLength >= ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4)))
1598 unsigned short remaining = msg.BufferLength -
1599 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4);
1600 RpcResults *results = (RpcResults*)((ULONG_PTR)server_address +
1601 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
1602 if ((results->num_results == 1) && (remaining >= sizeof(*results)))
1604 switch (results->results[0].result)
1606 case RESULT_ACCEPT:
1607 conn->assoc_group_id = response_hdr->bind_ack.assoc_gid;
1608 conn->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
1609 conn->ActiveInterface = *InterfaceId;
1610 break;
1611 case RESULT_PROVIDER_REJECTION:
1612 switch (results->results[0].reason)
1614 case REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED:
1615 ERR("syntax %s, %d.%d not supported\n",
1616 debugstr_guid(&InterfaceId->SyntaxGUID),
1617 InterfaceId->SyntaxVersion.MajorVersion,
1618 InterfaceId->SyntaxVersion.MinorVersion);
1619 status = RPC_S_UNKNOWN_IF;
1620 break;
1621 case REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED:
1622 ERR("transfer syntax not supported\n");
1623 status = RPC_S_SERVER_UNAVAILABLE;
1624 break;
1625 case REASON_NONE:
1626 default:
1627 status = RPC_S_CALL_FAILED_DNE;
1629 break;
1630 case RESULT_USER_REJECTION:
1631 default:
1632 ERR("rejection result %d\n", results->results[0].result);
1633 status = RPC_S_CALL_FAILED_DNE;
1636 else
1638 ERR("incorrect results size\n");
1639 status = RPC_S_CALL_FAILED_DNE;
1642 else
1644 ERR("bind ack packet too small (%d)\n", msg.BufferLength);
1645 status = RPC_S_PROTOCOL_ERROR;
1647 break;
1649 case PKT_BIND_NACK:
1650 switch (response_hdr->bind_nack.reject_reason)
1652 case REJECT_LOCAL_LIMIT_EXCEEDED:
1653 case REJECT_TEMPORARY_CONGESTION:
1654 ERR("server too busy\n");
1655 status = RPC_S_SERVER_TOO_BUSY;
1656 break;
1657 case REJECT_PROTOCOL_VERSION_NOT_SUPPORTED:
1658 ERR("protocol version not supported\n");
1659 status = RPC_S_PROTOCOL_ERROR;
1660 break;
1661 case REJECT_UNKNOWN_AUTHN_SERVICE:
1662 ERR("unknown authentication service\n");
1663 status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1664 break;
1665 case REJECT_INVALID_CHECKSUM:
1666 ERR("invalid checksum\n");
1667 status = ERROR_ACCESS_DENIED;
1668 break;
1669 default:
1670 ERR("rejected bind for reason %d\n", response_hdr->bind_nack.reject_reason);
1671 status = RPC_S_CALL_FAILED_DNE;
1673 break;
1674 default:
1675 ERR("wrong packet type received %d\n", response_hdr->common.ptype);
1676 status = RPC_S_PROTOCOL_ERROR;
1677 break;
1680 I_RpcFreeBuffer(&msg);
1681 RPCRT4_FreeHeader(response_hdr);
1682 return status;
1685 static RpcConnection *RpcAssoc_GetIdleConnection(RpcAssoc *assoc,
1686 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1687 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, const RpcAuthInfo *AuthInfo,
1688 const RpcQualityOfService *QOS)
1690 RpcConnection *Connection;
1691 EnterCriticalSection(&assoc->cs);
1692 /* try to find a compatible connection from the connection pool */
1693 LIST_FOR_EACH_ENTRY(Connection, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1695 if (!memcmp(&Connection->ActiveInterface, InterfaceId,
1696 sizeof(RPC_SYNTAX_IDENTIFIER)) &&
1697 RpcAuthInfo_IsEqual(Connection->AuthInfo, AuthInfo) &&
1698 RpcQualityOfService_IsEqual(Connection->QOS, QOS))
1700 list_remove(&Connection->conn_pool_entry);
1701 LeaveCriticalSection(&assoc->cs);
1702 TRACE("got connection from pool %p\n", Connection);
1703 return Connection;
1707 LeaveCriticalSection(&assoc->cs);
1708 return NULL;
1711 RPC_STATUS RpcAssoc_GetClientConnection(RpcAssoc *assoc,
1712 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1713 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, RpcAuthInfo *AuthInfo,
1714 RpcQualityOfService *QOS, RpcConnection **Connection)
1716 RpcConnection *NewConnection;
1717 RPC_STATUS status;
1719 *Connection = RpcAssoc_GetIdleConnection(assoc, InterfaceId, TransferSyntax, AuthInfo, QOS);
1720 if (*Connection)
1721 return RPC_S_OK;
1723 /* create a new connection */
1724 status = RPCRT4_CreateConnection(&NewConnection, FALSE /* is this a server connection? */,
1725 assoc->Protseq, assoc->NetworkAddr,
1726 assoc->Endpoint, assoc->NetworkOptions,
1727 AuthInfo, QOS);
1728 if (status != RPC_S_OK)
1729 return status;
1731 status = RPCRT4_OpenClientConnection(NewConnection);
1732 if (status != RPC_S_OK)
1734 RPCRT4_DestroyConnection(NewConnection);
1735 return status;
1738 status = RpcAssoc_BindConnection(assoc, NewConnection, InterfaceId, TransferSyntax);
1739 if (status != RPC_S_OK)
1741 RPCRT4_DestroyConnection(NewConnection);
1742 return status;
1745 *Connection = NewConnection;
1747 return RPC_S_OK;
1750 void RpcAssoc_ReleaseIdleConnection(RpcAssoc *assoc, RpcConnection *Connection)
1752 assert(!Connection->server);
1753 EnterCriticalSection(&assoc->cs);
1754 if (!assoc->assoc_group_id) assoc->assoc_group_id = Connection->assoc_group_id;
1755 list_add_head(&assoc->connection_pool, &Connection->conn_pool_entry);
1756 LeaveCriticalSection(&assoc->cs);
1760 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1762 RPC_STATUS err;
1764 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1765 rpcrt4_conn_get_name(OldConnection),
1766 OldConnection->NetworkAddr,
1767 OldConnection->Endpoint, NULL,
1768 OldConnection->AuthInfo, OldConnection->QOS);
1769 if (err == RPC_S_OK)
1770 rpcrt4_conn_handoff(OldConnection, *Connection);
1771 return err;
1774 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1776 TRACE("connection: %p\n", Connection);
1778 RPCRT4_CloseConnection(Connection);
1779 RPCRT4_strfree(Connection->Endpoint);
1780 RPCRT4_strfree(Connection->NetworkAddr);
1781 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1782 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1783 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1784 HeapFree(GetProcessHeap(), 0, Connection);
1785 return RPC_S_OK;
1788 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1789 size_t *tower_size,
1790 const char *protseq,
1791 const char *networkaddr,
1792 const char *endpoint)
1794 twr_empty_floor_t *protocol_floor;
1795 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1797 *tower_size = 0;
1799 if (!protseq_ops)
1800 return RPC_S_INVALID_RPC_PROTSEQ;
1802 if (!tower_data)
1804 *tower_size = sizeof(*protocol_floor);
1805 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1806 return RPC_S_OK;
1809 protocol_floor = (twr_empty_floor_t *)tower_data;
1810 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1811 protocol_floor->protid = protseq_ops->epm_protocols[0];
1812 protocol_floor->count_rhs = 0;
1814 tower_data += sizeof(*protocol_floor);
1816 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1817 if (!*tower_size)
1818 return EPT_S_NOT_REGISTERED;
1820 *tower_size += sizeof(*protocol_floor);
1822 return RPC_S_OK;
1825 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1826 size_t tower_size,
1827 char **protseq,
1828 char **networkaddr,
1829 char **endpoint)
1831 const twr_empty_floor_t *protocol_floor;
1832 const twr_empty_floor_t *floor4;
1833 const struct connection_ops *protseq_ops = NULL;
1834 RPC_STATUS status;
1835 int i;
1837 if (tower_size < sizeof(*protocol_floor))
1838 return EPT_S_NOT_REGISTERED;
1840 protocol_floor = (const twr_empty_floor_t *)tower_data;
1841 tower_data += sizeof(*protocol_floor);
1842 tower_size -= sizeof(*protocol_floor);
1843 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1844 (protocol_floor->count_rhs > tower_size))
1845 return EPT_S_NOT_REGISTERED;
1846 tower_data += protocol_floor->count_rhs;
1847 tower_size -= protocol_floor->count_rhs;
1849 floor4 = (const twr_empty_floor_t *)tower_data;
1850 if ((tower_size < sizeof(*floor4)) ||
1851 (floor4->count_lhs != sizeof(floor4->protid)))
1852 return EPT_S_NOT_REGISTERED;
1854 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1855 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1856 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1858 protseq_ops = &conn_protseq_list[i];
1859 break;
1862 if (!protseq_ops)
1863 return EPT_S_NOT_REGISTERED;
1865 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1867 if ((status == RPC_S_OK) && protseq)
1869 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1870 strcpy(*protseq, protseq_ops->name);
1873 return status;
1876 /***********************************************************************
1877 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1879 * Checks if the given protocol sequence is known by the RPC system.
1880 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1883 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1885 char ps[0x10];
1887 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1888 ps, sizeof ps, NULL, NULL);
1889 if (rpcrt4_get_conn_protseq_ops(ps))
1890 return RPC_S_OK;
1892 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1894 return RPC_S_INVALID_RPC_PROTSEQ;
1897 /***********************************************************************
1898 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1900 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1902 UNICODE_STRING protseqW;
1904 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1906 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1907 RtlFreeUnicodeString(&protseqW);
1908 return ret;
1910 return RPC_S_OUT_OF_MEMORY;