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
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
46 #ifdef HAVE_NETINET_TCP_H
47 # include <netinet/tcp.h>
49 #ifdef HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
55 #ifdef HAVE_SYS_POLL_H
64 #include "wine/unicode.h"
69 #include "wine/debug.h"
71 #include "rpc_binding.h"
72 #include "rpc_message.h"
73 #include "rpc_server.h"
74 #include "epm_towers.h"
77 # define SOL_TCP IPPROTO_TCP
80 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
82 /**** ncacn_np support ****/
84 typedef struct _RpcConnection_np
92 static RpcConnection *rpcrt4_conn_np_alloc(void)
94 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
98 memset(&npc->ovl, 0, sizeof(npc->ovl));
99 npc->listening = FALSE;
104 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
109 npc->listening = TRUE;
110 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
113 if (GetLastError() == ERROR_PIPE_CONNECTED) {
114 SetEvent(npc->ovl.hEvent);
117 if (GetLastError() == ERROR_IO_PENDING) {
118 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
121 npc->listening = FALSE;
122 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
123 return RPC_S_OUT_OF_RESOURCES;
126 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
128 RpcConnection_np *npc = (RpcConnection_np *) Connection;
129 TRACE("listening on %s\n", pname);
131 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
132 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
133 PIPE_UNLIMITED_INSTANCES,
134 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
135 if (npc->pipe == INVALID_HANDLE_VALUE) {
136 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
137 if (GetLastError() == ERROR_FILE_EXISTS)
138 return RPC_S_DUPLICATE_ENDPOINT;
140 return RPC_S_CANT_CREATE_ENDPOINT;
143 memset(&npc->ovl, 0, sizeof(npc->ovl));
144 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
146 /* Note: we don't call ConnectNamedPipe here because it must be done in the
147 * server thread as the thread must be alertable */
151 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
153 RpcConnection_np *npc = (RpcConnection_np *) Connection;
157 TRACE("connecting to %s\n", pname);
163 dwFlags = SECURITY_SQOS_PRESENT;
164 switch (Connection->QOS->qos->ImpersonationType)
166 case RPC_C_IMP_LEVEL_DEFAULT:
167 /* FIXME: what to do here? */
169 case RPC_C_IMP_LEVEL_ANONYMOUS:
170 dwFlags |= SECURITY_ANONYMOUS;
172 case RPC_C_IMP_LEVEL_IDENTIFY:
173 dwFlags |= SECURITY_IDENTIFICATION;
175 case RPC_C_IMP_LEVEL_IMPERSONATE:
176 dwFlags |= SECURITY_IMPERSONATION;
178 case RPC_C_IMP_LEVEL_DELEGATE:
179 dwFlags |= SECURITY_DELEGATION;
182 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
183 dwFlags |= SECURITY_CONTEXT_TRACKING;
185 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
186 OPEN_EXISTING, dwFlags, 0);
187 if (pipe != INVALID_HANDLE_VALUE) break;
188 err = GetLastError();
189 if (err == ERROR_PIPE_BUSY) {
190 TRACE("connection failed, error=%x\n", err);
191 return RPC_S_SERVER_TOO_BUSY;
194 return RPC_S_SERVER_UNAVAILABLE;
195 if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
196 err = GetLastError();
197 WARN("connection failed, error=%x\n", err);
198 return RPC_S_SERVER_UNAVAILABLE;
203 memset(&npc->ovl, 0, sizeof(npc->ovl));
204 /* pipe is connected; change to message-read mode. */
205 dwMode = PIPE_READMODE_MESSAGE;
206 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
207 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
213 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
215 RpcConnection_np *npc = (RpcConnection_np *) Connection;
216 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
220 /* already connected? */
224 /* protseq=ncalrpc: supposed to use NT LPC ports,
225 * but we'll implement it with named pipes for now */
226 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
227 strcat(strcpy(pname, prefix), Connection->Endpoint);
228 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
234 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
236 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
239 RpcConnection *Connection;
241 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
242 endpoint, NULL, NULL, NULL);
246 /* protseq=ncalrpc: supposed to use NT LPC ports,
247 * but we'll implement it with named pipes for now */
248 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
249 strcat(strcpy(pname, prefix), Connection->Endpoint);
250 r = rpcrt4_conn_create_pipe(Connection, pname);
253 EnterCriticalSection(&protseq->cs);
254 Connection->Next = protseq->conn;
255 protseq->conn = Connection;
256 LeaveCriticalSection(&protseq->cs);
261 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
263 RpcConnection_np *npc = (RpcConnection_np *) Connection;
264 static const char prefix[] = "\\\\.";
268 /* already connected? */
272 /* protseq=ncacn_np: named pipes */
273 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
274 strcat(strcpy(pname, prefix), Connection->Endpoint);
275 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
281 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
283 static const char prefix[] = "\\\\.";
286 RpcConnection *Connection;
288 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
289 endpoint, NULL, NULL, NULL);
293 /* protseq=ncacn_np: named pipes */
294 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
295 strcat(strcpy(pname, prefix), Connection->Endpoint);
296 r = rpcrt4_conn_create_pipe(Connection, pname);
299 EnterCriticalSection(&protseq->cs);
300 Connection->Next = protseq->conn;
301 protseq->conn = Connection;
302 LeaveCriticalSection(&protseq->cs);
307 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
309 /* because of the way named pipes work, we'll transfer the connected pipe
310 * to the child, then reopen the server binding to continue listening */
312 new_npc->pipe = old_npc->pipe;
313 new_npc->ovl = old_npc->ovl;
315 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
316 old_npc->listening = FALSE;
319 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
323 static const char prefix[] = "\\\\.";
325 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
327 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
328 strcat(strcpy(pname, prefix), old_conn->Endpoint);
329 status = rpcrt4_conn_create_pipe(old_conn, pname);
335 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
339 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
341 TRACE("%s\n", old_conn->Endpoint);
343 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
345 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
346 strcat(strcpy(pname, prefix), old_conn->Endpoint);
347 status = rpcrt4_conn_create_pipe(old_conn, pname);
353 static int rpcrt4_conn_np_read(RpcConnection *Connection,
354 void *buffer, unsigned int count)
356 RpcConnection_np *npc = (RpcConnection_np *) Connection;
359 unsigned int bytes_left = count;
364 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
365 if (!ret || !bytes_read)
367 bytes_left -= bytes_read;
370 return ret ? count : -1;
373 static int rpcrt4_conn_np_write(RpcConnection *Connection,
374 const void *buffer, unsigned int count)
376 RpcConnection_np *npc = (RpcConnection_np *) Connection;
377 const char *buf = buffer;
379 unsigned int bytes_left = count;
384 ret = WriteFile(npc->pipe, buf, count, &bytes_written, NULL);
385 if (!ret || !bytes_written)
387 bytes_left -= bytes_written;
388 buf += bytes_written;
390 return ret ? count : -1;
393 static int rpcrt4_conn_np_close(RpcConnection *Connection)
395 RpcConnection_np *npc = (RpcConnection_np *) Connection;
397 FlushFileBuffers(npc->pipe);
398 CloseHandle(npc->pipe);
401 if (npc->ovl.hEvent) {
402 CloseHandle(npc->ovl.hEvent);
408 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
410 /* FIXME: implement when named pipe writes use overlapped I/O */
413 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
415 /* FIXME: implement when named pipe writes use overlapped I/O */
419 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
420 const char *networkaddr,
421 const char *endpoint)
423 twr_empty_floor_t *smb_floor;
424 twr_empty_floor_t *nb_floor;
426 size_t networkaddr_size;
427 size_t endpoint_size;
429 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
431 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
432 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
433 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
438 smb_floor = (twr_empty_floor_t *)tower_data;
440 tower_data += sizeof(*smb_floor);
442 smb_floor->count_lhs = sizeof(smb_floor->protid);
443 smb_floor->protid = EPM_PROTOCOL_SMB;
444 smb_floor->count_rhs = endpoint_size;
447 memcpy(tower_data, endpoint, endpoint_size);
450 tower_data += endpoint_size;
452 nb_floor = (twr_empty_floor_t *)tower_data;
454 tower_data += sizeof(*nb_floor);
456 nb_floor->count_lhs = sizeof(nb_floor->protid);
457 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
458 nb_floor->count_rhs = networkaddr_size;
461 memcpy(tower_data, networkaddr, networkaddr_size);
464 tower_data += networkaddr_size;
469 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
474 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
475 const twr_empty_floor_t *nb_floor;
477 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
479 if (tower_size < sizeof(*smb_floor))
480 return EPT_S_NOT_REGISTERED;
482 tower_data += sizeof(*smb_floor);
483 tower_size -= sizeof(*smb_floor);
485 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
486 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
487 (smb_floor->count_rhs > tower_size))
488 return EPT_S_NOT_REGISTERED;
492 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
494 return RPC_S_OUT_OF_RESOURCES;
495 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
497 tower_data += smb_floor->count_rhs;
498 tower_size -= smb_floor->count_rhs;
500 if (tower_size < sizeof(*nb_floor))
501 return EPT_S_NOT_REGISTERED;
503 nb_floor = (const twr_empty_floor_t *)tower_data;
505 tower_data += sizeof(*nb_floor);
506 tower_size -= sizeof(*nb_floor);
508 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
509 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
510 (nb_floor->count_rhs > tower_size))
511 return EPT_S_NOT_REGISTERED;
515 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
520 I_RpcFree(*endpoint);
523 return RPC_S_OUT_OF_RESOURCES;
525 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
531 typedef struct _RpcServerProtseq_np
533 RpcServerProtseq common;
535 } RpcServerProtseq_np;
537 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
539 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
541 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
545 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
547 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
548 SetEvent(npps->mgr_event);
551 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
553 HANDLE *objs = prev_array;
554 RpcConnection_np *conn;
555 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
557 EnterCriticalSection(&protseq->cs);
559 /* open and count connections */
561 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
563 rpcrt4_conn_listen_pipe(conn);
564 if (conn->ovl.hEvent)
566 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
569 /* make array of connections */
571 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
573 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
576 ERR("couldn't allocate objs\n");
577 LeaveCriticalSection(&protseq->cs);
581 objs[0] = npps->mgr_event;
583 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
585 if ((objs[*count] = conn->ovl.hEvent))
587 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
589 LeaveCriticalSection(&protseq->cs);
593 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
595 HeapFree(GetProcessHeap(), 0, array);
598 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
601 HANDLE *objs = wait_array;
603 RpcConnection *cconn;
604 RpcConnection_np *conn;
611 /* an alertable wait isn't strictly necessary, but due to our
612 * overlapped I/O implementation in Wine we need to free some memory
613 * by the file user APC being called, even if no completion routine was
614 * specified at the time of starting the async operation */
615 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
616 } while (res == WAIT_IO_COMPLETION);
618 if (res == WAIT_OBJECT_0)
620 else if (res == WAIT_FAILED)
622 ERR("wait failed with error %d\n", GetLastError());
627 b_handle = objs[res - WAIT_OBJECT_0];
628 /* find which connection got a RPC */
629 EnterCriticalSection(&protseq->cs);
630 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
632 if (b_handle == conn->ovl.hEvent) break;
633 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
637 RPCRT4_SpawnConnection(&cconn, &conn->common);
639 ERR("failed to locate connection for handle %p\n", b_handle);
640 LeaveCriticalSection(&protseq->cs);
643 RPCRT4_new_client(cconn);
650 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
651 const char *networkaddr,
652 const char *endpoint)
654 twr_empty_floor_t *pipe_floor;
656 size_t endpoint_size;
658 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
660 endpoint_size = strlen(networkaddr) + 1;
661 size = sizeof(*pipe_floor) + endpoint_size;
666 pipe_floor = (twr_empty_floor_t *)tower_data;
668 tower_data += sizeof(*pipe_floor);
670 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
671 pipe_floor->protid = EPM_PROTOCOL_SMB;
672 pipe_floor->count_rhs = endpoint_size;
674 memcpy(tower_data, endpoint, endpoint_size);
675 tower_data += endpoint_size;
680 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
685 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
687 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
692 if (tower_size < sizeof(*pipe_floor))
693 return EPT_S_NOT_REGISTERED;
695 tower_data += sizeof(*pipe_floor);
696 tower_size -= sizeof(*pipe_floor);
698 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
699 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
700 (pipe_floor->count_rhs > tower_size))
701 return EPT_S_NOT_REGISTERED;
705 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
707 return RPC_S_OUT_OF_RESOURCES;
708 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
714 /**** ncacn_ip_tcp support ****/
716 typedef struct _RpcConnection_tcp
718 RpcConnection common;
723 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
725 RpcConnection_tcp *tcpc;
726 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
730 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
732 ERR("socketpair() failed: %s\n", strerror(errno));
733 HeapFree(GetProcessHeap(), 0, tcpc);
736 return &tcpc->common;
739 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
741 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
745 struct addrinfo *ai_cur;
746 struct addrinfo hints;
748 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
750 if (tcpc->sock != -1)
754 hints.ai_family = PF_UNSPEC;
755 hints.ai_socktype = SOCK_STREAM;
756 hints.ai_protocol = IPPROTO_TCP;
757 hints.ai_addrlen = 0;
758 hints.ai_addr = NULL;
759 hints.ai_canonname = NULL;
760 hints.ai_next = NULL;
762 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
765 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
766 Connection->Endpoint, gai_strerror(ret));
767 return RPC_S_SERVER_UNAVAILABLE;
770 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
778 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
779 host, sizeof(host), service, sizeof(service),
780 NI_NUMERICHOST | NI_NUMERICSERV);
781 TRACE("trying %s:%s\n", host, service);
784 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
787 WARN("socket() failed: %s\n", strerror(errno));
791 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
793 WARN("connect() failed: %s\n", strerror(errno));
798 /* RPC depends on having minimal latency so disable the Nagle algorithm */
800 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
801 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
806 TRACE("connected\n");
811 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
812 return RPC_S_SERVER_UNAVAILABLE;
815 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
817 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
821 struct addrinfo *ai_cur;
822 struct addrinfo hints;
823 RpcConnection *first_connection = NULL;
825 TRACE("(%p, %s)\n", protseq, endpoint);
827 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
828 hints.ai_family = PF_UNSPEC;
829 hints.ai_socktype = SOCK_STREAM;
830 hints.ai_protocol = IPPROTO_TCP;
831 hints.ai_addrlen = 0;
832 hints.ai_addr = NULL;
833 hints.ai_canonname = NULL;
834 hints.ai_next = NULL;
836 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
839 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
841 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
842 return RPC_S_INVALID_ENDPOINT_FORMAT;
843 return RPC_S_CANT_CREATE_ENDPOINT;
846 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
848 RpcConnection_tcp *tcpc;
849 RPC_STATUS create_status;
855 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
856 host, sizeof(host), service, sizeof(service),
857 NI_NUMERICHOST | NI_NUMERICSERV);
858 TRACE("trying %s:%s\n", host, service);
861 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
864 WARN("socket() failed: %s\n", strerror(errno));
865 status = RPC_S_CANT_CREATE_ENDPOINT;
869 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
872 WARN("bind failed: %s\n", strerror(errno));
874 if (errno == EADDRINUSE)
875 status = RPC_S_DUPLICATE_ENDPOINT;
877 status = RPC_S_CANT_CREATE_ENDPOINT;
880 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
881 protseq->Protseq, NULL,
882 endpoint, NULL, NULL, NULL);
883 if (create_status != RPC_S_OK)
886 status = create_status;
891 ret = listen(sock, protseq->MaxCalls);
894 WARN("listen failed: %s\n", strerror(errno));
895 RPCRT4_DestroyConnection(&tcpc->common);
896 status = RPC_S_OUT_OF_RESOURCES;
899 /* need a non-blocking socket, otherwise accept() has a potential
900 * race-condition (poll() says it is readable, connection drops,
901 * and accept() blocks until the next connection comes...)
903 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
906 WARN("couldn't make socket non-blocking, error %d\n", ret);
907 RPCRT4_DestroyConnection(&tcpc->common);
908 status = RPC_S_OUT_OF_RESOURCES;
912 tcpc->common.Next = first_connection;
913 first_connection = &tcpc->common;
918 /* if at least one connection was created for an endpoint then
920 if (first_connection)
924 /* find last element in list */
925 for (conn = first_connection; conn->Next; conn = conn->Next)
928 EnterCriticalSection(&protseq->cs);
929 conn->Next = protseq->conn;
930 protseq->conn = first_connection;
931 LeaveCriticalSection(&protseq->cs);
933 TRACE("listening on %s\n", endpoint);
937 ERR("couldn't listen on port %s\n", endpoint);
941 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
944 struct sockaddr_in address;
946 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
947 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
949 addrsize = sizeof(address);
950 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
953 ERR("Failed to accept a TCP connection: error %d\n", ret);
954 return RPC_S_OUT_OF_RESOURCES;
956 /* reset to blocking behaviour */
957 fcntl(ret, F_SETFL, 0);
959 TRACE("Accepted a new TCP connection\n");
963 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
964 void *buffer, unsigned int count)
966 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
970 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
975 else if (errno != EAGAIN)
977 WARN("recv() failed: %s\n", strerror(errno));
982 struct pollfd pfds[2];
983 pfds[0].fd = tcpc->sock;
984 pfds[0].events = POLLIN;
985 pfds[1].fd = tcpc->cancel_fds[0];
986 pfds[1].events = POLLIN;
987 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
989 ERR("poll() failed: %s\n", strerror(errno));
992 if (pfds[1].revents & POLLIN) /* canceled */
995 read(pfds[1].fd, &dummy, sizeof(dummy));
999 } while (bytes_read != count);
1000 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1004 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1005 const void *buffer, unsigned int count)
1007 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1008 int bytes_written = 0;
1011 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1014 else if (errno != EAGAIN)
1019 pfd.fd = tcpc->sock;
1020 pfd.events = POLLOUT;
1021 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1023 ERR("poll() failed: %s\n", strerror(errno));
1027 } while (bytes_written != count);
1028 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1029 return bytes_written;
1032 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1034 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1036 TRACE("%d\n", tcpc->sock);
1038 if (tcpc->sock != -1)
1041 close(tcpc->cancel_fds[0]);
1042 close(tcpc->cancel_fds[1]);
1046 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1048 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1051 TRACE("%p\n", Connection);
1053 write(tcpc->cancel_fds[1], &dummy, 1);
1056 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1058 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1059 struct pollfd pfds[2];
1061 TRACE("%p\n", Connection);
1063 pfds[0].fd = tcpc->sock;
1064 pfds[0].events = POLLIN;
1065 pfds[1].fd = tcpc->cancel_fds[0];
1066 pfds[1].events = POLLIN;
1067 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1069 ERR("poll() failed: %s\n", strerror(errno));
1072 if (pfds[1].revents & POLLIN) /* canceled */
1075 read(pfds[1].fd, &dummy, sizeof(dummy));
1082 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1083 const char *networkaddr,
1084 const char *endpoint)
1086 twr_tcp_floor_t *tcp_floor;
1087 twr_ipv4_floor_t *ipv4_floor;
1088 struct addrinfo *ai;
1089 struct addrinfo hints;
1091 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1093 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1098 tcp_floor = (twr_tcp_floor_t *)tower_data;
1099 tower_data += sizeof(*tcp_floor);
1101 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1103 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1104 tcp_floor->protid = EPM_PROTOCOL_TCP;
1105 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1107 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1108 ipv4_floor->protid = EPM_PROTOCOL_IP;
1109 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1111 hints.ai_flags = AI_NUMERICHOST;
1112 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1113 hints.ai_family = PF_INET;
1114 hints.ai_socktype = SOCK_STREAM;
1115 hints.ai_protocol = IPPROTO_TCP;
1116 hints.ai_addrlen = 0;
1117 hints.ai_addr = NULL;
1118 hints.ai_canonname = NULL;
1119 hints.ai_next = NULL;
1121 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1124 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1127 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1132 if (ai->ai_family == PF_INET)
1134 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1135 tcp_floor->port = sin->sin_port;
1136 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1140 ERR("unexpected protocol family %d\n", ai->ai_family);
1149 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1154 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1155 const twr_ipv4_floor_t *ipv4_floor;
1156 struct in_addr in_addr;
1158 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1160 if (tower_size < sizeof(*tcp_floor))
1161 return EPT_S_NOT_REGISTERED;
1163 tower_data += sizeof(*tcp_floor);
1164 tower_size -= sizeof(*tcp_floor);
1166 if (tower_size < sizeof(*ipv4_floor))
1167 return EPT_S_NOT_REGISTERED;
1169 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1171 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1172 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1173 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1174 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1175 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1176 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1177 return EPT_S_NOT_REGISTERED;
1181 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1183 return RPC_S_OUT_OF_RESOURCES;
1184 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1189 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1194 I_RpcFree(*endpoint);
1197 return RPC_S_OUT_OF_RESOURCES;
1199 in_addr.s_addr = ipv4_floor->ipv4addr;
1200 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1202 ERR("inet_ntop: %s\n", strerror(errno));
1203 I_RpcFree(*networkaddr);
1204 *networkaddr = NULL;
1207 I_RpcFree(*endpoint);
1210 return EPT_S_NOT_REGISTERED;
1217 typedef struct _RpcServerProtseq_sock
1219 RpcServerProtseq common;
1222 } RpcServerProtseq_sock;
1224 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1226 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1230 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1232 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1233 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1234 ps->mgr_event_rcv = fds[0];
1235 ps->mgr_event_snd = fds[1];
1239 ERR("socketpair failed with error %s\n", strerror(errno));
1240 HeapFree(GetProcessHeap(), 0, ps);
1247 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1249 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1251 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1254 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1256 struct pollfd *poll_info = prev_array;
1257 RpcConnection_tcp *conn;
1258 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1260 EnterCriticalSection(&protseq->cs);
1262 /* open and count connections */
1264 conn = (RpcConnection_tcp *)protseq->conn;
1266 if (conn->sock != -1)
1268 conn = (RpcConnection_tcp *)conn->common.Next;
1271 /* make array of connections */
1273 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1275 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1278 ERR("couldn't allocate poll_info\n");
1279 LeaveCriticalSection(&protseq->cs);
1283 poll_info[0].fd = sockps->mgr_event_rcv;
1284 poll_info[0].events = POLLIN;
1286 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1288 if (conn->sock != -1)
1290 poll_info[*count].fd = conn->sock;
1291 poll_info[*count].events = POLLIN;
1294 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1296 LeaveCriticalSection(&protseq->cs);
1300 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1302 HeapFree(GetProcessHeap(), 0, array);
1305 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1307 struct pollfd *poll_info = wait_array;
1309 RpcConnection *cconn;
1310 RpcConnection_tcp *conn;
1315 ret = poll(poll_info, count, -1);
1318 ERR("poll failed with error %d\n", ret);
1322 for (i = 0; i < count; i++)
1323 if (poll_info[i].revents & POLLIN)
1325 /* RPC server event */
1329 read(poll_info[0].fd, &dummy, sizeof(dummy));
1333 /* find which connection got a RPC */
1334 EnterCriticalSection(&protseq->cs);
1335 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1337 if (poll_info[i].fd == conn->sock) break;
1338 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1342 RPCRT4_SpawnConnection(&cconn, &conn->common);
1344 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1345 LeaveCriticalSection(&protseq->cs);
1347 RPCRT4_new_client(cconn);
1355 static const struct connection_ops conn_protseq_list[] = {
1357 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1358 rpcrt4_conn_np_alloc,
1359 rpcrt4_ncacn_np_open,
1360 rpcrt4_ncacn_np_handoff,
1361 rpcrt4_conn_np_read,
1362 rpcrt4_conn_np_write,
1363 rpcrt4_conn_np_close,
1364 rpcrt4_conn_np_cancel_call,
1365 rpcrt4_conn_np_wait_for_incoming_data,
1366 rpcrt4_ncacn_np_get_top_of_tower,
1367 rpcrt4_ncacn_np_parse_top_of_tower,
1370 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1371 rpcrt4_conn_np_alloc,
1372 rpcrt4_ncalrpc_open,
1373 rpcrt4_ncalrpc_handoff,
1374 rpcrt4_conn_np_read,
1375 rpcrt4_conn_np_write,
1376 rpcrt4_conn_np_close,
1377 rpcrt4_conn_np_cancel_call,
1378 rpcrt4_conn_np_wait_for_incoming_data,
1379 rpcrt4_ncalrpc_get_top_of_tower,
1380 rpcrt4_ncalrpc_parse_top_of_tower,
1383 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1384 rpcrt4_conn_tcp_alloc,
1385 rpcrt4_ncacn_ip_tcp_open,
1386 rpcrt4_conn_tcp_handoff,
1387 rpcrt4_conn_tcp_read,
1388 rpcrt4_conn_tcp_write,
1389 rpcrt4_conn_tcp_close,
1390 rpcrt4_conn_tcp_cancel_call,
1391 rpcrt4_conn_tcp_wait_for_incoming_data,
1392 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1393 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1398 static const struct protseq_ops protseq_list[] =
1402 rpcrt4_protseq_np_alloc,
1403 rpcrt4_protseq_np_signal_state_changed,
1404 rpcrt4_protseq_np_get_wait_array,
1405 rpcrt4_protseq_np_free_wait_array,
1406 rpcrt4_protseq_np_wait_for_new_connection,
1407 rpcrt4_protseq_ncacn_np_open_endpoint,
1411 rpcrt4_protseq_np_alloc,
1412 rpcrt4_protseq_np_signal_state_changed,
1413 rpcrt4_protseq_np_get_wait_array,
1414 rpcrt4_protseq_np_free_wait_array,
1415 rpcrt4_protseq_np_wait_for_new_connection,
1416 rpcrt4_protseq_ncalrpc_open_endpoint,
1420 rpcrt4_protseq_sock_alloc,
1421 rpcrt4_protseq_sock_signal_state_changed,
1422 rpcrt4_protseq_sock_get_wait_array,
1423 rpcrt4_protseq_sock_free_wait_array,
1424 rpcrt4_protseq_sock_wait_for_new_connection,
1425 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1429 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1431 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1434 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1435 if (!strcmp(protseq_list[i].name, protseq))
1436 return &protseq_list[i];
1440 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1443 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1444 if (!strcmp(conn_protseq_list[i].name, protseq))
1445 return &conn_protseq_list[i];
1449 /**** interface to rest of code ****/
1451 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1453 TRACE("(Connection == ^%p)\n", Connection);
1455 assert(!Connection->server);
1456 return Connection->ops->open_connection_client(Connection);
1459 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1461 TRACE("(Connection == ^%p)\n", Connection);
1462 if (SecIsValidHandle(&Connection->ctx))
1464 DeleteSecurityContext(&Connection->ctx);
1465 SecInvalidateHandle(&Connection->ctx);
1467 rpcrt4_conn_close(Connection);
1471 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1472 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1473 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1475 const struct connection_ops *ops;
1476 RpcConnection* NewConnection;
1478 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1481 FIXME("not supported for protseq %s\n", Protseq);
1482 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1485 NewConnection = ops->alloc();
1486 NewConnection->Next = NULL;
1487 NewConnection->server_binding = NULL;
1488 NewConnection->server = server;
1489 NewConnection->ops = ops;
1490 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1491 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1492 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1493 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1494 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1495 NewConnection->NextCallId = 1;
1497 SecInvalidateHandle(&NewConnection->ctx);
1498 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1499 NewConnection->attr = 0;
1500 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1501 NewConnection->AuthInfo = AuthInfo;
1502 NewConnection->encryption_auth_len = 0;
1503 NewConnection->signature_auth_len = 0;
1504 if (QOS) RpcQualityOfService_AddRef(QOS);
1505 NewConnection->QOS = QOS;
1507 list_init(&NewConnection->conn_pool_entry);
1508 NewConnection->async_state = NULL;
1510 TRACE("connection: %p\n", NewConnection);
1511 *Connection = NewConnection;
1517 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1521 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1522 rpcrt4_conn_get_name(OldConnection),
1523 OldConnection->NetworkAddr,
1524 OldConnection->Endpoint, NULL,
1525 OldConnection->AuthInfo, OldConnection->QOS);
1526 if (err == RPC_S_OK)
1527 rpcrt4_conn_handoff(OldConnection, *Connection);
1531 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1533 TRACE("connection: %p\n", Connection);
1535 RPCRT4_CloseConnection(Connection);
1536 RPCRT4_strfree(Connection->Endpoint);
1537 RPCRT4_strfree(Connection->NetworkAddr);
1538 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1539 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1540 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1543 if (Connection->server_binding) RPCRT4_DestroyBinding(Connection->server_binding);
1545 HeapFree(GetProcessHeap(), 0, Connection);
1549 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1551 const char *protseq,
1552 const char *networkaddr,
1553 const char *endpoint)
1555 twr_empty_floor_t *protocol_floor;
1556 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1561 return RPC_S_INVALID_RPC_PROTSEQ;
1565 *tower_size = sizeof(*protocol_floor);
1566 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1570 protocol_floor = (twr_empty_floor_t *)tower_data;
1571 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1572 protocol_floor->protid = protseq_ops->epm_protocols[0];
1573 protocol_floor->count_rhs = 0;
1575 tower_data += sizeof(*protocol_floor);
1577 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1579 return EPT_S_NOT_REGISTERED;
1581 *tower_size += sizeof(*protocol_floor);
1586 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1592 const twr_empty_floor_t *protocol_floor;
1593 const twr_empty_floor_t *floor4;
1594 const struct connection_ops *protseq_ops = NULL;
1598 if (tower_size < sizeof(*protocol_floor))
1599 return EPT_S_NOT_REGISTERED;
1601 protocol_floor = (const twr_empty_floor_t *)tower_data;
1602 tower_data += sizeof(*protocol_floor);
1603 tower_size -= sizeof(*protocol_floor);
1604 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1605 (protocol_floor->count_rhs > tower_size))
1606 return EPT_S_NOT_REGISTERED;
1607 tower_data += protocol_floor->count_rhs;
1608 tower_size -= protocol_floor->count_rhs;
1610 floor4 = (const twr_empty_floor_t *)tower_data;
1611 if ((tower_size < sizeof(*floor4)) ||
1612 (floor4->count_lhs != sizeof(floor4->protid)))
1613 return EPT_S_NOT_REGISTERED;
1615 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1616 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1617 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1619 protseq_ops = &conn_protseq_list[i];
1624 return EPT_S_NOT_REGISTERED;
1626 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1628 if ((status == RPC_S_OK) && protseq)
1630 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1631 strcpy(*protseq, protseq_ops->name);
1637 /***********************************************************************
1638 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1640 * Checks if the given protocol sequence is known by the RPC system.
1641 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1644 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1648 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1649 ps, sizeof ps, NULL, NULL);
1650 if (rpcrt4_get_conn_protseq_ops(ps))
1653 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1655 return RPC_S_INVALID_RPC_PROTSEQ;
1658 /***********************************************************************
1659 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1661 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1663 UNICODE_STRING protseqW;
1665 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1667 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1668 RtlFreeUnicodeString(&protseqW);
1671 return RPC_S_OUT_OF_MEMORY;