push c669110d8ebd04a85775e86a8310fabd7dc4466e
[wine/hacks.git] / dlls / rpcrt4 / rpc_transport.c
blob367ca35771534cb779fde8659269d3ae2e77f8bf
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 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection);
95 /**** ncacn_np support ****/
97 typedef struct _RpcConnection_np
99 RpcConnection common;
100 HANDLE pipe;
101 OVERLAPPED ovl;
102 BOOL listening;
103 } RpcConnection_np;
105 static RpcConnection *rpcrt4_conn_np_alloc(void)
107 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
108 if (npc)
110 npc->pipe = NULL;
111 memset(&npc->ovl, 0, sizeof(npc->ovl));
112 npc->listening = FALSE;
114 return &npc->common;
117 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
119 if (npc->listening)
120 return RPC_S_OK;
122 npc->listening = TRUE;
123 for (;;)
125 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
126 return RPC_S_OK;
128 switch(GetLastError())
130 case ERROR_PIPE_CONNECTED:
131 SetEvent(npc->ovl.hEvent);
132 return RPC_S_OK;
133 case ERROR_IO_PENDING:
134 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
135 return RPC_S_OK;
136 case ERROR_NO_DATA_DETECTED:
137 /* client has disconnected, retry */
138 DisconnectNamedPipe( npc->pipe );
139 break;
140 default:
141 npc->listening = FALSE;
142 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
143 return RPC_S_OUT_OF_RESOURCES;
148 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
150 RpcConnection_np *npc = (RpcConnection_np *) Connection;
151 TRACE("listening on %s\n", pname);
153 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
154 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
155 PIPE_UNLIMITED_INSTANCES,
156 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
157 if (npc->pipe == INVALID_HANDLE_VALUE) {
158 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
159 if (GetLastError() == ERROR_FILE_EXISTS)
160 return RPC_S_DUPLICATE_ENDPOINT;
161 else
162 return RPC_S_CANT_CREATE_ENDPOINT;
165 memset(&npc->ovl, 0, sizeof(npc->ovl));
166 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
168 /* Note: we don't call ConnectNamedPipe here because it must be done in the
169 * server thread as the thread must be alertable */
170 return RPC_S_OK;
173 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
175 RpcConnection_np *npc = (RpcConnection_np *) Connection;
176 HANDLE pipe;
177 DWORD err, dwMode;
179 TRACE("connecting to %s\n", pname);
181 while (TRUE) {
182 DWORD dwFlags = 0;
183 if (Connection->QOS)
185 dwFlags = SECURITY_SQOS_PRESENT;
186 switch (Connection->QOS->qos->ImpersonationType)
188 case RPC_C_IMP_LEVEL_DEFAULT:
189 /* FIXME: what to do here? */
190 break;
191 case RPC_C_IMP_LEVEL_ANONYMOUS:
192 dwFlags |= SECURITY_ANONYMOUS;
193 break;
194 case RPC_C_IMP_LEVEL_IDENTIFY:
195 dwFlags |= SECURITY_IDENTIFICATION;
196 break;
197 case RPC_C_IMP_LEVEL_IMPERSONATE:
198 dwFlags |= SECURITY_IMPERSONATION;
199 break;
200 case RPC_C_IMP_LEVEL_DELEGATE:
201 dwFlags |= SECURITY_DELEGATION;
202 break;
204 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
205 dwFlags |= SECURITY_CONTEXT_TRACKING;
207 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
208 OPEN_EXISTING, dwFlags, 0);
209 if (pipe != INVALID_HANDLE_VALUE) break;
210 err = GetLastError();
211 if (err == ERROR_PIPE_BUSY) {
212 TRACE("connection failed, error=%x\n", err);
213 return RPC_S_SERVER_TOO_BUSY;
215 if (!wait || !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 && GetLastError() == ERROR_MORE_DATA)
386 ret = TRUE;
387 if (!ret || !bytes_read)
388 break;
389 bytes_left -= bytes_read;
390 buf += bytes_read;
392 return ret ? count : -1;
395 static int rpcrt4_conn_np_write(RpcConnection *Connection,
396 const void *buffer, unsigned int count)
398 RpcConnection_np *npc = (RpcConnection_np *) Connection;
399 const char *buf = buffer;
400 BOOL ret = TRUE;
401 unsigned int bytes_left = count;
403 while (bytes_left)
405 DWORD bytes_written;
406 ret = WriteFile(npc->pipe, buf, bytes_left, &bytes_written, NULL);
407 if (!ret || !bytes_written)
408 break;
409 bytes_left -= bytes_written;
410 buf += bytes_written;
412 return ret ? count : -1;
415 static int rpcrt4_conn_np_close(RpcConnection *Connection)
417 RpcConnection_np *npc = (RpcConnection_np *) Connection;
418 if (npc->pipe) {
419 FlushFileBuffers(npc->pipe);
420 CloseHandle(npc->pipe);
421 npc->pipe = 0;
423 if (npc->ovl.hEvent) {
424 CloseHandle(npc->ovl.hEvent);
425 npc->ovl.hEvent = 0;
427 return 0;
430 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
432 /* FIXME: implement when named pipe writes use overlapped I/O */
435 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
437 /* FIXME: implement when named pipe writes use overlapped I/O */
438 return -1;
441 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
442 const char *networkaddr,
443 const char *endpoint)
445 twr_empty_floor_t *smb_floor;
446 twr_empty_floor_t *nb_floor;
447 size_t size;
448 size_t networkaddr_size;
449 size_t endpoint_size;
451 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
453 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
454 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
455 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
457 if (!tower_data)
458 return size;
460 smb_floor = (twr_empty_floor_t *)tower_data;
462 tower_data += sizeof(*smb_floor);
464 smb_floor->count_lhs = sizeof(smb_floor->protid);
465 smb_floor->protid = EPM_PROTOCOL_SMB;
466 smb_floor->count_rhs = endpoint_size;
468 if (endpoint)
469 memcpy(tower_data, endpoint, endpoint_size);
470 else
471 tower_data[0] = 0;
472 tower_data += endpoint_size;
474 nb_floor = (twr_empty_floor_t *)tower_data;
476 tower_data += sizeof(*nb_floor);
478 nb_floor->count_lhs = sizeof(nb_floor->protid);
479 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
480 nb_floor->count_rhs = networkaddr_size;
482 if (networkaddr)
483 memcpy(tower_data, networkaddr, networkaddr_size);
484 else
485 tower_data[0] = 0;
487 return size;
490 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
491 size_t tower_size,
492 char **networkaddr,
493 char **endpoint)
495 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
496 const twr_empty_floor_t *nb_floor;
498 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
500 if (tower_size < sizeof(*smb_floor))
501 return EPT_S_NOT_REGISTERED;
503 tower_data += sizeof(*smb_floor);
504 tower_size -= sizeof(*smb_floor);
506 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
507 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
508 (smb_floor->count_rhs > tower_size) ||
509 (tower_data[smb_floor->count_rhs - 1] != '\0'))
510 return EPT_S_NOT_REGISTERED;
512 if (endpoint)
514 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
515 if (!*endpoint)
516 return RPC_S_OUT_OF_RESOURCES;
517 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
519 tower_data += smb_floor->count_rhs;
520 tower_size -= smb_floor->count_rhs;
522 if (tower_size < sizeof(*nb_floor))
523 return EPT_S_NOT_REGISTERED;
525 nb_floor = (const twr_empty_floor_t *)tower_data;
527 tower_data += sizeof(*nb_floor);
528 tower_size -= sizeof(*nb_floor);
530 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
531 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
532 (nb_floor->count_rhs > tower_size) ||
533 (tower_data[nb_floor->count_rhs - 1] != '\0'))
534 return EPT_S_NOT_REGISTERED;
536 if (networkaddr)
538 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
539 if (!*networkaddr)
541 if (endpoint)
543 I_RpcFree(*endpoint);
544 *endpoint = NULL;
546 return RPC_S_OUT_OF_RESOURCES;
548 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
551 return RPC_S_OK;
554 typedef struct _RpcServerProtseq_np
556 RpcServerProtseq common;
557 HANDLE mgr_event;
558 } RpcServerProtseq_np;
560 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
562 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
563 if (ps)
564 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
565 return &ps->common;
568 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
570 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
571 SetEvent(npps->mgr_event);
574 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
576 HANDLE *objs = prev_array;
577 RpcConnection_np *conn;
578 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
580 EnterCriticalSection(&protseq->cs);
582 /* open and count connections */
583 *count = 1;
584 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
585 while (conn) {
586 rpcrt4_conn_listen_pipe(conn);
587 if (conn->ovl.hEvent)
588 (*count)++;
589 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
592 /* make array of connections */
593 if (objs)
594 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
595 else
596 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
597 if (!objs)
599 ERR("couldn't allocate objs\n");
600 LeaveCriticalSection(&protseq->cs);
601 return NULL;
604 objs[0] = npps->mgr_event;
605 *count = 1;
606 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
607 while (conn) {
608 if ((objs[*count] = conn->ovl.hEvent))
609 (*count)++;
610 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
612 LeaveCriticalSection(&protseq->cs);
613 return objs;
616 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
618 HeapFree(GetProcessHeap(), 0, array);
621 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
623 HANDLE b_handle;
624 HANDLE *objs = wait_array;
625 DWORD res;
626 RpcConnection *cconn;
627 RpcConnection_np *conn;
629 if (!objs)
630 return -1;
634 /* an alertable wait isn't strictly necessary, but due to our
635 * overlapped I/O implementation in Wine we need to free some memory
636 * by the file user APC being called, even if no completion routine was
637 * specified at the time of starting the async operation */
638 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
639 } while (res == WAIT_IO_COMPLETION);
641 if (res == WAIT_OBJECT_0)
642 return 0;
643 else if (res == WAIT_FAILED)
645 ERR("wait failed with error %d\n", GetLastError());
646 return -1;
648 else
650 b_handle = objs[res - WAIT_OBJECT_0];
651 /* find which connection got a RPC */
652 EnterCriticalSection(&protseq->cs);
653 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
654 while (conn) {
655 if (b_handle == conn->ovl.hEvent) break;
656 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
658 cconn = NULL;
659 if (conn)
660 RPCRT4_SpawnConnection(&cconn, &conn->common);
661 else
662 ERR("failed to locate connection for handle %p\n", b_handle);
663 LeaveCriticalSection(&protseq->cs);
664 if (cconn)
666 RPCRT4_new_client(cconn);
667 return 1;
669 else return -1;
673 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
674 const char *networkaddr,
675 const char *endpoint)
677 twr_empty_floor_t *pipe_floor;
678 size_t size;
679 size_t endpoint_size;
681 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
683 endpoint_size = strlen(endpoint) + 1;
684 size = sizeof(*pipe_floor) + endpoint_size;
686 if (!tower_data)
687 return size;
689 pipe_floor = (twr_empty_floor_t *)tower_data;
691 tower_data += sizeof(*pipe_floor);
693 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
694 pipe_floor->protid = EPM_PROTOCOL_PIPE;
695 pipe_floor->count_rhs = endpoint_size;
697 memcpy(tower_data, endpoint, 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 #ifdef HAVE_SOCKETPAIR
741 typedef struct _RpcConnection_tcp
743 RpcConnection common;
744 int sock;
745 int cancel_fds[2];
746 } RpcConnection_tcp;
748 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
750 RpcConnection_tcp *tcpc;
751 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
752 if (tcpc == NULL)
753 return NULL;
754 tcpc->sock = -1;
755 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
757 ERR("socketpair() failed: %s\n", strerror(errno));
758 HeapFree(GetProcessHeap(), 0, tcpc);
759 return NULL;
761 return &tcpc->common;
764 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
766 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
767 int sock;
768 int ret;
769 struct addrinfo *ai;
770 struct addrinfo *ai_cur;
771 struct addrinfo hints;
773 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
775 if (tcpc->sock != -1)
776 return RPC_S_OK;
778 hints.ai_flags = 0;
779 hints.ai_family = PF_UNSPEC;
780 hints.ai_socktype = SOCK_STREAM;
781 hints.ai_protocol = IPPROTO_TCP;
782 hints.ai_addrlen = 0;
783 hints.ai_addr = NULL;
784 hints.ai_canonname = NULL;
785 hints.ai_next = NULL;
787 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
788 if (ret)
790 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
791 Connection->Endpoint, gai_strerror(ret));
792 return RPC_S_SERVER_UNAVAILABLE;
795 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
797 int val;
799 if (TRACE_ON(rpc))
801 char host[256];
802 char service[256];
803 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
804 host, sizeof(host), service, sizeof(service),
805 NI_NUMERICHOST | NI_NUMERICSERV);
806 TRACE("trying %s:%s\n", host, service);
809 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
810 if (sock == -1)
812 WARN("socket() failed: %s\n", strerror(errno));
813 continue;
816 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
818 WARN("connect() failed: %s\n", strerror(errno));
819 closesocket(sock);
820 continue;
823 /* RPC depends on having minimal latency so disable the Nagle algorithm */
824 val = 1;
825 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
826 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
828 tcpc->sock = sock;
830 freeaddrinfo(ai);
831 TRACE("connected\n");
832 return RPC_S_OK;
835 freeaddrinfo(ai);
836 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
837 return RPC_S_SERVER_UNAVAILABLE;
840 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
842 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
843 int sock;
844 int ret;
845 struct addrinfo *ai;
846 struct addrinfo *ai_cur;
847 struct addrinfo hints;
848 RpcConnection *first_connection = NULL;
850 TRACE("(%p, %s)\n", protseq, endpoint);
852 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
853 hints.ai_family = PF_UNSPEC;
854 hints.ai_socktype = SOCK_STREAM;
855 hints.ai_protocol = IPPROTO_TCP;
856 hints.ai_addrlen = 0;
857 hints.ai_addr = NULL;
858 hints.ai_canonname = NULL;
859 hints.ai_next = NULL;
861 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
862 if (ret)
864 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
865 gai_strerror(ret));
866 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
867 return RPC_S_INVALID_ENDPOINT_FORMAT;
868 return RPC_S_CANT_CREATE_ENDPOINT;
871 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
873 RpcConnection_tcp *tcpc;
874 RPC_STATUS create_status;
876 if (TRACE_ON(rpc))
878 char host[256];
879 char service[256];
880 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
881 host, sizeof(host), service, sizeof(service),
882 NI_NUMERICHOST | NI_NUMERICSERV);
883 TRACE("trying %s:%s\n", host, service);
886 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
887 if (sock == -1)
889 WARN("socket() failed: %s\n", strerror(errno));
890 status = RPC_S_CANT_CREATE_ENDPOINT;
891 continue;
894 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
895 if (ret < 0)
897 WARN("bind failed: %s\n", strerror(errno));
898 closesocket(sock);
899 if (errno == EADDRINUSE)
900 status = RPC_S_DUPLICATE_ENDPOINT;
901 else
902 status = RPC_S_CANT_CREATE_ENDPOINT;
903 continue;
905 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
906 protseq->Protseq, NULL,
907 endpoint, NULL, NULL, NULL);
908 if (create_status != RPC_S_OK)
910 closesocket(sock);
911 status = create_status;
912 continue;
915 tcpc->sock = sock;
916 ret = listen(sock, protseq->MaxCalls);
917 if (ret < 0)
919 WARN("listen failed: %s\n", strerror(errno));
920 RPCRT4_DestroyConnection(&tcpc->common);
921 status = RPC_S_OUT_OF_RESOURCES;
922 continue;
924 /* need a non-blocking socket, otherwise accept() has a potential
925 * race-condition (poll() says it is readable, connection drops,
926 * and accept() blocks until the next connection comes...)
928 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
929 if (ret < 0)
931 WARN("couldn't make socket non-blocking, error %d\n", ret);
932 RPCRT4_DestroyConnection(&tcpc->common);
933 status = RPC_S_OUT_OF_RESOURCES;
934 continue;
937 tcpc->common.Next = first_connection;
938 first_connection = &tcpc->common;
941 freeaddrinfo(ai);
943 /* if at least one connection was created for an endpoint then
944 * return success */
945 if (first_connection)
947 RpcConnection *conn;
949 /* find last element in list */
950 for (conn = first_connection; conn->Next; conn = conn->Next)
953 EnterCriticalSection(&protseq->cs);
954 conn->Next = protseq->conn;
955 protseq->conn = first_connection;
956 LeaveCriticalSection(&protseq->cs);
958 TRACE("listening on %s\n", endpoint);
959 return RPC_S_OK;
962 ERR("couldn't listen on port %s\n", endpoint);
963 return status;
966 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
968 int ret;
969 struct sockaddr_in address;
970 socklen_t addrsize;
971 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
972 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
974 addrsize = sizeof(address);
975 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
976 if (ret < 0)
978 ERR("Failed to accept a TCP connection: error %d\n", ret);
979 return RPC_S_OUT_OF_RESOURCES;
981 /* reset to blocking behaviour */
982 fcntl(ret, F_SETFL, 0);
983 client->sock = ret;
984 TRACE("Accepted a new TCP connection\n");
985 return RPC_S_OK;
988 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
989 void *buffer, unsigned int count)
991 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
992 int bytes_read = 0;
995 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
996 if (!r)
997 return -1;
998 else if (r > 0)
999 bytes_read += r;
1000 else if (errno != EAGAIN)
1002 WARN("recv() failed: %s\n", strerror(errno));
1003 return -1;
1005 else
1007 struct pollfd pfds[2];
1008 pfds[0].fd = tcpc->sock;
1009 pfds[0].events = POLLIN;
1010 pfds[1].fd = tcpc->cancel_fds[0];
1011 pfds[1].events = POLLIN;
1012 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1014 ERR("poll() failed: %s\n", strerror(errno));
1015 return -1;
1017 if (pfds[1].revents & POLLIN) /* canceled */
1019 char dummy;
1020 read(pfds[1].fd, &dummy, sizeof(dummy));
1021 return -1;
1024 } while (bytes_read != count);
1025 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1026 return bytes_read;
1029 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1030 const void *buffer, unsigned int count)
1032 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1033 int bytes_written = 0;
1036 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1037 if (r >= 0)
1038 bytes_written += r;
1039 else if (errno != EAGAIN)
1040 return -1;
1041 else
1043 struct pollfd pfd;
1044 pfd.fd = tcpc->sock;
1045 pfd.events = POLLOUT;
1046 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1048 ERR("poll() failed: %s\n", strerror(errno));
1049 return -1;
1052 } while (bytes_written != count);
1053 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1054 return bytes_written;
1057 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1059 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1061 TRACE("%d\n", tcpc->sock);
1063 if (tcpc->sock != -1)
1064 closesocket(tcpc->sock);
1065 tcpc->sock = -1;
1066 close(tcpc->cancel_fds[0]);
1067 close(tcpc->cancel_fds[1]);
1068 return 0;
1071 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1073 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1074 char dummy = 1;
1076 TRACE("%p\n", Connection);
1078 write(tcpc->cancel_fds[1], &dummy, 1);
1081 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1083 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1084 struct pollfd pfds[2];
1086 TRACE("%p\n", Connection);
1088 pfds[0].fd = tcpc->sock;
1089 pfds[0].events = POLLIN;
1090 pfds[1].fd = tcpc->cancel_fds[0];
1091 pfds[1].events = POLLIN;
1092 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1094 ERR("poll() failed: %s\n", strerror(errno));
1095 return -1;
1097 if (pfds[1].revents & POLLIN) /* canceled */
1099 char dummy;
1100 read(pfds[1].fd, &dummy, sizeof(dummy));
1101 return -1;
1104 return 0;
1107 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1108 const char *networkaddr,
1109 const char *endpoint)
1111 twr_tcp_floor_t *tcp_floor;
1112 twr_ipv4_floor_t *ipv4_floor;
1113 struct addrinfo *ai;
1114 struct addrinfo hints;
1115 int ret;
1116 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1118 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1120 if (!tower_data)
1121 return size;
1123 tcp_floor = (twr_tcp_floor_t *)tower_data;
1124 tower_data += sizeof(*tcp_floor);
1126 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1128 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1129 tcp_floor->protid = EPM_PROTOCOL_TCP;
1130 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1132 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1133 ipv4_floor->protid = EPM_PROTOCOL_IP;
1134 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1136 hints.ai_flags = AI_NUMERICHOST;
1137 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1138 hints.ai_family = PF_INET;
1139 hints.ai_socktype = SOCK_STREAM;
1140 hints.ai_protocol = IPPROTO_TCP;
1141 hints.ai_addrlen = 0;
1142 hints.ai_addr = NULL;
1143 hints.ai_canonname = NULL;
1144 hints.ai_next = NULL;
1146 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1147 if (ret)
1149 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1150 if (ret)
1152 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1153 return 0;
1157 if (ai->ai_family == PF_INET)
1159 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1160 tcp_floor->port = sin->sin_port;
1161 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1163 else
1165 ERR("unexpected protocol family %d\n", ai->ai_family);
1166 return 0;
1169 freeaddrinfo(ai);
1171 return size;
1174 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1175 size_t tower_size,
1176 char **networkaddr,
1177 char **endpoint)
1179 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1180 const twr_ipv4_floor_t *ipv4_floor;
1181 struct in_addr in_addr;
1183 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1185 if (tower_size < sizeof(*tcp_floor))
1186 return EPT_S_NOT_REGISTERED;
1188 tower_data += sizeof(*tcp_floor);
1189 tower_size -= sizeof(*tcp_floor);
1191 if (tower_size < sizeof(*ipv4_floor))
1192 return EPT_S_NOT_REGISTERED;
1194 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1196 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1197 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1198 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1199 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1200 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1201 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1202 return EPT_S_NOT_REGISTERED;
1204 if (endpoint)
1206 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1207 if (!*endpoint)
1208 return RPC_S_OUT_OF_RESOURCES;
1209 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1212 if (networkaddr)
1214 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1215 if (!*networkaddr)
1217 if (endpoint)
1219 I_RpcFree(*endpoint);
1220 *endpoint = NULL;
1222 return RPC_S_OUT_OF_RESOURCES;
1224 in_addr.s_addr = ipv4_floor->ipv4addr;
1225 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1227 ERR("inet_ntop: %s\n", strerror(errno));
1228 I_RpcFree(*networkaddr);
1229 *networkaddr = NULL;
1230 if (endpoint)
1232 I_RpcFree(*endpoint);
1233 *endpoint = NULL;
1235 return EPT_S_NOT_REGISTERED;
1239 return RPC_S_OK;
1242 typedef struct _RpcServerProtseq_sock
1244 RpcServerProtseq common;
1245 int mgr_event_rcv;
1246 int mgr_event_snd;
1247 } RpcServerProtseq_sock;
1249 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1251 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1252 if (ps)
1254 int fds[2];
1255 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1257 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1258 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1259 ps->mgr_event_rcv = fds[0];
1260 ps->mgr_event_snd = fds[1];
1262 else
1264 ERR("socketpair failed with error %s\n", strerror(errno));
1265 HeapFree(GetProcessHeap(), 0, ps);
1266 return NULL;
1269 return &ps->common;
1272 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1274 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1275 char dummy = 1;
1276 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1279 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1281 struct pollfd *poll_info = prev_array;
1282 RpcConnection_tcp *conn;
1283 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1285 EnterCriticalSection(&protseq->cs);
1287 /* open and count connections */
1288 *count = 1;
1289 conn = (RpcConnection_tcp *)protseq->conn;
1290 while (conn) {
1291 if (conn->sock != -1)
1292 (*count)++;
1293 conn = (RpcConnection_tcp *)conn->common.Next;
1296 /* make array of connections */
1297 if (poll_info)
1298 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1299 else
1300 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1301 if (!poll_info)
1303 ERR("couldn't allocate poll_info\n");
1304 LeaveCriticalSection(&protseq->cs);
1305 return NULL;
1308 poll_info[0].fd = sockps->mgr_event_rcv;
1309 poll_info[0].events = POLLIN;
1310 *count = 1;
1311 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1312 while (conn) {
1313 if (conn->sock != -1)
1315 poll_info[*count].fd = conn->sock;
1316 poll_info[*count].events = POLLIN;
1317 (*count)++;
1319 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1321 LeaveCriticalSection(&protseq->cs);
1322 return poll_info;
1325 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1327 HeapFree(GetProcessHeap(), 0, array);
1330 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1332 struct pollfd *poll_info = wait_array;
1333 int ret;
1334 unsigned int i;
1335 RpcConnection *cconn;
1336 RpcConnection_tcp *conn;
1338 if (!poll_info)
1339 return -1;
1341 ret = poll(poll_info, count, -1);
1342 if (ret < 0)
1344 ERR("poll failed with error %d\n", ret);
1345 return -1;
1348 for (i = 0; i < count; i++)
1349 if (poll_info[i].revents & POLLIN)
1351 /* RPC server event */
1352 if (i == 0)
1354 char dummy;
1355 read(poll_info[0].fd, &dummy, sizeof(dummy));
1356 return 0;
1359 /* find which connection got a RPC */
1360 EnterCriticalSection(&protseq->cs);
1361 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1362 while (conn) {
1363 if (poll_info[i].fd == conn->sock) break;
1364 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1366 cconn = NULL;
1367 if (conn)
1368 RPCRT4_SpawnConnection(&cconn, &conn->common);
1369 else
1370 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1371 LeaveCriticalSection(&protseq->cs);
1372 if (cconn)
1373 RPCRT4_new_client(cconn);
1374 else
1375 return -1;
1378 return 1;
1381 #endif /* HAVE_SOCKETPAIR */
1383 static const struct connection_ops conn_protseq_list[] = {
1384 { "ncacn_np",
1385 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1386 rpcrt4_conn_np_alloc,
1387 rpcrt4_ncacn_np_open,
1388 rpcrt4_ncacn_np_handoff,
1389 rpcrt4_conn_np_read,
1390 rpcrt4_conn_np_write,
1391 rpcrt4_conn_np_close,
1392 rpcrt4_conn_np_cancel_call,
1393 rpcrt4_conn_np_wait_for_incoming_data,
1394 rpcrt4_ncacn_np_get_top_of_tower,
1395 rpcrt4_ncacn_np_parse_top_of_tower,
1397 { "ncalrpc",
1398 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1399 rpcrt4_conn_np_alloc,
1400 rpcrt4_ncalrpc_open,
1401 rpcrt4_ncalrpc_handoff,
1402 rpcrt4_conn_np_read,
1403 rpcrt4_conn_np_write,
1404 rpcrt4_conn_np_close,
1405 rpcrt4_conn_np_cancel_call,
1406 rpcrt4_conn_np_wait_for_incoming_data,
1407 rpcrt4_ncalrpc_get_top_of_tower,
1408 rpcrt4_ncalrpc_parse_top_of_tower,
1410 #ifdef HAVE_SOCKETPAIR
1411 { "ncacn_ip_tcp",
1412 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1413 rpcrt4_conn_tcp_alloc,
1414 rpcrt4_ncacn_ip_tcp_open,
1415 rpcrt4_conn_tcp_handoff,
1416 rpcrt4_conn_tcp_read,
1417 rpcrt4_conn_tcp_write,
1418 rpcrt4_conn_tcp_close,
1419 rpcrt4_conn_tcp_cancel_call,
1420 rpcrt4_conn_tcp_wait_for_incoming_data,
1421 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1422 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1424 #endif
1428 static const struct protseq_ops protseq_list[] =
1431 "ncacn_np",
1432 rpcrt4_protseq_np_alloc,
1433 rpcrt4_protseq_np_signal_state_changed,
1434 rpcrt4_protseq_np_get_wait_array,
1435 rpcrt4_protseq_np_free_wait_array,
1436 rpcrt4_protseq_np_wait_for_new_connection,
1437 rpcrt4_protseq_ncacn_np_open_endpoint,
1440 "ncalrpc",
1441 rpcrt4_protseq_np_alloc,
1442 rpcrt4_protseq_np_signal_state_changed,
1443 rpcrt4_protseq_np_get_wait_array,
1444 rpcrt4_protseq_np_free_wait_array,
1445 rpcrt4_protseq_np_wait_for_new_connection,
1446 rpcrt4_protseq_ncalrpc_open_endpoint,
1448 #ifdef HAVE_SOCKETPAIR
1450 "ncacn_ip_tcp",
1451 rpcrt4_protseq_sock_alloc,
1452 rpcrt4_protseq_sock_signal_state_changed,
1453 rpcrt4_protseq_sock_get_wait_array,
1454 rpcrt4_protseq_sock_free_wait_array,
1455 rpcrt4_protseq_sock_wait_for_new_connection,
1456 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1458 #endif
1461 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1463 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1465 unsigned int i;
1466 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1467 if (!strcmp(protseq_list[i].name, protseq))
1468 return &protseq_list[i];
1469 return NULL;
1472 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1474 unsigned int i;
1475 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1476 if (!strcmp(conn_protseq_list[i].name, protseq))
1477 return &conn_protseq_list[i];
1478 return NULL;
1481 /**** interface to rest of code ****/
1483 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1485 TRACE("(Connection == ^%p)\n", Connection);
1487 assert(!Connection->server);
1488 return Connection->ops->open_connection_client(Connection);
1491 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1493 TRACE("(Connection == ^%p)\n", Connection);
1494 if (SecIsValidHandle(&Connection->ctx))
1496 DeleteSecurityContext(&Connection->ctx);
1497 SecInvalidateHandle(&Connection->ctx);
1499 rpcrt4_conn_close(Connection);
1500 return RPC_S_OK;
1503 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1504 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1505 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1507 const struct connection_ops *ops;
1508 RpcConnection* NewConnection;
1510 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1511 if (!ops)
1513 FIXME("not supported for protseq %s\n", Protseq);
1514 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1517 NewConnection = ops->alloc();
1518 NewConnection->Next = NULL;
1519 NewConnection->server_binding = NULL;
1520 NewConnection->server = server;
1521 NewConnection->ops = ops;
1522 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1523 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1524 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1525 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1526 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1527 NewConnection->NextCallId = 1;
1529 SecInvalidateHandle(&NewConnection->ctx);
1530 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1531 NewConnection->attr = 0;
1532 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1533 NewConnection->AuthInfo = AuthInfo;
1534 NewConnection->encryption_auth_len = 0;
1535 NewConnection->signature_auth_len = 0;
1536 if (QOS) RpcQualityOfService_AddRef(QOS);
1537 NewConnection->QOS = QOS;
1539 list_init(&NewConnection->conn_pool_entry);
1540 NewConnection->async_state = NULL;
1542 TRACE("connection: %p\n", NewConnection);
1543 *Connection = NewConnection;
1545 return RPC_S_OK;
1548 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1550 RPC_STATUS err;
1552 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1553 rpcrt4_conn_get_name(OldConnection),
1554 OldConnection->NetworkAddr,
1555 OldConnection->Endpoint, NULL,
1556 OldConnection->AuthInfo, OldConnection->QOS);
1557 if (err == RPC_S_OK)
1558 rpcrt4_conn_handoff(OldConnection, *Connection);
1559 return err;
1562 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1564 TRACE("connection: %p\n", Connection);
1566 RPCRT4_CloseConnection(Connection);
1567 RPCRT4_strfree(Connection->Endpoint);
1568 RPCRT4_strfree(Connection->NetworkAddr);
1569 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1570 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1571 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1573 /* server-only */
1574 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
1576 HeapFree(GetProcessHeap(), 0, Connection);
1577 return RPC_S_OK;
1580 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1581 size_t *tower_size,
1582 const char *protseq,
1583 const char *networkaddr,
1584 const char *endpoint)
1586 twr_empty_floor_t *protocol_floor;
1587 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1589 *tower_size = 0;
1591 if (!protseq_ops)
1592 return RPC_S_INVALID_RPC_PROTSEQ;
1594 if (!tower_data)
1596 *tower_size = sizeof(*protocol_floor);
1597 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1598 return RPC_S_OK;
1601 protocol_floor = (twr_empty_floor_t *)tower_data;
1602 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1603 protocol_floor->protid = protseq_ops->epm_protocols[0];
1604 protocol_floor->count_rhs = 0;
1606 tower_data += sizeof(*protocol_floor);
1608 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1609 if (!*tower_size)
1610 return EPT_S_NOT_REGISTERED;
1612 *tower_size += sizeof(*protocol_floor);
1614 return RPC_S_OK;
1617 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1618 size_t tower_size,
1619 char **protseq,
1620 char **networkaddr,
1621 char **endpoint)
1623 const twr_empty_floor_t *protocol_floor;
1624 const twr_empty_floor_t *floor4;
1625 const struct connection_ops *protseq_ops = NULL;
1626 RPC_STATUS status;
1627 unsigned int i;
1629 if (tower_size < sizeof(*protocol_floor))
1630 return EPT_S_NOT_REGISTERED;
1632 protocol_floor = (const twr_empty_floor_t *)tower_data;
1633 tower_data += sizeof(*protocol_floor);
1634 tower_size -= sizeof(*protocol_floor);
1635 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1636 (protocol_floor->count_rhs > tower_size))
1637 return EPT_S_NOT_REGISTERED;
1638 tower_data += protocol_floor->count_rhs;
1639 tower_size -= protocol_floor->count_rhs;
1641 floor4 = (const twr_empty_floor_t *)tower_data;
1642 if ((tower_size < sizeof(*floor4)) ||
1643 (floor4->count_lhs != sizeof(floor4->protid)))
1644 return EPT_S_NOT_REGISTERED;
1646 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1647 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1648 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1650 protseq_ops = &conn_protseq_list[i];
1651 break;
1654 if (!protseq_ops)
1655 return EPT_S_NOT_REGISTERED;
1657 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1659 if ((status == RPC_S_OK) && protseq)
1661 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1662 strcpy(*protseq, protseq_ops->name);
1665 return status;
1668 /***********************************************************************
1669 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1671 * Checks if the given protocol sequence is known by the RPC system.
1672 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1675 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1677 char ps[0x10];
1679 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1680 ps, sizeof ps, NULL, NULL);
1681 if (rpcrt4_get_conn_protseq_ops(ps))
1682 return RPC_S_OK;
1684 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1686 return RPC_S_INVALID_RPC_PROTSEQ;
1689 /***********************************************************************
1690 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1692 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1694 UNICODE_STRING protseqW;
1696 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1698 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1699 RtlFreeUnicodeString(&protseqW);
1700 return ret;
1702 return RPC_S_OUT_OF_MEMORY;