rpcrt4: Add test for RpcBindingToStringBindingA.
[wine/wine64.git] / dlls / rpcrt4 / rpc_transport.c
blob75423b943698bea9c02542383f6af3a76091968b
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 <stdlib.h>
33 #include <sys/types.h>
35 #if defined(__MINGW32__) || defined (_MSC_VER)
36 # include <ws2tcpip.h>
37 # ifndef EADDRINUSE
38 # define EADDRINUSE WSAEADDRINUSE
39 # endif
40 # ifndef EAGAIN
41 # define EAGAIN WSAEWOULDBLOCK
42 # endif
43 #else
44 # include <errno.h>
45 # ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 # endif
48 # include <fcntl.h>
49 # ifdef HAVE_SYS_SOCKET_H
50 # include <sys/socket.h>
51 # endif
52 # ifdef HAVE_NETINET_IN_H
53 # include <netinet/in.h>
54 # endif
55 # ifdef HAVE_NETINET_TCP_H
56 # include <netinet/tcp.h>
57 # endif
58 # ifdef HAVE_ARPA_INET_H
59 # include <arpa/inet.h>
60 # endif
61 # ifdef HAVE_NETDB_H
62 # include <netdb.h>
63 # endif
64 # ifdef HAVE_SYS_POLL_H
65 # include <sys/poll.h>
66 # endif
67 # define closesocket close
68 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
70 #include "windef.h"
71 #include "winbase.h"
72 #include "winnls.h"
73 #include "winerror.h"
74 #include "winternl.h"
75 #include "wine/unicode.h"
77 #include "rpc.h"
78 #include "rpcndr.h"
80 #include "wine/debug.h"
82 #include "rpc_binding.h"
83 #include "rpc_message.h"
84 #include "rpc_server.h"
85 #include "epm_towers.h"
87 #ifndef SOL_TCP
88 # define SOL_TCP IPPROTO_TCP
89 #endif
91 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
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 for (;;)
123 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
124 return RPC_S_OK;
126 switch(GetLastError())
128 case ERROR_PIPE_CONNECTED:
129 SetEvent(npc->ovl.hEvent);
130 return RPC_S_OK;
131 case ERROR_IO_PENDING:
132 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
133 return RPC_S_OK;
134 case ERROR_NO_DATA_DETECTED:
135 /* client has disconnected, retry */
136 DisconnectNamedPipe( npc->pipe );
137 break;
138 default:
139 npc->listening = FALSE;
140 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
141 return RPC_S_OUT_OF_RESOURCES;
146 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
148 RpcConnection_np *npc = (RpcConnection_np *) Connection;
149 TRACE("listening on %s\n", pname);
151 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
152 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
153 PIPE_UNLIMITED_INSTANCES,
154 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
155 if (npc->pipe == INVALID_HANDLE_VALUE) {
156 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
157 if (GetLastError() == ERROR_FILE_EXISTS)
158 return RPC_S_DUPLICATE_ENDPOINT;
159 else
160 return RPC_S_CANT_CREATE_ENDPOINT;
163 memset(&npc->ovl, 0, sizeof(npc->ovl));
164 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
166 /* Note: we don't call ConnectNamedPipe here because it must be done in the
167 * server thread as the thread must be alertable */
168 return RPC_S_OK;
171 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
173 RpcConnection_np *npc = (RpcConnection_np *) Connection;
174 HANDLE pipe;
175 DWORD err, dwMode;
177 TRACE("connecting to %s\n", pname);
179 while (TRUE) {
180 DWORD dwFlags = 0;
181 if (Connection->QOS)
183 dwFlags = SECURITY_SQOS_PRESENT;
184 switch (Connection->QOS->qos->ImpersonationType)
186 case RPC_C_IMP_LEVEL_DEFAULT:
187 /* FIXME: what to do here? */
188 break;
189 case RPC_C_IMP_LEVEL_ANONYMOUS:
190 dwFlags |= SECURITY_ANONYMOUS;
191 break;
192 case RPC_C_IMP_LEVEL_IDENTIFY:
193 dwFlags |= SECURITY_IDENTIFICATION;
194 break;
195 case RPC_C_IMP_LEVEL_IMPERSONATE:
196 dwFlags |= SECURITY_IMPERSONATION;
197 break;
198 case RPC_C_IMP_LEVEL_DELEGATE:
199 dwFlags |= SECURITY_DELEGATION;
200 break;
202 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
203 dwFlags |= SECURITY_CONTEXT_TRACKING;
205 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
206 OPEN_EXISTING, dwFlags, 0);
207 if (pipe != INVALID_HANDLE_VALUE) break;
208 err = GetLastError();
209 if (err == ERROR_PIPE_BUSY) {
210 TRACE("connection failed, error=%x\n", err);
211 return RPC_S_SERVER_TOO_BUSY;
213 if (!wait)
214 return RPC_S_SERVER_UNAVAILABLE;
215 if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
216 err = GetLastError();
217 WARN("connection failed, error=%x\n", err);
218 return RPC_S_SERVER_UNAVAILABLE;
222 /* success */
223 memset(&npc->ovl, 0, sizeof(npc->ovl));
224 /* pipe is connected; change to message-read mode. */
225 dwMode = PIPE_READMODE_MESSAGE;
226 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
227 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
228 npc->pipe = pipe;
230 return RPC_S_OK;
233 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
235 RpcConnection_np *npc = (RpcConnection_np *) Connection;
236 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
237 RPC_STATUS r;
238 LPSTR pname;
240 /* already connected? */
241 if (npc->pipe)
242 return RPC_S_OK;
244 /* protseq=ncalrpc: supposed to use NT LPC ports,
245 * but we'll implement it with named pipes for now */
246 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
247 strcat(strcpy(pname, prefix), Connection->Endpoint);
248 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
249 I_RpcFree(pname);
251 return r;
254 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
256 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
257 RPC_STATUS r;
258 LPSTR pname;
259 RpcConnection *Connection;
261 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
262 endpoint, NULL, NULL, NULL);
263 if (r != RPC_S_OK)
264 return r;
266 /* protseq=ncalrpc: supposed to use NT LPC ports,
267 * but we'll implement it with named pipes for now */
268 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
269 strcat(strcpy(pname, prefix), Connection->Endpoint);
270 r = rpcrt4_conn_create_pipe(Connection, pname);
271 I_RpcFree(pname);
273 EnterCriticalSection(&protseq->cs);
274 Connection->Next = protseq->conn;
275 protseq->conn = Connection;
276 LeaveCriticalSection(&protseq->cs);
278 return r;
281 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
283 RpcConnection_np *npc = (RpcConnection_np *) Connection;
284 static const char prefix[] = "\\\\.";
285 RPC_STATUS r;
286 LPSTR pname;
288 /* already connected? */
289 if (npc->pipe)
290 return RPC_S_OK;
292 /* protseq=ncacn_np: named pipes */
293 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
294 strcat(strcpy(pname, prefix), Connection->Endpoint);
295 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
296 I_RpcFree(pname);
298 return r;
301 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
303 static const char prefix[] = "\\\\.";
304 RPC_STATUS r;
305 LPSTR pname;
306 RpcConnection *Connection;
308 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
309 endpoint, NULL, NULL, NULL);
310 if (r != RPC_S_OK)
311 return r;
313 /* protseq=ncacn_np: named pipes */
314 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
315 strcat(strcpy(pname, prefix), Connection->Endpoint);
316 r = rpcrt4_conn_create_pipe(Connection, pname);
317 I_RpcFree(pname);
319 EnterCriticalSection(&protseq->cs);
320 Connection->Next = protseq->conn;
321 protseq->conn = Connection;
322 LeaveCriticalSection(&protseq->cs);
324 return r;
327 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
329 /* because of the way named pipes work, we'll transfer the connected pipe
330 * to the child, then reopen the server binding to continue listening */
332 new_npc->pipe = old_npc->pipe;
333 new_npc->ovl = old_npc->ovl;
334 old_npc->pipe = 0;
335 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
336 old_npc->listening = FALSE;
339 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
341 RPC_STATUS status;
342 LPSTR pname;
343 static const char prefix[] = "\\\\.";
345 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
347 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
348 strcat(strcpy(pname, prefix), old_conn->Endpoint);
349 status = rpcrt4_conn_create_pipe(old_conn, pname);
350 I_RpcFree(pname);
352 return status;
355 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
357 RPC_STATUS status;
358 LPSTR pname;
359 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
361 TRACE("%s\n", old_conn->Endpoint);
363 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
365 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
366 strcat(strcpy(pname, prefix), old_conn->Endpoint);
367 status = rpcrt4_conn_create_pipe(old_conn, pname);
368 I_RpcFree(pname);
370 return status;
373 static int rpcrt4_conn_np_read(RpcConnection *Connection,
374 void *buffer, unsigned int count)
376 RpcConnection_np *npc = (RpcConnection_np *) Connection;
377 char *buf = buffer;
378 BOOL ret = TRUE;
379 unsigned int bytes_left = count;
381 while (bytes_left)
383 DWORD bytes_read;
384 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
385 if (!ret || !bytes_read)
386 break;
387 bytes_left -= bytes_read;
388 buf += bytes_read;
390 return ret ? count : -1;
393 static int rpcrt4_conn_np_write(RpcConnection *Connection,
394 const void *buffer, unsigned int count)
396 RpcConnection_np *npc = (RpcConnection_np *) Connection;
397 const char *buf = buffer;
398 BOOL ret = TRUE;
399 unsigned int bytes_left = count;
401 while (bytes_left)
403 DWORD bytes_written;
404 ret = WriteFile(npc->pipe, buf, bytes_left, &bytes_written, NULL);
405 if (!ret || !bytes_written)
406 break;
407 bytes_left -= bytes_written;
408 buf += bytes_written;
410 return ret ? count : -1;
413 static int rpcrt4_conn_np_close(RpcConnection *Connection)
415 RpcConnection_np *npc = (RpcConnection_np *) Connection;
416 if (npc->pipe) {
417 FlushFileBuffers(npc->pipe);
418 CloseHandle(npc->pipe);
419 npc->pipe = 0;
421 if (npc->ovl.hEvent) {
422 CloseHandle(npc->ovl.hEvent);
423 npc->ovl.hEvent = 0;
425 return 0;
428 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
430 /* FIXME: implement when named pipe writes use overlapped I/O */
433 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
435 /* FIXME: implement when named pipe writes use overlapped I/O */
436 return -1;
439 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
440 const char *networkaddr,
441 const char *endpoint)
443 twr_empty_floor_t *smb_floor;
444 twr_empty_floor_t *nb_floor;
445 size_t size;
446 size_t networkaddr_size;
447 size_t endpoint_size;
449 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
451 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
452 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
453 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
455 if (!tower_data)
456 return size;
458 smb_floor = (twr_empty_floor_t *)tower_data;
460 tower_data += sizeof(*smb_floor);
462 smb_floor->count_lhs = sizeof(smb_floor->protid);
463 smb_floor->protid = EPM_PROTOCOL_SMB;
464 smb_floor->count_rhs = endpoint_size;
466 if (endpoint)
467 memcpy(tower_data, endpoint, endpoint_size);
468 else
469 tower_data[0] = 0;
470 tower_data += endpoint_size;
472 nb_floor = (twr_empty_floor_t *)tower_data;
474 tower_data += sizeof(*nb_floor);
476 nb_floor->count_lhs = sizeof(nb_floor->protid);
477 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
478 nb_floor->count_rhs = networkaddr_size;
480 if (networkaddr)
481 memcpy(tower_data, networkaddr, networkaddr_size);
482 else
483 tower_data[0] = 0;
484 tower_data += networkaddr_size;
486 return size;
489 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
490 size_t tower_size,
491 char **networkaddr,
492 char **endpoint)
494 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
495 const twr_empty_floor_t *nb_floor;
497 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
499 if (tower_size < sizeof(*smb_floor))
500 return EPT_S_NOT_REGISTERED;
502 tower_data += sizeof(*smb_floor);
503 tower_size -= sizeof(*smb_floor);
505 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
506 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
507 (smb_floor->count_rhs > tower_size) ||
508 (tower_data[smb_floor->count_rhs - 1] != '\0'))
509 return EPT_S_NOT_REGISTERED;
511 if (endpoint)
513 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
514 if (!*endpoint)
515 return RPC_S_OUT_OF_RESOURCES;
516 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
518 tower_data += smb_floor->count_rhs;
519 tower_size -= smb_floor->count_rhs;
521 if (tower_size < sizeof(*nb_floor))
522 return EPT_S_NOT_REGISTERED;
524 nb_floor = (const twr_empty_floor_t *)tower_data;
526 tower_data += sizeof(*nb_floor);
527 tower_size -= sizeof(*nb_floor);
529 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
530 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
531 (nb_floor->count_rhs > tower_size) ||
532 (tower_data[nb_floor->count_rhs - 1] != '\0'))
533 return EPT_S_NOT_REGISTERED;
535 if (networkaddr)
537 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
538 if (!*networkaddr)
540 if (endpoint)
542 I_RpcFree(*endpoint);
543 *endpoint = NULL;
545 return RPC_S_OUT_OF_RESOURCES;
547 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
550 return RPC_S_OK;
553 typedef struct _RpcServerProtseq_np
555 RpcServerProtseq common;
556 HANDLE mgr_event;
557 } RpcServerProtseq_np;
559 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
561 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
562 if (ps)
563 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
564 return &ps->common;
567 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
569 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
570 SetEvent(npps->mgr_event);
573 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
575 HANDLE *objs = prev_array;
576 RpcConnection_np *conn;
577 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
579 EnterCriticalSection(&protseq->cs);
581 /* open and count connections */
582 *count = 1;
583 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
584 while (conn) {
585 rpcrt4_conn_listen_pipe(conn);
586 if (conn->ovl.hEvent)
587 (*count)++;
588 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
591 /* make array of connections */
592 if (objs)
593 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
594 else
595 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
596 if (!objs)
598 ERR("couldn't allocate objs\n");
599 LeaveCriticalSection(&protseq->cs);
600 return NULL;
603 objs[0] = npps->mgr_event;
604 *count = 1;
605 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
606 while (conn) {
607 if ((objs[*count] = conn->ovl.hEvent))
608 (*count)++;
609 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
611 LeaveCriticalSection(&protseq->cs);
612 return objs;
615 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
617 HeapFree(GetProcessHeap(), 0, array);
620 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
622 HANDLE b_handle;
623 HANDLE *objs = wait_array;
624 DWORD res;
625 RpcConnection *cconn;
626 RpcConnection_np *conn;
628 if (!objs)
629 return -1;
633 /* an alertable wait isn't strictly necessary, but due to our
634 * overlapped I/O implementation in Wine we need to free some memory
635 * by the file user APC being called, even if no completion routine was
636 * specified at the time of starting the async operation */
637 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
638 } while (res == WAIT_IO_COMPLETION);
640 if (res == WAIT_OBJECT_0)
641 return 0;
642 else if (res == WAIT_FAILED)
644 ERR("wait failed with error %d\n", GetLastError());
645 return -1;
647 else
649 b_handle = objs[res - WAIT_OBJECT_0];
650 /* find which connection got a RPC */
651 EnterCriticalSection(&protseq->cs);
652 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
653 while (conn) {
654 if (b_handle == conn->ovl.hEvent) break;
655 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
657 cconn = NULL;
658 if (conn)
659 RPCRT4_SpawnConnection(&cconn, &conn->common);
660 else
661 ERR("failed to locate connection for handle %p\n", b_handle);
662 LeaveCriticalSection(&protseq->cs);
663 if (cconn)
665 RPCRT4_new_client(cconn);
666 return 1;
668 else return -1;
672 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
673 const char *networkaddr,
674 const char *endpoint)
676 twr_empty_floor_t *pipe_floor;
677 size_t size;
678 size_t endpoint_size;
680 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
682 endpoint_size = strlen(endpoint) + 1;
683 size = sizeof(*pipe_floor) + endpoint_size;
685 if (!tower_data)
686 return size;
688 pipe_floor = (twr_empty_floor_t *)tower_data;
690 tower_data += sizeof(*pipe_floor);
692 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
693 pipe_floor->protid = EPM_PROTOCOL_PIPE;
694 pipe_floor->count_rhs = endpoint_size;
696 memcpy(tower_data, endpoint, endpoint_size);
697 tower_data += endpoint_size;
699 return size;
702 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
703 size_t tower_size,
704 char **networkaddr,
705 char **endpoint)
707 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
709 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
711 if (tower_size < sizeof(*pipe_floor))
712 return EPT_S_NOT_REGISTERED;
714 tower_data += sizeof(*pipe_floor);
715 tower_size -= sizeof(*pipe_floor);
717 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
718 (pipe_floor->protid != EPM_PROTOCOL_PIPE) ||
719 (pipe_floor->count_rhs > tower_size) ||
720 (tower_data[pipe_floor->count_rhs - 1] != '\0'))
721 return EPT_S_NOT_REGISTERED;
723 if (networkaddr)
724 *networkaddr = NULL;
726 if (endpoint)
728 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
729 if (!*endpoint)
730 return RPC_S_OUT_OF_RESOURCES;
731 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
734 return RPC_S_OK;
737 /**** ncacn_ip_tcp support ****/
739 typedef struct _RpcConnection_tcp
741 RpcConnection common;
742 int sock;
743 int cancel_fds[2];
744 } RpcConnection_tcp;
746 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
748 RpcConnection_tcp *tcpc;
749 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
750 if (tcpc == NULL)
751 return NULL;
752 tcpc->sock = -1;
753 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
755 ERR("socketpair() failed: %s\n", strerror(errno));
756 HeapFree(GetProcessHeap(), 0, tcpc);
757 return NULL;
759 return &tcpc->common;
762 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
764 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
765 int sock;
766 int ret;
767 struct addrinfo *ai;
768 struct addrinfo *ai_cur;
769 struct addrinfo hints;
771 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
773 if (tcpc->sock != -1)
774 return RPC_S_OK;
776 hints.ai_flags = 0;
777 hints.ai_family = PF_UNSPEC;
778 hints.ai_socktype = SOCK_STREAM;
779 hints.ai_protocol = IPPROTO_TCP;
780 hints.ai_addrlen = 0;
781 hints.ai_addr = NULL;
782 hints.ai_canonname = NULL;
783 hints.ai_next = NULL;
785 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
786 if (ret)
788 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
789 Connection->Endpoint, gai_strerror(ret));
790 return RPC_S_SERVER_UNAVAILABLE;
793 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
795 int val;
797 if (TRACE_ON(rpc))
799 char host[256];
800 char service[256];
801 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
802 host, sizeof(host), service, sizeof(service),
803 NI_NUMERICHOST | NI_NUMERICSERV);
804 TRACE("trying %s:%s\n", host, service);
807 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
808 if (sock == -1)
810 WARN("socket() failed: %s\n", strerror(errno));
811 continue;
814 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
816 WARN("connect() failed: %s\n", strerror(errno));
817 closesocket(sock);
818 continue;
821 /* RPC depends on having minimal latency so disable the Nagle algorithm */
822 val = 1;
823 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
824 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
826 tcpc->sock = sock;
828 freeaddrinfo(ai);
829 TRACE("connected\n");
830 return RPC_S_OK;
833 freeaddrinfo(ai);
834 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
835 return RPC_S_SERVER_UNAVAILABLE;
838 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
840 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
841 int sock;
842 int ret;
843 struct addrinfo *ai;
844 struct addrinfo *ai_cur;
845 struct addrinfo hints;
846 RpcConnection *first_connection = NULL;
848 TRACE("(%p, %s)\n", protseq, endpoint);
850 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
851 hints.ai_family = PF_UNSPEC;
852 hints.ai_socktype = SOCK_STREAM;
853 hints.ai_protocol = IPPROTO_TCP;
854 hints.ai_addrlen = 0;
855 hints.ai_addr = NULL;
856 hints.ai_canonname = NULL;
857 hints.ai_next = NULL;
859 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
860 if (ret)
862 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
863 gai_strerror(ret));
864 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
865 return RPC_S_INVALID_ENDPOINT_FORMAT;
866 return RPC_S_CANT_CREATE_ENDPOINT;
869 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
871 RpcConnection_tcp *tcpc;
872 RPC_STATUS create_status;
874 if (TRACE_ON(rpc))
876 char host[256];
877 char service[256];
878 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
879 host, sizeof(host), service, sizeof(service),
880 NI_NUMERICHOST | NI_NUMERICSERV);
881 TRACE("trying %s:%s\n", host, service);
884 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
885 if (sock == -1)
887 WARN("socket() failed: %s\n", strerror(errno));
888 status = RPC_S_CANT_CREATE_ENDPOINT;
889 continue;
892 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
893 if (ret < 0)
895 WARN("bind failed: %s\n", strerror(errno));
896 closesocket(sock);
897 if (errno == EADDRINUSE)
898 status = RPC_S_DUPLICATE_ENDPOINT;
899 else
900 status = RPC_S_CANT_CREATE_ENDPOINT;
901 continue;
903 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
904 protseq->Protseq, NULL,
905 endpoint, NULL, NULL, NULL);
906 if (create_status != RPC_S_OK)
908 closesocket(sock);
909 status = create_status;
910 continue;
913 tcpc->sock = sock;
914 ret = listen(sock, protseq->MaxCalls);
915 if (ret < 0)
917 WARN("listen failed: %s\n", strerror(errno));
918 RPCRT4_DestroyConnection(&tcpc->common);
919 status = RPC_S_OUT_OF_RESOURCES;
920 continue;
922 /* need a non-blocking socket, otherwise accept() has a potential
923 * race-condition (poll() says it is readable, connection drops,
924 * and accept() blocks until the next connection comes...)
926 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
927 if (ret < 0)
929 WARN("couldn't make socket non-blocking, error %d\n", ret);
930 RPCRT4_DestroyConnection(&tcpc->common);
931 status = RPC_S_OUT_OF_RESOURCES;
932 continue;
935 tcpc->common.Next = first_connection;
936 first_connection = &tcpc->common;
939 freeaddrinfo(ai);
941 /* if at least one connection was created for an endpoint then
942 * return success */
943 if (first_connection)
945 RpcConnection *conn;
947 /* find last element in list */
948 for (conn = first_connection; conn->Next; conn = conn->Next)
951 EnterCriticalSection(&protseq->cs);
952 conn->Next = protseq->conn;
953 protseq->conn = first_connection;
954 LeaveCriticalSection(&protseq->cs);
956 TRACE("listening on %s\n", endpoint);
957 return RPC_S_OK;
960 ERR("couldn't listen on port %s\n", endpoint);
961 return status;
964 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
966 int ret;
967 struct sockaddr_in address;
968 socklen_t addrsize;
969 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
970 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
972 addrsize = sizeof(address);
973 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
974 if (ret < 0)
976 ERR("Failed to accept a TCP connection: error %d\n", ret);
977 return RPC_S_OUT_OF_RESOURCES;
979 /* reset to blocking behaviour */
980 fcntl(ret, F_SETFL, 0);
981 client->sock = ret;
982 TRACE("Accepted a new TCP connection\n");
983 return RPC_S_OK;
986 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
987 void *buffer, unsigned int count)
989 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
990 int bytes_read = 0;
993 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
994 if (!r)
995 return -1;
996 else if (r > 0)
997 bytes_read += r;
998 else if (errno != EAGAIN)
1000 WARN("recv() failed: %s\n", strerror(errno));
1001 return -1;
1003 else
1005 struct pollfd pfds[2];
1006 pfds[0].fd = tcpc->sock;
1007 pfds[0].events = POLLIN;
1008 pfds[1].fd = tcpc->cancel_fds[0];
1009 pfds[1].events = POLLIN;
1010 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1012 ERR("poll() failed: %s\n", strerror(errno));
1013 return -1;
1015 if (pfds[1].revents & POLLIN) /* canceled */
1017 char dummy;
1018 read(pfds[1].fd, &dummy, sizeof(dummy));
1019 return -1;
1022 } while (bytes_read != count);
1023 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1024 return bytes_read;
1027 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1028 const void *buffer, unsigned int count)
1030 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1031 int bytes_written = 0;
1034 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1035 if (r >= 0)
1036 bytes_written += r;
1037 else if (errno != EAGAIN)
1038 return -1;
1039 else
1041 struct pollfd pfd;
1042 pfd.fd = tcpc->sock;
1043 pfd.events = POLLOUT;
1044 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1046 ERR("poll() failed: %s\n", strerror(errno));
1047 return -1;
1050 } while (bytes_written != count);
1051 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1052 return bytes_written;
1055 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1057 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1059 TRACE("%d\n", tcpc->sock);
1061 if (tcpc->sock != -1)
1062 closesocket(tcpc->sock);
1063 tcpc->sock = -1;
1064 close(tcpc->cancel_fds[0]);
1065 close(tcpc->cancel_fds[1]);
1066 return 0;
1069 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1071 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1072 char dummy = 1;
1074 TRACE("%p\n", Connection);
1076 write(tcpc->cancel_fds[1], &dummy, 1);
1079 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1081 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1082 struct pollfd pfds[2];
1084 TRACE("%p\n", Connection);
1086 pfds[0].fd = tcpc->sock;
1087 pfds[0].events = POLLIN;
1088 pfds[1].fd = tcpc->cancel_fds[0];
1089 pfds[1].events = POLLIN;
1090 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1092 ERR("poll() failed: %s\n", strerror(errno));
1093 return -1;
1095 if (pfds[1].revents & POLLIN) /* canceled */
1097 char dummy;
1098 read(pfds[1].fd, &dummy, sizeof(dummy));
1099 return -1;
1102 return 0;
1105 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1106 const char *networkaddr,
1107 const char *endpoint)
1109 twr_tcp_floor_t *tcp_floor;
1110 twr_ipv4_floor_t *ipv4_floor;
1111 struct addrinfo *ai;
1112 struct addrinfo hints;
1113 int ret;
1114 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1116 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1118 if (!tower_data)
1119 return size;
1121 tcp_floor = (twr_tcp_floor_t *)tower_data;
1122 tower_data += sizeof(*tcp_floor);
1124 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1126 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1127 tcp_floor->protid = EPM_PROTOCOL_TCP;
1128 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1130 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1131 ipv4_floor->protid = EPM_PROTOCOL_IP;
1132 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1134 hints.ai_flags = AI_NUMERICHOST;
1135 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1136 hints.ai_family = PF_INET;
1137 hints.ai_socktype = SOCK_STREAM;
1138 hints.ai_protocol = IPPROTO_TCP;
1139 hints.ai_addrlen = 0;
1140 hints.ai_addr = NULL;
1141 hints.ai_canonname = NULL;
1142 hints.ai_next = NULL;
1144 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1145 if (ret)
1147 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1148 if (ret)
1150 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1151 return 0;
1155 if (ai->ai_family == PF_INET)
1157 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1158 tcp_floor->port = sin->sin_port;
1159 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1161 else
1163 ERR("unexpected protocol family %d\n", ai->ai_family);
1164 return 0;
1167 freeaddrinfo(ai);
1169 return size;
1172 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1173 size_t tower_size,
1174 char **networkaddr,
1175 char **endpoint)
1177 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1178 const twr_ipv4_floor_t *ipv4_floor;
1179 struct in_addr in_addr;
1181 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1183 if (tower_size < sizeof(*tcp_floor))
1184 return EPT_S_NOT_REGISTERED;
1186 tower_data += sizeof(*tcp_floor);
1187 tower_size -= sizeof(*tcp_floor);
1189 if (tower_size < sizeof(*ipv4_floor))
1190 return EPT_S_NOT_REGISTERED;
1192 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1194 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1195 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1196 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1197 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1198 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1199 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1200 return EPT_S_NOT_REGISTERED;
1202 if (endpoint)
1204 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1205 if (!*endpoint)
1206 return RPC_S_OUT_OF_RESOURCES;
1207 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1210 if (networkaddr)
1212 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1213 if (!*networkaddr)
1215 if (endpoint)
1217 I_RpcFree(*endpoint);
1218 *endpoint = NULL;
1220 return RPC_S_OUT_OF_RESOURCES;
1222 in_addr.s_addr = ipv4_floor->ipv4addr;
1223 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1225 ERR("inet_ntop: %s\n", strerror(errno));
1226 I_RpcFree(*networkaddr);
1227 *networkaddr = NULL;
1228 if (endpoint)
1230 I_RpcFree(*endpoint);
1231 *endpoint = NULL;
1233 return EPT_S_NOT_REGISTERED;
1237 return RPC_S_OK;
1240 typedef struct _RpcServerProtseq_sock
1242 RpcServerProtseq common;
1243 int mgr_event_rcv;
1244 int mgr_event_snd;
1245 } RpcServerProtseq_sock;
1247 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1249 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1250 if (ps)
1252 int fds[2];
1253 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1255 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1256 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1257 ps->mgr_event_rcv = fds[0];
1258 ps->mgr_event_snd = fds[1];
1260 else
1262 ERR("socketpair failed with error %s\n", strerror(errno));
1263 HeapFree(GetProcessHeap(), 0, ps);
1264 return NULL;
1267 return &ps->common;
1270 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1272 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1273 char dummy = 1;
1274 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1277 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1279 struct pollfd *poll_info = prev_array;
1280 RpcConnection_tcp *conn;
1281 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1283 EnterCriticalSection(&protseq->cs);
1285 /* open and count connections */
1286 *count = 1;
1287 conn = (RpcConnection_tcp *)protseq->conn;
1288 while (conn) {
1289 if (conn->sock != -1)
1290 (*count)++;
1291 conn = (RpcConnection_tcp *)conn->common.Next;
1294 /* make array of connections */
1295 if (poll_info)
1296 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1297 else
1298 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1299 if (!poll_info)
1301 ERR("couldn't allocate poll_info\n");
1302 LeaveCriticalSection(&protseq->cs);
1303 return NULL;
1306 poll_info[0].fd = sockps->mgr_event_rcv;
1307 poll_info[0].events = POLLIN;
1308 *count = 1;
1309 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1310 while (conn) {
1311 if (conn->sock != -1)
1313 poll_info[*count].fd = conn->sock;
1314 poll_info[*count].events = POLLIN;
1315 (*count)++;
1317 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1319 LeaveCriticalSection(&protseq->cs);
1320 return poll_info;
1323 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1325 HeapFree(GetProcessHeap(), 0, array);
1328 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1330 struct pollfd *poll_info = wait_array;
1331 int ret, i;
1332 RpcConnection *cconn;
1333 RpcConnection_tcp *conn;
1335 if (!poll_info)
1336 return -1;
1338 ret = poll(poll_info, count, -1);
1339 if (ret < 0)
1341 ERR("poll failed with error %d\n", ret);
1342 return -1;
1345 for (i = 0; i < count; i++)
1346 if (poll_info[i].revents & POLLIN)
1348 /* RPC server event */
1349 if (i == 0)
1351 char dummy;
1352 read(poll_info[0].fd, &dummy, sizeof(dummy));
1353 return 0;
1356 /* find which connection got a RPC */
1357 EnterCriticalSection(&protseq->cs);
1358 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1359 while (conn) {
1360 if (poll_info[i].fd == conn->sock) break;
1361 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1363 cconn = NULL;
1364 if (conn)
1365 RPCRT4_SpawnConnection(&cconn, &conn->common);
1366 else
1367 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1368 LeaveCriticalSection(&protseq->cs);
1369 if (cconn)
1370 RPCRT4_new_client(cconn);
1371 else
1372 return -1;
1375 return 1;
1378 static const struct connection_ops conn_protseq_list[] = {
1379 { "ncacn_np",
1380 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1381 rpcrt4_conn_np_alloc,
1382 rpcrt4_ncacn_np_open,
1383 rpcrt4_ncacn_np_handoff,
1384 rpcrt4_conn_np_read,
1385 rpcrt4_conn_np_write,
1386 rpcrt4_conn_np_close,
1387 rpcrt4_conn_np_cancel_call,
1388 rpcrt4_conn_np_wait_for_incoming_data,
1389 rpcrt4_ncacn_np_get_top_of_tower,
1390 rpcrt4_ncacn_np_parse_top_of_tower,
1392 { "ncalrpc",
1393 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1394 rpcrt4_conn_np_alloc,
1395 rpcrt4_ncalrpc_open,
1396 rpcrt4_ncalrpc_handoff,
1397 rpcrt4_conn_np_read,
1398 rpcrt4_conn_np_write,
1399 rpcrt4_conn_np_close,
1400 rpcrt4_conn_np_cancel_call,
1401 rpcrt4_conn_np_wait_for_incoming_data,
1402 rpcrt4_ncalrpc_get_top_of_tower,
1403 rpcrt4_ncalrpc_parse_top_of_tower,
1405 { "ncacn_ip_tcp",
1406 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1407 rpcrt4_conn_tcp_alloc,
1408 rpcrt4_ncacn_ip_tcp_open,
1409 rpcrt4_conn_tcp_handoff,
1410 rpcrt4_conn_tcp_read,
1411 rpcrt4_conn_tcp_write,
1412 rpcrt4_conn_tcp_close,
1413 rpcrt4_conn_tcp_cancel_call,
1414 rpcrt4_conn_tcp_wait_for_incoming_data,
1415 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1416 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1421 static const struct protseq_ops protseq_list[] =
1424 "ncacn_np",
1425 rpcrt4_protseq_np_alloc,
1426 rpcrt4_protseq_np_signal_state_changed,
1427 rpcrt4_protseq_np_get_wait_array,
1428 rpcrt4_protseq_np_free_wait_array,
1429 rpcrt4_protseq_np_wait_for_new_connection,
1430 rpcrt4_protseq_ncacn_np_open_endpoint,
1433 "ncalrpc",
1434 rpcrt4_protseq_np_alloc,
1435 rpcrt4_protseq_np_signal_state_changed,
1436 rpcrt4_protseq_np_get_wait_array,
1437 rpcrt4_protseq_np_free_wait_array,
1438 rpcrt4_protseq_np_wait_for_new_connection,
1439 rpcrt4_protseq_ncalrpc_open_endpoint,
1442 "ncacn_ip_tcp",
1443 rpcrt4_protseq_sock_alloc,
1444 rpcrt4_protseq_sock_signal_state_changed,
1445 rpcrt4_protseq_sock_get_wait_array,
1446 rpcrt4_protseq_sock_free_wait_array,
1447 rpcrt4_protseq_sock_wait_for_new_connection,
1448 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1452 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1454 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1456 int i;
1457 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1458 if (!strcmp(protseq_list[i].name, protseq))
1459 return &protseq_list[i];
1460 return NULL;
1463 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1465 int i;
1466 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1467 if (!strcmp(conn_protseq_list[i].name, protseq))
1468 return &conn_protseq_list[i];
1469 return NULL;
1472 /**** interface to rest of code ****/
1474 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1476 TRACE("(Connection == ^%p)\n", Connection);
1478 assert(!Connection->server);
1479 return Connection->ops->open_connection_client(Connection);
1482 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1484 TRACE("(Connection == ^%p)\n", Connection);
1485 if (SecIsValidHandle(&Connection->ctx))
1487 DeleteSecurityContext(&Connection->ctx);
1488 SecInvalidateHandle(&Connection->ctx);
1490 rpcrt4_conn_close(Connection);
1491 return RPC_S_OK;
1494 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1495 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1496 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1498 const struct connection_ops *ops;
1499 RpcConnection* NewConnection;
1501 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1502 if (!ops)
1504 FIXME("not supported for protseq %s\n", Protseq);
1505 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1508 NewConnection = ops->alloc();
1509 NewConnection->Next = NULL;
1510 NewConnection->server_binding = NULL;
1511 NewConnection->server = server;
1512 NewConnection->ops = ops;
1513 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1514 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1515 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1516 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1517 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1518 NewConnection->NextCallId = 1;
1520 SecInvalidateHandle(&NewConnection->ctx);
1521 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1522 NewConnection->attr = 0;
1523 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1524 NewConnection->AuthInfo = AuthInfo;
1525 NewConnection->encryption_auth_len = 0;
1526 NewConnection->signature_auth_len = 0;
1527 if (QOS) RpcQualityOfService_AddRef(QOS);
1528 NewConnection->QOS = QOS;
1530 list_init(&NewConnection->conn_pool_entry);
1531 NewConnection->async_state = NULL;
1533 TRACE("connection: %p\n", NewConnection);
1534 *Connection = NewConnection;
1536 return RPC_S_OK;
1540 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1542 RPC_STATUS err;
1544 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1545 rpcrt4_conn_get_name(OldConnection),
1546 OldConnection->NetworkAddr,
1547 OldConnection->Endpoint, NULL,
1548 OldConnection->AuthInfo, OldConnection->QOS);
1549 if (err == RPC_S_OK)
1550 rpcrt4_conn_handoff(OldConnection, *Connection);
1551 return err;
1554 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1556 TRACE("connection: %p\n", Connection);
1558 RPCRT4_CloseConnection(Connection);
1559 RPCRT4_strfree(Connection->Endpoint);
1560 RPCRT4_strfree(Connection->NetworkAddr);
1561 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1562 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1563 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1565 /* server-only */
1566 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
1568 HeapFree(GetProcessHeap(), 0, Connection);
1569 return RPC_S_OK;
1572 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1573 size_t *tower_size,
1574 const char *protseq,
1575 const char *networkaddr,
1576 const char *endpoint)
1578 twr_empty_floor_t *protocol_floor;
1579 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1581 *tower_size = 0;
1583 if (!protseq_ops)
1584 return RPC_S_INVALID_RPC_PROTSEQ;
1586 if (!tower_data)
1588 *tower_size = sizeof(*protocol_floor);
1589 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1590 return RPC_S_OK;
1593 protocol_floor = (twr_empty_floor_t *)tower_data;
1594 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1595 protocol_floor->protid = protseq_ops->epm_protocols[0];
1596 protocol_floor->count_rhs = 0;
1598 tower_data += sizeof(*protocol_floor);
1600 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1601 if (!*tower_size)
1602 return EPT_S_NOT_REGISTERED;
1604 *tower_size += sizeof(*protocol_floor);
1606 return RPC_S_OK;
1609 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1610 size_t tower_size,
1611 char **protseq,
1612 char **networkaddr,
1613 char **endpoint)
1615 const twr_empty_floor_t *protocol_floor;
1616 const twr_empty_floor_t *floor4;
1617 const struct connection_ops *protseq_ops = NULL;
1618 RPC_STATUS status;
1619 int i;
1621 if (tower_size < sizeof(*protocol_floor))
1622 return EPT_S_NOT_REGISTERED;
1624 protocol_floor = (const twr_empty_floor_t *)tower_data;
1625 tower_data += sizeof(*protocol_floor);
1626 tower_size -= sizeof(*protocol_floor);
1627 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1628 (protocol_floor->count_rhs > tower_size))
1629 return EPT_S_NOT_REGISTERED;
1630 tower_data += protocol_floor->count_rhs;
1631 tower_size -= protocol_floor->count_rhs;
1633 floor4 = (const twr_empty_floor_t *)tower_data;
1634 if ((tower_size < sizeof(*floor4)) ||
1635 (floor4->count_lhs != sizeof(floor4->protid)))
1636 return EPT_S_NOT_REGISTERED;
1638 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1639 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1640 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1642 protseq_ops = &conn_protseq_list[i];
1643 break;
1646 if (!protseq_ops)
1647 return EPT_S_NOT_REGISTERED;
1649 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1651 if ((status == RPC_S_OK) && protseq)
1653 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1654 strcpy(*protseq, protseq_ops->name);
1657 return status;
1660 /***********************************************************************
1661 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1663 * Checks if the given protocol sequence is known by the RPC system.
1664 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1667 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1669 char ps[0x10];
1671 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1672 ps, sizeof ps, NULL, NULL);
1673 if (rpcrt4_get_conn_protseq_ops(ps))
1674 return RPC_S_OK;
1676 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1678 return RPC_S_INVALID_RPC_PROTSEQ;
1681 /***********************************************************************
1682 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1684 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1686 UNICODE_STRING protseqW;
1688 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1690 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1691 RtlFreeUnicodeString(&protseqW);
1692 return ret;
1694 return RPC_S_OUT_OF_MEMORY;