msi: Change the property variant if the types don't match.
[wine.git] / dlls / rpcrt4 / rpc_transport.c
blob085953711ddb8a8007b17063826c9975c30a8051
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 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
69 #include "windef.h"
70 #include "winbase.h"
71 #include "winnls.h"
72 #include "winerror.h"
73 #include "winternl.h"
74 #include "wine/unicode.h"
76 #include "rpc.h"
77 #include "rpcndr.h"
79 #include "wine/debug.h"
81 #include "rpc_binding.h"
82 #include "rpc_message.h"
83 #include "rpc_server.h"
84 #include "epm_towers.h"
86 #ifndef SOL_TCP
87 # define SOL_TCP IPPROTO_TCP
88 #endif
90 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
92 /**** ncacn_np support ****/
94 typedef struct _RpcConnection_np
96 RpcConnection common;
97 HANDLE pipe;
98 OVERLAPPED ovl;
99 BOOL listening;
100 } RpcConnection_np;
102 static RpcConnection *rpcrt4_conn_np_alloc(void)
104 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
105 if (npc)
107 npc->pipe = NULL;
108 memset(&npc->ovl, 0, sizeof(npc->ovl));
109 npc->listening = FALSE;
111 return &npc->common;
114 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
116 if (npc->listening)
117 return RPC_S_OK;
119 npc->listening = TRUE;
120 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
121 return RPC_S_OK;
123 if (GetLastError() == ERROR_PIPE_CONNECTED) {
124 SetEvent(npc->ovl.hEvent);
125 return RPC_S_OK;
127 if (GetLastError() == ERROR_IO_PENDING) {
128 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
129 return RPC_S_OK;
131 npc->listening = FALSE;
132 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
133 return RPC_S_OUT_OF_RESOURCES;
136 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
138 RpcConnection_np *npc = (RpcConnection_np *) Connection;
139 TRACE("listening on %s\n", pname);
141 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
142 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
143 PIPE_UNLIMITED_INSTANCES,
144 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
145 if (npc->pipe == INVALID_HANDLE_VALUE) {
146 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
147 if (GetLastError() == ERROR_FILE_EXISTS)
148 return RPC_S_DUPLICATE_ENDPOINT;
149 else
150 return RPC_S_CANT_CREATE_ENDPOINT;
153 memset(&npc->ovl, 0, sizeof(npc->ovl));
154 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
156 /* Note: we don't call ConnectNamedPipe here because it must be done in the
157 * server thread as the thread must be alertable */
158 return RPC_S_OK;
161 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
163 RpcConnection_np *npc = (RpcConnection_np *) Connection;
164 HANDLE pipe;
165 DWORD err, dwMode;
167 TRACE("connecting to %s\n", pname);
169 while (TRUE) {
170 DWORD dwFlags = 0;
171 if (Connection->QOS)
173 dwFlags = SECURITY_SQOS_PRESENT;
174 switch (Connection->QOS->qos->ImpersonationType)
176 case RPC_C_IMP_LEVEL_DEFAULT:
177 /* FIXME: what to do here? */
178 break;
179 case RPC_C_IMP_LEVEL_ANONYMOUS:
180 dwFlags |= SECURITY_ANONYMOUS;
181 break;
182 case RPC_C_IMP_LEVEL_IDENTIFY:
183 dwFlags |= SECURITY_IDENTIFICATION;
184 break;
185 case RPC_C_IMP_LEVEL_IMPERSONATE:
186 dwFlags |= SECURITY_IMPERSONATION;
187 break;
188 case RPC_C_IMP_LEVEL_DELEGATE:
189 dwFlags |= SECURITY_DELEGATION;
190 break;
192 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
193 dwFlags |= SECURITY_CONTEXT_TRACKING;
195 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
196 OPEN_EXISTING, dwFlags, 0);
197 if (pipe != INVALID_HANDLE_VALUE) break;
198 err = GetLastError();
199 if (err == ERROR_PIPE_BUSY) {
200 TRACE("connection failed, error=%x\n", err);
201 return RPC_S_SERVER_TOO_BUSY;
203 if (!wait)
204 return RPC_S_SERVER_UNAVAILABLE;
205 if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
206 err = GetLastError();
207 WARN("connection failed, error=%x\n", err);
208 return RPC_S_SERVER_UNAVAILABLE;
212 /* success */
213 memset(&npc->ovl, 0, sizeof(npc->ovl));
214 /* pipe is connected; change to message-read mode. */
215 dwMode = PIPE_READMODE_MESSAGE;
216 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
217 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
218 npc->pipe = pipe;
220 return RPC_S_OK;
223 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
225 RpcConnection_np *npc = (RpcConnection_np *) Connection;
226 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
227 RPC_STATUS r;
228 LPSTR pname;
230 /* already connected? */
231 if (npc->pipe)
232 return RPC_S_OK;
234 /* protseq=ncalrpc: supposed to use NT LPC ports,
235 * but we'll implement it with named pipes for now */
236 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
237 strcat(strcpy(pname, prefix), Connection->Endpoint);
238 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
239 I_RpcFree(pname);
241 return r;
244 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
246 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
247 RPC_STATUS r;
248 LPSTR pname;
249 RpcConnection *Connection;
251 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
252 endpoint, NULL, NULL, NULL);
253 if (r != RPC_S_OK)
254 return r;
256 /* protseq=ncalrpc: supposed to use NT LPC ports,
257 * but we'll implement it with named pipes for now */
258 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
259 strcat(strcpy(pname, prefix), Connection->Endpoint);
260 r = rpcrt4_conn_create_pipe(Connection, pname);
261 I_RpcFree(pname);
263 EnterCriticalSection(&protseq->cs);
264 Connection->Next = protseq->conn;
265 protseq->conn = Connection;
266 LeaveCriticalSection(&protseq->cs);
268 return r;
271 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
273 RpcConnection_np *npc = (RpcConnection_np *) Connection;
274 static const char prefix[] = "\\\\.";
275 RPC_STATUS r;
276 LPSTR pname;
278 /* already connected? */
279 if (npc->pipe)
280 return RPC_S_OK;
282 /* protseq=ncacn_np: named pipes */
283 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
284 strcat(strcpy(pname, prefix), Connection->Endpoint);
285 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
286 I_RpcFree(pname);
288 return r;
291 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
293 static const char prefix[] = "\\\\.";
294 RPC_STATUS r;
295 LPSTR pname;
296 RpcConnection *Connection;
298 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
299 endpoint, NULL, NULL, NULL);
300 if (r != RPC_S_OK)
301 return r;
303 /* protseq=ncacn_np: named pipes */
304 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
305 strcat(strcpy(pname, prefix), Connection->Endpoint);
306 r = rpcrt4_conn_create_pipe(Connection, pname);
307 I_RpcFree(pname);
309 EnterCriticalSection(&protseq->cs);
310 Connection->Next = protseq->conn;
311 protseq->conn = Connection;
312 LeaveCriticalSection(&protseq->cs);
314 return r;
317 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
319 /* because of the way named pipes work, we'll transfer the connected pipe
320 * to the child, then reopen the server binding to continue listening */
322 new_npc->pipe = old_npc->pipe;
323 new_npc->ovl = old_npc->ovl;
324 old_npc->pipe = 0;
325 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
326 old_npc->listening = FALSE;
329 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
331 RPC_STATUS status;
332 LPSTR pname;
333 static const char prefix[] = "\\\\.";
335 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
337 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
338 strcat(strcpy(pname, prefix), old_conn->Endpoint);
339 status = rpcrt4_conn_create_pipe(old_conn, pname);
340 I_RpcFree(pname);
342 return status;
345 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
347 RPC_STATUS status;
348 LPSTR pname;
349 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
351 TRACE("%s\n", old_conn->Endpoint);
353 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
355 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
356 strcat(strcpy(pname, prefix), old_conn->Endpoint);
357 status = rpcrt4_conn_create_pipe(old_conn, pname);
358 I_RpcFree(pname);
360 return status;
363 static int rpcrt4_conn_np_read(RpcConnection *Connection,
364 void *buffer, unsigned int count)
366 RpcConnection_np *npc = (RpcConnection_np *) Connection;
367 char *buf = buffer;
368 BOOL ret = TRUE;
369 unsigned int bytes_left = count;
371 while (bytes_left)
373 DWORD bytes_read;
374 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
375 if (!ret || !bytes_read)
376 break;
377 bytes_left -= bytes_read;
378 buf += bytes_read;
380 return ret ? count : -1;
383 static int rpcrt4_conn_np_write(RpcConnection *Connection,
384 const void *buffer, unsigned int count)
386 RpcConnection_np *npc = (RpcConnection_np *) Connection;
387 const char *buf = buffer;
388 BOOL ret = TRUE;
389 unsigned int bytes_left = count;
391 while (bytes_left)
393 DWORD bytes_written;
394 ret = WriteFile(npc->pipe, buf, count, &bytes_written, NULL);
395 if (!ret || !bytes_written)
396 break;
397 bytes_left -= bytes_written;
398 buf += bytes_written;
400 return ret ? count : -1;
403 static int rpcrt4_conn_np_close(RpcConnection *Connection)
405 RpcConnection_np *npc = (RpcConnection_np *) Connection;
406 if (npc->pipe) {
407 FlushFileBuffers(npc->pipe);
408 CloseHandle(npc->pipe);
409 npc->pipe = 0;
411 if (npc->ovl.hEvent) {
412 CloseHandle(npc->ovl.hEvent);
413 npc->ovl.hEvent = 0;
415 return 0;
418 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
420 /* FIXME: implement when named pipe writes use overlapped I/O */
423 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
425 /* FIXME: implement when named pipe writes use overlapped I/O */
426 return -1;
429 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
430 const char *networkaddr,
431 const char *endpoint)
433 twr_empty_floor_t *smb_floor;
434 twr_empty_floor_t *nb_floor;
435 size_t size;
436 size_t networkaddr_size;
437 size_t endpoint_size;
439 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
441 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
442 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
443 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
445 if (!tower_data)
446 return size;
448 smb_floor = (twr_empty_floor_t *)tower_data;
450 tower_data += sizeof(*smb_floor);
452 smb_floor->count_lhs = sizeof(smb_floor->protid);
453 smb_floor->protid = EPM_PROTOCOL_SMB;
454 smb_floor->count_rhs = endpoint_size;
456 if (endpoint)
457 memcpy(tower_data, endpoint, endpoint_size);
458 else
459 tower_data[0] = 0;
460 tower_data += endpoint_size;
462 nb_floor = (twr_empty_floor_t *)tower_data;
464 tower_data += sizeof(*nb_floor);
466 nb_floor->count_lhs = sizeof(nb_floor->protid);
467 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
468 nb_floor->count_rhs = networkaddr_size;
470 if (networkaddr)
471 memcpy(tower_data, networkaddr, networkaddr_size);
472 else
473 tower_data[0] = 0;
474 tower_data += networkaddr_size;
476 return size;
479 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
480 size_t tower_size,
481 char **networkaddr,
482 char **endpoint)
484 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
485 const twr_empty_floor_t *nb_floor;
487 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
489 if (tower_size < sizeof(*smb_floor))
490 return EPT_S_NOT_REGISTERED;
492 tower_data += sizeof(*smb_floor);
493 tower_size -= sizeof(*smb_floor);
495 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
496 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
497 (smb_floor->count_rhs > tower_size))
498 return EPT_S_NOT_REGISTERED;
500 if (endpoint)
502 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
503 if (!*endpoint)
504 return RPC_S_OUT_OF_RESOURCES;
505 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
507 tower_data += smb_floor->count_rhs;
508 tower_size -= smb_floor->count_rhs;
510 if (tower_size < sizeof(*nb_floor))
511 return EPT_S_NOT_REGISTERED;
513 nb_floor = (const twr_empty_floor_t *)tower_data;
515 tower_data += sizeof(*nb_floor);
516 tower_size -= sizeof(*nb_floor);
518 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
519 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
520 (nb_floor->count_rhs > tower_size))
521 return EPT_S_NOT_REGISTERED;
523 if (networkaddr)
525 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
526 if (!*networkaddr)
528 if (endpoint)
530 I_RpcFree(*endpoint);
531 *endpoint = NULL;
533 return RPC_S_OUT_OF_RESOURCES;
535 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
538 return RPC_S_OK;
541 typedef struct _RpcServerProtseq_np
543 RpcServerProtseq common;
544 HANDLE mgr_event;
545 } RpcServerProtseq_np;
547 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
549 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
550 if (ps)
551 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
552 return &ps->common;
555 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
557 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
558 SetEvent(npps->mgr_event);
561 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
563 HANDLE *objs = prev_array;
564 RpcConnection_np *conn;
565 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
567 EnterCriticalSection(&protseq->cs);
569 /* open and count connections */
570 *count = 1;
571 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
572 while (conn) {
573 rpcrt4_conn_listen_pipe(conn);
574 if (conn->ovl.hEvent)
575 (*count)++;
576 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
579 /* make array of connections */
580 if (objs)
581 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
582 else
583 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
584 if (!objs)
586 ERR("couldn't allocate objs\n");
587 LeaveCriticalSection(&protseq->cs);
588 return NULL;
591 objs[0] = npps->mgr_event;
592 *count = 1;
593 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
594 while (conn) {
595 if ((objs[*count] = conn->ovl.hEvent))
596 (*count)++;
597 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
599 LeaveCriticalSection(&protseq->cs);
600 return objs;
603 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
605 HeapFree(GetProcessHeap(), 0, array);
608 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
610 HANDLE b_handle;
611 HANDLE *objs = wait_array;
612 DWORD res;
613 RpcConnection *cconn;
614 RpcConnection_np *conn;
616 if (!objs)
617 return -1;
621 /* an alertable wait isn't strictly necessary, but due to our
622 * overlapped I/O implementation in Wine we need to free some memory
623 * by the file user APC being called, even if no completion routine was
624 * specified at the time of starting the async operation */
625 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
626 } while (res == WAIT_IO_COMPLETION);
628 if (res == WAIT_OBJECT_0)
629 return 0;
630 else if (res == WAIT_FAILED)
632 ERR("wait failed with error %d\n", GetLastError());
633 return -1;
635 else
637 b_handle = objs[res - WAIT_OBJECT_0];
638 /* find which connection got a RPC */
639 EnterCriticalSection(&protseq->cs);
640 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
641 while (conn) {
642 if (b_handle == conn->ovl.hEvent) break;
643 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
645 cconn = NULL;
646 if (conn)
647 RPCRT4_SpawnConnection(&cconn, &conn->common);
648 else
649 ERR("failed to locate connection for handle %p\n", b_handle);
650 LeaveCriticalSection(&protseq->cs);
651 if (cconn)
653 RPCRT4_new_client(cconn);
654 return 1;
656 else return -1;
660 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
661 const char *networkaddr,
662 const char *endpoint)
664 twr_empty_floor_t *pipe_floor;
665 size_t size;
666 size_t endpoint_size;
668 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
670 endpoint_size = strlen(networkaddr) + 1;
671 size = sizeof(*pipe_floor) + endpoint_size;
673 if (!tower_data)
674 return size;
676 pipe_floor = (twr_empty_floor_t *)tower_data;
678 tower_data += sizeof(*pipe_floor);
680 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
681 pipe_floor->protid = EPM_PROTOCOL_SMB;
682 pipe_floor->count_rhs = endpoint_size;
684 memcpy(tower_data, endpoint, endpoint_size);
685 tower_data += endpoint_size;
687 return size;
690 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
691 size_t tower_size,
692 char **networkaddr,
693 char **endpoint)
695 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
697 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
699 *networkaddr = NULL;
700 *endpoint = NULL;
702 if (tower_size < sizeof(*pipe_floor))
703 return EPT_S_NOT_REGISTERED;
705 tower_data += sizeof(*pipe_floor);
706 tower_size -= sizeof(*pipe_floor);
708 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
709 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
710 (pipe_floor->count_rhs > tower_size))
711 return EPT_S_NOT_REGISTERED;
713 if (endpoint)
715 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
716 if (!*endpoint)
717 return RPC_S_OUT_OF_RESOURCES;
718 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
721 return RPC_S_OK;
724 /**** ncacn_ip_tcp support ****/
726 typedef struct _RpcConnection_tcp
728 RpcConnection common;
729 int sock;
730 int cancel_fds[2];
731 } RpcConnection_tcp;
733 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
735 RpcConnection_tcp *tcpc;
736 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
737 if (tcpc == NULL)
738 return NULL;
739 tcpc->sock = -1;
740 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
742 ERR("socketpair() failed: %s\n", strerror(errno));
743 HeapFree(GetProcessHeap(), 0, tcpc);
744 return NULL;
746 return &tcpc->common;
749 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
751 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
752 int sock;
753 int ret;
754 struct addrinfo *ai;
755 struct addrinfo *ai_cur;
756 struct addrinfo hints;
758 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
760 if (tcpc->sock != -1)
761 return RPC_S_OK;
763 hints.ai_flags = 0;
764 hints.ai_family = PF_UNSPEC;
765 hints.ai_socktype = SOCK_STREAM;
766 hints.ai_protocol = IPPROTO_TCP;
767 hints.ai_addrlen = 0;
768 hints.ai_addr = NULL;
769 hints.ai_canonname = NULL;
770 hints.ai_next = NULL;
772 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
773 if (ret)
775 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
776 Connection->Endpoint, gai_strerror(ret));
777 return RPC_S_SERVER_UNAVAILABLE;
780 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
782 int val;
784 if (TRACE_ON(rpc))
786 char host[256];
787 char service[256];
788 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
789 host, sizeof(host), service, sizeof(service),
790 NI_NUMERICHOST | NI_NUMERICSERV);
791 TRACE("trying %s:%s\n", host, service);
794 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
795 if (sock == -1)
797 WARN("socket() failed: %s\n", strerror(errno));
798 continue;
801 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
803 WARN("connect() failed: %s\n", strerror(errno));
804 close(sock);
805 continue;
808 /* RPC depends on having minimal latency so disable the Nagle algorithm */
809 val = 1;
810 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
811 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
813 tcpc->sock = sock;
815 freeaddrinfo(ai);
816 TRACE("connected\n");
817 return RPC_S_OK;
820 freeaddrinfo(ai);
821 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
822 return RPC_S_SERVER_UNAVAILABLE;
825 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
827 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
828 int sock;
829 int ret;
830 struct addrinfo *ai;
831 struct addrinfo *ai_cur;
832 struct addrinfo hints;
833 RpcConnection *first_connection = NULL;
835 TRACE("(%p, %s)\n", protseq, endpoint);
837 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
838 hints.ai_family = PF_UNSPEC;
839 hints.ai_socktype = SOCK_STREAM;
840 hints.ai_protocol = IPPROTO_TCP;
841 hints.ai_addrlen = 0;
842 hints.ai_addr = NULL;
843 hints.ai_canonname = NULL;
844 hints.ai_next = NULL;
846 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
847 if (ret)
849 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
850 gai_strerror(ret));
851 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
852 return RPC_S_INVALID_ENDPOINT_FORMAT;
853 return RPC_S_CANT_CREATE_ENDPOINT;
856 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
858 RpcConnection_tcp *tcpc;
859 RPC_STATUS create_status;
861 if (TRACE_ON(rpc))
863 char host[256];
864 char service[256];
865 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
866 host, sizeof(host), service, sizeof(service),
867 NI_NUMERICHOST | NI_NUMERICSERV);
868 TRACE("trying %s:%s\n", host, service);
871 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
872 if (sock == -1)
874 WARN("socket() failed: %s\n", strerror(errno));
875 status = RPC_S_CANT_CREATE_ENDPOINT;
876 continue;
879 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
880 if (ret < 0)
882 WARN("bind failed: %s\n", strerror(errno));
883 close(sock);
884 if (errno == EADDRINUSE)
885 status = RPC_S_DUPLICATE_ENDPOINT;
886 else
887 status = RPC_S_CANT_CREATE_ENDPOINT;
888 continue;
890 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
891 protseq->Protseq, NULL,
892 endpoint, NULL, NULL, NULL);
893 if (create_status != RPC_S_OK)
895 close(sock);
896 status = create_status;
897 continue;
900 tcpc->sock = sock;
901 ret = listen(sock, protseq->MaxCalls);
902 if (ret < 0)
904 WARN("listen failed: %s\n", strerror(errno));
905 RPCRT4_DestroyConnection(&tcpc->common);
906 status = RPC_S_OUT_OF_RESOURCES;
907 continue;
909 /* need a non-blocking socket, otherwise accept() has a potential
910 * race-condition (poll() says it is readable, connection drops,
911 * and accept() blocks until the next connection comes...)
913 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
914 if (ret < 0)
916 WARN("couldn't make socket non-blocking, error %d\n", ret);
917 RPCRT4_DestroyConnection(&tcpc->common);
918 status = RPC_S_OUT_OF_RESOURCES;
919 continue;
922 tcpc->common.Next = first_connection;
923 first_connection = &tcpc->common;
926 freeaddrinfo(ai);
928 /* if at least one connection was created for an endpoint then
929 * return success */
930 if (first_connection)
932 RpcConnection *conn;
934 /* find last element in list */
935 for (conn = first_connection; conn->Next; conn = conn->Next)
938 EnterCriticalSection(&protseq->cs);
939 conn->Next = protseq->conn;
940 protseq->conn = first_connection;
941 LeaveCriticalSection(&protseq->cs);
943 TRACE("listening on %s\n", endpoint);
944 return RPC_S_OK;
947 ERR("couldn't listen on port %s\n", endpoint);
948 return status;
951 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
953 int ret;
954 struct sockaddr_in address;
955 socklen_t addrsize;
956 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
957 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
959 addrsize = sizeof(address);
960 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
961 if (ret < 0)
963 ERR("Failed to accept a TCP connection: error %d\n", ret);
964 return RPC_S_OUT_OF_RESOURCES;
966 /* reset to blocking behaviour */
967 fcntl(ret, F_SETFL, 0);
968 client->sock = ret;
969 TRACE("Accepted a new TCP connection\n");
970 return RPC_S_OK;
973 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
974 void *buffer, unsigned int count)
976 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
977 int bytes_read = 0;
980 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
981 if (!r)
982 return -1;
983 else if (r > 0)
984 bytes_read += r;
985 else if (errno != EAGAIN)
987 WARN("recv() failed: %s\n", strerror(errno));
988 return -1;
990 else
992 struct pollfd pfds[2];
993 pfds[0].fd = tcpc->sock;
994 pfds[0].events = POLLIN;
995 pfds[1].fd = tcpc->cancel_fds[0];
996 pfds[1].events = POLLIN;
997 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
999 ERR("poll() failed: %s\n", strerror(errno));
1000 return -1;
1002 if (pfds[1].revents & POLLIN) /* canceled */
1004 char dummy;
1005 read(pfds[1].fd, &dummy, sizeof(dummy));
1006 return -1;
1009 } while (bytes_read != count);
1010 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1011 return bytes_read;
1014 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1015 const void *buffer, unsigned int count)
1017 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1018 int bytes_written = 0;
1021 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1022 if (r >= 0)
1023 bytes_written += r;
1024 else if (errno != EAGAIN)
1025 return -1;
1026 else
1028 struct pollfd pfd;
1029 pfd.fd = tcpc->sock;
1030 pfd.events = POLLOUT;
1031 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1033 ERR("poll() failed: %s\n", strerror(errno));
1034 return -1;
1037 } while (bytes_written != count);
1038 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1039 return bytes_written;
1042 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1044 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1046 TRACE("%d\n", tcpc->sock);
1048 if (tcpc->sock != -1)
1049 close(tcpc->sock);
1050 tcpc->sock = -1;
1051 close(tcpc->cancel_fds[0]);
1052 close(tcpc->cancel_fds[1]);
1053 return 0;
1056 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1058 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1059 char dummy = 1;
1061 TRACE("%p\n", Connection);
1063 write(tcpc->cancel_fds[1], &dummy, 1);
1066 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1068 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1069 struct pollfd pfds[2];
1071 TRACE("%p\n", Connection);
1073 pfds[0].fd = tcpc->sock;
1074 pfds[0].events = POLLIN;
1075 pfds[1].fd = tcpc->cancel_fds[0];
1076 pfds[1].events = POLLIN;
1077 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1079 ERR("poll() failed: %s\n", strerror(errno));
1080 return -1;
1082 if (pfds[1].revents & POLLIN) /* canceled */
1084 char dummy;
1085 read(pfds[1].fd, &dummy, sizeof(dummy));
1086 return -1;
1089 return 0;
1092 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1093 const char *networkaddr,
1094 const char *endpoint)
1096 twr_tcp_floor_t *tcp_floor;
1097 twr_ipv4_floor_t *ipv4_floor;
1098 struct addrinfo *ai;
1099 struct addrinfo hints;
1100 int ret;
1101 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1103 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1105 if (!tower_data)
1106 return size;
1108 tcp_floor = (twr_tcp_floor_t *)tower_data;
1109 tower_data += sizeof(*tcp_floor);
1111 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1113 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1114 tcp_floor->protid = EPM_PROTOCOL_TCP;
1115 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1117 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1118 ipv4_floor->protid = EPM_PROTOCOL_IP;
1119 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1121 hints.ai_flags = AI_NUMERICHOST;
1122 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1123 hints.ai_family = PF_INET;
1124 hints.ai_socktype = SOCK_STREAM;
1125 hints.ai_protocol = IPPROTO_TCP;
1126 hints.ai_addrlen = 0;
1127 hints.ai_addr = NULL;
1128 hints.ai_canonname = NULL;
1129 hints.ai_next = NULL;
1131 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1132 if (ret)
1134 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1135 if (ret)
1137 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1138 return 0;
1142 if (ai->ai_family == PF_INET)
1144 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1145 tcp_floor->port = sin->sin_port;
1146 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1148 else
1150 ERR("unexpected protocol family %d\n", ai->ai_family);
1151 return 0;
1154 freeaddrinfo(ai);
1156 return size;
1159 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1160 size_t tower_size,
1161 char **networkaddr,
1162 char **endpoint)
1164 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1165 const twr_ipv4_floor_t *ipv4_floor;
1166 struct in_addr in_addr;
1168 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1170 if (tower_size < sizeof(*tcp_floor))
1171 return EPT_S_NOT_REGISTERED;
1173 tower_data += sizeof(*tcp_floor);
1174 tower_size -= sizeof(*tcp_floor);
1176 if (tower_size < sizeof(*ipv4_floor))
1177 return EPT_S_NOT_REGISTERED;
1179 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1181 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1182 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1183 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1184 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1185 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1186 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1187 return EPT_S_NOT_REGISTERED;
1189 if (endpoint)
1191 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1192 if (!*endpoint)
1193 return RPC_S_OUT_OF_RESOURCES;
1194 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1197 if (networkaddr)
1199 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1200 if (!*networkaddr)
1202 if (endpoint)
1204 I_RpcFree(*endpoint);
1205 *endpoint = NULL;
1207 return RPC_S_OUT_OF_RESOURCES;
1209 in_addr.s_addr = ipv4_floor->ipv4addr;
1210 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1212 ERR("inet_ntop: %s\n", strerror(errno));
1213 I_RpcFree(*networkaddr);
1214 *networkaddr = NULL;
1215 if (endpoint)
1217 I_RpcFree(*endpoint);
1218 *endpoint = NULL;
1220 return EPT_S_NOT_REGISTERED;
1224 return RPC_S_OK;
1227 typedef struct _RpcServerProtseq_sock
1229 RpcServerProtseq common;
1230 int mgr_event_rcv;
1231 int mgr_event_snd;
1232 } RpcServerProtseq_sock;
1234 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1236 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1237 if (ps)
1239 int fds[2];
1240 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1242 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1243 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1244 ps->mgr_event_rcv = fds[0];
1245 ps->mgr_event_snd = fds[1];
1247 else
1249 ERR("socketpair failed with error %s\n", strerror(errno));
1250 HeapFree(GetProcessHeap(), 0, ps);
1251 return NULL;
1254 return &ps->common;
1257 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1259 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1260 char dummy = 1;
1261 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1264 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1266 struct pollfd *poll_info = prev_array;
1267 RpcConnection_tcp *conn;
1268 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1270 EnterCriticalSection(&protseq->cs);
1272 /* open and count connections */
1273 *count = 1;
1274 conn = (RpcConnection_tcp *)protseq->conn;
1275 while (conn) {
1276 if (conn->sock != -1)
1277 (*count)++;
1278 conn = (RpcConnection_tcp *)conn->common.Next;
1281 /* make array of connections */
1282 if (poll_info)
1283 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1284 else
1285 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1286 if (!poll_info)
1288 ERR("couldn't allocate poll_info\n");
1289 LeaveCriticalSection(&protseq->cs);
1290 return NULL;
1293 poll_info[0].fd = sockps->mgr_event_rcv;
1294 poll_info[0].events = POLLIN;
1295 *count = 1;
1296 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1297 while (conn) {
1298 if (conn->sock != -1)
1300 poll_info[*count].fd = conn->sock;
1301 poll_info[*count].events = POLLIN;
1302 (*count)++;
1304 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1306 LeaveCriticalSection(&protseq->cs);
1307 return poll_info;
1310 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1312 HeapFree(GetProcessHeap(), 0, array);
1315 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1317 struct pollfd *poll_info = wait_array;
1318 int ret, i;
1319 RpcConnection *cconn;
1320 RpcConnection_tcp *conn;
1322 if (!poll_info)
1323 return -1;
1325 ret = poll(poll_info, count, -1);
1326 if (ret < 0)
1328 ERR("poll failed with error %d\n", ret);
1329 return -1;
1332 for (i = 0; i < count; i++)
1333 if (poll_info[i].revents & POLLIN)
1335 /* RPC server event */
1336 if (i == 0)
1338 char dummy;
1339 read(poll_info[0].fd, &dummy, sizeof(dummy));
1340 return 0;
1343 /* find which connection got a RPC */
1344 EnterCriticalSection(&protseq->cs);
1345 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1346 while (conn) {
1347 if (poll_info[i].fd == conn->sock) break;
1348 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1350 cconn = NULL;
1351 if (conn)
1352 RPCRT4_SpawnConnection(&cconn, &conn->common);
1353 else
1354 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1355 LeaveCriticalSection(&protseq->cs);
1356 if (cconn)
1357 RPCRT4_new_client(cconn);
1358 else
1359 return -1;
1362 return 1;
1365 static const struct connection_ops conn_protseq_list[] = {
1366 { "ncacn_np",
1367 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1368 rpcrt4_conn_np_alloc,
1369 rpcrt4_ncacn_np_open,
1370 rpcrt4_ncacn_np_handoff,
1371 rpcrt4_conn_np_read,
1372 rpcrt4_conn_np_write,
1373 rpcrt4_conn_np_close,
1374 rpcrt4_conn_np_cancel_call,
1375 rpcrt4_conn_np_wait_for_incoming_data,
1376 rpcrt4_ncacn_np_get_top_of_tower,
1377 rpcrt4_ncacn_np_parse_top_of_tower,
1379 { "ncalrpc",
1380 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1381 rpcrt4_conn_np_alloc,
1382 rpcrt4_ncalrpc_open,
1383 rpcrt4_ncalrpc_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_ncalrpc_get_top_of_tower,
1390 rpcrt4_ncalrpc_parse_top_of_tower,
1392 { "ncacn_ip_tcp",
1393 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1394 rpcrt4_conn_tcp_alloc,
1395 rpcrt4_ncacn_ip_tcp_open,
1396 rpcrt4_conn_tcp_handoff,
1397 rpcrt4_conn_tcp_read,
1398 rpcrt4_conn_tcp_write,
1399 rpcrt4_conn_tcp_close,
1400 rpcrt4_conn_tcp_cancel_call,
1401 rpcrt4_conn_tcp_wait_for_incoming_data,
1402 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1403 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1408 static const struct protseq_ops protseq_list[] =
1411 "ncacn_np",
1412 rpcrt4_protseq_np_alloc,
1413 rpcrt4_protseq_np_signal_state_changed,
1414 rpcrt4_protseq_np_get_wait_array,
1415 rpcrt4_protseq_np_free_wait_array,
1416 rpcrt4_protseq_np_wait_for_new_connection,
1417 rpcrt4_protseq_ncacn_np_open_endpoint,
1420 "ncalrpc",
1421 rpcrt4_protseq_np_alloc,
1422 rpcrt4_protseq_np_signal_state_changed,
1423 rpcrt4_protseq_np_get_wait_array,
1424 rpcrt4_protseq_np_free_wait_array,
1425 rpcrt4_protseq_np_wait_for_new_connection,
1426 rpcrt4_protseq_ncalrpc_open_endpoint,
1429 "ncacn_ip_tcp",
1430 rpcrt4_protseq_sock_alloc,
1431 rpcrt4_protseq_sock_signal_state_changed,
1432 rpcrt4_protseq_sock_get_wait_array,
1433 rpcrt4_protseq_sock_free_wait_array,
1434 rpcrt4_protseq_sock_wait_for_new_connection,
1435 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1439 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1441 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1443 int i;
1444 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1445 if (!strcmp(protseq_list[i].name, protseq))
1446 return &protseq_list[i];
1447 return NULL;
1450 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1452 int i;
1453 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1454 if (!strcmp(conn_protseq_list[i].name, protseq))
1455 return &conn_protseq_list[i];
1456 return NULL;
1459 /**** interface to rest of code ****/
1461 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1463 TRACE("(Connection == ^%p)\n", Connection);
1465 assert(!Connection->server);
1466 return Connection->ops->open_connection_client(Connection);
1469 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1471 TRACE("(Connection == ^%p)\n", Connection);
1472 if (SecIsValidHandle(&Connection->ctx))
1474 DeleteSecurityContext(&Connection->ctx);
1475 SecInvalidateHandle(&Connection->ctx);
1477 rpcrt4_conn_close(Connection);
1478 return RPC_S_OK;
1481 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1482 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1483 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1485 const struct connection_ops *ops;
1486 RpcConnection* NewConnection;
1488 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1489 if (!ops)
1491 FIXME("not supported for protseq %s\n", Protseq);
1492 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1495 NewConnection = ops->alloc();
1496 NewConnection->Next = NULL;
1497 NewConnection->server_binding = NULL;
1498 NewConnection->server = server;
1499 NewConnection->ops = ops;
1500 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1501 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1502 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1503 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1504 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1505 NewConnection->NextCallId = 1;
1507 SecInvalidateHandle(&NewConnection->ctx);
1508 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1509 NewConnection->attr = 0;
1510 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1511 NewConnection->AuthInfo = AuthInfo;
1512 NewConnection->encryption_auth_len = 0;
1513 NewConnection->signature_auth_len = 0;
1514 if (QOS) RpcQualityOfService_AddRef(QOS);
1515 NewConnection->QOS = QOS;
1517 list_init(&NewConnection->conn_pool_entry);
1518 NewConnection->async_state = NULL;
1520 TRACE("connection: %p\n", NewConnection);
1521 *Connection = NewConnection;
1523 return RPC_S_OK;
1527 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1529 RPC_STATUS err;
1531 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1532 rpcrt4_conn_get_name(OldConnection),
1533 OldConnection->NetworkAddr,
1534 OldConnection->Endpoint, NULL,
1535 OldConnection->AuthInfo, OldConnection->QOS);
1536 if (err == RPC_S_OK)
1537 rpcrt4_conn_handoff(OldConnection, *Connection);
1538 return err;
1541 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1543 TRACE("connection: %p\n", Connection);
1545 RPCRT4_CloseConnection(Connection);
1546 RPCRT4_strfree(Connection->Endpoint);
1547 RPCRT4_strfree(Connection->NetworkAddr);
1548 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1549 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1550 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1552 /* server-only */
1553 if (Connection->server_binding) RPCRT4_DestroyBinding(Connection->server_binding);
1555 HeapFree(GetProcessHeap(), 0, Connection);
1556 return RPC_S_OK;
1559 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1560 size_t *tower_size,
1561 const char *protseq,
1562 const char *networkaddr,
1563 const char *endpoint)
1565 twr_empty_floor_t *protocol_floor;
1566 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1568 *tower_size = 0;
1570 if (!protseq_ops)
1571 return RPC_S_INVALID_RPC_PROTSEQ;
1573 if (!tower_data)
1575 *tower_size = sizeof(*protocol_floor);
1576 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1577 return RPC_S_OK;
1580 protocol_floor = (twr_empty_floor_t *)tower_data;
1581 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1582 protocol_floor->protid = protseq_ops->epm_protocols[0];
1583 protocol_floor->count_rhs = 0;
1585 tower_data += sizeof(*protocol_floor);
1587 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1588 if (!*tower_size)
1589 return EPT_S_NOT_REGISTERED;
1591 *tower_size += sizeof(*protocol_floor);
1593 return RPC_S_OK;
1596 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1597 size_t tower_size,
1598 char **protseq,
1599 char **networkaddr,
1600 char **endpoint)
1602 const twr_empty_floor_t *protocol_floor;
1603 const twr_empty_floor_t *floor4;
1604 const struct connection_ops *protseq_ops = NULL;
1605 RPC_STATUS status;
1606 int i;
1608 if (tower_size < sizeof(*protocol_floor))
1609 return EPT_S_NOT_REGISTERED;
1611 protocol_floor = (const twr_empty_floor_t *)tower_data;
1612 tower_data += sizeof(*protocol_floor);
1613 tower_size -= sizeof(*protocol_floor);
1614 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1615 (protocol_floor->count_rhs > tower_size))
1616 return EPT_S_NOT_REGISTERED;
1617 tower_data += protocol_floor->count_rhs;
1618 tower_size -= protocol_floor->count_rhs;
1620 floor4 = (const twr_empty_floor_t *)tower_data;
1621 if ((tower_size < sizeof(*floor4)) ||
1622 (floor4->count_lhs != sizeof(floor4->protid)))
1623 return EPT_S_NOT_REGISTERED;
1625 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1626 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1627 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1629 protseq_ops = &conn_protseq_list[i];
1630 break;
1633 if (!protseq_ops)
1634 return EPT_S_NOT_REGISTERED;
1636 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1638 if ((status == RPC_S_OK) && protseq)
1640 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1641 strcpy(*protseq, protseq_ops->name);
1644 return status;
1647 /***********************************************************************
1648 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1650 * Checks if the given protocol sequence is known by the RPC system.
1651 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1654 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1656 char ps[0x10];
1658 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1659 ps, sizeof ps, NULL, NULL);
1660 if (rpcrt4_get_conn_protseq_ops(ps))
1661 return RPC_S_OK;
1663 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1665 return RPC_S_INVALID_RPC_PROTSEQ;
1668 /***********************************************************************
1669 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1671 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1673 UNICODE_STRING protseqW;
1675 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1677 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1678 RtlFreeUnicodeString(&protseqW);
1679 return ret;
1681 return RPC_S_OUT_OF_MEMORY;