rpcrt4: Implement RPC over HTTP support.
[wine/multimedia.git] / dlls / rpcrt4 / rpc_transport.c
blob7de623045fdac1963853ef37909c7c537c444821
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, LPSTR 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, LPSTR 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 #ifdef HAVE_SOCKETPAIR
767 typedef struct _RpcConnection_tcp
769 RpcConnection common;
770 int sock;
771 int cancel_fds[2];
772 } RpcConnection_tcp;
774 static RpcConnection *rpcrt4_conn_tcp_alloc(void)
776 RpcConnection_tcp *tcpc;
777 tcpc = HeapAlloc(GetProcessHeap(), 0, sizeof(RpcConnection_tcp));
778 if (tcpc == NULL)
779 return NULL;
780 tcpc->sock = -1;
781 if (socketpair(PF_UNIX, SOCK_STREAM, 0, tcpc->cancel_fds) < 0)
783 ERR("socketpair() failed: %s\n", strerror(errno));
784 HeapFree(GetProcessHeap(), 0, tcpc);
785 return NULL;
787 return &tcpc->common;
790 static RPC_STATUS rpcrt4_ncacn_ip_tcp_open(RpcConnection* Connection)
792 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
793 int sock;
794 int ret;
795 struct addrinfo *ai;
796 struct addrinfo *ai_cur;
797 struct addrinfo hints;
799 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
801 if (tcpc->sock != -1)
802 return RPC_S_OK;
804 hints.ai_flags = 0;
805 hints.ai_family = PF_UNSPEC;
806 hints.ai_socktype = SOCK_STREAM;
807 hints.ai_protocol = IPPROTO_TCP;
808 hints.ai_addrlen = 0;
809 hints.ai_addr = NULL;
810 hints.ai_canonname = NULL;
811 hints.ai_next = NULL;
813 ret = getaddrinfo(Connection->NetworkAddr, Connection->Endpoint, &hints, &ai);
814 if (ret)
816 ERR("getaddrinfo for %s:%s failed: %s\n", Connection->NetworkAddr,
817 Connection->Endpoint, gai_strerror(ret));
818 return RPC_S_SERVER_UNAVAILABLE;
821 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
823 int val;
825 if (TRACE_ON(rpc))
827 char host[256];
828 char service[256];
829 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
830 host, sizeof(host), service, sizeof(service),
831 NI_NUMERICHOST | NI_NUMERICSERV);
832 TRACE("trying %s:%s\n", host, service);
835 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
836 if (sock == -1)
838 WARN("socket() failed: %s\n", strerror(errno));
839 continue;
842 if (0>connect(sock, ai_cur->ai_addr, ai_cur->ai_addrlen))
844 WARN("connect() failed: %s\n", strerror(errno));
845 closesocket(sock);
846 continue;
849 /* RPC depends on having minimal latency so disable the Nagle algorithm */
850 val = 1;
851 setsockopt(sock, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
852 fcntl(sock, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
854 tcpc->sock = sock;
856 freeaddrinfo(ai);
857 TRACE("connected\n");
858 return RPC_S_OK;
861 freeaddrinfo(ai);
862 ERR("couldn't connect to %s:%s\n", Connection->NetworkAddr, Connection->Endpoint);
863 return RPC_S_SERVER_UNAVAILABLE;
866 static RPC_STATUS rpcrt4_protseq_ncacn_ip_tcp_open_endpoint(RpcServerProtseq *protseq, LPSTR endpoint)
868 RPC_STATUS status = RPC_S_CANT_CREATE_ENDPOINT;
869 int sock;
870 int ret;
871 struct addrinfo *ai;
872 struct addrinfo *ai_cur;
873 struct addrinfo hints;
874 RpcConnection *first_connection = NULL;
876 TRACE("(%p, %s)\n", protseq, endpoint);
878 hints.ai_flags = AI_PASSIVE /* for non-localhost addresses */;
879 hints.ai_family = PF_UNSPEC;
880 hints.ai_socktype = SOCK_STREAM;
881 hints.ai_protocol = IPPROTO_TCP;
882 hints.ai_addrlen = 0;
883 hints.ai_addr = NULL;
884 hints.ai_canonname = NULL;
885 hints.ai_next = NULL;
887 ret = getaddrinfo(NULL, endpoint, &hints, &ai);
888 if (ret)
890 ERR("getaddrinfo for port %s failed: %s\n", endpoint,
891 gai_strerror(ret));
892 if ((ret == EAI_SERVICE) || (ret == EAI_NONAME))
893 return RPC_S_INVALID_ENDPOINT_FORMAT;
894 return RPC_S_CANT_CREATE_ENDPOINT;
897 for (ai_cur = ai; ai_cur; ai_cur = ai_cur->ai_next)
899 RpcConnection_tcp *tcpc;
900 RPC_STATUS create_status;
902 if (TRACE_ON(rpc))
904 char host[256];
905 char service[256];
906 getnameinfo(ai_cur->ai_addr, ai_cur->ai_addrlen,
907 host, sizeof(host), service, sizeof(service),
908 NI_NUMERICHOST | NI_NUMERICSERV);
909 TRACE("trying %s:%s\n", host, service);
912 sock = socket(ai_cur->ai_family, ai_cur->ai_socktype, ai_cur->ai_protocol);
913 if (sock == -1)
915 WARN("socket() failed: %s\n", strerror(errno));
916 status = RPC_S_CANT_CREATE_ENDPOINT;
917 continue;
920 ret = bind(sock, ai_cur->ai_addr, ai_cur->ai_addrlen);
921 if (ret < 0)
923 WARN("bind failed: %s\n", strerror(errno));
924 closesocket(sock);
925 if (errno == EADDRINUSE)
926 status = RPC_S_DUPLICATE_ENDPOINT;
927 else
928 status = RPC_S_CANT_CREATE_ENDPOINT;
929 continue;
931 create_status = RPCRT4_CreateConnection((RpcConnection **)&tcpc, TRUE,
932 protseq->Protseq, NULL,
933 endpoint, NULL, NULL, NULL);
934 if (create_status != RPC_S_OK)
936 closesocket(sock);
937 status = create_status;
938 continue;
941 tcpc->sock = sock;
942 ret = listen(sock, protseq->MaxCalls);
943 if (ret < 0)
945 WARN("listen failed: %s\n", strerror(errno));
946 RPCRT4_DestroyConnection(&tcpc->common);
947 status = RPC_S_OUT_OF_RESOURCES;
948 continue;
950 /* need a non-blocking socket, otherwise accept() has a potential
951 * race-condition (poll() says it is readable, connection drops,
952 * and accept() blocks until the next connection comes...)
954 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
955 if (ret < 0)
957 WARN("couldn't make socket non-blocking, error %d\n", ret);
958 RPCRT4_DestroyConnection(&tcpc->common);
959 status = RPC_S_OUT_OF_RESOURCES;
960 continue;
963 tcpc->common.Next = first_connection;
964 first_connection = &tcpc->common;
967 freeaddrinfo(ai);
969 /* if at least one connection was created for an endpoint then
970 * return success */
971 if (first_connection)
973 RpcConnection *conn;
975 /* find last element in list */
976 for (conn = first_connection; conn->Next; conn = conn->Next)
979 EnterCriticalSection(&protseq->cs);
980 conn->Next = protseq->conn;
981 protseq->conn = first_connection;
982 LeaveCriticalSection(&protseq->cs);
984 TRACE("listening on %s\n", endpoint);
985 return RPC_S_OK;
988 ERR("couldn't listen on port %s\n", endpoint);
989 return status;
992 static RPC_STATUS rpcrt4_conn_tcp_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
994 int ret;
995 struct sockaddr_in address;
996 socklen_t addrsize;
997 RpcConnection_tcp *server = (RpcConnection_tcp*) old_conn;
998 RpcConnection_tcp *client = (RpcConnection_tcp*) new_conn;
1000 addrsize = sizeof(address);
1001 ret = accept(server->sock, (struct sockaddr*) &address, &addrsize);
1002 if (ret < 0)
1004 ERR("Failed to accept a TCP connection: error %d\n", ret);
1005 return RPC_S_OUT_OF_RESOURCES;
1007 /* reset to blocking behaviour */
1008 fcntl(ret, F_SETFL, 0);
1009 client->sock = ret;
1010 TRACE("Accepted a new TCP connection\n");
1011 return RPC_S_OK;
1014 static int rpcrt4_conn_tcp_read(RpcConnection *Connection,
1015 void *buffer, unsigned int count)
1017 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1018 int bytes_read = 0;
1021 int r = recv(tcpc->sock, (char *)buffer + bytes_read, count - bytes_read, 0);
1022 if (!r)
1023 return -1;
1024 else if (r > 0)
1025 bytes_read += r;
1026 else if (errno != EAGAIN)
1028 WARN("recv() failed: %s\n", strerror(errno));
1029 return -1;
1031 else
1033 struct pollfd pfds[2];
1034 pfds[0].fd = tcpc->sock;
1035 pfds[0].events = POLLIN;
1036 pfds[1].fd = tcpc->cancel_fds[0];
1037 pfds[1].events = POLLIN;
1038 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1040 ERR("poll() failed: %s\n", strerror(errno));
1041 return -1;
1043 if (pfds[1].revents & POLLIN) /* canceled */
1045 char dummy;
1046 read(pfds[1].fd, &dummy, sizeof(dummy));
1047 return -1;
1050 } while (bytes_read != count);
1051 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_read);
1052 return bytes_read;
1055 static int rpcrt4_conn_tcp_write(RpcConnection *Connection,
1056 const void *buffer, unsigned int count)
1058 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1059 int bytes_written = 0;
1062 int r = send(tcpc->sock, (const char *)buffer + bytes_written, count - bytes_written, 0);
1063 if (r >= 0)
1064 bytes_written += r;
1065 else if (errno != EAGAIN)
1066 return -1;
1067 else
1069 struct pollfd pfd;
1070 pfd.fd = tcpc->sock;
1071 pfd.events = POLLOUT;
1072 if (poll(&pfd, 1, -1 /* infinite */) == -1 && errno != EINTR)
1074 ERR("poll() failed: %s\n", strerror(errno));
1075 return -1;
1078 } while (bytes_written != count);
1079 TRACE("%d %p %u -> %d\n", tcpc->sock, buffer, count, bytes_written);
1080 return bytes_written;
1083 static int rpcrt4_conn_tcp_close(RpcConnection *Connection)
1085 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1087 TRACE("%d\n", tcpc->sock);
1089 if (tcpc->sock != -1)
1090 closesocket(tcpc->sock);
1091 tcpc->sock = -1;
1092 close(tcpc->cancel_fds[0]);
1093 close(tcpc->cancel_fds[1]);
1094 return 0;
1097 static void rpcrt4_conn_tcp_cancel_call(RpcConnection *Connection)
1099 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1100 char dummy = 1;
1102 TRACE("%p\n", Connection);
1104 write(tcpc->cancel_fds[1], &dummy, 1);
1107 static int rpcrt4_conn_tcp_wait_for_incoming_data(RpcConnection *Connection)
1109 RpcConnection_tcp *tcpc = (RpcConnection_tcp *) Connection;
1110 struct pollfd pfds[2];
1112 TRACE("%p\n", Connection);
1114 pfds[0].fd = tcpc->sock;
1115 pfds[0].events = POLLIN;
1116 pfds[1].fd = tcpc->cancel_fds[0];
1117 pfds[1].events = POLLIN;
1118 if (poll(pfds, 2, -1 /* infinite */) == -1 && errno != EINTR)
1120 ERR("poll() failed: %s\n", strerror(errno));
1121 return -1;
1123 if (pfds[1].revents & POLLIN) /* canceled */
1125 char dummy;
1126 read(pfds[1].fd, &dummy, sizeof(dummy));
1127 return -1;
1130 return 0;
1133 static size_t rpcrt4_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1134 const char *networkaddr,
1135 unsigned char tcp_protid,
1136 const char *endpoint)
1138 twr_tcp_floor_t *tcp_floor;
1139 twr_ipv4_floor_t *ipv4_floor;
1140 struct addrinfo *ai;
1141 struct addrinfo hints;
1142 int ret;
1143 size_t size = sizeof(*tcp_floor) + sizeof(*ipv4_floor);
1145 TRACE("(%p, %s, %s)\n", tower_data, networkaddr, endpoint);
1147 if (!tower_data)
1148 return size;
1150 tcp_floor = (twr_tcp_floor_t *)tower_data;
1151 tower_data += sizeof(*tcp_floor);
1153 ipv4_floor = (twr_ipv4_floor_t *)tower_data;
1155 tcp_floor->count_lhs = sizeof(tcp_floor->protid);
1156 tcp_floor->protid = tcp_protid;
1157 tcp_floor->count_rhs = sizeof(tcp_floor->port);
1159 ipv4_floor->count_lhs = sizeof(ipv4_floor->protid);
1160 ipv4_floor->protid = EPM_PROTOCOL_IP;
1161 ipv4_floor->count_rhs = sizeof(ipv4_floor->ipv4addr);
1163 hints.ai_flags = AI_NUMERICHOST;
1164 /* FIXME: only support IPv4 at the moment. how is IPv6 represented by the EPM? */
1165 hints.ai_family = PF_INET;
1166 hints.ai_socktype = SOCK_STREAM;
1167 hints.ai_protocol = IPPROTO_TCP;
1168 hints.ai_addrlen = 0;
1169 hints.ai_addr = NULL;
1170 hints.ai_canonname = NULL;
1171 hints.ai_next = NULL;
1173 ret = getaddrinfo(networkaddr, endpoint, &hints, &ai);
1174 if (ret)
1176 ret = getaddrinfo("0.0.0.0", endpoint, &hints, &ai);
1177 if (ret)
1179 ERR("getaddrinfo failed: %s\n", gai_strerror(ret));
1180 return 0;
1184 if (ai->ai_family == PF_INET)
1186 const struct sockaddr_in *sin = (const struct sockaddr_in *)ai->ai_addr;
1187 tcp_floor->port = sin->sin_port;
1188 ipv4_floor->ipv4addr = sin->sin_addr.s_addr;
1190 else
1192 ERR("unexpected protocol family %d\n", ai->ai_family);
1193 return 0;
1196 freeaddrinfo(ai);
1198 return size;
1201 static size_t rpcrt4_ncacn_ip_tcp_get_top_of_tower(unsigned char *tower_data,
1202 const char *networkaddr,
1203 const char *endpoint)
1205 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
1206 EPM_PROTOCOL_TCP, endpoint);
1209 static RPC_STATUS rpcrt4_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1210 size_t tower_size,
1211 char **networkaddr,
1212 unsigned char tcp_protid,
1213 char **endpoint)
1215 const twr_tcp_floor_t *tcp_floor = (const twr_tcp_floor_t *)tower_data;
1216 const twr_ipv4_floor_t *ipv4_floor;
1217 struct in_addr in_addr;
1219 TRACE("(%p, %d, %p, %p)\n", tower_data, (int)tower_size, networkaddr, endpoint);
1221 if (tower_size < sizeof(*tcp_floor))
1222 return EPT_S_NOT_REGISTERED;
1224 tower_data += sizeof(*tcp_floor);
1225 tower_size -= sizeof(*tcp_floor);
1227 if (tower_size < sizeof(*ipv4_floor))
1228 return EPT_S_NOT_REGISTERED;
1230 ipv4_floor = (const twr_ipv4_floor_t *)tower_data;
1232 if ((tcp_floor->count_lhs != sizeof(tcp_floor->protid)) ||
1233 (tcp_floor->protid != tcp_protid) ||
1234 (tcp_floor->count_rhs != sizeof(tcp_floor->port)) ||
1235 (ipv4_floor->count_lhs != sizeof(ipv4_floor->protid)) ||
1236 (ipv4_floor->protid != EPM_PROTOCOL_IP) ||
1237 (ipv4_floor->count_rhs != sizeof(ipv4_floor->ipv4addr)))
1238 return EPT_S_NOT_REGISTERED;
1240 if (endpoint)
1242 *endpoint = I_RpcAllocate(6 /* sizeof("65535") + 1 */);
1243 if (!*endpoint)
1244 return RPC_S_OUT_OF_RESOURCES;
1245 sprintf(*endpoint, "%u", ntohs(tcp_floor->port));
1248 if (networkaddr)
1250 *networkaddr = I_RpcAllocate(INET_ADDRSTRLEN);
1251 if (!*networkaddr)
1253 if (endpoint)
1255 I_RpcFree(*endpoint);
1256 *endpoint = NULL;
1258 return RPC_S_OUT_OF_RESOURCES;
1260 in_addr.s_addr = ipv4_floor->ipv4addr;
1261 if (!inet_ntop(AF_INET, &in_addr, *networkaddr, INET_ADDRSTRLEN))
1263 ERR("inet_ntop: %s\n", strerror(errno));
1264 I_RpcFree(*networkaddr);
1265 *networkaddr = NULL;
1266 if (endpoint)
1268 I_RpcFree(*endpoint);
1269 *endpoint = NULL;
1271 return EPT_S_NOT_REGISTERED;
1275 return RPC_S_OK;
1278 typedef struct _RpcServerProtseq_sock
1280 RpcServerProtseq common;
1281 int mgr_event_rcv;
1282 int mgr_event_snd;
1283 } RpcServerProtseq_sock;
1285 static RpcServerProtseq *rpcrt4_protseq_sock_alloc(void)
1287 RpcServerProtseq_sock *ps = HeapAlloc(GetProcessHeap(), 0, sizeof(*ps));
1288 if (ps)
1290 int fds[2];
1291 if (!socketpair(PF_UNIX, SOCK_DGRAM, 0, fds))
1293 fcntl(fds[0], F_SETFL, O_NONBLOCK);
1294 fcntl(fds[1], F_SETFL, O_NONBLOCK);
1295 ps->mgr_event_rcv = fds[0];
1296 ps->mgr_event_snd = fds[1];
1298 else
1300 ERR("socketpair failed with error %s\n", strerror(errno));
1301 HeapFree(GetProcessHeap(), 0, ps);
1302 return NULL;
1305 return &ps->common;
1308 static void rpcrt4_protseq_sock_signal_state_changed(RpcServerProtseq *protseq)
1310 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1311 char dummy = 1;
1312 write(sockps->mgr_event_snd, &dummy, sizeof(dummy));
1315 static void *rpcrt4_protseq_sock_get_wait_array(RpcServerProtseq *protseq, void *prev_array, unsigned int *count)
1317 struct pollfd *poll_info = prev_array;
1318 RpcConnection_tcp *conn;
1319 RpcServerProtseq_sock *sockps = CONTAINING_RECORD(protseq, RpcServerProtseq_sock, common);
1321 EnterCriticalSection(&protseq->cs);
1323 /* open and count connections */
1324 *count = 1;
1325 conn = (RpcConnection_tcp *)protseq->conn;
1326 while (conn) {
1327 if (conn->sock != -1)
1328 (*count)++;
1329 conn = (RpcConnection_tcp *)conn->common.Next;
1332 /* make array of connections */
1333 if (poll_info)
1334 poll_info = HeapReAlloc(GetProcessHeap(), 0, poll_info, *count*sizeof(*poll_info));
1335 else
1336 poll_info = HeapAlloc(GetProcessHeap(), 0, *count*sizeof(*poll_info));
1337 if (!poll_info)
1339 ERR("couldn't allocate poll_info\n");
1340 LeaveCriticalSection(&protseq->cs);
1341 return NULL;
1344 poll_info[0].fd = sockps->mgr_event_rcv;
1345 poll_info[0].events = POLLIN;
1346 *count = 1;
1347 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1348 while (conn) {
1349 if (conn->sock != -1)
1351 poll_info[*count].fd = conn->sock;
1352 poll_info[*count].events = POLLIN;
1353 (*count)++;
1355 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1357 LeaveCriticalSection(&protseq->cs);
1358 return poll_info;
1361 static void rpcrt4_protseq_sock_free_wait_array(RpcServerProtseq *protseq, void *array)
1363 HeapFree(GetProcessHeap(), 0, array);
1366 static int rpcrt4_protseq_sock_wait_for_new_connection(RpcServerProtseq *protseq, unsigned int count, void *wait_array)
1368 struct pollfd *poll_info = wait_array;
1369 int ret;
1370 unsigned int i;
1371 RpcConnection *cconn;
1372 RpcConnection_tcp *conn;
1374 if (!poll_info)
1375 return -1;
1377 ret = poll(poll_info, count, -1);
1378 if (ret < 0)
1380 ERR("poll failed with error %d\n", ret);
1381 return -1;
1384 for (i = 0; i < count; i++)
1385 if (poll_info[i].revents & POLLIN)
1387 /* RPC server event */
1388 if (i == 0)
1390 char dummy;
1391 read(poll_info[0].fd, &dummy, sizeof(dummy));
1392 return 0;
1395 /* find which connection got a RPC */
1396 EnterCriticalSection(&protseq->cs);
1397 conn = CONTAINING_RECORD(protseq->conn, RpcConnection_tcp, common);
1398 while (conn) {
1399 if (poll_info[i].fd == conn->sock) break;
1400 conn = CONTAINING_RECORD(conn->common.Next, RpcConnection_tcp, common);
1402 cconn = NULL;
1403 if (conn)
1404 RPCRT4_SpawnConnection(&cconn, &conn->common);
1405 else
1406 ERR("failed to locate connection for fd %d\n", poll_info[i].fd);
1407 LeaveCriticalSection(&protseq->cs);
1408 if (cconn)
1409 RPCRT4_new_client(cconn);
1410 else
1411 return -1;
1414 return 1;
1417 #endif /* HAVE_SOCKETPAIR */
1419 static RPC_STATUS rpcrt4_ncacn_ip_tcp_parse_top_of_tower(const unsigned char *tower_data,
1420 size_t tower_size,
1421 char **networkaddr,
1422 char **endpoint)
1424 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
1425 networkaddr, EPM_PROTOCOL_TCP,
1426 endpoint);
1429 /**** ncacn_http support ****/
1431 /* 60 seconds is the period native uses */
1432 #define HTTP_IDLE_TIME 60000
1434 /* reference counted to avoid a race between a cancelled call's connection
1435 * being destroyed and the asynchronous InternetReadFileEx call being
1436 * completed */
1437 typedef struct _RpcHttpAsyncData
1439 LONG refs;
1440 HANDLE completion_event;
1441 INTERNET_BUFFERSA inet_buffers;
1442 void *destination_buffer; /* the address that inet_buffers.lpvBuffer will be
1443 * copied into when the call completes */
1444 CRITICAL_SECTION cs;
1445 } RpcHttpAsyncData;
1447 static ULONG RpcHttpAsyncData_AddRef(RpcHttpAsyncData *data)
1449 return InterlockedIncrement(&data->refs);
1452 static ULONG RpcHttpAsyncData_Release(RpcHttpAsyncData *data)
1454 ULONG refs = InterlockedDecrement(&data->refs);
1455 if (!refs)
1457 TRACE("destroying async data %p\n", data);
1458 CloseHandle(data->completion_event);
1459 HeapFree(GetProcessHeap(), 0, data->inet_buffers.lpvBuffer);
1460 DeleteCriticalSection(&data->cs);
1461 HeapFree(GetProcessHeap(), 0, data);
1463 return refs;
1466 typedef struct _RpcConnection_http
1468 RpcConnection common;
1469 HINTERNET app_info;
1470 HINTERNET session;
1471 HINTERNET in_request;
1472 HINTERNET out_request;
1473 HANDLE timer_cancelled;
1474 HANDLE cancel_event;
1475 DWORD last_sent_time;
1476 ULONG bytes_received;
1477 ULONG flow_control_mark; /* send a control packet to the server when this many bytes received */
1478 ULONG flow_control_increment; /* number of bytes to increment flow_control_mark by */
1479 UUID connection_uuid;
1480 UUID in_pipe_uuid;
1481 UUID out_pipe_uuid;
1482 RpcHttpAsyncData *async_data;
1483 } RpcConnection_http;
1485 static RpcConnection *rpcrt4_ncacn_http_alloc(void)
1487 RpcConnection_http *httpc;
1488 httpc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*httpc));
1489 if (!httpc) return NULL;
1490 httpc->async_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcHttpAsyncData));
1491 if (!httpc->async_data)
1493 HeapFree(GetProcessHeap(), 0, httpc);
1494 return NULL;
1496 TRACE("async data = %p\n", httpc->async_data);
1497 httpc->cancel_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1498 httpc->async_data->refs = 1;
1499 httpc->async_data->inet_buffers.dwStructSize = sizeof(INTERNET_BUFFERSA);
1500 httpc->async_data->inet_buffers.lpvBuffer = NULL;
1501 httpc->async_data->destination_buffer = NULL;
1502 InitializeCriticalSection(&httpc->async_data->cs);
1503 return &httpc->common;
1506 typedef struct _HttpTimerThreadData
1508 PVOID timer_param;
1509 DWORD *last_sent_time;
1510 HANDLE timer_cancelled;
1511 } HttpTimerThreadData;
1513 static VOID CALLBACK rpcrt4_http_keep_connection_active_timer_proc(PVOID param, BOOLEAN dummy)
1515 HINTERNET in_request = param;
1516 RpcPktHdr *idle_pkt;
1518 idle_pkt = RPCRT4_BuildHttpHeader(NDR_LOCAL_DATA_REPRESENTATION, 0x0001,
1519 0, 0);
1520 if (idle_pkt)
1522 DWORD bytes_written;
1523 InternetWriteFile(in_request, idle_pkt, idle_pkt->common.frag_len, &bytes_written);
1524 RPCRT4_FreeHeader(idle_pkt);
1528 static inline DWORD rpcrt4_http_timer_calc_timeout(DWORD *last_sent_time)
1530 DWORD cur_time = GetTickCount();
1531 DWORD cached_last_sent_time = *last_sent_time;
1532 return HTTP_IDLE_TIME - (cur_time - cached_last_sent_time > HTTP_IDLE_TIME ? 0 : cur_time - cached_last_sent_time);
1535 static DWORD CALLBACK rpcrt4_http_timer_thread(PVOID param)
1537 HttpTimerThreadData *data_in = param;
1538 HttpTimerThreadData data;
1539 DWORD timeout;
1541 data = *data_in;
1542 HeapFree(GetProcessHeap(), 0, data_in);
1544 for (timeout = HTTP_IDLE_TIME;
1545 WaitForSingleObject(data.timer_cancelled, timeout) == WAIT_TIMEOUT;
1546 timeout = rpcrt4_http_timer_calc_timeout(data.last_sent_time))
1548 /* are we too soon after last send? */
1549 if (GetTickCount() - HTTP_IDLE_TIME < *data.last_sent_time)
1550 continue;
1551 rpcrt4_http_keep_connection_active_timer_proc(data.timer_param, TRUE);
1554 CloseHandle(data.timer_cancelled);
1555 return 0;
1558 static VOID WINAPI rpcrt4_http_internet_callback(
1559 HINTERNET hInternet,
1560 DWORD_PTR dwContext,
1561 DWORD dwInternetStatus,
1562 LPVOID lpvStatusInformation,
1563 DWORD dwStatusInformationLength)
1565 RpcHttpAsyncData *async_data = (RpcHttpAsyncData *)dwContext;
1567 switch (dwInternetStatus)
1569 case INTERNET_STATUS_REQUEST_COMPLETE:
1570 TRACE("INTERNET_STATUS_REQUEST_COMPLETED\n");
1571 if (async_data)
1573 if (async_data->inet_buffers.lpvBuffer)
1575 EnterCriticalSection(&async_data->cs);
1576 if (async_data->destination_buffer)
1578 memcpy(async_data->destination_buffer,
1579 async_data->inet_buffers.lpvBuffer,
1580 async_data->inet_buffers.dwBufferLength);
1581 async_data->destination_buffer = NULL;
1583 LeaveCriticalSection(&async_data->cs);
1585 HeapFree(GetProcessHeap(), 0, async_data->inet_buffers.lpvBuffer);
1586 async_data->inet_buffers.lpvBuffer = NULL;
1587 SetEvent(async_data->completion_event);
1588 RpcHttpAsyncData_Release(async_data);
1590 break;
1594 static RPC_STATUS rpcrt4_http_check_response(HINTERNET hor)
1596 BOOL ret;
1597 DWORD status_code;
1598 DWORD size;
1599 DWORD index;
1600 WCHAR status_text[32];
1602 TRACE("\n");
1604 index = 0;
1605 size = sizeof(status_code);
1606 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &status_code, &size, &index);
1607 if (!ret)
1608 return GetLastError();
1609 if (status_code < 400)
1610 return RPC_S_OK;
1611 index = 0;
1612 size = sizeof(status_text);
1613 ret = HttpQueryInfoW(hor, HTTP_QUERY_STATUS_TEXT, status_text, &size, &index);
1614 if (!ret)
1615 return GetLastError();
1616 ERR("server returned: %d %s\n", status_code, debugstr_w(status_text));
1617 if (status_code == HTTP_STATUS_DENIED)
1618 return ERROR_ACCESS_DENIED;
1619 return RPC_S_SERVER_UNAVAILABLE;
1622 static RPC_STATUS rpcrt4_http_internet_connect(RpcConnection_http *httpc)
1624 static const WCHAR wszUserAgent[] = {'M','S','R','P','C',0};
1625 LPWSTR proxy = NULL;
1626 LPWSTR user = NULL;
1627 LPWSTR password = NULL;
1628 LPWSTR servername = NULL;
1629 const WCHAR *option;
1630 INTERNET_PORT port = INTERNET_INVALID_PORT_NUMBER; /* use default port */
1632 if (httpc->common.QOS &&
1633 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP))
1635 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_cred = httpc->common.QOS->qos->u.HttpCredentials;
1636 if (http_cred->TransportCredentials)
1638 WCHAR *p;
1639 const SEC_WINNT_AUTH_IDENTITY_W *cred = http_cred->TransportCredentials;
1640 ULONG len = cred->DomainLength + 1 + cred->UserLength;
1641 user = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1642 if (!user)
1643 return RPC_S_OUT_OF_RESOURCES;
1644 p = user;
1645 if (cred->DomainLength)
1647 memcpy(p, cred->Domain, cred->DomainLength * sizeof(WCHAR));
1648 p += cred->DomainLength;
1649 *p = '\\';
1650 p++;
1652 memcpy(p, cred->User, cred->UserLength * sizeof(WCHAR));
1653 p[cred->UserLength] = 0;
1655 password = RPCRT4_strndupW(cred->Password, cred->PasswordLength);
1659 for (option = httpc->common.NetworkOptions; option;
1660 option = (strchrW(option, ',') ? strchrW(option, ',')+1 : NULL))
1662 static const WCHAR wszRpcProxy[] = {'R','p','c','P','r','o','x','y','=',0};
1663 static const WCHAR wszHttpProxy[] = {'H','t','t','p','P','r','o','x','y','=',0};
1665 if (!strncmpiW(option, wszRpcProxy, sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1))
1667 const WCHAR *value_start = option + sizeof(wszRpcProxy)/sizeof(wszRpcProxy[0])-1;
1668 const WCHAR *value_end;
1669 const WCHAR *p;
1671 value_end = strchrW(option, ',');
1672 if (!value_end)
1673 value_end = value_start + strlenW(value_start);
1674 for (p = value_start; p < value_end; p++)
1675 if (*p == ':')
1677 port = atoiW(p+1);
1678 value_end = p;
1679 break;
1681 TRACE("RpcProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
1682 servername = RPCRT4_strndupW(value_start, value_end-value_start);
1684 else if (!strncmpiW(option, wszHttpProxy, sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1))
1686 const WCHAR *value_start = option + sizeof(wszHttpProxy)/sizeof(wszHttpProxy[0])-1;
1687 const WCHAR *value_end;
1689 value_end = strchrW(option, ',');
1690 if (!value_end)
1691 value_end = value_start + strlenW(value_start);
1692 TRACE("HttpProxy value is %s\n", debugstr_wn(value_start, value_end-value_start));
1693 proxy = RPCRT4_strndupW(value_start, value_end-value_start);
1695 else
1696 FIXME("unhandled option %s\n", debugstr_w(option));
1699 httpc->app_info = InternetOpenW(wszUserAgent, proxy ? INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_PRECONFIG,
1700 NULL, NULL, INTERNET_FLAG_ASYNC);
1701 if (!httpc->app_info)
1703 HeapFree(GetProcessHeap(), 0, password);
1704 HeapFree(GetProcessHeap(), 0, user);
1705 ERR("InternetOpenW failed with error %d\n", GetLastError());
1706 return RPC_S_SERVER_UNAVAILABLE;
1708 InternetSetStatusCallbackW(httpc->app_info, rpcrt4_http_internet_callback);
1710 /* if no RpcProxy option specified, set the HTTP server address to the
1711 * RPC server address */
1712 if (!servername)
1714 servername = HeapAlloc(GetProcessHeap(), 0, (strlen(httpc->common.NetworkAddr) + 1)*sizeof(WCHAR));
1715 if (!servername)
1717 HeapFree(GetProcessHeap(), 0, password);
1718 HeapFree(GetProcessHeap(), 0, user);
1719 return RPC_S_OUT_OF_RESOURCES;
1721 MultiByteToWideChar(CP_ACP, 0, httpc->common.NetworkAddr, -1, servername, strlen(httpc->common.NetworkAddr) + 1);
1724 httpc->session = InternetConnectW(httpc->app_info, servername, port, user, password,
1725 INTERNET_SERVICE_HTTP, 0, 0);
1727 HeapFree(GetProcessHeap(), 0, password);
1728 HeapFree(GetProcessHeap(), 0, user);
1729 HeapFree(GetProcessHeap(), 0, servername);
1731 if (!httpc->session)
1733 ERR("InternetConnectW failed with error %d\n", GetLastError());
1734 return RPC_S_SERVER_UNAVAILABLE;
1737 return RPC_S_OK;
1740 /* prepare the in pipe for use by RPC packets */
1741 static RPC_STATUS rpcrt4_http_prepare_in_pipe(HINTERNET in_request, RpcHttpAsyncData *async_data,
1742 const UUID *connection_uuid,
1743 const UUID *in_pipe_uuid,
1744 const UUID *association_uuid)
1746 BYTE packet[44];
1747 BOOL ret;
1748 RPC_STATUS status;
1749 RpcPktHdr *hdr;
1750 INTERNET_BUFFERSW buffers_in;
1751 DWORD bytes_read, bytes_written;
1753 /* prepare in pipe */
1754 ResetEvent(async_data->completion_event);
1755 RpcHttpAsyncData_AddRef(async_data);
1756 ret = HttpSendRequestW(in_request, NULL, 0, NULL, 0);
1757 if (!ret)
1759 if (GetLastError() == ERROR_IO_PENDING)
1760 WaitForSingleObject(async_data->completion_event, INFINITE);
1761 else
1763 RpcHttpAsyncData_Release(async_data);
1764 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1765 return RPC_S_SERVER_UNAVAILABLE;
1768 status = rpcrt4_http_check_response(in_request);
1769 if (status != RPC_S_OK) return status;
1771 InternetReadFile(in_request, packet, 20, &bytes_read);
1772 /* FIXME: do something with retrieved data */
1774 memset(&buffers_in, 0, sizeof(buffers_in));
1775 buffers_in.dwStructSize = sizeof(buffers_in);
1776 /* FIXME: get this from the registry */
1777 buffers_in.dwBufferTotal = 1024 * 1024 * 1024; /* 1Gb */
1778 ResetEvent(async_data->completion_event);
1779 RpcHttpAsyncData_AddRef(async_data);
1780 ret = HttpSendRequestExW(in_request, &buffers_in, NULL, 0, 0);
1781 if (!ret)
1783 if (GetLastError() == ERROR_IO_PENDING)
1784 WaitForSingleObject(async_data->completion_event, INFINITE);
1785 else
1787 RpcHttpAsyncData_Release(async_data);
1788 ERR("HttpSendRequestExW failed with error %d\n", GetLastError());
1789 return RPC_S_SERVER_UNAVAILABLE;
1793 TRACE("sending HTTP connect header to server\n");
1794 hdr = RPCRT4_BuildHttpConnectHeader(0, FALSE, connection_uuid, in_pipe_uuid, association_uuid);
1795 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
1796 ret = InternetWriteFile(in_request, hdr, hdr->common.frag_len, &bytes_written);
1797 RPCRT4_FreeHeader(hdr);
1798 if (!ret)
1800 ERR("InternetWriteFile failed with error %d\n", GetLastError());
1801 return RPC_S_SERVER_UNAVAILABLE;
1804 return RPC_S_OK;
1807 static RPC_STATUS rpcrt4_http_read_http_packet(HINTERNET request, RpcPktHdr *hdr, BYTE **data)
1809 BOOL ret;
1810 DWORD bytes_read;
1811 unsigned short data_len;
1813 ret = InternetReadFile(request, hdr, sizeof(hdr->common), &bytes_read);
1814 if (!ret)
1815 return RPC_S_SERVER_UNAVAILABLE;
1816 if (hdr->common.ptype != PKT_HTTP || hdr->common.frag_len < sizeof(hdr->http))
1818 ERR("wrong packet type received %d or wrong frag_len %d\n",
1819 hdr->common.ptype, hdr->common.frag_len);
1820 return RPC_S_PROTOCOL_ERROR;
1823 ret = InternetReadFile(request, &hdr->common + 1, sizeof(hdr->http) - sizeof(hdr->common), &bytes_read);
1824 if (!ret)
1825 return RPC_S_SERVER_UNAVAILABLE;
1827 data_len = hdr->common.frag_len - sizeof(hdr->http);
1828 if (data_len)
1830 *data = HeapAlloc(GetProcessHeap(), 0, data_len);
1831 if (!*data)
1832 return RPC_S_OUT_OF_RESOURCES;
1833 ret = InternetReadFile(request, *data, data_len, &bytes_read);
1834 if (!ret)
1836 HeapFree(GetProcessHeap(), 0, *data);
1837 return RPC_S_SERVER_UNAVAILABLE;
1840 else
1841 *data = NULL;
1843 if (!RPCRT4_IsValidHttpPacket(hdr, *data, data_len))
1845 ERR("invalid http packet\n");
1846 return RPC_S_PROTOCOL_ERROR;
1849 return RPC_S_OK;
1852 /* prepare the out pipe for use by RPC packets */
1853 static RPC_STATUS rpcrt4_http_prepare_out_pipe(HINTERNET out_request,
1854 RpcHttpAsyncData *async_data,
1855 const UUID *connection_uuid,
1856 const UUID *out_pipe_uuid,
1857 ULONG *flow_control_increment)
1859 BYTE packet[20];
1860 BOOL ret;
1861 RPC_STATUS status;
1862 RpcPktHdr *hdr;
1863 DWORD bytes_read;
1864 BYTE *data_from_server;
1865 RpcPktHdr pkt_from_server;
1866 ULONG field1, field3;
1868 ResetEvent(async_data->completion_event);
1869 RpcHttpAsyncData_AddRef(async_data);
1870 ret = HttpSendRequestW(out_request, NULL, 0, NULL, 0);
1871 if (!ret)
1873 if (GetLastError() == ERROR_IO_PENDING)
1874 WaitForSingleObject(async_data->completion_event, INFINITE);
1875 else
1877 RpcHttpAsyncData_Release(async_data);
1878 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1879 return RPC_S_SERVER_UNAVAILABLE;
1882 status = rpcrt4_http_check_response(out_request);
1883 if (status != RPC_S_OK) return status;
1885 InternetReadFile(out_request, packet, 20, &bytes_read);
1886 /* FIXME: do something with retrieved data */
1888 hdr = RPCRT4_BuildHttpConnectHeader(0, TRUE, connection_uuid, out_pipe_uuid, NULL);
1889 if (!hdr) return RPC_S_OUT_OF_RESOURCES;
1890 ResetEvent(async_data->completion_event);
1891 RpcHttpAsyncData_AddRef(async_data);
1892 ret = HttpSendRequestW(out_request, NULL, 0, hdr, hdr->common.frag_len);
1893 if (!ret)
1895 if (GetLastError() == ERROR_IO_PENDING)
1896 WaitForSingleObject(async_data->completion_event, INFINITE);
1897 else
1899 RpcHttpAsyncData_Release(async_data);
1900 ERR("HttpSendRequestW failed with error %d\n", GetLastError());
1901 RPCRT4_FreeHeader(hdr);
1902 return RPC_S_SERVER_UNAVAILABLE;
1905 RPCRT4_FreeHeader(hdr);
1906 status = rpcrt4_http_check_response(out_request);
1907 if (status != RPC_S_OK) return status;
1909 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
1910 &data_from_server);
1911 if (status != RPC_S_OK) return status;
1912 status = RPCRT4_ParseHttpPrepareHeader1(&pkt_from_server, data_from_server,
1913 &field1);
1914 HeapFree(GetProcessHeap(), 0, data_from_server);
1915 if (status != RPC_S_OK) return status;
1916 TRACE("received (%d) from first prepare header\n", field1);
1918 status = rpcrt4_http_read_http_packet(out_request, &pkt_from_server,
1919 &data_from_server);
1920 if (status != RPC_S_OK) return status;
1921 status = RPCRT4_ParseHttpPrepareHeader2(&pkt_from_server, data_from_server,
1922 &field1, flow_control_increment,
1923 &field3);
1924 HeapFree(GetProcessHeap(), 0, data_from_server);
1925 if (status != RPC_S_OK) return status;
1926 TRACE("received (0x%08x 0x%08x %d) from second prepare header\n", field1, *flow_control_increment, field3);
1928 return RPC_S_OK;
1931 static RPC_STATUS rpcrt4_ncacn_http_open(RpcConnection* Connection)
1933 RpcConnection_http *httpc = (RpcConnection_http *)Connection;
1934 static const WCHAR wszVerbIn[] = {'R','P','C','_','I','N','_','D','A','T','A',0};
1935 static const WCHAR wszVerbOut[] = {'R','P','C','_','O','U','T','_','D','A','T','A',0};
1936 static const WCHAR wszRpcProxyPrefix[] = {'/','r','p','c','/','r','p','c','p','r','o','x','y','.','d','l','l','?',0};
1937 static const WCHAR wszColon[] = {':',0};
1938 static const WCHAR wszAcceptType[] = {'a','p','p','l','i','c','a','t','i','o','n','/','r','p','c',0};
1939 LPCWSTR wszAcceptTypes[] = { wszAcceptType, NULL };
1940 WCHAR *url;
1941 RPC_STATUS status;
1942 BOOL secure;
1943 HttpTimerThreadData *timer_data;
1944 HANDLE thread;
1946 TRACE("(%s, %s)\n", Connection->NetworkAddr, Connection->Endpoint);
1948 if (Connection->server)
1950 ERR("ncacn_http servers not supported yet\n");
1951 return RPC_S_SERVER_UNAVAILABLE;
1954 if (httpc->in_request)
1955 return RPC_S_OK;
1957 httpc->async_data->completion_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1959 status = UuidCreate(&httpc->connection_uuid);
1960 status = UuidCreate(&httpc->in_pipe_uuid);
1961 status = UuidCreate(&httpc->out_pipe_uuid);
1963 status = rpcrt4_http_internet_connect(httpc);
1964 if (status != RPC_S_OK)
1965 return status;
1967 url = HeapAlloc(GetProcessHeap(), 0, sizeof(wszRpcProxyPrefix) + (strlen(Connection->NetworkAddr) + 1 + strlen(Connection->Endpoint))*sizeof(WCHAR));
1968 if (!url)
1969 return RPC_S_OUT_OF_MEMORY;
1970 memcpy(url, wszRpcProxyPrefix, sizeof(wszRpcProxyPrefix));
1971 MultiByteToWideChar(CP_ACP, 0, Connection->NetworkAddr, -1, url+sizeof(wszRpcProxyPrefix)/sizeof(wszRpcProxyPrefix[0])-1, strlen(Connection->NetworkAddr)+1);
1972 strcatW(url, wszColon);
1973 MultiByteToWideChar(CP_ACP, 0, Connection->Endpoint, -1, url+strlenW(url), strlen(Connection->Endpoint)+1);
1975 secure = httpc->common.QOS &&
1976 (httpc->common.QOS->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP) &&
1977 (httpc->common.QOS->qos->u.HttpCredentials->Flags & RPC_C_HTTP_FLAG_USE_SSL);
1979 httpc->in_request = HttpOpenRequestW(httpc->session, wszVerbIn, url, NULL, NULL,
1980 wszAcceptTypes,
1981 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
1982 (DWORD_PTR)httpc->async_data);
1983 if (!httpc->in_request)
1985 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
1986 return RPC_S_SERVER_UNAVAILABLE;
1988 httpc->out_request = HttpOpenRequestW(httpc->session, wszVerbOut, url, NULL, NULL,
1989 wszAcceptTypes,
1990 (secure ? INTERNET_FLAG_SECURE : 0)|INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_PRAGMA_NOCACHE,
1991 (DWORD_PTR)httpc->async_data);
1992 if (!httpc->out_request)
1994 ERR("HttpOpenRequestW failed with error %d\n", GetLastError());
1995 return RPC_S_SERVER_UNAVAILABLE;
1998 status = rpcrt4_http_prepare_in_pipe(httpc->in_request,
1999 httpc->async_data,
2000 &httpc->connection_uuid,
2001 &httpc->in_pipe_uuid,
2002 &Connection->assoc->http_uuid);
2003 if (status != RPC_S_OK)
2004 return status;
2006 status = rpcrt4_http_prepare_out_pipe(httpc->out_request,
2007 httpc->async_data,
2008 &httpc->connection_uuid,
2009 &httpc->out_pipe_uuid,
2010 &httpc->flow_control_increment);
2011 if (status != RPC_S_OK)
2012 return status;
2014 httpc->flow_control_mark = httpc->flow_control_increment / 2;
2015 httpc->last_sent_time = GetTickCount();
2016 httpc->timer_cancelled = CreateEventW(NULL, FALSE, FALSE, NULL);
2018 timer_data = HeapAlloc(GetProcessHeap(), 0, sizeof(*timer_data));
2019 if (!timer_data)
2020 return ERROR_OUTOFMEMORY;
2021 timer_data->timer_param = httpc->in_request;
2022 timer_data->last_sent_time = &httpc->last_sent_time;
2023 timer_data->timer_cancelled = httpc->timer_cancelled;
2024 /* FIXME: should use CreateTimerQueueTimer when implemented */
2025 thread = CreateThread(NULL, 0, rpcrt4_http_timer_thread, timer_data, 0, NULL);
2026 if (!thread)
2028 HeapFree(GetProcessHeap(), 0, timer_data);
2029 return GetLastError();
2031 CloseHandle(thread);
2033 return RPC_S_OK;
2036 static RPC_STATUS rpcrt4_ncacn_http_handoff(RpcConnection *old_conn, RpcConnection *new_conn)
2038 assert(0);
2039 return RPC_S_SERVER_UNAVAILABLE;
2042 static int rpcrt4_ncacn_http_read(RpcConnection *Connection,
2043 void *buffer, unsigned int count)
2045 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2046 char *buf = buffer;
2047 BOOL ret = TRUE;
2048 unsigned int bytes_left = count;
2050 ResetEvent(httpc->async_data->completion_event);
2051 while (bytes_left)
2053 RpcHttpAsyncData_AddRef(httpc->async_data);
2054 httpc->async_data->inet_buffers.dwBufferLength = bytes_left;
2055 httpc->async_data->inet_buffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, bytes_left);
2056 httpc->async_data->destination_buffer = buf;
2057 ret = InternetReadFileExA(httpc->out_request, &httpc->async_data->inet_buffers, IRF_ASYNC, 0);
2058 if (ret)
2060 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2061 * async ref now */
2062 RpcHttpAsyncData_Release(httpc->async_data);
2063 memcpy(buf, httpc->async_data->inet_buffers.lpvBuffer,
2064 httpc->async_data->inet_buffers.dwBufferLength);
2065 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2066 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2067 httpc->async_data->destination_buffer = NULL;
2069 else
2071 if (GetLastError() == ERROR_IO_PENDING)
2073 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2074 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2075 if (result == WAIT_OBJECT_0)
2076 ret = TRUE;
2077 else
2079 TRACE("call cancelled\n");
2080 EnterCriticalSection(&httpc->async_data->cs);
2081 httpc->async_data->destination_buffer = NULL;
2082 LeaveCriticalSection(&httpc->async_data->cs);
2083 break;
2086 else
2088 HeapFree(GetProcessHeap(), 0, httpc->async_data->inet_buffers.lpvBuffer);
2089 httpc->async_data->inet_buffers.lpvBuffer = NULL;
2090 httpc->async_data->destination_buffer = NULL;
2091 RpcHttpAsyncData_Release(httpc->async_data);
2092 break;
2095 if (!httpc->async_data->inet_buffers.dwBufferLength)
2096 break;
2097 bytes_left -= httpc->async_data->inet_buffers.dwBufferLength;
2098 buf += httpc->async_data->inet_buffers.dwBufferLength;
2100 TRACE("%p %p %u -> %s\n", httpc->out_request, buffer, count, ret ? "TRUE" : "FALSE");
2101 return ret ? count : -1;
2104 static RPC_STATUS rpcrt4_ncacn_http_receive_fragment(RpcConnection *Connection, RpcPktHdr **Header, void **Payload)
2106 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2107 RPC_STATUS status;
2108 DWORD hdr_length;
2109 LONG dwRead;
2110 RpcPktCommonHdr common_hdr;
2112 *Header = NULL;
2114 TRACE("(%p, %p, %p)\n", Connection, Header, Payload);
2116 again:
2117 /* read packet common header */
2118 dwRead = rpcrt4_ncacn_http_read(Connection, &common_hdr, sizeof(common_hdr));
2119 if (dwRead != sizeof(common_hdr)) {
2120 WARN("Short read of header, %d bytes\n", dwRead);
2121 status = RPC_S_PROTOCOL_ERROR;
2122 goto fail;
2124 if (!memcmp(&common_hdr, "HTTP/1.1", sizeof("HTTP/1.1")) ||
2125 !memcmp(&common_hdr, "HTTP/1.0", sizeof("HTTP/1.0")))
2127 FIXME("server returned %s\n", debugstr_a((const char *)&common_hdr));
2128 status = RPC_S_PROTOCOL_ERROR;
2129 goto fail;
2132 status = RPCRT4_ValidateCommonHeader(&common_hdr);
2133 if (status != RPC_S_OK) goto fail;
2135 hdr_length = RPCRT4_GetHeaderSize((RpcPktHdr*)&common_hdr);
2136 if (hdr_length == 0) {
2137 WARN("header length == 0\n");
2138 status = RPC_S_PROTOCOL_ERROR;
2139 goto fail;
2142 *Header = HeapAlloc(GetProcessHeap(), 0, hdr_length);
2143 if (!*Header)
2145 status = RPC_S_OUT_OF_RESOURCES;
2146 goto fail;
2148 memcpy(*Header, &common_hdr, sizeof(common_hdr));
2150 /* read the rest of packet header */
2151 dwRead = rpcrt4_ncacn_http_read(Connection, &(*Header)->common + 1, hdr_length - sizeof(common_hdr));
2152 if (dwRead != hdr_length - sizeof(common_hdr)) {
2153 WARN("bad header length, %d bytes, hdr_length %d\n", dwRead, hdr_length);
2154 status = RPC_S_PROTOCOL_ERROR;
2155 goto fail;
2158 if (common_hdr.frag_len - hdr_length)
2160 *Payload = HeapAlloc(GetProcessHeap(), 0, common_hdr.frag_len - hdr_length);
2161 if (!*Payload)
2163 status = RPC_S_OUT_OF_RESOURCES;
2164 goto fail;
2167 dwRead = rpcrt4_ncacn_http_read(Connection, *Payload, common_hdr.frag_len - hdr_length);
2168 if (dwRead != common_hdr.frag_len - hdr_length)
2170 WARN("bad data length, %d/%d\n", dwRead, common_hdr.frag_len - hdr_length);
2171 status = RPC_S_PROTOCOL_ERROR;
2172 goto fail;
2175 else
2176 *Payload = NULL;
2178 if ((*Header)->common.ptype == PKT_HTTP)
2180 if (!RPCRT4_IsValidHttpPacket(*Header, *Payload, common_hdr.frag_len - hdr_length))
2182 ERR("invalid http packet of length %d bytes\n", (*Header)->common.frag_len);
2183 status = RPC_S_PROTOCOL_ERROR;
2184 goto fail;
2186 if ((*Header)->http.flags == 0x0001)
2188 TRACE("http idle packet, waiting for real packet\n");
2189 if ((*Header)->http.num_data_items != 0)
2191 ERR("HTTP idle packet should have no data items instead of %d\n", (*Header)->http.num_data_items);
2192 status = RPC_S_PROTOCOL_ERROR;
2193 goto fail;
2196 else if ((*Header)->http.flags == 0x0002)
2198 ULONG bytes_transmitted;
2199 ULONG flow_control_increment;
2200 UUID pipe_uuid;
2201 status = RPCRT4_ParseHttpFlowControlHeader(*Header, *Payload,
2202 Connection->server,
2203 &bytes_transmitted,
2204 &flow_control_increment,
2205 &pipe_uuid);
2206 if (status != RPC_S_OK)
2207 goto fail;
2208 TRACE("received http flow control header (0x%x, 0x%x, %s)\n",
2209 bytes_transmitted, flow_control_increment, debugstr_guid(&pipe_uuid));
2210 /* FIXME: do something with parsed data */
2212 else
2214 FIXME("unrecognised http packet with flags 0x%04x\n", (*Header)->http.flags);
2215 status = RPC_S_PROTOCOL_ERROR;
2216 goto fail;
2218 RPCRT4_FreeHeader(*Header);
2219 *Header = NULL;
2220 HeapFree(GetProcessHeap(), 0, *Payload);
2221 *Payload = NULL;
2222 goto again;
2225 /* success */
2226 status = RPC_S_OK;
2228 httpc->bytes_received += common_hdr.frag_len;
2230 TRACE("httpc->bytes_received = 0x%x\n", httpc->bytes_received);
2232 if (httpc->bytes_received > httpc->flow_control_mark)
2234 RpcPktHdr *hdr = RPCRT4_BuildHttpFlowControlHeader(httpc->common.server,
2235 httpc->bytes_received,
2236 httpc->flow_control_increment,
2237 &httpc->out_pipe_uuid);
2238 if (hdr)
2240 DWORD bytes_written;
2241 BOOL ret2;
2242 TRACE("sending flow control packet at 0x%x\n", httpc->bytes_received);
2243 ret2 = InternetWriteFile(httpc->in_request, hdr, hdr->common.frag_len, &bytes_written);
2244 RPCRT4_FreeHeader(hdr);
2245 if (ret2)
2246 httpc->flow_control_mark = httpc->bytes_received + httpc->flow_control_increment / 2;
2250 fail:
2251 if (status != RPC_S_OK) {
2252 RPCRT4_FreeHeader(*Header);
2253 *Header = NULL;
2254 HeapFree(GetProcessHeap(), 0, *Payload);
2255 *Payload = NULL;
2257 return status;
2260 static int rpcrt4_ncacn_http_write(RpcConnection *Connection,
2261 const void *buffer, unsigned int count)
2263 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2264 DWORD bytes_written;
2265 BOOL ret;
2267 httpc->last_sent_time = ~0UL; /* disable idle packet sending */
2268 ret = InternetWriteFile(httpc->in_request, buffer, count, &bytes_written);
2269 httpc->last_sent_time = GetTickCount();
2270 TRACE("%p %p %u -> %s\n", httpc->in_request, buffer, count, ret ? "TRUE" : "FALSE");
2271 return ret ? bytes_written : -1;
2274 static int rpcrt4_ncacn_http_close(RpcConnection *Connection)
2276 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2278 TRACE("\n");
2280 SetEvent(httpc->timer_cancelled);
2281 if (httpc->in_request)
2282 InternetCloseHandle(httpc->in_request);
2283 httpc->in_request = NULL;
2284 if (httpc->out_request)
2285 InternetCloseHandle(httpc->out_request);
2286 httpc->out_request = NULL;
2287 if (httpc->app_info)
2288 InternetCloseHandle(httpc->app_info);
2289 httpc->app_info = NULL;
2290 if (httpc->session)
2291 InternetCloseHandle(httpc->session);
2292 httpc->session = NULL;
2293 RpcHttpAsyncData_Release(httpc->async_data);
2294 if (httpc->cancel_event)
2295 CloseHandle(httpc->cancel_event);
2297 return 0;
2300 static void rpcrt4_ncacn_http_cancel_call(RpcConnection *Connection)
2302 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2304 SetEvent(httpc->cancel_event);
2307 static int rpcrt4_ncacn_http_wait_for_incoming_data(RpcConnection *Connection)
2309 BOOL ret;
2310 RpcConnection_http *httpc = (RpcConnection_http *) Connection;
2312 RpcHttpAsyncData_AddRef(httpc->async_data);
2313 ret = InternetQueryDataAvailable(httpc->out_request,
2314 &httpc->async_data->inet_buffers.dwBufferLength, IRF_ASYNC, 0);
2315 if (ret)
2317 /* INTERNET_STATUS_REQUEST_COMPLETED won't be sent, so release our
2318 * async ref now */
2319 RpcHttpAsyncData_Release(httpc->async_data);
2321 else
2323 if (GetLastError() == ERROR_IO_PENDING)
2325 HANDLE handles[2] = { httpc->async_data->completion_event, httpc->cancel_event };
2326 DWORD result = WaitForMultipleObjects(2, handles, FALSE, DEFAULT_NCACN_HTTP_TIMEOUT);
2327 if (result != WAIT_OBJECT_0)
2329 TRACE("call cancelled\n");
2330 return -1;
2333 else
2335 RpcHttpAsyncData_Release(httpc->async_data);
2336 return -1;
2340 /* success */
2341 return 0;
2344 static size_t rpcrt4_ncacn_http_get_top_of_tower(unsigned char *tower_data,
2345 const char *networkaddr,
2346 const char *endpoint)
2348 return rpcrt4_ip_tcp_get_top_of_tower(tower_data, networkaddr,
2349 EPM_PROTOCOL_HTTP, endpoint);
2352 static RPC_STATUS rpcrt4_ncacn_http_parse_top_of_tower(const unsigned char *tower_data,
2353 size_t tower_size,
2354 char **networkaddr,
2355 char **endpoint)
2357 return rpcrt4_ip_tcp_parse_top_of_tower(tower_data, tower_size,
2358 networkaddr, EPM_PROTOCOL_HTTP,
2359 endpoint);
2362 static const struct connection_ops conn_protseq_list[] = {
2363 { "ncacn_np",
2364 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_SMB },
2365 rpcrt4_conn_np_alloc,
2366 rpcrt4_ncacn_np_open,
2367 rpcrt4_ncacn_np_handoff,
2368 rpcrt4_conn_np_read,
2369 rpcrt4_conn_np_write,
2370 rpcrt4_conn_np_close,
2371 rpcrt4_conn_np_cancel_call,
2372 rpcrt4_conn_np_wait_for_incoming_data,
2373 rpcrt4_ncacn_np_get_top_of_tower,
2374 rpcrt4_ncacn_np_parse_top_of_tower,
2375 NULL,
2377 { "ncalrpc",
2378 { EPM_PROTOCOL_NCALRPC, EPM_PROTOCOL_PIPE },
2379 rpcrt4_conn_np_alloc,
2380 rpcrt4_ncalrpc_open,
2381 rpcrt4_ncalrpc_handoff,
2382 rpcrt4_conn_np_read,
2383 rpcrt4_conn_np_write,
2384 rpcrt4_conn_np_close,
2385 rpcrt4_conn_np_cancel_call,
2386 rpcrt4_conn_np_wait_for_incoming_data,
2387 rpcrt4_ncalrpc_get_top_of_tower,
2388 rpcrt4_ncalrpc_parse_top_of_tower,
2389 NULL,
2391 #ifdef HAVE_SOCKETPAIR
2392 { "ncacn_ip_tcp",
2393 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_TCP },
2394 rpcrt4_conn_tcp_alloc,
2395 rpcrt4_ncacn_ip_tcp_open,
2396 rpcrt4_conn_tcp_handoff,
2397 rpcrt4_conn_tcp_read,
2398 rpcrt4_conn_tcp_write,
2399 rpcrt4_conn_tcp_close,
2400 rpcrt4_conn_tcp_cancel_call,
2401 rpcrt4_conn_tcp_wait_for_incoming_data,
2402 rpcrt4_ncacn_ip_tcp_get_top_of_tower,
2403 rpcrt4_ncacn_ip_tcp_parse_top_of_tower,
2404 NULL,
2406 #endif
2407 { "ncacn_http",
2408 { EPM_PROTOCOL_NCACN, EPM_PROTOCOL_HTTP },
2409 rpcrt4_ncacn_http_alloc,
2410 rpcrt4_ncacn_http_open,
2411 rpcrt4_ncacn_http_handoff,
2412 rpcrt4_ncacn_http_read,
2413 rpcrt4_ncacn_http_write,
2414 rpcrt4_ncacn_http_close,
2415 rpcrt4_ncacn_http_cancel_call,
2416 rpcrt4_ncacn_http_wait_for_incoming_data,
2417 rpcrt4_ncacn_http_get_top_of_tower,
2418 rpcrt4_ncacn_http_parse_top_of_tower,
2419 rpcrt4_ncacn_http_receive_fragment,
2424 static const struct protseq_ops protseq_list[] =
2427 "ncacn_np",
2428 rpcrt4_protseq_np_alloc,
2429 rpcrt4_protseq_np_signal_state_changed,
2430 rpcrt4_protseq_np_get_wait_array,
2431 rpcrt4_protseq_np_free_wait_array,
2432 rpcrt4_protseq_np_wait_for_new_connection,
2433 rpcrt4_protseq_ncacn_np_open_endpoint,
2436 "ncalrpc",
2437 rpcrt4_protseq_np_alloc,
2438 rpcrt4_protseq_np_signal_state_changed,
2439 rpcrt4_protseq_np_get_wait_array,
2440 rpcrt4_protseq_np_free_wait_array,
2441 rpcrt4_protseq_np_wait_for_new_connection,
2442 rpcrt4_protseq_ncalrpc_open_endpoint,
2444 #ifdef HAVE_SOCKETPAIR
2446 "ncacn_ip_tcp",
2447 rpcrt4_protseq_sock_alloc,
2448 rpcrt4_protseq_sock_signal_state_changed,
2449 rpcrt4_protseq_sock_get_wait_array,
2450 rpcrt4_protseq_sock_free_wait_array,
2451 rpcrt4_protseq_sock_wait_for_new_connection,
2452 rpcrt4_protseq_ncacn_ip_tcp_open_endpoint,
2454 #endif
2457 #define ARRAYSIZE(a) (sizeof((a)) / sizeof((a)[0]))
2459 const struct protseq_ops *rpcrt4_get_protseq_ops(const char *protseq)
2461 unsigned int i;
2462 for(i=0; i<ARRAYSIZE(protseq_list); i++)
2463 if (!strcmp(protseq_list[i].name, protseq))
2464 return &protseq_list[i];
2465 return NULL;
2468 static const struct connection_ops *rpcrt4_get_conn_protseq_ops(const char *protseq)
2470 unsigned int i;
2471 for(i=0; i<ARRAYSIZE(conn_protseq_list); i++)
2472 if (!strcmp(conn_protseq_list[i].name, protseq))
2473 return &conn_protseq_list[i];
2474 return NULL;
2477 /**** interface to rest of code ****/
2479 RPC_STATUS RPCRT4_OpenClientConnection(RpcConnection* Connection)
2481 TRACE("(Connection == ^%p)\n", Connection);
2483 assert(!Connection->server);
2484 return Connection->ops->open_connection_client(Connection);
2487 RPC_STATUS RPCRT4_CloseConnection(RpcConnection* Connection)
2489 TRACE("(Connection == ^%p)\n", Connection);
2490 if (SecIsValidHandle(&Connection->ctx))
2492 DeleteSecurityContext(&Connection->ctx);
2493 SecInvalidateHandle(&Connection->ctx);
2495 rpcrt4_conn_close(Connection);
2496 return RPC_S_OK;
2499 RPC_STATUS RPCRT4_CreateConnection(RpcConnection** Connection, BOOL server,
2500 LPCSTR Protseq, LPCSTR NetworkAddr, LPCSTR Endpoint,
2501 LPCWSTR NetworkOptions, RpcAuthInfo* AuthInfo, RpcQualityOfService *QOS)
2503 const struct connection_ops *ops;
2504 RpcConnection* NewConnection;
2506 ops = rpcrt4_get_conn_protseq_ops(Protseq);
2507 if (!ops)
2509 FIXME("not supported for protseq %s\n", Protseq);
2510 return RPC_S_PROTSEQ_NOT_SUPPORTED;
2513 NewConnection = ops->alloc();
2514 NewConnection->Next = NULL;
2515 NewConnection->server_binding = NULL;
2516 NewConnection->server = server;
2517 NewConnection->ops = ops;
2518 NewConnection->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
2519 NewConnection->Endpoint = RPCRT4_strdupA(Endpoint);
2520 NewConnection->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
2521 NewConnection->MaxTransmissionSize = RPC_MAX_PACKET_SIZE;
2522 memset(&NewConnection->ActiveInterface, 0, sizeof(NewConnection->ActiveInterface));
2523 NewConnection->NextCallId = 1;
2525 SecInvalidateHandle(&NewConnection->ctx);
2526 memset(&NewConnection->exp, 0, sizeof(NewConnection->exp));
2527 NewConnection->attr = 0;
2528 if (AuthInfo) RpcAuthInfo_AddRef(AuthInfo);
2529 NewConnection->AuthInfo = AuthInfo;
2530 NewConnection->encryption_auth_len = 0;
2531 NewConnection->signature_auth_len = 0;
2532 if (QOS) RpcQualityOfService_AddRef(QOS);
2533 NewConnection->QOS = QOS;
2535 list_init(&NewConnection->conn_pool_entry);
2536 NewConnection->async_state = NULL;
2538 TRACE("connection: %p\n", NewConnection);
2539 *Connection = NewConnection;
2541 return RPC_S_OK;
2544 static RPC_STATUS RPCRT4_SpawnConnection(RpcConnection** Connection, RpcConnection* OldConnection)
2546 RPC_STATUS err;
2548 err = RPCRT4_CreateConnection(Connection, OldConnection->server,
2549 rpcrt4_conn_get_name(OldConnection),
2550 OldConnection->NetworkAddr,
2551 OldConnection->Endpoint, NULL,
2552 OldConnection->AuthInfo, OldConnection->QOS);
2553 if (err == RPC_S_OK)
2554 rpcrt4_conn_handoff(OldConnection, *Connection);
2555 return err;
2558 RPC_STATUS RPCRT4_DestroyConnection(RpcConnection* Connection)
2560 TRACE("connection: %p\n", Connection);
2562 RPCRT4_CloseConnection(Connection);
2563 RPCRT4_strfree(Connection->Endpoint);
2564 RPCRT4_strfree(Connection->NetworkAddr);
2565 HeapFree(GetProcessHeap(), 0, Connection->NetworkOptions);
2566 if (Connection->AuthInfo) RpcAuthInfo_Release(Connection->AuthInfo);
2567 if (Connection->QOS) RpcQualityOfService_Release(Connection->QOS);
2569 /* server-only */
2570 if (Connection->server_binding) RPCRT4_ReleaseBinding(Connection->server_binding);
2572 HeapFree(GetProcessHeap(), 0, Connection);
2573 return RPC_S_OK;
2576 RPC_STATUS RpcTransport_GetTopOfTower(unsigned char *tower_data,
2577 size_t *tower_size,
2578 const char *protseq,
2579 const char *networkaddr,
2580 const char *endpoint)
2582 twr_empty_floor_t *protocol_floor;
2583 const struct connection_ops *protseq_ops = rpcrt4_get_conn_protseq_ops(protseq);
2585 *tower_size = 0;
2587 if (!protseq_ops)
2588 return RPC_S_INVALID_RPC_PROTSEQ;
2590 if (!tower_data)
2592 *tower_size = sizeof(*protocol_floor);
2593 *tower_size += protseq_ops->get_top_of_tower(NULL, networkaddr, endpoint);
2594 return RPC_S_OK;
2597 protocol_floor = (twr_empty_floor_t *)tower_data;
2598 protocol_floor->count_lhs = sizeof(protocol_floor->protid);
2599 protocol_floor->protid = protseq_ops->epm_protocols[0];
2600 protocol_floor->count_rhs = 0;
2602 tower_data += sizeof(*protocol_floor);
2604 *tower_size = protseq_ops->get_top_of_tower(tower_data, networkaddr, endpoint);
2605 if (!*tower_size)
2606 return EPT_S_NOT_REGISTERED;
2608 *tower_size += sizeof(*protocol_floor);
2610 return RPC_S_OK;
2613 RPC_STATUS RpcTransport_ParseTopOfTower(const unsigned char *tower_data,
2614 size_t tower_size,
2615 char **protseq,
2616 char **networkaddr,
2617 char **endpoint)
2619 const twr_empty_floor_t *protocol_floor;
2620 const twr_empty_floor_t *floor4;
2621 const struct connection_ops *protseq_ops = NULL;
2622 RPC_STATUS status;
2623 unsigned int i;
2625 if (tower_size < sizeof(*protocol_floor))
2626 return EPT_S_NOT_REGISTERED;
2628 protocol_floor = (const twr_empty_floor_t *)tower_data;
2629 tower_data += sizeof(*protocol_floor);
2630 tower_size -= sizeof(*protocol_floor);
2631 if ((protocol_floor->count_lhs != sizeof(protocol_floor->protid)) ||
2632 (protocol_floor->count_rhs > tower_size))
2633 return EPT_S_NOT_REGISTERED;
2634 tower_data += protocol_floor->count_rhs;
2635 tower_size -= protocol_floor->count_rhs;
2637 floor4 = (const twr_empty_floor_t *)tower_data;
2638 if ((tower_size < sizeof(*floor4)) ||
2639 (floor4->count_lhs != sizeof(floor4->protid)))
2640 return EPT_S_NOT_REGISTERED;
2642 for(i = 0; i < ARRAYSIZE(conn_protseq_list); i++)
2643 if ((protocol_floor->protid == conn_protseq_list[i].epm_protocols[0]) &&
2644 (floor4->protid == conn_protseq_list[i].epm_protocols[1]))
2646 protseq_ops = &conn_protseq_list[i];
2647 break;
2650 if (!protseq_ops)
2651 return EPT_S_NOT_REGISTERED;
2653 status = protseq_ops->parse_top_of_tower(tower_data, tower_size, networkaddr, endpoint);
2655 if ((status == RPC_S_OK) && protseq)
2657 *protseq = I_RpcAllocate(strlen(protseq_ops->name) + 1);
2658 strcpy(*protseq, protseq_ops->name);
2661 return status;
2664 /***********************************************************************
2665 * RpcNetworkIsProtseqValidW (RPCRT4.@)
2667 * Checks if the given protocol sequence is known by the RPC system.
2668 * If it is, returns RPC_S_OK, otherwise RPC_S_PROTSEQ_NOT_SUPPORTED.
2671 RPC_STATUS WINAPI RpcNetworkIsProtseqValidW(RPC_WSTR protseq)
2673 char ps[0x10];
2675 WideCharToMultiByte(CP_ACP, 0, protseq, -1,
2676 ps, sizeof ps, NULL, NULL);
2677 if (rpcrt4_get_conn_protseq_ops(ps))
2678 return RPC_S_OK;
2680 FIXME("Unknown protseq %s\n", debugstr_w(protseq));
2682 return RPC_S_INVALID_RPC_PROTSEQ;
2685 /***********************************************************************
2686 * RpcNetworkIsProtseqValidA (RPCRT4.@)
2688 RPC_STATUS WINAPI RpcNetworkIsProtseqValidA(RPC_CSTR protseq)
2690 UNICODE_STRING protseqW;
2692 if (RtlCreateUnicodeStringFromAsciiz(&protseqW, (char*)protseq))
2694 RPC_STATUS ret = RpcNetworkIsProtseqValidW(protseqW.Buffer);
2695 RtlFreeUnicodeString(&protseqW);
2696 return ret;
2698 return RPC_S_OUT_OF_MEMORY;