push 4c10733ce69b31d47c27c6c508d6169199a0e387
[wine/hacks.git] / dlls / rpcrt4 / rpc_transport.c
blob9c8796172f398385402cd6f1bf9886d411b7c51c
1 /*
2 * RPC transport layer
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 Mike McCormack
8 * Copyright 2006 Damjan Jovanovic
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
35 #if defined(__MINGW32__) || defined (_MSC_VER)
36 # include <ws2tcpip.h>
37 # ifndef EADDRINUSE
38 # define EADDRINUSE WSAEADDRINUSE
39 # endif
40 # ifndef EAGAIN
41 # define EAGAIN WSAEWOULDBLOCK
42 # endif
43 #else
44 # include <errno.h>
45 # ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 # endif
48 # include <fcntl.h>
49 # ifdef HAVE_SYS_SOCKET_H
50 # include <sys/socket.h>
51 # endif
52 # ifdef HAVE_NETINET_IN_H
53 # include <netinet/in.h>
54 # endif
55 # ifdef HAVE_NETINET_TCP_H
56 # include <netinet/tcp.h>
57 # endif
58 # ifdef HAVE_ARPA_INET_H
59 # include <arpa/inet.h>
60 # endif
61 # ifdef HAVE_NETDB_H
62 # include <netdb.h>
63 # endif
64 # ifdef HAVE_SYS_POLL_H
65 # include <sys/poll.h>
66 # endif
67 # define closesocket close
68 #endif /* defined(__MINGW32__) || defined (_MSC_VER) */
70 #include "windef.h"
71 #include "winbase.h"
72 #include "winnls.h"
73 #include "winerror.h"
74 #include "wininet.h"
75 #include "winternl.h"
76 #include "wine/unicode.h"
78 #include "rpc.h"
79 #include "rpcndr.h"
81 #include "wine/debug.h"
83 #include "rpc_binding.h"
84 #include "rpc_assoc.h"
85 #include "rpc_message.h"
86 #include "rpc_server.h"
87 #include "epm_towers.h"
89 #ifndef SOL_TCP
90 # define SOL_TCP IPPROTO_TCP
91 #endif
93 #define DEFAULT_NCACN_HTTP_TIMEOUT (60 * 1000)
95 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
97 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection);
99 /**** ncacn_np support ****/
101 typedef struct _RpcConnection_np
103 RpcConnection common;
104 HANDLE pipe;
105 OVERLAPPED ovl;
106 BOOL listening;
107 } RpcConnection_np;
109 static RpcConnection *rpcrt4_conn_np_alloc(void)
111 RpcConnection_np *npc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_np));
112 if (npc)
114 npc->pipe = NULL;
115 memset(&npc->ovl, 0, sizeof(npc->ovl));
116 npc->listening = FALSE;
118 return &npc->common;
121 static RPC_STATUS rpcrt4_conn_listen_pipe(RpcConnection_np *npc)
123 if (npc->listening)
124 return RPC_S_OK;
126 npc->listening = TRUE;
127 for (;;)
129 if (ConnectNamedPipe(npc->pipe, &npc->ovl))
130 return RPC_S_OK;
132 switch(GetLastError())
134 case ERROR_PIPE_CONNECTED:
135 SetEvent(npc->ovl.hEvent);
136 return RPC_S_OK;
137 case ERROR_IO_PENDING:
138 /* will be completed in rpcrt4_protseq_np_wait_for_new_connection */
139 return RPC_S_OK;
140 case ERROR_NO_DATA_DETECTED:
141 /* client has disconnected, retry */
142 DisconnectNamedPipe( npc->pipe );
143 break;
144 default:
145 npc->listening = FALSE;
146 WARN("Couldn't ConnectNamedPipe (error was %d)\n", GetLastError());
147 return RPC_S_OUT_OF_RESOURCES;
152 static RPC_STATUS rpcrt4_conn_create_pipe(RpcConnection *Connection, LPCSTR pname)
154 RpcConnection_np *npc = (RpcConnection_np *) Connection;
155 TRACE("listening on %s\n", pname);
157 npc->pipe = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX,
158 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
159 PIPE_UNLIMITED_INSTANCES,
160 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE, 5000, NULL);
161 if (npc->pipe == INVALID_HANDLE_VALUE) {
162 WARN("CreateNamedPipe failed with error %d\n", GetLastError());
163 if (GetLastError() == ERROR_FILE_EXISTS)
164 return RPC_S_DUPLICATE_ENDPOINT;
165 else
166 return RPC_S_CANT_CREATE_ENDPOINT;
169 memset(&npc->ovl, 0, sizeof(npc->ovl));
170 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
172 /* Note: we don't call ConnectNamedPipe here because it must be done in the
173 * server thread as the thread must be alertable */
174 return RPC_S_OK;
177 static RPC_STATUS rpcrt4_conn_open_pipe(RpcConnection *Connection, LPCSTR pname, BOOL wait)
179 RpcConnection_np *npc = (RpcConnection_np *) Connection;
180 HANDLE pipe;
181 DWORD err, dwMode;
183 TRACE("connecting to %s\n", pname);
185 while (TRUE) {
186 DWORD dwFlags = 0;
187 if (Connection->QOS)
189 dwFlags = SECURITY_SQOS_PRESENT;
190 switch (Connection->QOS->qos->ImpersonationType)
192 case RPC_C_IMP_LEVEL_DEFAULT:
193 /* FIXME: what to do here? */
194 break;
195 case RPC_C_IMP_LEVEL_ANONYMOUS:
196 dwFlags |= SECURITY_ANONYMOUS;
197 break;
198 case RPC_C_IMP_LEVEL_IDENTIFY:
199 dwFlags |= SECURITY_IDENTIFICATION;
200 break;
201 case RPC_C_IMP_LEVEL_IMPERSONATE:
202 dwFlags |= SECURITY_IMPERSONATION;
203 break;
204 case RPC_C_IMP_LEVEL_DELEGATE:
205 dwFlags |= SECURITY_DELEGATION;
206 break;
208 if (Connection->QOS->qos->IdentityTracking == RPC_C_QOS_IDENTIFY_DYNAMIC)
209 dwFlags |= SECURITY_CONTEXT_TRACKING;
211 pipe = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
212 OPEN_EXISTING, dwFlags, 0);
213 if (pipe != INVALID_HANDLE_VALUE) break;
214 err = GetLastError();
215 if (err == ERROR_PIPE_BUSY) {
216 TRACE("connection failed, error=%x\n", err);
217 return RPC_S_SERVER_TOO_BUSY;
219 if (!wait || !WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
220 err = GetLastError();
221 WARN("connection failed, error=%x\n", err);
222 return RPC_S_SERVER_UNAVAILABLE;
226 /* success */
227 memset(&npc->ovl, 0, sizeof(npc->ovl));
228 /* pipe is connected; change to message-read mode. */
229 dwMode = PIPE_READMODE_MESSAGE;
230 SetNamedPipeHandleState(pipe, &dwMode, NULL, NULL);
231 npc->ovl.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
232 npc->pipe = pipe;
234 return RPC_S_OK;
237 static RPC_STATUS rpcrt4_ncalrpc_open(RpcConnection* Connection)
239 RpcConnection_np *npc = (RpcConnection_np *) Connection;
240 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
241 RPC_STATUS r;
242 LPSTR pname;
244 /* already connected? */
245 if (npc->pipe)
246 return RPC_S_OK;
248 /* protseq=ncalrpc: supposed to use NT LPC ports,
249 * but we'll implement it with named pipes for now */
250 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
251 strcat(strcpy(pname, prefix), Connection->Endpoint);
252 r = rpcrt4_conn_open_pipe(Connection, pname, TRUE);
253 I_RpcFree(pname);
255 return r;
258 static RPC_STATUS rpcrt4_protseq_ncalrpc_open_endpoint(RpcServerProtseq* protseq, const char *endpoint)
260 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
261 RPC_STATUS r;
262 LPSTR pname;
263 RpcConnection *Connection;
264 char generated_endpoint[22];
266 if (!endpoint)
268 static LONG lrpc_nameless_id;
269 DWORD process_id = GetCurrentProcessId();
270 ULONG id = InterlockedIncrement(&lrpc_nameless_id);
271 snprintf(generated_endpoint, sizeof(generated_endpoint),
272 "LRPC%08x.%08x", process_id, id);
273 endpoint = generated_endpoint;
276 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
277 endpoint, NULL, NULL, NULL);
278 if (r != RPC_S_OK)
279 return r;
281 /* protseq=ncalrpc: supposed to use NT LPC ports,
282 * but we'll implement it with named pipes for now */
283 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
284 strcat(strcpy(pname, prefix), Connection->Endpoint);
285 r = rpcrt4_conn_create_pipe(Connection, pname);
286 I_RpcFree(pname);
288 EnterCriticalSection(&protseq->cs);
289 Connection->Next = protseq->conn;
290 protseq->conn = Connection;
291 LeaveCriticalSection(&protseq->cs);
293 return r;
296 static RPC_STATUS rpcrt4_ncacn_np_open(RpcConnection* Connection)
298 RpcConnection_np *npc = (RpcConnection_np *) Connection;
299 static const char prefix[] = "\\\\.";
300 RPC_STATUS r;
301 LPSTR pname;
303 /* already connected? */
304 if (npc->pipe)
305 return RPC_S_OK;
307 /* protseq=ncacn_np: named pipes */
308 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
309 strcat(strcpy(pname, prefix), Connection->Endpoint);
310 r = rpcrt4_conn_open_pipe(Connection, pname, FALSE);
311 I_RpcFree(pname);
313 return r;
316 static RPC_STATUS rpcrt4_protseq_ncacn_np_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
318 static const char prefix[] = "\\\\.";
319 RPC_STATUS r;
320 LPSTR pname;
321 RpcConnection *Connection;
322 char generated_endpoint[21];
324 if (!endpoint)
326 static LONG np_nameless_id;
327 DWORD process_id = GetCurrentProcessId();
328 ULONG id = InterlockedExchangeAdd(&np_nameless_id, 1 );
329 snprintf(generated_endpoint, sizeof(generated_endpoint),
330 "\\\\pipe\\\\%08x.%03x", process_id, id);
331 endpoint = generated_endpoint;
334 r = RPCRT4_CreateConnection(&Connection, TRUE, protseq->Protseq, NULL,
335 endpoint, NULL, NULL, NULL);
336 if (r != RPC_S_OK)
337 return r;
339 /* protseq=ncacn_np: named pipes */
340 pname = I_RpcAllocate(strlen(prefix) + strlen(Connection->Endpoint) + 1);
341 strcat(strcpy(pname, prefix), Connection->Endpoint);
342 r = rpcrt4_conn_create_pipe(Connection, pname);
343 I_RpcFree(pname);
345 EnterCriticalSection(&protseq->cs);
346 Connection->Next = protseq->conn;
347 protseq->conn = Connection;
348 LeaveCriticalSection(&protseq->cs);
350 return r;
353 static void rpcrt4_conn_np_handoff(RpcConnection_np *old_npc, RpcConnection_np *new_npc)
355 /* because of the way named pipes work, we'll transfer the connected pipe
356 * to the child, then reopen the server binding to continue listening */
358 new_npc->pipe = old_npc->pipe;
359 new_npc->ovl = old_npc->ovl;
360 old_npc->pipe = 0;
361 memset(&old_npc->ovl, 0, sizeof(old_npc->ovl));
362 old_npc->listening = FALSE;
365 static RPC_STATUS rpcrt4_ncacn_np_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
367 RPC_STATUS status;
368 LPSTR pname;
369 static const char prefix[] = "\\\\.";
371 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
373 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
374 strcat(strcpy(pname, prefix), old_conn->Endpoint);
375 status = rpcrt4_conn_create_pipe(old_conn, pname);
376 I_RpcFree(pname);
378 return status;
381 static RPC_STATUS rpcrt4_ncalrpc_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
383 RPC_STATUS status;
384 LPSTR pname;
385 static const char prefix[] = "\\\\.\\pipe\\lrpc\\";
387 TRACE("%s\n", old_conn->Endpoint);
389 rpcrt4_conn_np_handoff((RpcConnection_np *)old_conn, (RpcConnection_np *)new_conn);
391 pname = I_RpcAllocate(strlen(prefix) + strlen(old_conn->Endpoint) + 1);
392 strcat(strcpy(pname, prefix), old_conn->Endpoint);
393 status = rpcrt4_conn_create_pipe(old_conn, pname);
394 I_RpcFree(pname);
396 return status;
399 static int rpcrt4_conn_np_read(RpcConnection *Connection,
400 void *buffer, unsigned int count)
402 RpcConnection_np *npc = (RpcConnection_np *) Connection;
403 char *buf = buffer;
404 BOOL ret = TRUE;
405 unsigned int bytes_left = count;
407 while (bytes_left)
409 DWORD bytes_read;
410 ret = ReadFile(npc->pipe, buf, bytes_left, &bytes_read, NULL);
411 if (!ret && GetLastError() == ERROR_MORE_DATA)
412 ret = TRUE;
413 if (!ret || !bytes_read)
414 break;
415 bytes_left -= bytes_read;
416 buf += bytes_read;
418 return ret ? count : -1;
421 static int rpcrt4_conn_np_write(RpcConnection *Connection,
422 const void *buffer, unsigned int count)
424 RpcConnection_np *npc = (RpcConnection_np *) Connection;
425 const char *buf = buffer;
426 BOOL ret = TRUE;
427 unsigned int bytes_left = count;
429 while (bytes_left)
431 DWORD bytes_written;
432 ret = WriteFile(npc->pipe, buf, bytes_left, &bytes_written, NULL);
433 if (!ret || !bytes_written)
434 break;
435 bytes_left -= bytes_written;
436 buf += bytes_written;
438 return ret ? count : -1;
441 static int rpcrt4_conn_np_close(RpcConnection *Connection)
443 RpcConnection_np *npc = (RpcConnection_np *) Connection;
444 if (npc->pipe) {
445 FlushFileBuffers(npc->pipe);
446 CloseHandle(npc->pipe);
447 npc->pipe = 0;
449 if (npc->ovl.hEvent) {
450 CloseHandle(npc->ovl.hEvent);
451 npc->ovl.hEvent = 0;
453 return 0;
456 static void rpcrt4_conn_np_cancel_call(RpcConnection *Connection)
458 /* FIXME: implement when named pipe writes use overlapped I/O */
461 static int rpcrt4_conn_np_wait_for_incoming_data(RpcConnection *Connection)
463 /* FIXME: implement when named pipe writes use overlapped I/O */
464 return -1;
467 static size_t rpcrt4_ncacn_np_get_top_of_tower(unsigned char *tower_data,
468 const char *networkaddr,
469 const char *endpoint)
471 twr_empty_floor_t *smb_floor;
472 twr_empty_floor_t *nb_floor;
473 size_t size;
474 size_t networkaddr_size;
475 size_t endpoint_size;
477 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
479 networkaddr_size = networkaddr ? strlen(networkaddr) + 1 : 1;
480 endpoint_size = endpoint ? strlen(endpoint) + 1 : 1;
481 size = sizeof(*smb_floor) + endpoint_size + sizeof(*nb_floor) + networkaddr_size;
483 if (!tower_data)
484 return size;
486 smb_floor = (twr_empty_floor_t *)tower_data;
488 tower_data += sizeof(*smb_floor);
490 smb_floor->count_lhs = sizeof(smb_floor->protid);
491 smb_floor->protid = EPM_PROTOCOL_SMB;
492 smb_floor->count_rhs = endpoint_size;
494 if (endpoint)
495 memcpy(tower_data, endpoint, endpoint_size);
496 else
497 tower_data[0] = 0;
498 tower_data += endpoint_size;
500 nb_floor = (twr_empty_floor_t *)tower_data;
502 tower_data += sizeof(*nb_floor);
504 nb_floor->count_lhs = sizeof(nb_floor->protid);
505 nb_floor->protid = EPM_PROTOCOL_NETBIOS;
506 nb_floor->count_rhs = networkaddr_size;
508 if (networkaddr)
509 memcpy(tower_data, networkaddr, networkaddr_size);
510 else
511 tower_data[0] = 0;
513 return size;
516 static RPC_STATUS rpcrt4_ncacn_np_parse_top_of_tower(const unsigned char *tower_data,
517 size_t tower_size,
518 char **networkaddr,
519 char **endpoint)
521 const twr_empty_floor_t *smb_floor = (const twr_empty_floor_t *)tower_data;
522 const twr_empty_floor_t *nb_floor;
524 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
526 if (tower_size < sizeof(*smb_floor))
527 return EPT_S_NOT_REGISTERED;
529 tower_data += sizeof(*smb_floor);
530 tower_size -= sizeof(*smb_floor);
532 if ((smb_floor->count_lhs != sizeof(smb_floor->protid)) ||
533 (smb_floor->protid != EPM_PROTOCOL_SMB) ||
534 (smb_floor->count_rhs > tower_size) ||
535 (tower_data[smb_floor->count_rhs - 1] != '\0'))
536 return EPT_S_NOT_REGISTERED;
538 if (endpoint)
540 *endpoint = I_RpcAllocate(smb_floor->count_rhs);
541 if (!*endpoint)
542 return RPC_S_OUT_OF_RESOURCES;
543 memcpy(*endpoint, tower_data, smb_floor->count_rhs);
545 tower_data += smb_floor->count_rhs;
546 tower_size -= smb_floor->count_rhs;
548 if (tower_size < sizeof(*nb_floor))
549 return EPT_S_NOT_REGISTERED;
551 nb_floor = (const twr_empty_floor_t *)tower_data;
553 tower_data += sizeof(*nb_floor);
554 tower_size -= sizeof(*nb_floor);
556 if ((nb_floor->count_lhs != sizeof(nb_floor->protid)) ||
557 (nb_floor->protid != EPM_PROTOCOL_NETBIOS) ||
558 (nb_floor->count_rhs > tower_size) ||
559 (tower_data[nb_floor->count_rhs - 1] != '\0'))
560 return EPT_S_NOT_REGISTERED;
562 if (networkaddr)
564 *networkaddr = I_RpcAllocate(nb_floor->count_rhs);
565 if (!*networkaddr)
567 if (endpoint)
569 I_RpcFree(*endpoint);
570 *endpoint = NULL;
572 return RPC_S_OUT_OF_RESOURCES;
574 memcpy(*networkaddr, tower_data, nb_floor->count_rhs);
577 return RPC_S_OK;
580 typedef struct _RpcServerProtseq_np
582 RpcServerProtseq common;
583 HANDLE mgr_event;
584 } RpcServerProtseq_np;
586 static RpcServerProtseq *rpcrt4_protseq_np_alloc(void)
588 RpcServerProtseq_np *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
589 if (ps)
590 ps->mgr_event = CreateEventW(NULL, FALSE, FALSE, NULL);
591 return &ps->common;
594 static void rpcrt4_protseq_np_signal_state_changed(RpcServerProtseq *protseq)
596 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
597 SetEvent(npps->mgr_event);
600 static void *rpcrt4_protseq_np_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
602 HANDLE *objs = prev_array;
603 RpcConnection_np *conn;
604 RpcServerProtseq_np *npps = CONTAINING_RECORD(protseq, RpcServerProtseq_np, common);
606 EnterCriticalSection(&protseq->cs);
608 /* open and count connections */
609 *count = 1;
610 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
611 while (conn) {
612 rpcrt4_conn_listen_pipe(conn);
613 if (conn->ovl.hEvent)
614 (*count)++;
615 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
618 /* make array of connections */
619 if (objs)
620 objs = HeapReAlloc(GetProcessHeap(), 0, objs, *count*sizeof(HANDLE));
621 else
622 objs = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(HANDLE));
623 if (!objs)
625 ERR("couldn't allocate objs\n");
626 LeaveCriticalSection(&protseq->cs);
627 return NULL;
630 objs[0] = npps->mgr_event;
631 *count = 1;
632 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
633 while (conn) {
634 if ((objs[*count] = conn->ovl.hEvent))
635 (*count)++;
636 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
638 LeaveCriticalSection(&protseq->cs);
639 return objs;
642 static void rpcrt4_protseq_np_free_wait_array(RpcServerProtseq *protseq, void *array)
644 HeapFree(GetProcessHeap(), 0, array);
647 static int rpcrt4_protseq_np_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
649 HANDLE b_handle;
650 HANDLE *objs = wait_array;
651 DWORD res;
652 RpcConnection *cconn;
653 RpcConnection_np *conn;
655 if (!objs)
656 return -1;
660 /* an alertable wait isn't strictly necessary, but due to our
661 * overlapped I/O implementation in Wine we need to free some memory
662 * by the file user APC being called, even if no completion routine was
663 * specified at the time of starting the async operation */
664 res = WaitForMultipleObjectsEx(count, objs, FALSE, INFINITE, TRUE);
665 } while (res == WAIT_IO_COMPLETION);
667 if (res == WAIT_OBJECT_0)
668 return 0;
669 else if (res == WAIT_FAILED)
671 ERR("wait failed with error %d\n", GetLastError());
672 return -1;
674 else
676 b_handle = objs[res - WAIT_OBJECT_0];
677 /* find which connection got a RPC */
678 EnterCriticalSection(&protseq->cs);
679 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_np, common);
680 while (conn) {
681 if (b_handle == conn->ovl.hEvent) break;
682 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_np, common);
684 cconn = NULL;
685 if (conn)
686 RPCRT4_SpawnConnection(&cconn, &conn->common);
687 else
688 ERR("failed to locate connection for handle %p\n", b_handle);
689 LeaveCriticalSection(&protseq->cs);
690 if (cconn)
692 RPCRT4_new_client(cconn);
693 return 1;
695 else return -1;
699 static size_t rpcrt4_ncalrpc_get_top_of_tower(unsigned char *tower_data,
700 const char *networkaddr,
701 const char *endpoint)
703 twr_empty_floor_t *pipe_floor;
704 size_t size;
705 size_t endpoint_size;
707 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
709 endpoint_size = strlen(endpoint) + 1;
710 size = sizeof(*pipe_floor) + endpoint_size;
712 if (!tower_data)
713 return size;
715 pipe_floor = (twr_empty_floor_t *)tower_data;
717 tower_data += sizeof(*pipe_floor);
719 pipe_floor->count_lhs = sizeof(pipe_floor->protid);
720 pipe_floor->protid = EPM_PROTOCOL_PIPE;
721 pipe_floor->count_rhs = endpoint_size;
723 memcpy(tower_data, endpoint, endpoint_size);
725 return size;
728 static RPC_STATUS rpcrt4_ncalrpc_parse_top_of_tower(const unsigned char *tower_data,
729 size_t tower_size,
730 char **networkaddr,
731 char **endpoint)
733 const twr_empty_floor_t *pipe_floor = (const twr_empty_floor_t *)tower_data;
735 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
737 if (tower_size < sizeof(*pipe_floor))
738 return EPT_S_NOT_REGISTERED;
740 tower_data += sizeof(*pipe_floor);
741 tower_size -= sizeof(*pipe_floor);
743 if ((pipe_floor->count_lhs != sizeof(pipe_floor->protid)) ||
744 (pipe_floor->protid != EPM_PROTOCOL_PIPE) ||
745 (pipe_floor->count_rhs > tower_size) ||
746 (tower_data[pipe_floor->count_rhs - 1] != '\0'))
747 return EPT_S_NOT_REGISTERED;
749 if (networkaddr)
750 *networkaddr = NULL;
752 if (endpoint)
754 *endpoint = I_RpcAllocate(pipe_floor->count_rhs);
755 if (!*endpoint)
756 return RPC_S_OUT_OF_RESOURCES;
757 memcpy(*endpoint, tower_data, pipe_floor->count_rhs);
760 return RPC_S_OK;
763 /**** ncacn_ip_tcp support ****/
765 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data,
766 const char *networkaddr,
767 unsigned char tcp_protid,
768 const char *endpoint)
770 twr_tcp_floor_t *tcp_floor;
771 twr_ipv4_floor_t *ipv4_floor;
772 struct addrinfo *ai;
773 struct addrinfo hints;
774 int ret;
775 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
777 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
779 if (!tower_data)
780 return size;
782 tcp_floor = (twr_tcp_floor_t *)tower_data;
783 tower_data += sizeof(*tcp_floor);
785 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
787 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
788 tcp_floor->protid = tcp_protid;
789 tcp_floor->count_rhs = sizeof(tcp_floor->port);
791 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
792 ipv4_floor->protid = EPM_PROTOCOL_IP;
793 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
795 hints.ai_flags = AI_NUMERICHOST;
796 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
797 hints.ai_family = PF_INET;
798 hints.ai_socktype = SOCK_STREAM;
799 hints.ai_protocol = IPPROTO_TCP;
800 hints.ai_addrlen = 0;
801 hints.ai_addr = NULL;
802 hints.ai_canonname = NULL;
803 hints.ai_next = NULL;
805 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
806 if (ret)
808 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
809 if (ret)
811 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
812 return 0;
816 if (ai->ai_family == PF_INET)
818 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
819 tcp_floor->port = sin->sin_port;
820 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
822 else
824 ERR("unexpected protocol family %d\n", ai->ai_family);
825 return 0;
828 freeaddrinfo(ai);
830 return size;
833 static RPC_STATUS rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
834 size_t tower_size,
835 char **networkaddr,
836 unsigned char tcp_protid,
837 char **endpoint)
839 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
840 const twr_ipv4_floor_t *ipv4_floor;
841 struct in_addr in_addr;
843 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
845 if (tower_size < sizeof(*tcp_floor))
846 return EPT_S_NOT_REGISTERED;
848 tower_data += sizeof(*tcp_floor);
849 tower_size -= sizeof(*tcp_floor);
851 if (tower_size < sizeof(*ipv4_floor))
852 return EPT_S_NOT_REGISTERED;
854 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
856 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
857 (tcp_floor->protid != tcp_protid) ||
858 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
859 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
860 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
861 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
862 return EPT_S_NOT_REGISTERED;
864 if (endpoint)
866 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
867 if (!*endpoint)
868 return RPC_S_OUT_OF_RESOURCES;
869 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
872 if (networkaddr)
874 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
875 if (!*networkaddr)
877 if (endpoint)
879 I_RpcFree(*endpoint);
880 *endpoint = NULL;
882 return RPC_S_OUT_OF_RESOURCES;
884 in_addr.s_addr = ipv4_floor->ipv4addr;
885 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
887 ERR("inet_ntop: %s\n", strerror(errno));
888 I_RpcFree(*networkaddr);
889 *networkaddr = NULL;
890 if (endpoint)
892 I_RpcFree(*endpoint);
893 *endpoint = NULL;
895 return EPT_S_NOT_REGISTERED;
899 return RPC_S_OK;
902 #ifdef HAVE_SOCKETPAIR
904 typedef struct _RpcConnection_tcp
906 RpcConnection common;
907 int sock;
908 int cancel_fds[2];
909 } RpcConnection_tcp;
911 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
913 RpcConnection_tcp *tcpc;
914 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
915 if (tcpc == NULL)
916 return NULL;
917 tcpc->sock = -1;
918 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
920 ERR("socketpair() failed: %s\n", strerror(errno));
921 HeapFree(GetProcessHeap(), 0, tcpc);
922 return NULL;
924 return &tcpc->common;
927 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
929 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
930 int sock;
931 int ret;
932 struct addrinfo *ai;
933 struct addrinfo *ai_cur;
934 struct addrinfo hints;
936 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
938 if (tcpc->sock != -1)
939 return RPC_S_OK;
941 hints.ai_flags = 0;
942 hints.ai_family = PF_UNSPEC;
943 hints.ai_socktype = SOCK_STREAM;
944 hints.ai_protocol = IPPROTO_TCP;
945 hints.ai_addrlen = 0;
946 hints.ai_addr = NULL;
947 hints.ai_canonname = NULL;
948 hints.ai_next = NULL;
950 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
951 if (ret)
953 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
954 Connection->Endpoint, gai_strerror(ret));
955 return RPC_S_SERVER_UNAVAILABLE;
958 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
960 int val;
962 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
964 TRACE("skipping non-IP/IPv6 address family\n");
965 continue;
968 if (TRACE_ON(rpc))
970 char host[256];
971 char service[256];
972 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
973 host, sizeof(host), service, sizeof(service),
974 NI_NUMERICHOST | NI_NUMERICSERV);
975 TRACE("trying %s:%s\n", host, service);
978 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
979 if (sock == -1)
981 WARN("socket() failed: %s\n", strerror(errno));
982 continue;
985 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
987 WARN("connect() failed: %s\n", strerror(errno));
988 closesocket(sock);
989 continue;
992 /* RPC depends on having minimal latency so disable the Nagle algorithm */
993 val = 1;
994 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
995 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
997 tcpc->sock = sock;
999 freeaddrinfo(ai);
1000 TRACE("connected\n");
1001 return RPC_S_OK;
1004 freeaddrinfo(ai);
1005 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
1006 return RPC_S_SERVER_UNAVAILABLE;
1009 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, const char *endpoint)
1011 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
1012 int sock;
1013 int ret;
1014 struct addrinfo *ai;
1015 struct addrinfo *ai_cur;
1016 struct addrinfo hints;
1017 RpcConnection *first_connection = NULL;
1019 TRACE("(%p, %s)\n", protseq, endpoint);
1021 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
1022 hints.ai_family = PF_UNSPEC;
1023 hints.ai_socktype = SOCK_STREAM;
1024 hints.ai_protocol = IPPROTO_TCP;
1025 hints.ai_addrlen = 0;
1026 hints.ai_addr = NULL;
1027 hints.ai_canonname = NULL;
1028 hints.ai_next = NULL;
1030 ret = getaddrinfo(NULL, endpoint ? endpoint : "0", &hints, &ai);
1031 if (ret)
1033 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
1034 gai_strerror(ret));
1035 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
1036 return RPC_S_INVALID_ENDPOINT_FORMAT;
1037 return RPC_S_CANT_CREATE_ENDPOINT;
1040 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
1042 RpcConnection_tcp *tcpc;
1043 RPC_STATUS create_status;
1044 struct sockaddr_storage sa;
1045 socklen_t sa_len;
1046 char service[NI_MAXSERV];
1048 if (ai_cur->ai_family != AF_INET && ai_cur->ai_family != AF_INET6)
1050 TRACE("skipping non-IP/IPv6 address family\n");
1051 continue;
1054 if (TRACE_ON(rpc))
1056 char host[256];
1057 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
1058 host, sizeof(host), service, sizeof(service),
1059 NI_NUMERICHOST | NI_NUMERICSERV);
1060 TRACE("trying %s:%s\n", host, service);
1063 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
1064 if (sock == -1)
1066 WARN("socket() failed: %s\n", strerror(errno));
1067 status = RPC_S_CANT_CREATE_ENDPOINT;
1068 continue;
1071 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
1072 if (ret < 0)
1074 WARN("bind failed: %s\n", strerror(errno));
1075 closesocket(sock);
1076 if (errno == EADDRINUSE)
1077 status = RPC_S_DUPLICATE_ENDPOINT;
1078 else
1079 status = RPC_S_CANT_CREATE_ENDPOINT;
1080 continue;
1083 sa_len = sizeof(sa);
1084 if (getsockname(sock, (struct sockaddr *)&sa, &sa_len))
1086 WARN("getsockname() failed: %s\n", strerror(errno));
1087 status = RPC_S_CANT_CREATE_ENDPOINT;
1088 continue;
1091 ret = getnameinfo((struct sockaddr *)&sa, sa_len,
1092 NULL, 0, service, sizeof(service),
1093 NI_NUMERICSERV);
1094 if (ret)
1096 WARN("getnameinfo failed: %s\n", gai_strerror(ret));
1097 status = RPC_S_CANT_CREATE_ENDPOINT;
1098 continue;
1101 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
1102 protseq->Protseq, NULL,
1103 service, NULL, NULL, NULL);
1104 if (create_status != RPC_S_OK)
1106 closesocket(sock);
1107 status = create_status;
1108 continue;
1111 tcpc->sock = sock;
1112 ret = listen(sock, protseq->MaxCalls);
1113 if (ret < 0)
1115 WARN("listen failed: %s\n", strerror(errno));
1116 RPCRT4_DestroyConnection(&tcpc->common);
1117 status = RPC_S_OUT_OF_RESOURCES;
1118 continue;
1120 /* need a non-blocking socket, otherwise accept() has a potential
1121 * race-condition (poll() says it is readable, connection drops,
1122 * and accept() blocks until the next connection comes...)
1124 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1125 if (ret < 0)
1127 WARN("couldn't make socket non-blocking, error %d\n", ret);
1128 RPCRT4_DestroyConnection(&tcpc->common);
1129 status = RPC_S_OUT_OF_RESOURCES;
1130 continue;
1133 tcpc->common.Next = first_connection;
1134 first_connection = &tcpc->common;
1136 /* since IPv4 and IPv6 share the same port space, we only need one
1137 * successful bind to listen for both */
1138 break;
1141 freeaddrinfo(ai);
1143 /* if at least one connection was created for an endpoint then
1144 * return success */
1145 if (first_connection)
1147 RpcConnection *conn;
1149 /* find last element in list */
1150 for (conn = first_connection; conn->Next; conn = conn->Next)
1153 EnterCriticalSection(&protseq->cs);
1154 conn->Next = protseq->conn;
1155 protseq->conn = first_connection;
1156 LeaveCriticalSection(&protseq->cs);
1158 TRACE("listening on %s\n", endpoint);
1159 return RPC_S_OK;
1162 ERR("couldn't listen on port %s\n", endpoint);
1163 return status;
1166 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
1168 int ret;
1169 struct sockaddr_in address;
1170 socklen_t addrsize;
1171 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
1172 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
1174 addrsize = sizeof(address);
1175 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
1176 if (ret < 0)
1178 ERR("Failed to accept a TCP connection: error %d\n", ret);
1179 return RPC_S_OUT_OF_RESOURCES;
1181 /* reset to blocking behaviour */
1182 fcntl(ret, F_SETFL, 0);
1183 client->sock = ret;
1184 TRACE("Accepted a new TCP connection\n");
1185 return RPC_S_OK;
1188 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
1189 void *buffer, unsigned int count)
1191 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1192 int bytes_read = 0;
1195 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
1196 if (!r)
1197 return -1;
1198 else if (r > 0)
1199 bytes_read += r;
1200 else if (errno != EAGAIN)
1202 WARN("recv() failed: %s\n", strerror(errno));
1203 return -1;
1205 else
1207 struct pollfd pfds[2];
1208 pfds[0].fd = tcpc->sock;
1209 pfds[0].events = POLLIN;
1210 pfds[1].fd = tcpc->cancel_fds[0];
1211 pfds[1].events = POLLIN;
1212 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1214 ERR("poll() failed: %s\n", strerror(errno));
1215 return -1;
1217 if (pfds[1].revents & POLLIN) /* canceled */
1219 char dummy;
1220 read(pfds[1].fd, &dummy, sizeof(dummy));
1221 return -1;
1224 } while (bytes_read != count);
1225 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1226 return bytes_read;
1229 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1230 const void *buffer, unsigned int count)
1232 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1233 int bytes_written = 0;
1236 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1237 if (r >= 0)
1238 bytes_written += r;
1239 else if (errno != EAGAIN)
1240 return -1;
1241 else
1243 struct pollfd pfd;
1244 pfd.fd = tcpc->sock;
1245 pfd.events = POLLOUT;
1246 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1248 ERR("poll() failed: %s\n", strerror(errno));
1249 return -1;
1252 } while (bytes_written != count);
1253 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1254 return bytes_written;
1257 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1259 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1261 TRACE("%d\n", tcpc->sock);
1263 if (tcpc->sock != -1)
1264 closesocket(tcpc->sock);
1265 tcpc->sock = -1;
1266 close(tcpc->cancel_fds[0]);
1267 close(tcpc->cancel_fds[1]);
1268 return 0;
1271 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1273 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1274 char dummy = 1;
1276 TRACE("%p\n", Connection);
1278 write(tcpc->cancel_fds[1], &dummy, 1);
1281 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1283 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1284 struct pollfd pfds[2];
1286 TRACE("%p\n", Connection);
1288 pfds[0].fd = tcpc->sock;
1289 pfds[0].events = POLLIN;
1290 pfds[1].fd = tcpc->cancel_fds[0];
1291 pfds[1].events = POLLIN;
1292 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1294 ERR("poll() failed: %s\n", strerror(errno));
1295 return -1;
1297 if (pfds[1].revents & POLLIN) /* canceled */
1299 char dummy;
1300 read(pfds[1].fd, &dummy, sizeof(dummy));
1301 return -1;
1304 return 0;
1307 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1308 const char *networkaddr,
1309 const char *endpoint)
1311 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
1312 EPM_PROTOCOL_TCP, endpoint);
1315 typedef struct _RpcServerProtseq_sock
1317 RpcServerProtseq common;
1318 int mgr_event_rcv;
1319 int mgr_event_snd;
1320 } RpcServerProtseq_sock;
1322 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1324 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1325 if (ps)
1327 int fds[2];
1328 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1330 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1331 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1332 ps->mgr_event_rcv = fds[0];
1333 ps->mgr_event_snd = fds[1];
1335 else
1337 ERR("socketpair failed with error %s\n", strerror(errno));
1338 HeapFree(GetProcessHeap(), 0, ps);
1339 return NULL;
1342 return &ps->common;
1345 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1347 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1348 char dummy = 1;
1349 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1352 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1354 struct pollfd *poll_info = prev_array;
1355 RpcConnection_tcp *conn;
1356 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1358 EnterCriticalSection(&protseq->cs);
1360 /* open and count connections */
1361 *count = 1;
1362 conn = (RpcConnection_tcp *)protseq->conn;
1363 while (conn) {
1364 if (conn->sock != -1)
1365 (*count)++;
1366 conn = (RpcConnection_tcp *)conn->common.Next;
1369 /* make array of connections */
1370 if (poll_info)
1371 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1372 else
1373 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1374 if (!poll_info)
1376 ERR("couldn't allocate poll_info\n");
1377 LeaveCriticalSection(&protseq->cs);
1378 return NULL;
1381 poll_info[0].fd = sockps->mgr_event_rcv;
1382 poll_info[0].events = POLLIN;
1383 *count = 1;
1384 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1385 while (conn) {
1386 if (conn->sock != -1)
1388 poll_info[*count].fd = conn->sock;
1389 poll_info[*count].events = POLLIN;
1390 (*count)++;
1392 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1394 LeaveCriticalSection(&protseq->cs);
1395 return poll_info;
1398 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1400 HeapFree(GetProcessHeap(), 0, array);
1403 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1405 struct pollfd *poll_info = wait_array;
1406 int ret;
1407 unsigned int i;
1408 RpcConnection *cconn;
1409 RpcConnection_tcp *conn;
1411 if (!poll_info)
1412 return -1;
1414 ret = poll(poll_info, count, -1);
1415 if (ret < 0)
1417 ERR("poll failed with error %d\n", ret);
1418 return -1;
1421 for (i = 0; i < count; i++)
1422 if (poll_info[i].revents & POLLIN)
1424 /* RPC server event */
1425 if (i == 0)
1427 char dummy;
1428 read(poll_info[0].fd, &dummy, sizeof(dummy));
1429 return 0;
1432 /* find which connection got a RPC */
1433 EnterCriticalSection(&protseq->cs);
1434 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1435 while (conn) {
1436 if (poll_info[i].fd == conn->sock) break;
1437 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1439 cconn = NULL;
1440 if (conn)
1441 RPCRT4_SpawnConnection(&cconn, &conn->common);
1442 else
1443 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1444 LeaveCriticalSection(&protseq->cs);
1445 if (cconn)
1446 RPCRT4_new_client(cconn);
1447 else
1448 return -1;
1451 return 1;
1454 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1455 size_t tower_size,
1456 char **networkaddr,
1457 char **endpoint)
1459 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
1460 networkaddr, EPM_PROTOCOL_TCP,
1461 endpoint);
1464 #endif /* HAVE_SOCKETPAIR */
1466 /**** ncacn_http support ****/
1468 /* 60 seconds is the period native uses */
1469 #define HTTP_IDLE_TIME 60000
1471 /* reference counted to avoid a race between a cancelled call's connection
1472 * being destroyed and the asynchronous InternetReadFileEx call being
1473 * completed */
1474 typedef struct _RpcHttpAsyncData
1476 LONG refs;
1477 HANDLE completion_event;
1478 INTERNET_BUFFERSA inet_buffers;
1479 void *destination_buffer; /* the address that inet_buffers.lpvBuffer will be
1480 * copied into when the call completes */
1481 CRITICAL_SECTION cs;
1482 } RpcHttpAsyncData;
1484 static ULONG RpcHttpAsyncData_AddRef(RpcHttpAsyncData *data)
1486 return InterlockedIncrement(&data->refs);
1489 static ULONG RpcHttpAsyncData_Release(RpcHttpAsyncData *data)
1491 ULONG refs = InterlockedDecrement(&data->refs);
1492 if (!refs)
1494 TRACE("destroying async data %p\n", data);
1495 CloseHandle(data->completion_event);
1496 HeapFree(GetProcessHeap(), 0, data->inet_buffers.lpvBuffer);
1497 DeleteCriticalSection(&data->cs);
1498 HeapFree(GetProcessHeap(), 0, data);
1500 return refs;
1503 typedef struct _RpcConnection_http
1505 RpcConnection common;
1506 HINTERNET app_info;
1507 HINTERNET session;
1508 HINTERNET in_request;
1509 HINTERNET out_request;
1510 HANDLE timer_cancelled;
1511 HANDLE cancel_event;
1512 DWORD last_sent_time;
1513 ULONG bytes_received;
1514 ULONG flow_control_mark; /* send a control packet to the server when this many bytes received */
1515 ULONG flow_control_increment; /* number of bytes to increment flow_control_mark by */
1516 UUID connection_uuid;
1517 UUID in_pipe_uuid;
1518 UUID out_pipe_uuid;
1519 RpcHttpAsyncData *async_data;
1520 } RpcConnection_http;
1522 static RpcConnection *rpcrt4_ncacn_http_alloc(void)
1524 RpcConnection_http *httpc;
1525 httpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*httpc));
1526 if (!httpc) return NULL;
1527 httpc->async_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcHttpAsyncData));
1528 if (!httpc->async_data)
1530 HeapFree(GetProcessHeap(), 0, httpc);
1531 return NULL;
1533 TRACE("async data = %p\n", httpc->async_data);
1534 httpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1535 httpc->async_data->refs = 1;
1536 httpc->async_data->inet_buffers.dwStructSize = sizeof(INTERNET_BUFFERSA);
1537 httpc->async_data->inet_buffers.lpvBuffer = NULL;
1538 httpc->async_data->destination_buffer = NULL;
1539 InitializeCriticalSection(&httpc->async_data->cs);
1540 return &httpc->common;
1543 typedef struct _HttpTimerThreadData
1545 PVOID timer_param;
1546 DWORD *last_sent_time;
1547 HANDLE timer_cancelled;
1548 } HttpTimerThreadData;
1550 static VOID CALLBACK rpcrt4_http_keep_connection_active_timer_proc(PVOID param, BOOLEAN dummy)
1552 HINTERNET in_request = param;
1553 RpcPktHdr *idle_pkt;
1555 idle_pkt = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x0001,
1556 0, 0);
1557 if (idle_pkt)
1559 DWORD bytes_written;
1560 InternetWriteFile(in_request, idle_pkt, idle_pkt->common.frag_len, &bytes_written);
1561 RPCRT4_FreeHeader(idle_pkt);
1565 static inline DWORD rpcrt4_http_timer_calc_timeout(DWORD *last_sent_time)
1567 DWORD cur_time = GetTickCount();
1568 DWORD cached_last_sent_time = *last_sent_time;
1569 return HTTP_IDLE_TIME - (cur_time - cached_last_sent_time > HTTP_IDLE_TIME ? 0 : cur_time - cached_last_sent_time);
1572 static DWORD CALLBACK rpcrt4_http_timer_thread(PVOID param)
1574 HttpTimerThreadData *data_in = param;
1575 HttpTimerThreadData data;
1576 DWORD timeout;
1578 data = *data_in;
1579 HeapFree(GetProcessHeap(), 0, data_in);
1581 for (timeout = HTTP_IDLE_TIME;
1582 WaitForSingleObject(data.timer_cancelled, timeout) == WAIT_TIMEOUT;
1583 timeout = rpcrt4_http_timer_calc_timeout(data.last_sent_time))
1585 /* are we too soon after last send? */
1586 if (GetTickCount() - HTTP_IDLE_TIME < *data.last_sent_time)
1587 continue;
1588 rpcrt4_http_keep_connection_active_timer_proc(data.timer_param, TRUE);
1591 CloseHandle(data.timer_cancelled);
1592 return 0;
1595 static VOID WINAPI rpcrt4_http_internet_callback(
1596 HINTERNET hInternet,
1597 DWORD_PTR dwContext,
1598 DWORD dwInternetStatus,
1599 LPVOID lpvStatusInformation,
1600 DWORD dwStatusInformationLength)
1602 RpcHttpAsyncData *async_data = (RpcHttpAsyncData *)dwContext;
1604 switch (dwInternetStatus)
1606 case INTERNET_STATUS_REQUEST_COMPLETE:
1607 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1608 if (async_data)
1610 if (async_data->inet_buffers.lpvBuffer)
1612 EnterCriticalSection(&async_data->cs);
1613 if (async_data->destination_buffer)
1615 memcpy(async_data->destination_buffer,
1616 async_data->inet_buffers.lpvBuffer,
1617 async_data->inet_buffers.dwBufferLength);
1618 async_data->destination_buffer = NULL;
1620 LeaveCriticalSection(&async_data->cs);
1622 HeapFree(GetProcessHeap(), 0, async_data->inet_buffers.lpvBuffer);
1623 async_data->inet_buffers.lpvBuffer = NULL;
1624 SetEvent(async_data->completion_event);
1625 RpcHttpAsyncData_Release(async_data);
1627 break;
1631 static RPC_STATUS rpcrt4_http_check_response(HINTERNET hor)
1633 BOOL ret;
1634 DWORD status_code;
1635 DWORD size;
1636 DWORD index;
1637 WCHAR buf[32];
1638 WCHAR *status_text = buf;
1639 TRACE("\n");
1641 index = 0;
1642 size = sizeof(status_code);
1643 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status_code, &size, &index);
1644 if (!ret)
1645 return GetLastError();
1646 if (status_code < 400)
1647 return RPC_S_OK;
1648 index = 0;
1649 size = sizeof(buf);
1650 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
1651 if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1653 status_text = HeapAlloc(GetProcessHeap(), 0, size);
1654 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
1657 ERR("server returned: %d %s\n", status_code, ret ? debugstr_w(status_text) : "<status text unavailable>");
1658 if(status_text != buf) HeapFree(GetProcessHeap(), 0, status_text);
1660 if (status_code == HTTP_STATUS_DENIED)
1661 return ERROR_ACCESS_DENIED;
1662 return RPC_S_SERVER_UNAVAILABLE;
1665 static RPC_STATUS rpcrt4_http_internet_connect(RpcConnection_http *httpc)
1667 static const WCHAR wszUserAgent[] = {'M','S','R','P','C',0};
1668 LPWSTR proxy = NULL;
1669 LPWSTR user = NULL;
1670 LPWSTR password = NULL;
1671 LPWSTR servername = NULL;
1672 const WCHAR *option;
1673 INTERNET_PORT port = INTERNET_INVALID_PORT_NUMBER; /* use default port */
1675 if (httpc->common.QOS &&
1676 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP))
1678 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_cred = httpc->common.QOS->qos->u.HttpCredentials;
1679 if (http_cred->TransportCredentials)
1681 WCHAR *p;
1682 const SEC_WINNT_AUTH_IDENTITY_W *cred = http_cred->TransportCredentials;
1683 ULONG len = cred->DomainLength + 1 + cred->UserLength;
1684 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1685 if (!user)
1686 return RPC_S_OUT_OF_RESOURCES;
1687 p = user;
1688 if (cred->DomainLength)
1690 memcpy(p, cred->Domain, cred->DomainLength * sizeof(WCHAR));
1691 p += cred->DomainLength;
1692 *p = '\\';
1693 p++;
1695 memcpy(p, cred->User, cred->UserLength * sizeof(WCHAR));
1696 p[cred->UserLength] = 0;
1698 password = RPCRT4_strndupW(cred->Password, cred->PasswordLength);
1702 for (option = httpc->common.NetworkOptions; option;
1703 option = (strchrW(option, ',') ? strchrW(option, ',')+1 : NULL))
1705 static const WCHAR wszRpcProxy[] = {'R','p','c','P','r','o','x','y','=',0};
1706 static const WCHAR wszHttpProxy[] = {'H','t','t','p','P','r','o','x','y','=',0};
1708 if (!strncmpiW(option, wszRpcProxy, sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1))
1710 const WCHAR *value_start = option + sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1;
1711 const WCHAR *value_end;
1712 const WCHAR *p;
1714 value_end = strchrW(option, ',');
1715 if (!value_end)
1716 value_end = value_start + strlenW(value_start);
1717 for (p = value_start; p < value_end; p++)
1718 if (*p == ':')
1720 port = atoiW(p+1);
1721 value_end = p;
1722 break;
1724 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
1725 servername = RPCRT4_strndupW(value_start, value_end-value_start);
1727 else if (!strncmpiW(option, wszHttpProxy, sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1))
1729 const WCHAR *value_start = option + sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1;
1730 const WCHAR *value_end;
1732 value_end = strchrW(option, ',');
1733 if (!value_end)
1734 value_end = value_start + strlenW(value_start);
1735 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
1736 proxy = RPCRT4_strndupW(value_start, value_end-value_start);
1738 else
1739 FIXME("unhandled option %s\n", debugstr_w(option));
1742 httpc->app_info = InternetOpenW(wszUserAgent, proxy ? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
1743 NULL, NULL, INTERNET_FLAG_ASYNC);
1744 if (!httpc->app_info)
1746 HeapFree(GetProcessHeap(), 0, password);
1747 HeapFree(GetProcessHeap(), 0, user);
1748 ERR("InternetOpenW failed with error %d\n", GetLastError());
1749 return RPC_S_SERVER_UNAVAILABLE;
1751 InternetSetStatusCallbackW(httpc->app_info, rpcrt4_http_internet_callback);
1753 /* if no RpcProxy option specified, set the HTTP server address to the
1754 * RPC server address */
1755 if (!servername)
1757 servername = HeapAlloc(GetProcessHeap(), 0, (strlen(httpc->common.NetworkAddr) + 1)*sizeof(WCHAR));
1758 if (!servername)
1760 HeapFree(GetProcessHeap(), 0, password);
1761 HeapFree(GetProcessHeap(), 0, user);
1762 return RPC_S_OUT_OF_RESOURCES;
1764 MultiByteToWideChar(CP_ACP, 0, httpc->common.NetworkAddr, -1, servername, strlen(httpc->common.NetworkAddr) + 1);
1767 httpc->session = InternetConnectW(httpc->app_info, servername, port, user, password,
1768 INTERNET_SERVICE_HTTP, 0, 0);
1770 HeapFree(GetProcessHeap(), 0, password);
1771 HeapFree(GetProcessHeap(), 0, user);
1772 HeapFree(GetProcessHeap(), 0, servername);
1774 if (!httpc->session)
1776 ERR("InternetConnectW failed with error %d\n", GetLastError());
1777 return RPC_S_SERVER_UNAVAILABLE;
1780 return RPC_S_OK;
1783 /* prepare the in pipe for use by RPC packets */
1784 static RPC_STATUS rpcrt4_http_prepare_in_pipe(HINTERNET in_request, RpcHttpAsyncData *async_data,
1785 const UUID *connection_uuid,
1786 const UUID *in_pipe_uuid,
1787 const UUID *association_uuid)
1789 BYTE packet[44];
1790 BOOL ret;
1791 RPC_STATUS status;
1792 RpcPktHdr *hdr;
1793 INTERNET_BUFFERSW buffers_in;
1794 DWORD bytes_read, bytes_written;
1796 /* prepare in pipe */
1797 ResetEvent(async_data->completion_event);
1798 RpcHttpAsyncData_AddRef(async_data);
1799 ret = HttpSendRequestW(in_request, NULL, 0, NULL, 0);
1800 if (!ret)
1802 if (GetLastError() == ERROR_IO_PENDING)
1803 WaitForSingleObject(async_data->completion_event, INFINITE);
1804 else
1806 RpcHttpAsyncData_Release(async_data);
1807 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1808 return RPC_S_SERVER_UNAVAILABLE;
1811 status = rpcrt4_http_check_response(in_request);
1812 if (status != RPC_S_OK) return status;
1814 InternetReadFile(in_request, packet, 20, &bytes_read);
1815 /* FIXME: do something with retrieved data */
1817 memset(&buffers_in, 0, sizeof(buffers_in));
1818 buffers_in.dwStructSize = sizeof(buffers_in);
1819 /* FIXME: get this from the registry */
1820 buffers_in.dwBufferTotal = 1024 * 1024 * 1024; /* 1Gb */
1821 ResetEvent(async_data->completion_event);
1822 RpcHttpAsyncData_AddRef(async_data);
1823 ret = HttpSendRequestExW(in_request, &buffers_in, NULL, 0, 0);
1824 if (!ret)
1826 if (GetLastError() == ERROR_IO_PENDING)
1827 WaitForSingleObject(async_data->completion_event, INFINITE);
1828 else
1830 RpcHttpAsyncData_Release(async_data);
1831 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
1832 return RPC_S_SERVER_UNAVAILABLE;
1836 TRACE("sending HTTP connect header to server\n");
1837 hdr = RPCRT4_BuildHttpConnectHeader(0, FALSE, connection_uuid, in_pipe_uuid, association_uuid);
1838 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
1839 ret = InternetWriteFile(in_request, hdr, hdr->common.frag_len, &bytes_written);
1840 RPCRT4_FreeHeader(hdr);
1841 if (!ret)
1843 ERR("InternetWriteFile failed with error %d\n", GetLastError());
1844 return RPC_S_SERVER_UNAVAILABLE;
1847 return RPC_S_OK;
1850 static RPC_STATUS rpcrt4_http_read_http_packet(HINTERNET request, RpcPktHdr *hdr, BYTE **data)
1852 BOOL ret;
1853 DWORD bytes_read;
1854 unsigned short data_len;
1856 ret = InternetReadFile(request, hdr, sizeof(hdr->common), &bytes_read);
1857 if (!ret)
1858 return RPC_S_SERVER_UNAVAILABLE;
1859 if (hdr->common.ptype != PKT_HTTP || hdr->common.frag_len < sizeof(hdr->http))
1861 ERR("wrong packet type received %d or wrong frag_len %d\n",
1862 hdr->common.ptype, hdr->common.frag_len);
1863 return RPC_S_PROTOCOL_ERROR;
1866 ret = InternetReadFile(request, &hdr->common + 1, sizeof(hdr->http) - sizeof(hdr->common), &bytes_read);
1867 if (!ret)
1868 return RPC_S_SERVER_UNAVAILABLE;
1870 data_len = hdr->common.frag_len - sizeof(hdr->http);
1871 if (data_len)
1873 *data = HeapAlloc(GetProcessHeap(), 0, data_len);
1874 if (!*data)
1875 return RPC_S_OUT_OF_RESOURCES;
1876 ret = InternetReadFile(request, *data, data_len, &bytes_read);
1877 if (!ret)
1879 HeapFree(GetProcessHeap(), 0, *data);
1880 return RPC_S_SERVER_UNAVAILABLE;
1883 else
1884 *data = NULL;
1886 if (!RPCRT4_IsValidHttpPacket(hdr, *data, data_len))
1888 ERR("invalid http packet\n");
1889 return RPC_S_PROTOCOL_ERROR;
1892 return RPC_S_OK;
1895 /* prepare the out pipe for use by RPC packets */
1896 static RPC_STATUS rpcrt4_http_prepare_out_pipe(HINTERNET out_request,
1897 RpcHttpAsyncData *async_data,
1898 const UUID *connection_uuid,
1899 const UUID *out_pipe_uuid,
1900 ULONG *flow_control_increment)
1902 BYTE packet[20];
1903 BOOL ret;
1904 RPC_STATUS status;
1905 RpcPktHdr *hdr;
1906 DWORD bytes_read;
1907 BYTE *data_from_server;
1908 RpcPktHdr pkt_from_server;
1909 ULONG field1, field3;
1911 ResetEvent(async_data->completion_event);
1912 RpcHttpAsyncData_AddRef(async_data);
1913 ret = HttpSendRequestW(out_request, NULL, 0, NULL, 0);
1914 if (!ret)
1916 if (GetLastError() == ERROR_IO_PENDING)
1917 WaitForSingleObject(async_data->completion_event, INFINITE);
1918 else
1920 RpcHttpAsyncData_Release(async_data);
1921 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1922 return RPC_S_SERVER_UNAVAILABLE;
1925 status = rpcrt4_http_check_response(out_request);
1926 if (status != RPC_S_OK) return status;
1928 InternetReadFile(out_request, packet, 20, &bytes_read);
1929 /* FIXME: do something with retrieved data */
1931 hdr = RPCRT4_BuildHttpConnectHeader(0, TRUE, connection_uuid, out_pipe_uuid, NULL);
1932 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
1933 ResetEvent(async_data->completion_event);
1934 RpcHttpAsyncData_AddRef(async_data);
1935 ret = HttpSendRequestW(out_request, NULL, 0, hdr, hdr->common.frag_len);
1936 if (!ret)
1938 if (GetLastError() == ERROR_IO_PENDING)
1939 WaitForSingleObject(async_data->completion_event, INFINITE);
1940 else
1942 RpcHttpAsyncData_Release(async_data);
1943 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1944 RPCRT4_FreeHeader(hdr);
1945 return RPC_S_SERVER_UNAVAILABLE;
1948 RPCRT4_FreeHeader(hdr);
1949 status = rpcrt4_http_check_response(out_request);
1950 if (status != RPC_S_OK) return status;
1952 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
1953 &data_from_server);
1954 if (status != RPC_S_OK) return status;
1955 status = RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server, data_from_server,
1956 &field1);
1957 HeapFree(GetProcessHeap(), 0, data_from_server);
1958 if (status != RPC_S_OK) return status;
1959 TRACE("received (%d) from first prepare header\n", field1);
1961 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
1962 &data_from_server);
1963 if (status != RPC_S_OK) return status;
1964 status = RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server, data_from_server,
1965 &field1, flow_control_increment,
1966 &field3);
1967 HeapFree(GetProcessHeap(), 0, data_from_server);
1968 if (status != RPC_S_OK) return status;
1969 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
1971 return RPC_S_OK;
1974 static RPC_STATUS rpcrt4_ncacn_http_open(RpcConnection* Connection)
1976 RpcConnection_http *httpc = (RpcConnection_http *)Connection;
1977 static const WCHAR wszVerbIn[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
1978 static const WCHAR wszVerbOut[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
1979 static const WCHAR wszRpcProxyPrefix[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
1980 static const WCHAR wszColon[] = {':',0};
1981 static const WCHAR wszAcceptType[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
1982 LPCWSTR wszAcceptTypes[] = { wszAcceptType, NULL };
1983 WCHAR *url;
1984 RPC_STATUS status;
1985 BOOL secure;
1986 HttpTimerThreadData *timer_data;
1987 HANDLE thread;
1989 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
1991 if (Connection->server)
1993 ERR("ncacn_http servers not supported yet\n");
1994 return RPC_S_SERVER_UNAVAILABLE;
1997 if (httpc->in_request)
1998 return RPC_S_OK;
2000 httpc->async_data->completion_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2002 status = UuidCreate(&httpc->connection_uuid);
2003 status = UuidCreate(&httpc->in_pipe_uuid);
2004 status = UuidCreate(&httpc->out_pipe_uuid);
2006 status = rpcrt4_http_internet_connect(httpc);
2007 if (status != RPC_S_OK)
2008 return status;
2010 url = HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix) + (strlen(Connection->NetworkAddr) + 1 + strlen(Connection->Endpoint))*sizeof(WCHAR));
2011 if (!url)
2012 return RPC_S_OUT_OF_MEMORY;
2013 memcpy(url, wszRpcProxyPrefix, sizeof(wszRpcProxyPrefix));
2014 MultiByteToWideChar(CP_ACP, 0, Connection->NetworkAddr, -1, url+sizeof(wszRpcProxyPrefix)/sizeof(wszRpcProxyPrefix[0])-1, strlen(Connection->NetworkAddr)+1);
2015 strcatW(url, wszColon);
2016 MultiByteToWideChar(CP_ACP, 0, Connection->Endpoint, -1, url+strlenW(url), strlen(Connection->Endpoint)+1);
2018 secure = httpc->common.QOS &&
2019 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
2020 (httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL);
2022 httpc->in_request = HttpOpenRequestW(httpc->session, wszVerbIn, url, NULL, NULL,
2023 wszAcceptTypes,
2024 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
2025 (DWORD_PTR)httpc->async_data);
2026 if (!httpc->in_request)
2028 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2029 return RPC_S_SERVER_UNAVAILABLE;
2031 httpc->out_request = HttpOpenRequestW(httpc->session, wszVerbOut, url, NULL, NULL,
2032 wszAcceptTypes,
2033 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
2034 (DWORD_PTR)httpc->async_data);
2035 if (!httpc->out_request)
2037 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
2038 return RPC_S_SERVER_UNAVAILABLE;
2041 status = rpcrt4_http_prepare_in_pipe(httpc->in_request,
2042 httpc->async_data,
2043 &httpc->connection_uuid,
2044 &httpc->in_pipe_uuid,
2045 &Connection->assoc->http_uuid);
2046 if (status != RPC_S_OK)
2047 return status;
2049 status = rpcrt4_http_prepare_out_pipe(httpc->out_request,
2050 httpc->async_data,
2051 &httpc->connection_uuid,
2052 &httpc->out_pipe_uuid,
2053 &httpc->flow_control_increment);
2054 if (status != RPC_S_OK)
2055 return status;
2057 httpc->flow_control_mark = httpc->flow_control_increment / 2;
2058 httpc->last_sent_time = GetTickCount();
2059 httpc->timer_cancelled = CreateEventW(NULL, FALSE, FALSE, NULL);
2061 timer_data = HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data));
2062 if (!timer_data)
2063 return ERROR_OUTOFMEMORY;
2064 timer_data->timer_param = httpc->in_request;
2065 timer_data->last_sent_time = &httpc->last_sent_time;
2066 timer_data->timer_cancelled = httpc->timer_cancelled;
2067 /* FIXME: should use CreateTimerQueueTimer when implemented */
2068 thread = CreateThread(NULL, 0, rpcrt4_http_timer_thread, timer_data, 0, NULL);
2069 if (!thread)
2071 HeapFree(GetProcessHeap(), 0, timer_data);
2072 return GetLastError();
2074 CloseHandle(thread);
2076 return RPC_S_OK;
2079 static RPC_STATUS rpcrt4_ncacn_http_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
2081 assert(0);
2082 return RPC_S_SERVER_UNAVAILABLE;
2085 static int rpcrt4_ncacn_http_read(RpcConnection *Connection,
2086 void *buffer, unsigned int count)
2088 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2089 char *buf = buffer;
2090 BOOL ret = TRUE;
2091 unsigned int bytes_left = count;
2093 ResetEvent(httpc->async_data->completion_event);
2094 while (bytes_left)
2096 RpcHttpAsyncData_AddRef(httpc->async_data);
2097 httpc->async_data->inet_buffers.dwBufferLength = bytes_left;
2098 httpc->async_data->inet_buffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, bytes_left);
2099 httpc->async_data->destination_buffer = buf;
2100 ret = InternetReadFileExA(httpc->out_request, &httpc->async_data->inet_buffers, IRF_ASYNC, 0);
2101 if (ret)
2103 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2104 * async ref now */
2105 RpcHttpAsyncData_Release(httpc->async_data);
2106 memcpy(buf, httpc->async_data->inet_buffers.lpvBuffer,
2107 httpc->async_data->inet_buffers.dwBufferLength);
2108 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2109 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2110 httpc->async_data->destination_buffer = NULL;
2112 else
2114 if (GetLastError() == ERROR_IO_PENDING)
2116 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2117 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2118 if (result == WAIT_OBJECT_0)
2119 ret = TRUE;
2120 else
2122 TRACE("call cancelled\n");
2123 EnterCriticalSection(&httpc->async_data->cs);
2124 httpc->async_data->destination_buffer = NULL;
2125 LeaveCriticalSection(&httpc->async_data->cs);
2126 break;
2129 else
2131 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2132 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2133 httpc->async_data->destination_buffer = NULL;
2134 RpcHttpAsyncData_Release(httpc->async_data);
2135 break;
2138 if (!httpc->async_data->inet_buffers.dwBufferLength)
2139 break;
2140 bytes_left -= httpc->async_data->inet_buffers.dwBufferLength;
2141 buf += httpc->async_data->inet_buffers.dwBufferLength;
2143 TRACE("%p %p %u -> %s\n", httpc->out_request, buffer, count, ret ? "TRUE" : "FALSE");
2144 return ret ? count : -1;
2147 static RPC_STATUS rpcrt4_ncacn_http_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
2149 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2150 RPC_STATUS status;
2151 DWORD hdr_length;
2152 LONG dwRead;
2153 RpcPktCommonHdr common_hdr;
2155 *Header = NULL;
2157 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
2159 again:
2160 /* read packet common header */
2161 dwRead = rpcrt4_ncacn_http_read(Connection, &common_hdr, sizeof(common_hdr));
2162 if (dwRead != sizeof(common_hdr)) {
2163 WARN("Short read of header, %d bytes\n", dwRead);
2164 status = RPC_S_PROTOCOL_ERROR;
2165 goto fail;
2167 if (!memcmp(&common_hdr, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2168 !memcmp(&common_hdr, "HTTP/1.0", sizeof("HTTP/1.0")))
2170 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr));
2171 status = RPC_S_PROTOCOL_ERROR;
2172 goto fail;
2175 status = RPCRT4_ValidateCommonHeader(&common_hdr);
2176 if (status != RPC_S_OK) goto fail;
2178 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
2179 if (hdr_length == 0) {
2180 WARN("header length == 0\n");
2181 status = RPC_S_PROTOCOL_ERROR;
2182 goto fail;
2185 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
2186 if (!*Header)
2188 status = RPC_S_OUT_OF_RESOURCES;
2189 goto fail;
2191 memcpy(*Header, &common_hdr, sizeof(common_hdr));
2193 /* read the rest of packet header */
2194 dwRead = rpcrt4_ncacn_http_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
2195 if (dwRead != hdr_length - sizeof(common_hdr)) {
2196 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
2197 status = RPC_S_PROTOCOL_ERROR;
2198 goto fail;
2201 if (common_hdr.frag_len - hdr_length)
2203 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
2204 if (!*Payload)
2206 status = RPC_S_OUT_OF_RESOURCES;
2207 goto fail;
2210 dwRead = rpcrt4_ncacn_http_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
2211 if (dwRead != common_hdr.frag_len - hdr_length)
2213 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
2214 status = RPC_S_PROTOCOL_ERROR;
2215 goto fail;
2218 else
2219 *Payload = NULL;
2221 if ((*Header)->common.ptype == PKT_HTTP)
2223 if (!RPCRT4_IsValidHttpPacket(*Header, *Payload, common_hdr.frag_len - hdr_length))
2225 ERR("invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
2226 status = RPC_S_PROTOCOL_ERROR;
2227 goto fail;
2229 if ((*Header)->http.flags == 0x0001)
2231 TRACE("http idle packet, waiting for real packet\n");
2232 if ((*Header)->http.num_data_items != 0)
2234 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
2235 status = RPC_S_PROTOCOL_ERROR;
2236 goto fail;
2239 else if ((*Header)->http.flags == 0x0002)
2241 ULONG bytes_transmitted;
2242 ULONG flow_control_increment;
2243 UUID pipe_uuid;
2244 status = RPCRT4_ParseHttpFlowControlHeader(*Header, *Payload,
2245 Connection->server,
2246 &bytes_transmitted,
2247 &flow_control_increment,
2248 &pipe_uuid);
2249 if (status != RPC_S_OK)
2250 goto fail;
2251 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2252 bytes_transmitted, flow_control_increment, debugstr_guid(&pipe_uuid));
2253 /* FIXME: do something with parsed data */
2255 else
2257 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
2258 status = RPC_S_PROTOCOL_ERROR;
2259 goto fail;
2261 RPCRT4_FreeHeader(*Header);
2262 *Header = NULL;
2263 HeapFree(GetProcessHeap(), 0, *Payload);
2264 *Payload = NULL;
2265 goto again;
2268 /* success */
2269 status = RPC_S_OK;
2271 httpc->bytes_received += common_hdr.frag_len;
2273 TRACE("httpc->bytes_received = 0x%x\n", httpc->bytes_received);
2275 if (httpc->bytes_received > httpc->flow_control_mark)
2277 RpcPktHdr *hdr = RPCRT4_BuildHttpFlowControlHeader(httpc->common.server,
2278 httpc->bytes_received,
2279 httpc->flow_control_increment,
2280 &httpc->out_pipe_uuid);
2281 if (hdr)
2283 DWORD bytes_written;
2284 BOOL ret2;
2285 TRACE("sending flow control packet at 0x%x\n", httpc->bytes_received);
2286 ret2 = InternetWriteFile(httpc->in_request, hdr, hdr->common.frag_len, &bytes_written);
2287 RPCRT4_FreeHeader(hdr);
2288 if (ret2)
2289 httpc->flow_control_mark = httpc->bytes_received + httpc->flow_control_increment / 2;
2293 fail:
2294 if (status != RPC_S_OK) {
2295 RPCRT4_FreeHeader(*Header);
2296 *Header = NULL;
2297 HeapFree(GetProcessHeap(), 0, *Payload);
2298 *Payload = NULL;
2300 return status;
2303 static int rpcrt4_ncacn_http_write(RpcConnection *Connection,
2304 const void *buffer, unsigned int count)
2306 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2307 DWORD bytes_written;
2308 BOOL ret;
2310 httpc->last_sent_time = ~0UL; /* disable idle packet sending */
2311 ret = InternetWriteFile(httpc->in_request, buffer, count, &bytes_written);
2312 httpc->last_sent_time = GetTickCount();
2313 TRACE("%p %p %u -> %s\n", httpc->in_request, buffer, count, ret ? "TRUE" : "FALSE");
2314 return ret ? bytes_written : -1;
2317 static int rpcrt4_ncacn_http_close(RpcConnection *Connection)
2319 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2321 TRACE("\n");
2323 SetEvent(httpc->timer_cancelled);
2324 if (httpc->in_request)
2325 InternetCloseHandle(httpc->in_request);
2326 httpc->in_request = NULL;
2327 if (httpc->out_request)
2328 InternetCloseHandle(httpc->out_request);
2329 httpc->out_request = NULL;
2330 if (httpc->app_info)
2331 InternetCloseHandle(httpc->app_info);
2332 httpc->app_info = NULL;
2333 if (httpc->session)
2334 InternetCloseHandle(httpc->session);
2335 httpc->session = NULL;
2336 RpcHttpAsyncData_Release(httpc->async_data);
2337 if (httpc->cancel_event)
2338 CloseHandle(httpc->cancel_event);
2340 return 0;
2343 static void rpcrt4_ncacn_http_cancel_call(RpcConnection *Connection)
2345 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2347 SetEvent(httpc->cancel_event);
2350 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection *Connection)
2352 BOOL ret;
2353 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2355 RpcHttpAsyncData_AddRef(httpc->async_data);
2356 ret = InternetQueryDataAvailable(httpc->out_request,
2357 &httpc->async_data->inet_buffers.dwBufferLength, IRF_ASYNC, 0);
2358 if (ret)
2360 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2361 * async ref now */
2362 RpcHttpAsyncData_Release(httpc->async_data);
2364 else
2366 if (GetLastError() == ERROR_IO_PENDING)
2368 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2369 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2370 if (result != WAIT_OBJECT_0)
2372 TRACE("call cancelled\n");
2373 return -1;
2376 else
2378 RpcHttpAsyncData_Release(httpc->async_data);
2379 return -1;
2383 /* success */
2384 return 0;
2387 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data,
2388 const char *networkaddr,
2389 const char *endpoint)
2391 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
2392 EPM_PROTOCOL_HTTP, endpoint);
2395 static RPC_STATUS rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data,
2396 size_t tower_size,
2397 char **networkaddr,
2398 char **endpoint)
2400 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
2401 networkaddr, EPM_PROTOCOL_HTTP,
2402 endpoint);
2405 static const struct connection_ops conn_protseq_list[] = {
2406 { "ncacn_np",
2407 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
2408 rpcrt4_conn_np_alloc,
2409 rpcrt4_ncacn_np_open,
2410 rpcrt4_ncacn_np_handoff,
2411 rpcrt4_conn_np_read,
2412 rpcrt4_conn_np_write,
2413 rpcrt4_conn_np_close,
2414 rpcrt4_conn_np_cancel_call,
2415 rpcrt4_conn_np_wait_for_incoming_data,
2416 rpcrt4_ncacn_np_get_top_of_tower,
2417 rpcrt4_ncacn_np_parse_top_of_tower,
2418 NULL,
2420 { "ncalrpc",
2421 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
2422 rpcrt4_conn_np_alloc,
2423 rpcrt4_ncalrpc_open,
2424 rpcrt4_ncalrpc_handoff,
2425 rpcrt4_conn_np_read,
2426 rpcrt4_conn_np_write,
2427 rpcrt4_conn_np_close,
2428 rpcrt4_conn_np_cancel_call,
2429 rpcrt4_conn_np_wait_for_incoming_data,
2430 rpcrt4_ncalrpc_get_top_of_tower,
2431 rpcrt4_ncalrpc_parse_top_of_tower,
2432 NULL,
2434 #ifdef HAVE_SOCKETPAIR
2435 { "ncacn_ip_tcp",
2436 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
2437 rpcrt4_conn_tcp_alloc,
2438 rpcrt4_ncacn_ip_tcp_open,
2439 rpcrt4_conn_tcp_handoff,
2440 rpcrt4_conn_tcp_read,
2441 rpcrt4_conn_tcp_write,
2442 rpcrt4_conn_tcp_close,
2443 rpcrt4_conn_tcp_cancel_call,
2444 rpcrt4_conn_tcp_wait_for_incoming_data,
2445 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
2446 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
2447 NULL,
2449 #endif
2450 { "ncacn_http",
2451 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP },
2452 rpcrt4_ncacn_http_alloc,
2453 rpcrt4_ncacn_http_open,
2454 rpcrt4_ncacn_http_handoff,
2455 rpcrt4_ncacn_http_read,
2456 rpcrt4_ncacn_http_write,
2457 rpcrt4_ncacn_http_close,
2458 rpcrt4_ncacn_http_cancel_call,
2459 rpcrt4_ncacn_http_wait_for_incoming_data,
2460 rpcrt4_ncacn_http_get_top_of_tower,
2461 rpcrt4_ncacn_http_parse_top_of_tower,
2462 rpcrt4_ncacn_http_receive_fragment,
2467 static const struct protseq_ops protseq_list[] =
2470 "ncacn_np",
2471 rpcrt4_protseq_np_alloc,
2472 rpcrt4_protseq_np_signal_state_changed,
2473 rpcrt4_protseq_np_get_wait_array,
2474 rpcrt4_protseq_np_free_wait_array,
2475 rpcrt4_protseq_np_wait_for_new_connection,
2476 rpcrt4_protseq_ncacn_np_open_endpoint,
2479 "ncalrpc",
2480 rpcrt4_protseq_np_alloc,
2481 rpcrt4_protseq_np_signal_state_changed,
2482 rpcrt4_protseq_np_get_wait_array,
2483 rpcrt4_protseq_np_free_wait_array,
2484 rpcrt4_protseq_np_wait_for_new_connection,
2485 rpcrt4_protseq_ncalrpc_open_endpoint,
2487 #ifdef HAVE_SOCKETPAIR
2489 "ncacn_ip_tcp",
2490 rpcrt4_protseq_sock_alloc,
2491 rpcrt4_protseq_sock_signal_state_changed,
2492 rpcrt4_protseq_sock_get_wait_array,
2493 rpcrt4_protseq_sock_free_wait_array,
2494 rpcrt4_protseq_sock_wait_for_new_connection,
2495 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
2497 #endif
2500 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2502 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
2504 unsigned int i;
2505 for(i=0; i<ARRAYSIZE(protseq_list); i++)
2506 if (!strcmp(protseq_list[i].name, protseq))
2507 return &protseq_list[i];
2508 return NULL;
2511 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
2513 unsigned int i;
2514 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
2515 if (!strcmp(conn_protseq_list[i].name, protseq))
2516 return &conn_protseq_list[i];
2517 return NULL;
2520 /**** interface to rest of code ****/
2522 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
2524 TRACE("(Connection == ^%p)\n", Connection);
2526 assert(!Connection->server);
2527 return Connection->ops->open_connection_client(Connection);
2530 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
2532 TRACE("(Connection == ^%p)\n", Connection);
2533 if (SecIsValidHandle(&Connection->ctx))
2535 DeleteSecurityContext(&Connection->ctx);
2536 SecInvalidateHandle(&Connection->ctx);
2538 rpcrt4_conn_close(Connection);
2539 return RPC_S_OK;
2542 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
2543 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
2544 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
2546 const struct connection_ops *ops;
2547 RpcConnection* NewConnection;
2549 ops = rpcrt4_get_conn_protseq_ops(Protseq);
2550 if (!ops)
2552 FIXME("not supported for protseq %s\n", Protseq);
2553 return RPC_S_PROTSEQ_NOT_SUPPORTED;
2556 NewConnection = ops->alloc();
2557 NewConnection->Next = NULL;
2558 NewConnection->server_binding = NULL;
2559 NewConnection->server = server;
2560 NewConnection->ops = ops;
2561 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
2562 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
2563 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
2564 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
2565 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
2566 NewConnection->NextCallId = 1;
2568 SecInvalidateHandle(&NewConnection->ctx);
2569 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
2570 NewConnection->attr = 0;
2571 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
2572 NewConnection->AuthInfo = AuthInfo;
2573 NewConnection->encryption_auth_len = 0;
2574 NewConnection->signature_auth_len = 0;
2575 if (QOS) RpcQualityOfService_AddRef(QOS);
2576 NewConnection->QOS = QOS;
2578 list_init(&NewConnection->conn_pool_entry);
2579 NewConnection->async_state = NULL;
2581 TRACE("connection: %p\n", NewConnection);
2582 *Connection = NewConnection;
2584 return RPC_S_OK;
2587 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
2589 RPC_STATUS err;
2591 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
2592 rpcrt4_conn_get_name(OldConnection),
2593 OldConnection->NetworkAddr,
2594 OldConnection->Endpoint, NULL,
2595 OldConnection->AuthInfo, OldConnection->QOS);
2596 if (err == RPC_S_OK)
2597 rpcrt4_conn_handoff(OldConnection, *Connection);
2598 return err;
2601 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
2603 TRACE("connection: %p\n", Connection);
2605 RPCRT4_CloseConnection(Connection);
2606 RPCRT4_strfree(Connection->Endpoint);
2607 RPCRT4_strfree(Connection->NetworkAddr);
2608 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
2609 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
2610 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
2612 /* server-only */
2613 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
2615 HeapFree(GetProcessHeap(), 0, Connection);
2616 return RPC_S_OK;
2619 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
2620 size_t *tower_size,
2621 const char *protseq,
2622 const char *networkaddr,
2623 const char *endpoint)
2625 twr_empty_floor_t *protocol_floor;
2626 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
2628 *tower_size = 0;
2630 if (!protseq_ops)
2631 return RPC_S_INVALID_RPC_PROTSEQ;
2633 if (!tower_data)
2635 *tower_size = sizeof(*protocol_floor);
2636 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
2637 return RPC_S_OK;
2640 protocol_floor = (twr_empty_floor_t *)tower_data;
2641 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
2642 protocol_floor->protid = protseq_ops->epm_protocols[0];
2643 protocol_floor->count_rhs = 0;
2645 tower_data += sizeof(*protocol_floor);
2647 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
2648 if (!*tower_size)
2649 return EPT_S_NOT_REGISTERED;
2651 *tower_size += sizeof(*protocol_floor);
2653 return RPC_S_OK;
2656 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
2657 size_t tower_size,
2658 char **protseq,
2659 char **networkaddr,
2660 char **endpoint)
2662 const twr_empty_floor_t *protocol_floor;
2663 const twr_empty_floor_t *floor4;
2664 const struct connection_ops *protseq_ops = NULL;
2665 RPC_STATUS status;
2666 unsigned int i;
2668 if (tower_size < sizeof(*protocol_floor))
2669 return EPT_S_NOT_REGISTERED;
2671 protocol_floor = (const twr_empty_floor_t *)tower_data;
2672 tower_data += sizeof(*protocol_floor);
2673 tower_size -= sizeof(*protocol_floor);
2674 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
2675 (protocol_floor->count_rhs > tower_size))
2676 return EPT_S_NOT_REGISTERED;
2677 tower_data += protocol_floor->count_rhs;
2678 tower_size -= protocol_floor->count_rhs;
2680 floor4 = (const twr_empty_floor_t *)tower_data;
2681 if ((tower_size < sizeof(*floor4)) ||
2682 (floor4->count_lhs != sizeof(floor4->protid)))
2683 return EPT_S_NOT_REGISTERED;
2685 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
2686 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
2687 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
2689 protseq_ops = &conn_protseq_list[i];
2690 break;
2693 if (!protseq_ops)
2694 return EPT_S_NOT_REGISTERED;
2696 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
2698 if ((status == RPC_S_OK) && protseq)
2700 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
2701 strcpy(*protseq, protseq_ops->name);
2704 return status;
2707 /***********************************************************************
2708 * RpcNetworkIsProtseqValidW (RPCRT4.@)
2710 * Checks if the given protocol sequence is known by the RPC system.
2711 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
2714 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
2716 char ps[0x10];
2718 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
2719 ps, sizeof ps, NULL, NULL);
2720 if (rpcrt4_get_conn_protseq_ops(ps))
2721 return RPC_S_OK;
2723 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
2725 return RPC_S_INVALID_RPC_PROTSEQ;
2728 /***********************************************************************
2729 * RpcNetworkIsProtseqValidA (RPCRT4.@)
2731 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
2733 UNICODE_STRING protseqW;
2735 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
2737 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
2738 RtlFreeUnicodeString(&protseqW);
2739 return ret;
2741 return RPC_S_OUT_OF_MEMORY;