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 static CRITICAL_SECTION assoc_list_cs;
83 static CRITICAL_SECTION_DEBUG assoc_list_cs_debug =
86 { &assoc_list_cs_debug.ProcessLocksList, &assoc_list_cs_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": assoc_list_cs") }
89 static CRITICAL_SECTION assoc_list_cs = { &assoc_list_cs_debug, -1, 0, 0, 0, 0 };
91 static struct list assoc_list = LIST_INIT(assoc_list);
93 /**** ncacn_np support ****/
95 typedef struct _RpcConnection_np
103 static RpcConnection *rpcrt4_conn_np_alloc(void)
105 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
109 memset(&npc->ovl, 0, sizeof(npc->ovl));
110 npc->listening = FALSE;
115 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
120 npc->listening = TRUE;
121 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
124 if (GetLastError() == ERROR_PIPE_CONNECTED) {
125 SetEvent(npc->ovl.hEvent);
128 if (GetLastError() == ERROR_IO_PENDING) {
129 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
132 npc->listening = FALSE;
133 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
134 return RPC_S_OUT_OF_RESOURCES;
137 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
139 RpcConnection_np *npc = (RpcConnection_np *) Connection;
140 TRACE("listening on %s\n", pname);
142 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
143 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
144 PIPE_UNLIMITED_INSTANCES,
145 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
146 if (npc->pipe == INVALID_HANDLE_VALUE) {
147 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
148 if (GetLastError() == ERROR_FILE_EXISTS)
149 return RPC_S_DUPLICATE_ENDPOINT;
151 return RPC_S_CANT_CREATE_ENDPOINT;
154 memset(&npc->ovl, 0, sizeof(npc->ovl));
155 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
157 /* Note: we don't call ConnectNamedPipe here because it must be done in the
158 * server thread as the thread must be alertable */
162 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
164 RpcConnection_np *npc = (RpcConnection_np *) Connection;
168 TRACE("connecting to %s\n", pname);
174 dwFlags = SECURITY_SQOS_PRESENT;
175 switch (Connection->QOS->qos->ImpersonationType)
177 case RPC_C_IMP_LEVEL_DEFAULT:
178 /* FIXME: what to do here? */
180 case RPC_C_IMP_LEVEL_ANONYMOUS:
181 dwFlags |= SECURITY_ANONYMOUS;
183 case RPC_C_IMP_LEVEL_IDENTIFY:
184 dwFlags |= SECURITY_IDENTIFICATION;
186 case RPC_C_IMP_LEVEL_IMPERSONATE:
187 dwFlags |= SECURITY_IMPERSONATION;
189 case RPC_C_IMP_LEVEL_DELEGATE:
190 dwFlags |= SECURITY_DELEGATION;
193 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
194 dwFlags |= SECURITY_CONTEXT_TRACKING;
196 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
197 OPEN_EXISTING, dwFlags, 0);
198 if (pipe != INVALID_HANDLE_VALUE) break;
199 err = GetLastError();
200 if (err == ERROR_PIPE_BUSY) {
201 TRACE("connection failed, error=%x\n", err);
202 return RPC_S_SERVER_TOO_BUSY;
205 return RPC_S_SERVER_UNAVAILABLE;
206 if (!WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
207 err = GetLastError();
208 WARN("connection failed, error=%x\n", err);
209 return RPC_S_SERVER_UNAVAILABLE;
214 memset(&npc->ovl, 0, sizeof(npc->ovl));
215 /* pipe is connected; change to message-read mode. */
216 dwMode = PIPE_READMODE_MESSAGE;
217 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
218 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
224 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
226 RpcConnection_np *npc = (RpcConnection_np *) Connection;
227 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
231 /* already connected? */
235 /* protseq=ncalrpc: supposed to use NT LPC ports,
236 * but we'll implement it with named pipes for now */
237 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
238 strcat(strcpy(pname, prefix), Connection->Endpoint);
239 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
245 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, LPSTR endpoint)
247 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
250 RpcConnection *Connection;
252 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
253 endpoint, NULL, NULL, NULL);
257 /* protseq=ncalrpc: supposed to use NT LPC ports,
258 * but we'll implement it with named pipes for now */
259 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
260 strcat(strcpy(pname, prefix), Connection->Endpoint);
261 r = rpcrt4_conn_create_pipe(Connection, pname);
264 EnterCriticalSection(&protseq->cs);
265 Connection->Next = protseq->conn;
266 protseq->conn = Connection;
267 LeaveCriticalSection(&protseq->cs);
272 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
274 RpcConnection_np *npc = (RpcConnection_np *) Connection;
275 static const char prefix[] = "\\\\.";
279 /* already connected? */
283 /* protseq=ncacn_np: named pipes */
284 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
285 strcat(strcpy(pname, prefix), Connection->Endpoint);
286 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
292 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
294 static const char prefix[] = "\\\\.";
297 RpcConnection *Connection;
299 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
300 endpoint, NULL, NULL, NULL);
304 /* protseq=ncacn_np: named pipes */
305 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
306 strcat(strcpy(pname, prefix), Connection->Endpoint);
307 r = rpcrt4_conn_create_pipe(Connection, pname);
310 EnterCriticalSection(&protseq->cs);
311 Connection->Next = protseq->conn;
312 protseq->conn = Connection;
313 LeaveCriticalSection(&protseq->cs);
318 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
320 /* because of the way named pipes work, we'll transfer the connected pipe
321 * to the child, then reopen the server binding to continue listening */
323 new_npc->pipe = old_npc->pipe;
324 new_npc->ovl = old_npc->ovl;
326 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
327 old_npc->listening = FALSE;
330 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
334 static const char prefix[] = "\\\\.";
336 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
338 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
339 strcat(strcpy(pname, prefix), old_conn->Endpoint);
340 status = rpcrt4_conn_create_pipe(old_conn, pname);
346 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
350 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
352 TRACE("%s\n", old_conn->Endpoint);
354 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
356 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
357 strcat(strcpy(pname, prefix), old_conn->Endpoint);
358 status = rpcrt4_conn_create_pipe(old_conn, pname);
364 static int rpcrt4_conn_np_read(RpcConnection *Connection,
365 void *buffer, unsigned int count)
367 RpcConnection_np *npc = (RpcConnection_np *) Connection;
370 unsigned int bytes_left = count;
375 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
376 if (!ret || !bytes_read)
378 bytes_left -= bytes_read;
381 return ret ? count : -1;
384 static int rpcrt4_conn_np_write(RpcConnection *Connection,
385 const void *buffer, unsigned int count)
387 RpcConnection_np *npc = (RpcConnection_np *) Connection;
388 const char *buf = buffer;
390 unsigned int bytes_left = count;
395 ret = WriteFile(npc->pipe, buf, count, &bytes_written, NULL);
396 if (!ret || !bytes_written)
398 bytes_left -= bytes_written;
399 buf += bytes_written;
401 return ret ? count : -1;
404 static int rpcrt4_conn_np_close(RpcConnection *Connection)
406 RpcConnection_np *npc = (RpcConnection_np *) Connection;
408 FlushFileBuffers(npc->pipe);
409 CloseHandle(npc->pipe);
412 if (npc->ovl.hEvent) {
413 CloseHandle(npc->ovl.hEvent);
419 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
421 /* FIXME: implement when named pipe writes use overlapped I/O */
424 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
425 const char *networkaddr,
426 const char *endpoint)
428 twr_empty_floor_t *smb_floor;
429 twr_empty_floor_t *nb_floor;
431 size_t networkaddr_size;
432 size_t endpoint_size;
434 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
436 networkaddr_size = strlen(networkaddr) + 1;
437 endpoint_size = strlen(endpoint) + 1;
438 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
443 smb_floor = (twr_empty_floor_t *)tower_data;
445 tower_data += sizeof(*smb_floor);
447 smb_floor->count_lhs = sizeof(smb_floor->protid);
448 smb_floor->protid = EPM_PROTOCOL_SMB;
449 smb_floor->count_rhs = endpoint_size;
451 memcpy(tower_data, endpoint, endpoint_size);
452 tower_data += endpoint_size;
454 nb_floor = (twr_empty_floor_t *)tower_data;
456 tower_data += sizeof(*nb_floor);
458 nb_floor->count_lhs = sizeof(nb_floor->protid);
459 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
460 nb_floor->count_rhs = networkaddr_size;
462 memcpy(tower_data, networkaddr, networkaddr_size);
463 tower_data += networkaddr_size;
468 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
473 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
474 const twr_empty_floor_t *nb_floor;
476 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
478 if (tower_size < sizeof(*smb_floor))
479 return EPT_S_NOT_REGISTERED;
481 tower_data += sizeof(*smb_floor);
482 tower_size -= sizeof(*smb_floor);
484 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
485 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
486 (smb_floor->count_rhs > tower_size))
487 return EPT_S_NOT_REGISTERED;
491 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
493 return RPC_S_OUT_OF_RESOURCES;
494 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
496 tower_data += smb_floor->count_rhs;
497 tower_size -= smb_floor->count_rhs;
499 if (tower_size < sizeof(*nb_floor))
500 return EPT_S_NOT_REGISTERED;
502 nb_floor = (const twr_empty_floor_t *)tower_data;
504 tower_data += sizeof(*nb_floor);
505 tower_size -= sizeof(*nb_floor);
507 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
508 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
509 (nb_floor->count_rhs > tower_size))
510 return EPT_S_NOT_REGISTERED;
514 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
519 I_RpcFree(*endpoint);
522 return RPC_S_OUT_OF_RESOURCES;
524 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
530 typedef struct _RpcServerProtseq_np
532 RpcServerProtseq common;
534 } RpcServerProtseq_np;
536 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
538 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
540 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
544 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
546 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
547 SetEvent(npps->mgr_event);
550 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
552 HANDLE *objs = prev_array;
553 RpcConnection_np *conn;
554 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
556 EnterCriticalSection(&protseq->cs);
558 /* open and count connections */
560 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
562 rpcrt4_conn_listen_pipe(conn);
563 if (conn->ovl.hEvent)
565 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
568 /* make array of connections */
570 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
572 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
575 ERR("couldn't allocate objs\n");
576 LeaveCriticalSection(&protseq->cs);
580 objs[0] = npps->mgr_event;
582 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
584 if ((objs[*count] = conn->ovl.hEvent))
586 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
588 LeaveCriticalSection(&protseq->cs);
592 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
594 HeapFree(GetProcessHeap(), 0, array);
597 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
600 HANDLE *objs = wait_array;
602 RpcConnection *cconn;
603 RpcConnection_np *conn;
610 /* an alertable wait isn't strictly necessary, but due to our
611 * overlapped I/O implementation in Wine we need to free some memory
612 * by the file user APC being called, even if no completion routine was
613 * specified at the time of starting the async operation */
614 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
615 } while (res == WAIT_IO_COMPLETION);
617 if (res == WAIT_OBJECT_0)
619 else if (res == WAIT_FAILED)
621 ERR("wait failed with error %d\n", GetLastError());
626 b_handle = objs[res - WAIT_OBJECT_0];
627 /* find which connection got a RPC */
628 EnterCriticalSection(&protseq->cs);
629 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
631 if (b_handle == conn->ovl.hEvent) break;
632 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
636 RPCRT4_SpawnConnection(&cconn, &conn->common);
638 ERR("failed to locate connection for handle %p\n", b_handle);
639 LeaveCriticalSection(&protseq->cs);
642 RPCRT4_new_client(cconn);
649 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
650 const char *networkaddr,
651 const char *endpoint)
653 twr_empty_floor_t *pipe_floor;
655 size_t endpoint_size;
657 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
659 endpoint_size = strlen(networkaddr) + 1;
660 size = sizeof(*pipe_floor) + endpoint_size;
665 pipe_floor = (twr_empty_floor_t *)tower_data;
667 tower_data += sizeof(*pipe_floor);
669 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
670 pipe_floor->protid = EPM_PROTOCOL_SMB;
671 pipe_floor->count_rhs = endpoint_size;
673 memcpy(tower_data, endpoint, endpoint_size);
674 tower_data += endpoint_size;
679 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
684 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
686 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
691 if (tower_size < sizeof(*pipe_floor))
692 return EPT_S_NOT_REGISTERED;
694 tower_data += sizeof(*pipe_floor);
695 tower_size -= sizeof(*pipe_floor);
697 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
698 (pipe_floor->protid != EPM_PROTOCOL_SMB) ||
699 (pipe_floor->count_rhs > tower_size))
700 return EPT_S_NOT_REGISTERED;
704 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
706 return RPC_S_OUT_OF_RESOURCES;
707 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
713 /**** ncacn_ip_tcp support ****/
715 typedef struct _RpcConnection_tcp
717 RpcConnection common;
722 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
724 RpcConnection_tcp *tcpc;
725 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
729 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
731 ERR("socketpair() failed: %s\n", strerror(errno));
732 HeapFree(GetProcessHeap(), 0, tcpc);
735 return &tcpc->common;
738 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
740 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
744 struct addrinfo *ai_cur;
745 struct addrinfo hints;
747 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
749 if (tcpc->sock != -1)
753 hints.ai_family = PF_UNSPEC;
754 hints.ai_socktype = SOCK_STREAM;
755 hints.ai_protocol = IPPROTO_TCP;
756 hints.ai_addrlen = 0;
757 hints.ai_addr = NULL;
758 hints.ai_canonname = NULL;
759 hints.ai_next = NULL;
761 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
764 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
765 Connection->Endpoint, gai_strerror(ret));
766 return RPC_S_SERVER_UNAVAILABLE;
769 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
777 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
778 host, sizeof(host), service, sizeof(service),
779 NI_NUMERICHOST | NI_NUMERICSERV);
780 TRACE("trying %s:%s\n", host, service);
783 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
786 WARN("socket() failed: %s\n", strerror(errno));
790 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
792 WARN("connect() failed: %s\n", strerror(errno));
797 /* RPC depends on having minimal latency so disable the Nagle algorithm */
799 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
800 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
805 TRACE("connected\n");
810 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
811 return RPC_S_SERVER_UNAVAILABLE;
814 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
816 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
820 struct addrinfo *ai_cur;
821 struct addrinfo hints;
822 RpcConnection *first_connection = NULL;
824 TRACE("(%p, %s)\n", protseq, endpoint);
826 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
827 hints.ai_family = PF_UNSPEC;
828 hints.ai_socktype = SOCK_STREAM;
829 hints.ai_protocol = IPPROTO_TCP;
830 hints.ai_addrlen = 0;
831 hints.ai_addr = NULL;
832 hints.ai_canonname = NULL;
833 hints.ai_next = NULL;
835 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
838 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
840 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
841 return RPC_S_INVALID_ENDPOINT_FORMAT;
842 return RPC_S_CANT_CREATE_ENDPOINT;
845 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
847 RpcConnection_tcp *tcpc;
848 RPC_STATUS create_status;
854 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
855 host, sizeof(host), service, sizeof(service),
856 NI_NUMERICHOST | NI_NUMERICSERV);
857 TRACE("trying %s:%s\n", host, service);
860 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
863 WARN("socket() failed: %s\n", strerror(errno));
864 status = RPC_S_CANT_CREATE_ENDPOINT;
868 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
871 WARN("bind failed: %s\n", strerror(errno));
873 if (errno == EADDRINUSE)
874 status = RPC_S_DUPLICATE_ENDPOINT;
876 status = RPC_S_CANT_CREATE_ENDPOINT;
879 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
880 protseq->Protseq, NULL,
881 endpoint, NULL, NULL, NULL);
882 if (create_status != RPC_S_OK)
885 status = create_status;
890 ret = listen(sock, protseq->MaxCalls);
893 WARN("listen failed: %s\n", strerror(errno));
894 RPCRT4_DestroyConnection(&tcpc->common);
895 status = RPC_S_OUT_OF_RESOURCES;
898 /* need a non-blocking socket, otherwise accept() has a potential
899 * race-condition (poll() says it is readable, connection drops,
900 * and accept() blocks until the next connection comes...)
902 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
905 WARN("couldn't make socket non-blocking, error %d\n", ret);
906 RPCRT4_DestroyConnection(&tcpc->common);
907 status = RPC_S_OUT_OF_RESOURCES;
911 tcpc->common.Next = first_connection;
912 first_connection = &tcpc->common;
917 /* if at least one connection was created for an endpoint then
919 if (first_connection)
923 /* find last element in list */
924 for (conn = first_connection; conn->Next; conn = conn->Next)
927 EnterCriticalSection(&protseq->cs);
928 conn->Next = protseq->conn;
929 protseq->conn = first_connection;
930 LeaveCriticalSection(&protseq->cs);
932 TRACE("listening on %s\n", endpoint);
936 ERR("couldn't listen on port %s\n", endpoint);
940 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
943 struct sockaddr_in address;
945 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
946 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
948 addrsize = sizeof(address);
949 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
952 ERR("Failed to accept a TCP connection: error %d\n", ret);
953 return RPC_S_OUT_OF_RESOURCES;
955 /* reset to blocking behaviour */
956 fcntl(ret, F_SETFL, 0);
958 TRACE("Accepted a new TCP connection\n");
962 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
963 void *buffer, unsigned int count)
965 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
969 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
972 else if (errno != EAGAIN)
976 struct pollfd pfds[2];
977 pfds[0].fd = tcpc->sock;
978 pfds[0].events = POLLIN;
979 pfds[1].fd = tcpc->cancel_fds[0];
980 pfds[1].events = POLLIN;
981 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
983 ERR("poll() failed: %s\n", strerror(errno));
986 if (pfds[1].revents & POLLIN) /* canceled */
989 read(pfds[1].fd, &dummy, sizeof(dummy));
993 } while (bytes_read != count);
994 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
998 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
999 const void *buffer, unsigned int count)
1001 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1002 int bytes_written = 0;
1005 int r = write(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written);
1008 else if (errno != EAGAIN)
1013 pfd.fd = tcpc->sock;
1014 pfd.events = POLLOUT;
1015 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1017 ERR("poll() failed: %s\n", strerror(errno));
1021 } while (bytes_written != count);
1022 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1023 return bytes_written;
1026 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1028 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1030 TRACE("%d\n", tcpc->sock);
1032 if (tcpc->sock != -1)
1035 close(tcpc->cancel_fds[0]);
1036 close(tcpc->cancel_fds[1]);
1040 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1042 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1045 TRACE("%p\n", Connection);
1047 write(tcpc->cancel_fds[1], &dummy, 1);
1050 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1051 const char *networkaddr,
1052 const char *endpoint)
1054 twr_tcp_floor_t *tcp_floor;
1055 twr_ipv4_floor_t *ipv4_floor;
1056 struct addrinfo *ai;
1057 struct addrinfo hints;
1059 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1061 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1066 tcp_floor = (twr_tcp_floor_t *)tower_data;
1067 tower_data += sizeof(*tcp_floor);
1069 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1071 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1072 tcp_floor->protid = EPM_PROTOCOL_TCP;
1073 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1075 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1076 ipv4_floor->protid = EPM_PROTOCOL_IP;
1077 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1079 hints.ai_flags = AI_NUMERICHOST;
1080 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1081 hints.ai_family = PF_INET;
1082 hints.ai_socktype = SOCK_STREAM;
1083 hints.ai_protocol = IPPROTO_TCP;
1084 hints.ai_addrlen = 0;
1085 hints.ai_addr = NULL;
1086 hints.ai_canonname = NULL;
1087 hints.ai_next = NULL;
1089 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1092 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1095 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1100 if (ai->ai_family == PF_INET)
1102 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1103 tcp_floor->port = sin->sin_port;
1104 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1108 ERR("unexpected protocol family %d\n", ai->ai_family);
1117 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1122 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1123 const twr_ipv4_floor_t *ipv4_floor;
1124 struct in_addr in_addr;
1126 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1128 if (tower_size < sizeof(*tcp_floor))
1129 return EPT_S_NOT_REGISTERED;
1131 tower_data += sizeof(*tcp_floor);
1132 tower_size -= sizeof(*tcp_floor);
1134 if (tower_size < sizeof(*ipv4_floor))
1135 return EPT_S_NOT_REGISTERED;
1137 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1139 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1140 (tcp_floor->protid != EPM_PROTOCOL_TCP) ||
1141 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1142 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1143 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1144 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1145 return EPT_S_NOT_REGISTERED;
1149 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1151 return RPC_S_OUT_OF_RESOURCES;
1152 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1157 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1162 I_RpcFree(*endpoint);
1165 return RPC_S_OUT_OF_RESOURCES;
1167 in_addr.s_addr = ipv4_floor->ipv4addr;
1168 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1170 ERR("inet_ntop: %s\n", strerror(errno));
1171 I_RpcFree(*networkaddr);
1172 *networkaddr = NULL;
1175 I_RpcFree(*endpoint);
1178 return EPT_S_NOT_REGISTERED;
1185 typedef struct _RpcServerProtseq_sock
1187 RpcServerProtseq common;
1190 } RpcServerProtseq_sock;
1192 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1194 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1198 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1200 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1201 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1202 ps->mgr_event_rcv = fds[0];
1203 ps->mgr_event_snd = fds[1];
1207 ERR("socketpair failed with error %s\n", strerror(errno));
1208 HeapFree(GetProcessHeap(), 0, ps);
1215 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1217 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1219 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1222 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1224 struct pollfd *poll_info = prev_array;
1225 RpcConnection_tcp *conn;
1226 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1228 EnterCriticalSection(&protseq->cs);
1230 /* open and count connections */
1232 conn = (RpcConnection_tcp *)protseq->conn;
1234 if (conn->sock != -1)
1236 conn = (RpcConnection_tcp *)conn->common.Next;
1239 /* make array of connections */
1241 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1243 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1246 ERR("couldn't allocate poll_info\n");
1247 LeaveCriticalSection(&protseq->cs);
1251 poll_info[0].fd = sockps->mgr_event_rcv;
1252 poll_info[0].events = POLLIN;
1254 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1256 if (conn->sock != -1)
1258 poll_info[*count].fd = conn->sock;
1259 poll_info[*count].events = POLLIN;
1262 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1264 LeaveCriticalSection(&protseq->cs);
1268 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1270 HeapFree(GetProcessHeap(), 0, array);
1273 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1275 struct pollfd *poll_info = wait_array;
1277 RpcConnection *cconn;
1278 RpcConnection_tcp *conn;
1283 ret = poll(poll_info, count, -1);
1286 ERR("poll failed with error %d\n", ret);
1290 for (i = 0; i < count; i++)
1291 if (poll_info[i].revents & POLLIN)
1293 /* RPC server event */
1297 read(poll_info[0].fd, &dummy, sizeof(dummy));
1301 /* find which connection got a RPC */
1302 EnterCriticalSection(&protseq->cs);
1303 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1305 if (poll_info[i].fd == conn->sock) break;
1306 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1310 RPCRT4_SpawnConnection(&cconn, &conn->common);
1312 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1313 LeaveCriticalSection(&protseq->cs);
1315 RPCRT4_new_client(cconn);
1323 static const struct connection_ops conn_protseq_list[] = {
1325 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
1326 rpcrt4_conn_np_alloc,
1327 rpcrt4_ncacn_np_open,
1328 rpcrt4_ncacn_np_handoff,
1329 rpcrt4_conn_np_read,
1330 rpcrt4_conn_np_write,
1331 rpcrt4_conn_np_close,
1332 rpcrt4_conn_np_cancel_call,
1333 rpcrt4_ncacn_np_get_top_of_tower,
1334 rpcrt4_ncacn_np_parse_top_of_tower,
1337 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
1338 rpcrt4_conn_np_alloc,
1339 rpcrt4_ncalrpc_open,
1340 rpcrt4_ncalrpc_handoff,
1341 rpcrt4_conn_np_read,
1342 rpcrt4_conn_np_write,
1343 rpcrt4_conn_np_close,
1344 rpcrt4_conn_np_cancel_call,
1345 rpcrt4_ncalrpc_get_top_of_tower,
1346 rpcrt4_ncalrpc_parse_top_of_tower,
1349 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
1350 rpcrt4_conn_tcp_alloc,
1351 rpcrt4_ncacn_ip_tcp_open,
1352 rpcrt4_conn_tcp_handoff,
1353 rpcrt4_conn_tcp_read,
1354 rpcrt4_conn_tcp_write,
1355 rpcrt4_conn_tcp_close,
1356 rpcrt4_conn_tcp_cancel_call,
1357 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
1358 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
1363 static const struct protseq_ops protseq_list[] =
1367 rpcrt4_protseq_np_alloc,
1368 rpcrt4_protseq_np_signal_state_changed,
1369 rpcrt4_protseq_np_get_wait_array,
1370 rpcrt4_protseq_np_free_wait_array,
1371 rpcrt4_protseq_np_wait_for_new_connection,
1372 rpcrt4_protseq_ncacn_np_open_endpoint,
1376 rpcrt4_protseq_np_alloc,
1377 rpcrt4_protseq_np_signal_state_changed,
1378 rpcrt4_protseq_np_get_wait_array,
1379 rpcrt4_protseq_np_free_wait_array,
1380 rpcrt4_protseq_np_wait_for_new_connection,
1381 rpcrt4_protseq_ncalrpc_open_endpoint,
1385 rpcrt4_protseq_sock_alloc,
1386 rpcrt4_protseq_sock_signal_state_changed,
1387 rpcrt4_protseq_sock_get_wait_array,
1388 rpcrt4_protseq_sock_free_wait_array,
1389 rpcrt4_protseq_sock_wait_for_new_connection,
1390 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
1394 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
1396 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
1399 for(i=0; i<ARRAYSIZE(protseq_list); i++)
1400 if (!strcmp(protseq_list[i].name, protseq))
1401 return &protseq_list[i];
1405 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
1408 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
1409 if (!strcmp(conn_protseq_list[i].name, protseq))
1410 return &conn_protseq_list[i];
1414 /**** interface to rest of code ****/
1416 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
1418 TRACE("(Connection == ^%p)\n", Connection);
1420 assert(!Connection->server);
1421 return Connection->ops->open_connection_client(Connection);
1424 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
1426 TRACE("(Connection == ^%p)\n", Connection);
1427 if (SecIsValidHandle(&Connection->ctx))
1429 DeleteSecurityContext(&Connection->ctx);
1430 SecInvalidateHandle(&Connection->ctx);
1432 rpcrt4_conn_close(Connection);
1436 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
1437 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
1438 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
1440 const struct connection_ops *ops;
1441 RpcConnection* NewConnection;
1443 ops = rpcrt4_get_conn_protseq_ops(Protseq);
1446 FIXME("not supported for protseq %s\n", Protseq);
1447 return RPC_S_PROTSEQ_NOT_SUPPORTED;
1450 NewConnection = ops->alloc();
1451 NewConnection->Next = NULL;
1452 NewConnection->server = server;
1453 NewConnection->ops = ops;
1454 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1455 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
1456 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
1457 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
1458 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
1459 NewConnection->NextCallId = 1;
1461 SecInvalidateHandle(&NewConnection->ctx);
1462 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
1463 NewConnection->attr = 0;
1464 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
1465 NewConnection->AuthInfo = AuthInfo;
1466 NewConnection->encryption_auth_len = 0;
1467 NewConnection->signature_auth_len = 0;
1468 if (QOS) RpcQualityOfService_AddRef(QOS);
1469 NewConnection->QOS = QOS;
1471 list_init(&NewConnection->conn_pool_entry);
1473 TRACE("connection: %p\n", NewConnection);
1474 *Connection = NewConnection;
1479 RPC_STATUS RPCRT4_GetAssociation(LPCSTR Protseq, LPCSTR NetworkAddr,
1480 LPCSTR Endpoint, LPCWSTR NetworkOptions,
1481 RpcAssoc **assoc_out)
1485 EnterCriticalSection(&assoc_list_cs);
1486 LIST_FOR_EACH_ENTRY(assoc, &assoc_list, RpcAssoc, entry)
1488 if (!strcmp(Protseq, assoc->Protseq) &&
1489 !strcmp(NetworkAddr, assoc->NetworkAddr) &&
1490 !strcmp(Endpoint, assoc->Endpoint) &&
1491 ((!assoc->NetworkOptions && !NetworkOptions) || !strcmpW(NetworkOptions, assoc->NetworkOptions)))
1495 LeaveCriticalSection(&assoc_list_cs);
1496 TRACE("using existing assoc %p\n", assoc);
1501 assoc = HeapAlloc(GetProcessHeap(), 0, sizeof(*assoc));
1504 LeaveCriticalSection(&assoc_list_cs);
1505 return RPC_S_OUT_OF_RESOURCES;
1508 list_init(&assoc->connection_pool);
1509 InitializeCriticalSection(&assoc->cs);
1510 assoc->Protseq = RPCRT4_strdupA(Protseq);
1511 assoc->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
1512 assoc->Endpoint = RPCRT4_strdupA(Endpoint);
1513 assoc->NetworkOptions = NetworkOptions ? RPCRT4_strdupW(NetworkOptions) : NULL;
1514 assoc->assoc_group_id = 0;
1515 list_add_head(&assoc_list, &assoc->entry);
1518 LeaveCriticalSection(&assoc_list_cs);
1520 TRACE("new assoc %p\n", assoc);
1525 ULONG RpcAssoc_Release(RpcAssoc *assoc)
1529 EnterCriticalSection(&assoc_list_cs);
1530 refs = --assoc->refs;
1532 list_remove(&assoc->entry);
1533 LeaveCriticalSection(&assoc_list_cs);
1537 RpcConnection *Connection, *cursor2;
1539 TRACE("destroying assoc %p\n", assoc);
1541 LIST_FOR_EACH_ENTRY_SAFE(Connection, cursor2, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1543 list_remove(&Connection->conn_pool_entry);
1544 RPCRT4_DestroyConnection(Connection);
1547 HeapFree(GetProcessHeap(), 0, assoc->NetworkOptions);
1548 HeapFree(GetProcessHeap(), 0, assoc->Endpoint);
1549 HeapFree(GetProcessHeap(), 0, assoc->NetworkAddr);
1550 HeapFree(GetProcessHeap(), 0, assoc->Protseq);
1552 DeleteCriticalSection(&assoc->cs);
1554 HeapFree(GetProcessHeap(), 0, assoc);
1560 #define ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment)-1))
1562 static RPC_STATUS RpcAssoc_BindConnection(const RpcAssoc *assoc, RpcConnection *conn,
1563 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1564 const RPC_SYNTAX_IDENTIFIER *TransferSyntax)
1567 RpcPktHdr *response_hdr;
1571 TRACE("sending bind request to server\n");
1573 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
1574 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
1575 assoc->assoc_group_id,
1576 InterfaceId, TransferSyntax);
1578 status = RPCRT4_Send(conn, hdr, NULL, 0);
1579 RPCRT4_FreeHeader(hdr);
1580 if (status != RPC_S_OK)
1583 status = RPCRT4_Receive(conn, &response_hdr, &msg);
1584 if (status != RPC_S_OK)
1586 ERR("receive failed\n");
1590 switch (response_hdr->common.ptype)
1594 RpcAddressString *server_address = msg.Buffer;
1595 if ((msg.BufferLength >= FIELD_OFFSET(RpcAddressString, string[0])) ||
1596 (msg.BufferLength >= ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4)))
1598 unsigned short remaining = msg.BufferLength -
1599 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4);
1600 RpcResults *results = (RpcResults*)((ULONG_PTR)server_address +
1601 ROUND_UP(FIELD_OFFSET(RpcAddressString, string[server_address->length]), 4));
1602 if ((results->num_results == 1) && (remaining >= sizeof(*results)))
1604 switch (results->results[0].result)
1607 conn->assoc_group_id = response_hdr->bind_ack.assoc_gid;
1608 conn->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
1609 conn->ActiveInterface = *InterfaceId;
1611 case RESULT_PROVIDER_REJECTION:
1612 switch (results->results[0].reason)
1614 case REASON_ABSTRACT_SYNTAX_NOT_SUPPORTED:
1615 ERR("syntax %s, %d.%d not supported\n",
1616 debugstr_guid(&InterfaceId->SyntaxGUID),
1617 InterfaceId->SyntaxVersion.MajorVersion,
1618 InterfaceId->SyntaxVersion.MinorVersion);
1619 status = RPC_S_UNKNOWN_IF;
1621 case REASON_TRANSFER_SYNTAXES_NOT_SUPPORTED:
1622 ERR("transfer syntax not supported\n");
1623 status = RPC_S_SERVER_UNAVAILABLE;
1627 status = RPC_S_CALL_FAILED_DNE;
1630 case RESULT_USER_REJECTION:
1632 ERR("rejection result %d\n", results->results[0].result);
1633 status = RPC_S_CALL_FAILED_DNE;
1638 ERR("incorrect results size\n");
1639 status = RPC_S_CALL_FAILED_DNE;
1644 ERR("bind ack packet too small (%d)\n", msg.BufferLength);
1645 status = RPC_S_PROTOCOL_ERROR;
1650 switch (response_hdr->bind_nack.reject_reason)
1652 case REJECT_LOCAL_LIMIT_EXCEEDED:
1653 case REJECT_TEMPORARY_CONGESTION:
1654 ERR("server too busy\n");
1655 status = RPC_S_SERVER_TOO_BUSY;
1657 case REJECT_PROTOCOL_VERSION_NOT_SUPPORTED:
1658 ERR("protocol version not supported\n");
1659 status = RPC_S_PROTOCOL_ERROR;
1661 case REJECT_UNKNOWN_AUTHN_SERVICE:
1662 ERR("unknown authentication service\n");
1663 status = RPC_S_UNKNOWN_AUTHN_SERVICE;
1665 case REJECT_INVALID_CHECKSUM:
1666 ERR("invalid checksum\n");
1667 status = ERROR_ACCESS_DENIED;
1670 ERR("rejected bind for reason %d\n", response_hdr->bind_nack.reject_reason);
1671 status = RPC_S_CALL_FAILED_DNE;
1675 ERR("wrong packet type received %d\n", response_hdr->common.ptype);
1676 status = RPC_S_PROTOCOL_ERROR;
1680 I_RpcFreeBuffer(&msg);
1681 RPCRT4_FreeHeader(response_hdr);
1685 static RpcConnection *RpcAssoc_GetIdleConnection(RpcAssoc *assoc,
1686 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1687 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, const RpcAuthInfo *AuthInfo,
1688 const RpcQualityOfService *QOS)
1690 RpcConnection *Connection;
1691 EnterCriticalSection(&assoc->cs);
1692 /* try to find a compatible connection from the connection pool */
1693 LIST_FOR_EACH_ENTRY(Connection, &assoc->connection_pool, RpcConnection, conn_pool_entry)
1695 if (!memcmp(&Connection->ActiveInterface, InterfaceId,
1696 sizeof(RPC_SYNTAX_IDENTIFIER)) &&
1697 RpcAuthInfo_IsEqual(Connection->AuthInfo, AuthInfo) &&
1698 RpcQualityOfService_IsEqual(Connection->QOS, QOS))
1700 list_remove(&Connection->conn_pool_entry);
1701 LeaveCriticalSection(&assoc->cs);
1702 TRACE("got connection from pool %p\n", Connection);
1707 LeaveCriticalSection(&assoc->cs);
1711 RPC_STATUS RpcAssoc_GetClientConnection(RpcAssoc *assoc,
1712 const RPC_SYNTAX_IDENTIFIER *InterfaceId,
1713 const RPC_SYNTAX_IDENTIFIER *TransferSyntax, RpcAuthInfo *AuthInfo,
1714 RpcQualityOfService *QOS, RpcConnection **Connection)
1716 RpcConnection *NewConnection;
1719 *Connection = RpcAssoc_GetIdleConnection(assoc, InterfaceId, TransferSyntax, AuthInfo, QOS);
1723 /* create a new connection */
1724 status = RPCRT4_CreateConnection(&NewConnection, FALSE /* is this a server connection? */,
1725 assoc->Protseq, assoc->NetworkAddr,
1726 assoc->Endpoint, assoc->NetworkOptions,
1728 if (status != RPC_S_OK)
1731 status = RPCRT4_OpenClientConnection(NewConnection);
1732 if (status != RPC_S_OK)
1734 RPCRT4_DestroyConnection(NewConnection);
1738 status = RpcAssoc_BindConnection(assoc, NewConnection, InterfaceId, TransferSyntax);
1739 if (status != RPC_S_OK)
1741 RPCRT4_DestroyConnection(NewConnection);
1745 *Connection = NewConnection;
1750 void RpcAssoc_ReleaseIdleConnection(RpcAssoc *assoc, RpcConnection *Connection)
1752 assert(!Connection->server);
1753 EnterCriticalSection(&assoc->cs);
1754 if (!assoc->assoc_group_id) assoc->assoc_group_id = Connection->assoc_group_id;
1755 list_add_head(&assoc->connection_pool, &Connection->conn_pool_entry);
1756 LeaveCriticalSection(&assoc->cs);
1760 RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
1764 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
1765 rpcrt4_conn_get_name(OldConnection),
1766 OldConnection->NetworkAddr,
1767 OldConnection->Endpoint, NULL,
1768 OldConnection->AuthInfo, OldConnection->QOS);
1769 if (err == RPC_S_OK)
1770 rpcrt4_conn_handoff(OldConnection, *Connection);
1774 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
1776 TRACE("connection: %p\n", Connection);
1778 RPCRT4_CloseConnection(Connection);
1779 RPCRT4_strfree(Connection->Endpoint);
1780 RPCRT4_strfree(Connection->NetworkAddr);
1781 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
1782 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
1783 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
1784 HeapFree(GetProcessHeap(), 0, Connection);
1788 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
1790 const char *protseq,
1791 const char *networkaddr,
1792 const char *endpoint)
1794 twr_empty_floor_t *protocol_floor;
1795 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
1800 return RPC_S_INVALID_RPC_PROTSEQ;
1804 *tower_size = sizeof(*protocol_floor);
1805 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
1809 protocol_floor = (twr_empty_floor_t *)tower_data;
1810 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
1811 protocol_floor->protid = protseq_ops->epm_protocols[0];
1812 protocol_floor->count_rhs = 0;
1814 tower_data += sizeof(*protocol_floor);
1816 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
1818 return EPT_S_NOT_REGISTERED;
1820 *tower_size += sizeof(*protocol_floor);
1825 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
1831 const twr_empty_floor_t *protocol_floor;
1832 const twr_empty_floor_t *floor4;
1833 const struct connection_ops *protseq_ops = NULL;
1837 if (tower_size < sizeof(*protocol_floor))
1838 return EPT_S_NOT_REGISTERED;
1840 protocol_floor = (const twr_empty_floor_t *)tower_data;
1841 tower_data += sizeof(*protocol_floor);
1842 tower_size -= sizeof(*protocol_floor);
1843 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
1844 (protocol_floor->count_rhs > tower_size))
1845 return EPT_S_NOT_REGISTERED;
1846 tower_data += protocol_floor->count_rhs;
1847 tower_size -= protocol_floor->count_rhs;
1849 floor4 = (const twr_empty_floor_t *)tower_data;
1850 if ((tower_size < sizeof(*floor4)) ||
1851 (floor4->count_lhs != sizeof(floor4->protid)))
1852 return EPT_S_NOT_REGISTERED;
1854 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
1855 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
1856 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
1858 protseq_ops = &conn_protseq_list[i];
1863 return EPT_S_NOT_REGISTERED;
1865 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
1867 if ((status == RPC_S_OK) && protseq)
1869 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
1870 strcpy(*protseq, protseq_ops->name);
1876 /***********************************************************************
1877 * RpcNetworkIsProtseqValidW (RPCRT4.@)
1879 * Checks if the given protocol sequence is known by the RPC system.
1880 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
1883 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
1887 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
1888 ps, sizeof ps, NULL, NULL);
1889 if (rpcrt4_get_conn_protseq_ops(ps))
1892 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
1894 return RPC_S_INVALID_RPC_PROTSEQ;
1897 /***********************************************************************
1898 * RpcNetworkIsProtseqValidA (RPCRT4.@)
1900 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
1902 UNICODE_STRING protseqW;
1904 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
1906 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
1907 RtlFreeUnicodeString(&protseqW);
1910 return RPC_S_OUT_OF_MEMORY;